NewsController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace manager\controllers;
  3. use Yii;
  4. use \app\models\base\Story;
  5. use \app\models\base\NewsTopic;
  6. use yii\web\UploadedFile;
  7. class NewsController extends BaseController
  8. {
  9. // ** Статьи (новости) **
  10. public function actions()
  11. {
  12. return [
  13. 'error' => [
  14. 'class' => 'yii\web\ErrorAction',
  15. ],
  16. ];
  17. }
  18. public function actionIndex()
  19. {
  20. return $this->render("/default/news",[]);
  21. }
  22. public function actionList()
  23. {
  24. return $this->render('/default/newsList', []);
  25. }
  26. public function actionInactive()
  27. {
  28. return $this->render('/default/newsInactive', []);
  29. }
  30. public function actionNew()
  31. {
  32. return $this->render('/default/newsNew', []);
  33. }
  34. // ** Рубрики **
  35. public function actionTopiclist()
  36. {
  37. return $this->render('/default/topicList', []);
  38. }
  39. public function actionTopicall()
  40. {
  41. return $this->render('/default/topicAll', []);
  42. }
  43. public function actionTopicdel($id)
  44. {
  45. $model = $this->findTopicModel($id);
  46. if($model) $model->del($id);
  47. return $this->render('/default/topicList', []);
  48. }
  49. public function actionTopicshow($id)
  50. {
  51. $model = $this->findTopicModel($id);
  52. if($model) $model->Show($id);
  53. return $this->render('/default/topicList', []);
  54. }
  55. public function actionTopicactive($id)
  56. {
  57. $model = $this->findTopicModel($id);
  58. if($model) $model->Active($id);
  59. return $this->render('/default/topicList', []);
  60. }
  61. public function actionTopicsort()
  62. {
  63. if (isset($_POST['item']) && is_array($_POST['item'])) {
  64. $i = 0;
  65. $a = array();
  66. // beginTransaction(); было бы неплохо обернуть в транзакцию но хз как это сделать
  67. foreach ($_POST['item'] as $item) {
  68. $model = $this->findTopicModel($item);
  69. $model->order = $i;
  70. $a[$model->id] = $i;
  71. $model->save(true, ['order']);
  72. // print_a($model->errors);
  73. $i++;
  74. }
  75. return json_encode(['status'=>'ok','data'=>$a]);
  76. }
  77. return json_encode(['status'=>'err']);
  78. }
  79. public function actionTopiccreate()
  80. {
  81. $model = new NewsTopic();
  82. if (Yii::$app->request->isPost && Yii::$app->request->post()){
  83. if($model->load($_POST) && $model->save()){
  84. if (is_uploaded_file($_FILES['NewsTopic']['tmp_name']['photo'])) {
  85. // $id = -1; //test
  86. $model->saveImg($model->id, $_FILES['NewsTopic']['tmp_name']['photo'], $_FILES['NewsTopic']['type']['photo']);
  87. }
  88. }else{
  89. print_a($model->errors);
  90. }
  91. return $this->redirect('topiclist');
  92. }
  93. return $this->render('/default/topicCreate', ['model'=>$model]);
  94. }
  95. public function actionTopicupdate($id)
  96. {
  97. $model = $this->findTopicModel($id);
  98. if (Yii::$app->request->isPost && Yii::$app->request->post()){
  99. if($model->load($_POST) && $model->save()){
  100. // обновление фото
  101. if (is_uploaded_file($_FILES['NewsTopic']['tmp_name']['photo'])) {
  102. $model->saveImg($model->id, $_FILES['NewsTopic']['tmp_name']['photo'], $_FILES['NewsTopic']['type']['photo']);
  103. }
  104. }else{
  105. print_a($model->errors);
  106. }
  107. return $this->redirect('topiclist');
  108. }
  109. return $this->render('/default/topicUpdate', ['model'=>$model]);
  110. }
  111. // ** Сюжеты **
  112. public function actionStorylist()
  113. {
  114. return $this->render('/default/storyList', []);
  115. }
  116. public function actionStorydel($id)
  117. {
  118. $model = $this->findStoryModel($id);
  119. if($model) $model->del($id);
  120. return $this->render('/default/storyList', []);
  121. }
  122. public function actionStoryshow($id)
  123. {
  124. $model = $this->findStoryModel($id);
  125. if($model) $model->Show($id);
  126. return $this->render('/default/storyList', []);
  127. }
  128. public function actionAjaxstory()
  129. {
  130. if (Yii::$app->request->isGet && $get = Yii::$app->request->get('q')){
  131. $model = new Story();
  132. $items = $model->search($get, 10);
  133. $res = array();
  134. if( $items && is_array( $items ) ){
  135. foreach( $items as $item ){
  136. $res[] = array( 'id'=>$item['id']*1, 'label'=>$item['title'] );
  137. }
  138. return json_encode( ['status'=>'ok', 'item'=>$res] );
  139. }
  140. }
  141. return json_encode( ['status'=>'err'] );
  142. }
  143. public function actionStoryactive($id)
  144. {
  145. $model = $this->findStoryModel($id);
  146. if($model) $model->Active($id);
  147. return $this->render('/default/storyList', []);
  148. }
  149. public function actionStorycreate()
  150. {
  151. $model = new Story();
  152. if (Yii::$app->request->isPost && Yii::$app->request->post()){
  153. if($model->load($_POST) && $model->save()){
  154. if (is_uploaded_file($_FILES['Story']['tmp_name']['photo'])) {
  155. // $id = -1; //test
  156. $model->saveImg($model->id, $_FILES['Story']['tmp_name']['photo'], $_FILES['Story']['type']['photo']);
  157. }
  158. }else{
  159. print_a($model->errors);
  160. }
  161. return $this->redirect('storylist');
  162. }
  163. return $this->render('/default/storyCreate', ['model'=>$model]);
  164. }
  165. public function actionStoryupdate($id)
  166. {
  167. $model = $this->findStoryModel($id);
  168. if (Yii::$app->request->isPost && Yii::$app->request->post()){
  169. if($model->load($_POST) && $model->save()){
  170. // обновление фото
  171. if (is_uploaded_file($_FILES['Story']['tmp_name']['photo'])) {
  172. $model->saveImg($model->id, $_FILES['Story']['tmp_name']['photo'], $_FILES['Story']['type']['photo']);
  173. }
  174. }else{
  175. print_a($model->errors);
  176. }
  177. return $this->redirect('storylist');
  178. }
  179. return $this->render('/default/storyUpdate', ['model'=>$model]);
  180. }
  181. // ** отладка **
  182. public function actionTest()
  183. {
  184. return $this->render('/default/test', []);
  185. }
  186. /**
  187. * Найти сюжет.
  188. *
  189. * @param $id
  190. * @return Story|null
  191. * @throws NotFoundHttpException
  192. */
  193. protected function findStoryModel($id)
  194. {
  195. if (($model = Story::findOne($id)) !== null) {
  196. return $model;
  197. }
  198. throw new NotFoundHttpException('Ой! сюжет не найдена.');
  199. return false;
  200. }
  201. /**
  202. * Найти Ркбрику.
  203. *
  204. * @param $id
  205. * @return Story|null
  206. * @throws NotFoundHttpException
  207. */
  208. protected function findTopicModel($id)
  209. {
  210. if (($model = NewsTopic::findOne($id)) !== null) {
  211. return $model;
  212. }
  213. throw new NotFoundHttpException('Ой! рубрика не найдена.');
  214. return false;
  215. }
  216. }