/**
  * Lists all Category models.
  * @return mixed
  */
 public function actionIndex()
 {
     //if(!Yii::$app->user->can('viewYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     $searchModel = new CategorySearch();
     $dataProvider = Category::get(0, Category::find()->asArray()->all());
     return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
 }
Example #2
0
 public function actionView($id)
 {
     if ($id <= 0) {
         $this->goHome();
     }
     $allCategory = Category::find()->asArray()->all();
     $arrayCategoryIdName = ArrayHelper::map($allCategory, 'id', 'name');
     $arrSubCat = Category::getArraySubCatalogId($id, $allCategory);
     /****** 价格筛选 ****/
     $result = (new Query())->select('min(price) as min, max(price) as max')->from('product')->where(['category_id' => $arrSubCat, 'status' => Status::STATUS_ACTIVE])->one();
     $min = $result['min'];
     $max = $result['max'];
     if ($max > $min && $max > 0) {
         // 计算跨度
         $priceGrade = 0.0001;
         for ($i = -2; $i < log10($max); $i++) {
             $priceGrade *= 10;
         }
         $span = ceil(($max - $min) / 5 / $priceGrade) * $priceGrade;
         if ($span == 0) {
             $span = $priceGrade;
         }
         // 计算价格的起点和终点
         for ($i = 1; $min > $span * $i; $i++) {
         }
         for ($j = 1; $min > $span * ($i - 1) + $priceGrade * $j; $j++) {
         }
         $priceFilter['start'] = $span * ($i - 1) + $priceGrade * ($j - 1);
         for (; $max >= $span * $i; $i++) {
         }
         $priceFilter['end'] = $span * $i + $priceGrade * ($j - 1);
         $priceFilter['span'] = $span;
     }
     /****** 价格筛选 end ****/
     /****** 品牌筛选 start ****/
     $result = (new Query())->select('distinct(brand_id)')->from('product')->where(['category_id' => $arrSubCat, 'status' => Status::STATUS_ACTIVE])->all();
     $ids = ArrayHelper::map($result, 'brand_id', 'brand_id');
     $brandFilter = Brand::find()->where(['id' => $ids])->orderBy(['name' => SORT_ASC])->all();
     /****** 品牌筛选 end ****/
     $query = Product::find()->where(['category_id' => $arrSubCat, 'status' => Status::STATUS_ACTIVE]);
     // 如果选择了价格区间
     if (Yii::$app->request->get('max')) {
         $min = intval(Yii::$app->request->get('min'));
         $max = intval(Yii::$app->request->get('max'));
         if ($min >= 0 && $max) {
             $query->andWhere(['and', ['>', 'price', $min], ['<=', 'price', $max]]);
         }
     }
     // 如果选择了品牌
     if (Yii::$app->request->get('brand_id')) {
         $brandId = intval(Yii::$app->request->get('brand_id'));
         if ($brandId >= 0) {
             $query->andWhere(['brand_id' => $brandId]);
         }
     }
     // 侧边热销商品
     $sameCategoryProducts = Product::find()->where(['category_id' => $id])->orderBy(['sales' => SORT_DESC])->limit(5)->all();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => Yii::$app->params['defaultPageSizeProduct']], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     return $this->render('view', ['model' => $this->findModel($id), 'allCategory' => $allCategory, 'arrayCategoryIdName' => $arrayCategoryIdName, 'products' => $dataProvider->getModels(), 'pagination' => $dataProvider->pagination, 'priceFilter' => isset($priceFilter) ? $priceFilter : null, 'brandFilter' => $brandFilter, 'sameCategoryProducts' => $sameCategoryProducts]);
 }
Example #3
0
 public function search($params)
 {
     $query = Category::find()->where(['status' => Category::STATUS_DISPLAY]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     if ($this->name) {
         $query->andFilterWhere(['like', 'name', $this->name]);
     }
     if ($this->slug) {
         $query->andFilterWhere(['like', 'slug', $this->slug]);
     }
     if ($this->parent) {
         $category = Category::find()->where(['like', 'name', $this->parent])->all();
         if ($category) {
             $c_ids = [];
             foreach ($category as $v) {
                 $c_ids[] = $v->id;
             }
             $query->andFilterWhere(['in', 'parent', $c_ids]);
         } else {
             $query->andFilterWhere(['in', 'id', [0]]);
         }
     }
     return $dataProvider;
 }
Example #4
0
 public function insertCategories($categoryIds, $beforeCount = true, $afterCount = true)
 {
     if (!is_array($categoryIds)) {
         return false;
     }
     $categoryIds = array_unique($categoryIds);
     $this->deleteCategories($beforeCount);
     //先删除文章分类
     //插入新分类
     if ($categoryIds) {
         foreach ($categoryIds as $v) {
             if (!Category::find()->andWhere('mid=:mid', [':mid' => $v])->one()) {
                 continue;
             }
             $model = new Relationship();
             $model->cid = $this->cid;
             $model->mid = $v;
             $model->insert(false);
             if ($afterCount) {
                 //更新分类文章数
                 Category::updateAllCounters(['count' => 1], ['mid' => $v]);
             }
         }
     }
     return true;
 }
Example #5
0
 public function actionView($id)
 {
     $model = $this->findModel($id);
     $allCategory = Category::find()->asArray()->all();
     $arrayCategoryIdName = ArrayHelper::map($allCategory, 'id', 'name');
     $rootCategoryId = Category::getRootCatalogId($model->category_id, $allCategory);
     $arraySameRootCategory = Category::getArraySubCatalogId($rootCategoryId, $allCategory);
     // 同类商品  和 同大类商品
     $sameCategoryProducts = Product::find()->where(['category_id' => $model->category_id])->orderBy(['sales' => SORT_DESC])->limit(3)->all();
     $sameRootCategoryProducts = Product::find()->where(['category_id' => $arraySameRootCategory])->orderBy(['sales' => SORT_DESC])->limit(Yii::$app->params['productHotCount'])->all();
     // 记录浏览日志
     $historyProducts = [];
     $cookies = Yii::$app->request->cookies;
     if ($cookies->has('productHistory')) {
         $arrHistory = explode(',', $cookies->getValue('productHistory'));
         foreach ($arrHistory as $id) {
             $product = Product::findOne($id);
             if ($product) {
                 array_push($historyProducts, $product);
             }
         }
         array_unshift($arrHistory, $id);
         $arrHistory = array_unique($arrHistory);
         while (count($arrHistory) > Yii::$app->params['productHistoryCount']) {
             array_pop($arrHistory);
         }
         Yii::$app->response->cookies->remove('productHistory');
         Yii::$app->response->cookies->add(new Cookie(['name' => 'productHistory', 'value' => implode(',', $arrHistory), 'expire' => time() + 3600 * 24 * 30]));
     } else {
         Yii::$app->response->cookies->add(new Cookie(['name' => 'productHistory', 'value' => $id, 'expire' => time() + 3600 * 24 * 30]));
     }
     return $this->render('view', ['model' => $model, 'allCategory' => $allCategory, 'arrayCategoryIdName' => $arrayCategoryIdName, 'sameCategoryProducts' => $sameCategoryProducts, 'sameRootCategoryProducts' => $sameRootCategoryProducts, 'historyProducts' => $historyProducts]);
 }
 /**
  * Finds the Category model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return Category the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if ($id !== null && ($model = Category::find($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #7
0
 /**
  * 概要
  * @return string
  */
 public function actionIndex()
 {
     $recentPublishedPost = Post::find()->selectNoText()->recentPublished()->all();
     $postCount = Post::find()->published()->count();
     $categoryCount = Category::find()->count();
     //todo: 评论数量 最新回复
     return $this->render('index', ['recentPublishedPost' => $recentPublishedPost, 'postCount' => $postCount, 'categoryCount' => $categoryCount]);
 }
Example #8
0
 public function init()
 {
     parent::init();
     foreach (Category::find()->all() as $category) {
         $this->_catagories[$category->parent_id][] = $category;
     }
     //print_r($this->_catagories);die;
 }
Example #9
0
 public function getCategoryObject()
 {
     if (!empty($this->_category)) {
         return $this->_category;
     }
     $category = Category::find()->where(['id' => $this->category])->one();
     return $this->_category = $category;
 }
Example #10
0
 /**
  * Action method for creating a new forum topic.
  * @return string|Response action method execution result.
  */
 public function actionCreate()
 {
     $topic = new Topic();
     if ($topic->load(Yii::$app->request->post()) && $topic->save()) {
         return $this->redirect($topic->url);
     }
     return $this->render('create', ['topic' => $topic, 'categories' => Category::find()->all(), 'sections' => empty($topic->category_id) ? [] : Section::findAll(['category_id' => $topic->category_id])]);
 }
 public function actionIndex($c_url)
 {
     $model = Category::find()->with(['activeSubcategories'])->byUrl($c_url)->active()->one();
     if ($model === null) {
         throw new NotFoundHttpException();
     }
     return $this->render('index', ['model' => $model]);
 }
Example #12
0
 /**
  * Finds the Meta model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Category the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Category::find()->andWhere('mid=:mid', [':mid' => $id])->one()) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #13
0
 public static function getNews($cid, $num)
 {
     $allCategory = Category::find()->asArray()->all();
     $arrayCategoryIdName = \yii\helpers\ArrayHelper::map($allCategory, 'id', 'name');
     $arrSubCat = Category::getArraySubCatalogId($cid, $allCategory);
     $where = ['and', ['category_id' => $arrSubCat], 'status>=' . Status::STATUS_ACTIVE];
     $news = News::find()->where($where)->limit($num)->all();
     return $news;
 }
Example #14
0
 /**
  * Редактирование поста.
  * @param string $id идентификатор редактируемого поста
  * @return string|Response
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model, 'authors' => User::find()->all(), 'tags' => Tags::find()->all(), 'category' => Category::find()->all()]);
     }
 }
Example #15
0
 /**
  * Получить массив названий категорий с их ID.
  * @return array
  */
 public static function getToSelect()
 {
     $result = [];
     $categories = Category::find()->where(['[[status]]' => true])->orderBy('[[created_at]] ASC')->all();
     if (!empty($categories)) {
         $result = ArrayHelper::map($categories, 'id', 'name');
     }
     return $result;
 }
Example #16
0
 /**
  * @return array
  */
 public static function categoryIdToName()
 {
     $category = Category::find()->select(['name', 'id'])->indexBy('id')->asArray()->all();
     $categoryArray = [];
     foreach ($category as $k => $v) {
         $categoryArray[$k] = $v['name'];
     }
     return $categoryArray;
 }
Example #17
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Category::find()->orderBy('parent_id ASC, id ASC');
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['like', 'title', $this->title]);
     return $dataProvider;
 }
Example #18
0
 /**
  * Updates an existing Category model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $categories = Category::find()->all();
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model, 'categories' => $categories]);
     }
 }
Example #19
0
 public function actionIndex($cid = 1)
 {
     $allCategory = Category::find()->asArray()->all();
     $arrayCategoryIdName = ArrayHelper::map($allCategory, 'id', 'name');
     $arrSubCat = Category::getArraySubCatalogId($cid, $allCategory);
     $where = ['and', ['category_id' => $arrSubCat], 'status>=' . Status::STATUS_ACTIVE];
     $query = News::find()->where($where);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => 8], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     return $this->render('index', ['models' => $dataProvider->getModels(), 'pagination' => $dataProvider->pagination, 'cid' => $cid]);
 }
Example #20
0
 public function getCategories($type = NULL, &$data = [], $parent = NULL)
 {
     $category = Category::find()->where(['parent_id' => $parent, 'type' => $type])->andWhere(['NOT IN', 'id', !$this->isNewRecord ? $this->id : 0])->all();
     foreach ($category as $key => $value) {
         $data[$value->id] = $this->getIndent($value->indent) . $value->title;
         unset($category[$key]);
         $this->getCategories($type, $data, $value->id);
     }
     return $data;
 }
Example #21
0
 /**
  * @return string
  */
 public function actionIndex()
 {
     $this->layout = 'home';
     $subcat = new Subcategory();
     $cat = Category::find()->all();
     $basicModel = PaymentPlans::findOne(['name' => 'Basic']);
     $advancedModel = PaymentPlans::findOne(['name' => 'Advanced']);
     $proModel = PaymentPlans::findOne(['name' => 'Pro']);
     return $this->render('index', compact('cat', 'subcat', 'basic', 'advanced', 'pro', 'basicModel', 'advancedModel', 'proModel'));
 }
Example #22
0
 public function actionCategory($slug)
 {
     $category = Category::find()->andWhere(['slug' => $slug])->one();
     if (!$category) {
         throw new NotFoundHttpException('页面不存在');
     }
     $pagination = new Pagination(['totalCount' => $category->getPosts()->count()]);
     $posts = $category->getPosts()->offset($pagination->offset)->limit($pagination->limit)->all();
     return $this->render('list', ['posts' => $posts, 'pagination' => $pagination, 'category' => $category]);
 }
Example #23
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Category::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'type' => $this->type, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
 /**
  * Updates an existing Category model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param string $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $dataProvider = new ActiveDataProvider(['query' => Category::find()]);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         Yii::$app->session->setFlash('message', 'The category has been updated successfully.');
         return $this->redirect('/category/index', ['dataProvider' => $dataProvider]);
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }
 public function init()
 {
     parent::init();
     $geo = new \jisoft\sypexgeo\Sypexgeo();
     $res = $geo->get();
     $request = \Yii::$app->request->getQueryParams();
     $category = Category::find()->byUrl(ArrayHelper::getValue($request, Category::URL_PARAM_CATEGORY))->one();
     $subcategory = Category::find()->byUrl(ArrayHelper::getValue($request, Category::URL_PARAM_SUBCATEGORY))->one();
     $rubric = Category::find()->byUrl(ArrayHelper::getValue($request, Category::URL_PARAM_RUBRIC))->one();
     $this->view->params = ['bundle' => AppAsset::register($this->view), 'categories' => Category::getMain(), 'countryEn' => $res['country']['name_en'], 'category' => $category, 'subcategory' => $subcategory, 'rubric' => $rubric];
 }
Example #26
0
 public function actionIndex()
 {
     $lastPosts = Post::find()->orderBy('created_at desc')->limit(10)->all();
     $featured = Post::featured();
     $drafts = Post::drafts();
     $mostPopularCategory = Category::find()->joinWith('posts')->select('category.id, post.category_id, count(distinct post.title) as qty, category.title')->groupBy('post.category_id')->orderBy('qty desc')->one();
     $mostPopularTag = Tag::find()->joinWith('posts')->select('tags.id, posts_tags.tag_id, count(distinct posts_tags.post_id) as qty, tags.title')->groupBy('posts_tags.tag_id')->orderBy('qty desc')->one();
     $postsCount = Post::find()->where('active = 1')->count();
     $categoryCount = Category::find()->count();
     return $this->render('index', ['lastPosts' => $lastPosts, 'featured' => $featured, 'drafts' => $drafts, 'mostPopularCategory' => $mostPopularCategory, 'mostPopularTag' => $mostPopularTag, 'postsCount' => $postsCount, 'categoryCount' => $categoryCount]);
 }
Example #27
0
 public function actionIndex()
 {
     //页面左侧分类
     $bigCate = Category::find()->where(['is_nav' => \common\models\YesNo::YES])->orderBy(['sort_order' => SORT_ASC])->all();
     //首页轮播图
     $slider = Slider::find()->where(['place' => 0])->orderBy(['ord' => SORT_ASC])->all();
     //师生风采
     $teachers = \common\models\Album::find()->orderBy(['id' => SORT_DESC])->limit(5)->all();
     //友情链接
     $friendLink = \common\models\Friendlink::find()->where(['isshow' => \common\models\YesNo::YES])->orderBy(['ord' => SORT_ASC])->all();
     return $this->render('index', ['bigCate' => $bigCate, 'slider' => $slider, 'teachers' => $teachers, 'friendlink' => $friendLink]);
 }
Example #28
0
 public function actionGetcategory($id)
 {
     $rows = \common\models\Category::find()->where(['main_category_id' => $id, 'is_status' => 1])->all();
     echo "<option value=''>" . Yii::t('app', '-- Select Category --') . "</option>";
     if (count($rows) > 0) {
         foreach ($rows as $row) {
             echo "<option value='{$row->id}'>{$row->category}</option>";
         }
     } else {
         echo "";
     }
 }
Example #29
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Category::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['tree' => SORT_ASC], 'attributes' => ['tree', 'name', 'status']], 'pagination' => ['pageSize' => Yii::$app->params['pageSize']]]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'tree' => $this->tree, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
Example #30
0
 public function search($params)
 {
     $query = Category::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['sort' => SORT_ASC]], 'pagination' => ['pageSize' => 20]]);
     // load the seach form data and validate
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     // adjust the query by adding the filters
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'slug', $this->slug]);
     return $dataProvider;
 }