Total Complexity | 42 |
Total Lines | 344 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like infolog_customfields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use infolog_customfields, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class infolog_customfields extends admin_customfields |
||
20 | { |
||
21 | public $appname = 'infolog'; |
||
22 | /** |
||
23 | * Instance of the infolog BO class |
||
24 | * |
||
25 | * @var infolog_bo |
||
26 | */ |
||
27 | var $bo; |
||
28 | /** |
||
29 | * instance of the config class for infolog |
||
30 | * |
||
31 | * @var Api\Config |
||
32 | */ |
||
33 | var $config_data; |
||
34 | /** |
||
35 | * Group owners for certain types read from the infolog config |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | var $group_owners; |
||
40 | |||
41 | function __construct( ) |
||
42 | { |
||
43 | parent::__construct('infolog'); |
||
44 | |||
45 | $this->bo = new infolog_bo(); |
||
46 | $this->tmpl = new Etemplate(); |
||
47 | $this->content_types = &$this->bo->enums['type']; |
||
48 | $this->status = &$this->bo->status; |
||
|
|||
49 | $this->config_data = Api\Config::read('infolog'); |
||
50 | $this->fields = &$this->bo->customfields; |
||
51 | $this->group_owners =& $this->bo->group_owners; |
||
52 | |||
53 | Api\Translation::add_app('infolog'); |
||
54 | } |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Hook from parent class so we can interfere and add owner & status |
||
59 | */ |
||
60 | protected function app_index(&$content, &$sel_options, &$readonlys, &$preserve) |
||
61 | { |
||
62 | unset($sel_options); // not used, but required by function signature |
||
63 | |||
64 | $n = 0; |
||
65 | foreach($this->status[$this->content_type] as $name => $label) |
||
66 | { |
||
67 | $content['content_type_options']['status'][++$n] = array( |
||
68 | 'name' => $name, |
||
69 | 'label' => $label, |
||
70 | 'disabled' => False |
||
71 | ); |
||
72 | $preserve['content_type_options']['status'][$n]['old_name'] = $name; |
||
73 | if (isset($this->bo->stock_status[$this->content_type][$name])) |
||
74 | { |
||
75 | $readonlys['content_type_options']['status']["delete[$name]"] = |
||
76 | $readonlys['content_type_options']['status'][$n.'[name]'] = True; |
||
77 | } |
||
78 | $readonlys['content_type_options']['status']["create$name"] = True; |
||
79 | } |
||
80 | $content['content_type_options']['status'][++$n] = array( |
||
81 | 'name' => '', |
||
82 | 'label' => '', |
||
83 | 'disabled' => False |
||
84 | ); |
||
85 | $content['content_type_options']['status']['default'] = $this->status['defaults'][$this->content_type]; |
||
86 | $content['content_type_options']['group_owner'] = $this->group_owners[$this->content_type]; |
||
87 | $readonlys['content_types']['delete'] = isset($this->bo->stock_enums['type'][$this->content_type]); |
||
88 | } |
||
89 | |||
90 | function update_fields(&$content) |
||
91 | { |
||
92 | $fields = &$content['fields']; |
||
93 | |||
94 | $create = $fields['create']; |
||
95 | unset($fields['create']); |
||
96 | |||
97 | if ($fields['delete']) |
||
98 | { |
||
99 | $delete = key($fields['delete']); |
||
100 | unset($fields['delete']); |
||
101 | } |
||
102 | |||
103 | foreach($fields as $field) |
||
104 | { |
||
105 | $name = trim($field['name']); |
||
106 | $old_name = $field['old_name']; |
||
107 | |||
108 | if (!empty($delete) && $delete == $old_name) |
||
109 | { |
||
110 | unset($this->fields[$old_name]); |
||
111 | continue; |
||
112 | } |
||
113 | if (isset($field['name']) && empty($name) && ($create || !empty($old_name))) // empty name not allowed |
||
114 | { |
||
115 | $content['error_msg'] = lang('Name must not be empty !!!'); |
||
116 | } |
||
117 | if (isset($field['old_name'])) |
||
118 | { |
||
119 | if (!empty($name) && $old_name != $name) // renamed |
||
120 | { |
||
121 | unset($this->fields[$old_name]); |
||
122 | } |
||
123 | elseif (empty($name)) |
||
124 | { |
||
125 | $name = $old_name; |
||
126 | } |
||
127 | } |
||
128 | elseif (empty($name)) // new item and empty ==> ignore it |
||
129 | { |
||
130 | continue; |
||
131 | } |
||
132 | $values = array(); |
||
133 | if (!empty($field['values'])) |
||
134 | { |
||
135 | foreach(explode("\n",$field['values']) as $line) |
||
136 | { |
||
137 | list($var2,$value) = explode('=',trim($line),2); |
||
138 | $var = trim($var2); |
||
139 | $values[$var] = empty($value) ? $var : $value; |
||
140 | } |
||
141 | } |
||
142 | $this->fields[$name] = array( |
||
143 | 'type2' => $field['type2'], |
||
144 | 'type' => $field['type'], |
||
145 | 'label' => empty($field['label']) ? $name : $field['label'], |
||
146 | 'help' => $field['help'], |
||
147 | 'values'=> $values, |
||
148 | 'len' => $field['len'], |
||
149 | 'rows' => (int)$field['rows'], |
||
150 | 'order' => (int)$field['order'], |
||
151 | 'needed' => $field['needed'], |
||
152 | ); |
||
153 | } |
||
154 | if (!function_exists('sort_by_order')) |
||
155 | { |
||
156 | function sort_by_order($arr1,$arr2) |
||
157 | { |
||
158 | return $arr1['order'] - $arr2['order']; |
||
159 | } |
||
160 | } |
||
161 | uasort($this->fields,sort_by_order); |
||
162 | |||
163 | $n = 0; |
||
164 | foreach(array_keys($this->fields) as $name) |
||
165 | { |
||
166 | $this->fields[$name]['order'] = ($n += 10); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | function update_status(&$content) |
||
227 | } |
||
228 | } |
||
229 | |||
230 | function update(&$content) |
||
231 | { |
||
232 | $old = array( |
||
233 | 'status' => $this->status, |
||
234 | 'group_owners' => $this->group_owners |
||
235 | ); |
||
236 | $this->update_status($content); |
||
237 | |||
238 | if ($content['content_type_options']['group_owner']) |
||
239 | { |
||
240 | $this->group_owners[$this->content_type] = $content['content_type_options']['group_owner']; |
||
241 | } |
||
242 | else |
||
243 | { |
||
244 | unset($this->group_owners[$this->content_type]); |
||
245 | } |
||
246 | $changed = array(); |
||
247 | foreach($old as $key => $value) |
||
248 | { |
||
249 | if($this->$key != $value) |
||
250 | { |
||
251 | // NB: Statuses are monolithic - we can't record just the one type |
||
252 | // that was changed, or we loose the other types. All status must |
||
253 | // be recorded. |
||
254 | $changed[$key] = $this->$key; |
||
255 | } |
||
256 | else |
||
257 | { |
||
258 | unset($old[$key]); |
||
259 | } |
||
260 | } |
||
261 | if($changed) |
||
262 | { |
||
263 | $cmd = new admin_cmd_config('infolog',$changed, $old); |
||
264 | $cmd->run(); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | function delete(&$content) |
||
283 | } |
||
284 | |||
285 | function create_content_type(&$content) |
||
286 | { |
||
287 | $new_name = trim($content['content_types']['name']); |
||
288 | if (empty($new_name)) |
||
289 | { |
||
290 | $this->tmpl->set_validation_error('content_types[name]',lang('You have to enter a name, to create a new type!')); |
||
291 | return false; |
||
292 | } |
||
293 | else |
||
294 | { |
||
295 | foreach($this->content_types as $name) |
||
296 | { |
||
297 | if($name == $new_name) |
||
298 | { |
||
299 | $this->tmpl->set_validation_error('content_types[name]',lang("type '%1' already exists !!!",$new_name)); |
||
300 | return false; |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | $this->content_types[$new_name] = $new_name; |
||
305 | $this->status[$new_name] = array( |
||
306 | 'ongoing' => 'ongoing', |
||
307 | 'done' => 'done' |
||
308 | ); |
||
309 | $this->status['defaults'][$new_name] = 'ongoing'; |
||
310 | |||
311 | // save changes to repository |
||
312 | $this->save_repository(); |
||
313 | return $new_name; |
||
314 | } |
||
315 | |||
316 | function save_repository() |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Change account_ids in group_owners configuration hook called from admin_cmd_change_account_id |
||
329 | * |
||
330 | * @param array $changes |
||
331 | * @return int number of changed account_ids |
||
332 | */ |
||
333 | public static function change_account_ids(array $changes) |
||
363 | } |
||
364 | } |
||
365 |