/backend/modules/systemAdmin/Catalog/controllers/__CatalogitemController.php
PHP | 388 lines | 243 code | 47 blank | 98 comment | 27 complexity | 5a4f83dbc66c764a60db4808e575a38c MD5 | raw file
- <?php
- namespace app\modules\systemAdmin\Catalog\controllers;
- use backend\modules\systemAdmin\Catalog\models\Models;
- use backend\modules\systemAdmin\Catalog\models\Section;
- use backend\modules\systemAdmin\Catalog\models\Size;
- use backend\modules\systemAdmin\Catalog\models\SystemCatalogs;
- use common\models\catalog\CatalogItemSize;
- use Yii;
- use backend\modules\systemAdmin\Catalog\models\CatalogItem;
- use backend\modules\systemAdmin\Catalog\models\CatalogItemSearch;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use \yii\web\Response;
- use yii\helpers\Html;
- /**
- * CatalogitemController implements the CRUD actions for CatalogItem model.
- */
- class CatalogitemController extends Controller
- {
- private $rootID = 1;
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['post'],
- 'bulk-delete' => ['post'],
- ],
- ],
- ];
- }
- /**
- * Lists all CatalogItem models.
- * @return mixed
- */
- public function actionIndex()
- {
- $searchModel = new CatalogItemSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single CatalogItem 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'=> "CatalogItem #".$id,
- 'content'=>$this->renderPartial('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 CatalogItem 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 CatalogItem();
- $itemSizeModel = [new CatalogItemSize];
- $size = Size::find()->all();
- // var_dump($itemSizeModel);
- // exit;
- if($request->isAjax){
- /*
- * Process for ajax request
- */
- Yii::$app->response->format = Response::FORMAT_JSON;
- if($request->isGet){
- return [
- 'title'=> "Create new CatalogItem",
- 'content'=>$this->renderPartial('create', [
- 'model' => $model,
- 'itemSizeModel' => (empty($itemSizeModel)) ? [new CatalogItemSize] : $itemSizeModel,
- // 'size' => $size,
- ]),
- '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()){
- // echo '<pre>';
- // print_r($request->post());
- // print_r($model);
- // exit;
- return [
- 'forceReload'=>'true',
- 'title'=> "Create new CatalogItem",
- 'content'=>'<span class="text-success">Create CatalogItem 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 CatalogItem",
- 'content'=>$this->renderPartial('create', [
- 'model' => $model,
- 'itemSizeModel' => (empty($itemSizeModel)) ? [new CatalogItemSize] : $itemSizeModel,
- // 'size' => $size,
- ]),
- '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,
- 'itemSizeModel' => (empty($itemSizeModel)) ? [new CatalogItemSize] : $itemSizeModel,
- // 'size' => $size,
- ]);
- }
- }
- }
- /**
- * Updates an existing CatalogItem 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)
- {
- if($id == $this->rootID) return $this->redirect(['index']);
- $request = Yii::$app->request;
- $model = $this->findModel($id);
- // echo '<pre>';
- // print_r($request->post());
- // print_r($model);
- // exit;
- if($request->isAjax){
- /*
- * Process for ajax request
- */
- Yii::$app->response->format = Response::FORMAT_JSON;
- if($request->isGet){
- // echo '<pre>';
- // print_r($this->findModel($id)->catalog);
- return [
- 'title'=> "Update CatalogItem #".$id,
- 'content'=>$this->renderPartial('update', [
- 'model' => $this->findModel($id),
- ]),
- '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'=>'true',
- 'title'=> "CatalogItem #".$id,
- 'content'=>$this->renderPartial('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{
- print_r($model);
- exit;
- return [
- 'title'=> "Update CatalogItem #".$id,
- 'content'=>$this->renderPartial('update', [
- 'model' => $this->findModel($id),
- ]),
- '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 CatalogItem 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)
- {
- if($id == $this->rootID) return $this->redirect(['index']);
- $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'=>true];
- }else{
- /*
- * Process for non-ajax request
- */
- return $this->redirect(['index']);
- }
- }
- /**
- * Delete multiple existing CatalogItem 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 (CatalogItem::findAll(json_decode($pks)) as $model) {
- if($model->id != $this->rootID){
- $model->delete();
- }
- }
-
- if($request->isAjax){
- /*
- * Process for ajax request
- */
- Yii::$app->response->format = Response::FORMAT_JSON;
- return ['forceClose'=>true,'forceReload'=>true];
- }else{
- /*
- * Process for non-ajax request
- */
- return $this->redirect(['index']);
- }
-
- }
- /**
- * Finds the CatalogItem model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return CatalogItem the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = CatalogItem::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- public function actionLists($id){
- $countItems = CatalogItem::find()
- ->leftJoin(Section::tableName(), ['section.id' => 'catalog_item.section_id'])
- ->where(['catalog_item.section_id' => $id])
- ->count();
- $items = CatalogItem::find()
- ->where(['section_id' => $id])
- ->all();
- if($countItems > 0){
- echo "<option value=''>" . Yii::t('app', 'Select section') . "</option>";
- echo "<option value='1'>" . Yii::t('app', 'root') . "</option>";
- foreach($items as $item){
- echo "<option value='" . $item->id . "'>" . $item->name . "</option>";
- }
- }else{
- echo "<option value=''>" . Yii::t('app', 'Select section') . "</option>";
- echo "<option value='1'>" . Yii::t('app', 'root') . "</option>";
- }
- }
- public function actionModellists($id){
- $countItems = Models::find()
- ->where(['type_id' => $id])
- ->count();
- $items = Models::find()
- ->leftJoin('detail_instances', ['details.instance_id' => 'detail_instances.id'])
- ->leftJoin('instance_icons', ['instance_icons.id' => 'detail_instances.icon_id'])
- ->leftJoin('detail_types', ['detail_types.id' => 'details.type_id'])
- ->where(['type_id' => $id])
- ->all();
- // print_r($items[0]->icon->icon->icon);
- if($countItems > 0){
- echo '<ul style="list-style-type: none; height: 100px; width: 100%; padding-top: 10px; display: inline-block;">';
- // echo "<option value=''>" . Yii::t('app', 'Select section') . "</option>";
- // echo "<option value='1'>" . Yii::t('app', 'root') . "</option>";
- foreach($items as $item){
- // echo '<option onmouseover="alert();" value="' . $item->id . '" src="/advanced/backend/web/' . $item->type->name . '/' . $item->icon->icon->icon . '"></option>';
- echo '<li class="iconItem"
- style="display: inline-block; width: 42px; height: 72px; padding-left: 5px; padding-right: 5px;"
- id="' . $item->id . '" src="/advanced/backend/web/' . $item->icon->icon->icon . '">
- <img height="70" width="40" src="/advanced/backend/web/' . $item->icon->icon->icon . '">
- </li>';
- }
- echo "</ul>";
- }else{
- echo "<option value=''>" . Yii::t('app', 'Select section') . "</option>";
- // echo "<option value='1'>" . Yii::t('app', 'root') . "</option>";
- }
- }
- public function actionGetcurrentimage($id){
- $item = Models::find()
- ->leftJoin('detail_instances', ['details.instance_id' => 'detail_instances.id'])
- ->leftJoin('instance_icons', ['instance_icons.id' => 'detail_instances.icon_id'])
- ->leftJoin('detail_types', ['detail_types.id' => 'details.type_id'])
- ->where(['details.id' => $id])
- ->one();
- // $item = new CatalogItem();
- // print_r($id);
- // print_r($item->model->icon->icon);
- //
- // exit;
- return '../' . $item->model->icon->icon;
- }
- }