Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\modules\catalog\models\forms;
- use app\modules\catalog\models\Product;
- use app\modules\catalog\models\ProductManufacturer;
- use app\modules\catalog\models\ProductSpareType;
- use yii\data\ActiveDataProvider;
- use yii\helpers\ArrayHelper;
- /**
- * Form for search products filter
- */
- class SearchProductsForm extends Product
- {
- /**
- * @var string $categoryId Category id
- */
- public $categoryId;
- /**
- * @var integer Count products on one page
- */
- const COUNT_PRODUCTS_ON_PAGE = 12;
- /**
- * {@inheritDoc}
- *
- * @return array
- */
- public function rules()
- {
- return [
- [['manufacturer_id', 'type_spare_id', 'categoryId'], 'integer'],
- ];
- }
- /**
- * Search products by filter
- *
- * @param array $params Search params
- * @return \yii\db\ActiveDataProvider
- */
- public function search($params)
- {
- $query = self::find()
- ->where(['active' => 1, 'category_id' => $this->categoryId])
- ->orderBy(['title' => SORT_ASC]);
- $dataProvider = new ActiveDataProvider([
- 'query' => $query,
- 'pagination' => [
- 'pageSize' => self::COUNT_PRODUCTS_ON_PAGE,
- 'forcePageParam' => false,
- 'pageSizeParam' => false,
- ],
- ]);
- $this->load($params);
- if (!$this->validate()) {
- return $dataProvider;
- }
- $query->andFilterWhere([
- 'manufacturer_id' => $this->manufacturer_id,
- 'type_spare_id' => $this->type_spare_id,
- ]);
- return $dataProvider;
- }
- /**
- * Get hash table for manufactures
- *
- * @return array
- */
- public function getListManufactures()
- {
- return ArrayHelper::map(ProductManufacturer::find()->where(['active' => 1])->all(), 'id', 'title');
- }
- /**
- * Get hash table for spare types
- *
- * @return array
- */
- public function getListSpareTypes()
- {
- return ArrayHelper::map(ProductSpareType::find()->where(['active' => 1])->all(), 'id', 'title');
- }
- /**
- * Get active name manufacture in filter
- *
- * @return string
- */
- public function getActiveManufacture()
- {
- $manufacturer = ProductManufacturer::find()
- ->where(['id' => $this->manufacturer_id, 'active' => 1])
- ->one();
- if ($manufacturer) {
- return $manufacturer->title;
- }
- return 'Производитель';
- }
- /**
- * Get active name spare type in filter
- *
- * @return string
- */
- public function getActiveSpareType()
- {
- $typeSpare = ProductSpareType::find()
- ->where(['id' => $this->type_spare_id, 'active' => 1])
- ->one();
- if ($typeSpare) {
- return $typeSpare->title;
- }
- return 'Тип запчасти';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement