[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare - admin: Custom fields * 4 * http://www.egroupware.org * 5 * -------------------------------------------- * 6 * This program is free software; you can redistribute it and/or modify it * 7 * under the terms of the GNU General Public License as published by the * 8 * Free Software Foundation; either version 2 of the License, or (at your * 9 * option) any later version. * 10 \**************************************************************************/ 11 12 /* $Id: class.customfields.inc.php 21864 2006-06-16 15:12:23Z nelius_weiss $ */ 13 14 /** 15 * Custiomfields class - manages customfield definitions in egw_config table 16 * 17 * The repository name (config_name) is 'customfields'. 18 * 19 * @license GPL 20 * @author Ralf Becker <ralfbecker-AT-outdoor-training.de> 21 * @author Cornelius Weiss <nelius-AT-von-und-zu-weiss.de> 22 * @package admin 23 */ 24 class customfields 25 { 26 27 /** 28 * @var string $appname string appname of app which want to add / edit its customfields 29 */ 30 var $appname; 31 32 /** 33 * @var array $types array with allowd types of customfields 34 */ 35 var $types = array( 36 'text' => 'Text', 37 'label' => 'Label', 38 'select' => 'Selectbox', 39 'radio' => 'Radiobutton', 40 'checkbox' => 'Checkbox', 41 ); 42 43 /** 44 * @var $types2 array with userdefiened types e.g. type of infolog 45 */ 46 var $types2 = array(); 47 48 var $public_functions = array 49 ( 50 'edit' => True 51 ); 52 53 function customfields($appname='') 54 { 55 $this->appname = $appname ? $appname : $_GET['appname']; 56 $this->tmpl =& CreateObject('etemplate.etemplate'); 57 $this->config =& CreateObject('phpgwapi.config',$this->appname); 58 } 59 60 /** 61 * Edit/Create Custom fields with type 62 * 63 * @author Ralf Becker <ralfbecker-AT-outdoor-training.de> 64 * @param array $content Content from the eTemplate Exec 65 */ 66 function edit($content = null) 67 { 68 $GLOBALS['egw']->translation->add_app('infolog'); // til we move the translations 69 70 if (is_array($content)) 71 { 72 // setting our app again 73 $this->config->config($this->appname = $content['appname']); 74 $this->fields = $this->get_customfields(); 75 76 //echo '<pre style="text-align: left;">'; print_r($content); echo "</pre>\n"; 77 if($content['fields']['delete'] || $content['fields']['create']) 78 { 79 if($content['fields']['delete']) 80 { 81 $this->delete($content); 82 } 83 elseif($content['fields']['create']) 84 { 85 $this->create($content); 86 } 87 } 88 else 89 { 90 list($action) = @each($content['button']); 91 switch($action) 92 { 93 default: 94 if (!$content['fields']['create'] && !$content['fields']['delete']) 95 { 96 break; // type change 97 } 98 case 'save': 99 case 'apply': 100 $this->update($content); 101 if ($action != 'save') 102 { 103 break; 104 } 105 case 'cancel': 106 $GLOBALS['egw']->redirect_link($content['referer'] ? $content['referer'] : '/admin/index.php'); 107 exit; 108 } 109 } 110 $referer = $content['referer']; 111 } 112 else 113 { 114 $this->fields = $this->get_customfields(); 115 116 list($type) = each($this->types); 117 $content = array( 118 'type' => $type, 119 ); 120 $referer = $GLOBALS['egw']->common->get_referer(); 121 } 122 $GLOBALS['egw_info']['flags']['app_header'] = $GLOBALS['egw_info']['apps'][$this->appname]['title'].' - '.lang('Custom fields'); 123 124 $readonlys = array(); 125 126 //echo 'customfields=<pre style="text-align: left;">'; print_r($this->fields); echo "</pre>\n"; 127 $content['fields'] = array(); 128 $n = 0; 129 foreach($this->fields as $name => $data) 130 { 131 if(!is_array($data)) 132 { 133 $data = array(); 134 $data['label'] = $name; 135 $data['order'] = ($n+1) * 10; 136 } 137 if (is_array($data['values'])) 138 { 139 $values = ''; 140 foreach($data['values'] as $var => $value) 141 { 142 $values .= (!empty($values) ? "\n" : '').$var.'='.$value; 143 } 144 $data['values'] = $values; 145 } 146 $content['fields'][++$n] = (array)$data + array( 147 'name' => $name 148 ); 149 $preserv_fields[$n]['old_name'] = $name; 150 $readonlys['fields']["create$name"] = True; 151 } 152 $content['fields'][++$n] = array('name'=>'','order' => 10 * $n); // new line for create 153 $readonlys['fields']["delete[]"] = True; 154 //echo '<p>uicustomfields.edit(content = <pre style="text-align: left;">'; print_r($content); echo "</pre>\n"; 155 //echo 'readonlys = <pre style="text-align: left;">'; print_r($readonlys); echo "</pre>\n"; 156 $this->tmpl->read('admin.customfields'); 157 $this->tmpl->exec('admin.customfields.edit',$content,array( 158 'type' => $this->types, 159 'type2' => $this->types2, 160 ),$readonlys,array( 161 'fields' => $preserv_fields, 162 'appname' => $this->appname, 163 'referer' => $referer, 164 )); 165 } 166 167 function update_fields(&$content) 168 { 169 foreach($content['fields'] as $field) 170 { 171 $name = trim($field['name']); 172 $old_name = $field['old_name']; 173 174 if (!empty($delete) && $delete == $old_name) 175 { 176 unset($this->fields[$old_name]); 177 continue; 178 } 179 if (isset($field['old_name'])) 180 { 181 if (empty($name)) // empty name not allowed 182 { 183 $content['error_msg'] = lang('Name must not be empty !!!'); 184 $name = $old_name; 185 } 186 if (!empty($name) && $old_name != $name) // renamed 187 { 188 unset($this->fields[$old_name]); 189 } 190 } 191 elseif (empty($name)) // new item and empty ==> ignore it 192 { 193 continue; 194 } 195 $values = array(); 196 if (!empty($field['values'])) 197 { 198 foreach(explode("\n",$field['values']) as $line) 199 { 200 list($var,$value) = split('=',trim($line),2); 201 $var = trim($var); 202 $values[$var] = empty($value) ? $var : $value; 203 } 204 } 205 $this->fields[$name] = array( 206 'type' => $field['type'], 207 'type2' => $field['type2'], 208 'label' => empty($field['label']) ? $name : $field['label'], 209 'help' => $field['help'], 210 'values'=> $values, 211 'len' => $field['len'], 212 'rows' => intval($field['rows']), 213 'order' => intval($field['order']) 214 ); 215 } 216 if (!function_exists('sort_by_order')) 217 { 218 function sort_by_order($arr1,$arr2) 219 { 220 return $arr1['order'] - $arr2['order']; 221 } 222 } 223 uasort($this->fields,sort_by_order); 224 225 $n = 0; 226 foreach($this->fields as $name => $data) 227 { 228 $this->fields[$name]['order'] = ($n += 10); 229 } 230 } 231 232 233 function update(&$content) 234 { 235 $this->update_fields($content); 236 // save changes to repository 237 $this->save_repository(); 238 } 239 240 /** 241 * deletes custom field from customfield definitions 242 */ 243 function delete(&$content) 244 { 245 unset($this->fields[key($content['fields']['delete'])]); 246 247 // save changes to repository 248 $this->save_repository(); 249 } 250 251 /** 252 * create a new custom field 253 */ 254 function create(&$content) 255 { 256 $new_name = trim($content['fields'][count($content['fields'])-1]['name']); 257 if (empty($new_name) || isset($this->fields[$new_name])) 258 { 259 $content['error_msg'] .= empty($new_name) ? 260 lang('You have to enter a name, to create a new field!!!') : 261 lang("Field '%1' already exists !!!",$new_name); 262 } 263 else 264 { 265 $this->fields[$new_name] = $content['fields'][count($content['fields'])-1]; 266 if(!$this->fields[$new_name]['label']) $this->fields[$new_name]['label'] = $this->fields[$new_name]['name']; 267 $this->save_repository(); 268 } 269 } 270 271 /** 272 * save changes to repository 273 */ 274 function save_repository() 275 { 276 //echo '<p>uicustomfields::save_repository() \$this->fields=<pre style="text-aling: left;">'; print_r($this->fields); echo "</pre>\n"; 277 $this->config->value('customfields',$this->fields); 278 279 $this->config->save_repository(); 280 } 281 282 /** 283 * get customfields of using application 284 * 285 * @author Cornelius Weiss 286 * @return array with customfields 287 */ 288 function get_customfields() 289 { 290 $config = $this->config->read_repository(); 291 //merge old config_name in phpgw_config table 292 $config_name = isset($config['customfields']) ? 'customfields' : 'custom_fields'; 293 294 return is_array($config[$config_name]) ? $config[$config_name] : array(); 295 } 296 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |