Advertisement
Guest User

Untitled

a guest
Feb 14th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\modules\catalog\models\forms;
  4.  
  5. use app\modules\catalog\models\Product;
  6. use app\modules\catalog\models\ProductManufacturer;
  7. use app\modules\catalog\models\ProductSpareType;
  8. use yii\data\ActiveDataProvider;
  9. use yii\helpers\ArrayHelper;
  10.  
  11. /**
  12.  * Form for search products filter
  13.  */
  14. class SearchProductsForm extends Product
  15. {
  16.     /**
  17.      * @var string $categoryId Category id
  18.      */
  19.     public $categoryId;
  20.  
  21.     /**
  22.      * @var integer Count products on one page
  23.      */
  24.     const COUNT_PRODUCTS_ON_PAGE = 12;
  25.  
  26.     /**
  27.      * {@inheritDoc}
  28.      *
  29.      * @return array
  30.      */
  31.     public function rules()
  32.     {
  33.         return [
  34.             [['manufacturer_id', 'type_spare_id', 'categoryId'], 'integer'],
  35.         ];
  36.     }
  37.  
  38.     /**
  39.      * Search products by filter
  40.      *
  41.      * @param array $params Search params
  42.      * @return \yii\db\ActiveDataProvider
  43.      */
  44.     public function search($params)
  45.     {
  46.         $query = self::find()
  47.             ->where(['active' => 1, 'category_id' => $this->categoryId])
  48.             ->orderBy(['title' => SORT_ASC]);
  49.  
  50.         $dataProvider = new ActiveDataProvider([
  51.             'query' => $query,
  52.             'pagination' => [
  53.                 'pageSize' => self::COUNT_PRODUCTS_ON_PAGE,
  54.                 'forcePageParam' => false,
  55.                 'pageSizeParam' => false,
  56.             ],
  57.         ]);
  58.  
  59.         $this->load($params);
  60.  
  61.         if (!$this->validate()) {
  62.             return $dataProvider;
  63.         }
  64.  
  65.         $query->andFilterWhere([
  66.             'manufacturer_id' => $this->manufacturer_id,
  67.             'type_spare_id'   => $this->type_spare_id,
  68.         ]);
  69.  
  70.         return $dataProvider;
  71.     }
  72.  
  73.     /**
  74.      * Get hash table for manufactures
  75.      *
  76.      * @return array
  77.      */
  78.     public function getListManufactures()
  79.     {
  80.         return ArrayHelper::map(ProductManufacturer::find()->where(['active' => 1])->all(), 'id', 'title');
  81.     }
  82.  
  83.     /**
  84.      * Get hash table for spare types
  85.      *
  86.      * @return array
  87.      */
  88.     public function getListSpareTypes()
  89.     {
  90.         return ArrayHelper::map(ProductSpareType::find()->where(['active' => 1])->all(), 'id', 'title');
  91.     }
  92.  
  93.     /**
  94.      * Get active name manufacture in filter
  95.      *
  96.      * @return string
  97.      */
  98.     public function getActiveManufacture()
  99.     {
  100.         $manufacturer = ProductManufacturer::find()
  101.             ->where(['id' => $this->manufacturer_id, 'active' => 1])
  102.             ->one();
  103.  
  104.         if ($manufacturer) {
  105.             return $manufacturer->title;
  106.         }
  107.  
  108.         return 'Производитель';
  109.     }
  110.  
  111.     /**
  112.      * Get active name spare type in filter
  113.      *
  114.      * @return string
  115.      */
  116.     public function getActiveSpareType()
  117.     {
  118.         $typeSpare = ProductSpareType::find()
  119.             ->where(['id' => $this->type_spare_id, 'active' => 1])
  120.             ->one();
  121.  
  122.         if ($typeSpare) {
  123.             return $typeSpare->title;
  124.         }
  125.  
  126.         return 'Тип запчасти';
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement