DesignController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace manager\controllers;
  3. use Yii;
  4. use \app\models\Design;
  5. class DesignController extends BaseController
  6. {
  7. // ** Статьи (новости) **
  8. public function actions()
  9. {
  10. return [
  11. 'error' => [
  12. 'class' => 'yii\web\ErrorAction',
  13. ],
  14. ];
  15. }
  16. public function actionIndex()
  17. {
  18. return $this->redirect("blocks");
  19. }
  20. public function actionActive($id)
  21. {
  22. $model = Design::findOne($id);
  23. if($model) $model->Active($id);
  24. return $this->redirect(['blocks']);
  25. }
  26. public function actionDelete($id)
  27. {
  28. Design::findOne($id)->delete();
  29. return $this->redirect(['index']);
  30. }
  31. public function actionTopicactive($id)
  32. {
  33. $model = Design::findOne($id);
  34. if($model) $model->Active($id);
  35. return $this->redirect('blocks');
  36. }
  37. public function actionCreate()
  38. {
  39. $model = new Design();
  40. if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){
  41. // print_a($post);exit;
  42. $post['Design'] = $this->formNormalizer($post['Design']);
  43. if($model->load($post, 'Design'))
  44. {
  45. if($model->save())
  46. {
  47. return $this->redirect('blocks');
  48. }else{
  49. print_a($model->getErrors());
  50. }
  51. }else{
  52. print_a($model->getErrors());
  53. }
  54. }
  55. return $this->render('/design/form', ['model' => $model]);
  56. }
  57. public function actionPurge()
  58. {
  59. Yii::$app->cache->delete("main-page-main-view-glob");
  60. Yii::$app->cache->delete("MainPageBlock_desktop");
  61. Yii::$app->cache->delete("MainPageBlock_mobile");
  62. Yii::$app->cache->delete("main-page-main-view");
  63. Yii::$app->cache->delete("main-page-third-row-l2");
  64. Yii::$app->cache->delete("main-page-third-row-l2-0");
  65. Yii::$app->cache->delete("main-page-third-row-l2-1");
  66. Yii::$app->cache->delete("main-page-third-row-l2-2");
  67. Yii::$app->cache->delete("main-page-topic-widget-0");
  68. Yii::$app->cache->delete("main-page-topic-widget-1");
  69. Yii::$app->cache->delete("main-page-topic-widget-2");
  70. Yii::$app->cache->delete("main-page-wide-widget-0");
  71. Yii::$app->cache->delete("main-page-wide-widget-1");
  72. Yii::$app->cache->delete("main-page-wide-widget-2");
  73. return $this->render('/design/purge');
  74. }
  75. public function actionUpdate($id)
  76. {
  77. $model = Design::findOne($id);
  78. if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){
  79. // print_a($post);exit;
  80. $post['Design'] = $this->formNormalizer($post['Design']);
  81. if($model->load($post, 'Design'))
  82. {
  83. if($model->save())
  84. {
  85. return $this->redirect('blocks');
  86. }else{
  87. print_a($model->getErrors());
  88. }
  89. }else{
  90. print_a($model->getErrors());
  91. }
  92. }
  93. return $this->render('/design/form', ['model' => $model]);
  94. }
  95. public function actionSort()
  96. {
  97. if (isset($_POST['item']) && is_array($_POST['item'])) {
  98. $i = 0;
  99. $a = array();
  100. // beginTransaction(); было бы неплохо обернуть в транзакцию но хз как это сделать
  101. foreach ($_POST['item'] as $item) {
  102. $model = Design::findOne($item);
  103. $model->order = $i;
  104. $a[$model->id] = $i;
  105. $model->save(true, ['order']);
  106. // print_a($model->errors);
  107. $i++;
  108. }
  109. return json_encode(['status'=>'ok','data'=>$a]);
  110. }
  111. return json_encode(['status'=>'err']);
  112. }
  113. public function formNormalizer($post)
  114. {
  115. switch($post['type']){
  116. case 'story':
  117. $post['parent_id'] = $post['Story'];
  118. break;
  119. case 'topic':
  120. $post['parent_id'] = $post['Topic'];
  121. break;
  122. case 'tag':
  123. $post['parent_id'] = $post['Tag'];
  124. break;
  125. case 'inquirer':
  126. $post['parent_id'] = $post['Inquirer'];
  127. break;
  128. case 3:
  129. break;
  130. }
  131. $post['active'] = $this->CheckYN(@$post['active']);
  132. return $post;
  133. }
  134. public function CheckYN($attr)
  135. {
  136. return (isset($attr) && $attr == 'Y')?'Y':'N';
  137. }
  138. public function actionBlocks()
  139. {
  140. $model = new Design();;
  141. return $this->render('/design/list', ['model' => $model]);
  142. }
  143. public function actionTop()
  144. {
  145. return $this->render('/design/top', []);
  146. }
  147. }