From 81225e0041acd0063966df514cb224f2dee29b49 Mon Sep 17 00:00:00 2001 From: Deepa Date: Thu, 12 Mar 2020 18:37:25 +0530 Subject: [PATCH] Issue #18 chore: Support for custom fields --- src/users/users.php | 186 +++++++++++++++++++++++++++----------------- 1 file changed, 116 insertions(+), 70 deletions(-) diff --git a/src/users/users.php b/src/users/users.php index 86e51c0..0efc537 100644 --- a/src/users/users.php +++ b/src/users/users.php @@ -38,7 +38,7 @@ class UsersApiResourceUsers extends ApiResource /** * Function delete for user record. * - *@deprecated 2.0 use UsersApiResourceUser delete instead + * @deprecated 2.0 use UsersApiResourceUser delete instead * * @return void */ @@ -217,18 +217,18 @@ public function post() $data['app'] = $app->input->get('app_name', 'Easysocial App', 'STRING'); $data['profile_id'] = $app->input->get('profile_id', 1, 'INT'); - $eobj = new stdClass(); + $eobj = new stdClass; - if( $data['username']=='' || $data['password'] =='' || $data['name'] == '' || $data['email']== '') + if ($data['username'] == '' || $data['password'] == '' || $data['name'] == '' || $data['email'] == '') { $eobj->status = false; $eobj->id = 0; $eobj->code = '403'; - $eobj->message = JText::_( 'PLG_API_USERS_REQUIRED_DATA_EMPTY_MESSAGE' ); + $eobj->message = JText::_('PLG_API_USERS_REQUIRED_DATA_EMPTY_MESSAGE'); $this->plugin->setResponse($eobj); - return; + return; } jimport('joomla.user.helper'); @@ -269,7 +269,7 @@ public function post() // True on success, false otherwise if (!$user->save()) { - //$message = "not created because of " . $user->getError(); + // $message = "not created because of " . $user->getError(); $message = $user->getError(); $eobj->status = false; @@ -277,11 +277,11 @@ public function post() $eobj->code = '403'; $eobj->message = $message; $this->plugin->setResponse($eobj); + return; } else { - /* // Auto registration if( $data['activation'] == 0) @@ -295,50 +295,59 @@ public function post() { $emailSubject = 'Email Subject for activate the account'; $emailBody = 'Email body for for activate the account'; - $user_activation_url = JURI::base().JRoute::_('index.php?option=com_users&task=registration.activate&token=' . $user->activation, false); // Append this URL in your email body - $return = JFactory::getMailer()->sendMail('sender email', 'sender name', $user->email, $emailSubject, $emailBody); + $user_activation_url = JURI::base().JRoute::_('index.php?option=com_users&task=registration.activate&token=' . $user->activation, false); + // Append this URL in your email body + $return = JFactory::getMailer()->sendMail('sender email', 'sender name', $user->email, $emailSubject, $emailBody); } */ + /* Update profile type */ - $profiles = FD::model( 'profiles' ); + $profiles = FD::model('profiles'); $all_profiles = $profiles->getAllProfiles(); - foreach ($all_profiles as $key) { - if($key->id == $data['profile_id']){ - $profiles->updateUserProfile($user->id,$data['profile_id']); + foreach ($all_profiles as $key) + { + if ($key->id == $data['profile_id']) + { + $profiles->updateUserProfile($user->id, $data['profile_id']); } } $mail_sent = $this->sendRegisterEmail($data); - $easysocial = JPATH_ADMINISTRATOR .'/components/com_easysocial/easysocial.php'; - //eb version - if( JFile::exists( $easysocial ) ) + $easysocial = JPATH_ADMINISTRATOR . '/components/com_easysocial/easysocial.php'; + + // Eb version + if (JFile::exists($easysocial)) { $pobj = $this->createEsprofile($user->id); - //$message = "created of username-" . $user->username .",send mail of details please check"; + /*$message = "created of username-" . $user->username .",send mail of details please check";*/ $message = JText::_('PLG_API_USERS_ACCOUNT_CREATED_SUCCESSFULLY_MESSAGE'); } else - $message = JText::_('PLG_API_USERS_ACCOUNT_CREATED_SUCCESSFULLY_MESSAGE'); + { + $message = JText::_('PLG_API_USERS_ACCOUNT_CREATED_SUCCESSFULLY_MESSAGE'); + } // Assign badge for the person. $badge = FD::badges(); - $badge->log( 'com_easysocial' , 'registration.create' , $user->id , JText::_( 'COM_EASYSOCIAL_REGISTRATION_BADGE_REGISTERED' ) ); - + $badge->log('com_easysocial', 'registration.create', $user->id, JText::_('COM_EASYSOCIAL_REGISTRATION_BADGE_REGISTERED')); } + $userid = $user->id; // Result message - //$result = array('user id ' => $userid, 'message' => $message); - //$result = ($userid) ? $result : $message; + + /*$result = array('user id ' => $userid, 'message' => $message); + $result = ($userid) ? $result : $message;*/ $eobj->status = true; $eobj->id = $userid; $eobj->code = '200'; $eobj->message = $message; $this->plugin->setResponse($eobj); + return; } @@ -351,19 +360,21 @@ public function post() */ public function get() { - $input = JFactory::getApplication()->input; // If we have an id try to fetch the user if ($id = $input->get('id')) { $user = JUser::getInstance($id); + if (!$user->id) { - $this->plugin->setResponse($this->getErrorResponse(JText::_( 'PLG_API_USERS_USER_NOT_FOUND_MESSAGE' ))); + $this->plugin->setResponse($this->getErrorResponse(JText::_('PLG_API_USERS_USER_NOT_FOUND_MESSAGE'))); return; } + + $user = $this->getUserCustomFieldData($user); $this->plugin->setResponse($user); } else @@ -373,6 +384,7 @@ public function get() foreach ($users as $k => $v) { + $this->getUserCustomFieldData($v); unset($users[$k]->password); } @@ -380,6 +392,33 @@ public function get() } } + /** + * This function return the user respective custom field data + * + * @param String $user User + * + * @return Object user + */ + public function getUserCustomFieldData($user) + { + JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); + + $userFields = FieldsHelper::getFields('com_users.user', $user, true); + $userCustomFields = array(); + + if (!empty($userFields)) + { + foreach ($userFields as $field) + { + $userCustomFields[$field->name] = $field->rawvalue; + } + + $user->customField = $userCustomFields; + } + + return $user; + } + /** * Function create easysocial profile. * @@ -389,7 +428,7 @@ public function get() */ public function createEsprofile($log_user) { - $obj = new stdClass(); + $obj = new stdClass; if (JComponentHelper::isEnabled('com_easysocial', true)) { @@ -397,7 +436,7 @@ public function createEsprofile($log_user) $epost = $app->input->get('fields', '', 'ARRAY'); - require_once JPATH_ADMINISTRATOR.'/components/com_easysocial/includes/foundry.php'; + require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/includes/foundry.php'; // Get all published fields apps that are available in the current form to perform validations $fieldsModel = FD::model('Fields'); @@ -406,11 +445,17 @@ public function createEsprofile($log_user) $my = FD::user($log_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 ); + $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); - $epost = $this->create_field_arr($fields,$epost); + $epost = $this->create_field_arr($fields, $epost); // Load json library. $json = FD::json(); @@ -422,12 +467,13 @@ public function createEsprofile($log_user) $disallowed = array( FD::token() , 'option' , 'task' , 'controller' ); // Process $_POST vars - foreach ($epost as $key => $value) { - - if (!in_array($key, $disallowed)) { - - if (is_array($value) && $key != 'es-fields-11') { - $value = $json->encode( $value ); + foreach ($epost as $key => $value) + { + if (!in_array($key, $disallowed)) + { + if (is_array($value) && $key != 'es-fields-11') + { + $value = $json->encode($value); } $registry->set($key, $value); @@ -449,7 +495,7 @@ public function createEsprofile($log_user) // Ensure that there is no errors. // @trigger onEditValidate - //$errors = $fieldsLib->trigger( 'onEditValidate' , SOCIAL_FIELDS_GROUP_USER , $fields , $args, array( $handler, 'validate' ) ); + /*$errors = $fieldsLib->trigger('onEditValidate', SOCIAL_FIELDS_GROUP_USER, $fields, $args, array($handler, 'validate'));*/ // Bind the my object with appropriate data. $my->bind($data); @@ -461,7 +507,7 @@ public function createEsprofile($log_user) $args = array(&$data, &$my); // @trigger onEditAfterSave - $fieldsLib->trigger( 'onRegisterAfterSave' , SOCIAL_FIELDS_GROUP_USER , $fields , $args ); + $fieldsLib->trigger('onRegisterAfterSave', SOCIAL_FIELDS_GROUP_USER, $fields, $args); // Bind custom fields for the user. $my->bindCustomFields($data); @@ -470,9 +516,9 @@ public function createEsprofile($log_user) $args = array(&$data, &$my); // @trigger onEditAfterSaveFields - $fieldsLib->trigger( 'onEditAfterSaveFields' , SOCIAL_FIELDS_GROUP_USER , $fields , $args ); + $fieldsLib->trigger('onEditAfterSaveFields', SOCIAL_FIELDS_GROUP_USER, $fields, $args); - if($sval) + if ($sval) { $obj->success = 1; $obj->message = JText::_('PLG_API_USERS_PROFILE_CREATED_SUCCESSFULLY_MESSAGE'); @@ -480,18 +526,16 @@ public function createEsprofile($log_user) else { $obj->success = 0; - $obj->message = JText::_( 'PLG_API_USERS_UNABLE_CREATE_PROFILE_MESSAGE' ); + $obj->message = JText::_('PLG_API_USERS_UNABLE_CREATE_PROFILE_MESSAGE'); } - } else { $obj->success = 0; - $obj->message = JText::_( 'PLG_API_USERS_EASYSOCIAL_NOT_INSTALL_MESSAGE'); + $obj->message = JText::_('PLG_API_USERS_EASYSOCIAL_NOT_INSTALL_MESSAGE'); } return $obj; - } /** @@ -506,55 +550,58 @@ public function create_field_arr($fields,$post) $fld_data = array(); $app = JFactory::getApplication(); - require_once JPATH_SITE.'/plugins/api/easysocial/libraries/uploadHelper.php'; - //for upload photo - if(!empty($_FILES['avatar']['name'])) - { - $upload_obj = new EasySocialApiUploadHelper(); + require_once JPATH_SITE . '/plugins/api/easysocial/libraries/uploadHelper.php'; - $phto_obj = $upload_obj->ajax_avatar($_FILES['avatar']); - $avtar_pth = $phto_obj['temp_path']; - $avtar_scr = $phto_obj['temp_uri']; - $avtar_typ = 'upload'; - $avatar_file_name = $_FILES['avatar']['name']; - } + // For upload photo + if (!empty($_FILES['avatar']['name'])) + { + $upload_obj = new EasySocialApiUploadHelper; - foreach($fields as $field) + $phto_obj = $upload_obj->ajax_avatar($_FILES['avatar']); + $avtar_pth = $phto_obj['temp_path']; + $avtar_scr = $phto_obj['temp_uri']; + $avtar_typ = 'upload'; + $avatar_file_name = $_FILES['avatar']['name']; + } + + foreach ($fields as $field) { - $fobj = new stdClass(); + $fobj = new stdClass; $fullname = $app->input->get('name', '', 'STRING'); $fld_data['first_name'] = $app->input->get('name', '', 'STRING'); - - $fobj->first = $fld_data['first_name']; $fobj->middle = ''; $fobj->last = ''; $fobj->name = $fullname; - switch($field->unique_key) + + switch ($field->unique_key) { - case 'HEADER': break; + case 'HEADER': + break; - case 'JOOMLA_FULLNAME': $fld_data['es-fields-'.$field->id] = $fobj; + case 'JOOMLA_FULLNAME': $fld_data['es-fields-' . $field->id] = $fobj; break; - case 'JOOMLA_USERNAME': $fld_data['es-fields-'.$field->id] = $app->input->get('username', '', 'STRING'); + case 'JOOMLA_USERNAME': $fld_data['es-fields-' . $field->id] = $app->input->get('username', '', 'STRING'); break; - case 'JOOMLA_PASSWORD': $fld_data['es-fields-'.$field->id] = $app->input->get('password', '', 'STRING'); + case 'JOOMLA_PASSWORD': $fld_data['es-fields-' . $field->id] = $app->input->get('password', '', 'STRING'); break; - case 'JOOMLA_EMAIL': $fld_data['es-fields-'.$field->id] = $app->input->get('email', '', 'STRING'); + case 'JOOMLA_EMAIL': $fld_data['es-fields-' . $field->id] = $app->input->get('email', '', 'STRING'); break; - case 'AVATAR': if(isset($avtar_scr)){ - $fld_data['es-fields-'.$field->id] = Array + + case 'AVATAR': if (isset($avtar_scr)){ + $fld_data['es-fields-' . $field->id] = Array ( - 'source' =>$avtar_scr, - 'path' =>$avtar_pth, + 'source' => $avtar_scr, + 'path' => $avtar_pth, 'data' => '', 'type' => $avtar_typ, 'name' => $avatar_file_name ); - } + } break; } } + return $fld_data; } @@ -606,7 +653,6 @@ public function sendRegisterEmail($base_dt) $base_dt['password'] ); } - } elseif ($data['activation'] == 1) { @@ -636,7 +682,7 @@ public function sendRegisterEmail($base_dt) } // Send the registration email. $return = JFactory::getMailer()->sendMail($data['mailfrom'], $data['fromname'], $base_dt['email'], $emailSubject, $emailBody); - return $return; + return $return; } }