You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

100 lines
2.7 KiB

  1. <?php
  2. /**
  3. * Class helper_plugin_bureaucracy_fielduser
  4. *
  5. * Create single user input, with autocompletion
  6. */
  7. class helper_plugin_bureaucracy_fielduser extends helper_plugin_bureaucracy_fieldtextbox {
  8. /**
  9. * Arguments:
  10. * - cmd
  11. * - label
  12. * - ^ (optional)
  13. *
  14. * @param array $args The tokenized definition, only split at spaces
  15. */
  16. public function initialize($args) {
  17. parent::initialize($args);
  18. $this->tpl['class'] .= ' userpicker';
  19. }
  20. /**
  21. * Allow receiving user attributes by ".". Ex. user.name
  22. * You can pass an optional argument to user.grps enclosed in brackets, used as an groups delimiter Ex. user.grps(, )
  23. *
  24. * @return string
  25. */
  26. public function getReplacementPattern() {
  27. $label = $this->opt['label'];
  28. return '/(@@|##)' . preg_quote($label, '/') .
  29. '(?:\.(.*?))?' . //match attribute after "."
  30. '(?:\((.*?)\))?' . //match parameter enclosed in "()". Used for grps separator
  31. '\1/si';
  32. }
  33. /**
  34. * Used as an callback for preg_replace_callback
  35. *
  36. * @param $matches
  37. * @return string
  38. */
  39. public function replacementValueCallback($matches) {
  40. /** @var DokuWiki_Auth_Plugin $auth */
  41. global $auth;
  42. $value = $this->opt['value'];
  43. //attr doesn't exists
  44. if (!isset($matches[2])) {
  45. return is_null($value) || $value === false ? '' : $value;
  46. }
  47. $attr = $matches[2];
  48. $udata = $auth->getUserData($value);
  49. //no such user
  50. if ($udata === false) {
  51. return $matches[0];
  52. }
  53. switch($attr) {
  54. case 'name':
  55. case 'mail':
  56. return $udata[$attr];
  57. case 'grps':
  58. $delitmiter = ', ';
  59. if (isset($matches[3])) {
  60. $delitmiter = $matches[3];
  61. }
  62. return implode($delitmiter, $udata['grps']);
  63. default:
  64. return $matches[0];
  65. }
  66. }
  67. /**
  68. * Return the callback for user replacement
  69. *
  70. * @return array
  71. */
  72. public function getReplacementValue() {
  73. return array($this, 'replacementValueCallback');
  74. }
  75. /**
  76. * Validate value of field
  77. *
  78. * @throws Exception when user not exists
  79. */
  80. protected function _validate() {
  81. parent::_validate();
  82. /** @var DokuWiki_Auth_Plugin $auth */
  83. global $auth;
  84. $value = $this->getParam('value');
  85. if (!is_null($value) && $auth->getUserData($value) === false) {
  86. throw new Exception(sprintf($this->getLang('e_user'),hsc($this->getParam('display'))));
  87. }
  88. }
  89. }