123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace app\modules\admin\controllers;
- use app\common\controllers\BController;
- use app\common\components\CacheId;
- use app\common\components\FormElements;
- use app\modules\cms\models\Category;
- use app\modules\cms\models\CategoryTypeRel;
- use Yii;
- class CacheController extends BController
- {
- public $layout = 'main';
- public $syscacheList;//系统缓存
- public function init()
- {
- parent::init();
- $this->syscacheList = array(
- array('name'=>'模型','function'=>'_contentModel'),//内容模型
- array('name' =>'栏目', 'function' => '_category'),//栏目
- array('name' =>'导航', 'function' => '_nav'),//导航
- array('name' =>'推荐位', 'function' => '_position'),//推荐位
- array('name' =>'联动菜单', 'function' => '_linkmenu'),//联动菜单
- array('name' =>'类别', 'function' => '_type'),//内容类别
- array('name' =>'用户组', 'function' => '_usergroup'),//用户组
- array('name' =>'来源', 'function' => '_copyfrom'),//来源
- array('name' =>'站点配置', 'function' => '_siteconfig'),//站点
- );
- }
- //更新系统缓存
- public function actionSyscache()
- {
- if (isset($_POST['docache']) || isset($_GET['docache'])) {
- $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
- $part = $this->syscacheList[$page];
- if($part)
- {
- $function = $part['function'];
- $this->$function();
- $result = array('error'=>0,'msg'=>'更新'.$part['name'].'缓存成功..........','data'=>['num'=>($page+1)*12.5]);
- }
- else
- {
- $result = array('error'=>0,'msg'=>'更新'.$part['name'].'更新全站缓存成功','data'=>['num'=>100]);
- }
- echo_json($result);
- }
- return $this->render('syscache');
- }
- private function _contentModel()
- {
- define('FIELDS_PATH', BASE_PATH.'common'.DIRECTORY_SEPARATOR.'widget'.DIRECTORY_SEPARATOR.'fields'.DIRECTORY_SEPARATOR);
- $fields = FormElements::getFieldsInfo('types');
- //更新内容模型类:表单生成、入库、更新、输出
- $class_types = array('form','input','update','output');
- foreach($class_types as $class_type) {
- $cache_data = file_get_contents(FIELDS_PATH.'content_'.$class_type.'.class.php');
- $cache_data = str_replace('}?>','',$cache_data);
- foreach($fields as $field=>$field_value) {
- if(file_exists(FIELDS_PATH.$field.DIRECTORY_SEPARATOR.$class_type.'.inc.php')) {
- $cache_data .= file_get_contents(FIELDS_PATH.$field.DIRECTORY_SEPARATOR.$class_type.'.inc.php');
- }
- }
- $cache_data .= "\r\n } \r\n?>";
- file_put_contents(CACHE_PATH.'content_'.$class_type.'.class.php',$cache_data);
- }
- //更新模型数据缓存
- $array = array();
- $sql = "SELECT * FROM {{%content_model}} where disabled=0 order by list_order asc";
- $datas= Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($datas as $r) {
- $array[$r['model_id']] = $r;
- $this->_contentmodel_field($r['model_id']);
- }
- Yii::$app->cache->set(CacheId::modelListCacheId(), json_encode($array),0);
- return true;
- }
- private function _category()
- {
- $categorys = array();
- $models = json_decode(Yii::$app->cache->get(CacheId::modelListCacheId()),true);
- if(is_array($models))
- {
- foreach ($models as $model_id=>$model)
- {
- $sql = "SELECT * FROM {{%category}} where content_model_id=".$model_id." and disabled=0 order by list_order asc";
- $datas= Yii::$app->db->createCommand($sql)->queryAll();
- $array = array();
- foreach ($datas as $r)
- {
- $parentModel = Category::findOne($r['parent_id']);
- if($parentModel)Category::updateParents($r['cat_id']);
- Category::updateChilds($r['cat_id']);
- Category::updateChilds($r['parent_id']);
- Category::updateTypes($r['cat_id']);
- if($r['cat_type']==1) $array[$r['cat_id']] = $r;
- }
- Yii::$app->cache->set(CacheId::categoryItemsCacheId($model_id), json_encode($array),0);
- }
- }
- $sql = "SELECT * FROM {{%category}} where disabled=0 order by list_order ASC";
- $categoryResult = Yii::$app->db->createCommand($sql)->queryAll();
- if(is_array($categoryResult))
- {
- foreach($categoryResult as $r) {
- $parentModel = Category::findOne($r['parent_id']);
- if($parentModel)Category::updateParents($r['cat_id']);
- Category::updateChilds($r['cat_id']);
- Category::updateChilds($r['parent_id']);
- Category::updateTypes($r['cat_id']);
- $settings = string2array($r['settings']);
- if(is_array($settings))foreach($settings as $kk=>$vv)
- {
- $r[$kk] = $vv;
- }
- $r['is_domain'] = '0';
- $categorys[$r['cat_id']] = $r;
- }
- }
- Yii::$app->cache->set(CacheId::categoryCacheId(), json_encode($categorys),0);
- return true;
- }
- private function _position()
- {
- $array = array();
- $sql = "SELECT * FROM {{%position}} order by list_order asc";
- $lists= Yii::$app->db->createCommand($sql)->queryAll();
- if($lists)foreach($lists as $_value) {
- $array[$_value['position_id']] = $_value;
- }
- Yii::$app->cache->set(CacheId::positionCacheId(), json_encode($array),0);
- return true;
- }
- private function _nav()
- {
- $array = array();
- $sql = "SELECT * FROM {{%nav}} where disabled=0 order by list_order asc";
- $lists= Yii::$app->db->createCommand($sql)->queryAll();
- if($lists)foreach($lists as $_value) {
- $array[$_value['type']][] = $_value;
- }
- Yii::$app->cache->set(CacheId::navCacheId(), json_encode($array),0);
- return true;
- }
- private function _linkmenu()
- {
- return true;
- }
- private function _type()
- {
- $array = array();
- $sql = "SELECT * FROM {{%category_type}} where disabled=0 order by list_order asc";
- $lists= Yii::$app->db->createCommand($sql)->queryAll();
- if(is_array($lists))foreach($lists as $_value) {
- $array[$_value['type_id']] = $_value;
- }
- Yii::$app->cache->set(CacheId::typeCacheId(), json_encode($array),0);
- return true;
- }
- private function _usergroup()
- {
- //更新模型数据缓存
- $array = array();
- $sql = "SELECT * FROM {{%user_group}} where disabled=0 order by list_order asc";
- $datas= Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($datas as $r) {
- $array[$r['group_id']] = $r;
- }
- Yii::$app->cache->set(CacheId::groupCacheId(), json_encode($array),0);
- return true;
- }
- private function _copyfrom()
- {
- $array = array();
- $sql = "SELECT * FROM {{%copy_from}} order by list_order asc";
- $datas= Yii::$app->db->createCommand($sql)->queryAll();
- if(is_array($datas))foreach ($datas as $r) {
- $array[$r['id']] = $r;
- }
- Yii::$app->cache->set(CacheId::copyFromCacheId(), json_encode($array),0);
- return true;
- }
- private function _siteconfig()
- {
- return true;
- }
- //模型字段缓存
- private function _contentmodel_field($model_id)
- {
- $array = array();
- $sql = "SELECT * FROM {{%content_model_field}} where disabled=0 and model_id=".$model_id." order by list_order asc";
- $fields= Yii::$app->db->createCommand($sql)->queryAll();
- foreach($fields as $_value) {
- $setting = string2array($_value['setting']);
- $_value = array_merge($_value,$setting);
- $array[$_value['field']] = $_value;
- }
- Yii::$app->cache->set(CacheId::modelFieldCacheId($model_id), json_encode($array),0);
- return true;
- }
- //更新栏目子级信息
- private function _updateChilds($id)
- {
- if($id==0)return;
- $parentModel = Category::findOne($id);
- if($parentModel)
- {
- $childs = array();
- $childs[]=$id;
- $categoryList = Category::find()->where('parent_id='.$id)->all();
- if(is_array($categoryList))
- {
- foreach($categoryList as $category)
- {
- $childs[] = $category['cat_id'];
- }
- }
- if(!empty($childs)&&count($childs)>1)
- {
- //asort($childs);
- $parentModel->arr_child_ids =join(',',$childs);
- $parentModel->has_child = 1;
- $parentModel->save();
- }
- else
- {
- $parentModel->arr_child_ids ='';
- $parentModel->has_child = 0;
- $parentModel->save();
- }
- }
- }
- //更新栏目父级信息
- private function _updateParents($id,$parents = array())
- {
- $model = Category::findOne($id);
- if($model)
- {
- if(empty($parents)){
- $parents[] = $id;
- if($model->parent_id>0)$parents[]=$model->parent_id;
- }
- if(count($parents)==1)
- {
- $model->arr_parent_ids = '';
- $model->save();
- return;
- }
- else
- {
- $parentModel = Category::findOne($parents[count($parents)-1]);
- if($parentModel->parent_id==0)
- {
- $arr_parent_id =','.join(',',$parents).',';
- $model->arr_parent_ids = trim($arr_parent_id,',');
- $model->save();
- return;
- }
- else
- {
- $parents[] = $parentModel->parent_id;
- return $this->_updateParents($id,$parents);
- }
- }
- }
- }
- }
|