Example #1
0
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     $groups = array();
     foreach (Groups::model()->findAll() as $group) {
         $groups[$group->id] = CHtml::encode($group->name);
     }
     $selectedGroups = array();
     foreach (GroupToUser::model()->findAllByAttributes(array('userId' => $model->id)) as $link) {
         $selectedGroups[] = $link->groupId;
     }
     $roles = array();
     foreach (Roles::model()->findAll() as $role) {
         $roles[$role->id] = CHtml::encode($role->name);
     }
     $selectedRoles = array();
     foreach (RoleToUser::model()->findAllByAttributes(array('userId' => $model->id)) as $link) {
         $selectedRoles[] = $link->roleId;
     }
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (!isset($model->userAlias)) {
         $model->userAlias = $model->username;
     }
     if (isset($_POST['User'])) {
         $old = $model->attributes;
         $temp = $model->password;
         $model->attributes = $_POST['User'];
         if ($model->password != "") {
             $model->password = PasswordUtil::createHash($model->password);
         } else {
             $model->password = $temp;
         }
         if (empty($model->userKey)) {
             $model->userKey = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 32)), 0, 32);
         }
         if ($model->save()) {
             $profile = $model->profile;
             if (!empty($profile)) {
                 $profile->emailAddress = $model->emailAddress;
                 $profile->fullName = $model->firstName . ' ' . $model->lastName;
                 $profile->save();
             }
             if ($old['username'] != $model->username) {
                 $fieldRecords = Fields::model()->findAllByAttributes(array('fieldName' => 'assignedTo'));
                 $modelList = array();
                 foreach ($fieldRecords as $record) {
                     $modelList[$record->modelName] = $record->linkType;
                 }
                 foreach ($modelList as $modelName => $type) {
                     if ($modelName == 'Quotes') {
                         $modelName = "Quote";
                     }
                     if ($modelName == 'Products') {
                         $modelName = 'Product';
                     }
                     if (empty($type)) {
                         $list = X2Model::model($modelName)->findAllByAttributes(array('assignedTo' => $old['username']));
                         foreach ($list as $item) {
                             $item->assignedTo = $model->username;
                             $item->save();
                         }
                     } else {
                         $list = X2Model::model($modelName)->findAllBySql("SELECT * FROM " . X2Model::model($modelName)->tableName() . " WHERE assignedTo LIKE '%" . $old['username'] . "%'");
                         foreach ($list as $item) {
                             $assignedTo = explode(", ", $item->assignedTo);
                             $key = array_search($old['username'], $assignedTo);
                             if ($key >= 0) {
                                 $assignedTo[$key] = $model->username;
                             }
                             $item->assignedTo = implode(", ", $assignedTo);
                             $item->save();
                         }
                     }
                 }
                 $profile = Profile::model()->findByAttributes(array('username' => $old['username']));
                 if (isset($profile)) {
                     $profile->username = $model->username;
                     $profile->save();
                 }
             }
             foreach (RoleToUser::model()->findAllByAttributes(array('userId' => $model->id)) as $link) {
                 $link->delete();
             }
             foreach (GroupToUser::model()->findAllByAttributes(array('userId' => $model->id)) as $link) {
                 $link->delete();
             }
             if (isset($_POST['roles'])) {
                 $roles = $_POST['roles'];
                 foreach ($roles as $role) {
                     $link = new RoleToUser();
                     $link->roleId = $role;
                     $link->type = "user";
                     $link->userId = $model->id;
                     $link->save();
                 }
             }
             if (isset($_POST['groups'])) {
                 $groups = $_POST['groups'];
                 foreach ($groups as $group) {
                     $link = new GroupToUser();
                     $link->groupId = $group;
                     $link->userId = $model->id;
                     $link->username = $model->username;
                     $link->save();
                 }
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('update', array('model' => $model, 'groups' => $groups, 'roles' => $roles, 'selectedGroups' => $selectedGroups, 'selectedRoles' => $selectedRoles));
 }
Example #2
0
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     $users = User::getNames();
     $selected = array();
     $links = GroupToUser::model()->findAllByAttributes(array('groupId' => $id));
     foreach ($links as $link) {
         $user = User::model()->findByPk($link->userId);
         if (isset($user)) {
             $selected[] = $user->username;
         }
     }
     unset($users['admin']);
     unset($users['']);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Groups'])) {
         $userLinks = GroupToUser::model()->findAllByAttributes(array('groupId' => $model->id));
         foreach ($userLinks as $userLink) {
             $userLink->delete();
         }
         $model->attributes = $_POST['Groups'];
         if (isset($_POST['users'])) {
             $users = $_POST['users'];
         } else {
             $users = array();
         }
         if ($model->save()) {
             $changeMade = false;
             foreach ($users as $user) {
                 $link = new GroupToUser();
                 $link->groupId = $model->id;
                 $userRecord = User::model()->findByAttributes(array('username' => $user));
                 if (isset($userRecord)) {
                     $link->userId = $userRecord->id;
                     $link->username = $userRecord->username;
                     $test = GroupToUser::model()->findByAttributes(array('groupId' => $model->id, 'userId' => $userRecord->id));
                     if (!isset($test)) {
                         $link->save();
                         $changeMade = true;
                     }
                 }
             }
             if ($changeMade) {
                 Yii::app()->authCache->clear();
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('update', array('model' => $model, 'users' => $users, 'selected' => $selected));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     $groups = array();
     foreach (Groups::model()->findAll() as $group) {
         $groups[$group->id] = $group->name;
     }
     $selectedGroups = array();
     foreach (GroupToUser::model()->findAllByAttributes(array('userId' => $model->id)) as $link) {
         $selectedGroups[] = $link->groupId;
     }
     $roles = array();
     foreach (Roles::model()->findAll() as $role) {
         $roles[$role->id] = $role->name;
     }
     $selectedRoles = array();
     foreach (RoleToUser::model()->findAllByAttributes(array('userId' => $model->id)) as $link) {
         $selectedRoles[] = $link->roleId;
     }
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['User'])) {
         $temp = $model->password;
         $model->attributes = $_POST['User'];
         if ($model->password != "") {
             $model->password = md5($model->password);
         } else {
             $model->password = $temp;
         }
         if ($model->save()) {
             foreach (RoleToUser::model()->findAllByAttributes(array('userId' => $model->id)) as $link) {
                 $link->delete();
             }
             foreach (GroupToUser::model()->findAllByAttributes(array('userId' => $model->id)) as $link) {
                 $link->delete();
             }
             if (isset($_POST['roles'])) {
                 $roles = $_POST['roles'];
                 foreach ($roles as $role) {
                     $link = new RoleToUser();
                     $link->roleId = $role;
                     $link->userId = $model->id;
                     $link->save();
                 }
             }
             if (isset($_POST['groups'])) {
                 $groups = $_POST['groups'];
                 foreach ($groups as $group) {
                     $link = new GroupToUser();
                     $link->groupId = $group;
                     $link->userId = $model->id;
                     $link->username = $model->username;
                     $link->save();
                 }
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('update', array('model' => $model, 'groups' => $groups, 'roles' => $roles, 'selectedGroups' => $selectedGroups, 'selectedRoles' => $selectedRoles));
 }