PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/modules/systemAdmin/Catalog/controllers/__CatalogitemController.php

https://gitlab.com/aintenebris/memoria
PHP | 388 lines | 243 code | 47 blank | 98 comment | 27 complexity | 5a4f83dbc66c764a60db4808e575a38c MD5 | raw file
  1. <?php
  2. namespace app\modules\systemAdmin\Catalog\controllers;
  3. use backend\modules\systemAdmin\Catalog\models\Models;
  4. use backend\modules\systemAdmin\Catalog\models\Section;
  5. use backend\modules\systemAdmin\Catalog\models\Size;
  6. use backend\modules\systemAdmin\Catalog\models\SystemCatalogs;
  7. use common\models\catalog\CatalogItemSize;
  8. use Yii;
  9. use backend\modules\systemAdmin\Catalog\models\CatalogItem;
  10. use backend\modules\systemAdmin\Catalog\models\CatalogItemSearch;
  11. use yii\web\Controller;
  12. use yii\web\NotFoundHttpException;
  13. use yii\filters\VerbFilter;
  14. use \yii\web\Response;
  15. use yii\helpers\Html;
  16. /**
  17. * CatalogitemController implements the CRUD actions for CatalogItem model.
  18. */
  19. class CatalogitemController extends Controller
  20. {
  21. private $rootID = 1;
  22. /**
  23. * @inheritdoc
  24. */
  25. public function behaviors()
  26. {
  27. return [
  28. 'verbs' => [
  29. 'class' => VerbFilter::className(),
  30. 'actions' => [
  31. 'delete' => ['post'],
  32. 'bulk-delete' => ['post'],
  33. ],
  34. ],
  35. ];
  36. }
  37. /**
  38. * Lists all CatalogItem models.
  39. * @return mixed
  40. */
  41. public function actionIndex()
  42. {
  43. $searchModel = new CatalogItemSearch();
  44. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  45. return $this->render('index', [
  46. 'searchModel' => $searchModel,
  47. 'dataProvider' => $dataProvider,
  48. ]);
  49. }
  50. /**
  51. * Displays a single CatalogItem model.
  52. * @param integer $id
  53. * @return mixed
  54. */
  55. public function actionView($id)
  56. {
  57. $request = Yii::$app->request;
  58. if($request->isAjax){
  59. Yii::$app->response->format = Response::FORMAT_JSON;
  60. return [
  61. 'title'=> "CatalogItem #".$id,
  62. 'content'=>$this->renderPartial('view', [
  63. 'model' => $this->findModel($id),
  64. ]),
  65. 'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
  66. Html::a('Edit',['update','id'=>$id],['class'=>'btn btn-primary','role'=>'modal-remote'])
  67. ];
  68. }else{
  69. return $this->render('view', [
  70. 'model' => $this->findModel($id),
  71. ]);
  72. }
  73. }
  74. /**
  75. * Creates a new CatalogItem model.
  76. * For ajax request will return json object
  77. * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
  78. * @return mixed
  79. */
  80. public function actionCreate()
  81. {
  82. $request = Yii::$app->request;
  83. $model = new CatalogItem();
  84. $itemSizeModel = [new CatalogItemSize];
  85. $size = Size::find()->all();
  86. // var_dump($itemSizeModel);
  87. // exit;
  88. if($request->isAjax){
  89. /*
  90. * Process for ajax request
  91. */
  92. Yii::$app->response->format = Response::FORMAT_JSON;
  93. if($request->isGet){
  94. return [
  95. 'title'=> "Create new CatalogItem",
  96. 'content'=>$this->renderPartial('create', [
  97. 'model' => $model,
  98. 'itemSizeModel' => (empty($itemSizeModel)) ? [new CatalogItemSize] : $itemSizeModel,
  99. // 'size' => $size,
  100. ]),
  101. 'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
  102. Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])
  103. ];
  104. }else if($model->load($request->post()) && $model->save()){
  105. // echo '<pre>';
  106. // print_r($request->post());
  107. // print_r($model);
  108. // exit;
  109. return [
  110. 'forceReload'=>'true',
  111. 'title'=> "Create new CatalogItem",
  112. 'content'=>'<span class="text-success">Create CatalogItem success</span>',
  113. 'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
  114. Html::a('Create More',['create'],['class'=>'btn btn-primary','role'=>'modal-remote'])
  115. ];
  116. }else{
  117. return [
  118. 'title'=> "Create new CatalogItem",
  119. 'content'=>$this->renderPartial('create', [
  120. 'model' => $model,
  121. 'itemSizeModel' => (empty($itemSizeModel)) ? [new CatalogItemSize] : $itemSizeModel,
  122. // 'size' => $size,
  123. ]),
  124. 'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
  125. Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])
  126. ];
  127. }
  128. }else{
  129. /*
  130. * Process for non-ajax request
  131. */
  132. if ($model->load($request->post()) && $model->save()) {
  133. return $this->redirect(['view', 'id' => $model->id]);
  134. } else {
  135. return $this->render('create', [
  136. 'model' => $model,
  137. 'itemSizeModel' => (empty($itemSizeModel)) ? [new CatalogItemSize] : $itemSizeModel,
  138. // 'size' => $size,
  139. ]);
  140. }
  141. }
  142. }
  143. /**
  144. * Updates an existing CatalogItem model.
  145. * For ajax request will return json object
  146. * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
  147. * @param integer $id
  148. * @return mixed
  149. */
  150. public function actionUpdate($id)
  151. {
  152. if($id == $this->rootID) return $this->redirect(['index']);
  153. $request = Yii::$app->request;
  154. $model = $this->findModel($id);
  155. // echo '<pre>';
  156. // print_r($request->post());
  157. // print_r($model);
  158. // exit;
  159. if($request->isAjax){
  160. /*
  161. * Process for ajax request
  162. */
  163. Yii::$app->response->format = Response::FORMAT_JSON;
  164. if($request->isGet){
  165. // echo '<pre>';
  166. // print_r($this->findModel($id)->catalog);
  167. return [
  168. 'title'=> "Update CatalogItem #".$id,
  169. 'content'=>$this->renderPartial('update', [
  170. 'model' => $this->findModel($id),
  171. ]),
  172. 'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
  173. Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])
  174. ];
  175. }else if($model->load($request->post()) && $model->save()){
  176. return [
  177. 'forceReload'=>'true',
  178. 'title'=> "CatalogItem #".$id,
  179. 'content'=>$this->renderPartial('view', [
  180. 'model' => $this->findModel($id),
  181. ]),
  182. 'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
  183. Html::a('Edit',['update','id'=>$id],['class'=>'btn btn-primary','role'=>'modal-remote'])
  184. ];
  185. }else{
  186. print_r($model);
  187. exit;
  188. return [
  189. 'title'=> "Update CatalogItem #".$id,
  190. 'content'=>$this->renderPartial('update', [
  191. 'model' => $this->findModel($id),
  192. ]),
  193. 'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
  194. Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])
  195. ];
  196. }
  197. }else{
  198. /*
  199. * Process for non-ajax request
  200. */
  201. if ($model->load($request->post()) && $model->save()) {
  202. return $this->redirect(['view', 'id' => $model->id]);
  203. } else {
  204. return $this->render('update', [
  205. 'model' => $model,
  206. ]);
  207. }
  208. }
  209. }
  210. /**
  211. * Delete an existing CatalogItem model.
  212. * For ajax request will return json object
  213. * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
  214. * @param integer $id
  215. * @return mixed
  216. */
  217. public function actionDelete($id)
  218. {
  219. if($id == $this->rootID) return $this->redirect(['index']);
  220. $request = Yii::$app->request;
  221. $this->findModel($id)->delete();
  222. if($request->isAjax){
  223. /*
  224. * Process for ajax request
  225. */
  226. Yii::$app->response->format = Response::FORMAT_JSON;
  227. return ['forceClose'=>true,'forceReload'=>true];
  228. }else{
  229. /*
  230. * Process for non-ajax request
  231. */
  232. return $this->redirect(['index']);
  233. }
  234. }
  235. /**
  236. * Delete multiple existing CatalogItem model.
  237. * For ajax request will return json object
  238. * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
  239. * @param integer $id
  240. * @return mixed
  241. */
  242. public function actionBulkDelete()
  243. {
  244. $request = Yii::$app->request;
  245. $pks = $request->post('pks'); // Array or selected records primary keys
  246. foreach (CatalogItem::findAll(json_decode($pks)) as $model) {
  247. if($model->id != $this->rootID){
  248. $model->delete();
  249. }
  250. }
  251. if($request->isAjax){
  252. /*
  253. * Process for ajax request
  254. */
  255. Yii::$app->response->format = Response::FORMAT_JSON;
  256. return ['forceClose'=>true,'forceReload'=>true];
  257. }else{
  258. /*
  259. * Process for non-ajax request
  260. */
  261. return $this->redirect(['index']);
  262. }
  263. }
  264. /**
  265. * Finds the CatalogItem model based on its primary key value.
  266. * If the model is not found, a 404 HTTP exception will be thrown.
  267. * @param integer $id
  268. * @return CatalogItem the loaded model
  269. * @throws NotFoundHttpException if the model cannot be found
  270. */
  271. protected function findModel($id)
  272. {
  273. if (($model = CatalogItem::findOne($id)) !== null) {
  274. return $model;
  275. } else {
  276. throw new NotFoundHttpException('The requested page does not exist.');
  277. }
  278. }
  279. public function actionLists($id){
  280. $countItems = CatalogItem::find()
  281. ->leftJoin(Section::tableName(), ['section.id' => 'catalog_item.section_id'])
  282. ->where(['catalog_item.section_id' => $id])
  283. ->count();
  284. $items = CatalogItem::find()
  285. ->where(['section_id' => $id])
  286. ->all();
  287. if($countItems > 0){
  288. echo "<option value=''>" . Yii::t('app', 'Select section') . "</option>";
  289. echo "<option value='1'>" . Yii::t('app', 'root') . "</option>";
  290. foreach($items as $item){
  291. echo "<option value='" . $item->id . "'>" . $item->name . "</option>";
  292. }
  293. }else{
  294. echo "<option value=''>" . Yii::t('app', 'Select section') . "</option>";
  295. echo "<option value='1'>" . Yii::t('app', 'root') . "</option>";
  296. }
  297. }
  298. public function actionModellists($id){
  299. $countItems = Models::find()
  300. ->where(['type_id' => $id])
  301. ->count();
  302. $items = Models::find()
  303. ->leftJoin('detail_instances', ['details.instance_id' => 'detail_instances.id'])
  304. ->leftJoin('instance_icons', ['instance_icons.id' => 'detail_instances.icon_id'])
  305. ->leftJoin('detail_types', ['detail_types.id' => 'details.type_id'])
  306. ->where(['type_id' => $id])
  307. ->all();
  308. // print_r($items[0]->icon->icon->icon);
  309. if($countItems > 0){
  310. echo '<ul style="list-style-type: none; height: 100px; width: 100%; padding-top: 10px; display: inline-block;">';
  311. // echo "<option value=''>" . Yii::t('app', 'Select section') . "</option>";
  312. // echo "<option value='1'>" . Yii::t('app', 'root') . "</option>";
  313. foreach($items as $item){
  314. // echo '<option onmouseover="alert();" value="' . $item->id . '" src="/advanced/backend/web/' . $item->type->name . '/' . $item->icon->icon->icon . '"></option>';
  315. echo '<li class="iconItem"
  316. style="display: inline-block; width: 42px; height: 72px; padding-left: 5px; padding-right: 5px;"
  317. id="' . $item->id . '" src="/advanced/backend/web/' . $item->icon->icon->icon . '">
  318. <img height="70" width="40" src="/advanced/backend/web/' . $item->icon->icon->icon . '">
  319. </li>';
  320. }
  321. echo "</ul>";
  322. }else{
  323. echo "<option value=''>" . Yii::t('app', 'Select section') . "</option>";
  324. // echo "<option value='1'>" . Yii::t('app', 'root') . "</option>";
  325. }
  326. }
  327. public function actionGetcurrentimage($id){
  328. $item = Models::find()
  329. ->leftJoin('detail_instances', ['details.instance_id' => 'detail_instances.id'])
  330. ->leftJoin('instance_icons', ['instance_icons.id' => 'detail_instances.icon_id'])
  331. ->leftJoin('detail_types', ['detail_types.id' => 'details.type_id'])
  332. ->where(['details.id' => $id])
  333. ->one();
  334. // $item = new CatalogItem();
  335. // print_r($id);
  336. // print_r($item->model->icon->icon);
  337. //
  338. // exit;
  339. return '../' . $item->model->icon->icon;
  340. }
  341. }