PageRenderTime 61ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 69 lines | 26 code | 8 blank | 35 comment | 2 complexity | 108defcd426d1b7265fe56028927643c MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  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 object &$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. * @throws JException on invalid rule.
  34. */
  35. public function test(&$element, $value, $group = null, &$input = null, &$form = null)
  36. {
  37. // Get the database object and a new query object.
  38. $db = JFactory::getDBO();
  39. $query = $db->getQuery(true);
  40. // Build the query.
  41. $query->select('COUNT(*)');
  42. $query->from('#__users');
  43. $query->where('username = ' . $db->quote($value));
  44. // Get the extra field check attribute.
  45. $userId = ($form instanceof JForm) ? $form->getValue('id') : '';
  46. $query->where($db->quoteName('id') . ' <> ' . (int) $userId);
  47. // Set and query the database.
  48. $db->setQuery($query);
  49. $duplicate = (bool) $db->loadResult();
  50. // Check for a database error.
  51. if ($db->getErrorNum())
  52. {
  53. JError::raiseWarning(500, $db->getErrorMsg());
  54. }
  55. if ($duplicate)
  56. {
  57. return false;
  58. }
  59. return true;
  60. }
  61. }