private static function findAllAncestry($categoryId, $parentCategoriesArray = []) { $category = Category::findOne($categoryId); $parentCategoryId = $category->parent_id; if (!empty($parentCategoryId)) { $parentCategoriesArray[] = $parentCategoryId; return self::findAllAncestry($parentCategoryId, $parentCategoriesArray); } else { return $parentCategoriesArray; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Category::find()->joinWith('translations')->orderBy(['parent_id' => SORT_ASC, 'position' => SORT_ASC]); $this->load($params); // grid filtering conditions $query->andFilterWhere(['shop_category.parent_id' => $this->parent_id])->andFilterWhere(['like', 'shop_category_translation.title', $this->title])->andFilterWhere(['shop_category.show' => $this->show]); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } return $dataProvider; }
public function safeUp() { $this->addColumn('shop_product', 'position', 'INT(5) AFTER id'); $categories = Category::find()->all(); foreach ($categories as $category) { $products = Product::find()->where(['category_id' => $category->id])->all(); for ($i = 0; $i < count($products); $i++) { $this->execute('UPDATE shop_product SET position=' . ($i + 1) . ' WHERE id=' . $products[$i]->id . ' AND category_id=' . $category->id); } } $this->addColumn('shop_category', 'position', $this->integer()); for ($i = 0; $i < count($categories); $i++) { $this->execute('UPDATE shop_category SET position=' . ($i + 1) . ' WHERE id=' . $categories[$i]->id); } }
<!--CONTENT--> <div class="panel-body"> <?php Pjax::begin(['linkSelector' => '.pjax', 'enablePushState' => true, 'timeout' => 10000]); ?> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'filterRowOptions' => ['class' => 'm-b-sm m-t-sm'], 'options' => ['class' => 'project-list'], 'tableOptions' => ['id' => 'my-grid', 'class' => 'table table-hover'], 'summary' => "", 'columns' => [['headerOptions' => ['class' => 'text-center col-md-1'], 'format' => 'html', 'label' => Yii::t('shop', 'Position'), 'value' => function ($model) { $buttonUp = Html::a('', Url::toRoute(['up', 'id' => $model->id]), ['class' => 'pjax product-nav glyphicon glyphicon-arrow-up text-primary pull-left']); $buttonDown = Html::a('', Url::toRoute(['down', 'id' => $model->id]), ['class' => 'pjax product-nav glyphicon glyphicon-arrow-down text-primary pull-left']); return $buttonUp . $model->position . $buttonDown; }, 'contentOptions' => ['class' => 'vote-actions col-md-1']], ['headerOptions' => ['class' => 'text-center col-md-4'], 'attribute' => 'title', 'value' => function ($model) { $content = Html::a($model->translation->title, Url::toRoute(['save', 'id' => $model->id, 'languageId' => Language::getCurrent()->id])); $content .= '<br><small>' . Yii::t('shop', 'Created') . ' ' . $model->creation_time . '</small>'; return $content; }, 'label' => Yii::t('shop', 'Title'), 'format' => 'html', 'contentOptions' => ['class' => 'project-title col-md-4']], ['headerOptions' => ['class' => 'text-center col-md-2'], 'attribute' => 'category', 'value' => 'category.translation.title', 'label' => Yii::t('shop', 'Category'), 'format' => 'text', 'filter' => ArrayHelper::map(Category::find()->all(), 'id', 'translation.title'), 'contentOptions' => ['class' => 'project-title col-md-2']], ['headerOptions' => ['class' => 'text-center col-md-1'], 'value' => 'price', 'label' => Yii::t('shop', 'Price'), 'format' => 'text', 'contentOptions' => ['class' => 'col-md-1 text-center']], ['headerOptions' => ['class' => 'text-center col-md-1'], 'attribute' => 'images', 'value' => function ($model) { $content = ''; $number = 3; $i = 0; foreach ($model->images as $image) { if (!empty($image)) { if ($i < $number) { $content .= Html::img($image->small, ['class' => 'img-circle']); $i++; } } } return Html::a($content, Url::toRoute(['add-image', 'id' => $model->id, 'languageId' => Language::getCurrent()->id])); }, 'label' => Yii::t('shop', 'Images'), 'format' => 'html', 'contentOptions' => ['class' => 'col-md-1 project-people']], ['headerOptions' => ['class' => 'text-center col-md-1'], 'attribute' => \Yii::t('shop', 'Status'), 'value' => function ($model) { switch ($model->status) { case Product::STATUS_ON_MODERATION:
/** * Creates a URL according to the given route and parameters. * @param UrlManager $manager the URL manager * @param string $route the route. It should not have slashes at the beginning or the end. * @param array $params the parameters * @return string|boolean the created URL, or false if this rule cannot be used for creating this URL. */ public function createUrl($manager, $route, $params) { $pathInfo = ''; if ($route == $this->categoryRoute && empty($params['id'])) { $pathInfo = $this->prefix; } else { if (($route == $this->productRoute || $route == $this->categoryRoute) && !empty($params['id'])) { $id = $params['id']; $parentId = null; $language = Language::findOne(['lang_id' => $manager->language]); if ($route == $this->productRoute) { $product = Product::findOne($id); if (empty($product)) { return false; } if ($product->getTranslation($language->id) && $product->getTranslation($language->id)->seoUrl) { $pathInfo = $product->getTranslation($language->id)->seoUrl; $parentId = $product->category_id; } else { return false; } } else { if ($route == $this->categoryRoute) { $category = Category::findOne($id); if ($category->getTranslation($language->id) && $category->getTranslation($language->id)->seoUrl) { $pathInfo = $category->getTranslation($language->id)->seoUrl; $parentId = $category->parent_id; } else { return false; } } } while ($parentId != null) { $category = Category::findOne($parentId); if ($category->getTranslation($language->id) && $category->getTranslation($language->id)->seoUrl) { $pathInfo = $category->getTranslation($language->id)->seoUrl . '/' . $pathInfo; $parentId = $category->parent_id; } else { return false; } } if (!empty($this->prefix)) { $pathInfo = $this->prefix . '/' . $pathInfo; } unset($params['id']); } else { return false; } } return $pathInfo . '?' . http_build_query($params); }
/** * Shows or hides category * * @param integer $id * @return mixed * @throws ForbiddenHttpException */ public function actionSwitchShow($id) { $category = Category::findOne($id); if (!empty($category)) { $category->show = !$category->show; $category->save(); } return $this->actionIndex(); }
/** * @param Category $category * @return array * * Gets children of category and its children. */ public function getDescendants($category) { $children = $category->getChildren($category->id); if (!empty($children)) { foreach ($children as $child) { $grandChildren = $this->getDescendants($child); if (!empty($grandChildren)) { $children = array_merge($children, $grandChildren); } } } return $children; }
/** * @return \yii\db\ActiveQuery */ public function getCategory() { return $this->hasOne(Category::className(), ['id' => 'category_id']); }
/** * @inheritdoc */ public function rules() { return [[['category_id', 'filter_type', 'input_type'], 'integer'], [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']], [['filter_type'], 'exist', 'skipOnError' => true, 'targetClass' => FilterType::className(), 'targetAttribute' => ['filter_type' => 'id']], [['input_type'], 'exist', 'skipOnError' => true, 'targetClass' => FilterInputType::className(), 'targetAttribute' => ['input_type' => 'id']]]; }