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