Skip to content

Commit

Permalink
apply user state on query
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Feb 4, 2019
1 parent b0912fa commit 7651464
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 9 deletions.
30 changes: 26 additions & 4 deletions src/elements/TypesAttributeTrait.php
Expand Up @@ -85,6 +85,30 @@ public function getActiveType()
return (false === $this->activeType) ? null : $this->activeType;
}

/************************************************************
* TYPES QUERY
************************************************************/

/**
* @param array $criteria
* @return OrganizationTypeQuery
*/
public function userTypeQuery($criteria = []): OrganizationTypeQuery
{
/** @noinspection PhpUndefinedMethodInspection */
$query = OrganizationType::find()
->organization($this);

if (!empty($criteria)) {
QueryHelper::configure(
$query,
$criteria
);
}

return $query;
}

/************************************************************
* TYPES
************************************************************/
Expand All @@ -98,8 +122,7 @@ public function getActiveType()
public function getTypes($criteria = [])
{
if (null === $this->types) {
$this->types = OrganizationType::find()
->organization($this);
$this->types = $this->userTypeQuery();
}

if (!empty($criteria)) {
Expand All @@ -126,8 +149,7 @@ public function setTypes($types)
}

// Reset the query
$this->types = OrganizationType::find()
->organization($this);
$this->types = $this->userTypeQuery();

// Remove all types
$this->types->setCachedResult([]);
Expand Down
18 changes: 14 additions & 4 deletions src/elements/UsersAttributeTrait.php
Expand Up @@ -15,7 +15,6 @@
use craft\helpers\ArrayHelper;
use flipbox\craft\ember\helpers\QueryHelper;
use flipbox\organizations\records\UserAssociation;
use flipbox\organizations\records\UserAssociation as OrganizationUsersRecord;

/**
* @author Flipbox Factory <hello@flipboxfactory.com>
Expand All @@ -39,7 +38,7 @@ private static function eagerLoadingUsersMap(array $sourceElements)

$map = (new Query())
->select(['organizationId as source', 'userId as target'])
->from(OrganizationUsersRecord::tableName())
->from(UserAssociation::tableName())
->where(['organizationId' => $sourceElementIds])
->all();

Expand Down Expand Up @@ -68,8 +67,9 @@ public function setUsersFromRequest(string $identifier = 'users')
return $this;
}


/************************************************************
* USERS
* USERS QUERY
************************************************************/

/**
Expand Down Expand Up @@ -137,7 +137,13 @@ public function setUsers($users)
// Remove all users
$this->users->setCachedResult([]);

$this->addUsers($users);
if (!empty($users)) {
if (!is_array($users)) {
$users = [$users];
}

$this->addUsers($users);
}

return $this;
}
Expand Down Expand Up @@ -167,6 +173,10 @@ public function addUsers(array $users)
return $this;
}

/**
* @param $user
* @return User
*/
protected function resolveUser($user)
{
if (is_array($user) &&
Expand Down
25 changes: 24 additions & 1 deletion src/queries/OrganizationQuery.php
Expand Up @@ -36,6 +36,7 @@ class OrganizationQuery extends ElementQuery
use UserAttributeTrait,
UserGroupAttributeTrait,
UserTypeAttributeTrait,
UserStateAttributeTrait,
OrganizationTypeAttributeTrait;

/**
Expand Down Expand Up @@ -94,6 +95,7 @@ protected function prepareRelationsParams()
$this->applyUserParam($alias);
$this->applyUserGroupParam($alias);
$this->applyUserTypeParam($alias);
$this->applyUserStateParam($alias);
}


Expand Down Expand Up @@ -193,7 +195,7 @@ protected function applyUserGroupParam(string $alias)


/************************************************************
* USER COLLECTION
* USER TYPE
************************************************************/

/**
Expand All @@ -220,6 +222,27 @@ protected function applyUserTypeParam(string $alias)
}


/************************************************************
* USER STATE
************************************************************/

/**
* @param string $alias
*
* @return void
*/
protected function applyUserStateParam(string $alias)
{
if (empty($this->userState)) {
return;
}

$this->subQuery->andWhere(
Db::parseParam($alias . '.state', $this->userState)
);
}


/************************************************************
* TYPE
************************************************************/
Expand Down
42 changes: 42 additions & 0 deletions src/queries/UserStateAttributeTrait.php
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (c) Flipbox Digital Limited
* @license https://flipboxfactory.com/software/organization/license
* @link https://www.flipboxfactory.com/software/organization/
*/

namespace flipbox\organizations\queries;

/**
* @author Flipbox Factory <hello@flipboxfactory.com>
* @since 1.0.0
*/
trait UserStateAttributeTrait
{
/**
* The user state(s) that the resulting organizations’ users must be in.
*
* @var string|string[]|null
*/
public $userState;

/**
* @param string|string[]|int|int[]|null $value
* @return static The query object
*/
public function setUserState($value)
{
$this->userState = $value;
return $this;
}

/**
* @param string|string[]|int|int[]|null $value
* @return static The query object
*/
public function userState($value)
{
return $this->setUserState($value);
}
}

0 comments on commit 7651464

Please sign in to comment.