Passed
Branch develop (623006)
by
unknown
32:40
created

box_dolibarr_state_board::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 10
rs 10
1
<?php
2
/* Copyright (C) 2003-2007 Rodolphe Quiedeville <[email protected]>
3
 * Copyright (C) 2004-2010 Laurent Destailleur  <[email protected]>
4
 * Copyright (C) 2005-2009 Regis Houssin        <[email protected]>
5
 * Copyright (C) 2015      Frederic France      <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
/**
22
 *	\file       htdocs/core/boxes/box_dolibarr_state_board.php
23
 *	\ingroup
24
 *	\brief      Module Dolibarr state base
25
 */
26
27
include_once DOL_DOCUMENT_ROOT . '/core/boxes/modules_boxes.php';
28
include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
29
30
31
/**
32
 * Class to manage the box to show last thirdparties
33
 */
34
class box_dolibarr_state_board extends ModeleBoxes
35
{
36
	public $boxcode = "dolibarrstatebox";
37
	public $boximg = "box_user";
38
	public $boxlabel = "BoxDolibarrStateBoard";
39
	public $depends = array("user");
40
41
	/**
42
	 * @var DoliDB Database handler.
43
	 */
44
	public $db;
45
46
	public $enabled = 1;
47
48
	public $info_box_head = array();
49
	public $info_box_contents = array();
50
51
52
	/**
53
	 *  Constructor
54
	 *
55
	 *  @param  DoliDB	$db      	Database handler
56
	 *  @param	string	$param		More parameters
57
	 */
58
	public function __construct($db, $param = '')
59
	{
60
		global $conf, $user;
61
62
		$this->db = $db;
63
64
		// disable box for such cases
65
		if (!empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) $this->enabled = 0; // disabled by this option
66
67
		$this->hidden = !($user->rights->societe->lire && empty($user->socid));
68
	}
69
70
	/**
71
	 *  Load data for box to show them later
72
	 *
73
	 *  @param	int		$max        Maximum number of records to load
74
	 *  @return	void
75
	 */
76
	public function loadBox($max = 5)
77
	{
78
		global $user, $langs, $conf;
79
		$langs->load("boxes");
80
81
		$this->max = $max;
82
		$this->info_box_head = array('text' => $langs->trans("DolibarrStateBoard"));
83
84
		if (empty($user->socid) && empty($conf->global->MAIN_DISABLE_GLOBAL_BOXSTATS))
85
		{
86
			$hookmanager = new HookManager($this->db);
87
			$hookmanager->initHooks(array('index'));
88
			$boxstatItems = array();
89
			$boxstatFromHook = '';
90
			$boxstatFromHook = $hookmanager->resPrint;
91
			$boxstat = '';
92
93
			$keys = array(
94
				'users',
95
				'members',
96
				'expensereports',
97
				'holidays',
98
				'customers',
99
				'prospects',
100
				'suppliers',
101
				'contacts',
102
				'products',
103
				'services',
104
				'projects',
105
				'proposals',
106
				'orders',
107
				'invoices',
108
				'donations',
109
				'supplier_proposals',
110
				'supplier_orders',
111
				'supplier_invoices',
112
				'contracts',
113
				'interventions',
114
				'ticket'
115
			);
116
			$conditions = array(
117
				'users' => $user->rights->user->user->lire,
118
				'members' => !empty($conf->adherent->enabled) && $user->rights->adherent->lire,
119
				'customers' => !empty($conf->societe->enabled) && $user->rights->societe->lire && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS_STATS),
120
				'prospects' => !empty($conf->societe->enabled) && $user->rights->societe->lire && empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTS_STATS),
121
				'suppliers' => !empty($conf->fournisseur->enabled) && $user->rights->fournisseur->lire && empty($conf->global->SOCIETE_DISABLE_SUPPLIERS_STATS),
122
				'contacts' => !empty($conf->societe->enabled) && $user->rights->societe->contact->lire,
123
				'products' => !empty($conf->product->enabled) && $user->rights->produit->lire,
124
				'services' => !empty($conf->service->enabled) && $user->rights->service->lire,
125
				'proposals' => !empty($conf->propal->enabled) && $user->rights->propale->lire,
126
				'orders' => !empty($conf->commande->enabled) && $user->rights->commande->lire,
127
				'invoices' => !empty($conf->facture->enabled) && $user->rights->facture->lire,
128
				'donations' => !empty($conf->don->enabled) && $user->rights->don->lire,
129
				'contracts' => !empty($conf->contrat->enabled) && $user->rights->contrat->lire,
130
				'interventions' => !empty($conf->ficheinter->enabled) && $user->rights->ficheinter->lire,
131
				'supplier_orders' => !empty($conf->supplier_order->enabled) && $user->rights->fournisseur->commande->lire && empty($conf->global->SOCIETE_DISABLE_SUPPLIERS_ORDERS_STATS),
132
				'supplier_invoices' => !empty($conf->supplier_invoice->enabled) && $user->rights->fournisseur->facture->lire && empty($conf->global->SOCIETE_DISABLE_SUPPLIERS_INVOICES_STATS),
133
				'supplier_proposals' => !empty($conf->supplier_proposal->enabled) && $user->rights->supplier_proposal->lire && empty($conf->global->SOCIETE_DISABLE_SUPPLIERS_PROPOSAL_STATS),
134
				'projects' => !empty($conf->projet->enabled) && $user->rights->projet->lire,
135
				'expensereports' => !empty($conf->expensereport->enabled) && $user->rights->expensereport->lire,
136
				'holidays' => !empty($conf->holiday->enabled) && $user->rights->holiday->read,
137
				'ticket' => !empty($conf->ticket->enabled) && $user->rights->ticket->read
138
			);
139
			$classes = array(
140
				'users' => 'User',
141
				'members' => 'Adherent',
142
				'customers' => 'Client',
143
				'prospects' => 'Client',
144
				'suppliers' => 'Fournisseur',
145
				'contacts' => 'Contact',
146
				'products' => 'Product',
147
				'services' => 'ProductService',
148
				'proposals' => 'Propal',
149
				'orders' => 'Commande',
150
				'invoices' => 'Facture',
151
				'donations' => 'Don',
152
				'contracts' => 'Contrat',
153
				'interventions' => 'Fichinter',
154
				'supplier_orders' => 'CommandeFournisseur',
155
				'supplier_invoices' => 'FactureFournisseur',
156
				'supplier_proposals' => 'SupplierProposal',
157
				'projects' => 'Project',
158
				'expensereports' => 'ExpenseReport',
159
				'holidays' => 'Holiday',
160
				'ticket' => 'Ticket',
161
			);
162
			$includes = array(
163
				'users' => DOL_DOCUMENT_ROOT . "/user/class/user.class.php",
164
				'members' => DOL_DOCUMENT_ROOT . "/adherents/class/adherent.class.php",
165
				'customers' => DOL_DOCUMENT_ROOT . "/societe/class/client.class.php",
166
				'prospects' => DOL_DOCUMENT_ROOT . "/societe/class/client.class.php",
167
				'suppliers' => DOL_DOCUMENT_ROOT . "/fourn/class/fournisseur.class.php",
168
				'contacts' => DOL_DOCUMENT_ROOT . "/contact/class/contact.class.php",
169
				'products' => DOL_DOCUMENT_ROOT . "/product/class/product.class.php",
170
				'services' => DOL_DOCUMENT_ROOT . "/product/class/product.class.php",
171
				'proposals' => DOL_DOCUMENT_ROOT . "/comm/propal/class/propal.class.php",
172
				'orders' => DOL_DOCUMENT_ROOT . "/commande/class/commande.class.php",
173
				'invoices' => DOL_DOCUMENT_ROOT . "/compta/facture/class/facture.class.php",
174
				'donations' => DOL_DOCUMENT_ROOT . "/don/class/don.class.php",
175
				'contracts' => DOL_DOCUMENT_ROOT . "/contrat/class/contrat.class.php",
176
				'interventions' => DOL_DOCUMENT_ROOT . "/fichinter/class/fichinter.class.php",
177
				'supplier_orders' => DOL_DOCUMENT_ROOT . "/fourn/class/fournisseur.commande.class.php",
178
				'supplier_invoices' => DOL_DOCUMENT_ROOT . "/fourn/class/fournisseur.facture.class.php",
179
				'supplier_proposals' => DOL_DOCUMENT_ROOT . "/supplier_proposal/class/supplier_proposal.class.php",
180
				'projects' => DOL_DOCUMENT_ROOT . "/projet/class/project.class.php",
181
				'expensereports' => DOL_DOCUMENT_ROOT . "/expensereport/class/expensereport.class.php",
182
				'holidays' => DOL_DOCUMENT_ROOT . "/holiday/class/holiday.class.php",
183
				'ticket' => DOL_DOCUMENT_ROOT . "/ticket/class/ticket.class.php"
184
			);
185
			$links = array(
186
				'users' => DOL_URL_ROOT . '/user/list.php',
187
				'members' => DOL_URL_ROOT . '/adherents/list.php?statut=1&mainmenu=members',
188
				'customers' => DOL_URL_ROOT . '/societe/list.php?type=c&mainmenu=companies',
189
				'prospects' => DOL_URL_ROOT . '/societe/list.php?type=p&mainmenu=companies',
190
				'suppliers' => DOL_URL_ROOT . '/societe/list.php?type=f&mainmenu=companies',
191
				'contacts' => DOL_URL_ROOT . '/contact/list.php?mainmenu=companies',
192
				'products' => DOL_URL_ROOT . '/product/list.php?type=0&mainmenu=products',
193
				'services' => DOL_URL_ROOT . '/product/list.php?type=1&mainmenu=products',
194
				'proposals' => DOL_URL_ROOT . '/comm/propal/list.php?mainmenu=commercial&leftmenu=propals',
195
				'orders' => DOL_URL_ROOT . '/commande/list.php?mainmenu=commercial&leftmenu=orders',
196
				'invoices' => DOL_URL_ROOT . '/compta/facture/list.php?mainmenu=billing&leftmenu=customers_bills',
197
				'donations' => DOL_URL_ROOT . '/don/list.php?leftmenu=donations',
198
				'contracts' => DOL_URL_ROOT . '/contrat/list.php?mainmenu=commercial&leftmenu=contracts',
199
				'interventions' => DOL_URL_ROOT . '/fichinter/list.php?mainmenu=commercial&leftmenu=ficheinter',
200
				'supplier_orders' => DOL_URL_ROOT . '/fourn/commande/list.php?mainmenu=commercial&leftmenu=orders_suppliers',
201
				'supplier_invoices' => DOL_URL_ROOT . '/fourn/facture/list.php?mainmenu=billing&leftmenu=suppliers_bills',
202
				'supplier_proposals' => DOL_URL_ROOT . '/supplier_proposal/list.php?mainmenu=commercial&leftmenu=',
203
				'projects' => DOL_URL_ROOT . '/projet/list.php?mainmenu=project',
204
				'expensereports' => DOL_URL_ROOT . '/expensereport/list.php?mainmenu=hrm&leftmenu=expensereport',
205
				'holidays' => DOL_URL_ROOT . '/holiday/list.php?mainmenu=hrm&leftmenu=holiday',
206
				'ticket' => DOL_URL_ROOT . '/ticket/list.php?leftmenu=ticket'
207
			);
208
			$titres = array(
209
				'users' => "Users",
210
				'members' => "Members",
211
				'customers' => "ThirdPartyCustomersStats",
212
				'prospects' => "ThirdPartyProspectsStats",
213
				'suppliers' => "Suppliers",
214
				'contacts' => "Contacts",
215
				'products' => "Products",
216
				'services' => "Services",
217
				'proposals' => "CommercialProposalsShort",
218
				'orders' => "CustomersOrders",
219
				'invoices' => "BillsCustomers",
220
				'donations' => "Donations",
221
				'contracts' => "Contracts",
222
				'interventions' => "Interventions",
223
				'supplier_orders' => "SuppliersOrders",
224
				'supplier_invoices' => "SuppliersInvoices",
225
				'supplier_proposals' => "SupplierProposalShort",
226
				'projects' => "Projects",
227
				'expensereports' => "ExpenseReports",
228
				'holidays' => "Holidays",
229
				'ticket' => "Ticket",
230
			);
231
			$langfile = array(
232
				'customers' => "companies",
233
				'contacts' => "companies",
234
				'services' => "products",
235
				'proposals' => "propal",
236
				'invoices' => "bills",
237
				'supplier_orders' => "orders",
238
				'supplier_invoices' => "bills",
239
				'supplier_proposals' => 'supplier_proposal',
240
				'expensereports' => "trips",
241
				'holidays' => "holiday",
242
			);
243
			$boardloaded = array();
244
245
			foreach ($keys as $val)
246
			{
247
				if ($conditions[$val])
248
				{
249
					$boxstatItem = '';
250
					$class = $classes[$val];
251
					// Search in cache if load_state_board is already realized
252
					$classkeyforcache = $class;
253
					if ($classkeyforcache == 'ProductService') $classkeyforcache = 'Product'; // ProductService use same load_state_board than Product
254
255
					if (!isset($boardloaded[$classkeyforcache]) || !is_object($boardloaded[$classkeyforcache]))
256
					{
257
						include_once $includes[$val]; // Loading a class cost around 1Mb
258
259
						$board = new $class($this->db);
260
						$board->load_state_board();
261
						$boardloaded[$class] = $board;
262
					} else {
263
						$board = $boardloaded[$classkeyforcache];
264
					}
265
266
					$langs->load(empty($langfile[$val]) ? $val : $langfile[$val]);
267
268
					$text = $langs->trans($titres[$val]);
269
					$boxstatItem .= '<a href="' . $links[$val] . '" class="boxstatsindicator thumbstat nobold nounderline">';
270
					$boxstatItem .= '<div class="boxstats">';
271
					$boxstatItem .= '<span class="boxstatstext" title="' . dol_escape_htmltag($text) . '">' . $text . '</span><br>';
272
					$boxstatItem .= '<span class="boxstatsindicator">' . img_object("", $board->picto, 'class="inline-block"') . ' ' . ($board->nb[$val] ? $board->nb[$val] : 0) . '</span>';
273
					$boxstatItem .= '</div>';
274
					$boxstatItem .= '</a>';
275
276
					$boxstatItems[$val] = $boxstatItem;
277
				}
278
			}
279
280
			if (!empty($boxstatFromHook) || !empty($boxstatItems))
281
			{
282
				$boxstat .= $boxstatFromHook;
283
284
				if (is_array($boxstatItems) && count($boxstatItems) > 0)
285
				{
286
					$boxstat .= implode('', $boxstatItems);
287
				}
288
289
				$boxstat .= '<a class="boxstatsindicator thumbstat nobold nounderline"><div class="boxstatsempty"></div></a>';
290
				$boxstat .= '<a class="boxstatsindicator thumbstat nobold nounderline"><div class="boxstatsempty"></div></a>';
291
				$boxstat .= '<a class="boxstatsindicator thumbstat nobold nounderline"><div class="boxstatsempty"></div></a>';
292
				$boxstat .= '<a class="boxstatsindicator thumbstat nobold nounderline"><div class="boxstatsempty"></div></a>';
293
				$boxstat .= '<a class="boxstatsindicator thumbstat nobold nounderline"><div class="boxstatsempty"></div></a>';
294
				$boxstat .= '<a class="boxstatsindicator thumbstat nobold nounderline"><div class="boxstatsempty"></div></a>';
295
				$boxstat .= '<a class="boxstatsindicator thumbstat nobold nounderline"><div class="boxstatsempty"></div></a>';
296
				$boxstat .= '<a class="boxstatsindicator thumbstat nobold nounderline"><div class="boxstatsempty"></div></a>';
297
298
				$boxstat .= '</td></tr>';
299
				$boxstat .= '</table>';
300
301
				$this->info_box_contents[0][0] = array(
302
					'td' => '',
303
					'textnoformat' => $boxstat
304
				);
305
			} else {
306
				$this->info_box_contents[0][0] = array(
307
					'td' => 'class="nohover center"',
308
					'maxlength' => 500,
309
					'text' => ($this->db->error() . ' sql=' . $sql)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sql seems to be never defined.
Loading history...
310
				);
311
			}
312
		} else {
313
			$this->info_box_contents[0][0] = array(
314
				'td' => '',
315
				'text' => $langs->trans("ReadPermissionNotAllowed")
316
			);
317
		}
318
	}
319
320
321
	/**
322
	 *	Method to show box
323
	 *
324
	 *	@param	array	$head       Array with properties of box title
325
	 *	@param  array	$contents   Array with properties of box lines
326
	 *  @param	int		$nooutput	No print, only return string
327
	 *	@return	string
328
	 */
329
	public function showBox($head = null, $contents = null, $nooutput = 0)
330
	{
331
		return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
332
	}
333
}
334