Total Complexity | 47 |
Total Lines | 297 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AdherentTypeController 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 AdherentTypeController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class AdherentTypeController extends DolibarrController |
||
49 | { |
||
50 | public $rowid; |
||
51 | public $extrafields; |
||
52 | public $massaction; |
||
53 | public $cancel; |
||
54 | public $toselect; |
||
55 | public $contextpage; |
||
56 | public $backtopage; |
||
57 | public $mode; |
||
58 | public $sall; |
||
59 | public $filter; |
||
60 | public $search_ref; |
||
61 | public $search_lastname; |
||
62 | public $search_login; |
||
63 | public $search_email; |
||
64 | public $type; |
||
65 | public $status; |
||
66 | public $optioncss; |
||
67 | public $limit; |
||
68 | public $sortfield; |
||
69 | public $sortorder; |
||
70 | public $page; |
||
71 | public $pagenext; |
||
72 | public $label; |
||
73 | public $morphy; |
||
74 | public $offset; |
||
75 | public $pageprev; |
||
76 | public $subscription; |
||
77 | public $amount; |
||
78 | public $duration_value; |
||
79 | public $duration_unit; |
||
80 | public $vote; |
||
81 | public $comment; |
||
82 | public $mail_valid; |
||
83 | public $caneditamount; |
||
84 | |||
85 | /** |
||
86 | * \file htdocs/adherents/type.php |
||
87 | * \ingroup member |
||
88 | * \brief Member's type setup |
||
89 | */ |
||
90 | public function doIndex(): bool |
||
91 | { |
||
92 | global $conf; |
||
93 | global $db; |
||
94 | global $user; |
||
95 | global $hookmanager; |
||
96 | global $user; |
||
97 | global $menumanager; |
||
98 | global $langs; |
||
99 | |||
100 | // Load translation files required by the page |
||
101 | $this->langs->load("members"); |
||
102 | |||
103 | $action = $this->filterPost('action', 'aZ09'); |
||
104 | if ($action === 'create') { |
||
105 | $this->template = '/page/adherent/type_edit'; |
||
106 | return true; |
||
107 | } |
||
108 | |||
109 | if ($action === 'cancel') { |
||
110 | $this->template = '/page/adherent/type_list'; |
||
111 | return true; |
||
112 | } |
||
113 | |||
114 | /* |
||
115 | * Actions |
||
116 | */ |
||
117 | |||
118 | if ($this->filterPost('button_removefilter_x', 'alpha') || $this->filterPost('button_removefilter_x', 'alpha') || $this->filterPost('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers |
||
119 | $search_ref = ""; |
||
120 | $search_lastname = ""; |
||
121 | $search_login = ""; |
||
122 | $search_email = ""; |
||
123 | $type = ""; |
||
124 | $sall = ""; |
||
125 | } |
||
126 | |||
127 | if ($this->filterPost('cancel', 'alpha')) { |
||
128 | $action = 'list'; |
||
129 | $massaction = ''; |
||
130 | } |
||
131 | |||
132 | if (!$this->filterPost('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { |
||
133 | $massaction = ''; |
||
134 | } |
||
135 | |||
136 | if ($cancel) { |
||
|
|||
137 | $action = ''; |
||
138 | |||
139 | if (!empty($backtopage)) { |
||
140 | header("Location: " . $backtopage); |
||
141 | exit; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | if ($action == 'add' && $user->hasRight('adherent', 'configurer')) { |
||
146 | $this->object->label = trim($label); |
||
147 | $this->object->morphy = trim($morphy); |
||
148 | $this->object->status = (int)$status; |
||
149 | $this->object->subscription = (int)$subscription; |
||
150 | $this->object->amount = ($amount == '' ? '' : price2num($amount, 'MT')); |
||
151 | $this->object->caneditamount = $caneditamount; |
||
152 | $this->object->duration_value = $duration_value; |
||
153 | $this->object->duration_unit = $duration_unit; |
||
154 | $this->object->note_public = trim($comment); |
||
155 | $this->object->note_private = ''; |
||
156 | $this->object->mail_valid = trim($mail_valid); |
||
157 | $this->object->vote = (int)$vote; |
||
158 | |||
159 | // Fill array 'array_options' with data from add form |
||
160 | $ret = $extrafields->setOptionalsFromPost(null, $this->object); |
||
161 | if ($ret < 0) { |
||
162 | $error++; |
||
163 | } |
||
164 | |||
165 | if (empty($this->object->label)) { |
||
166 | $error++; |
||
167 | setEventMessages($this->langs->trans("ErrorFieldRequired", $this->langs->transnoentities("Label")), null, 'errors'); |
||
168 | } else { |
||
169 | $sql = "SELECT libelle FROM " . MAIN_DB_PREFIX . "adherent_type WHERE libelle = '" . $this->db->escape($this->object->label) . "'"; |
||
170 | $sql .= " WHERE entity IN (" . getEntity('member_type') . ")"; |
||
171 | $result = $this->db->query($sql); |
||
172 | $num = null; |
||
173 | if ($result) { |
||
174 | $num = $this->db->num_rows($result); |
||
175 | } |
||
176 | if ($num) { |
||
177 | $error++; |
||
178 | $this->langs->load("errors"); |
||
179 | setEventMessages($this->langs->trans("ErrorLabelAlreadyExists", $login), null, 'errors'); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | if (!$error) { |
||
184 | $id = $this->object->create($user); |
||
185 | if ($id > 0) { |
||
186 | header("Location: " . $_SERVER['PHP_SELF']); |
||
187 | exit; |
||
188 | } else { |
||
189 | setEventMessages($this->object->error, $this->object->errors, 'errors'); |
||
190 | $action = 'create'; |
||
191 | } |
||
192 | } else { |
||
193 | $action = 'create'; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | if ($action == 'update' && $user->hasRight('adherent', 'configurer')) { |
||
198 | $this->object->fetch($this->rowid); |
||
199 | |||
200 | $this->object->oldcopy = dol_clone($this->object, 2); |
||
201 | |||
202 | $this->object->label = trim($label); |
||
203 | $this->object->morphy = trim($morphy); |
||
204 | $this->object->status = (int)$status; |
||
205 | $this->object->subscription = (int)$subscription; |
||
206 | $this->object->amount = ($amount == '' ? '' : price2num($amount, 'MT')); |
||
207 | $this->object->caneditamount = $caneditamount; |
||
208 | $this->object->duration_value = $duration_value; |
||
209 | $this->object->duration_unit = $duration_unit; |
||
210 | $this->object->note_public = trim($comment); |
||
211 | $this->object->note_private = ''; |
||
212 | $this->object->mail_valid = trim($mail_valid); |
||
213 | $this->object->vote = (bool)trim($vote); |
||
214 | |||
215 | // Fill array 'array_options' with data from add form |
||
216 | $ret = $extrafields->setOptionalsFromPost(null, $this->object, '@GETPOSTISSET'); |
||
217 | if ($ret < 0) { |
||
218 | $error++; |
||
219 | } |
||
220 | |||
221 | $ret = $this->object->update($user); |
||
222 | |||
223 | if ($ret >= 0 && !count($this->object->errors)) { |
||
224 | setEventMessages($this->langs->trans("MemberTypeModified"), null, 'mesgs'); |
||
225 | } else { |
||
226 | setEventMessages($this->object->error, $this->object->errors, 'errors'); |
||
227 | } |
||
228 | |||
229 | header("Location: " . $_SERVER['PHP_SELF'] . "?rowid=" . $this->object->id); |
||
230 | exit; |
||
231 | } |
||
232 | |||
233 | if ($action == 'confirm_delete' && $user->hasRight('adherent', 'configurer')) { |
||
234 | $this->object->fetch($this->rowid); |
||
235 | $res = $this->object->delete($user); |
||
236 | |||
237 | if ($res > 0) { |
||
238 | setEventMessages($this->langs->trans("MemberTypeDeleted"), null, 'mesgs'); |
||
239 | header("Location: " . $_SERVER['PHP_SELF']); |
||
240 | exit; |
||
241 | } else { |
||
242 | setEventMessages($this->langs->trans("MemberTypeCanNotBeDeleted"), null, 'errors'); |
||
243 | $action = ''; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | $this->db->close(); |
||
248 | |||
249 | $this->template = '/page/adherent/type_list'; |
||
250 | if ($this->rowid) { |
||
251 | if ($_GET['action'] === 'edit') { |
||
252 | $this->template = '/page/adherent/type_edit'; |
||
253 | } else { |
||
254 | $this->template = '/page/adherent/type_show'; |
||
255 | } |
||
256 | } |
||
257 | |||
258 | |||
259 | $this->menu = []; |
||
260 | foreach ($menu as $item) { |
||
261 | if ($item['type'] === 'left' || !$item['enabled']) { |
||
262 | continue; |
||
263 | } |
||
264 | $this->menu[] = [ |
||
265 | 'name' => $item['mainmenu'], |
||
266 | 'href' => BASE_URL . $item['url'], |
||
267 | 'prefix' => $item['prefix'], |
||
268 | 'title' => $item['titre'], |
||
269 | 'selected' => false, |
||
270 | ]; |
||
271 | } |
||
272 | |||
273 | return true; |
||
274 | } |
||
275 | |||
276 | public function loadRecord() |
||
277 | { |
||
278 | // Initialize technical objects |
||
279 | $this->object = new AdherentType($this->db); |
||
280 | $this->extrafields = new ExtraFields($this->db); |
||
281 | $this->hookmanager->initHooks(['membertypecard', 'globalcard']); |
||
282 | |||
283 | // Fetch optionals attributes and labels |
||
284 | $this->extrafields->fetch_name_optionals_label($this->object->table_element); |
||
285 | |||
286 | $this->rowid = $this->filterPostInt('rowid'); |
||
287 | if ($this->rowid) { |
||
288 | $this->object->fetch($this->rowid); |
||
289 | } |
||
290 | |||
291 | return true; |
||
292 | } |
||
293 | |||
294 | public function loadPost() |
||
345 | } |
||
346 | } |
||
347 |