id) ? $data->id : 0; if (!isset($data->profile) and $userId > 0) { // Load the profile data from the database. $db = JFactory::getDbo(); $db->setQuery( 'SELECT profile_key, profile_value FROM #__user_profiles' . ' WHERE user_id = ' . (int) $userId . " AND profile_key LIKE 'profile.%'" . ' ORDER BY ordering' ); try { $results = $db->loadRowList(); } catch (RuntimeException $e) { $this->_subject->setError($e->getMessage()); return false; } $db->clear(); //echo $db; // NEED-To-Do /* Query database ggeek_hikashop_user and return the hikashop user_id Query database and get address from ggeek_hikashop_address where id matches this new item */ // Load the users hikashop user_id to match the the next query with the hikashop address // Get a db connection. $hikashop_user_id = JFactory::getDbo(); // Create a new query object. $hikashop_query = $hikashop_user_id->getQuery(true); $hikashop_query->select($hikashop_user_id->quoteName(array('user_id', 'user_cms_id'))); $hikashop_query->from($hikashop_user_id->quoteName('#__hikashop_user')); $hikashop_query->where($hikashop_user_id->quoteName('user_cms_id') . ' LIKE ' . $userId); $hikashop_user_id->setQuery($hikashop_query); try { $hikashop_user_id_results = $hikashop_user_id->loadObject(); } catch (RuntimeException $e) { $this->_subject->setError($e->getMessage()); return false; } //print_r($hikashop_user_id); //echo $hikashop_user_id['user_id']; //$hikashop_id = $hikashop_user_id_results['user_id']; $hikashop_user_id->clear(); //echo $hikashop_id; //$hikashop_user_id->setQuery('SELECT user_id, user_cms_id FROM #__hikashop_user WHERE user_cms_id = ' . (int) $userId); // Set $hikashop_id as the id we matched up in the hikashop_user_id query // Load the users hikashop address that matches the correct id //$hikashop_db = JFactory::getDbo(); //$hikashop_db->setQuery('SELECT address_id, address_user_id FROM #__hikashop_address WHERE address_user_id =' . (int) $hikashop_id); // Load the users hikashop address that matches the correct id //$hikashop_db = JFactory::getDbo(); //$hikashop_db_query = $hikashop_db->getQuery(true); //$hikashop_db_query->select($hikashop_db->quoteName(array('address_id', 'address_user_id'))); //$hikashop_db_query->from($hikashop_db->quoteName('#__hikashop_address')); //$hikashop_db_query->where($hikashop_db->quoteName('address_user_id') . ' LIKE ' . $hikashop_id); //$hikashop_db->setQuery($hikashop_db_query); //try { // $hikashop_db_results = $hikashop_db->loadObject(); } //catch (RuntimeException $e) { // $this->_subject->setError($e->getMessage()); // return false; } //$hikashop_db->clear(); // Merge the profile data. $data->profile = array(); foreach ($results as $v) { $k = str_replace('profile.', '', $v[0]); $data->profile[$k] = json_decode($v[1], true); if ($data->profile[$k] === null) { $data->profile[$k] = $v[1]; } } // Merge the profile data. //$data->hikashop_user_data = array(); //$data->profile = array(); //$data->hikashop_user_data['username'] = 'hello'; //foreach ($hikashop_db_results as $hikashop_user) { // $data->hikashop_user_data = $hikashop_user; //$data->profile = $hikashop_user; } } if (!JHtml::isRegistered('users.url')) { JHtml::register('users.url', array(__CLASS__, 'url')); } if (!JHtml::isRegistered('users.calendar')) { JHtml::register('users.calendar', array(__CLASS__, 'calendar')); } if (!JHtml::isRegistered('users.tos')) { JHtml::register('users.tos', array(__CLASS__, 'tos')); } } return true; } public static function url($value) { if (empty($value)) { return JHtml::_('users.value', $value); } else { // Convert website url to utf8 for display $value = JStringPunycode::urlToUTF8(htmlspecialchars($value)); if (substr($value, 0, 4) == "http") { return '' . $value . ''; } else { return '' . $value . ''; } } } public static function calendar($value) { if (empty($value)) { return JHtml::_('users.value', $value); } else { return JHtml::_('date', $value, null, null); } } public static function tos($value) { if ($value) { return JText::_('JYES'); } else { return JText::_('JNO'); } } /** * @param JForm $form The form to be altered. * @param array $data The associated data for the form. * * @return boolean * @since 1.6 */ public function onContentPrepareForm($form, $data) { if (!($form instanceof JForm)) { $this->_subject->setError('JERROR_NOT_A_FORM'); return false; } // Check we are manipulating a valid form. $name = $form->getName(); if (!in_array($name, array('com_admin.profile', 'com_users.user', 'com_users.profile', 'com_usermod.profile', 'com_usermod.notlogged', 'com_users.registration'))) { return true; } // Add the registration fields to the form. JForm::addFormPath(__DIR__ . '/profiles'); $form->loadFile('profile', false); // shows all the plugin fields (extra fields chosen in the backend of usermod_profiles plugin $fields = array( 'address1', 'address_street', 'address2', 'city', 'region', 'country', 'postal_code', 'phone', 'website', 'favoritebook', 'aboutme', 'dob', 'tos', ); //Change fields description when displayed in front-end $app = JFactory::getApplication(); if ($app->isSite()) { $form->setFieldAttribute('address1', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('address_street', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('address2', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('city', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('region', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('country', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('postal_code', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('phone', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('website', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('favoritebook', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('aboutme', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('dob', 'description', 'PLG_USER_USERMODPROFILE_FILL_FIELD_DESC_SITE', 'profile'); $form->setFieldAttribute('tos', 'description', 'PLG_USER_USERMODPROFILE_FIELD_TOS_DESC_SITE', 'profile'); } $tosarticle = $this->params->get('register_tos_article'); $tosenabled = $this->params->get('register-require_tos', 0); // We need to be in the registration form, field needs to be enabled and we need an article ID if ($name != 'com_users.registration' || $name != 'com_usermod.notlogged' || !$tosenabled || !$tosarticle) { // We only want the TOS in the registration form $form->removeField('tos', 'profile'); } else { // Push the TOS article ID into the TOS field. $form->setFieldAttribute('tos', 'article', $tosarticle, 'profile'); } foreach ($fields as $field) { // Case using the users manager in admin if ($name == 'com_users.user' || $name == 'com_usermod.usermod') { // Remove the field if it is disabled in registration and profile if ($this->params->get('register-require_' . $field, 1) == 0 && $this->params->get('profile-require_' . $field, 1) == 0 ) { $form->removeField($field, 'profile'); } } // Case registration elseif ($name == 'com_users.registration' || $name == 'com_usermod.notlogged') { // Toggle whether the field is required. if ($this->params->get('register-require_' . $field, 1) > 0) { $form->setFieldAttribute($field, 'required', ($this->params->get('register-require_' . $field) == 2) ? 'required' : '', 'profile'); } else { $form->removeField($field, 'profile'); } if ($this->params->get('register-require_dob', 1) > 0) { $form->setFieldAttribute('spacer', 'type', 'spacer', 'profile'); } } // Case profile in site or admin elseif ($name == 'com_user.profile' || $name == 'com_admin.profile' || $name == 'com_usermod.profile') { // Toggle whether the field is required. if ($this->params->get('profile-require_' . $field, 1) > 0) { $form->setFieldAttribute($field, 'required', ($this->params->get('profile-require_' . $field) == 2) ? 'required' : '', 'profile'); } else { $form->removeField($field, 'profile'); // controls the hiding of fields (ones set in the backend of the usermod_profile plugin to disabled } if ($this->params->get('profile-require_dob', 1) > 0) { $form->setFieldAttribute('spacer', 'type', 'spacer', 'profile'); } } } return true; } /** * Method is called before user data is stored in the database * * @param array $user Holds the old user data. * @param boolean $isnew True if a new user is stored. * @param array $data Holds the new user data. * * @return boolean * * @since 3.1 * @throws InvalidArgumentException on invalid date. */ public function onUserBeforeSave($user, $isnew, $data) { // Check that the date is valid. if (!empty($data['profile']['dob'])) { try { // Convert website url to punycode $data['profile']['website'] = JStringPunycode::urlToPunycode($data['profile']['website']); $date = new JDate($data['profile']['dob']); $this->_date = $date->format('Y-m-d'); } catch (Exception $e) { // Throw an exception if date is not valid. throw new InvalidArgumentException(JText::_('PLG_USER_USERMODPROFILE_ERROR_INVALID_DOB')); } } return true; } public function onUserAfterSave($data, $isNew, $result, $error) { $userId = JArrayHelper::getValue($data, 'id', 0, 'int'); if ($userId && $result && isset($data['profile']) && (count($data['profile']))) { try { // Sanitize the date $data['profile']['dob'] = $this->_date; $db = JFactory::getDbo(); $query = $db->getQuery(true) ->delete($db->quoteName('#__user_profiles')) ->where($db->quoteName('user_id') . ' = ' . (int) $userId) ->where($db->quoteName('profile_key') . ' LIKE ' . $db->quote('profile.%')); $db->setQuery($query); $db->execute(); $tuples = array(); $order = 1; foreach ($data['profile'] as $k => $v) { $tuples[] = '(' . $userId . ', ' . $db->quote('profile.' . $k) . ', ' . $db->quote(json_encode($v)) . ', ' . $order++ . ')'; } $db->setQuery('INSERT INTO #__user_profiles VALUES ' . implode(', ', $tuples)); $db->execute(); } catch (RuntimeException $e) { $this->_subject->setError($e->getMessage()); return false; } } return true; } /** * Remove all user profile information for the given user ID * * Method is called after user data is deleted from the database * * @param array $user Holds the user data * @param boolean $success True if user was succesfully stored in the database * @param string $msg Message * * @return boolean */ public function onUserAfterDelete($user, $success, $msg) { if (!$success) { return false; } $userId = JArrayHelper::getValue($user, 'id', 0, 'int'); if ($userId) { try { $db = JFactory::getDbo(); $db->setQuery( 'DELETE FROM #__user_profiles WHERE user_id = ' . $userId . " AND profile_key LIKE 'profile.%'" ); $db->execute(); } catch (Exception $e) { $this->_subject->setError($e->getMessage()); return false; } } return true; } }