/** * Form Validator which checks the invite field * * @param type $attribute * @param type $params */ public function checkInvite($attribute, $params) { // Check if email field is not empty if ($this->{$attribute} != "") { $invites = explode(",", $this->{$attribute}); foreach ($invites as $userGuid) { $userGuid = preg_replace("/[^A-Za-z0-9\\-]/", '', $userGuid); if ($userGuid == "") { continue; } // Try load user $user = User::model()->findByAttributes(array('guid' => $userGuid)); if ($user != null) { $membership = SpaceMembership::model()->findByAttributes(array('space_id' => $this->space->id, 'user_id' => $user->id)); if ($membership != null && $membership->status == SpaceMembership::STATUS_MEMBER) { $this->addError($attribute, Yii::t('SpaceModule.forms_SpaceInviteForm', "User is already member!")); continue; } } else { $this->addError($attribute, Yii::t('SpaceModule.forms_SpaceInviteForm', "User not found!")); continue; } $this->invites[] = $user; } } }
public function actionIndex() { $criteria = new CDbCriteria(); if (HSetting::Get('spaceOrder', 'space') == 0) { $criteria->order = 'name ASC'; } else { $criteria->order = 'last_visit DESC'; } $memberships = SpaceMembership::model()->with('space')->findAllByAttributes(array('user_id' => Yii::app()->user->id, 'status' => SpaceMembership::STATUS_MEMBER), $criteria); $this->renderPartial('index', array('memberships' => $memberships), false, true); }
/** * On User delete, also delete his space related stuff * * @param type $event */ public static function onUserDelete($event) { $user = $event->sender; // Check if the user owns some spaces foreach (SpaceMembership::GetUserSpaces($user->id) as $space) { if ($space->isSpaceOwner($user->id)) { throw new CHttpException(500, Yii::t('SpaceModule.base', 'Could not delete user who is a space owner! Name of Space: {spaceName}', array('spaceName' => $space->name))); } } // Cancel all space memberships foreach (SpaceMembership::model()->findAllByAttributes(array('user_id' => $user->id)) as $membership) { $membership->space->removeMember($user->id); } // Cancel all space invites by the user foreach (SpaceMembership::model()->findAllByAttributes(array('originator_user_id' => $user->id, 'status' => SpaceMembership::STATUS_INVITED)) as $membership) { $membership->space->removeMember($membership->user_id); } return true; }
/** * Members Administration Action */ public function actionMembers() { $membersPerPage = 10; $space = $this->getSpace(); // User Role Management if (isset($_POST['users'])) { $users = Yii::app()->request->getParam('users'); // Loop over all users in Form foreach ($users as $userGuid) { // Get informations if (isset($_POST['user_' . $userGuid])) { $userSettings = Yii::app()->request->getParam('user_' . $userGuid); $user = User::model()->findByAttributes(array('guid' => $userGuid)); if ($user != null) { // No changes on the Owner if ($space->isSpaceOwner($user->id)) { continue; } $membership = SpaceMembership::model()->findByAttributes(array('user_id' => $user->id, 'space_id' => $space->id)); if ($membership != null) { $membership->invite_role = isset($userSettings['inviteRole']) && $userSettings['inviteRole'] == 1 ? 1 : 0; $membership->admin_role = isset($userSettings['adminRole']) && $userSettings['adminRole'] == 1 ? 1 : 0; $membership->share_role = isset($userSettings['shareRole']) && $userSettings['shareRole'] == 1 ? 1 : 0; $membership->save(); } } } } // Change owner if changed if ($space->isSpaceOwner()) { $owner = $space->getSpaceOwner(); $newOwnerId = Yii::app()->request->getParam('ownerId'); if ($newOwnerId != $owner->id) { if ($space->isMember($newOwnerId)) { $space->setSpaceOwner($newOwnerId); // Redirect to current space $this->redirect($this->createUrl('admin/members', array('sguid' => $this->getSpace()->guid))); } } } Yii::app()->user->setFlash('data-saved', Yii::t('SpaceModule.controllers_AdminController', 'Saved')); } // Updated Users $criteria = new CDbCriteria(); $criteria->condition = "1"; // Allow User Searches $search = Yii::app()->request->getQuery('search'); if ($search != "") { $criteria->join = "LEFT JOIN user ON memberships.user_id = user.id "; $criteria->condition .= " AND ("; $criteria->condition .= ' user.username LIKE :search'; $criteria->condition .= ' OR user.email like :search'; $criteria->condition .= " ) "; $criteria->params = array(':search' => '%' . $search . '%'); } //ToDo: Better Counting $allMemberCount = count($space->memberships($criteria)); $pages = new CPagination($allMemberCount); $pages->setPageSize($membersPerPage); $pages->applyLimit($criteria); $members = $space->memberships($criteria); $invited_members = SpaceMembership::model()->findAllByAttributes(array('space_id' => $space->id, 'status' => SpaceMembership::STATUS_INVITED)); $this->render('members', array('space' => $space, 'members' => $members, 'invited_members' => $invited_members, 'item_count' => $allMemberCount, 'page_size' => $membersPerPage, 'search' => $search, 'pages' => $pages)); }
/** * Remove Membership * * @param $userId UserId of User to Remove */ public function removeMember($userId = "") { if ($userId == "") { $userId = Yii::app()->user->id; } $user = User::model()->findByPk($userId); $membership = $this->getMembership($userId); if ($this->isSpaceOwner($userId)) { return false; } if ($membership == null) { return true; } // If was member, create a activity for that if ($membership->status == SpaceMembership::STATUS_MEMBER) { $activity = new Activity(); $activity->content->space_id = $this->getOwner()->id; $activity->content->visibility = Content::VISIBILITY_PRIVATE; $activity->type = "ActivitySpaceMemberRemoved"; $activity->created_by = $userId; $activity->save(); $activity->fire(); } // Was invited, but declined the request if ($membership->status == SpaceMembership::STATUS_INVITED) { SpaceInviteDeclinedNotification::fire($membership->originator_user_id, $user, $this->getOwner()); } foreach (SpaceMembership::model()->findAllByAttributes(array('user_id' => $userId, 'space_id' => $this->getOwner()->id)) as $membership) { $membership->delete(); } // Cleanup Notifications SpaceApprovalRequestNotification::remove($userId, $this->getOwner()); SpaceInviteNotification::remove($userId, $this->getOwner()); SpaceApprovalRequestNotification::remove($userId, $this->getOwner()); }
/** * Returns a JSON Object which contains a lot of informations about * current states like new posts on workspaces */ public function actionGetFrontEndInfo() { $json = array(); $json['workspaces'] = array(); $criteria = new CDbCriteria(); $criteria->order = 'last_visit DESC'; $memberships = SpaceMembership::model()->with('workspace')->findAllByAttributes(array('user_id' => Yii::app()->user->id, 'status' => SpaceMembership::STATUS_MEMBER), $criteria); foreach ($memberships as $membership) { $workspace = $membership->workspace; $info = array(); $info['name'] = CHtml::encode($workspace->name); #$info['id'] = $workspace->id; # should be hidden at frontend $info['guid'] = $workspace->guid; $info['totalItems'] = $workspace->countItems(); $info['newItems'] = $membership->countNewItems(); $json['workspaces'][] = $info; } // New notification count $sql = "SELECT count(id)\n\t\tFROM notification\n\t\tWHERE user_id = :user_id AND seen != 1"; $connection = Yii::app()->db; $command = $connection->createCommand($sql); $userId = Yii::app()->user->id; $command->bindParam(":user_id", $userId); $json['newNotifications'] = $command->queryScalar(); print CJSON::encode($json); Yii::app()->end(); }
/** * Before deletion of a Space */ protected function beforeDelete() { foreach (SpaceSetting::model()->findAllByAttributes(array('space_id' => $this->id)) as $spaceSetting) { $spaceSetting->delete(); } // Disable all enabled modules foreach ($this->getAvailableModules() as $moduleId => $module) { if ($this->isModuleEnabled($moduleId)) { $this->disableModule($moduleId); } } HSearch::getInstance()->deleteModel($this); $this->getProfileImage()->delete(); // Remove all Follwers UserFollow::model()->deleteAllByAttributes(array('object_id' => $this->id, 'object_model' => 'Space')); //Delete all memberships: //First select, then delete - done to make sure that SpaceMembership::beforeDelete() is triggered $spaceMemberships = SpaceMembership::model()->findAllByAttributes(array('space_id' => $this->id)); foreach ($spaceMemberships as $spaceMembership) { $spaceMembership->delete(); } UserInvite::model()->deleteAllByAttributes(array('space_invite_id' => $this->id)); // Delete all content objects of this space foreach (Content::model()->findAllByAttributes(array('space_id' => $this->id)) as $content) { $content->delete(); } // When this workspace is used in a group as default workspace, delete the link foreach (Group::model()->findAllByAttributes(array('space_id' => $this->id)) as $group) { $group->space_id = ""; $group->save(); } Wall::model()->deleteAllByAttributes(array('id' => $this->wall_id)); return parent::beforeDelete(); }
/** * Returns a list of all spaces of the given userId * * @param type $userId */ public static function GetUserSpaces($userId = "") { // Take current userid if none is given if ($userId == "") { $userId = Yii::app()->user->id; } $cacheId = "userSpaces_" . $userId; $cacheValue = Yii::app()->cache->get($cacheId); $orderSetting = HSetting::Get('spaceOrder', 'space'); if ($cacheValue === false) { $criteria = new CDbCriteria(); if ($orderSetting == 0) { $criteria->order = 'name ASC'; } else { $criteria->order = 'last_visit DESC'; } $spaces = array(); $memberships = SpaceMembership::model()->with('space')->findAllByAttributes(array('user_id' => $userId, 'status' => SpaceMembership::STATUS_MEMBER), $criteria); foreach ($memberships as $membership) { $spaces[] = $membership->space; } Yii::app()->cache->set($cacheId, $spaces, HSetting::Get('expireTime', 'cache')); return $spaces; } else { return $cacheValue; } }
/** * When a user clicks on the Accept Invite Link, this action is called. * After this the user should be member of this workspace. */ public function actionInviteAccept() { // Get Current Space $space = $this->getSpace(); // Load Pending Membership $membership = SpaceMembership::model()->findByAttributes(array('user_id' => Yii::app()->user->id, 'space_id' => $space->id)); if ($membership == null) { throw new CHttpException(404, Yii::t('SpaceModule.controllers_SpaceController', 'There is no pending invite!')); } // Check there are really an Invite if ($membership->status == SpaceMembership::STATUS_INVITED) { $space->addMember(Yii::app()->user->id); //SpaceInviteAcceptedNotification::fire($membership->originator_user_id, Yii::app()->user, $space); } $this->redirect($space->getUrl()); }
/** * Delete users that are not space members anymore * * @param $spaceId * @throws CDbException */ private function deleteMissingUsers($spaceId) { $attributes = array('space_id' => $spaceId); $reputationUsers = ReputationUser::model()->findAllByAttributes($attributes); foreach ($reputationUsers as $user) { $criteria = new CDbCriteria(); $criteria->condition = 'space_id=:spaceId AND user_id=:userId'; $criteria->params = array(':spaceId' => $spaceId, ':userId' => $user->user_id); if (SpaceMembership::model()->count($criteria) <= 0) { $user->delete(); } } }