PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/form/rules/username.php

https://bitbucket.org/eternaware/joomus
PHP | 62 lines | 22 code | 7 blank | 33 comment | 1 complexity | 69b4c37a0ed7b20f812c3f59c2410c08 MD5 | raw file
Possible License(s): LGPL-2.1
  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. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Form Rule class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Form
  15. * @since 11.1
  16. */
  17. class JFormRuleUsername extends JFormRule
  18. {
  19. /**
  20. * Method to test the username for uniqueness.
  21. *
  22. * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
  23. * @param mixed $value The form field value to validate.
  24. * @param string $group The field name group control value. This acts as as an array container for the field.
  25. * For example if the field has name="foo" and the group value is set to "bar" then the
  26. * full field name would end up being "bar[foo]".
  27. * @param JRegistry $input An optional JRegistry object with the entire data set to validate against the entire form.
  28. * @param JForm $form The form object for which the field is being tested.
  29. *
  30. * @return boolean True if the value is valid, false otherwise.
  31. *
  32. * @since 11.1
  33. */
  34. public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
  35. {
  36. // Get the database object and a new query object.
  37. $db = JFactory::getDBO();
  38. $query = $db->getQuery(true);
  39. // Build the query.
  40. $query->select('COUNT(*)');
  41. $query->from('#__users');
  42. $query->where('username = ' . $db->quote($value));
  43. // Get the extra field check attribute.
  44. $userId = ($form instanceof JForm) ? $form->getValue('id') : '';
  45. $query->where($db->quoteName('id') . ' <> ' . (int) $userId);
  46. // Set and query the database.
  47. $db->setQuery($query);
  48. $duplicate = (bool) $db->loadResult();
  49. if ($duplicate)
  50. {
  51. return false;
  52. }
  53. return true;
  54. }
  55. }