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