1<?php 2namespace plugin\struct\types; 3 4use plugin\struct\meta\StructException; 5use plugin\struct\meta\ValidationException; 6 7class User extends AbstractMultiBaseType { 8 9 protected $config = array( 10 'fullname' => true, 11 'autocomplete' => array( 12 'mininput' => 2, 13 'maxresult' => 5, 14 ), 15 ); 16 17 /** 18 * @param string $value the user to validate 19 * @return int|string|void 20 */ 21 public function validate($value) { 22 $value = parent::validate($value); 23 24 /** @var \DokuWiki_Auth_Plugin $auth */ 25 global $auth; 26 $info = $auth->getUserData($value, false); 27 if($info === false) throw new ValidationException('User not found', $value); 28 return $value; 29 } 30 31 /** 32 * @param string $value the user to display 33 * @param \Doku_Renderer $R 34 * @param string $mode 35 * @return bool 36 */ 37 public function renderValue($value, \Doku_Renderer $R, $mode) { 38 if($mode == 'xhtml') { 39 $name = userlink($value); 40 $R->doc .= $name; 41 } else { 42 $name = userlink($value, true); 43 $R->cdata($name); 44 } 45 return true; 46 } 47 48 /** 49 * Autocompletion for user names 50 * 51 * @todo should we have any security mechanism? Currently everybody can look up users 52 * @return array 53 */ 54 public function handleAjax() { 55 /** @var \DokuWiki_Auth_Plugin $auth */ 56 global $auth; 57 global $INPUT; 58 59 if(!$auth->canDo('getUsers')) { 60 throw new StructException('The user backend can not search for users'); 61 } 62 63 // check minimum length 64 $lookup = trim($INPUT->str('search')); 65 if(utf8_strlen($lookup) < $this->config['autocomplete']['mininput']) return array(); 66 67 // results wanted? 68 $max = $this->config['autocomplete']['maxresult']; 69 if($max <= 0) return array(); 70 71 // find users by login, fill up with names if wanted 72 $logins = (array) $auth->retrieveUsers(0, $max, array('user' => $lookup)); 73 if((count($logins) < $max) && $this->config['fullname']) { 74 $logins = array_merge($logins, (array) $auth->retrieveUsers(0, $max, array('name' => $lookup))); 75 } 76 77 // reformat result for jQuery UI Autocomplete 78 $users = array(); 79 foreach($logins as $login => $info) { 80 $users[] = array( 81 'label' => $info['name'] . ' [' . $login . ']', 82 'value' => $login 83 ); 84 } 85 86 return $users; 87 } 88 89} 90