1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare - TimeSheet: Administration of custom fields * |
---|
4 | * http://www.egroupware.org * |
---|
5 | * Written and (c) by Christoph Mueller Metaways Infosystems GmbH * |
---|
6 | * -------------------------------------------- * |
---|
7 | * This program is free software; you can redistribute it and/or modify it * |
---|
8 | * under the terms of the GNU General Public License as published by the * |
---|
9 | * Free Software Foundation; version 2 of the License. * |
---|
10 | \**************************************************************************/ |
---|
11 | |
---|
12 | /* $Id: class.uicustomfields.inc.php 19761 2005-11-12 13:25:59Z ralfbecker $ */ |
---|
13 | |
---|
14 | /** |
---|
15 | * Administration of custom fields |
---|
16 | * |
---|
17 | * @package timesheet |
---|
18 | * @author Christoph Mueller |
---|
19 | * @copyright (c) by Ralf Becker <RalfBecker@outdoor-training.de> |
---|
20 | * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
---|
21 | */ |
---|
22 | |
---|
23 | if (!defined('TIMESHEET_APP')) |
---|
24 | { |
---|
25 | define('TIMESHEET_APP','timesheet'); |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | class uicustomfields |
---|
30 | { |
---|
31 | var $public_functions = array |
---|
32 | ( |
---|
33 | 'edit' => True |
---|
34 | ); |
---|
35 | |
---|
36 | /** |
---|
37 | * Customfield types, without the link app-names |
---|
38 | * |
---|
39 | * @var array |
---|
40 | */ |
---|
41 | var $cf_types = array( |
---|
42 | 'text' => 'Text', |
---|
43 | 'label' => 'Label', |
---|
44 | 'select' => 'Selectbox', |
---|
45 | 'radio' => 'Radiobutton', |
---|
46 | 'checkbox' => 'Checkbox', |
---|
47 | ); |
---|
48 | |
---|
49 | var $config; |
---|
50 | var $bo; |
---|
51 | var $tmpl; |
---|
52 | var $fields; |
---|
53 | |
---|
54 | |
---|
55 | function uicustomfields( ) |
---|
56 | { |
---|
57 | $this->bo =& CreateObject('timesheet.botimesheet'); |
---|
58 | $this->tmpl =& CreateObject('etemplate.etemplate'); |
---|
59 | $this->config = &$this->bo->config; |
---|
60 | $this->fields = &$this->bo->customfields; |
---|
61 | |
---|
62 | $GLOBALS['phpgw']->translation->add_app('etemplate'); |
---|
63 | foreach($this->cf_types as $name => $label) $this->cf_types[$name] = lang($label); |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Edit/Create an Timesheet Custom field |
---|
69 | * |
---|
70 | * @param array $content Content from the eTemplate Exec |
---|
71 | */ |
---|
72 | function edit($content=null) |
---|
73 | { |
---|
74 | |
---|
75 | $GLOBALS['phpgw_info']['flags']['app_header'] = lang(TIMESHEET_APP).' - '.lang('Custom fields'); |
---|
76 | |
---|
77 | if (is_array($content)) |
---|
78 | { |
---|
79 | //echo '<pre style="text-align: left;">'; print_r($content); echo "</pre>\n"; |
---|
80 | list($action) = @each($content['button']); |
---|
81 | switch($action) |
---|
82 | { |
---|
83 | default: |
---|
84 | if(!$content['fields']['create'] && !$content['fields']['delete']) { |
---|
85 | break; |
---|
86 | } |
---|
87 | case 'save': |
---|
88 | case 'apply': |
---|
89 | $this->update($content); |
---|
90 | if ($action != 'save') |
---|
91 | { |
---|
92 | break; |
---|
93 | } |
---|
94 | case 'cancel': |
---|
95 | $GLOBALS['phpgw']->redirect_link('/timesheet/index.php?menuaction=timesheet.uitimesheet.index'); |
---|
96 | exit; |
---|
97 | } |
---|
98 | } |
---|
99 | $readonlys = array(); |
---|
100 | |
---|
101 | //echo 'customfields=<pre style="text-align: left;">'; print_r($this->fields); echo "</pre>\n"; |
---|
102 | $content['fields'] = array(); |
---|
103 | $n = 0; |
---|
104 | if(is_array($this->fields)) { |
---|
105 | foreach($this->fields as $name => $data) |
---|
106 | { |
---|
107 | if (is_array($data['values'])) |
---|
108 | { |
---|
109 | $values = ''; |
---|
110 | foreach($data['values'] as $var => $value) |
---|
111 | { |
---|
112 | $values .= (!empty($values) ? "\n" : '').$var.'='.$value; |
---|
113 | } |
---|
114 | $data['values'] = $values; |
---|
115 | } |
---|
116 | $content['fields'][++$n] = $data + array( |
---|
117 | 'typ' => '', |
---|
118 | 'name' => $name |
---|
119 | ); |
---|
120 | $preserv_fields[$n]['old_name'] = $name; |
---|
121 | $readonlys['fields']["create$name"] = True; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | //$content['fields'][++$n] = array('typ'=>'','order' => 10 * $n); // new line for create |
---|
126 | $content['fields'][++$n] = array('typ' => '', 'label'=>'', 'help'=>'', 'values'=>'', 'len'=>'', 'rows'=>'', 'order'=>10 * $n, 'name'=>''); |
---|
127 | |
---|
128 | $readonlys['fields']["delete[]"] = True; |
---|
129 | |
---|
130 | //echo '<p>uicustomfields.edit(content = <pre style="text-align: left;">'; print_r($content); echo "</pre>\n"; |
---|
131 | //echo 'readonlys = <pre style="text-align: left;">'; print_r($readonlys); echo "</pre>\n"; |
---|
132 | $this->tmpl->read('timesheet.customfields'); |
---|
133 | $this->tmpl->exec('timesheet.uicustomfields.edit',$content,array( |
---|
134 | 'type' => $this->cf_types, |
---|
135 | ),$readonlys,array('fields' => $preserv_fields)); |
---|
136 | } |
---|
137 | |
---|
138 | function update_fields(&$content) |
---|
139 | { |
---|
140 | $fields = &$content['fields']; |
---|
141 | $create = $fields['create']; |
---|
142 | unset($fields['create']); |
---|
143 | |
---|
144 | if ($fields['delete']) |
---|
145 | { |
---|
146 | list($delete) = each($fields['delete']); |
---|
147 | unset($fields['delete']); |
---|
148 | } |
---|
149 | |
---|
150 | foreach($fields as $field) |
---|
151 | { |
---|
152 | $name = trim($field['name']); |
---|
153 | $old_name = $field['old_name']; |
---|
154 | |
---|
155 | if (!empty($delete) && $delete == $old_name) |
---|
156 | { |
---|
157 | //delete all timesheet extra entries with that certain name |
---|
158 | $this->bo->delete_extra('',$old_name); |
---|
159 | unset($this->fields[$old_name]); |
---|
160 | continue; |
---|
161 | } |
---|
162 | if (isset($field['name']) && empty($name) && ($create || !empty($old_name))) // empty name not allowed |
---|
163 | { |
---|
164 | $content['error_msg'] = lang('Name must not be empty !!!'); |
---|
165 | } |
---|
166 | if (isset($field['old_name'])) |
---|
167 | { |
---|
168 | if (!empty($name) && $old_name != $name) // renamed |
---|
169 | { |
---|
170 | //update all timesheet_extra entries with that certain name |
---|
171 | $this->bo->save_extra(True,$old_name,$name); |
---|
172 | unset($this->fields[$old_name]); |
---|
173 | } |
---|
174 | elseif (empty($name)) |
---|
175 | { |
---|
176 | $name = $old_name; |
---|
177 | } |
---|
178 | } |
---|
179 | elseif (empty($name)) // new item and empty ==> ignore it |
---|
180 | { |
---|
181 | continue; |
---|
182 | } |
---|
183 | $values = array(); |
---|
184 | if (!empty($field['values'])) |
---|
185 | { |
---|
186 | foreach(explode("\n",$field['values']) as $line) |
---|
187 | { |
---|
188 | list($var,$value) = split('=',trim($line),2); |
---|
189 | $var = trim($var); |
---|
190 | $values[$var] = empty($value) ? $var : $value; |
---|
191 | } |
---|
192 | } |
---|
193 | $this->fields[$name] = array( |
---|
194 | 'type' => $field['type'], |
---|
195 | 'label' => empty($field['label']) ? $name : $field['label'], |
---|
196 | 'help' => $field['help'], |
---|
197 | 'values'=> $values, |
---|
198 | 'len' => $field['len'], |
---|
199 | 'rows' => intval($field['rows']), |
---|
200 | 'order' => intval($field['order']) |
---|
201 | ); |
---|
202 | } |
---|
203 | if (!function_exists('sort_by_order')) |
---|
204 | { |
---|
205 | function sort_by_order($arr1,$arr2) |
---|
206 | { |
---|
207 | return $arr1['order'] - $arr2['order']; |
---|
208 | } |
---|
209 | } |
---|
210 | uasort($this->fields,sort_by_order); |
---|
211 | |
---|
212 | $n = 0; |
---|
213 | foreach($this->fields as $name => $data) |
---|
214 | { |
---|
215 | $this->fields[$name]['order'] = ($n += 10); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | function update(&$content) |
---|
220 | { |
---|
221 | $this->update_fields($content); |
---|
222 | // save changes to repository |
---|
223 | $this->save_repository(); |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | function save_repository() |
---|
228 | { |
---|
229 | // save changes to repository |
---|
230 | |
---|
231 | //echo '<p>uicustomfields::save_repository() \$this->fields=<pre style="text-aling: left;">'; print_r($this->fields); echo "</pre>\n"; |
---|
232 | $this->config->value('customfields',$this->fields); |
---|
233 | $this->config->save_repository(); |
---|
234 | } |
---|
235 | } |
---|