Esempio n. 1
0
 /**
  * 
  * Enter description here ...
  * @param unknown_type $params
  */
 public static function Pages($params = array())
 {
     //设置默认参数
     $_defaults_params = array('allow_cache' => true, 'page' => isset($_GET['page']) ? intval($_GET['page']) : 1, 'pagesize' => 15);
     $params = array_merge($_defaults_params, $params);
     //有开启缓存功能,则从缓存中取数据, 如果有数据,则直接返回结果
     if ($params['allow_cache'] && isset(Yii::app()->cache)) {
         $_cache_key = md5('content.archive.pages.' . serialize($params));
         $_r = json_decode(Yii::app()->cache->get($_cache_key), true);
         if ($_r && is_array($_r)) {
             return $_r;
         }
     }
     $cmd = Yii::app()->db->createCommand();
     $cmd->select('COUNT(DISTINCT a.content_archives_id) AS COUNT')->from('{{content_archives}} a')->join('{{content_archives_classes_relating}} acr', 'acr.content_archives_id=a.content_archives_id')->join('{{content_archives_classes}} ac', 'ac.class_id=acr.class_id')->join('{{content_model}} m', 'm.content_model_id=ac.content_model_id');
     //添加条件
     if (isset($params['content_archives_status']) && !empty($params['content_archives_status'])) {
         $__addons = array('AND', 'a.content_archives_status=:content_archives_status');
         $__params = array(':content_archives_status' => $params['content_archives_status']);
     } else {
         $__addons = array('AND', 'a.content_archives_status>:content_archives_status');
         $__params = array(':content_archives_status' => 0);
     }
     //
     if (isset($params['content_archives_id']) && !empty($params['content_archives_id'])) {
         $__addons[] = array('AND', 'a.content_archives_id=:content_archives_id');
         $__params[':content_archives_id'] = $params['content_archives_id'];
     }
     //
     if (isset($params['content_model_id']) && !empty($params['content_model_id'])) {
         $__addons[] = array('AND', 'ac.content_model_id=:content_model_id');
         $__params[':content_model_id'] = $params['content_model_id'];
     }
     //
     if (isset($params['class_id']) && !empty($params['class_id'])) {
         $_classes = ContentArchivesClass::get_classes_children($params['class_id']);
         $_s = '';
         foreach ($_classes as $_k => $_v) {
             $_s .= ",:class_id{$_k}";
             $__params[":class_id{$_k}"] = $_v;
         }
         $_s = trim($_s, ',');
         $__addons[] = array('AND', "acr.class_id IN({$_s})");
         unset($_classes, $_s);
     }
     //
     if (isset($params['content_archives_subject']) && !empty($params['content_archives_subject'])) {
         $__addons[] = array('OR', array('OR LIKE', 'a.content_archives_subject', "%{$params['content_archives_subject']}%"));
         //$__addons[] = array('LIKE', 'a.content_archives_name', '%:content_archives_name%');
         //$__params = array_merge($__params, array(':content_archives_name'=>$params['content_archives_name']));
     }
     //
     if (isset($params['search_key']) && $params['search_key']) {
         if (is_numeric($params['search_key'])) {
             $__addons[] = array('AND', 'a.content_archives_id=:content_archives_id');
             $__params[':content_archives_id'] = $params['search_key'];
         } else {
             $__addons[] = array('OR', array('OR LIKE', 'a.content_archives_subject', "%{$params['search_key']}%"));
         }
         //$__params = array_merge($__params, array(':search_key'=>$params['search_key']));
     }
     $cmd->where($__addons, $__params);
     //统计数量
     $count = $cmd->queryScalar();
     //分页处理
     $pages = new CPagination($count);
     //设置分页大小
     $pages->pageSize = $params['pagesize'];
     //清空前面执行过的SQL
     $cmd->setText('');
     if (isset($params['orderby']) && $params['orderby']) {
         $cmd->order($params['orderby']);
     } else {
         $cmd->order(array('a.content_archives_rank ASC', 'a.content_archives_dateline DESC'));
     }
     $cmd->select('a.content_archives_id, ac.content_model_id, m.content_model_name, ac.class_id, ac.class_name, a.content_archives_subject, a.content_archives_color, a.content_archives_short_subject, a.content_archives_flag, a.content_archives_jump_url, a.content_archives_source, a.content_archives_author, a.content_archives_thumb, a.content_archives_keywords, a.content_archives_summary, a.content_archives_status, a.content_archives_rank, a.content_archives_pubtime, a.content_archives_is_build, a.content_archives_lasttime, a.content_archives_dateline')->group('a.content_archives_id')->limit($pages->getLimit())->offset($pages->getOffset());
     $_r['pages'] = $pages;
     $_r['rows'] = $cmd->queryAll();
     //有开启缓存,则把结果添加到缓存中
     if ($params['allow_cache'] && isset(Yii::app()->cache)) {
         $_cache_cache_time = Setting::getSettingCache('CONTENT_ARCHIVES_PAGES_CACHE_TIME');
         Yii::app()->cache->set($_cache_key, json_encode($_r), $_cache_cache_time);
         unset($_cache_cache_time, $_cache_key);
     }
     return $_r;
 }