1<?php 2/** 3 * Class helper_plugin_dataau_aliastextbox 4 * 5 * Create a field with properties defined by given type alias 6 * Mostly this is a single line input field, but for enum type a select field. 7 */ 8class helper_plugin_dataau_aliastextbox extends helper_plugin_bureaucracyau _field { 9 10 private $args; 11 private $additional; 12 13 /** 14 * Arguments: 15 * - cmd 16 * - label 17 * - _typealias (optional) 18 * - ^ (optional) 19 * 20 * @param array $args The tokenized definition, only split at spaces 21 */ 22 public function initialize($args) { 23 $this->init($args); 24 $n_args = array(); 25 $this->args = array(); 26 foreach($args as $arg) { 27 if($arg[0] !== '_') { 28 $n_args[] = $arg; 29 continue; 30 } 31 $this->args[] = $arg; 32 } 33 $this->standardArgs($n_args); 34 35 } 36 37 /** 38 * Prepare 39 * 40 * @param array $args dataau plugin related field arguments 41 */ 42 private function prepareColumns($args) { 43 /** @var helper_plugin_dataau $dthlp */ 44 $dthlp = plugin_load('helper', 'dataau'); 45 if(!$dthlp) msg('Loading the dataau helper failed. Make sure the dataau plugin is installed.', -1); 46 47 foreach($args as $arg) { 48 $arg = $this->replaceTranslation($arg); 49 $datatype = $dthlp->_column($arg); 50 if(is_array($datatype['type'])) { 51 $datatype['basetype'] = $datatype['type']['type']; 52 $datatype['enum'] = $datatype['type']['enum']; 53 $datatype['type'] = $datatype['origtype']; 54 } else { 55 $datatype['basetype'] = $datatype['type']; 56 } 57 } 58 $datatype['title'] = '@@DISPLAY@@'; 59 if(isset($datatype['enum'])) { 60 $values = preg_split('/\s*,\s*/', $datatype['enum']); 61 if(!$datatype['multi'] && $this->opt['optional']) array_unshift($values, ''); 62 $this->opt['args'] = $values; 63 $this->additional = ($datatype['multi'] ? array('multiple' => 'multiple') : array()); 64 } else { 65 $classes = 'dataau_type_' . $datatype['type'] . ($datatype['multi'] ? 's' : '') . ' ' . 66 'dataau_type_' . $datatype['basetype'] . ($datatype['multi'] ? 's' : ''); 67 $content = form_makeTextField('@@NAME@@', '@@VALUE@@', '@@DISPLAY@@', '@@ID@@', '@@CLASS@@ ' . $classes); 68 69 $this->tpl = $content; 70 } 71 if(!isset($this->opt['display'])) { 72 $this->opt['display'] = $this->opt['label']; 73 } 74 75 } 76 77 /** 78 * Render the field as XHTML 79 * 80 * Creates a single line input field or select field 81 * 82 * @params array $params Additional HTML specific parameters 83 * @params Doku_Form $form The target Doku_Form object 84 * @params int $formid unique identifier of the form which contains this field 85 */ 86 public function renderfield($params, Doku_Form $form, $formid) { 87 $this->prepareColumns($this->args); 88 89 if(isset($this->tpl)) { 90 parent::renderfield($params, $form, $formid); 91 } else { 92 // Is an enum type, otherwise $this->tpl would be set in __construct 93 $this->_handlePreload(); 94 if(!$form->_infieldset) { 95 $form->startFieldset(''); 96 } 97 if($this->error) { 98 $params['class'] = 'bureaucracyau _error'; 99 } 100 $params = array_merge($this->opt, $params); 101 $params['value'] = preg_split('/\s*,\s*/', $params['value'], -1, PREG_SPLIT_NO_EMPTY); 102 if(count($params['value']) === 0) { 103 $params['value'] = $params['args'][0]; 104 } 105 if(!isset($this->opt['optional'])) { 106 $this->additional['required'] = 'required'; 107 } 108 109 $form->addElement(call_user_func_array('form_makeListboxField', 110 $this->_parse_tpl( 111 array( 112 '@@NAME@@[]', 113 $params['args'], 114 $params['value'], 115 '@@DISPLAY@@', 116 '', 117 '@@CLASS@@', 118 $this->additional 119 ), 120 $params 121 ))); 122 } 123 } 124 125 /** 126 * Handle a post to the field 127 * 128 * Accepts and validates a posted value. 129 * 130 * @param string $value The passed value or array or null if none given 131 * @param helper_plugin_bureaucracyau _field[] $fields (reference) form fields (POST handled upto $this field) 132 * @param int $index index number of field in form 133 * @param int $formid unique identifier of the form which contains this field 134 * @return bool Whether the passed value is valid 135 */ 136 public function handle_post($value, &$fields, $index, $formid) { 137 if(is_array($value)) { 138 $value = join(', ', $value); 139 } 140 141 return parent::handle_post($value, $fields, $index, $formid); 142 } 143 144 /** 145 * Replace the translation placeholders 146 * 147 * @param string $string 148 * @return string parsed string 149 */ 150 private function replaceTranslation($string) { 151 global $ID; 152 global $conf; 153 154 $lang = $conf['lang']; 155 $trans = ''; 156 157 /** @var helper_plugin_translation $translationPlugin */ 158 $translationPlugin = plugin_load('helper', 'translation'); 159 if($translationPlugin) { 160 $trans = $translationPlugin->getLangPart($ID); 161 $lang = $translationPlugin->realLC(''); 162 } 163 164 $string = str_replace('@LANG@', $lang, $string); 165 return str_replace('@TRANS@', $trans, $string); 166 } 167} 168 169