public function save($data)
 {
     if (!is_array($data)) {
         $data = parse_params($data);
     }
     if (!isset($data['id'])) {
         if (!isset($data['field_name'])) {
             return array('error' => "You must set 'field' parameter");
         }
         if (!isset($data['field_value'])) {
             return array('error' => "You must set 'value' parameter");
         }
     }
     if (!isset($data['rel_type']) and isset($data['content_id'])) {
         $data['rel_type'] = 'content';
         $data['rel_id'] = $data['content_id'];
     }
     if (isset($data['field_name']) and isset($data['rel_id']) and isset($data['rel_type'])) {
         $is_existing_data = array();
         $is_existing_data['field_name'] = $data['field_name'];
         $is_existing_data['rel_id'] = $data['rel_id'];
         $is_existing_data['rel_type'] = $data['rel_type'];
         $is_existing_data['one'] = true;
         $is_existing = $this->get($is_existing_data);
         if (is_array($is_existing) and isset($is_existing['id'])) {
             $data['id'] = $is_existing['id'];
         }
     }
     if (isset($data['content_id'])) {
         $data['rel_id'] = intval($data['content_id']);
     }
     if (isset($data['field_value']) and is_array($data['field_value'])) {
         $data['field_value'] = json_encode($data['field_value']);
     }
     if (!isset($data['rel_type'])) {
         $data['rel_type'] = 'content';
     }
     if (isset($data['rel_type']) and $data['rel_type'] == 'content') {
         if (isset($data['rel_id'])) {
             $data['content_id'] = $data['rel_id'];
         }
     }
     $save = parent::save($data);
     return $save;
 }
 /**
  * Get array of content items from the database.
  *
  * It accepts string or array as parameters. You can pass any db field name as parameter to filter content by it.
  * All parameter are passed to the get() function
  *
  * You can get and filter content and also order the results by criteria
  *
  * @function get_content
  *
  *
  * @desc     Get array of content items from the content DB table
  *
  * @uses     get() You can use all the options of get(), such as limit, order_by, count, etc...
  *
  * @param mixed|array|bool|string $params You can pass parameters as string or as array
  * @params
  *
  * *Some parameters you can use*
  *  You can use all defined database fields as parameters
  *
  * .[params-table]
  *|-----------------------------------------------------------------------------
  *| Field Name          | Description               | Values
  *|------------------------------------------------------------------------------
  *| id                  | the id of the content     |
  *| is_active           | published or unpublished  | "y" or "n"
  *| parent              | get content with parent   | any id or 0
  *| created_by          | get by author id          | any user id
  *| created_at          | the date of creation      |
  *| updated_at          | the date of last edit     |
  *| content_type        | the type of the content   | "page" or "post", anything custom
  *| subtype             | subtype of the content    | "static","dynamic","post","product", anything custom
  *| url                 | the link to the content   |
  *| title               | Title of the content      |
  *| content             | The html content saved in the database |
  *| description         | Description used for the content list |
  *| position            | The order position        |
  *| active_site_template   | Current template for the content |
  *| layout_file         | Current layout from the template directory |
  *| is_deleted          | flag for deleted content  |  "n" or "y"
  *| is_home             | flag for homepage         |  "n" or "y"
  *| is_shop             | flag for shop page        |  "n" or "y"
  *
  * @return array|bool|mixed Array of content or false if nothing is found
  *
  * @example
  * #### Get with parameters as array
  * <code>
  *
  * $params = array();
  * $params['is_active'] = 1; //get only active content
  * $params['parent'] = 2; //get by parent id
  * $params['created_by'] = 1; //get by author id
  * $params['content_type'] = 'post'; //get by content type
  * $params['subtype'] = 'product'; //get by subtype
  * $params['title'] = 'my title'; //get by title
  *
  * $data = $this->get($params);
  * var_dump($data);
  *
  * </code>
  * @example
  * #### Get by params as string
  * <code>
  *  $data = $this->get('is_active=1');
  *  var_dump($data);
  * </code>
  * @example
  * #### Ordering and sorting
  * <code>
  *  //Order by position
  *  $data = $this->get('content_type=post&is_active=1&order_by=position desc');
  *  var_dump($data);
  *
  *  //Order by date
  *  $data = $this->get('content_type=post&is_active=1&order_by=updated_at desc');
  *  var_dump($data);
  *
  *  //Order by title
  *  $data = $this->get('content_type=post&is_active=1&order_by=title asc');
  *  var_dump($data);
  *
  *  //Get content from last week
  *  $data = $this->get('created_at=[mt]-1 week&is_active=1&order_by=title asc');
  *  var_dump($data);
  * </code>
  */
 public function get($params = false)
 {
     $params2 = array();
     if (is_string($params)) {
         $params = parse_str($params, $params2);
         $params = $params2;
     }
     if (!is_array($params)) {
         $params = array();
         $params['is_active'] = 1;
     }
     $cache_group = 'content/global';
     if (isset($params['cache_group'])) {
         $cache_group = $params['cache_group'];
     }
     $table = $this->tables['content'];
     if (!isset($params['is_deleted'])) {
         $params['is_deleted'] = 0;
     }
     $params['table'] = $table;
     $params['cache_group'] = $cache_group;
     if (isset($params['id'])) {
         $params['id'] = intval($params['id']);
     }
     if (isset($params['search_by_keyword'])) {
         $params['keyword'] = $params['search_by_keyword'];
     }
     if (isset($params['category']) and is_numeric($params['category'])) {
         $params['category'] = intval($params['category']);
         //check if this is dynamic category
         $cat_id = $params['category'];
         $category = $this->app->category_manager->get_by_id($cat_id);
         if (is_array($category) and isset($category['category_subtype']) and $category['category_subtype'] == 'content_filter' and isset($category['category_subtype_settings']) and isset($category['category_subtype_settings']['filter_content_by_keywords']) and trim($category['category_subtype_settings']['filter_content_by_keywords']) != '') {
             $params['keyword'] = $category['category_subtype_settings']['filter_content_by_keywords'];
         }
     }
     if (isset($params['keyword']) and !isset($params['search_in_fields'])) {
         $params['search_in_fields'] = array('title', 'content_body', 'content', 'description', 'content_meta_keywords', 'content_meta_title', 'url');
     }
     if (isset($params['keyword'])) {
         if (!is_admin()) {
             $params['is_deleted'] = 0;
             $params['is_active'] = 1;
         }
     }
     $get = parent::get($params);
     if (isset($params['count']) or isset($params['single']) or isset($params['one']) or isset($params['data-count']) or isset($params['page_count']) or isset($params['data-page-count'])) {
         if (isset($get['url'])) {
             $get['full_url'] = $this->app->url_manager->site($get['url']);
         }
         return $get;
     }
     if (is_array($get)) {
         $data2 = array();
         foreach ($get as $item) {
             if (isset($item['url'])) {
                 $item['url'] = $this->app->url_manager->site($item['url']);
             }
             $data2[] = $item;
         }
         $get = $data2;
         return $get;
     }
 }