Inheritance: extends yii\db\ActiveRecord
Example #1
0
 /**
  * Просмотр списка постов по тегу
  * @param $id
  * @return string
  */
 public function actionView($id)
 {
     $tagModel = new Tags();
     $tag = $tagModel->getTag($id);
     $categoryModel = new Category();
     return $this->render('view', ['tag' => $tag, 'posts' => $tag->getTagPosts(), 'categories' => $categoryModel->getCategories()]);
 }
 public function actionLoadGoods($id, $category = 'all', $q = '')
 {
     $offset = (int) Yii::$app->request->post('offset');
     Yii::$app->response->format = Response::FORMAT_JSON;
     /* @var $model Store */
     $model = Store::findOne(['id' => $id, 'status' => [Store::STATUS_ACTIVE, Store::STATUS_REST]]);
     if (!$model) {
         throw new NotFoundHttpException('未找到该营业点!');
     }
     if ($q !== '') {
         $query = $model->getGoods()->andWhere(['or', ['like', 'name', $q], ['like', 'description', $q]]);
     } else {
         $modelCate = $category === 'all' ? null : Category::findOne(['slug' => $category]);
         if ($modelCate) {
             $query = $model->getGoods($modelCate->id);
         } else {
             $query = $model->getGoods();
         }
     }
     $limit = 8;
     $goodsList = $query->offset($offset)->limit($limit)->all();
     $output = ['status' => 'ok', 'html' => '', 'length' => count($goodsList)];
     $output['end'] = $output['length'] < $limit;
     foreach ($goodsList as $goods) {
         $output['html'] .= $this->renderPartial('_item', ['goods' => $goods, 'lazy' => false]);
     }
     return $output;
 }
Example #3
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 #4
0
 public function actionCategory($id)
 {
     $backUrl = \Yii::$app->request->referrer;
     $category = Category::findOne($id);
     $this->layout = 'basic-category';
     return $this->render('category', compact('category', 'backUrl'));
 }
 public function actionIndex($slug)
 {
     $query = Vidmage::find()->joinWith('vidmageCategories.category')->where(['category.slug' => $slug])->orderBy(['id' => SORT_DESC]);
     $vidmages = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]);
     $channel = Category::findOne(['slug' => $slug]);
     return $this->render('index', compact('vidmages', 'channel'));
 }
 public function actionDetail($id)
 {
     /* @var $model Store */
     $model = Store::findOne($id);
     if (!$model) {
         throw new BadRequestHttpException('未找到该营业点!');
     }
     if ($model->status === Store::STATUS_DISABLED) {
         throw new BadRequestHttpException('该店铺已被禁用,请重新选择!');
     }
     $data['store'] = $model->toArray(['id', 'name', 'school_id', 'address', 'cellphone', 'telephone', 'notice', 'status', 'hours', 'has_book', 'has_down', 'has_gift', 'has_least', 'down_upper', 'down_val', 'gift_upper', 'gift_val', 'least_val', 'created_at']);
     $data['store']['downMsg'] = $model->downMsg;
     $data['store']['giftMsg'] = $model->giftMsg;
     $data['categories'] = Category::getKeyValuePairs();
     foreach ($data['categories'] as $key => $name) {
         $goodsList = $model->getGoods($key)->all();
         if ($goodsList) {
             foreach ($goodsList as $goods) {
                 $goodsArr = $goods->toArray();
                 $data['goodsList'][$key][] = $goodsArr;
             }
         } else {
             $data['goodsList'][$key] = [];
         }
     }
     return $data;
 }
Example #7
0
 /**
  * 其他分类需要显示在导航栏的新增方法
  * @param string $ctype 其他分类表标识 a:文章分类表,c:商品分类表
  * @param int $cid 其他表分类id
  * @return mixed
  */
 public function addData($ctype, $cid)
 {
     // 如果已经存在就不继续执行了
     if ($this->find()->where(['ctype' => $ctype, 'cid' => $cid])->one()) {
         return true;
     }
     switch ($ctype) {
         case 'a':
             // 文章分类
             $this->name = ArticleCat::findOne($cid)->cat_name;
             $this->url = '';
             // 前台还没有做,暂时空起
             break;
         case 'c':
             // 商品分类
             $this->name = Category::findOne($cid)->cat_name;
             $this->url = '';
             // 前台还没有做,暂时空起
             break;
         default:
             return false;
     }
     $this->ctype = $ctype;
     $this->cid = $cid;
     $this->type = 'middle';
     if ($this->insert(false)) {
         return true;
     }
     return false;
 }
Example #8
0
 public function beforeSave($insert)
 {
     $redirectLink = new Redirect(['from' => $this->fullLink]);
     if (!$this->link) {
         $this->link = TranslitHelper::to($this->title);
     }
     if ($this->isAttributeChanged('parent') || $this->isAttributeChanged('link')) {
         $this->fullLink = '';
         if ($parent = $this->getParentCategory()) {
             $linkParts = [$parent->link];
             while ($parent = $parent->getParentCategory()) {
                 $linkParts[] = $parent->getRelativeLink();
             }
             $this->fullLink = implode('/', $linkParts) . '/';
         }
         $this->fullLink .= $this->link;
         if ($childs = $this->getChildCategories()) {
             foreach ($childs as $childCategory) {
                 $childCategory->fullLink = $this->fullLink . '/' . $childCategory->link;
                 $childCategory->save(false);
             }
         }
         $redirectLink->to = $this->fullLink;
     }
     if ($redirectLink->to) {
         $redirectLink->save();
     }
     return parent::beforeSave($insert);
     // TODO: Change the autogenerated stub
 }
Example #9
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]);
 }
 public static function getProductByCategory($category_id, $limit = 6)
 {
     $category = new Category();
     $product = new Product();
     $categoriesData = $category->findAll(['parent_id' => $category_id]);
     $categoryIDs = [];
     if ($categoriesData) {
         foreach ($categoriesData as $categoryData) {
             $categoryIDs[] = $categoryData->id;
         }
         $products = $product->find()->where(['status' => 10, 'is_featured' => 10])->andWhere(['in', 'category_id', $categoryIDs])->limit($limit)->all();
     } else {
         $products = $product->find()->where(['status' => 10, 'is_front' => 10, 'category_id' => $category_id])->limit($limit)->all();
     }
     return $products;
 }
Example #11
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 #12
0
 protected function findById($id)
 {
     if (($model = Category::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('请求页面不存在');
     }
 }
 /**
  * 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 (($model = Category::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #14
0
 /**
  * Возвращает список возможных категорий товара.
  * Примечание: используется в backend/user/index представлении.
  *
  * @return mixed
  */
 public static function getCategoryList()
 {
     $categories = [];
     foreach (Category::getCategory() as $item_name) {
         $categories[$item_name->id] = $item_name->name;
     }
     return $categories;
 }
 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]);
 }
 public function actionDelete()
 {
     /**
      * @var $model Category
      */
     $model = Category::findOne(Yii::$app->request->post('id'));
     $model->delete();
 }
 /**
  * Action method for viewing a single forum category.
  * @param integer $id of the forum category to be shown.
  * @param string|null $slug of the forum category to be shown. Default value is `null` meaning the requested topic
  * has no set slug.
  * @return string|Response action method execution result.
  * @throws HttpException in case category cannot be found.
  */
 public function actionView($id, $slug = null)
 {
     $category = Category::findOne($slug === null ? $id : ['id' => $id, 'slug' => $slug]);
     if ($category === null) {
         throw new HttpException(404, 'Cannot find requested forum category!');
     }
     return $this->render('view', ['category' => $category]);
 }
Example #18
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 #19
0
 public function init()
 {
     parent::init();
     foreach (Category::find()->all() as $category) {
         $this->_catagories[$category->parent_id][] = $category;
     }
     //print_r($this->_catagories);die;
 }
 public function actionIndex($id)
 {
     $user = User::findIdentity($id);
     $comments = Comment::getRecentComments();
     $sidebarCategories = Category::getSidebarCategories();
     $tags = Tag::getTagList();
     return $this->render('index', ['user' => $user, 'comments' => $comments, 'sidebarCategories' => $sidebarCategories, 'tags' => $tags]);
 }
Example #21
0
 public function getCategoryObject()
 {
     if (!empty($this->_category)) {
         return $this->_category;
     }
     $category = Category::find()->where(['id' => $this->category])->one();
     return $this->_category = $category;
 }
Example #22
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])]);
 }
Example #23
0
 /**
  * Finds the Category model based on its slug.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $slug
  * @return Category the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findCategory($slug)
 {
     if (($model = Category::findOne(['slug' => $slug])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException(Yii::t('frontend', 'The requested page does not exist.'));
     }
 }
Example #24
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 #25
0
 /**
  * Возвращает модель категории.
  * @param int $id идентификатор категории
  * @throws NotFoundHttpException в случае, когда категория не найдена
  * @return Category
  */
 public function getCategory($id)
 {
     if (($model = Category::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested post does not exist.');
     }
 }
Example #26
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 #27
0
 public function actionCategory($url)
 {
     $category = Category::findOne(['url' => $url]);
     if (empty($category)) {
         throw new BadRequestHttpException('Category Not Found!', 404);
     }
     $posts = Post::find()->where(['category_id' => $category['id']])->with('category')->all();
     return $this->render('list', ['posts' => $posts]);
 }
Example #28
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 #29
0
 public function beforeDelete()
 {
     if (parent::beforeDelete()) {
         Category::deleteAll('main_category_id = ' . $this->id);
         return true;
     } else {
         return false;
     }
 }
Example #30
0
 public function getCategory()
 {
     $post = Post::findOne($this->id);
     foreach (explode(',', $post->category_id) as $value) {
         $category = Category::findOne($value);
         $data[] = ['id' => $category->id, 'parent_id' => $category->parent_id, 'title' => $category->title, 'indent' => $this->getIndent($category->indent)];
     }
     return $data;
 }