[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/form/rules/ -> username.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Form
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  /**
  13   * Form Rule class for the Joomla Platform.
  14   *
  15   * @package     Joomla.Platform
  16   * @subpackage  Form
  17   * @since       11.1
  18   */
  19  class JFormRuleUsername extends JFormRule
  20  {
  21      /**
  22       * Method to test the username for uniqueness.
  23       *
  24       * @param   JXMLElement  &$element  The JXMLElement object representing the <field /> tag for the form field object.
  25       * @param   mixed        $value     The form field value to validate.
  26       * @param   string       $group     The field name group control value. This acts as as an array container for the field.
  27       *                                  For example if the field has name="foo" and the group value is set to "bar" then the
  28       *                                  full field name would end up being "bar[foo]".
  29       * @param   JRegistry    &$input    An optional JRegistry object with the entire data set to validate against the entire form.
  30       * @param   object       &$form     The form object for which the field is being tested.
  31       *
  32       * @return  boolean  True if the value is valid, false otherwise.
  33       *
  34       * @since   11.1
  35       * @throws  JException on invalid rule.
  36       */
  37  	public function test(&$element, $value, $group = null, &$input = null, &$form = null)
  38      {
  39          // Get the database object and a new query object.
  40          $db = JFactory::getDBO();
  41          $query = $db->getQuery(true);
  42  
  43          // Build the query.
  44          $query->select('COUNT(*)');
  45          $query->from('#__users');
  46          $query->where('username = ' . $db->quote($value));
  47  
  48          // Get the extra field check attribute.
  49          $userId = ($form instanceof JForm) ? $form->getValue('id') : '';
  50          $query->where($db->quoteName('id') . ' <> ' . (int) $userId);
  51  
  52          // Set and query the database.
  53          $db->setQuery($query);
  54          $duplicate = (bool) $db->loadResult();
  55  
  56          // Check for a database error.
  57          if ($db->getErrorNum())
  58          {
  59              JError::raiseWarning(500, $db->getErrorMsg());
  60          }
  61  
  62          if ($duplicate)
  63          {
  64              return false;
  65          }
  66  
  67          return true;
  68      }
  69  }


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