Guest User

componentcomtroller

a guest
Sep 7th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.73 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use Yii;
  6. use app\models\Component;
  7. use yii\helpers\ArrayHelper;
  8. use yii\data\ActiveDataProvider;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12. use app\models\Tractormodel;
  13. use app\models\Dependency;
  14.  
  15. /**
  16.  * ComponentController implements the CRUD actions for Component model.
  17.  */
  18. class ComponentController extends Controller {
  19.  
  20.     /**
  21.       +     * @inheritdoc
  22.       + */
  23.     public function behaviors() {
  24.         return [
  25.             'verbs' => [
  26.                 'class' => VerbFilter::className(),
  27.                 'actions' => [
  28.                     'delete' => ['POST'],
  29.                 ],
  30.             ],
  31.         ];
  32.     }
  33.  
  34.     /**
  35.      * Lists all Component models.
  36.      * @return mixed
  37.      */
  38.     public function actionIndex() {
  39.         $dataProvider = new ActiveDataProvider([
  40.             'query' => Component::find()->with("dependencies"),
  41.         ]);
  42.  
  43.         return $this->render('index', [
  44.                     'dataProvider' => $dataProvider,
  45.         ]);
  46.     }
  47.  
  48.     /**
  49.      * Displays a single Component model.
  50.      * @param integer $id
  51.      * @return mixed
  52.      */
  53.     public function actionView($id) {
  54.  
  55.         $tractorModels = ArrayHelper::map(Tractormodel::find()->all(), 'id', 'model');
  56.  
  57.         $dataProvider = new ActiveDataProvider([
  58.             'query' => Component::find()->with("dependencies"),
  59.         ]);
  60.  
  61.         return $this->render('view', [
  62.                     'model' => $this->findModel($id), 'tractorModels' => $tractorModels, 'dataProvider' => $dataProvider,
  63.         ]);
  64.     }
  65.  
  66.     /**
  67.      * Creates a new Component model.
  68.      * If creation is successful, the browser will be redirected to the 'view' page.
  69.      * @return mixed
  70.      */
  71.     public function actionCreate() {
  72.         $model = new Component();
  73.  
  74.         $tractorModels = ArrayHelper::map(Tractormodel::find()->all(), 'id', 'model');
  75.  
  76.         $components = Component::findBySql(" SELECT c.* FROM component c WHERE c.id NOT IN (SELECT component_id FROM dependency d) ")->all();
  77.  
  78.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  79.  
  80.             $dependendComponents = Yii::$app->request->bodyParams['ids'];
  81.             foreach ($dependendComponents as $dComp) {
  82.  
  83.                 $dependencyModel = new Dependency();
  84.                 $dependencyModel->setAttributes([
  85.                     'count' => $dComp['quantity'],
  86.                     'component_id' => $model->id,
  87.                     'dependent_id' => $dComp['id']
  88.                 ]);
  89.                 $dependencyModel->save();
  90.             }
  91.  
  92.             return $this->redirect(['index', 'id' => $model->id]);
  93.         } else {
  94.             return $this->render('create', ['model' => $model, 'tractorModels' => $tractorModels, 'components' => $components,
  95.             ]);
  96.         }
  97.     }
  98.  
  99.     /**
  100.      * Updates an existing Component model.
  101.      * If update is successful, the browser will be redirected to the 'view' page.
  102.      * @param integer $id
  103.      * @return mixed
  104.      */
  105.     public function actionUpdate($id) {
  106.  
  107.         $model = Component::find()->where(['id' => $id])->one();
  108.  
  109.         $tractorModels = ArrayHelper::map(Tractormodel::find()->all(), 'id', 'model');
  110.  
  111.         $sql = "SELECT c.* FROM component c WHERE c.id NOT IN (SELECT component_id FROM dependency d) AND id != :currentComponent ";
  112.  
  113.         $components = Component::findBySql($sql, ['currentComponent' => $id])->all();
  114.  
  115.         $depModels = Dependency::find()->where(['component_id' => $id])->all();
  116.  
  117.         $deletedIDs = Yii::$app->request->post("deletedIds");
  118.  
  119.         if ($deletedIDs && is_array($deletedIDs)) {
  120.             Dependency::deleteAll(['IN', 'id', $deletedIDs]);
  121.         }
  122.  
  123.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  124.  
  125.             $dependendComponents = Yii::$app->request->bodyParams['ids'];
  126.  
  127.             foreach ($dependendComponents as $dComp) {
  128.  
  129.                 if (array_key_exists('dependentId', $dComp) && isset($dComp['dependentId'])) {
  130.                     $dependencyModel = Dependency::find()->where(['id' => $dComp['dependentId']])->one();
  131.                 } else {
  132.                     $dependencyModel = new Dependency();
  133.                }
  134.  
  135.                 $dependencyModel->setAttributes([
  136.                     'count' => $dComp['quantity'],
  137.                     'component_id' => $model->id,
  138.                     'dependent_id' => $dComp['id']
  139.                 ]);
  140.                 $dependencyModel->save();
  141.                  
  142.             }
  143.             return $this->redirect(['index', 'id' => $model->id]);
  144.         } else {
  145.             return $this->render('update', [
  146.                         'model' => $model, 'tractorModels' => $tractorModels,
  147.                         'components' => $components, 'depModels' => $depModels,
  148.             ]);
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Deletes an existing Component model.
  154.      * If deletion is successful, the browser will be redirected to the 'index' page.
  155.      * @param integer $id
  156.      * @return mixed
  157.      */
  158.     public function actionDelete($id) {
  159.         $this->findModel($id)->delete();
  160.  
  161.         return $this->redirect(['index']);
  162.     }
  163.  
  164.     /**
  165.      * Finds the Component model based on its primary key value.
  166.      * If the model is not found, a 404 HTTP exception will be thrown.
  167.      * @param integer $id
  168.      * @return Component the loaded model
  169.      * @throws NotFoundHttpException if the model cannot be found
  170.      */
  171.     protected function findModel($id) {
  172.         if (($model = Component::findOne($id)) !== null) {
  173.             return $model;
  174.         } else {
  175.             throw new NotFoundHttpException('The requested page does not exist.');
  176.         }
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment