Microweber\Providers\CategoryManager::save PHP Метод

save() публичный Метод

public save ( $data, $preserve_cache = false )
    public function save($data, $preserve_cache = false)
    {
        $sid = $this->app->user_manager->session_id();
        if (is_string($data)) {
            $data = parse_params($data);
        }
        $orig_data = $data;
        if (isset($data['id']) and $data['id'] == 0 or !isset($data['id'])) {
            if (!isset($data['title']) or isset($data['title']) and $data['title'] == false) {
                return array('error' => 'Title is required');
            }
        } elseif (isset($data['id']) and $data['id'] != 0) {
            if (isset($data['title']) and $data['title'] == false) {
                return array('error' => 'Title cannot be blank');
            }
        }
        if (isset($data['id']) and $data['id'] != 0 and isset($data['parent_id'])) {
            if (intval($data['id']) == intval($data['parent_id'])) {
                return array('error' => 'Invalid parent category');
            }
        }
        $table = $this->tables['categories'];
        $table_items = $this->tables['categories_items'];
        $content_ids = false;
        $simple_save = false;
        if (isset($data['content_id']) and !isset($data['rel_type'])) {
            $data['rel_type'] = 'content';
            $data['rel_id'] = $data['content_id'];
        }
        if (isset($data['rel']) and !isset($data['rel_type'])) {
            $data['rel_type'] = $data['rel'];
        }
        if (!isset($data['data_type']) or !$data['data_type']) {
            $data['data_type'] = 'category';
        }
        if (isset($data['users_can_create_content']) and $data['users_can_create_content'] == 'y') {
            $data['users_can_create_content'] = 1;
        } elseif (isset($data['users_can_create_content']) and $data['users_can_create_content'] == 'n') {
            $data['users_can_create_content'] = 0;
        }
        if (isset($data['rel_type']) and $data['rel_type'] == '' or !isset($data['rel_type'])) {
            $data['rel_type'] = 'content';
        }
        if (isset($data['simple_save'])) {
            $simple_save = $data['simple_save'];
        }
        if (isset($data['content_id'])) {
            if (is_array($data['content_id']) and !empty($data['content_id']) and trim($data['data_type']) != '') {
                $content_ids = $data['content_id'];
            }
        }
        if (isset($data['position'])) {
            $data['position'] = intval($data['position']);
        }
        if (isset($data['category_subtype_settings'])) {
            $data['category_subtype_settings'] = @json_encode($data['category_subtype_settings']);
        }
        $no_position_fix = false;
        if (isset($data['rel_type']) and isset($data['rel_id']) and trim($data['rel_type']) != '' and trim($data['rel_id']) != '') {
            $no_position_fix = true;
        }
        if (isset($data['parent_page'])) {
            $data['rel_type'] = 'content';
            $data['rel_id'] = $data['parent_page'];
        }
        if (isset($data['table']) and $data['table'] != '') {
            $table = $data['table'];
        }
        if (isset($data['id']) and intval($data['id']) != 0 and isset($data['parent_id']) and intval($data['parent_id']) != 0) {
            if ($data['id'] == $data['parent_id']) {
                unset($data['parent_id']);
            }
        } elseif ((!isset($data['id']) or intval($data['id']) == 0) and !isset($data['parent_id'])) {
            $data['parent_id'] = 0;
        }
        if (isset($data['rel_type']) and isset($data['rel_id']) and trim($data['rel_type']) == 'content' and intval($data['rel_id']) != 0) {
            $cont_check = $this->app->content_manager->get_by_id($data['rel_id']);
            if ($cont_check != false and isset($cont_check['subtype']) and $cont_check['subtype'] == 'static') {
                $cs = array();
                $cs['id'] = intval($data['rel_id']);
                $cs['subtype'] = 'dynamic';
                $table_c = $this->tables['content'];
                $save = $this->app->database_manager->save($table_c, $cs);
            }
        }
        if ((!isset($data['id']) or $data['id'] == 0) and !isset($data['is_deleted'])) {
            $data['is_deleted'] = 0;
        }
        if ((!isset($data['id']) or $data['id'] == 0) and (!isset($data['url']) or trim($data['url']) == false) and isset($data['title'])) {
            $data['url'] = $data['title'];
        }
        $old_parent = false;
        if (isset($data['id'])) {
            $old_category = $this->get_by_id($data['id']);
            if (isset($old_category['parent_id'])) {
                $old_parent = $old_category['parent_id'];
            }
        }
        if (isset($data['url']) and trim($data['url']) != false) {
            $possible_slug = $this->app->url_manager->slug($data['url']);
            if ($possible_slug) {
                $possible_slug_check = $this->get_by_slug($possible_slug);
                if (isset($possible_slug_check['id'])) {
                    if (isset($data['id']) and $data['id'] == $possible_slug_check['id']) {
                        //slug is the same
                    } else {
                        $possible_slug = $possible_slug . '-' . date('YmdHis');
                    }
                }
            }
            if ($possible_slug) {
                $data['url'] = $possible_slug;
            } else {
                $data['url'] = false;
            }
        } elseif (isset($data['url']) and trim($data['url']) != false) {
            $data['url'] = false;
        }
        /* if (!empty($orig_data)) {
               $data_str = 'data_';
               $data_str_l = strlen($data_str);
               foreach ($orig_data as $k => $v) {
                   if (is_string($k)) {
                       if (strlen($k) > $data_str_l) {
                           $rest = substr($k, 0, $data_str_l);
                           $left = substr($k, $data_str_l, strlen($k));
                           if ($rest == $data_str) {
                               if (!isset($data['data_fields'])) {
                                   $data['data_fields'] = array();
                               }
                               $data['data_fields'][ $left ] = $v;
                           }
                       }
                   }
               }
           }*/
        $data['allow_html'] = true;
        // \Log::info(print_r($data, true));
        $id = $save = $this->app->database_manager->extended_save($table, $data);
        if ($simple_save == true) {
            return $save;
        }
        if (intval($save) == 0) {
            return false;
        }
        DB::transaction(function () use($sid, $id) {
            DB::table($this->tables['custom_fields'])->whereSessionId($sid)->where(function ($query) {
                $query->whereRelId(0)->orWhere('rel_id', null);
            })->whereRelType('categories')->update(['rel_type' => 'categories', 'rel_id' => $id]);
            DB::table($this->tables['media'])->whereSessionId($sid)->where(function ($query) {
                $query->whereRelId(0)->orWhere('rel_id', null);
            })->whereRelType('categories')->update(['rel_id' => $id]);
        });
        //$this->app->cache_manager->clear('media');
        // $this->app->database_manager->q($clean);
        if (isset($content_ids) and !empty($content_ids)) {
            $content_ids = array_unique($content_ids);
            $data_type = trim($data['data_type']) . '_item';
            $content_ids_all = implode(',', $content_ids);
            $q = "DELETE FROM {$table} WHERE rel_type='content'\n\t\tAND content_type='post'\n\t\tAND parent_id={$save}\n\t\tAND  data_type ='{$data_type}' ";
            $this->app->database_manager->q($q);
            foreach ($content_ids as $id) {
                $item_save = array();
                $item_save['rel_type'] = 'content';
                $item_save['rel_id'] = $id;
                $item_save['data_type'] = $data_type;
                $item_save['content_type'] = 'post';
                $item_save['parent_id'] = intval($save);
                $item_save = $this->app->database_manager->save($table_items, $item_save);
            }
        }
        if ($old_parent != false) {
            // $this->app->cache_manager->clear('categories' . DIRECTORY_SEPARATOR . $old_parent);
        }
        // $this->app->cache_manager->clear('categories');
        return $save;
    }