1<?php 2namespace dokuwiki\plugin\structgroup\types; 3 4use dokuwiki\plugin\struct\meta\QueryBuilder; 5use dokuwiki\plugin\struct\meta\QueryBuilderWhere; 6use dokuwiki\plugin\struct\meta\StructException; 7use dokuwiki\plugin\struct\meta\ValidationException; 8use dokuwiki\plugin\struct\types\AbstractMultiBaseType; 9 10class Group extends AbstractMultiBaseType { 11 12 protected $config = array( 13 'existingonly' => true, 14 'autocomplete' => array( 15 'mininput' => 2, 16 'maxresult' => 5, 17 ), 18 ); 19 20 /** 21 * @param string $rawvalue the user to validate 22 * @return int|string 23 */ 24 public function validate($rawvalue) { 25 $rawvalue = parent::validate($rawvalue); 26 27 if($this->config['existingonly']) { 28 /** @var \helper_plugin_structgroup_authgroup $authgroup */ 29 $authgroup = plugin_load('helper', 'structgroup_authgroup'); 30 31 if(! in_array($rawvalue, $authgroup->getGroups())) { 32 throw new ValidationException('Group not found: "' . $rawvalue . 33 '". Available groups: ' . implode(', ', $authgroup->getGroups())); 34 } 35 } 36 37 return $rawvalue; 38 } 39 40 /** 41 * @param string $value the user to display 42 * @param \Doku_Renderer $R 43 * @param string $mode 44 * @return bool 45 */ 46 public function renderValue($value, \Doku_Renderer $R, $mode) { 47 $R->cdata('@' . $value); 48 return true; 49 } 50 51 /** 52 * Autocompletion for user names 53 * 54 * @todo should we have any security mechanism? Currently everybody can look up groups 55 * @return array 56 */ 57 public function handleAjax() { 58 global $INPUT; 59 60 /** @var \helper_plugin_structgroup_authgroup $authgroup */ 61 $authgroup = plugin_load('helper', 'structgroup_authgroup'); 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 $groups = (array) $authgroup->getGroups($max, $lookup); 72 73 // reformat result for jQuery UI Autocomplete 74 return array_map(function ($group) { 75 return array( 76 'label' => '@' . $group, 77 'value' => $group 78 ); 79 }, $groups); 80 } 81} 82