1<?php
2/* Copyright (C) 2013-2018	Jean-François Ferry	<hello+jf@librethic.io>
3 * Copyright (C) 2016		Gilles Poirier 		<glgpoirier@gmail.com>
4 * Copyright (C) 2019		Josep Lluís Amador	<joseplluis@lliuretic.cat>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/**
21 *      \file       resource/element_resource.php
22 *      \ingroup    resource
23 *      \brief      Page to show and manage linked resources to an element
24 */
25
26
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
31if (!empty($conf->projet->enabled)) {
32	require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
33	require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
34}
35if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) {
36	require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
37}
38
39// Load translation files required by the page
40$langs->loadLangs(array('resource', 'other', 'interventions'));
41
42/*
43$sortorder                      = GETPOST('sortorder','alpha');
44$sortfield                      = GETPOST('sortfield','alpha');
45$page                           = GETPOST('page','int');
46*/
47
48if (!$user->rights->resource->read)
49		accessforbidden();
50
51$object = new Dolresource($db);
52
53$hookmanager->initHooks(array('element_resource'));
54$object->available_resources = array('dolresource');
55
56// Get parameters
57$id                     = GETPOST('id', 'int'); // resource id
58$element_id             = GETPOST('element_id', 'int'); // element_id
59$element_ref            = GETPOST('ref', 'alpha'); // element ref
60$element                = GETPOST('element', 'alpha'); // element_type
61$action                 = GETPOST('action', 'alpha');
62$mode                   = GETPOST('mode', 'alpha');
63$lineid                 = GETPOST('lineid', 'int');
64$resource_id            = GETPOST('fk_resource', 'int');
65$resource_type          = GETPOST('resource_type', 'alpha');
66$busy                   = GETPOST('busy', 'int');
67$mandatory              = GETPOST('mandatory', 'int');
68$cancel                 = GETPOST('cancel', 'alpha');
69$confirm                = GETPOST('confirm', 'alpha');
70$socid                  = GETPOST('socid', 'int');
71
72if ($socid > 0) // Special for thirdparty
73{
74	$element_id = $socid;
75	$element = 'societe';
76}
77
78
79
80/*
81 * Actions
82 */
83
84$parameters = array('resource_id' => $resource_id);
85$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
86if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
87
88if (empty($reshook))
89{
90	$error = 0;
91
92	if ($action == 'add_element_resource' && !$cancel)
93	{
94		$res = 0;
95		if (!($resource_id > 0))
96		{
97			$error++;
98			setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Resource")), null, 'errors');
99			$action = '';
100		} else {
101			$objstat = fetchObjectByElement($element_id, $element, $element_ref);
102			$objstat->element = $element; // For externals module, we need to keep @xx
103
104			// TODO : add this check at update_linked_resource and when modifying event start or end date
105			// check if an event resource is already in use
106			if (!empty($conf->global->RESOURCE_USED_IN_EVENT_CHECK) && $objstat->element == 'action' && $resource_type == 'dolresource' && intval($busy) == 1) {
107				$eventDateStart = $objstat->datep;
108				$eventDateEnd   = $objstat->datef;
109				$isFullDayEvent = intval($objstat->fulldayevent);
110				if (empty($eventDateEnd)) {
111					if ($isFullDayEvent) {
112						$eventDateStartArr = dol_getdate($eventDateStart);
113						$eventDateStart = dol_mktime(0, 0, 0, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
114						$eventDateEnd   = dol_mktime(23, 59, 59, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
115					}
116				}
117
118				$sql  = "SELECT er.rowid, r.ref as r_ref, ac.id as ac_id, ac.label as ac_label";
119				$sql .= " FROM ".MAIN_DB_PREFIX."element_resources as er";
120				$sql .= " INNER JOIN ".MAIN_DB_PREFIX."resource as r ON r.rowid = er.resource_id AND er.resource_type = '".$db->escape($resource_type)."'";
121				$sql .= " INNER JOIN ".MAIN_DB_PREFIX."actioncomm as ac ON ac.id = er.element_id AND er.element_type = '".$db->escape($objstat->element)."'";
122				$sql .= " WHERE er.resource_id = ".$resource_id;
123				$sql .= " AND er.busy = 1";
124				$sql .= " AND (";
125
126				// event date start between ac.datep and ac.datep2 (if datep2 is null we consider there is no end)
127				$sql .= " (ac.datep <= '".$db->idate($eventDateStart)."' AND (ac.datep2 IS NULL OR ac.datep2 >= '".$db->idate($eventDateStart)."'))";
128				// event date end between ac.datep and ac.datep2
129				if (!empty($eventDateEnd)) {
130					$sql .= " OR (ac.datep <= '".$db->idate($eventDateEnd)."' AND (ac.datep2 >= '".$db->idate($eventDateEnd)."'))";
131				}
132				// event date start before ac.datep and event date end after ac.datep2
133				$sql .= " OR (";
134				$sql .= "ac.datep >= '".$db->idate($eventDateStart)."'";
135				if (!empty($eventDateEnd)) {
136					$sql .= " AND (ac.datep2 IS NOT NULL AND ac.datep2 <= '".$db->idate($eventDateEnd)."')";
137				}
138				$sql .= ")";
139
140				$sql .= ")";
141				$resql = $db->query($sql);
142				if (!$resql) {
143					$error++;
144					$objstat->error    = $db->lasterror();
145					$objstat->errors[] = $objstat->error;
146				} else {
147					if ($db->num_rows($resql) > 0) {
148						// already in use
149						$error++;
150						$objstat->error = $langs->trans('ErrorResourcesAlreadyInUse').' : ';
151						while ($obj = $db->fetch_object($resql)) {
152							$objstat->error .= '<br> - '.$langs->trans('ErrorResourceUseInEvent', $obj->r_ref, $obj->ac_label.' ['.$obj->ac_id.']');
153						}
154						$objstat->errors[] = $objstat->error;
155					}
156					$db->free($resql);
157				}
158			}
159
160			if (!$error) {
161				$res = $objstat->add_element_resource($resource_id, $resource_type, $busy, $mandatory);
162			}
163		}
164
165		if (!$error && $res > 0)
166		{
167			setEventMessages($langs->trans('ResourceLinkedWithSuccess'), null, 'mesgs');
168			header("Location: ".$_SERVER['PHP_SELF'].'?element='.$element.'&element_id='.$objstat->id);
169			exit;
170		} elseif ($objstat)
171		{
172			setEventMessages($objstat->error, $objstat->errors, 'errors');
173		}
174	}
175
176	// Update ressource
177	if ($action == 'update_linked_resource' && $user->rights->resource->write && !GETPOST('cancel', 'alpha'))
178	{
179		$res = $object->fetch_element_resource($lineid);
180		if ($res)
181		{
182			$object->busy = $busy;
183			$object->mandatory = $mandatory;
184
185			if (!empty($conf->global->RESOURCE_USED_IN_EVENT_CHECK) && $object->element_type == 'action' && $object->resource_type == 'dolresource' && intval($object->busy) == 1) {
186				$eventDateStart = $object->objelement->datep;
187				$eventDateEnd   = $object->objelement->datef;
188				$isFullDayEvent = intval($objstat->fulldayevent);
189				if (empty($eventDateEnd)) {
190					if ($isFullDayEvent) {
191						$eventDateStartArr = dol_getdate($eventDateStart);
192						$eventDateStart = dol_mktime(0, 0, 0, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
193						$eventDateEnd   = dol_mktime(23, 59, 59, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
194					}
195				}
196
197				$sql  = "SELECT er.rowid, r.ref as r_ref, ac.id as ac_id, ac.label as ac_label";
198				$sql .= " FROM ".MAIN_DB_PREFIX."element_resources as er";
199				$sql .= " INNER JOIN ".MAIN_DB_PREFIX."resource as r ON r.rowid = er.resource_id AND er.resource_type = '".$db->escape($object->resource_type)."'";
200				$sql .= " INNER JOIN ".MAIN_DB_PREFIX."actioncomm as ac ON ac.id = er.element_id AND er.element_type = '".$db->escape($object->element_type)."'";
201				$sql .= " WHERE er.resource_id = ".$object->resource_id;
202				$sql .= " AND ac.id != ".$object->element_id;
203				$sql .= " AND er.busy = 1";
204				$sql .= " AND (";
205
206				// event date start between ac.datep and ac.datep2 (if datep2 is null we consider there is no end)
207				$sql .= " (ac.datep <= '".$db->idate($eventDateStart)."' AND (ac.datep2 IS NULL OR ac.datep2 >= '".$db->idate($eventDateStart)."'))";
208				// event date end between ac.datep and ac.datep2
209				if (!empty($eventDateEnd)) {
210					$sql .= " OR (ac.datep <= '".$db->idate($eventDateEnd)."' AND (ac.datep2 IS NULL OR ac.datep2 >= '".$db->idate($eventDateEnd)."'))";
211				}
212				// event date start before ac.datep and event date end after ac.datep2
213				$sql .= " OR (";
214				$sql .= "ac.datep >= '".$db->idate($eventDateStart)."'";
215				if (!empty($eventDateEnd)) {
216					$sql .= " AND (ac.datep2 IS NOT NULL AND ac.datep2 <= '".$db->idate($eventDateEnd)."')";
217				}
218				$sql .= ")";
219
220				$sql .= ")";
221				$resql = $db->query($sql);
222				if (!$resql) {
223					$error++;
224					$object->error    = $db->lasterror();
225					$object->errors[] = $object->error;
226				} else {
227					if ($db->num_rows($resql) > 0) {
228						// already in use
229						$error++;
230						$object->error = $langs->trans('ErrorResourcesAlreadyInUse').' : ';
231						while ($obj = $db->fetch_object($resql)) {
232							$object->error .= '<br> - '.$langs->trans('ErrorResourceUseInEvent', $obj->r_ref, $obj->ac_label.' ['.$obj->ac_id.']');
233						}
234						$object->errors[] = $objstat->error;
235					}
236					$db->free($resql);
237				}
238			}
239
240			if (!$error) {
241				$result = $object->update_element_resource($user);
242				if ($result < 0)    $error++;
243			}
244
245			if ($error) {
246				setEventMessages($object->error, $object->errors, 'errors');
247			} else {
248				setEventMessages($langs->trans('RessourceLineSuccessfullyUpdated'), null, 'mesgs');
249				header("Location: ".$_SERVER['PHP_SELF']."?element=".$element."&element_id=".$element_id);
250				exit;
251			}
252		}
253	}
254
255	// Delete a resource linked to an element
256	if ($action == 'confirm_delete_linked_resource' && $user->rights->resource->delete && $confirm === 'yes')
257	{
258		$result = $object->delete_resource($lineid, $element);
259
260		if ($result >= 0)
261		{
262			setEventMessages($langs->trans('RessourceLineSuccessfullyDeleted'), null, 'mesgs');
263			header("Location: ".$_SERVER['PHP_SELF']."?element=".$element."&element_id=".$element_id);
264			exit;
265		} else {
266			setEventMessages($object->error, $object->errors, 'errors');
267		}
268	}
269}
270
271$parameters = array('resource_id'=>$resource_id);
272$reshook = $hookmanager->executeHooks('getElementResources', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
273if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
274
275
276
277/*
278 * View
279 */
280
281$form = new Form($db);
282
283$pagetitle = $langs->trans('ResourceElementPage');
284llxHeader('', $pagetitle, '');
285
286
287// Load available resource, declared by modules
288$ret = count($object->available_resources);
289if ($ret == -1) {
290	dol_print_error($db, $object->error);
291	exit;
292}
293if (!$ret) {
294	print '<div class="warning">'.$langs->trans('NoResourceInDatabase').'</div>';
295} else {
296	// Confirmation suppression resource line
297	if ($action == 'delete_resource')
298	{
299		print $form->formconfirm("element_resource.php?element=".$element."&element_id=".$element_id."&id=".$id."&lineid=".$lineid, $langs->trans("DeleteResource"), $langs->trans("ConfirmDeleteResourceElement"), "confirm_delete_linked_resource", '', '', 1);
300	}
301
302
303	// Specific to agenda module
304	if (($element_id || $element_ref) && $element == 'action')
305	{
306		require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php';
307
308		$act = fetchObjectByElement($element_id, $element, $element_ref);
309		if (is_object($act))
310		{
311			$head = actions_prepare_head($act);
312
313			print dol_get_fiche_head($head, 'resources', $langs->trans("Action"), -1, 'action');
314
315			$linkback = img_picto($langs->trans("BackToList"), 'object_list', 'class="hideonsmartphone pictoactionview"');
316			$linkback .= '<a href="'.DOL_URL_ROOT.'/comm/action/list.php?action=show_list">'.$langs->trans("BackToList").'</a>';
317
318			// Link to other agenda views
319			$out = '';
320			$out .= '</li><li class="noborder litext">'.img_picto($langs->trans("ViewPerUser"), 'object_calendarperuser', 'class="hideonsmartphone pictoactionview"');
321			$out .= '<a href="'.DOL_URL_ROOT.'/comm/action/peruser.php?action=show_peruser&year='.dol_print_date($act->datep, '%Y').'&month='.dol_print_date($act->datep, '%m').'&day='.dol_print_date($act->datep, '%d').'">'.$langs->trans("ViewPerUser").'</a>';
322			$out .= '</li><li class="noborder litext">'.img_picto($langs->trans("ViewCal"), 'object_calendar', 'class="hideonsmartphone pictoactionview"');
323			$out .= '<a href="'.DOL_URL_ROOT.'/comm/action/index.php?action=show_month&year='.dol_print_date($act->datep, '%Y').'&month='.dol_print_date($act->datep, '%m').'&day='.dol_print_date($act->datep, '%d').'">'.$langs->trans("ViewCal").'</a>';
324			$out .= '</li><li class="noborder litext">'.img_picto($langs->trans("ViewWeek"), 'object_calendarweek', 'class="hideonsmartphone pictoactionview"');
325			$out .= '<a href="'.DOL_URL_ROOT.'/comm/action/index.php?action=show_day&year='.dol_print_date($act->datep, '%Y').'&month='.dol_print_date($act->datep, '%m').'&day='.dol_print_date($act->datep, '%d').'">'.$langs->trans("ViewWeek").'</a>';
326			$out .= '</li><li class="noborder litext">'.img_picto($langs->trans("ViewDay"), 'object_calendarday', 'class="hideonsmartphone pictoactionview"');
327			$out .= '<a href="'.DOL_URL_ROOT.'/comm/action/index.php?action=show_day&year='.dol_print_date($act->datep, '%Y').'&month='.dol_print_date($act->datep, '%m').'&day='.dol_print_date($act->datep, '%d').'">'.$langs->trans("ViewDay").'</a>';
328
329			$linkback .= $out;
330
331			$morehtmlref = '<div class="refidno">';
332			// Thirdparty
333			//$morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . $object->thirdparty->getNomUrl(1);
334			// Project
335			if (!empty($conf->projet->enabled))
336			{
337				$langs->load("projects");
338				//$morehtmlref.='<br>'.$langs->trans('Project') . ' ';
339				$morehtmlref .= $langs->trans('Project').': ';
340				if (!empty($act->fk_project)) {
341					$proj = new Project($db);
342					$proj->fetch($act->fk_project);
343					$morehtmlref .= '<a href="'.DOL_URL_ROOT.'/projet/card.php?id='.$act->fk_project.'" title="'.$langs->trans('ShowProject').'">';
344					$morehtmlref .= $proj->ref;
345					$morehtmlref .= '</a>';
346					if ($proj->title) $morehtmlref .= ' - '.$proj->title;
347				} else {
348					$morehtmlref .= '';
349				}
350			}
351			$morehtmlref .= '</div>';
352
353			dol_banner_tab($act, 'element_id', $linkback, ($user->socid ? 0 : 1), 'id', 'ref', $morehtmlref, '&element='.$element, 0, '', '');
354
355			print '<div class="fichecenter">';
356
357			print '<div class="underbanner clearboth"></div>';
358
359			print '<table class="border tableforfield" width="100%">';
360
361			// Type
362			if (!empty($conf->global->AGENDA_USE_EVENT_TYPE))
363			{
364				print '<tr><td class="titlefield">'.$langs->trans("Type").'</td><td colspan="3">'.$act->type.'</td></tr>';
365			}
366
367			// Full day event
368			print '<tr><td class="titlefield">'.$langs->trans("EventOnFullDay").'</td><td colspan="3">'.yn($act->fulldayevent, 3).'</td></tr>';
369
370			// Date start
371			print '<tr><td>'.$langs->trans("DateActionStart").'</td><td colspan="3">';
372			if (!$act->fulldayevent) print dol_print_date($act->datep, 'dayhour');
373			else print dol_print_date($act->datep, 'day');
374			if ($act->percentage == 0 && $act->datep && $act->datep < ($now - $delay_warning)) print img_warning($langs->trans("Late"));
375			print '</td>';
376			print '</tr>';
377
378			// Date end
379			print '<tr><td>'.$langs->trans("DateActionEnd").'</td><td colspan="3">';
380			if (!$act->fulldayevent) print dol_print_date($act->datef, 'dayhour');
381			else print dol_print_date($act->datef, 'day');
382			if ($act->percentage > 0 && $act->percentage < 100 && $act->datef && $act->datef < ($now - $delay_warning)) print img_warning($langs->trans("Late"));
383			print '</td></tr>';
384
385			// Location
386			if (empty($conf->global->AGENDA_DISABLE_LOCATION))
387			{
388				print '<tr><td>'.$langs->trans("Location").'</td><td colspan="3">'.$act->location.'</td></tr>';
389			}
390
391			// Assigned to
392			print '<tr><td class="nowrap">'.$langs->trans("ActionAffectedTo").'</td><td colspan="3">';
393			$listofuserid = array();
394			if (empty($donotclearsession))
395			{
396				if ($act->userownerid > 0) $listofuserid[$act->userownerid] = array('id'=>$act->userownerid, 'transparency'=>$act->transparency); // Owner first
397				if (!empty($act->userassigned))	// Now concat assigned users
398				{
399					// Restore array with key with same value than param 'id'
400					$tmplist1 = $act->userassigned; $tmplist2 = array();
401					foreach ($tmplist1 as $key => $val)
402					{
403						if ($val['id'] && $val['id'] != $act->userownerid) $listofuserid[$val['id']] = $val;
404					}
405				}
406				$_SESSION['assignedtouser'] = json_encode($listofuserid);
407			} else {
408				if (!empty($_SESSION['assignedtouser']))
409				{
410					$listofuserid = json_decode($_SESSION['assignedtouser'], true);
411				}
412			}
413			$listofcontactid = array(); // not used yet
414			$listofotherid = array(); // not used yet
415			print '<div class="assignedtouser">';
416			print $form->select_dolusers_forevent('view', 'assignedtouser', 1, '', 0, '', '', 0, 0, 0, '', ($act->datep != $act->datef) ? 1 : 0, $listofuserid, $listofcontactid, $listofotherid);
417			print '</div>';
418			/*if (in_array($user->id,array_keys($listofuserid)))
419			{
420				print '<div class="myavailability">';
421				print $langs->trans("MyAvailability").': '.(($act->userassigned[$user->id]['transparency'] > 0)?$langs->trans("Busy"):$langs->trans("Available"));	// We show nothing if event is assigned to nobody
422				print '</div>';
423			}*/
424			print '	</td></tr>';
425
426			print '</table>';
427
428			print '</div>';
429
430			print dol_get_fiche_end();
431		}
432	}
433
434	// Specific to thirdparty module
435	if (($element_id || $element_ref) && $element == 'societe')
436	{
437		$socstatic = fetchObjectByElement($element_id, $element, $element_ref);
438		if (is_object($socstatic)) {
439			$savobject = $object;
440			$object = $socstatic;
441
442			require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
443			$head = societe_prepare_head($socstatic);
444
445			print dol_get_fiche_head($head, 'resources', $langs->trans("ThirdParty"), -1, 'company');
446
447			dol_banner_tab($socstatic, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom', '', '&element='.$element);
448
449			print '<div class="fichecenter">';
450
451			print '<div class="underbanner clearboth"></div>';
452			print '<table class="border centpercent">';
453
454			// Alias name (commercial, trademark or alias name)
455			print '<tr><td class="titlefield">'.$langs->trans('AliasNames').'</td><td colspan="3">';
456			print $socstatic->name_alias;
457			print "</td></tr>";
458
459			print '</table>';
460
461			print '</div>';
462
463			print dol_get_fiche_end();
464
465			$object = $savobject;
466		}
467	}
468
469	// Specific to fichinter module
470	if (($element_id || $element_ref) && $element == 'fichinter')
471	{
472		require_once DOL_DOCUMENT_ROOT.'/core/lib/fichinter.lib.php';
473
474		$fichinter = new Fichinter($db);
475		$fichinter->fetch($element_id, $element_ref);
476		$fichinter->fetch_thirdparty();
477
478		if (is_object($fichinter))
479		{
480			$head = fichinter_prepare_head($fichinter);
481			print dol_get_fiche_head($head, 'resource', $langs->trans("InterventionCard"), -1, 'intervention');
482
483			// Intervention card
484			$linkback = '<a href="'.DOL_URL_ROOT.'/fichinter/list.php'.(!empty($socid) ? '?socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
485
486
487			$morehtmlref = '<div class="refidno">';
488			// Ref customer
489			//$morehtmlref.=$form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', 0, 1);
490			//$morehtmlref.=$form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', null, null, '', 1);
491			// Thirdparty
492			$morehtmlref .= $langs->trans('ThirdParty').' : '.$fichinter->thirdparty->getNomUrl(1);
493			// Project
494			if (!empty($conf->projet->enabled))
495			{
496				$langs->load("projects");
497				$morehtmlref .= '<br>'.$langs->trans('Project').' ';
498				if ($user->rights->commande->creer)
499				{
500					if ($action != 'classify')
501						//$morehtmlref.='<a class="editfielda" href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $fichinter->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
502						$morehtmlref .= ' : ';
503					if ($action == 'classify') {
504						//$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $fichinter->id, $fichinter->socid, $fichinter->fk_project, 'projectid', 0, 0, 1, 1);
505						$morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$fichinter->id.'">';
506						$morehtmlref .= '<input type="hidden" name="action" value="classin">';
507						$morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
508						$morehtmlref .= $formproject->select_projects($fichinter->socid, $fichinter->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
509						$morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
510						$morehtmlref .= '</form>';
511					} else {
512						$morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$fichinter->id, $fichinter->socid, $fichinter->fk_project, 'none', 0, 0, 0, 1);
513					}
514				} else {
515					if (!empty($fichinter->fk_project)) {
516						$proj = new Project($db);
517						$proj->fetch($fichinter->fk_project);
518						$morehtmlref .= '<a href="'.DOL_URL_ROOT.'/projet/card.php?id='.$fichinter->fk_project.'" title="'.$langs->trans('ShowProject').'">';
519						$morehtmlref .= $proj->ref;
520						$morehtmlref .= '</a>';
521					} else {
522						$morehtmlref .= '';
523					}
524				}
525			}
526			$morehtmlref .= '</div>';
527
528			dol_banner_tab($fichinter, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '&element='.$element, 0, '', '', 1);
529
530			print dol_get_fiche_end();
531		}
532	}
533
534	// Specific to product/service module
535	if (($element_id || $element_ref) && ($element == 'product' || $element == 'service'))
536	{
537		require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
538
539		$product = new Product($db);
540		$product->fetch($element_id, $element_ref);
541
542		if (is_object($product))
543		{
544			$head = product_prepare_head($product);
545			$titre = $langs->trans("CardProduct".$product->type);
546			$picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
547
548			print dol_get_fiche_head($head, 'resources', $titre, -1, $picto);
549
550			$shownav = 1;
551			if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) $shownav = 0;
552			dol_banner_tab($product, 'ref', '', $shownav, 'ref', 'ref', '', '&element='.$element);
553
554			print dol_get_fiche_end();
555		}
556	}
557
558
559	// hook for other elements linked
560	$parameters = array('element'=>$element, 'element_id'=>$element_id, 'element_ref'=>$element_ref);
561	$reshook = $hookmanager->executeHooks('printElementTab', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
562	if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
563
564
565	//print load_fiche_titre($langs->trans('ResourcesLinkedToElement'),'','');
566	print '<br>';
567
568	// Show list of resource links
569
570	foreach ($object->available_resources as $modresources => $resources)
571	{
572		$resources = (array) $resources; // To be sure $resources is an array
573		foreach ($resources as $resource_obj)
574		{
575			$element_prop = getElementProperties($resource_obj);
576
577			//print '/'.$modresources.'/class/'.$resource_obj.'.class.php<br>';
578
579			$path = '';
580			if (strpos($resource_obj, '@'))
581				$path .= '/'.$element_prop['module'];
582
583			$linked_resources = $object->getElementResources($element, $element_id, $resource_obj);
584
585			// Output template part (modules that overwrite templates must declare this into descriptor)
586			$defaulttpldir = '/core/tpl';
587			$dirtpls = array_merge($conf->modules_parts['tpl'], array($defaulttpldir), array($path.$defaulttpldir));
588
589			foreach ($dirtpls as $module => $reldir)
590			{
591				if (file_exists(dol_buildpath($reldir.'/resource_'.$element_prop['element'].'_add.tpl.php')))
592				{
593					$tpl = dol_buildpath($reldir.'/resource_'.$element_prop['element'].'_add.tpl.php');
594				} else {
595					$tpl = DOL_DOCUMENT_ROOT.$reldir.'/resource_add.tpl.php';
596				}
597				if (empty($conf->file->strict_mode)) {
598					$res = @include $tpl;
599				} else {
600					$res = include $tpl; // for debug
601				}
602				if ($res) break;
603			}
604
605			if ($mode != 'add' || $resource_obj != $resource_type)
606			{
607				foreach ($dirtpls as $module => $reldir)
608				{
609					if (file_exists(dol_buildpath($reldir.'/resource_'.$element_prop['element'].'_view.tpl.php')))
610					{
611						$tpl = dol_buildpath($reldir.'/resource_'.$element_prop['element'].'_view.tpl.php');
612					} else {
613						$tpl = DOL_DOCUMENT_ROOT.$reldir.'/resource_view.tpl.php';
614					}
615					if (empty($conf->file->strict_mode)) {
616						$res = @include $tpl;
617					} else {
618						$res = include $tpl; // for debug
619					}
620					if ($res) break;
621				}
622			}
623		}
624	}
625}
626
627// End of page
628llxFooter();
629$db->close();
630