DictionarynewsController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/12
  6. * Time: 上午10:49
  7. * 资讯- 知识库
  8. */
  9. namespace backend\controllers;
  10. use backend\base\CommonController;
  11. use backend\base\Help;
  12. use common\models\NewKnowledgelabel;
  13. use Yii;
  14. use yii\helpers\ArrayHelper;
  15. class DictionarynewsController extends CommonController
  16. {
  17. //知识库
  18. public function actionLabel()
  19. {
  20. return $this->render('label');
  21. }
  22. public function actionLabelform()
  23. {
  24. $model = new NewKnowledgelabel();
  25. $input = Yii::$app->request->get();
  26. //搜索的情况下
  27. if(ArrayHelper::keyExists('id',$input))
  28. {
  29. $p = $model->FindById($input['id']);
  30. if(!empty($p))
  31. {
  32. if($p['pid'] == 0)
  33. {
  34. $model->pid = $p['id'];
  35. }
  36. if($p['pid'] != 0)
  37. {
  38. $model->id = $p['id'];
  39. $p = $model->FindById($p['pid']);
  40. }
  41. }
  42. }
  43. $query = $model->getList([],'id,name,pid,create_at,sort,is_show',2);
  44. if(!empty($query))
  45. {
  46. //重组数组,返回父类
  47. if(!empty($p))
  48. {
  49. array_push($query,$p->toArray());
  50. }
  51. foreach ($query as &$val)
  52. {
  53. $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
  54. }
  55. return Help::JsonCode(Help::SUCCESS,Yii::t('app','aget_success'),$query);
  56. }
  57. return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'),[]);
  58. }
  59. public function actionLabelcreate()
  60. {
  61. $model = new NewKnowledgelabel();
  62. $model->pid = 0;
  63. $rows = $model->getList([],'id,name,pid');
  64. return $this->render('labelcreate',['model'=>$rows]);
  65. }
  66. public function actionLabelcreateform()
  67. {
  68. $models = new NewKnowledgelabel();
  69. $models->scenario = 'add';
  70. $model = $models->Authenticator(Yii::$app->request->post('data'));
  71. if(!empty($model->pid))
  72. {
  73. $row = $models->FindById($model->pid);
  74. if(!empty($row))
  75. {
  76. $model->path = $row->path.$row->id.',';
  77. }
  78. }
  79. if(is_object($model))
  80. {
  81. if($model->save() == true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
  82. }
  83. return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$model);
  84. }
  85. public function actionEditlabel()
  86. {
  87. $input = Yii::$app->request->get();
  88. if(!empty($input['id']))
  89. {
  90. $models = new NewKnowledgelabel();
  91. $models->pid = 0;
  92. $result = $models->getList([],'id,name,pid');
  93. $row = $models->FindById($input['id']);
  94. return $this->render('editlabel',['model'=>$result,'row'=>$row]);
  95. }
  96. }
  97. public function actionEditlabelform()
  98. {
  99. $input = Yii::$app->request->post('data');
  100. $model = new NewKnowledgelabel();
  101. if(ArrayHelper::keyExists('pid',$input) && is_numeric($input['pid']))
  102. {
  103. $row = $model->FindById($input['pid']);
  104. if(!empty($row))
  105. {
  106. $input['pid'] = $row['id'];
  107. $input['path'] = $row['path'].$row->id.',';
  108. }
  109. }
  110. $res = $model->FindById($input['id']);
  111. //处理删除
  112. if(ArrayHelper::keyExists('del',$input) && is_numeric($input['del']))
  113. {
  114. if($res['pid']== 0)
  115. {
  116. $model->pid = $res['id'];
  117. $pidList = $model->getList([],'id');
  118. if(!empty($pidList)) return Help::JsonCode(Help::ERROR,'检测到该父类下存在子类,请先删除子类在删除父类');
  119. }
  120. }
  121. if($res->load($input,'') && $res->save())
  122. {
  123. return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  124. }
  125. return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
  126. }
  127. public function actionPidlist()
  128. {
  129. $input = Yii::$app->request->post();
  130. $model = new NewKnowledgelabel();
  131. if(empty($input['pid']) || $input['pid'] == 0)
  132. {
  133. $model->pid = 0;
  134. }
  135. else
  136. {
  137. $model->pid = $input['pid'];
  138. }
  139. $result = $model->getList([],'id,name',1);
  140. if(!empty($result)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','get_success'),$result);
  141. return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'));
  142. }
  143. }