/** * 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; }
/** * Used by user picker for adding additional users to a conversaion, * searches user which are allwed messaging permissions for the current user (v1.1). * Disables users already participating in a conversation. * * @return type */ public function actionSearchAddUser() { Yii::$app->response->format = 'json'; $keyword = Yii::$app->request->get('keyword'); $message = $this->getMessage(Yii::$app->request->get('id')); if ($message == null) { throw new HttpException(404, 'Could not find message!'); } $maxResults = 10; $result = null; if (version_compare(Yii::$app->version, '1.1', 'lt')) { $result = $this->findUserByFilter($keyword, $maxResults); } else { $result = \humhub\modules\user\models\UserFilter::forUser()->getUserPickerResult($keyword, $maxResults, false, new SendMail()); } //Disable already participating users $i = 0; foreach ($result as $user) { $result[$i++]['disabled'] = $this->isParticipant($message, $user); } return $result; }