<?php
    
    namespace backend\controllers;
    
    use common\modules\language\models\Language;
    use common\modules\product\models\Category;
    use common\modules\rubrication\models\TaxGroup;
    use common\modules\rubrication\models\TaxOption;
    use Yii;
    use common\models\CategoryToPurpose;
    use common\models\CategoryToPurposeSearch;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;
    
    /**
     * CategoryToPurposeController implements the CRUD actions for CategoryToPurpose model.
     */
    class CategoryToPurposeController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'verbs' => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'delete' => [ 'POST' ],
                    ],
                ],
            ];
        }
        
        /**
         * Lists all CategoryToPurpose models.
         *
         * @return mixed
         */
        public function actionIndex()
        {
            $searchModel = new CategoryToPurposeSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            
            return $this->render(
                'index',
                [
                    'searchModel'  => $searchModel,
                    'dataProvider' => $dataProvider,
                ]
            );
        }
        
        /**
         * Displays a single CategoryToPurpose model.
         *
         * @param integer $category_id
         * @param integer $purpose_id
         *
         * @return mixed
         */
        public function actionView($category_id, $purpose_id)
        {
            return $this->render(
                'view',
                [
                    'model' => $this->findModel($category_id, $purpose_id),
                ]
            );
        }
        
        /**
         * Creates a new CategoryToPurpose model.
         * If creation is successful, the browser will be redirected to the 'view' page.
         *
         * @return mixed
         */
        public function actionCreate()
        {
            $model = new CategoryToPurpose();
            
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(
                    [
                        'view',
                        'category_id' => $model->category_id,
                        'purpose_id'  => $model->purpose_id,
                    ]
                );
            } else {
                
                $language_id = Language::getDefaultLanguage()->language_id;
                
                $categories = Category::find()
                                      ->innerJoinWith('lang')
                                      ->select(
                                          [
                                              'category_lang.name',
                                              'category.category_id',
                                          ]
                                      )
                                      ->where([ 'category_lang.language_id' => $language_id ])
                                      ->indexBy('category_id')
                                      ->column();
                $purposes = TaxOption::find()
                                     ->innerJoinWith('taxGroup')
                                     ->where(
                                         [
                                             'tax_group.tax_group_id'      => 5,
                                             'level'                       => 0,
                                             'tax_option_lang.language_id' => $language_id,
                                         ]
                                     )
                                     ->joinWith('lang')
                                     ->select(
                                         [
                                             'tax_option_lang.value',
                                             'tax_option.tax_option_id',
                                         ]
                                     )
                                     ->indexBy('tax_option_id')
                                     ->column();
                
                return $this->render(
                    'create',
                    [
                        'model'      => $model,
                        'categories' => $categories,
                        'purposes'   => $purposes,
                    ]
                );
            }
        }
        
        /**
         * Updates an existing CategoryToPurpose model.
         * If update is successful, the browser will be redirected to the 'view' page.
         *
         * @param integer $category_id
         * @param integer $purpose_id
         *
         * @return mixed
         */
        public function actionUpdate($category_id, $purpose_id)
        {
            $model = $this->findModel($category_id, $purpose_id);
            
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(
                    [
                        'view',
                        'category_id' => $model->category_id,
                        'purpose_id'  => $model->purpose_id,
                    ]
                );
            } else {
                
                $language_id = Language::getDefaultLanguage()->language_id;
                
                $categories = Category::find()
                                      ->innerJoinWith('lang')
                                      ->select(
                                          [
                                              'category_lang.name',
                                              'category.category_id',
                                          ]
                                      )
                                      ->where([ 'category_lang.language_id' => $language_id ])
                                      ->indexBy('category_id')
                                      ->column();
                $purposes = TaxOption::find()
                                     ->innerJoinWith('taxGroup')
                                     ->where(
                                         [
                                             'tax_group.tax_group_id'      => 5,
                                             'level'                       => 0,
                                             'tax_option_lang.language_id' => $language_id,
                                         ]
                                     )
                                     ->joinWith('lang')
                                     ->select(
                                         [
                                             'tax_option_lang.value',
                                             'tax_option.tax_option_id',
                                         ]
                                     )
                                     ->indexBy('tax_option_id')
                                     ->column();
                
                return $this->render(
                    'update',
                    [
                        'model'      => $model,
                        'categories' => $categories,
                        'purposes'   => $purposes,
                    ]
                );
            }
        }
        
        /**
         * Deletes an existing CategoryToPurpose model.
         * If deletion is successful, the browser will be redirected to the 'index' page.
         *
         * @param integer $category_id
         * @param integer $purpose_id
         *
         * @return mixed
         */
        public function actionDelete($category_id, $purpose_id)
        {
            $this->findModel($category_id, $purpose_id)
                 ->delete();
            
            return $this->redirect([ 'index' ]);
        }
        
        /**
         * Finds the CategoryToPurpose model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         *
         * @param integer $category_id
         * @param integer $purpose_id
         *
         * @return CategoryToPurpose the loaded model
         * @throws NotFoundHttpException if the model cannot be found
         */
        protected function findModel($category_id, $purpose_id)
        {
            if (( $model = CategoryToPurpose::findOne(
                    [
                        'category_id' => $category_id,
                        'purpose_id'  => $purpose_id,
                    ]
                ) ) !== null
            ) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
    }
