Exemple #1
0
 public function search($options = array(), $extend_options = array(), $extend_data = array())
 {
     parent::search($options, $extend_options, $extend_data);
     $this->sql_params['where'] = array();
     if (isset($options['blog'])) {
         switch ($options['blog']) {
             case 'all':
                 break;
             case 'published':
                 $this->sql_params['where'][] = $this->getWhereByField('status', self::STATUS_PUBLIC);
                 break;
             default:
                 $this->sql_params['where'][] = $this->getWhereByField('id', $options['blog']);
                 break;
         }
     }
     $this->sql_params['order'] = "{$this->table}.sort DESC";
     /**
      * @event search_blogs_backend
      * @event search_blogs_frontend
      * @param array $options
      * @return array
      */
     $res = wa()->event('search_blogs_' . wa()->getEnv(), $options);
     foreach ($res as $plugin => $plugin_options) {
         foreach ($plugin_options as $properties => $values) {
             if ($values) {
                 if (!is_array($values)) {
                     $values = array($values);
                 }
                 if (!isset($this->sql_params[$properties])) {
                     $this->sql_params[$properties] = $values;
                 } else {
                     $this->sql_params[$properties] = array_merge($this->sql_params[$properties], $values);
                 }
             }
         }
     }
     return $this;
 }
 /**
  * Generic search post entries method
  *
  * $options
  * - sort:
  * - year: int|int[] 2011|array(2009,2011)|array(2007,2008,...) ate option if single exact match, interval if array of two items, one of items in array more then two items
  * - month:int|int[]
  * - day: int|int[]
  * - datetime:
  * - status: Status has default self::STATUS_PUBLISHED if not specified, all statuses if false or specified in array<
  * - contact_id: If specified records will be checked authorship in non self::STATUS_PUBLISHED status
  * - blog_id:
  * - text:
  *
  * @param array @options
  * @param array $extend_options see prepareView method
  * @param array $extend_data
  * @see blogItemModel::search()
  * @return blogPostModel
  */
 public function search($options = array(), $extend_options = array(), $extend_data = array())
 {
     parent::search($options, $extend_options, $extend_data);
     $this->sql_params['where'] = array();
     $option_names = array();
     $option_names['status'] = self::STATUS_PUBLISHED;
     $option_names['blog_id'] = false;
     $option_names['id'] = false;
     $option_names['url'] = false;
     $date_options = array('year' => 'YEAR(datetime)', 'month' => 'MONTH(datetime)', 'day' => 'DAYOFMONTH(datetime)', 'datetime' => 'datetime');
     foreach ($date_options as $field => $expression) {
         if (isset($options[$field]) && $options[$field]) {
             if (is_array($options[$field])) {
                 if (count($options[$field]) == 2) {
                     if ($this->sql_params[$field . '_min'] = array_shift($options[$field])) {
                         $this->sql_params['where'][] = "{$expression} >= :{$field}_min";
                     }
                     if ($this->sql_params[$field . '_max'] = array_shift($options[$field])) {
                         $this->sql_params['where'][] = "{$expression} <= :{$field}_max";
                     }
                 } else {
                     $values = array_map('intval', $options[$field]);
                     $this->sql_params['where'][] = "{$expression} IN (" . implode(',', $values) . ")";
                 }
             } else {
                 $this->sql_params[$field] = $options[$field];
                 $this->sql_params['where'][] = "{$expression} = :{$field}";
             }
         }
     }
     if (isset($options['contact_id']) && $options['contact_id']) {
         if (isset($extend_data['blog'])) {
             $writable_blog = array('write' => array(), 'full' => array());
             foreach ($extend_data['blog'] as $id => $blog) {
                 if ($blog['rights'] >= blogRightConfig::RIGHT_FULL) {
                     $writable_blog['full'][] = $id;
                 } elseif ($blog['rights'] >= blogRightConfig::RIGHT_READ_WRITE) {
                     $writable_blog['write'][] = $id;
                 }
             }
         }
         if (isset($options['status']) && $options['status'] === false) {
             $options['status'] = array(self::STATUS_DRAFT, self::STATUS_SCHEDULED, self::STATUS_DEADLINE, self::STATUS_PUBLISHED);
         }
         if (isset($options['status'])) {
             if (isset($writable_blog)) {
                 $where = array();
                 $statuses = array_intersect(array(self::STATUS_PUBLISHED), $options['status']);
                 if ($statuses) {
                     $where[] = $this->getWhereByField('status', $statuses);
                 }
                 $statuses = array_intersect(array(self::STATUS_DRAFT, self::STATUS_SCHEDULED, self::STATUS_DEADLINE), $options['status']);
                 if ($statuses) {
                     if ($writable_blog['write']) {
                         $where[] = "(" . $this->getWhereByField('status', $statuses) . " AND " . $this->getWhereByField('blog_id', $writable_blog['write']) . " AND " . $this->getWhereByField('contact_id', $options['contact_id']) . ")";
                     }
                     if ($writable_blog['full']) {
                         $where[] = "(" . $this->getWhereByField('status', $statuses) . " AND " . $this->getWhereByField('blog_id', $writable_blog['full']) . ")";
                     }
                 }
                 $this->sql_params['where'][] = "(" . implode(' OR ', $where) . ")";
             } else {
                 if (!is_array($options['status'])) {
                     $options['status'] = array($options['status']);
                 }
                 $where = array();
                 $statuses = array_intersect(array(self::STATUS_PUBLISHED), $options['status']);
                 if ($statuses) {
                     $where[] = $this->getWhereByField('status', $statuses);
                 }
                 $statuses = array_intersect(array(self::STATUS_DRAFT, self::STATUS_SCHEDULED, self::STATUS_DEADLINE), $options['status']);
                 if ($statuses) {
                     $where[] = "(" . $this->getWhereByField('status', $statuses) . " AND " . $this->getWhereByField('contact_id', $options['contact_id']) . ")";
                 }
                 if ($where) {
                     $this->sql_params['where'][] = "(" . implode(' OR ', $where) . ")";
                 }
             }
         } elseif (isset($option_names['status'])) {
             $this->sql_params['where'][] = $this->getWhereByField('status', $option_names['status']);
             $this->sql_params['where'][] = $this->getWhereByField('contact_id', $options['contact_id']);
         } else {
             $this->sql_params['where'][] = $this->getWhereByField('contact_id', $options['contact_id']);
         }
         unset($option_names['status']);
     }
     foreach ($option_names as $field => $default) {
         if (isset($options[$field])) {
             if ($options[$field] !== false) {
                 $this->sql_params['where'][] = $this->getWhereByField($field, $options[$field]);
             }
         } elseif ($default) {
             $this->sql_params['where'][] = $this->getWhereByField($field, $default);
         }
     }
     if (isset($options['sort'])) {
         switch ($options['sort']) {
             case 'create':
                 $this->sql_params['order'] = "{$this->table}.{$this->id} DESC";
                 break;
             case 'overdue':
                 $time = time();
                 $this->sql_params['order'] = "IF({$this->table}.status = '" . self::STATUS_DEADLINE . "', ({$time} - UNIX_TIMESTAMP({$this->table}.datetime)), ({$this->table}.{$this->id} - {$time})) DESC";
                 break;
             default:
                 if (in_array($options['sort'], $this->fields)) {
                     $this->sql_params['order'] = "{$this->table}.{$options['sort']} DESC";
                 }
                 break;
         }
     } else {
         $this->sql_params['order'] = "{$this->table}.datetime DESC";
     }
     if (!empty($options['text'])) {
         $this->sql_params['like'] = "%" . str_replace(array('%', '_'), array('\\%', '\\_'), $options['text']) . "%";
         $this->sql_params['where'][] = "(blog_post.title LIKE s:like OR blog_post.text LIKE s:like)";
     }
     if (!isset($extend_options['plugin']) || $extend_options['plugin']) {
         /**
          * Build post search query
          * @event search_posts_backend
          * @event search_posts_frontend
          * @example public function postSearch($options)
          * {
          *     $result = null;
          *     //check se
          *     if (is_array($options) && isset($options['plugin'])) {
          *         if (isset($options['plugin'][$this->id])) {
          *             $result = array();
          *             $result['where'][] = 'contact_id = '.wa()->getUser()->getId();
          *             $response = wa()->getResponse();
          *             $title = $response->getTitle();
          *             $title = _wp('My posts');
          *             $response->setTitle($title);
          *         }
          *     }
          *     return $result;
          * }
          * @param array [string]mixed $options
          * @return array[string][string][]string $return['%plugin_id%']['join'] Join conditions
          * @return array[string][string][]string $return['%plugin_id%']['where'] Where conditions
          * @return array[string][string][]string $return['%plugin_id%']['order'] order conditions
          */
         $res = wa('blog')->event('search_posts_' . wa()->getEnv(), $options);
         foreach ($res as $plugin_options) {
             foreach ($plugin_options as $properties => $values) {
                 if ($values) {
                     if (!is_array($values)) {
                         $values = array($values);
                     }
                     if (!isset($this->sql_params[$properties])) {
                         $this->sql_params[$properties] = $values;
                     } else {
                         $this->sql_params[$properties] = array_merge($this->sql_params[$properties], $values);
                     }
                 }
             }
         }
     }
     return $this;
 }