<commit_before><?php

namespace app\controllers;

use Yii;
use app\models\Category;
use app\models\CategorySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \yii\web\Response;
use yii\helpers\Html;

/**
 * CategoryController implements the CRUD actions for Category model.
 */
class CategoryController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => 'yii\filters\AccessControl',
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['create'],
                        'roles' => ['administrator','responsibleArea','executive'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['lists'],
                        'roles' => ['@', '?'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['index', 'view'],
                        'roles' => ['employeeArea','executive','responsibleArea','administrator'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['update'],
                        'roles' => ['responsibleArea','executive','administrator'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['delete'],
                        'roles' => ['administrator','responsibleArea','executive'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                    'bulk-delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Category models.
     * @return mixed
     */
    public function actionIndex()
    {    
        $searchModel = new CategorySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }


    /**
     * Displays a single Category model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {   
        $request = Yii::$app->request;
        if ($request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return [
                    'title'=> "Category #".$id,
                    'content'=>$this->renderAjax('view', [
                        'model' => $this->findModel($id),
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left', 
                            'data-dismiss'=>"modal"
                        ]).
                        Html::a('Edit', ['update','id'=>$id],  [
                            'class'=>'btn btn-primary',
                            'role'=>'modal-remote'
                        ])
                ];    
        } else {
            return $this->render('view', [
                'model' => $this->findModel($id),
            ]);
        }
    }

    /**
     * Creates a new Category model.
     * For ajax request will return json object
     * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $request = Yii::$app->request;
        $model = new Category();  

        if ($request->isAjax) {
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            if ($request->isGet) {
                return [
                    'title'=> "Create new Category",
                    'content'=>$this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::button('Save', [
                            'class'=>'btn btn-primary',
                            'type'=>"submit"
                        ])
        
                ];         
            } else if (   $model->load($request->post()) 
                       && $model->save())    {
                return [
                    'forceReload'=>'#crud-datatable-pjax',
                    'title'=> "Create new Category",
                    'content'=>'<span class="text-success">Create Category success</span>',
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::a('Create More', ['create'], [
                            'class'=>'btn btn-primary',
                            'role'=>'modal-remote'
                        ])
        
                ];         
            }   else   {           
                return [
                    'title'=> "Create new Category",
                    'content'=>$this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::button('Save', [
                            'class'=>'btn btn-primary',
                            'type'=>"submit"
                        ])
        
                ];         
            }
        }   else   {
            /*
            *   Process for non-ajax request
            */
            if (   $model->load($request->post()) 
                && $model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        }
       
    }

    /**
     * Updates an existing Category model.
     * For ajax request will return json object
     * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $request = Yii::$app->request;
        $model = $this->findModel($id);       

        if  ($request->isAjax)  {
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            if  ($request->isGet)  {
                return [
                    'title'=> "Update Category #".$id,
                    'content'=>$this->renderAjax('update', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::button('Save', [
                            'class'=>'btn btn-primary',
                            'type'=>"submit"
                        ])
                ];         
            }  else if (  $model->load($request->post()) 
                       && $model->save())  {
                return [
                    'forceReload'=>'#crud-datatable-pjax',
                    'title'=> "Category #".$id,
                    'content'=>$this->renderAjax('view', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::a('Edit', ['update','id'=>$id], [
                            'class'=>'btn btn-primary',
                            'role'=>'modal-remote'
                        ])
                ];    
            }  else  {
                 return [
                    'title'=> "Update Category #".$id,
                    'content'=>$this->renderAjax('update', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::button('Save', [
                            'class'=>'btn btn-primary',
                            'type'=>"submit"
                        ])
                ];        
            }
        }  else  {
            /*
            *   Process for non-ajax request
            */
            if (   $model->load($request->post()) 
                && $model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        }
    }

    /**
     * Delete an existing Category model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $request = Yii::$app->request;
        $this->findModel($id)->delete();

        if ($request->isAjax) {
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ['forceClose'=>true,'forceReload'=>'#crud-datatable-pjax'];
        } else {
            /*
            *   Process for non-ajax request
            */
            return $this->redirect(['index']);
        }


    }

     /**
     * Delete multiple existing Category model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionBulkDelete()
    {        
        $request = Yii::$app->request;
        $pks = $request->post('pks'); // Array or selected records primary keys
        foreach (Category::findAll(json_decode($pks)) as $model) {
            $model->delete();
        }
        

        if ($request->isAjax) {
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ['forceClose'=>true,'forceReload'=>'#crud-datatable-pjax'];
        } else {
            /*
            *   Process for non-ajax request
            */
            return $this->redirect(['index']);
        }
       
    }

    /**
     * Finds the Category model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Category the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Category::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionLists($id){
        $rows = \app\models\Category::find()->where(['id_area' => $id])->all();
        if(count($rows)>0){
            echo "<option value='0'>Opcional: Selecciona una opcion</option>";
            foreach($rows as $row){
                echo "<option value='$row->id'>$row->name</option>";
            }
        }else{
            echo "<option value='0'>No hay categorias en esta área</option>";
        }
    }
}
<commit_msg>Cambio Correctivo: Se comenta el codigo que hace que salga un error 403 en categorias<commit_after><?php

namespace app\controllers;

use Yii;
use app\models\Category;
use app\models\CategorySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \yii\web\Response;
use yii\helpers\Html;

/**
 * CategoryController implements the CRUD actions for Category model.
 */
class CategoryController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            /*
            'access' => [
                'class' => 'yii\filters\AccessControl',
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['create'],
                        'roles' => ['administrator','responsibleArea','executive'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['lists'],
                        'roles' => ['@', '?'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['index', 'view'],
                        'roles' => ['employeeArea','executive','responsibleArea','administrator'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['update'],
                        'roles' => ['responsibleArea','executive','administrator'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['delete'],
                        'roles' => ['administrator','responsibleArea','executive'],
                    ],
                ],
            ],
            */
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                    'bulk-delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Category models.
     * @return mixed
     */
    public function actionIndex()
    {    
        $searchModel = new CategorySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }


    /**
     * Displays a single Category model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {   
        $request = Yii::$app->request;
        if ($request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return [
                    'title'=> "Category #".$id,
                    'content'=>$this->renderAjax('view', [
                        'model' => $this->findModel($id),
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left', 
                            'data-dismiss'=>"modal"
                        ]).
                        Html::a('Edit', ['update','id'=>$id],  [
                            'class'=>'btn btn-primary',
                            'role'=>'modal-remote'
                        ])
                ];    
        } else {
            return $this->render('view', [
                'model' => $this->findModel($id),
            ]);
        }
    }

    /**
     * Creates a new Category model.
     * For ajax request will return json object
     * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $request = Yii::$app->request;
        $model = new Category();  

        if ($request->isAjax) {
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            if ($request->isGet) {
                return [
                    'title'=> "Create new Category",
                    'content'=>$this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::button('Save', [
                            'class'=>'btn btn-primary',
                            'type'=>"submit"
                        ])
        
                ];         
            } else if (   $model->load($request->post()) 
                       && $model->save())    {
                return [
                    'forceReload'=>'#crud-datatable-pjax',
                    'title'=> "Create new Category",
                    'content'=>'<span class="text-success">Create Category success</span>',
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::a('Create More', ['create'], [
                            'class'=>'btn btn-primary',
                            'role'=>'modal-remote'
                        ])
        
                ];         
            }   else   {           
                return [
                    'title'=> "Create new Category",
                    'content'=>$this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::button('Save', [
                            'class'=>'btn btn-primary',
                            'type'=>"submit"
                        ])
        
                ];         
            }
        }   else   {
            /*
            *   Process for non-ajax request
            */
            if (   $model->load($request->post()) 
                && $model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        }
       
    }

    /**
     * Updates an existing Category model.
     * For ajax request will return json object
     * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $request = Yii::$app->request;
        $model = $this->findModel($id);       

        if  ($request->isAjax)  {
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            if  ($request->isGet)  {
                return [
                    'title'=> "Update Category #".$id,
                    'content'=>$this->renderAjax('update', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::button('Save', [
                            'class'=>'btn btn-primary',
                            'type'=>"submit"
                        ])
                ];         
            }  else if (  $model->load($request->post()) 
                       && $model->save())  {
                return [
                    'forceReload'=>'#crud-datatable-pjax',
                    'title'=> "Category #".$id,
                    'content'=>$this->renderAjax('view', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::a('Edit', ['update','id'=>$id], [
                            'class'=>'btn btn-primary',
                            'role'=>'modal-remote'
                        ])
                ];    
            }  else  {
                 return [
                    'title'=> "Update Category #".$id,
                    'content'=>$this->renderAjax('update', [
                        'model' => $model,
                    ]),
                    'footer'=> 
                        Html::button('Close', [
                            'class'=>'btn btn-default pull-left',
                            'data-dismiss'=>"modal"
                        ]).
                        Html::button('Save', [
                            'class'=>'btn btn-primary',
                            'type'=>"submit"
                        ])
                ];        
            }
        }  else  {
            /*
            *   Process for non-ajax request
            */
            if (   $model->load($request->post()) 
                && $model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        }
    }

    /**
     * Delete an existing Category model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $request = Yii::$app->request;
        $this->findModel($id)->delete();

        if ($request->isAjax) {
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ['forceClose'=>true,'forceReload'=>'#crud-datatable-pjax'];
        } else {
            /*
            *   Process for non-ajax request
            */
            return $this->redirect(['index']);
        }


    }

     /**
     * Delete multiple existing Category model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionBulkDelete()
    {        
        $request = Yii::$app->request;
        $pks = $request->post('pks'); // Array or selected records primary keys
        foreach (Category::findAll(json_decode($pks)) as $model) {
            $model->delete();
        }
        

        if ($request->isAjax) {
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ['forceClose'=>true,'forceReload'=>'#crud-datatable-pjax'];
        } else {
            /*
            *   Process for non-ajax request
            */
            return $this->redirect(['index']);
        }
       
    }

    /**
     * Finds the Category model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Category the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Category::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionLists($id){
        $rows = \app\models\Category::find()->where(['id_area' => $id])->all();
        if(count($rows)>0){
            echo "<option value='0'>Opcional: Selecciona una opcion</option>";
            foreach($rows as $row){
                echo "<option value='$row->id'>$row->name</option>";
            }
        }else{
            echo "<option value='0'>No hay categorias en esta área</option>";
        }
    }
}

{'fraction_non_alphanumeric': 0.13843622059886007, 'fraction_numerical': 0.0006020711246688609, 'mean_word_length': 1.0558626949418268, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 337, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4103657', 'n_tokens_mistral': 6177, 'n_tokens_neox': 5844, 'n_words': 1232}