MessagesController.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use app\models\Messages;
  5. use yii\data\SqlDataProvider;
  6. use yii\web\NotFoundHttpException;
  7. use yii\filters\VerbFilter;
  8. use yii\web\Controller;
  9. class MessagesController extends Controller
  10. {
  11. public function behaviors()
  12. {
  13. return [
  14. 'verbs' => [
  15. 'class' => VerbFilter::className(),
  16. 'actions' => [
  17. //'delete' => ['post'],
  18. ],
  19. ],
  20. ];
  21. }
  22. public function init(){
  23. if(Yii::$app->user->isGuest)
  24. {
  25. return $this->redirect('/user/login');
  26. }
  27. if(@!Yii::$app->user->identity->confirmed_at && (@Yii::$app->user->identity->role == 1)){
  28. return $this->redirect('/registration/organizer');
  29. }
  30. $this->layout = '@app/views/layouts/backend/user';
  31. parent::init();
  32. }
  33. public function actionIndex()
  34. {
  35. //$dataProvider = new ActiveDataProvider([
  36. // 'query' => Messages::find()->where(['user_id' => Yii::$app->user->identity->id]),
  37. //]);
  38. // Obnylator Messages
  39. Yii::$app->db->createCommand("UPDATE messages SET status=1 WHERE user_id=:user_id")->bindValue(":user_id",Yii::$app->user->identity->id)->execute();
  40. // Pagination
  41. $totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM messages WHERE user_id=:user_id', [':user_id' => Yii::$app->user->identity->id])->queryScalar();
  42. $query = 'SELECT * FROM messages WHERE user_id=:user_id ORDER by DATE DESC';
  43. $dataProvider = new SqlDataProvider([
  44. 'sql' => $query,
  45. 'params' => [':user_id' => Yii::$app->user->identity->id],
  46. 'totalCount' => (int)$totalCount,
  47. 'pagination' => [
  48. 'pageSize' => 30,
  49. ]
  50. ]);
  51. //$userRole = Yii::$app->authManager->getRole('org');
  52. //Yii::$app->authManager->assign($userRole, Yii::$app->user->identity->id);
  53. return $this->render('index', [
  54. 'dataProvider' => $dataProvider,
  55. ]);
  56. }
  57. public function actionView($id)
  58. {
  59. $model = $this->findModel($id);
  60. return $this->render('view', [
  61. 'model' => $model,
  62. ]);
  63. }
  64. public function actionDelete($id)
  65. {
  66. if(Yii::$app->user->can('member'))
  67. {
  68. return $this->redirect(['index']);
  69. }
  70. else
  71. {
  72. $this->findModel($id)->delete();
  73. return $this->redirect(['index']);
  74. }
  75. }
  76. protected function findModel($id)
  77. {
  78. if (($model = Messages::findOne($id)) !== null) {
  79. return $model;
  80. } else {
  81. throw new NotFoundHttpException('The requested page does not exist.');
  82. }
  83. }
  84. }