/** * 用户与群组绑定 */ public function bind($userId, $groupId) { $criteria = new CDbCriteria(); $criteria->condition = "user_id=:user_id and group_id =:group_id"; $criteria->params = array('user_id' => $userId, 'group_id' => $groupId); $item = UserGroupRelation::model()->find($criteria); if (empty($item)) { $group = new UserGroupRelation(); $group['user_id'] = $userId; $group['group_id'] = $groupId; $group->save(); return array('success' => true, 'msg' => 'success'); } else { return array('success' => false, 'msg' => 'name existed'); } }
/** * Updates user-group relations. Inserts the selected friend into the selected groups or remove the selected friend * from unselected groups */ public function actionUpdateGroup() { $model = new GroupSettingsForm(); $processOutput = true; //Get all of the groups that the logged in user has created $groupsOfUser = Groups::model()->findAll('owner=:owner', array(':owner' => Yii::app()->user->id)); $selected_groups = array(); //array to hold all the groups of the user's selected friend to be used for the checking the checkboxes in the initial dialog $unselected_groups = array(); //array to hold the unselected user groups so that it can be used for user removal from unselected groups if (isset($_REQUEST['friendId'])) { $friendId = (int) $_REQUEST['friendId']; //Take all the user-group relation rows that the user's friend added to $relationRowsSelectedFriendBelongsTo = UserGroupRelation::model()->findAll('userId=:userId', array(':userId' => $friendId)); //Get only the group ID fields into $selected_groups from the obtained rows foreach ($relationRowsSelectedFriendBelongsTo as $relationRow) { $selected_groups[] = $relationRow->groupId; } } if (isset($_POST['GroupSettingsForm'])) { //echo 'SET'; $model->attributes = $_POST['GroupSettingsForm']; //$model->groupStatusArray = $_POST['GroupSettingsForm']['groupStatusArray']; if ($model->validate()) { // if(!empty($model->groupStatusArray)) //Check empty() in order to avoid error in foreach // { // echo 'Selecteds: '; // // foreach($model->groupStatusArray as $selectedItem) // { // echo $selectedItem.' '; // } // // echo '</br>'; // } //We know the selected groups from $model->groupStatusArray, but not know the unselected ones //So construct the unselected groups using $groupsOfUser and $model->groupStatusArray foreach ($groupsOfUser as $selectedOwnerGroup) { $groupFound = false; if (!empty($model->groupStatusArray)) { foreach ($model->groupStatusArray as $selectedFriendGroup) { if ($selectedOwnerGroup->id == $selectedFriendGroup) { $groupFound = true; break; } } } else { } if ($groupFound == false) { $unselected_groups[] = $selectedOwnerGroup->id; } } // if(!empty($unselected_groups)) //Check empty() in order to avoid error in foreach // { // echo 'UnSelecteds: '; // // foreach($unselected_groups as $unselectedItem) // { // echo $unselectedItem.' '; // } // // echo '</br>'; // } //Check whether the friend belongs to the selected group or not before. If he does not, add his relation to the traceper_user_group_relation table if (!empty($model->groupStatusArray)) { foreach ($model->groupStatusArray as $selectedFriendGroup) { $relationQueryResult = UserGroupRelation::model()->find(array('condition' => 'userId=:userId AND groupId=:groupId', 'params' => array(':userId' => $friendId, ':groupId' => $selectedFriendGroup))); if ($relationQueryResult != null) { //traceper_user_group_relation table already has the desired relation, so do nothing //echo 'Relation already exists for groupId '.$selectedFriendGroup.'</br>'; } else { $userGroupRelation = new UserGroupRelation(); $userGroupRelation->userId = $friendId; $userGroupRelation->groupId = $selectedFriendGroup; if ($userGroupRelation->save()) { //Relation added to the traceper_user_group_relation table //echo 'Relation added for groupId '.$selectedFriendGroup.'</br>'; } else { echo CJSON::encode(array("result" => "Unknown error")); Yii::app()->end(); } } } } //Check whether the friend belongs to the unselected group or not. If he does, delete his relation from the traceper_user_group_relation table foreach ($unselected_groups as $unselectedFriendGroup) { $relationQueryResult = UserGroupRelation::model()->find(array('condition' => 'userId=:userId AND groupId=:groupId', 'params' => array(':userId' => $friendId, ':groupId' => $unselectedFriendGroup))); if ($relationQueryResult != null) { if ($relationQueryResult->delete()) { //Relation deleted from the traceper_user_group_relation table //echo 'Relation deleted for groupId '.$unselectedFriendGroup.'</br>'; } else { echo CJSON::encode(array("result" => "Unknown error")); Yii::app()->end(); } } else { //traceper_user_group_relation table has not the desired relation, so do nothing //echo 'Relation does not already exist for groupId '.$unselectedFriendGroup.'</br>'; } } echo CJSON::encode(array("result" => "1")); Yii::app()->end(); } if (Yii::app()->request->isAjaxRequest) { $processOutput = false; //echo ' Ajax'; } } else { //echo 'ELSE'; //If the form is opened, check the checkboxes according to traceper_user_group_relation table //Here $model->groupStatusArray is assigned to all of the groups that the user's friend is resgistered no matter the groups' owner is the logged user or not $model->groupStatusArray = $selected_groups; } Yii::app()->clientScript->scriptMap['jquery.js'] = false; Yii::app()->clientScript->scriptMap['jquery-ui.min.js'] = false; //$this->renderPartial('groupSettings',array('model'=>$model, 'groupsOfUser'=>$groupsOfUser, 'relationRowsSelectedFriendBelongsTo'=>$relationRowsSelectedFriendBelongsTo, 'friendId'=>$friendId), false, $processOutput); $this->renderPartial('groupSettings', array('model' => $model, 'groupsOfUser' => $groupsOfUser, 'friendId' => $friendId), false, $processOutput); }