[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/plugins/user/profile/ -> profile.php (source)

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  defined('JPATH_BASE') or die;
   8  
   9  jimport('joomla.utilities.date');
  10  
  11  /**
  12   * An example custom profile plugin.
  13   *
  14   * @package        Joomla.Plugin
  15   * @subpackage    User.profile
  16   * @version        1.6
  17   */
  18  class plgUserProfile extends JPlugin
  19  {
  20      /**
  21       * Constructor
  22       *
  23       * @access      protected
  24       * @param       object  $subject The object to observe
  25       * @param       array   $config  An array that holds the plugin configuration
  26       * @since       1.5
  27       */
  28  	public function __construct(& $subject, $config)
  29      {
  30          parent::__construct($subject, $config);
  31          $this->loadLanguage();
  32      }
  33  
  34      /**
  35       * @param    string    $context    The context for the data
  36       * @param    int        $data        The user id
  37       * @param    object
  38       *
  39       * @return    boolean
  40       * @since    1.6
  41       */
  42  	function onContentPrepareData($context, $data)
  43      {
  44          // Check we are manipulating a valid form.
  45          if (!in_array($context, array('com_users.profile', 'com_users.user', 'com_users.registration', 'com_admin.profile'))) {
  46              return true;
  47          }
  48  
  49          if (is_object($data))
  50          {
  51              $userId = isset($data->id) ? $data->id : 0;
  52  
  53              if (!isset($data->profile) and $userId > 0) {
  54  
  55                  // Load the profile data from the database.
  56                  $db = JFactory::getDbo();
  57                  $db->setQuery(
  58                      'SELECT profile_key, profile_value FROM #__user_profiles' .
  59                      ' WHERE user_id = '.(int) $userId." AND profile_key LIKE 'profile.%'" .
  60                      ' ORDER BY ordering'
  61                  );
  62                  $results = $db->loadRowList();
  63  
  64                  // Check for a database error.
  65                  if ($db->getErrorNum())
  66                  {
  67                      $this->_subject->setError($db->getErrorMsg());
  68                      return false;
  69                  }
  70  
  71                  // Merge the profile data.
  72                  $data->profile = array();
  73  
  74                  foreach ($results as $v)
  75                  {
  76                      $k = str_replace('profile.', '', $v[0]);
  77                      $data->profile[$k] = json_decode($v[1], true);
  78                      if ($data->profile[$k] === null)
  79                      {
  80                          $data->profile[$k] = $v[1];
  81                      }
  82                  }
  83              }
  84  
  85              if (!JHtml::isRegistered('users.url')) {
  86                  JHtml::register('users.url', array(__CLASS__, 'url'));
  87              }
  88              if (!JHtml::isRegistered('users.calendar')) {
  89                  JHtml::register('users.calendar', array(__CLASS__, 'calendar'));
  90              }
  91              if (!JHtml::isRegistered('users.tos')) {
  92                  JHtml::register('users.tos', array(__CLASS__, 'tos'));
  93              }
  94          }
  95  
  96          return true;
  97      }
  98  
  99  	public static function url($value)
 100      {
 101          if (empty($value))
 102          {
 103              return JHtml::_('users.value', $value);
 104          }
 105          else
 106          {
 107              $value = htmlspecialchars($value);
 108              if(substr ($value, 0, 4) == "http") {
 109                  return '<a href="'.$value.'">'.$value.'</a>';
 110              }
 111              else {
 112                  return '<a href="http://'.$value.'">'.$value.'</a>';
 113              }
 114          }
 115      }
 116  
 117  	public static function calendar($value)
 118      {
 119          if (empty($value)) {
 120              return JHtml::_('users.value', $value);
 121          } else {
 122              return JHtml::_('date', $value, null, null);
 123          }
 124      }
 125  
 126  	public static function tos($value)
 127      {
 128          if ($value) {
 129              return JText::_('JYES');
 130          }
 131          else {
 132              return JText::_('JNO');
 133          }
 134      }
 135  
 136      /**
 137       * @param    JForm    $form    The form to be altered.
 138       * @param    array    $data    The associated data for the form.
 139       *
 140       * @return    boolean
 141       * @since    1.6
 142       */
 143  	function onContentPrepareForm($form, $data)
 144      {
 145  
 146          if (!($form instanceof JForm))
 147          {
 148              $this->_subject->setError('JERROR_NOT_A_FORM');
 149              return false;
 150          }
 151  
 152          // Check we are manipulating a valid form.
 153          $name = $form->getName();
 154          if (!in_array($name, array('com_admin.profile', 'com_users.user', 'com_users.profile', 'com_users.registration'))) {
 155              return true;
 156          }
 157  
 158          // Add the registration fields to the form.
 159          JForm::addFormPath(dirname(__FILE__).'/profiles');
 160          $form->loadFile('profile', false);
 161  
 162          $fields = array(
 163              'address1',
 164              'address2',
 165              'city',
 166              'region',
 167              'country',
 168              'postal_code',
 169              'phone',
 170              'website',
 171              'favoritebook',
 172              'aboutme',
 173              'tos',
 174              'dob',
 175          );
 176  
 177          foreach ($fields as $field) {
 178              // Case using the users manager in admin
 179              if ($name == 'com_users.user') {
 180                  // Remove the field if it is disabled in registration and profile
 181                  if ($this->params->get('register-require_' . $field, 1) == 0 &&
 182                      $this->params->get('profile-require_' . $field, 1) == 0) {
 183                      $form->removeField($field, 'profile');
 184                  }
 185              }
 186              // Case registration
 187              elseif ($name == 'com_users.registration') {
 188                  // Toggle whether the field is required.
 189                  if ($this->params->get('register-require_' . $field, 1) > 0) {
 190                      $form->setFieldAttribute($field, 'required', ($this->params->get('register-require_' . $field) == 2) ? 'required' : '', 'profile');
 191                  }
 192                  else {
 193                      $form->removeField($field, 'profile');
 194                  }
 195              }
 196              // Case profile in site or admin
 197              elseif ($name == 'com_users.profile' || $name == 'com_admin.profile') {
 198                  // Toggle whether the field is required.
 199                  if ($this->params->get('profile-require_' . $field, 1) > 0) {
 200                      $form->setFieldAttribute($field, 'required', ($this->params->get('profile-require_' . $field) == 2) ? 'required' : '', 'profile');
 201                  }
 202                  else {
 203                      $form->removeField($field, 'profile');
 204                  }
 205              }
 206          }
 207  
 208          return true;
 209      }
 210  
 211  	function onUserAfterSave($data, $isNew, $result, $error)
 212      {
 213          $userId    = JArrayHelper::getValue($data, 'id', 0, 'int');
 214  
 215          if ($userId && $result && isset($data['profile']) && (count($data['profile'])))
 216          {
 217              try
 218              {
 219                  //Sanitize the date
 220                  if (!empty($data['profile']['dob'])) {
 221                      $date = new JDate($data['profile']['dob']);
 222                      $data['profile']['dob'] = $date->format('Y-m-d');
 223                  }
 224  
 225                  $db = JFactory::getDbo();
 226                  $db->setQuery(
 227                      'DELETE FROM #__user_profiles WHERE user_id = '.$userId .
 228                      " AND profile_key LIKE 'profile.%'"
 229                  );
 230  
 231                  if (!$db->query()) {
 232                      throw new Exception($db->getErrorMsg());
 233                  }
 234  
 235                  $tuples = array();
 236                  $order    = 1;
 237  
 238                  foreach ($data['profile'] as $k => $v)
 239                  {
 240                      $tuples[] = '('.$userId.', '.$db->quote('profile.'.$k).', '.$db->quote(json_encode($v)).', '.$order++.')';
 241                  }
 242  
 243                  $db->setQuery('INSERT INTO #__user_profiles VALUES '.implode(', ', $tuples));
 244  
 245                  if (!$db->query()) {
 246                      throw new Exception($db->getErrorMsg());
 247                  }
 248  
 249              }
 250              catch (JException $e)
 251              {
 252                  $this->_subject->setError($e->getMessage());
 253                  return false;
 254              }
 255          }
 256  
 257          return true;
 258      }
 259  
 260      /**
 261       * Remove all user profile information for the given user ID
 262       *
 263       * Method is called after user data is deleted from the database
 264       *
 265       * @param    array        $user        Holds the user data
 266       * @param    boolean        $success    True if user was succesfully stored in the database
 267       * @param    string        $msg        Message
 268       */
 269  	function onUserAfterDelete($user, $success, $msg)
 270      {
 271          if (!$success) {
 272              return false;
 273          }
 274  
 275          $userId    = JArrayHelper::getValue($user, 'id', 0, 'int');
 276  
 277          if ($userId)
 278          {
 279              try
 280              {
 281                  $db = JFactory::getDbo();
 282                  $db->setQuery(
 283                      'DELETE FROM #__user_profiles WHERE user_id = '.$userId .
 284                      " AND profile_key LIKE 'profile.%'"
 285                  );
 286  
 287                  if (!$db->query()) {
 288                      throw new Exception($db->getErrorMsg());
 289                  }
 290              }
 291              catch (JException $e)
 292              {
 293                  $this->_subject->setError($e->getMessage());
 294                  return false;
 295              }
 296          }
 297  
 298          return true;
 299      }
 300  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1