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.
 
 
 
 
 

97 lines
2.9 KiB

  1. <?php
  2. /**
  3. * Class helper_plugin_bureaucracy_fieldusers
  4. *
  5. * Create multi-user input, with autocompletion
  6. */
  7. class helper_plugin_bureaucracy_fieldusers 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'] .= ' userspicker';
  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. '(?:\((?P<delimiter>.*?)\))?' .//delimiter
  30. '(?:\.(?P<attribute>.*?))?' . //match attribute after "."
  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. //copy the value by default
  44. if (isset($matches[2]) && is_array($matches[2]) && count($matches[2]) == 2) {
  45. return is_null($value) || $value === false ? $matches[0] : $value;
  46. }
  47. $attribute = isset($matches['attribute']) ? $matches['attribute'] : '';
  48. //check if matched string containts a pair of brackets
  49. $delimiter = preg_match('/\(.*\)/s', $matches[0]) ? $matches['delimiter'] : ', ';
  50. $users = array_map('trim', explode(',', $value));
  51. switch($attribute) {
  52. case '':
  53. return implode($delimiter, $users);
  54. case 'name':
  55. case 'mail':
  56. return implode($delimiter, array_map(function ($user) use ($auth, $attribute) {
  57. return $auth->getUserData($user)[$attribute];
  58. }, $users));
  59. default:
  60. return $matches[0];
  61. }
  62. }
  63. /**
  64. * Return the callback for user replacement
  65. *
  66. * @return array
  67. */
  68. public function getReplacementValue() {
  69. return array($this, 'replacementValueCallback');
  70. }
  71. /**
  72. * Validate value of field
  73. *
  74. * @throws Exception when user not exists
  75. */
  76. protected function _validate() {
  77. parent::_validate();
  78. /** @var DokuWiki_Auth_Plugin $auth */
  79. global $auth;
  80. $users = array_filter(preg_split('/\s*,\s*/', $this->getParam('value')));
  81. foreach ($users as $user) {
  82. if ($auth->getUserData($user) === false) {
  83. throw new Exception(sprintf($this->getLang('e_users'), hsc($this->getParam('display'))));
  84. }
  85. }
  86. }
  87. }