PageRenderTime 88ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/user/profile/profile.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 407 lines | 277 code | 40 blank | 90 comment | 42 complexity | 76eab992258970a5c83046c67fefd633 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage User.profile
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('JPATH_BASE') or die;
  10. /**
  11. * An example custom profile plugin.
  12. *
  13. * @package Joomla.Plugin
  14. * @subpackage User.profile
  15. * @since 1.6
  16. */
  17. class PlgUserProfile extends JPlugin
  18. {
  19. /**
  20. * Date of birth.
  21. *
  22. * @var string
  23. * @since 3.1
  24. */
  25. private $_date = '';
  26. /**
  27. * Load the language file on instantiation.
  28. *
  29. * @var boolean
  30. * @since 3.1
  31. */
  32. protected $autoloadLanguage = true;
  33. /**
  34. * Constructor
  35. *
  36. * @param object $subject The object to observe
  37. * @param array $config An array that holds the plugin configuration
  38. *
  39. * @since 1.5
  40. */
  41. public function __construct(& $subject, $config)
  42. {
  43. parent::__construct($subject, $config);
  44. JFormHelper::addFieldPath(__DIR__ . '/fields');
  45. }
  46. /**
  47. * @param string $context The context for the data
  48. * @param integer $data The user id
  49. *
  50. * @return boolean
  51. *
  52. * @since 1.6
  53. */
  54. public function onContentPrepareData($context, $data)
  55. {
  56. // Check we are manipulating a valid form.
  57. if (!in_array($context, array('com_users.profile', 'com_users.user', 'com_users.registration', 'com_admin.profile')))
  58. {
  59. return true;
  60. }
  61. if (is_object($data))
  62. {
  63. $userId = isset($data->id) ? $data->id : 0;
  64. if (!isset($data->profile) and $userId > 0)
  65. {
  66. // Load the profile data from the database.
  67. $db = JFactory::getDbo();
  68. $db->setQuery(
  69. 'SELECT profile_key, profile_value FROM #__user_profiles' .
  70. ' WHERE user_id = ' . (int) $userId . " AND profile_key LIKE 'profile.%'" .
  71. ' ORDER BY ordering'
  72. );
  73. try
  74. {
  75. $results = $db->loadRowList();
  76. }
  77. catch (RuntimeException $e)
  78. {
  79. $this->_subject->setError($e->getMessage());
  80. return false;
  81. }
  82. // Merge the profile data.
  83. $data->profile = array();
  84. foreach ($results as $v)
  85. {
  86. $k = str_replace('profile.', '', $v[0]);
  87. $data->profile[$k] = json_decode($v[1], true);
  88. if ($data->profile[$k] === null)
  89. {
  90. $data->profile[$k] = $v[1];
  91. }
  92. }
  93. }
  94. if (!JHtml::isRegistered('users.url'))
  95. {
  96. JHtml::register('users.url', array(__CLASS__, 'url'));
  97. }
  98. if (!JHtml::isRegistered('users.calendar'))
  99. {
  100. JHtml::register('users.calendar', array(__CLASS__, 'calendar'));
  101. }
  102. if (!JHtml::isRegistered('users.tos'))
  103. {
  104. JHtml::register('users.tos', array(__CLASS__, 'tos'));
  105. }
  106. }
  107. return true;
  108. }
  109. public static function url($value)
  110. {
  111. if (empty($value))
  112. {
  113. return JHtml::_('users.value', $value);
  114. }
  115. else
  116. {
  117. $value = htmlspecialchars($value);
  118. if (substr($value, 0, 4) == "http")
  119. {
  120. return '<a href="' . $value . '">' . $value . '</a>';
  121. }
  122. else
  123. {
  124. return '<a href="http://' . $value . '">' . $value . '</a>';
  125. }
  126. }
  127. }
  128. public static function calendar($value)
  129. {
  130. if (empty($value))
  131. {
  132. return JHtml::_('users.value', $value);
  133. }
  134. else
  135. {
  136. return JHtml::_('date', $value, null, null);
  137. }
  138. }
  139. public static function tos($value)
  140. {
  141. if ($value)
  142. {
  143. return JText::_('JYES');
  144. }
  145. else
  146. {
  147. return JText::_('JNO');
  148. }
  149. }
  150. /**
  151. * @param JForm $form The form to be altered.
  152. * @param array $data The associated data for the form.
  153. *
  154. * @return boolean
  155. * @since 1.6
  156. */
  157. public function onContentPrepareForm($form, $data)
  158. {
  159. if (!($form instanceof JForm))
  160. {
  161. $this->_subject->setError('JERROR_NOT_A_FORM');
  162. return false;
  163. }
  164. // Check we are manipulating a valid form.
  165. $name = $form->getName();
  166. if (!in_array($name, array('com_admin.profile', 'com_users.user', 'com_users.profile', 'com_users.registration')))
  167. {
  168. return true;
  169. }
  170. // Add the registration fields to the form.
  171. JForm::addFormPath(__DIR__ . '/profiles');
  172. $form->loadFile('profile', false);
  173. $fields = array(
  174. 'address1',
  175. 'address2',
  176. 'city',
  177. 'region',
  178. 'country',
  179. 'postal_code',
  180. 'phone',
  181. 'website',
  182. 'favoritebook',
  183. 'aboutme',
  184. 'dob',
  185. 'tos',
  186. );
  187. //Change fields description when displayed in front-end
  188. $app = JFactory::getApplication();
  189. if ($app->isSite())
  190. {
  191. $form->setFieldAttribute('address1', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  192. $form->setFieldAttribute('address2', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  193. $form->setFieldAttribute('city', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  194. $form->setFieldAttribute('region', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  195. $form->setFieldAttribute('country', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  196. $form->setFieldAttribute('postal_code', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  197. $form->setFieldAttribute('phone', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  198. $form->setFieldAttribute('website', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  199. $form->setFieldAttribute('favoritebook', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  200. $form->setFieldAttribute('aboutme', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  201. $form->setFieldAttribute('dob', 'description', 'PLG_USER_PROFILE_FILL_FIELD_DESC_SITE', 'profile');
  202. $form->setFieldAttribute('tos', 'description', 'PLG_USER_PROFILE_FIELD_TOS_DESC_SITE', 'profile');
  203. }
  204. $tosarticle = $this->params->get('register_tos_article');
  205. $tosenabled = $this->params->get('register-require_tos', 0);
  206. // We need to be in the registration form, field needs to be enabled and we need an article ID
  207. if ($name != 'com_users.registration' || !$tosenabled || !$tosarticle)
  208. {
  209. // We only want the TOS in the registration form
  210. $form->removeField('tos', 'profile');
  211. }
  212. else
  213. {
  214. // Push the TOS article ID into the TOS field.
  215. $form->setFieldAttribute('tos', 'article', $tosarticle, 'profile');
  216. }
  217. foreach ($fields as $field)
  218. {
  219. // Case using the users manager in admin
  220. if ($name == 'com_users.user')
  221. {
  222. // Remove the field if it is disabled in registration and profile
  223. if ($this->params->get('register-require_' . $field, 1) == 0
  224. && $this->params->get('profile-require_' . $field, 1) == 0
  225. )
  226. {
  227. $form->removeField($field, 'profile');
  228. }
  229. }
  230. // Case registration
  231. elseif ($name == 'com_users.registration')
  232. {
  233. // Toggle whether the field is required.
  234. if ($this->params->get('register-require_' . $field, 1) > 0)
  235. {
  236. $form->setFieldAttribute($field, 'required', ($this->params->get('register-require_' . $field) == 2) ? 'required' : '', 'profile');
  237. }
  238. else
  239. {
  240. $form->removeField($field, 'profile');
  241. }
  242. if ($this->params->get('register-require_dob', 1) > 0)
  243. {
  244. $form->setFieldAttribute('spacer', 'type', 'spacer', 'profile');
  245. }
  246. }
  247. // Case profile in site or admin
  248. elseif ($name == 'com_users.profile' || $name == 'com_admin.profile')
  249. {
  250. // Toggle whether the field is required.
  251. if ($this->params->get('profile-require_' . $field, 1) > 0)
  252. {
  253. $form->setFieldAttribute($field, 'required', ($this->params->get('profile-require_' . $field) == 2) ? 'required' : '', 'profile');
  254. }
  255. else
  256. {
  257. $form->removeField($field, 'profile');
  258. }
  259. if ($this->params->get('profile-require_dob', 1) > 0)
  260. {
  261. $form->setFieldAttribute('spacer', 'type', 'spacer', 'profile');
  262. }
  263. }
  264. }
  265. return true;
  266. }
  267. /**
  268. * Method is called before user data is stored in the database
  269. *
  270. * @param array $user Holds the old user data.
  271. * @param boolean $isnew True if a new user is stored.
  272. * @param array $data Holds the new user data.
  273. *
  274. * @return boolean
  275. *
  276. * @since 3.1
  277. * @throws InvalidArgumentException on invalid date.
  278. */
  279. public function onUserBeforeSave($user, $isnew, $data)
  280. {
  281. // Check that the date is valid.
  282. if (!empty($data['profile']['dob']))
  283. {
  284. try
  285. {
  286. $date = new JDate($data['profile']['dob']);
  287. $this->_date = $date->format('Y-m-d');
  288. }
  289. catch (Exception $e)
  290. {
  291. // Throw an exception if date is not valid.
  292. throw new InvalidArgumentException(JText::_('PLG_USER_PROFILE_ERROR_INVALID_DOB'));
  293. }
  294. }
  295. return true;
  296. }
  297. public function onUserAfterSave($data, $isNew, $result, $error)
  298. {
  299. $userId = JArrayHelper::getValue($data, 'id', 0, 'int');
  300. if ($userId && $result && isset($data['profile']) && (count($data['profile'])))
  301. {
  302. try
  303. {
  304. // Sanitize the date
  305. $data['profile']['dob'] = $this->_date;
  306. $db = JFactory::getDbo();
  307. $query = $db->getQuery(true)
  308. ->delete($db->quoteName('#__user_profiles'))
  309. ->where($db->quoteName('userid') . ' = ' . (int) $userId)
  310. ->where($db->quoteName('profile_key') . ' LIKE ' . $db->quote('profile.%'));
  311. $db->setQuery($query);
  312. $db->execute();
  313. $tuples = array();
  314. $order = 1;
  315. foreach ($data['profile'] as $k => $v)
  316. {
  317. $tuples[] = '(' . $userId . ', ' . $db->quote('profile.' . $k) . ', ' . $db->quote(json_encode($v)) . ', ' . $order++ . ')';
  318. }
  319. $db->setQuery('INSERT INTO #__user_profiles VALUES ' . implode(', ', $tuples));
  320. $db->execute();
  321. }
  322. catch (RuntimeException $e)
  323. {
  324. $this->_subject->setError($e->getMessage());
  325. return false;
  326. }
  327. }
  328. return true;
  329. }
  330. /**
  331. * Remove all user profile information for the given user ID
  332. *
  333. * Method is called after user data is deleted from the database
  334. *
  335. * @param array $user Holds the user data
  336. * @param boolean $success True if user was succesfully stored in the database
  337. * @param string $msg Message
  338. *
  339. * @return boolean
  340. */
  341. public function onUserAfterDelete($user, $success, $msg)
  342. {
  343. if (!$success)
  344. {
  345. return false;
  346. }
  347. $userId = JArrayHelper::getValue($user, 'id', 0, 'int');
  348. if ($userId)
  349. {
  350. try
  351. {
  352. $db = JFactory::getDbo();
  353. $db->setQuery(
  354. 'DELETE FROM #__user_profiles WHERE user_id = ' . $userId .
  355. " AND profile_key LIKE 'profile.%'"
  356. );
  357. $db->execute();
  358. }
  359. catch (Exception $e)
  360. {
  361. $this->_subject->setError($e->getMessage());
  362. return false;
  363. }
  364. }
  365. return true;
  366. }
  367. }