Source for file profile.php

Documentation is available at profile.php

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

Documentation generated on Tue, 19 Nov 2013 15:11:20 +0100 by phpDocumentor 1.4.3