/libraries/cms/form/field/user.php

https://bitbucket.org/eternaware/joomus · PHP · 128 lines · 63 code · 15 blank · 50 comment · 3 complexity · 6333a89ee5725e99a964a5d5f498e030 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Libraries
  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. * Field to select a user id from a modal list.
  12. *
  13. * @package Joomla.Libraries
  14. * @subpackage Form
  15. * @since 1.6.0
  16. */
  17. class JFormFieldUser extends JFormField
  18. {
  19. /**
  20. * The form field type.
  21. *
  22. * @var string
  23. * @since 1.6.0
  24. */
  25. public $type = 'User';
  26. /**
  27. * Method to get the user field input markup.
  28. *
  29. * @return string The field input markup.
  30. *
  31. * @since 1.6.0
  32. */
  33. protected function getInput()
  34. {
  35. $html = array();
  36. $groups = $this->getGroups();
  37. $excluded = $this->getExcluded();
  38. $link = 'index.php?option=com_users&amp;view=users&amp;layout=modal&amp;tmpl=component&amp;field=' . $this->id
  39. . (isset($groups) ? ('&amp;groups=' . base64_encode(json_encode($groups))) : '')
  40. . (isset($excluded) ? ('&amp;excluded=' . base64_encode(json_encode($excluded))) : '');
  41. // Initialize some field attributes.
  42. $attr = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
  43. $attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
  44. // Initialize JavaScript field attributes.
  45. $onchange = (string) $this->element['onchange'];
  46. // Load the modal behavior script.
  47. JHtml::_('behavior.modal', 'a.modal_' . $this->id);
  48. // Build the script.
  49. $script = array();
  50. $script[] = ' function jSelectUser_' . $this->id . '(id, title) {';
  51. $script[] = ' var old_id = document.getElementById("' . $this->id . '_id").value;';
  52. $script[] = ' if (old_id != id) {';
  53. $script[] = ' document.getElementById("' . $this->id . '_id").value = id;';
  54. $script[] = ' document.getElementById("' . $this->id . '_name").value = title;';
  55. $script[] = ' ' . $onchange;
  56. $script[] = ' }';
  57. $script[] = ' SqueezeBox.close();';
  58. $script[] = ' }';
  59. // Add the script to the document head.
  60. JFactory::getDocument()->addScriptDeclaration(implode("\n", $script));
  61. // Load the current username if available.
  62. $table = JTable::getInstance('user');
  63. if ($this->value)
  64. {
  65. $table->load($this->value);
  66. }
  67. else
  68. {
  69. $table->username = JText::_('JLIB_FORM_SELECT_USER');
  70. }
  71. // Create a dummy text field with the user name.
  72. $html[] = '<div class="fltlft">';
  73. $html[] = ' <input type="text" id="' . $this->id . '_name"' . ' value="' . htmlspecialchars($table->name, ENT_COMPAT, 'UTF-8') . '"'
  74. . ' disabled="disabled"' . $attr . ' />';
  75. $html[] = '</div>';
  76. // Create the user select button.
  77. $html[] = '<div class="button2-left">';
  78. $html[] = ' <div class="blank">';
  79. if ($this->element['readonly'] != 'true')
  80. {
  81. $html[] = ' <a class="modal_' . $this->id . '" title="' . JText::_('JLIB_FORM_CHANGE_USER') . '"' . ' href="' . $link . '"'
  82. . ' rel="{handler: \'iframe\', size: {x: 800, y: 500}}">';
  83. $html[] = ' ' . JText::_('JLIB_FORM_CHANGE_USER') . '</a>';
  84. }
  85. $html[] = ' </div>';
  86. $html[] = '</div>';
  87. // Create the real field, hidden, that stored the user id.
  88. $html[] = '<input type="hidden" id="' . $this->id . '_id" name="' . $this->name . '" value="' . (int) $this->value . '" />';
  89. return implode("\n", $html);
  90. }
  91. /**
  92. * Method to get the filtering groups (null means no filtering)
  93. *
  94. * @return mixed array of filtering groups or null.
  95. *
  96. * @since 1.6.0
  97. */
  98. protected function getGroups()
  99. {
  100. return null;
  101. }
  102. /**
  103. * Method to get the users to exclude from the list of users
  104. *
  105. * @return mixed Array of users to exclude or null to to not exclude them
  106. *
  107. * @since 1.6.0
  108. */
  109. protected function getExcluded()
  110. {
  111. return null;
  112. }
  113. }