From 190f471093cbac4f4f1a650c842cbb2754b4e25d Mon Sep 17 00:00:00 2001 From: dvaganov Date: Sun, 5 Jun 2016 22:39:39 +0300 Subject: [PATCH] Add shared events obtaining. --- controllers/CalendarController.php | 17 +++++++---------- models/query/AccessQuery.php | 2 +- models/query/CalendarQuery.php | 29 +++++++++++++++++++++++++++++ models/search/CalendarSearch.php | 18 ++++++++++++++++++ 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/controllers/CalendarController.php b/controllers/CalendarController.php index 891701a..fc1628a 100644 --- a/controllers/CalendarController.php +++ b/controllers/CalendarController.php @@ -4,6 +4,7 @@ use Yii; use app\models\Calendar; +use app\models\Access; use app\models\search\CalendarSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; @@ -36,16 +37,12 @@ public function behaviors() public function actionIndex() { $searchModel = new CalendarSearch(); - $dataProviderMy = $searchModel->search([ - 'CalendarSearch' => [ - 'creatorID' => \Yii::$app->user->id - ] - ]); - $dataProviderShared = $searchModel->search([ - 'CalendarSearch' => [ - 'creatorID' => 2 - ] - ]); + + $dataProviderMy = $searchModel->byOwner(\Yii::$app->user->id); + + $access = Access::find()->whereGuest(\Yii::$app->user->id)->all(); + + $dataProviderShared = $searchModel->searchShared($access); return $this->render('index', [ 'searchModel' => $searchModel, diff --git a/models/query/AccessQuery.php b/models/query/AccessQuery.php index 9996a33..94c0330 100644 --- a/models/query/AccessQuery.php +++ b/models/query/AccessQuery.php @@ -36,7 +36,7 @@ public function one($db = null) * Add guest filter to access. * @param int $userID guest id */ - public function asGuest($userID) + public function whereGuest($userID) { return $this->andWhere('guestID = :guestID', ['guestID' => $userID]); } diff --git a/models/query/CalendarQuery.php b/models/query/CalendarQuery.php index c5ca9bf..d043b66 100644 --- a/models/query/CalendarQuery.php +++ b/models/query/CalendarQuery.php @@ -31,4 +31,33 @@ public function one($db = null) { return parent::one($db); } + + /** + * Add guest filter to access. + * @param string $date date in format Y-m-d + */ + public function whereDate($date) + { + return $this->andWhere(['like', 'date', $date]); + } + + /** + * Add creator filter to calendar. + * @param int $userID creator id + */ + public function whereOwner($userID) + { + return $this->andWhere('creatorID = :userID', ['userID' => $userID]); + } + + public function withUserAndDate($userID, $date) + { + echo $userID . $date; + return $this->orWhere([ + 'and', 'creatorID = :userID', + ['like', 'dateEvent', $date] + ], + ['userID' => $userID] + ); + } } diff --git a/models/search/CalendarSearch.php b/models/search/CalendarSearch.php index c017328..1e23a91 100644 --- a/models/search/CalendarSearch.php +++ b/models/search/CalendarSearch.php @@ -67,4 +67,22 @@ public function search($params) return $dataProvider; } + + public function byOwner($ownerID) + { + $query = Calendar::find()->whereOwner($ownerID); + return new ActiveDataProvider(['query' => $query]); + } + + /** + * @param \app\models\Access $accesses + */ + public function searchShared($accesses) + { + $query = Calendar::find(); + foreach($accesses as $access) { + $query->withUserAndDate($access->ownerID, $access->date); + } + return new ActiveDataProvider(['query' => $query]); + } }