PersonController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace manager\controllers;
  3. use app\models\Person;
  4. use Yii;
  5. use yii\data\ActiveDataProvider;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\filters\VerbFilter;
  9. /**
  10. * PersonController implements the CRUD actions for Person model.
  11. */
  12. class PersonController extends Controller
  13. {
  14. /**
  15. * @inheritDoc
  16. */
  17. public function behaviors()
  18. {
  19. return array_merge(
  20. parent::behaviors(),
  21. [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['POST'],
  26. ],
  27. ],
  28. ]
  29. );
  30. }
  31. /**
  32. * Lists all Person models.
  33. *
  34. * @return string
  35. */
  36. public function actionIndex()
  37. {
  38. $dataProvider = new ActiveDataProvider([
  39. 'query' => Person::find(),
  40. /*
  41. 'pagination' => [
  42. 'pageSize' => 50
  43. ],
  44. 'sort' => [
  45. 'defaultOrder' => [
  46. 'id' => SORT_DESC,
  47. ]
  48. ],
  49. */
  50. ]);
  51. return $this->render('index', [
  52. 'dataProvider' => $dataProvider,
  53. ]);
  54. }
  55. /**
  56. * Displays a single Person model.
  57. * @param int $id ID
  58. * @return string
  59. * @throws NotFoundHttpException if the model cannot be found
  60. */
  61. public function actionView($id)
  62. {
  63. return $this->render('view', [
  64. 'model' => $this->findModel($id),
  65. ]);
  66. }
  67. /**
  68. * Creates a new Person model.
  69. * If creation is successful, the browser will be redirected to the 'view' page.
  70. * @return string|\yii\web\Response
  71. */
  72. public function actionCreate()
  73. {
  74. $model = new Person();
  75. if ($this->request->isPost) {
  76. if ($model->load($this->request->post()) && $model->save()) {
  77. if (is_uploaded_file($_FILES['Person']['tmp_name']['photo'])) {
  78. $model->saveImg($model->id, $_FILES['Person']['tmp_name']['photo'], $_FILES['Person']['type']['photo']);
  79. }
  80. return $this->redirect(['view', 'id' => $model->id]);
  81. }
  82. } else {
  83. $model->loadDefaultValues();
  84. }
  85. return $this->render('create', [
  86. 'model' => $model,
  87. ]);
  88. }
  89. /**
  90. * Updates an existing Person model.
  91. * If update is successful, the browser will be redirected to the 'view' page.
  92. * @param int $id ID
  93. * @return string|\yii\web\Response
  94. * @throws NotFoundHttpException if the model cannot be found
  95. */
  96. public function actionUpdate($id)
  97. {
  98. $model = $this->findModel($id);
  99. //var_dump($this->request->post());
  100. if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
  101. if (is_uploaded_file($_FILES['Person']['tmp_name']['photo'])) {
  102. $model->saveImg($model->id, $_FILES['Person']['tmp_name']['photo'], $_FILES['Person']['type']['photo']);
  103. }
  104. return $this->redirect(['view', 'id' => $model->id]);
  105. }
  106. return $this->render('update', [
  107. 'model' => $model,
  108. ]);
  109. }
  110. /**
  111. * Deletes an existing Person model.
  112. * If deletion is successful, the browser will be redirected to the 'index' page.
  113. * @param int $id ID
  114. * @return \yii\web\Response
  115. * @throws NotFoundHttpException if the model cannot be found
  116. */
  117. public function actionDelete($id)
  118. {
  119. if(Yii::$app->user->can('deleteNews')){
  120. $this->findModel($id)->delete();
  121. }
  122. return $this->redirect(['index']);
  123. }
  124. /**
  125. * Finds the Person model based on its primary key value.
  126. * If the model is not found, a 404 HTTP exception will be thrown.
  127. * @param int $id ID
  128. * @return Person the loaded model
  129. * @throws NotFoundHttpException if the model cannot be found
  130. */
  131. protected function findModel($id)
  132. {
  133. if (($model = Person::findOne(['id' => $id])) !== null) {
  134. return $model;
  135. }
  136. throw new NotFoundHttpException('The requested page does not exist.');
  137. }
  138. public function actionAjaxlist($q = NULL){
  139. $model = Person::find()
  140. ->andWhere(['show'=>'Y'])
  141. ->andFilterWhere(['like','lower(name)',mb_strtolower($q)])
  142. ->limit(50)
  143. ->all();
  144. $a = array();
  145. foreach( $model as $item ){
  146. /**
  147. * @var $item Person
  148. */
  149. $a[] = [
  150. "id"=> $item->id,
  151. 'avatar_url'=>"https://www.amic.ru".$item->getImg(4),
  152. 'name' => $item->name,
  153. 'alias' => '/person/'.$item->alias,
  154. 'description' => $item->jobtitle,
  155. ];
  156. }
  157. return $this->asJson(["items"=>$a]);
  158. }
  159. }