Пример #1
0
 protected function _findIndex($state, $query, $results = array())
 {
     if ($state == 'before') {
         if (!isset($query['named'])) {
             $query['named'] = array();
         }
         $query['named'] = array_merge(array('collaborators' => null, 'contains' => array(), 'contributors' => null, 'forks' => null, 'has' => array(), 'open_issues' => null, 'query' => null, 'since' => null, 'watchers' => null, 'with' => array()), $query['named']);
         $query['named']['has'] = array_merge((array) $query['named']['with'], (array) $query['named']['contains'], (array) $query['named']['has']);
         $query['conditions'] = array("{$this->alias}.deleted" => false);
         $query['contain'] = array('Maintainer');
         $query['fields'] = array("{$this->alias}.{$this->primaryKey}", "{$this->alias}.name", "{$this->alias}.description", "{$this->alias}.watchers", "{$this->alias}.modified", 'Category.name', 'Category.slug', 'Maintainer.username');
         $direction = 'asc';
         if (!empty($query['named']['direction'])) {
             $query['named']['direction'] = strtolower((string) $query['named']['direction']);
             if ($query['named']['direction'] == 'dsc' || $query['named']['direction'] == 'des') {
                 $query['named']['direction'] = 'desc';
             }
             if ($query['named']['direction'] != 'asc' && $query['named']['direction'] != 'desc') {
                 $query['named']['direction'] = 'desc';
             }
             $direction = $query['named']['direction'];
         }
         $sortField = 'username';
         if (!empty($query['named']['sort'])) {
             $query['named']['sort'] = strtolower($query['named']['sort']);
             if (in_array($query['named']['sort'], Package::$_validOrders)) {
                 $sortField = $query['named']['sort'];
             }
         }
         if ($sortField == 'username') {
             $query['order'] = array(array("Maintainer.{$sortField} {$direction}"));
         } else {
             $query['order'] = array(array("{$this->alias}.{$sortField} {$direction}"));
         }
         if ($query['named']['collaborators'] !== null) {
             $query['conditions']["{$this->alias}.collaborators >="] = (int) $query['named']['collaborators'];
         }
         if ($query['named']['contributors'] !== null) {
             $query['conditions']["{$this->alias}.contributors >="] = (int) $query['named']['contributors'];
         }
         if ($query['named']['forks'] !== null) {
             $query['conditions']["{$this->alias}.forks >="] = (int) $query['named']['forks'];
         }
         if (!empty($query['named']['has'])) {
             foreach ($query['named']['has'] as $has) {
                 $has = inflector::singularize(strtolower($has));
                 if (in_array($has, $this->validTypes)) {
                     $query['conditions'][] = array('Tag.keyname' => $has, 'Tag.identifier' => 'contains');
                 }
             }
             $query['joins'][] = array('alias' => 'Tagged', 'table' => 'tagged', 'type' => 'INNER', 'conditions' => array('`Tagged`.`foreign_key` = `' . $this->alias . '`.`id`'));
             $query['joins'][] = array('alias' => 'Tag', 'table' => 'tags', 'type' => 'INNER', 'conditions' => array('`Tagged`.`tag_id` = `Tag`.`id`'));
         }
         if (!empty($query['named']['category'])) {
             $this->unbindModel(array('belongsTo' => array('Categories.Category')));
             $query['joins'][] = array('alias' => 'Category', 'table' => 'categories', 'type' => 'INNER', 'conditions' => array('`Category`.`id` = `Package`.`category_id`', '`Category`.`slug`' => $query['named']['category']));
         } else {
             $query['contain'][] = 'Category';
         }
         if ($query['named']['open_issues'] !== null) {
             $query['conditions']["{$this->alias}.open_issues <="] = (int) $query['named']['open_issues'];
         }
         if ($query['named']['query'] !== null) {
             $query['conditions'][]['OR'] = array("{$this->alias}.name LIKE" => '%' . $query['named']['query'] . '%', "{$this->alias}.description LIKE" => '%' . $query['named']['query'] . '%', "Maintainer.username LIKE" => '%' . $query['named']['query'] . '%');
         }
         if ($query['named']['since'] !== null) {
             $time = date('Y-m-d H:i:s', strtotime($query['named']['since']));
             $query['conditions']["{$this->alias}.last_pushed_at >"] = $time;
         }
         if ($query['named']['watchers'] !== null) {
             $query['conditions']["{$this->alias}.watchers >="] = (int) $query['named']['watchers'];
         }
         if (!empty($query['operation'])) {
             return $query;
         }
         return $query;
     }
     if (!empty($query['operation'])) {
         return $results;
     }
     foreach ($results as $i => $result) {
         $results[$i]['Package']['description'] = trim($result['Package']['description']);
         if (empty($result['Package']['description'])) {
             $results[$i]['Package']['description'] = 'No description available';
         }
         $results[$i]['Category']['color'] = '';
         if (!empty($result['Category']['slug'])) {
             $results[$i]['Category']['color'] = $this->packageColor($result['Category']['slug']);
         }
     }
     return $results;
 }