1<?php
2/* Copyright (C) 2007-2017 Laurent Destailleur  <eldy@users.sourceforge.net>
3 * Copyright (C) 2020 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
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       workstation_list.php
21 *		\ingroup    workstation
22 *		\brief      List page for workstation
23 */
24
25// Load Dolibarr environment
26require '../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/resource/class/html.formresource.class.php';
31require_once DOL_DOCUMENT_ROOT.'/workstation/class/workstation.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array("workstation", "other"));
35
36$action     = GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : 'view'; // The action 'add', 'create', 'edit', 'update', 'view', ...
37$massaction = GETPOST('massaction', 'alpha'); // The bulk action (combo box choice into lists)
38$show_files = GETPOST('show_files', 'int'); // Show files area generated by bulk actions ?
39$confirm    = GETPOST('confirm', 'alpha'); // Result of a confirmation
40$cancel     = GETPOST('cancel', 'alpha'); // We click on a Cancel button
41$toselect   = GETPOST('toselect', 'array'); // Array of ids of elements selected into a list
42$contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'workstationlist'; // To manage different context of search
43$backtopage = GETPOST('backtopage', 'alpha'); // Go back to a dedicated page
44$optioncss  = GETPOST('optioncss', 'aZ'); // Option for the css output (always '' except when 'print')
45
46$id = GETPOST('id', 'int');
47
48// Load variable for pagination
49$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
50$sortfield = GETPOST('sortfield', 'aZ09comma');
51$sortorder = GETPOST('sortorder', 'aZ09comma');
52$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
53if (empty($page) || $page < 0 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
54	$page = 0;
55}     // If $page is not defined, or '' or -1 or if we click on clear filters
56$offset = $limit * $page;
57$pageprev = $page - 1;
58$pagenext = $page + 1;
59
60// Initialize technical objects
61$object = new Workstation($db);
62$extrafields = new ExtraFields($db);
63$diroutputmassaction = $conf->workstation->dir_output.'/temp/massgeneration/'.$user->id;
64$hookmanager->initHooks(array('workstationlist')); // Note that conf->hooks_modules contains array
65
66// Fetch optionals attributes and labels
67$extrafields->fetch_name_optionals_label($object->table_element);
68//$extrafields->fetch_name_optionals_label($object->table_element_line);
69
70$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
71
72// Default sort order (if not yet defined by previous GETPOST)
73if (!$sortfield) {
74	reset($object->fields);					// Reset is required to avoid key() to return null.
75	$sortfield = "t.".key($object->fields); // Set here default search field. By default 1st field in definition.
76}
77if (!$sortorder) {
78	$sortorder = "ASC";
79}
80
81// Initialize array of search criterias
82$search_all = GETPOST('search_all', 'alphanohtml') ? GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml');
83$search = array();
84
85foreach ($object->fields as $key => $val) {
86	if (GETPOST('search_'.$key, 'alpha') !== '') {
87		$search[$key] = GETPOST('search_'.$key, 'alpha');
88	}
89	if (preg_match('/^(date|timestamp|datetime)/', $val['type'])) {
90		$search[$key.'_dtstart'] = dol_mktime(0, 0, 0, GETPOST('search_'.$key.'_dtstartmonth', 'int'), GETPOST('search_'.$key.'_dtstartday', 'int'), GETPOST('search_'.$key.'_dtstartyear', 'int'));
91		$search[$key.'_dtend'] = dol_mktime(23, 59, 59, GETPOST('search_'.$key.'_dtendmonth', 'int'), GETPOST('search_'.$key.'_dtendday', 'int'), GETPOST('search_'.$key.'_dtendyear', 'int'));
92	}
93}
94
95$groups = GETPOST('groups', 'array:int');
96$resources = GETPOST('resources', 'array:int');
97
98// List of fields to search into when doing a "search in all"
99$fieldstosearchall = array();
100foreach ($object->fields as $key => $val) {
101	if (!empty($val['searchall'])) {
102		$fieldstosearchall['t.'.$key] = $val['label'];
103	}
104}
105
106// Definition of array of fields for columns
107$arrayfields = array();
108foreach ($object->fields as $key => $val) {
109	// If $val['visible']==0, then we never show the field
110	if (!empty($val['visible'])) {
111		$visible = (int) dol_eval($val['visible'], 1);
112		$arrayfields['t.'.$key] = array(
113			'label'=>$val['label'],
114			'checked'=>(($visible < 0) ? 0 : 1),
115			'enabled'=>($visible != 3 && dol_eval($val['enabled'], 1)),
116			'position'=>$val['position'],
117			'help' => empty($val['help']) ? '' : $val['help']
118		);
119	}
120}
121
122$arrayfields['wug.fk_usergroup'] = array(
123	'label'=>$langs->trans('Groups'),
124	'checked'=>(($visible < 0) ? 0 : 1),
125	'enabled'=>($visible != 3 && dol_eval($val['enabled'], 1)),
126	'position'=>1000,
127	'help' => empty($val['help']) ? '' : $val['help']
128);
129
130$arrayfields['wr.fk_resource'] = array(
131	'label'=>$langs->trans('Resources'),
132	'checked'=>(($visible < 0) ? 0 : 1),
133	'enabled'=>($visible != 3 && dol_eval($val['enabled'], 1)),
134	'position'=>1001,
135	'help' => empty($val['help']) ? '' : $val['help']
136);
137
138// Extra fields
139include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php';
140
141$object->fields = dol_sort_array($object->fields, 'position');
142$arrayfields = dol_sort_array($arrayfields, 'position');
143
144$permissiontoread = $user->rights->workstation->workstation->read;
145$permissiontoadd = $user->rights->workstation->workstation->write;
146$permissiontodelete = $user->rights->workstation->workstation->delete;
147
148// Security check
149restrictedArea($user, $object->element, 0, $object->table_element, 'workstation');
150
151
152/*
153 * Actions
154 */
155
156if (GETPOST('cancel', 'alpha')) {
157	$action = 'list';
158	$massaction = '';
159}
160if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
161	$massaction = '';
162}
163
164$parameters = array();
165$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
166if ($reshook < 0) {
167	setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
168}
169
170if (empty($reshook)) {
171	// Selection of new fields
172	include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
173
174	// Purge search criteria
175	if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
176		foreach ($object->fields as $key => $val) {
177			$search[$key] = '';
178			if (preg_match('/^(date|timestamp|datetime)/', $val['type'])) {
179				$search[$key.'_dtstart'] = '';
180				$search[$key.'_dtend'] = '';
181			}
182		}
183		$groups=$resources=array();
184		$toselect = '';
185		$search_array_options = array();
186	}
187	if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')
188		|| GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha') || GETPOST('button_search', 'alpha')) {
189		$massaction = ''; // Protection to avoid mass action if we force a new search during a mass action confirmation
190	}
191
192	// Mass actions
193	$objectclass = 'Workstation';
194	$objectlabel = 'Workstation';
195	$uploaddir = $conf->workstation->dir_output;
196	include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
197}
198
199
200
201/*
202 * View
203 */
204
205$form = new Form($db);
206$formresource = new FormResource($db);
207
208$now = dol_now();
209
210$title = $langs->trans('ListOf', $langs->transnoentitiesnoconv("Workstations"));
211$help_url = 'EN:Module_Workstation';
212$morejs = array();
213$morecss = array();
214
215
216// Build and execute select
217// --------------------------------------------------------------------
218$sql = 'SELECT ';
219$sql .= $object->getFieldList('t');
220// Add fields from extrafields
221if (!empty($extrafields->attributes[$object->table_element]['label'])) {
222	foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
223		$sql .= ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? ", ef.".$key.' as options_'.$key.', ' : '');
224	}
225}
226// Add fields from hooks
227$parameters = array();
228$reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object); // Note that $action and $object may have been modified by hook
229$sql .= preg_replace('/^,/', '', $hookmanager->resPrint);
230$sql = preg_replace('/,\s*$/', '', $sql);
231$sql .= " FROM ".MAIN_DB_PREFIX.$object->table_element." as t";
232if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
233	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as ef on (t.rowid = ef.fk_object)";
234}
235if (!empty($groups)) {
236	$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'workstation_workstation_usergroup wug ON (wug.fk_workstation = t.rowid)';
237}
238if (!empty($resources)) {
239	$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'workstation_workstation_resource wr ON (wr.fk_workstation = t.rowid)';
240}
241// Add table from hooks
242$parameters = array();
243$reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters, $object); // Note that $action and $object may have been modified by hook
244$sql .= $hookmanager->resPrint;
245if ($object->ismultientitymanaged == 1) {
246	$sql .= " WHERE t.entity IN (".getEntity($object->element).")";
247} else {
248	$sql .= " WHERE 1 = 1";
249}
250foreach ($search as $key => $val) {
251	if (array_key_exists($key, $object->fields)) {
252		if ($key == 'status' && $search[$key] == -1) {
253			continue;
254		}
255		$mode_search = (($object->isInt($object->fields[$key]) || $object->isFloat($object->fields[$key])) ? 1 : 0);
256		if ((strpos($object->fields[$key]['type'], 'integer:') === 0) || (strpos($object->fields[$key]['type'], 'sellist:') === 0) || !empty($object->fields[$key]['arrayofkeyval'])) {
257			if ($search[$key] == '-1' || ($search[$key] === '0' && (empty($object->fields[$key]['arrayofkeyval']) || !array_key_exists('0', $object->fields[$key]['arrayofkeyval'])))) {
258				$search[$key] = '';
259			}
260			$mode_search = 2;
261		}
262		if ($search[$key] != '') {
263			$sql .= natural_search($key, $search[$key], (($key == 'status') ? 2 : $mode_search));
264		}
265	} else {
266		if (preg_match('/(_dtstart|_dtend)$/', $key) && $search[$key] != '') {
267			$columnName = preg_replace('/(_dtstart|_dtend)$/', '', $key);
268			if (preg_match('/^(date|timestamp|datetime)/', $object->fields[$columnName]['type'])) {
269				if (preg_match('/_dtstart$/', $key)) {
270					$sql .= " AND t.".$columnName." >= '".$db->idate($search[$key])."'";
271				}
272				if (preg_match('/_dtend$/', $key)) {
273					$sql .= " AND t." . $columnName . " <= '" . $db->idate($search[$key]) . "'";
274				}
275			}
276		}
277	}
278}
279if ($search_all) {
280	$sql .= natural_search(array_keys($fieldstosearchall), $search_all);
281}
282//$sql.= dolSqlDateFilter("t.field", $search_xxxday, $search_xxxmonth, $search_xxxyear);
283// Add where from extra fields
284
285// usergroups
286if (!empty($groups)) {
287	$sql.= ' AND wug.fk_usergroup IN('.$db->sanitize(implode(',', $groups)).')';
288}
289
290// resources
291if (!empty($resources)) {
292	$sql.= ' AND wr.fk_resource IN('.$db->sanitize(implode(',', $resources)).')';
293}
294
295include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
296// Add where from hooks
297$parameters = array();
298$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $object may have been modified by hook
299$sql .= $hookmanager->resPrint;
300
301$sql.= " GROUP BY ";
302foreach ($object->fields as $key => $val) {
303	$sql.='t.'.$key.', ';
304}
305// Add fields from extrafields
306if (! empty($extrafields->attributes[$object->table_element]['label'])) {
307	foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) $sql.=($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? "ef.".$key.', ' : '');
308}
309// Add where from hooks
310$parameters = array();
311$reshook = $hookmanager->executeHooks('printFieldListGroupBy', $parameters, $object);    // Note that $action and $object may have been modified by hook
312$sql .= $hookmanager->resPrint;
313$sql = preg_replace('/,\s*$/', '', $sql);
314$sql .= $db->order($sortfield, $sortorder);
315//print $sql;
316
317// Count total nb of records
318$nbtotalofrecords = '';
319if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
320	$resql = $db->query($sql);
321	$nbtotalofrecords = $db->num_rows($resql);
322	if (($page * $limit) > $nbtotalofrecords) {	// if total of record found is smaller than page * limit, goto and load page 0
323		$page = 0;
324		$offset = 0;
325	}
326}
327// if total of record found is smaller than limit, no need to do paging and to restart another select with limits set.
328if (is_numeric($nbtotalofrecords) && ($limit > $nbtotalofrecords || empty($limit))) {
329	$num = $nbtotalofrecords;
330} else {
331	if ($limit) {
332		$sql .= $db->plimit($limit + 1, $offset);
333	}
334
335	$resql = $db->query($sql);
336	if (!$resql) {
337		dol_print_error($db);
338		exit;
339	}
340
341	$num = $db->num_rows($resql);
342}
343
344// Direct jump if only one record found
345if ($num == 1 && !empty($conf->global->MAIN_SEARCH_DIRECT_OPEN_IF_ONLY_ONE) && $search_all && !$page) {
346	$obj = $db->fetch_object($resql);
347	$id = $obj->rowid;
348	header("Location: ".dol_buildpath('/workstation/workstation_card.php', 1).'?id='.$id);
349	exit;
350}
351
352
353// Output page
354// --------------------------------------------------------------------
355
356llxHeader('', $title, $help_url, '', 0, 0, $morejs, $morecss, '', 'classforhorizontalscrolloftabs');
357
358$arrayofselected = is_array($toselect) ? $toselect : array();
359
360$param = '';
361if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
362	$param .= '&contextpage='.urlencode($contextpage);
363}
364if ($limit > 0 && $limit != $conf->liste_limit) {
365	$param .= '&limit='.urlencode($limit);
366}
367foreach ($search as $key => $val) {
368	if (is_array($search[$key]) && count($search[$key])) {
369		foreach ($search[$key] as $skey) {
370			$param .= '&search_'.$key.'[]='.urlencode($skey);
371		}
372	} else {
373		$param .= '&search_'.$key.'='.urlencode($search[$key]);
374	}
375}
376if ($optioncss != '') {
377	$param .= '&optioncss='.urlencode($optioncss);
378}
379// Add $param from extra fields
380include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php';
381// Add $param from hooks
382$parameters = array();
383$reshook = $hookmanager->executeHooks('printFieldListSearchParam', $parameters, $object); // Note that $action and $object may have been modified by hook
384$param .= $hookmanager->resPrint;
385
386// List of mass actions available
387$arrayofmassactions = array(
388	//'validate'=>img_picto('', 'check', 'class="pictofixedwidth"').$langs->trans("Validate"),
389	//'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
390	//'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
391	//'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
392);
393if ($permissiontodelete) {
394	$arrayofmassactions['predelete'] = img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete");
395}
396if (GETPOST('nomassaction', 'int') || in_array($massaction, array('presend', 'predelete'))) {
397	$arrayofmassactions = array();
398}
399$massactionbutton = $form->selectMassAction('', $arrayofmassactions);
400
401print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">'."\n";
402if ($optioncss != '') {
403	print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
404}
405print '<input type="hidden" name="token" value="'.newToken().'">';
406print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
407print '<input type="hidden" name="action" value="list">';
408print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
409print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
410print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
411
412$newcardbutton = dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', dol_buildpath('/workstation/workstation_card.php', 1).'?action=create&backtopage='.urlencode($_SERVER['PHP_SELF']), '', $permissiontoadd);
413
414print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'object_'.$object->picto, 0, $newcardbutton, '', $limit, 0, 0, 1);
415
416// Add code for pre mass action (confirmation or email presend form)
417$topicmail = "SendWorkstationRef";
418$modelmail = "workstation";
419$objecttmp = new Workstation($db);
420$trackid = 'xxxx'.$object->id;
421include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
422
423if ($search_all) {
424	foreach ($fieldstosearchall as $key => $val) {
425		$fieldstosearchall[$key] = $langs->trans($val);
426	}
427	print '<div class="divsearchfieldfilter">'.$langs->trans("FilterOnInto", $search_all).join(', ', $fieldstosearchall).'</div>';
428}
429
430$moreforfilter = '';
431/*$moreforfilter.='<div class="divsearchfield">';
432$moreforfilter.= $langs->trans('MyFilter') . ': <input type="text" name="search_myfield" value="'.dol_escape_htmltag($search_myfield).'">';
433$moreforfilter.= '</div>';*/
434
435$parameters = array();
436$reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters, $object); // Note that $action and $object may have been modified by hook
437if (empty($reshook)) {
438	$moreforfilter .= $hookmanager->resPrint;
439} else {
440	$moreforfilter = $hookmanager->resPrint;
441}
442
443if (!empty($moreforfilter)) {
444	print '<div class="liste_titre liste_titre_bydiv centpercent">';
445	print $moreforfilter;
446	print '</div>';
447}
448
449$varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
450$selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
451$selectedfields .= (count($arrayofmassactions) ? $form->showCheckAddButtons('checkforselect', 1) : '');
452
453print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
454print '<table class="tagtable nobottomiftotal liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
455
456
457// Fields title search
458// --------------------------------------------------------------------
459
460print '<tr class="liste_titre">';
461foreach ($object->fields as $key => $val) {
462	$cssforfield = (empty($val['css']) ? '' : $val['css']);
463	if ($key == 'status') {
464		$cssforfield .= ($cssforfield ? ' ' : '').'center';
465	} elseif (in_array($val['type'], array('date', 'datetime', 'timestamp'))) {
466		$cssforfield .= ($cssforfield ? ' ' : '').'center';
467	} elseif (in_array($val['type'], array('timestamp'))) {
468		$cssforfield .= ($cssforfield ? ' ' : '').'nowrap';
469	} elseif (in_array($val['type'], array('double(24,8)', 'double(6,3)', 'integer', 'real', 'price')) && $val['label'] != 'TechnicalID') {
470		$cssforfield .= ($cssforfield ? ' ' : '').'right';
471	}
472	if (!empty($arrayfields['t.'.$key]['checked'])) {
473		print '<td class="liste_titre'.($cssforfield ? ' '.$cssforfield : '').'">';
474		if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
475			print $form->selectarray('search_'.$key, $val['arrayofkeyval'], $search[$key], $val['notnull'], 0, 0, '', 1, 0, 0, '', 'maxwidth100', 1);
476		} elseif ((strpos($val['type'], 'integer:') === 0) || (strpos($val['type'], 'sellist:')=== 0)) {
477			print $object->showInputField($val, $key, $search[$key], '', '', 'search_', 'maxwidth125', 1);
478		} elseif (!preg_match('/^(date|timestamp|datetime)/', $val['type'])) {
479			print '<input type="text" class="flat maxwidth75" name="search_'.$key.'" value="'.dol_escape_htmltag($search[$key]).'">';
480		} elseif (preg_match('/^(date|timestamp|datetime)/', $val['type'])) {
481			print '<div class="nowrap">';
482			print $form->selectDate($search[$key.'_dtstart'] ? $search[$key.'_dtstart'] : '', "search_".$key."_dtstart", 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('From'));
483			print '</div>';
484			print '<div class="nowrap">';
485			print $form->selectDate($search[$key.'_dtend'] ? $search[$key.'_dtend'] : '', "search_".$key."_dtend", 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('to'));
486			print '</div>';
487		}
488		print '</td>';
489	}
490}
491
492// usergroups
493if (!empty($arrayfields['wug.fk_usergroup']['checked'])) {
494	print '<td class="liste_titre">';
495	print $form->select_dolgroups($groups, 'groups', 1, '', 0, '', '', $conf->entity, true);
496	print '</td>';
497}
498
499// resources
500if (!empty($arrayfields['wr.fk_resource']['checked'])) {
501	print '<td class="liste_titre">';
502	print $formresource->select_resource_list($resources, 'resources', '', '', 0, '', '', $conf->entity, true, 0, '', true);
503	print '</td>';
504}
505
506// Extra fields
507include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_input.tpl.php';
508
509// Fields from hook
510$parameters = array('arrayfields'=>$arrayfields);
511$reshook = $hookmanager->executeHooks('printFieldListOption', $parameters, $object); // Note that $action and $object may have been modified by hook
512print $hookmanager->resPrint;
513// Action column
514print '<td class="liste_titre maxwidthsearch">';
515$searchpicto = $form->showFilterButtons();
516print $searchpicto;
517print '</td>';
518print '</tr>'."\n";
519
520
521// Fields title label
522// --------------------------------------------------------------------
523print '<tr class="liste_titre">';
524foreach ($object->fields as $key => $val) {
525	$cssforfield = (empty($val['csslist']) ? (empty($val['css']) ? '' : $val['css']) : $val['csslist']);
526	if ($key == 'status') {
527		$cssforfield .= ($cssforfield ? ' ' : '').'center';
528	} elseif (in_array($val['type'], array('date', 'datetime', 'timestamp'))) {
529		$cssforfield .= ($cssforfield ? ' ' : '').'center';
530	} elseif (in_array($val['type'], array('timestamp'))) {
531		$cssforfield .= ($cssforfield ? ' ' : '').'nowrap';
532	} elseif (in_array($val['type'], array('double(24,8)', 'double(6,3)', 'integer', 'real', 'price')) && $val['label'] != 'TechnicalID') {
533		$cssforfield .= ($cssforfield ? ' ' : '').'right';
534	}
535	if (!empty($arrayfields['t.'.$key]['checked'])) {
536		print getTitleFieldOfList($arrayfields['t.'.$key]['label'], 0, $_SERVER['PHP_SELF'], 't.'.$key, '', $param, ($cssforfield ? 'class="'.$cssforfield.'"' : ''), $sortfield, $sortorder, ($cssforfield ? $cssforfield.' ' : ''))."\n";
537	}
538}
539
540// usergroups
541if (!empty($arrayfields['wug.fk_usergroup']['checked'])) {
542	print getTitleFieldOfList($arrayfields['wug.fk_usergroup']['label'], 0, $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, '') . "\n";
543}
544
545// resources
546if (!empty($arrayfields['wr.fk_resource']['checked'])) {
547	print getTitleFieldOfList($arrayfields['wr.fk_resource']['label'], 0, $_SERVER['PHP_SELF'], '', '', $param, '', $sortfield, $sortorder, '') . "\n";
548}
549
550// Extra fields
551include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_title.tpl.php';
552// Hook fields
553$parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
554$reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object); // Note that $action and $object may have been modified by hook
555print $hookmanager->resPrint;
556// Action column
557print getTitleFieldOfList($selectedfields, 0, $_SERVER["PHP_SELF"], '', '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ')."\n";
558print '</tr>'."\n";
559
560
561// Detect if we need a fetch on each output line
562$needToFetchEachLine = 0;
563if (is_array($extrafields->attributes[$object->table_element]['computed']) && count($extrafields->attributes[$object->table_element]['computed']) > 0) {
564	foreach ($extrafields->attributes[$object->table_element]['computed'] as $key => $val) {
565		if (preg_match('/\$object/', $val)) {
566			$needToFetchEachLine++; // There is at least one compute field that use $object
567		}
568	}
569}
570
571
572// Loop on record
573// --------------------------------------------------------------------
574$i = 0;
575$totalarray = array();
576while ($i < ($limit ? min($num, $limit) : $num)) {
577	$obj = $db->fetch_object($resql);
578	if (empty($obj)) {
579		break; // Should not happen
580	}
581
582	// Store properties in $object
583	$object->setVarsFromFetchObj($obj);
584	$object->usergroups = WorkstationUserGroup::getAllGroupsOfWorkstation($obj->rowid);
585	$object->resources = WorkstationResource::getAllResourcesOfWorkstation($obj->rowid);
586
587	// Show here line of result
588	print '<tr class="oddeven">';
589	foreach ($object->fields as $key => $val) {
590		$cssforfield = (empty($val['csslist']) ? (empty($val['css']) ? '' : $val['css']) : $val['csslist']);
591		if (in_array($val['type'], array('date', 'datetime', 'timestamp'))) {
592			$cssforfield .= ($cssforfield ? ' ' : '').'center';
593		} elseif ($key == 'status') {
594			$cssforfield .= ($cssforfield ? ' ' : '').'center';
595		}
596
597		if (in_array($val['type'], array('timestamp'))) {
598			$cssforfield .= ($cssforfield ? ' ' : '').'nowrap';
599		} elseif ($key == 'ref') {
600			$cssforfield .= ($cssforfield ? ' ' : '').'nowrap';
601		}
602
603		if (in_array($val['type'], array('double(24,8)', 'double(6,3)', 'integer', 'real', 'price')) && !in_array($key, array('rowid', 'status'))) {
604			$cssforfield .= ($cssforfield ? ' ' : '').'right';
605		}
606		//if (in_array($key, array('fk_soc', 'fk_user', 'fk_warehouse'))) $cssforfield = 'tdoverflowmax100';
607
608		if (!empty($arrayfields['t.'.$key]['checked'])) {
609			print '<td'.($cssforfield ? ' class="'.$cssforfield.'"' : '').'>';
610			if ($key == 'status') {
611				print $object->getLibStatut(5);
612			} else {
613				print $object->showOutputField($val, $key, $object->$key, '');
614			}
615			print '</td>';
616			if (!$i) {
617				$totalarray['nbfield']++;
618			}
619			if (!empty($val['isameasure'])) {
620				if (!$i) {
621					$totalarray['pos'][$totalarray['nbfield']] = 't.'.$key;
622				}
623				if (!isset($totalarray['val'])) {
624					$totalarray['val'] = array();
625				}
626				if (!isset($totalarray['val']['t.'.$key])) {
627					$totalarray['val']['t.'.$key] = 0;
628				}
629				$totalarray['val']['t.'.$key] += $object->$key;
630			}
631		}
632	}
633
634	if (!empty($arrayfields['wug.fk_usergroup']['checked'])) {
635		$toprint = array();
636		foreach ($object->usergroups as $id_group) {
637			$g = new UserGroup($db);
638			$g->fetch($id_group);
639			$toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">' . $g->getNomUrl(1) . '</li>';
640		}
641		print '<td>';
642		print '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
643		print '</td>';
644	}
645
646	if (!empty($arrayfields['wr.fk_resource']['checked'])) {
647		$toprint = array();
648		foreach ($object->resources as $id_resource) {
649			$r = new Dolresource($db);
650			$r->fetch($id_resource);
651			$toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">' . $r->getNomUrl(1) . '</li>';
652		}
653		print '<td>';
654		print '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
655		print '</td>';
656	}
657
658	// Extra fields
659	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php';
660	// Fields from hook
661	$parameters = array('arrayfields'=>$arrayfields, 'object'=>$object, 'obj'=>$obj, 'i'=>$i, 'totalarray'=>&$totalarray);
662	$reshook = $hookmanager->executeHooks('printFieldListValue', $parameters, $object); // Note that $action and $object may have been modified by hook
663	print $hookmanager->resPrint;
664	// Action column
665	print '<td class="nowrap center">';
666	if ($massactionbutton || $massaction) {   // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
667		$selected = 0;
668		if (in_array($object->id, $arrayofselected)) {
669			$selected = 1;
670		}
671		print '<input id="cb'.$object->id.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$object->id.'"'.($selected ? ' checked="checked"' : '').'>';
672	}
673	print '</td>';
674	if (!$i) {
675		$totalarray['nbfield']++;
676	}
677
678	print '</tr>'."\n";
679
680	$i++;
681}
682
683// Show total line
684include DOL_DOCUMENT_ROOT.'/core/tpl/list_print_total.tpl.php';
685
686// If no record found
687if ($num == 0) {
688	$colspan = 1;
689	foreach ($arrayfields as $key => $val) {
690		if (!empty($val['checked'])) {
691			$colspan++;
692		}
693	}
694	print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoRecordFound").'</td></tr>';
695}
696
697
698$db->free($resql);
699
700$parameters = array('arrayfields'=>$arrayfields, 'sql'=>$sql);
701$reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters, $object); // Note that $action and $object may have been modified by hook
702print $hookmanager->resPrint;
703
704print '</table>'."\n";
705print '</div>'."\n";
706
707print '</form>'."\n";
708
709if (in_array('builddoc', $arrayofmassactions) && ($nbtotalofrecords === '' || $nbtotalofrecords)) {
710	$hidegeneratedfilelistifempty = 1;
711	if ($massaction == 'builddoc' || $action == 'remove_file' || $show_files) {
712		$hidegeneratedfilelistifempty = 0;
713	}
714
715	require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
716	$formfile = new FormFile($db);
717
718	// Show list of available documents
719	$urlsource = $_SERVER['PHP_SELF'].'?sortfield='.$sortfield.'&sortorder='.$sortorder;
720	$urlsource .= str_replace('&amp;', '&', $param);
721
722	$filedir = $diroutputmassaction;
723	$genallowed = $permissiontoread;
724	$delallowed = $permissiontoadd;
725
726	print $formfile->showdocuments('massfilesarea_workstation', '', $filedir, $urlsource, 0, $delallowed, '', 1, 1, 0, 48, 1, $param, $title, '', '', '', null, $hidegeneratedfilelistifempty);
727}
728
729// End of page
730llxFooter();
731$db->close();
732