コード例 #1
0
ファイル: users.php プロジェクト: knigherrant/decopatio
 /**
  * Stores the user object
  *
  * @since	1.0
  * @access	public
  */
 public function store()
 {
     // Check for request forgeries
     FD::checkToken();
     // Load front end's language file
     FD::language()->loadSite();
     // Get the current task
     $task = $this->getTask();
     // Determine if this is an edited user.
     $id = $this->input->get('id', 0, 'int');
     $id = !$id ? null : $id;
     // Get the posted data
     $post = $this->input->getArray('post');
     // this should come from backend user management page only.
     $autoApproval = isset($post['autoapproval']) ? $post['autoapproval'] : 0;
     // Create an options array for custom fields
     $options = array();
     if (!$id) {
         $user = new SocialUser();
         // Get the profile id
         $profileId = $this->input->get('profileId');
     } else {
         // Here we assume that the user record already exists.
         $user = FD::user($id);
         // Get the profile id from the user
         $profileId = $user->getProfile()->id;
         $options['data'] = true;
         $options['dataId'] = $id;
         $options['dataType'] = SOCIAL_TYPE_USER;
     }
     // Set the profile id
     $options['profile_id'] = $profileId;
     // Set the group
     $options['group'] = SOCIAL_FIELDS_GROUP_USER;
     // Load the profile
     $profile = FD::table('Profile');
     $profile->load($profileId);
     // Set the visibility
     // since this is at backend so we assume admin is editing someone else.
     if (!$id) {
         $options['visible'] = SOCIAL_PROFILES_VIEW_REGISTRATION;
     }
     // Get fields model
     $fieldsModel = ES::model('Fields');
     // Get the custom fields
     $fields = $fieldsModel->getCustomFields($options);
     // Initialize default registry
     $registry = ES::registry();
     // Get disallowed keys so we wont get wrong values.
     $disallowed = array(ES::token(), 'option', 'task', 'controller', 'autoapproval');
     // Process $_POST vars
     foreach ($post as $key => $value) {
         if (!in_array($key, $disallowed)) {
             if (is_array($value)) {
                 $value = json_encode($value);
             }
             $registry->set($key, $value);
         }
     }
     // Test to see if the points has changed.
     $points = $this->input->get('points', 0, 'int');
     // Lets get the difference of the points
     $userPoints = $user->getPoints();
     // If there is a difference, the admin may have altered the user points
     if ($userPoints != $points) {
         // Insert a new points record for this new adjustments.
         if ($points > $userPoints) {
             // If the result points is larger, we always need to subtract and get the balance.
             $totalPoints = $points - $userPoints;
         } else {
             // If the result points is smaller, we always need to subtract.
             $totalPoints = -($userPoints - $points);
         }
         $pointsLib = FD::points();
         $pointsLib->assignCustom($user->id, $totalPoints, JText::_('COM_EASYSOCIAL_POINTS_ADJUSTMENTS'));
         $user->points = $points;
     }
     // Convert the values into an array.
     $data = $registry->toArray();
     // Get the fields lib
     $fieldsLib = FD::fields();
     // Build arguments to be passed to the field apps.
     $args = array(&$data, &$user);
     // @trigger onAdminEditValidate
     $errors = $fieldsLib->trigger('onAdminEditValidate', SOCIAL_FIELDS_GROUP_USER, $fields, $args);
     // If there are errors, we should be exiting here.
     if (is_array($errors) && count($errors) > 0) {
         $this->view->setMessage(JText::_('COM_EASYSOCIAL_PROFILE_SAVE_ERRORS'), SOCIAL_MSG_ERROR);
         // We need to set the data into the post again because onEditValidate might have changed the data structure
         JRequest::set($data, 'post');
         return $this->view->call('form', $errors);
     }
     // @trigger onAdminEditBeforeSave
     $errors = $fieldsLib->trigger('onAdminEditBeforeSave', SOCIAL_FIELDS_GROUP_USER, $fields, $args);
     if (is_array($errors) && count($errors) > 0) {
         $this->view->setMessage(JText::_('COM_EASYSOCIAL_PROFILE_ERRORS_IN_FORM'), SOCIAL_MSG_ERROR);
         // We need to set the data into the post again because onEditValidate might have changed the data structure
         JRequest::set($data, 'post');
         return $this->view->call('form', $errors);
     }
     // Update the user's gid
     $gid = $this->input->get('gid', array(), 'array');
     $data['gid'] = $gid;
     // Bind the user object with the form data.
     $user->bind($data);
     // Create a new user record if the id don't exist yet.
     if (!$id) {
         $model = ES::model('Users');
         $user = $model->create($data, $user, $profile);
         if (!$user) {
             $this->view->setMessage($model->getError(), SOCIAL_MSG_ERROR);
             // We need to set the data into the post again because onEditValidate might have changed the data structure
             JRequest::set($data, 'post');
             return $this->view->call('form');
         }
         // If admin selected auto approval, automatically approve this user.
         if ($autoApproval) {
             $user->approve(false);
         }
         $message = $autoApproval ? JText::_('COM_EASYSOCIAL_USERS_CREATED_SUCCESSFULLY_AND_APPROVED') : JText::_('COM_EASYSOCIAL_USERS_CREATED_SUCCESSFULLY');
     } else {
         // If this was an edited user, save the user object.
         $user->save();
         $message = JText::_('COM_EASYSOCIAL_USERS_USER_UPDATED_SUCCESSFULLY');
     }
     // Reconstruct args
     $args = array(&$data, &$user);
     // @trigger onEditAfterSave
     $fieldsLib->trigger('onAdminEditAfterSave', SOCIAL_FIELDS_GROUP_USER, $fields, $args);
     // Bind the custom fields for the user.
     $user->bindCustomFields($data);
     // Reconstruct args
     $args = array(&$data, &$user);
     // @trigger onEditAfterSaveFields
     $fieldsLib->trigger('onAdminEditAfterSaveFields', SOCIAL_FIELDS_GROUP_USER, $fields, $args);
     // Prepare the dispatcher
     FD::apps()->load(SOCIAL_TYPE_USER);
     $dispatcher = FD::dispatcher();
     $args = array(&$user, &$fields, &$data);
     // @trigger: onUserProfileUpdate
     $dispatcher->trigger(SOCIAL_TYPE_USER, 'onUserProfileUpdate', $args);
     // Process notifications
     if (isset($post['notifications']) && !empty($post['notifications'])) {
         $systemNotifications = $post['notifications']['system'];
         $emailNotifications = $post['notifications']['email'];
         // Store the notification settings for this user.
         $model = ES::model('Notifications');
         $model->saveNotifications($systemNotifications, $emailNotifications, $user);
     }
     // Process privacy items
     if (isset($post['privacy']) && !empty($post['privacy'])) {
         $resetPrivacy = isset($post['privacyReset']) ? true : false;
         $user->bindPrivacy($post['privacy'], $post['privacyID'], $post['privacyCustom'], $post['privacyOld'], $resetPrivacy);
     }
     $this->view->setMessage($message, SOCIAL_MSG_SUCCESS);
     return $this->view->call(__FUNCTION__, $task, $user);
 }