Exemple #1
0
 /**
  * @deprecated since version number
  * Default implementation for user picker filter.
  * 
  * @param type $keywords
  * @param type $maxResults
  * @param type $friendsOnly
  * @param type $permission
  * @return type
  */
 public function getUserPickerResult($keywords = null, $maxResults = null, $friendsOnly = false, $permission = null)
 {
     if (!Yii::$app->getModule('friendship')->getIsEnabled()) {
         //We don't use the permission here for filtering since we include user with no permission as disabled in the result.
         //The problem here is we do not prefer users with permission in the query.
         $users = $this->getUserByFilter($keywords, $maxResults);
         return UserPicker::asJSON($users, $permission);
     }
     $friends = self::getFriendsByFilter($keywords, $maxResults);
     //Create userinfo json with with set 'disabled' field if the user is not permitted
     $jsonResult = UserPicker::asJSON($friends, $permission);
     //Fill the remaining space with member users with the given permission
     if (!$friendsOnly && count($friends) < $maxResults) {
         $additionalUser = [];
         //Here we filter with permission since we don't want to have non friend user without the permission in the result
         foreach ($this->getUserByFilter($keywords, $maxResults - count($friends), $permission) as $user) {
             if (!$this->containsUser($friends, $user)) {
                 $additionalUser[] = $user;
             }
         }
         $jsonResult = array_merge($jsonResult, UserPicker::asJSON($additionalUser));
     }
     return $jsonResult;
 }
Exemple #2
0
 /**
  * Creates a json user array used in the userpicker js frontend.
  * The $cfg is used to specify the filter values the following values are available:
  * 
  * query - (ActiveQuery) The initial query which is used to append additional filters. - default = User Friends if friendship module is enabled else User::find()
  * 
  * active - (boolean) Specifies if only active user should be included in the result - default = true
  * 
  * maxResults - (int) The max number of entries returned in the array - default = 10
  * 
  * keyword - (string) A keyword which filters user by username, firstname, lastname, email and title
  * 
  * permission - (BasePermission) An additional permission filter
  * 
  * fillQuery - (ActiveQuery) Can be used to fill the result array if the initial query does not return the maxResults, these results will have a lower priority
  * 
  * fillUser - (boolean) When set to true and no fillQuery is given the result is filled with User::find() results
  * 
  * disableFillUser - Specifies if the results of the fillQuery should be disabled in the userpicker results - default = true
  * 
  * @param type $cfg filter configuration
  * @return type json representation used by the userpicker
  */
 public static function filter($cfg = null)
 {
     $defaultCfg = ['active' => true, 'maxResult' => 10, 'disableFillUser' => true, 'keyword' => null, 'permission' => null, 'fillQuery' => null, 'fillUser' => false];
     $cfg = $cfg == null ? $defaultCfg : array_merge($defaultCfg, $cfg);
     //If no initial query is given we use getFriends if friendship module is enabled otherwise all users
     if (!isset($cfg['query'])) {
         $cfg['query'] = Yii::$app->getModule('friendship')->getIsEnabled() ? Yii::$app->user->getIdentity()->getFriends() : UserFilter::find();
     }
     //Filter the initial query and disable user without the given permission
     $user = UserFilter::filter($cfg['query'], $cfg['keyword'], $cfg['maxResult'], null, $cfg['active']);
     $jsonResult = self::asJSON($user, $cfg['permission'], 2);
     //Fill the result with additional users if it's allowed and the result count less than maxResult
     if (count($user) < $cfg['maxResult'] && (isset($cfg['fillQuery']) || $cfg['fillUser'])) {
         //Filter out users by means of the fillQuery or default the fillQuery
         $fillQuery = isset($cfg['fillQuery']) ? $cfg['fillQuery'] : UserFilter::find();
         UserFilter::addKeywordFilter($fillQuery, $cfg['keyword'], $cfg['maxResult'] - count($user));
         $fillQuery->andFilterWhere(['not in', 'id', self::getUserIdArray($user)]);
         $fillUser = $fillQuery->all();
         //Either the additional users are disabled (by default) or we disable them by permission
         $disableCondition = isset($cfg['permission']) ? $cfg['permission'] : $cfg['disableFillUser'];
         $jsonResult = array_merge($jsonResult, UserPicker::asJSON($fillUser, $disableCondition, 1));
     }
     return $jsonResult;
 }