Skip to content

Commit

Permalink
Add shared events obtaining.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaganov committed Jun 5, 2016
1 parent 10c2085 commit 190f471
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
17 changes: 7 additions & 10 deletions controllers/CalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion models/query/AccessQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
29 changes: 29 additions & 0 deletions models/query/CalendarQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);
}
}
18 changes: 18 additions & 0 deletions models/search/CalendarSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit 190f471

Please sign in to comment.