ArticleController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace backend\controllers;
  3. use backend\models\search\ArticleSearch;
  4. use common\components\Controller;
  5. use common\models\Article;
  6. use common\models\ArticleModule;
  7. use common\models\Category;
  8. use Yii;
  9. use yii\base\Exception;
  10. use yii\data\ActiveDataProvider;
  11. use yii\filters\VerbFilter;
  12. use yii\helpers\Url;
  13. use yii\web\NotFoundHttpException;
  14. use yii\web\Response;
  15. /**
  16. * ArticleController implements the CRUD actions for Article model.
  17. */
  18. class ArticleController extends Controller
  19. {
  20. public function behaviors()
  21. {
  22. return [
  23. 'verbs' => [
  24. 'class' => VerbFilter::className(),
  25. 'actions' => [
  26. 'delete' => ['post'],
  27. ],
  28. ],
  29. ];
  30. }
  31. public function actions()
  32. {
  33. return [
  34. 'ajax-update-field' => [
  35. 'class' => 'common\\actions\\AjaxUpdateFieldAction',
  36. 'allowFields' => ['status', 'is_top', 'is_hot', 'is_best'],
  37. 'findModel' => [$this, 'findModel']
  38. ],
  39. 'switcher' => [
  40. 'class' => 'backend\widgets\grid\SwitcherAction'
  41. ]
  42. ];
  43. }
  44. /**
  45. * Lists all Article models.
  46. *
  47. * @return mixed
  48. */
  49. public function actionIndex()
  50. {
  51. Url::remember();
  52. $searchModel = new ArticleSearch();
  53. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  54. return $this->render('index', [
  55. 'searchModel' => $searchModel,
  56. 'dataProvider' => $dataProvider,
  57. ]);
  58. }
  59. /**
  60. * 回收站列表
  61. *
  62. * @return mixed
  63. */
  64. public function actionTrash()
  65. {
  66. $query = \common\models\Article::find()->onlyTrashed();
  67. $dataProvider = new ActiveDataProvider([
  68. 'query' => $query,
  69. 'sort' => [
  70. 'defaultOrder' => [
  71. 'id' => SORT_DESC
  72. ]
  73. ]
  74. ]);
  75. return $this->render('trash',[
  76. 'dataProvider' => $dataProvider
  77. ]);
  78. }
  79. /**
  80. * 还原
  81. * @return array
  82. * @throws NotFoundHttpException
  83. */
  84. public function actionReduction()
  85. {
  86. Yii::$app->response->format = Response::FORMAT_JSON;
  87. $id = Yii::$app->request->post('id');
  88. $model = Article::find()->where(['id' => $id])->onlyTrashed()->one();
  89. if(!$model) {
  90. throw new NotFoundHttpException('人才不存在!');
  91. }
  92. $model->restore();
  93. return [
  94. 'message' => '操作成功'
  95. ];
  96. }
  97. /**
  98. * 彻底删除
  99. * @return array
  100. * @throws NotFoundHttpException
  101. * @throws \Exception
  102. */
  103. public function actionHardDelete()
  104. {
  105. Yii::$app->response->format = Response::FORMAT_JSON;
  106. $id = Yii::$app->request->post('id');
  107. $model = Article::find()->where(['id' => $id])->onlyTrashed()->one();
  108. if(!$model) {
  109. throw new NotFoundHttpException('人才不存在!');
  110. }
  111. $model->delete();
  112. return [
  113. 'message' => '操作成功'
  114. ];
  115. }
  116. public function actionClear()
  117. {
  118. if (Article::deleteAll(['>', 'deleted_at', 0]) !== false) {
  119. Yii::$app->session->setFlash('success', '操作成功');
  120. return $this->redirect('trash');
  121. }
  122. throw new Exception('操作失败');
  123. }
  124. /**
  125. * Displays a single Article model.
  126. *
  127. * @param int $id
  128. *
  129. * @return mixed
  130. */
  131. public function actionView($id)
  132. {
  133. return $this->render('view', [
  134. 'model' => $this->findModel($id),
  135. ]);
  136. }
  137. /**
  138. * Creates a new Article model.
  139. * If creation is successful, the browser will be redirected to the 'view' page.
  140. *
  141. * @param string $module 人才类型
  142. * @return mixed
  143. */
  144. public function actionCreate($module = 'talent')
  145. {
  146. $model = new Article();
  147. $model->status = Article::STATUS_ACTIVE;
  148. $model->module = $module;
  149. $moduleModelClass = $model->findModuleClass($module);
  150. $moduleModel = new $moduleModelClass;
  151. $this->performAjaxValidation($model);
  152. // $this->performAjaxValidation($moduleModel);
  153. if (Yii::$app->request->isPost) {
  154. $transaction = Yii::$app->db->beginTransaction();
  155. try{
  156. $model->load(Yii::$app->request->post());
  157. $model->save();
  158. if($model->hasErrors()) {
  159. throw new Exception('操作失败');
  160. }
  161. // $moduleModel->load(Yii::$app->request->post());
  162. // $moduleModel->id = $model->id;
  163. // $moduleModel->save();
  164. // if($moduleModel->hasErrors()) {
  165. // throw new Exception('操作失败');
  166. // }
  167. $transaction->commit();
  168. Yii::$app->session->setFlash('success', '发布成功');
  169. } catch (\Exception $e) {
  170. $transaction->rollBack();
  171. Yii::$app->session->setFlash('error', $e->getMessage());
  172. }
  173. return $this->goBack();
  174. }
  175. $articleModules = ArticleModule::find()->all();
  176. $articleModuleItems = [];
  177. foreach($articleModules as $articleModule) {
  178. $articleModuleItem = [];
  179. $articleModuleItem['label'] = $articleModule->title;
  180. $articleModuleItem['url'] = ['/article/create', 'module' => $articleModule->name];
  181. $articleModuleItem['active'] = $module == $articleModule->name;
  182. $articleModuleItems[] = $articleModuleItem;
  183. }
  184. return $this->render('create', [
  185. 'model' => $model,
  186. 'moduleModel' => $moduleModel,
  187. 'module' => $module,
  188. 'articleModuleItems' => $articleModuleItems
  189. ]);
  190. }
  191. /**
  192. * Updates an existing Article model.
  193. * If update is successful, the browser will be redirected to the 'view' page.
  194. *
  195. * @param int $id
  196. *
  197. * @return mixed
  198. */
  199. public function actionUpdate($id, $module = null)
  200. {
  201. $model = Article::find()->where(['id' => $id])->one();
  202. if ($module) {
  203. $model->module = $module;
  204. }
  205. // $moduleModel = $model->data;
  206. $this->performAjaxValidation($model);
  207. // $this->performAjaxValidation($moduleModel);
  208. if (Yii::$app->request->isPost) {
  209. $transaction = Yii::$app->db->beginTransaction();
  210. try {
  211. $model->load(Yii::$app->request->post());
  212. $model->save();
  213. if($model->hasErrors()) {
  214. throw new Exception('操作失败');
  215. }
  216. // if ($moduleModel) {
  217. // $moduleModel->load(Yii::$app->request->post());
  218. // $moduleModel->save();
  219. // if($moduleModel->hasErrors()) {
  220. // throw new Exception('操作失败');
  221. // }
  222. // }
  223. $transaction->commit();
  224. Yii::$app->session->setFlash('success', '操作成功');
  225. } catch (\Exception $e) {
  226. $transaction->rollBack();
  227. Yii::$app->session->setFlash('error', $e->getMessage());
  228. }
  229. return $this->goBack();
  230. }
  231. $moduleModelClass = $model->findModuleClass($model->module);
  232. $moduleModel = new $moduleModelClass;
  233. $articleModules = ArticleModule::find()->all();
  234. $articleModuleItems = [];
  235. foreach($articleModules as $articleModule) {
  236. $articleModuleItem = [];
  237. $articleModuleItem['label'] = $articleModule->title;
  238. $articleModuleItem['url'] = ['/article/update', 'id' => $id, 'module' => $articleModule->name];
  239. $articleModuleItem['active'] = $model->module == $articleModule->name;
  240. $articleModuleItems[] = $articleModuleItem;
  241. }
  242. //渲染产业链
  243. $model->chain = explode(',', $model->chain);
  244. return $this->render('update', [
  245. 'model' => $model,
  246. 'moduleModel' => $moduleModel,
  247. 'module' => $model->module,
  248. 'articleModuleItems' => $articleModuleItems
  249. ]);
  250. }
  251. /**
  252. * Deletes an existing Article model.
  253. * If deletion is successful, the browser will be redirected to the 'index' page.
  254. *
  255. * @param int $id
  256. *
  257. * @return mixed
  258. */
  259. public function actionDelete($id)
  260. {
  261. $this->findModel($id)->softDelete();
  262. Yii::$app->session->setFlash('success', '操作成功');
  263. return $this->goBack();
  264. }
  265. /**
  266. * Finds the Article model based on its primary key value.
  267. * If the model is not found, a 404 HTTP exception will be thrown.
  268. *
  269. * @param int $id
  270. *
  271. * @return Article the loaded model
  272. *
  273. * @throws NotFoundHttpException if the model cannot be found
  274. */
  275. public function findModel($id)
  276. {
  277. if (($model = Article::find()->where(['id' => $id])->notTrashed()->one()) !== null) {
  278. return $model;
  279. } else {
  280. throw new NotFoundHttpException('The requested page does not exist.');
  281. }
  282. }
  283. }