/** * Save user's information. * * @since 1.0 * @access public */ public function save() { // Check for request forgeries. FD::checkToken(); // Ensure that the user is registered FD::requireLogin(); // Clear previous session $session = JFactory::getSession(); $session->clear('easysocial.profile.errors', SOCIAL_SESSION_NAMESPACE); // Get post data. $post = JRequest::get('POST'); // Get the current view. $view = $this->getCurrentView(); // Get all published fields apps that are available in the current form to perform validations $fieldsModel = FD::model('Fields'); // Get current user. $my = FD::user(); // Only fetch relevant fields for this user. $options = array('profile_id' => $my->getProfile()->id, 'data' => true, 'dataId' => $my->id, 'dataType' => SOCIAL_TYPE_USER, 'visible' => SOCIAL_PROFILES_VIEW_EDIT, 'group' => SOCIAL_FIELDS_GROUP_USER); $fields = $fieldsModel->getCustomFields($options); // Load json library. $json = FD::json(); // Initialize default registry $registry = FD::registry(); // Get disallowed keys so we wont get wrong values. $disallowed = array(FD::token(), 'option', 'task', 'controller'); // 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); } } // Convert the values into an array. $data = $registry->toArray(); // Perform field validations here. Validation should only trigger apps that are loaded on the form // @trigger onRegisterValidate $fieldsLib = FD::fields(); // Get the general field trigger handler $handler = $fieldsLib->getHandler(); // Build arguments to be passed to the field apps. $args = array(&$data, &$my); // Ensure that there is no errors. // @trigger onEditValidate $errors = $fieldsLib->trigger('onEditValidate', SOCIAL_FIELDS_GROUP_USER, $fields, $args, array($handler, 'validate')); // If there are errors, we should be exiting here. if (is_array($errors) && count($errors) > 0) { $view->setMessage(JText::_('COM_EASYSOCIAL_PROFILE_SAVE_ERRORS'), SOCIAL_MSG_ERROR); // We need to set the proper vars here so that the es-wrapper contains appropriate class JRequest::setVar('view', 'profile', 'POST'); JRequest::setVar('layout', 'edit', 'POST'); // We need to set the data into the post again because onEditValidate might have changed the data structure JRequest::set($data, 'post'); return $view->call('edit', $errors, $data); } // @trigger onEditBeforeSave $errors = $fieldsLib->trigger('onEditBeforeSave', SOCIAL_FIELDS_GROUP_USER, $fields, $args, array($handler, 'beforeSave')); if (is_array($errors) && count($errors) > 0) { $view->setMessage(JText::_('COM_EASYSOCIAL_PROFILE_ERRORS_IN_FORM'), SOCIAL_MSG_ERROR); // We need to set the proper vars here so that the es-wrapper contains appropriate class JRequest::setVar('view', 'profile'); JRequest::setVar('layout', 'edit'); // We need to set the data into the post again because onEditValidate might have changed the data structure JRequest::set($data, 'post'); return $view->call('edit', $errors); } // Bind the my object with appropriate data. $my->bind($data); // Save the user object. $my->save(); // Reconstruct args $args = array(&$data, &$my); // @trigger onEditAfterSave $fieldsLib->trigger('onEditAfterSave', SOCIAL_FIELDS_GROUP_USER, $fields, $args); // Bind custom fields for the user. $my->bindCustomFields($data); // Reconstruct args $args = array(&$data, &$my); // @trigger onEditAfterSaveFields $fieldsLib->trigger('onEditAfterSaveFields', SOCIAL_FIELDS_GROUP_USER, $fields, $args); // Now we update the Facebook details if it is available $associatedFacebook = $this->input->get('associatedFacebook', 0, 'int'); if (!empty($associatedFacebook)) { $facebookPull = $this->input->get('oauth_facebook_pull', null, 'default'); $facebookPush = $this->input->get('oauth_facebook_push', null, 'default'); $my = FD::user(); $facebookTable = $my->getOAuth(SOCIAL_TYPE_FACEBOOK); if ($facebookTable) { $facebookTable->pull = $facebookPull; $facebookTable->push = $facebookPush; $facebookTable->store(); } } // Add stream item to notify the world that this user updated their profile. $my->addStream('updateProfile'); // Update indexer $my->syncIndex(); // @points: profile.update // Assign points to the user when their profile is updated $points = FD::points(); $points->assign('profile.update', 'com_easysocial', $my->id); // 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); // @trigger onProfileCompleteCheck // This should return an array of booleans to state which field is filled in. // We count the returned result since it will be an array of trues that marks the field that have data for profile completeness checking. // We do this after all the data has been saved, and we reget the fields from the model again. // We also need to reset the cached field data SocialTableField::$_fielddata = array(); $fields = $fieldsModel->getCustomFields(array('profile_id' => $my->getProfile()->id, 'data' => true, 'dataId' => $my->id, 'dataType' => SOCIAL_TYPE_USER, 'visible' => SOCIAL_PROFILES_VIEW_EDIT, 'group' => SOCIAL_FIELDS_GROUP_USER)); $args = array(&$my); $completedFields = $fieldsLib->trigger('onProfileCompleteCheck', SOCIAL_FIELDS_GROUP_USER, $fields, $args); $table = FD::table('Users'); $table->load(array('user_id' => $my->id)); $table->completed_fields = count($completedFields); $table->store(); $view->setMessage(JText::_('COM_EASYSOCIAL_PROFILE_ACCOUNT_UPDATED_SUCCESSFULLY'), SOCIAL_MSG_SUCCESS); return $view->call(__FUNCTION__, $my); }
/** * Helper method to determine if this field is required. * * @since 1.0 * @access public * @return bool */ public function isRequired() { return $this->field->isRequired(); }