Passed
Branch develop (59f205)
by
unknown
29:12
created

CActionComm   F

Complexity

Total Complexity 70

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 141
dl 0
loc 283
rs 2.8
c 1
b 0
f 0
wmc 70

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fetch() 0 33 4
A getNomUrl() 0 8 2
D liste_array() 0 141 63

How to fix   Complexity   

Complex Class

Complex classes like CActionComm 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 CActionComm, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* Copyright (C) 2002-2003 Rodolphe Quiedeville <[email protected]>
3
 * Copyright (C) 2004-2014 Laurent Destailleur  <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 *       \file       htdocs/comm/action/class/cactioncomm.class.php
21
 *       \ingroup    agenda
22
 *       \brief      File of class to manage type of agenda events
23
 */
24
25
26
/**
27
 *      Class to manage different types of events
28
 */
29
class CActionComm
30
{
31
	/**
32
	 * @var string Error code (or message)
33
	 */
34
	public $error = '';
35
36
	/**
37
	 * @var DoliDB Database handler.
38
	 */
39
	public $db;
40
41
	/**
42
	 * @var int ID
43
	 */
44
	public $id;
45
46
	/**
47
	 * @var string code
48
	 */
49
	public $code;
50
51
	/**
52
	 * @var string type
53
	 */
54
	public $type;
55
56
	/**
57
	 * @var string label
58
	 * @deprecated
59
	 * @see $label
60
	 */
61
	public $libelle;
62
63
	/**
64
	 * @var string Type of agenda event label
65
	 */
66
	public $label;
67
68
	/**
69
	 * @var int active
70
	 */
71
	public $active;
72
73
	/**
74
	 * @var string color hex
75
	 */
76
	public $color;
77
78
	/**
79
	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
80
	 */
81
	public $picto;
82
83
	/**
84
	 * @var array array of type_actions
85
	 */
86
	public $type_actions = array();
87
88
89
	/**
90
	 *  Constructor
91
	 *
92
	 *  @param	DoliDB		$db		Database handler
93
	 */
94
	public function __construct($db)
95
	{
96
		$this->db = $db;
97
	}
98
99
	/**
100
	 *  Load action type from database
101
	 *
102
	 *  @param  int|string	$id     id or code of action type to read
103
	 *  @return int             	1=ok, 0=not found, -1=error
104
	 */
105
	public function fetch($id)
106
	{
107
		$sql = "SELECT id, code, type, libelle as label, color, active, picto";
108
		$sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm";
109
		if (is_numeric($id)) {
110
			$sql .= " WHERE id=".(int) $id;
111
		} else {
112
			$sql .= " WHERE code='".$this->db->escape($id)."'";
113
		}
114
115
		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
116
		$resql = $this->db->query($sql);
117
		if ($resql) {
118
			if ($this->db->num_rows($resql)) {
119
				$obj = $this->db->fetch_object($resql);
120
121
				$this->id      = $obj->id;
122
				$this->code    = $obj->code;
123
				$this->type    = $obj->type;
124
				$this->libelle = $obj->label; // deprecated
125
				$this->label   = $obj->label;
126
				$this->active  = $obj->active;
127
				$this->color   = $obj->color;
128
129
				$this->db->free($resql);
130
				return 1;
131
			} else {
132
				$this->db->free($resql);
133
				return 0;
134
			}
135
		} else {
136
			$this->error = $this->db->error();
137
			return -1;
138
		}
139
	}
140
141
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
142
	/**
143
	 *  Return list of event types: array(id=>label) or array(code=>label)
144
	 *
145
	 *  @param  string|int  $active         1 or 0 to filter on event state active or not ('' by default = no filter)
146
	 *  @param  string      $idorcode       'id' or 'code' or 'all'
147
	 *  @param  string      $excludetype    Type to exclude ('system' or 'systemauto')
148
	 *  @param  int         $onlyautoornot  1=Group all type AC_XXX into 1 line AC_MANUAL. 0=Keep details of type, -1=Keep details and add a combined line per calendar (Default, Auto, BoothConf, ...)
149
	 *  @param  string      $morefilter     Add more SQL filter
150
	 *  @param  int         $shortlabel     1=Get short label instead of long label
151
	 *  @return mixed                       Array of all event types if OK, <0 if KO. Key of array is id or code depending on parameter $idorcode.
152
	 */
153
	public function liste_array($active = '', $idorcode = 'id', $excludetype = '', $onlyautoornot = 0, $morefilter = '', $shortlabel = 0)
154
	{
155
		// phpcs:enable
156
		global $langs, $conf, $user;
157
		$langs->load("commercial");
158
159
		$repid = array();
160
		$repcode = array();
161
		$repall = array();
162
163
		$sql = "SELECT id, code, libelle as label, module, type, color, picto";
164
		$sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm";
165
		$sql .= " WHERE 1=1";
166
		if ($active != '') {
167
			$sql .= " AND active=".(int) $active;
168
		}
169
		if (!empty($excludetype)) {
170
			$sql .= " AND type <> '".$this->db->escape($excludetype)."'";
171
		}
172
		if ($morefilter) {
173
			$sql .= " AND ".$morefilter;
174
		}
175
		$sql .= " ORDER BY type, position, module";
176
177
		dol_syslog(get_class($this)."::liste_array", LOG_DEBUG);
178
		$resql = $this->db->query($sql);
179
		if ($resql) {
180
			$nump = $this->db->num_rows($resql);
181
			if ($nump) {
182
				$idforallfornewmodule = 97;
183
				$i = 0;
184
				while ($i < $nump) {
185
					$obj = $this->db->fetch_object($resql);
186
187
					$qualified = 1;
188
189
					// $obj->type can be system, systemauto, module, moduleauto, xxx, xxxauto
190
					if ($qualified && $onlyautoornot > 0 && preg_match('/^system/', $obj->type) && !preg_match('/^AC_OTH/', $obj->code)) {
191
						$qualified = 0; // We discard detailed system events. We keep only the 2 generic lines (AC_OTH and AC_OTH_AUTO)
192
					}
193
194
					if ($qualified && !empty($obj->module)) {
195
						if ($obj->module == 'invoice' && empty($conf->facture->enabled)  && empty($user->facture->lire)) {
196
							$qualified = 0;
197
						}
198
						if ($obj->module == 'order' && empty($conf->commande->enabled) && empty($user->commande->lire)) {
199
							$qualified = 0;
200
						}
201
						if ($obj->module == 'propal' && empty($conf->propal->enabled)  && empty($user->propale->lire)) {
202
							$qualified = 0;
203
						}
204
						if ($obj->module == 'invoice_supplier' && ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || empty($conf->supplier_invoice->enabled)) && empty($user->fournisseur->facture->lire)) {
205
							$qualified = 0;
206
						}
207
						if ($obj->module == 'order_supplier' && ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || empty($conf->supplier_order->enabled)) && empty($user->fournisseur->commande->lire)) {
208
							$qualified = 0;
209
						}
210
						if ($obj->module == 'shipping' && empty($conf->expedition->enabled) && empty($user->expedition->lire)) {
211
							$qualified = 0;
212
						}
213
						if (preg_match('/@eventorganization/', $obj->module) && empty($conf->eventorganization->enabled) && empty($user->eventorganization->read)) {
214
							$qualified = 0;
215
						}
216
						if (!preg_match('/^system/', $obj->type) && isset($conf->{$obj->module}) && empty($conf->{$obj->module}->enabled)) {
217
							$qualified = 0;
218
						}
219
					}
220
221
					if ($qualified) {
222
						$keyfortrans = '';
223
						$transcode = '';
224
						$code = $obj->code;
225
						$typecalendar = $obj->type;
226
227
						if ($onlyautoornot > 0 && $typecalendar == 'system') {
228
							$code = 'AC_MANUAL';
229
						} elseif ($onlyautoornot > 0 && $typecalendar == 'systemauto') {
230
							$code = 'AC_AUTO';
231
						} elseif ($onlyautoornot > 0) {
232
							$code = 'AC_'.strtoupper($obj->module);
233
						}
234
235
						if ($shortlabel) {
236
							$keyfortrans = "Action".$code.'Short';
237
							$transcode = $langs->trans($keyfortrans);
238
						}
239
						if (empty($keyfortrans) || $keyfortrans == $transcode) {
240
							$keyfortrans = "Action".$code;
241
							$transcode = $langs->trans($keyfortrans);
242
						}
243
						$label = (($transcode != $keyfortrans) ? $transcode : $langs->trans($obj->label));
244
						if ($onlyautoornot == -1 && !empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
245
							if ($typecalendar == 'system') {
246
								$label = '&nbsp;&nbsp; '.$label;
247
								$repid[-99] = $langs->trans("ActionAC_MANUAL");
248
								$repcode['AC_NON_AUTO'] = '-- '.$langs->trans("ActionAC_MANUAL");
249
							}
250
							if ($typecalendar == 'systemauto') {
251
								$label = '&nbsp;&nbsp; '.$label;
252
								$repid[-98] = $langs->trans("ActionAC_AUTO");
253
								$repcode['AC_ALL_AUTO'] = '-- '.$langs->trans("ActionAC_AUTO");
254
							}
255
							if ($typecalendar == 'module') {
256
								//TODO check if possible to push it between system and systemauto
257
								if (preg_match('/@/', $obj->module)) {
258
									$module = explode('@', $obj->module)[1];
259
								} else {
260
									$module = $obj->module;
261
								}
262
								$label = '&nbsp;&nbsp; '.$label;
263
								if (!isset($repcode['AC_ALL_'.strtoupper($module)])) {	// If first time for this module
264
									$idforallfornewmodule--;
265
								}
266
								$repid[$idforallfornewmodule] = $langs->trans("ActionAC_ALL_".strtoupper($module));
267
								$repcode['AC_ALL_'.strtoupper($module)] = '-- '.$langs->trans("Module").' '.ucfirst($module);
268
							}
269
						}
270
						$repid[$obj->id] = $label;
271
						$repcode[$obj->code] = $label;
272
						$repall[$obj->code] = array('id' => $label, 'label' => $label, 'type' => $typecalendar, 'color' => $obj->color, 'picto' => $obj->picto);
273
						if ($onlyautoornot > 0 && preg_match('/^module/', $obj->type) && $obj->module) {
274
							$repcode[$obj->code] .= ' ('.$langs->trans("Module").': '.$obj->module.')';
275
							$repall[$obj->code]['label'] .= ' ('.$langs->trans("Module").': '.$obj->module.')';
276
						}
277
					}
278
					$i++;
279
				}
280
			}
281
282
			if ($idorcode == 'id') {
283
				$this->liste_array = $repid;
284
			} elseif ($idorcode == 'code') {
285
				$this->liste_array = $repcode;
286
			} else {
287
				$this->liste_array = $repall;
288
			}
289
290
			return $this->liste_array;
291
		} else {
292
			$this->error = $this->db->lasterror();
293
			return -1;
294
		}
295
	}
296
297
298
	/**
299
	 *  Return name of action type as a label translated
300
	 *
301
	 *	@param	int		$withpicto		0=No picto, 1=Include picto into link, 2=Picto only
302
	 *  @return string			      	Label of action type
303
	 */
304
	public function getNomUrl($withpicto = 0)
305
	{
306
		global $langs;
307
308
		// Check if translation available
309
		$transcode = $langs->trans("Action".$this->code);
310
		if ($transcode != "Action".$this->code) {
311
			return $transcode;
312
		}
313
	}
314
}
315