[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Site 5 * @subpackage com_users 6 * 7 * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Component\Users\Site\Controller; 12 13 use Joomla\CMS\Language\Text; 14 use Joomla\CMS\MVC\Controller\BaseController; 15 use Joomla\CMS\Router\Route; 16 use Joomla\CMS\Uri\Uri; 17 18 // phpcs:disable PSR1.Files.SideEffects 19 \defined('_JEXEC') or die; 20 // phpcs:enable PSR1.Files.SideEffects 21 22 /** 23 * Profile controller class for Users. 24 * 25 * @since 1.6 26 */ 27 class ProfileController extends BaseController 28 { 29 /** 30 * Method to check out a user for editing and redirect to the edit form. 31 * 32 * @return boolean 33 * 34 * @since 1.6 35 */ 36 public function edit() 37 { 38 $app = $this->app; 39 $user = $this->app->getIdentity(); 40 $loginUserId = (int) $user->get('id'); 41 42 // Get the current user id. 43 $userId = $this->input->getInt('user_id'); 44 45 // Check if the user is trying to edit another users profile. 46 if ($userId != $loginUserId) { 47 $app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error'); 48 $app->setHeader('status', 403, true); 49 50 return false; 51 } 52 53 $cookieLogin = $user->get('cookieLogin'); 54 55 // Check if the user logged in with a cookie 56 if (!empty($cookieLogin)) { 57 // If so, the user must login to edit the password and other data. 58 $app->enqueueMessage(Text::_('JGLOBAL_REMEMBER_MUST_LOGIN'), 'message'); 59 $this->setRedirect(Route::_('index.php?option=com_users&view=login', false)); 60 61 return false; 62 } 63 64 // Set the user id for the user to edit in the session. 65 $app->setUserState('com_users.edit.profile.id', $userId); 66 67 // Redirect to the edit screen. 68 $this->setRedirect(Route::_('index.php?option=com_users&view=profile&layout=edit', false)); 69 70 return true; 71 } 72 73 /** 74 * Method to save a user's profile data. 75 * 76 * @return void|boolean 77 * 78 * @since 1.6 79 * @throws \Exception 80 */ 81 public function save() 82 { 83 // Check for request forgeries. 84 $this->checkToken(); 85 86 $app = $this->app; 87 88 /** @var \Joomla\Component\Users\Site\Model\ProfileModel $model */ 89 $model = $this->getModel('Profile', 'Site'); 90 $user = $this->app->getIdentity(); 91 $userId = (int) $user->get('id'); 92 93 // Get the user data. 94 $requestData = $app->input->post->get('jform', array(), 'array'); 95 96 // Force the ID to this user. 97 $requestData['id'] = $userId; 98 99 // Validate the posted data. 100 $form = $model->getForm(); 101 102 if (!$form) { 103 throw new \Exception($model->getError(), 500); 104 } 105 106 // Send an object which can be modified through the plugin event 107 $objData = (object) $requestData; 108 $app->triggerEvent( 109 'onContentNormaliseRequestData', 110 array('com_users.user', $objData, $form) 111 ); 112 $requestData = (array) $objData; 113 114 // Validate the posted data. 115 $data = $model->validate($form, $requestData); 116 117 // Check for errors. 118 if ($data === false) { 119 // Get the validation messages. 120 $errors = $model->getErrors(); 121 122 // Push up to three validation messages out to the user. 123 for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) { 124 if ($errors[$i] instanceof \Exception) { 125 $app->enqueueMessage($errors[$i]->getMessage(), 'warning'); 126 } else { 127 $app->enqueueMessage($errors[$i], 'warning'); 128 } 129 } 130 131 // Unset the passwords. 132 unset($requestData['password1'], $requestData['password2']); 133 134 // Save the data in the session. 135 $app->setUserState('com_users.edit.profile.data', $requestData); 136 137 // Redirect back to the edit screen. 138 $userId = (int) $app->getUserState('com_users.edit.profile.id'); 139 $this->setRedirect(Route::_('index.php?option=com_users&view=profile&layout=edit&user_id=' . $userId, false)); 140 141 return false; 142 } 143 144 // Attempt to save the data. 145 $return = $model->save($data); 146 147 // Check for errors. 148 if ($return === false) { 149 // Save the data in the session. 150 $app->setUserState('com_users.edit.profile.data', $data); 151 152 // Redirect back to the edit screen. 153 $userId = (int) $app->getUserState('com_users.edit.profile.id'); 154 $this->setMessage(Text::sprintf('COM_USERS_PROFILE_SAVE_FAILED', $model->getError()), 'warning'); 155 $this->setRedirect(Route::_('index.php?option=com_users&view=profile&layout=edit&user_id=' . $userId, false)); 156 157 return false; 158 } 159 160 // Redirect the user and adjust session state based on the chosen task. 161 switch ($this->getTask()) { 162 case 'apply': 163 // Check out the profile. 164 $app->setUserState('com_users.edit.profile.id', $return); 165 166 // Redirect back to the edit screen. 167 $this->setMessage(Text::_('COM_USERS_PROFILE_SAVE_SUCCESS')); 168 169 $redirect = $app->getUserState('com_users.edit.profile.redirect'); 170 171 // Don't redirect to an external URL. 172 if (!Uri::isInternal($redirect)) { 173 $redirect = null; 174 } 175 176 if (!$redirect) { 177 $redirect = 'index.php?option=com_users&view=profile&layout=edit&hidemainmenu=1'; 178 } 179 180 $this->setRedirect(Route::_($redirect, false)); 181 break; 182 183 default: 184 // Clear the profile id from the session. 185 $app->setUserState('com_users.edit.profile.id', null); 186 187 $redirect = $app->getUserState('com_users.edit.profile.redirect'); 188 189 // Don't redirect to an external URL. 190 if (!Uri::isInternal($redirect)) { 191 $redirect = null; 192 } 193 194 if (!$redirect) { 195 $redirect = 'index.php?option=com_users&view=profile&user_id=' . $return; 196 } 197 198 // Redirect to the list screen. 199 $this->setMessage(Text::_('COM_USERS_PROFILE_SAVE_SUCCESS')); 200 $this->setRedirect(Route::_($redirect, false)); 201 break; 202 } 203 204 // Flush the data from the session. 205 $app->setUserState('com_users.edit.profile.data', null); 206 } 207 208 /** 209 * Method to cancel an edit. 210 * 211 * @return void 212 * 213 * @since 4.0.0 214 */ 215 public function cancel() 216 { 217 // Check for request forgeries. 218 $this->checkToken(); 219 220 // Flush the data from the session. 221 $this->app->setUserState('com_users.edit.profile', null); 222 223 // Redirect to user profile. 224 $this->setRedirect(Route::_('index.php?option=com_users&view=profile', false)); 225 } 226 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |