element_resource.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. *
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file resource/element_resource.php
  20. * \ingroup resource
  21. * \brief Page to show and manage linked resources to an element
  22. */
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  27. if (! empty($conf->projet->enabled)) {
  28. require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
  29. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php';
  30. }
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('resource', 'other', 'interventions'));
  33. /*
  34. $sortorder = GETPOST('sortorder','alpha');
  35. $sortfield = GETPOST('sortfield','alpha');
  36. $page = GETPOST('page','int');
  37. */
  38. if( ! $user->rights->resource->read)
  39. accessforbidden();
  40. $object=new Dolresource($db);
  41. $hookmanager->initHooks(array('element_resource'));
  42. $object->available_resources = array('dolresource');
  43. // Get parameters
  44. $id = GETPOST('id','int'); // resource id
  45. $element_id = GETPOST('element_id','int'); // element_id
  46. $element_ref = GETPOST('ref','alpha'); // element ref
  47. $element = GETPOST('element','alpha'); // element_type
  48. $action = GETPOST('action','alpha');
  49. $mode = GETPOST('mode','alpha');
  50. $lineid = GETPOST('lineid','int');
  51. $resource_id = GETPOST('fk_resource','int');
  52. $resource_type = GETPOST('resource_type','alpha');
  53. $busy = GETPOST('busy','int');
  54. $mandatory = GETPOST('mandatory','int');
  55. $cancel = GETPOST('cancel','alpha');
  56. $confirm = GETPOST('confirm','alpha');
  57. $socid = GETPOST('socid','int');
  58. if ($socid > 0) // Special for thirdparty
  59. {
  60. $element_id = $socid;
  61. $element = 'societe';
  62. }
  63. /*
  64. * Actions
  65. */
  66. if ($action == 'add_element_resource' && ! $cancel)
  67. {
  68. $error++;
  69. $res = 0;
  70. if (! ($resource_id > 0))
  71. {
  72. $error++;
  73. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Resource")), null, 'errors');
  74. $action='';
  75. }
  76. else
  77. {
  78. $objstat = fetchObjectByElement($element_id, $element);
  79. $objstat->element = $element; // For externals module, we need to keep @xx
  80. $res = $objstat->add_element_resource($resource_id, $resource_type, $busy, $mandatory);
  81. }
  82. if (! $error && $res > 0)
  83. {
  84. setEventMessages($langs->trans('ResourceLinkedWithSuccess'), null, 'mesgs');
  85. header("Location: ".$_SERVER['PHP_SELF'].'?element='.$element.'&element_id='.$element_id);
  86. exit;
  87. }
  88. }
  89. // Update ressource
  90. if ($action == 'update_linked_resource' && $user->rights->resource->write && !GETPOST('cancel','alpha') )
  91. {
  92. $res = $object->fetch_element_resource($lineid);
  93. if($res)
  94. {
  95. $object->busy = $busy;
  96. $object->mandatory = $mandatory;
  97. $result = $object->update_element_resource($user);
  98. if ($result >= 0)
  99. {
  100. setEventMessages($langs->trans('RessourceLineSuccessfullyUpdated'), null, 'mesgs');
  101. header("Location: ".$_SERVER['PHP_SELF']."?element=".$element."&element_id=".$element_id);
  102. exit;
  103. }
  104. else
  105. {
  106. setEventMessages($object->error, $object->errors, 'errors');
  107. }
  108. }
  109. }
  110. // Delete a resource linked to an element
  111. if ($action == 'confirm_delete_linked_resource' && $user->rights->resource->delete && $confirm === 'yes')
  112. {
  113. $result = $object->delete_resource($lineid,$element);
  114. if ($result >= 0)
  115. {
  116. setEventMessages($langs->trans('RessourceLineSuccessfullyDeleted'), null, 'mesgs');
  117. header("Location: ".$_SERVER['PHP_SELF']."?element=".$element."&element_id=".$element_id);
  118. exit;
  119. }
  120. else
  121. {
  122. setEventMessages($object->error, $object->errors, 'errors');
  123. }
  124. }
  125. $parameters=array('resource_id'=>$resource_id);
  126. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  127. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  128. $parameters=array('resource_id'=>$resource_id);
  129. $reshook=$hookmanager->executeHooks('getElementResources',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  130. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  131. /*
  132. * View
  133. */
  134. $form=new Form($db);
  135. $pagetitle=$langs->trans('ResourceElementPage');
  136. llxHeader('',$pagetitle,'');
  137. // Load available resource, declared by modules
  138. $ret = count($object->available_resources);
  139. if($ret == -1) {
  140. dol_print_error($db,$object->error);
  141. exit;
  142. }
  143. if (!$ret) {
  144. print '<div class="warning">'.$langs->trans('NoResourceInDatabase').'</div>';
  145. }
  146. else
  147. {
  148. // Confirmation suppression resource line
  149. if ($action == 'delete_resource')
  150. {
  151. 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);
  152. }
  153. // Specific to agenda module
  154. if (($element_id || $element_ref) && $element == 'action')
  155. {
  156. require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php';
  157. $act = fetchObjectByElement($element_id,$element, $element_ref);
  158. if (is_object($act))
  159. {
  160. $head=actions_prepare_head($act);
  161. dol_fiche_head($head, 'resources', $langs->trans("Action"), -1, 'action');
  162. $linkback =img_picto($langs->trans("BackToList"),'object_list','class="hideonsmartphone pictoactionview"');
  163. $linkback.= '<a href="'.DOL_URL_ROOT.'/comm/action/list.php">'.$langs->trans("BackToList").'</a>';
  164. // Link to other agenda views
  165. $out='';
  166. $out.='</li><li class="noborder litext">'.img_picto($langs->trans("ViewPerUser"),'object_calendarperuser','class="hideonsmartphone pictoactionview"');
  167. $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>';
  168. $out.='</li><li class="noborder litext">'.img_picto($langs->trans("ViewCal"),'object_calendar','class="hideonsmartphone pictoactionview"');
  169. $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>';
  170. $out.='</li><li class="noborder litext">'.img_picto($langs->trans("ViewWeek"),'object_calendarweek','class="hideonsmartphone pictoactionview"');
  171. $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>';
  172. $out.='</li><li class="noborder litext">'.img_picto($langs->trans("ViewDay"),'object_calendarday','class="hideonsmartphone pictoactionview"');
  173. $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>';
  174. $linkback.=$out;
  175. $morehtmlref='<div class="refidno">';
  176. // Thirdparty
  177. //$morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . $object->thirdparty->getNomUrl(1);
  178. // Project
  179. if (! empty($conf->projet->enabled))
  180. {
  181. $langs->load("projects");
  182. //$morehtmlref.='<br>'.$langs->trans('Project') . ' ';
  183. $morehtmlref.=$langs->trans('Project') . ': ';
  184. if (! empty($act->fk_project)) {
  185. $proj = new Project($db);
  186. $proj->fetch($act->fk_project);
  187. $morehtmlref.='<a href="'.DOL_URL_ROOT.'/projet/card.php?id=' . $act->fk_project . '" title="' . $langs->trans('ShowProject') . '">';
  188. $morehtmlref.=$proj->ref;
  189. $morehtmlref.='</a>';
  190. if ($proj->title) $morehtmlref.=' - '.$proj->title;
  191. } else {
  192. $morehtmlref.='';
  193. }
  194. }
  195. $morehtmlref.='</div>';
  196. dol_banner_tab($act, 'element_id', $linkback, ($user->societe_id?0:1), 'id', 'ref', $morehtmlref, '&element='.$element, 0, '', '');
  197. print '<div class="fichecenter">';
  198. print '<div class="underbanner clearboth"></div>';
  199. print '<table class="border" width="100%">';
  200. // Type
  201. if (! empty($conf->global->AGENDA_USE_EVENT_TYPE))
  202. {
  203. print '<tr><td class="titlefield">'.$langs->trans("Type").'</td><td colspan="3">'.$act->type.'</td></tr>';
  204. }
  205. // Full day event
  206. print '<tr><td class="titlefield">'.$langs->trans("EventOnFullDay").'</td><td colspan="3">'.yn($act->fulldayevent, 3).'</td></tr>';
  207. // Date start
  208. print '<tr><td>'.$langs->trans("DateActionStart").'</td><td colspan="3">';
  209. if (! $act->fulldayevent) print dol_print_date($act->datep,'dayhour');
  210. else print dol_print_date($act->datep,'day');
  211. if ($act->percentage == 0 && $act->datep && $act->datep < ($now - $delay_warning)) print img_warning($langs->trans("Late"));
  212. print '</td>';
  213. print '</tr>';
  214. // Date end
  215. print '<tr><td>'.$langs->trans("DateActionEnd").'</td><td colspan="3">';
  216. if (! $act->fulldayevent) print dol_print_date($act->datef,'dayhour');
  217. else print dol_print_date($act->datef,'day');
  218. if ($act->percentage > 0 && $act->percentage < 100 && $act->datef && $act->datef < ($now- $delay_warning)) print img_warning($langs->trans("Late"));
  219. print '</td></tr>';
  220. // Location
  221. if (empty($conf->global->AGENDA_DISABLE_LOCATION))
  222. {
  223. print '<tr><td>'.$langs->trans("Location").'</td><td colspan="3">'.$act->location.'</td></tr>';
  224. }
  225. // Assigned to
  226. print '<tr><td class="nowrap">'.$langs->trans("ActionAffectedTo").'</td><td colspan="3">';
  227. $listofuserid=array();
  228. if (empty($donotclearsession))
  229. {
  230. if ($act->userownerid > 0) $listofuserid[$act->userownerid]=array('id'=>$act->userownerid,'transparency'=>$act->transparency); // Owner first
  231. if (! empty($act->userassigned)) // Now concat assigned users
  232. {
  233. // Restore array with key with same value than param 'id'
  234. $tmplist1=$act->userassigned; $tmplist2=array();
  235. foreach($tmplist1 as $key => $val)
  236. {
  237. if ($val['id'] && $val['id'] != $act->userownerid) $listofuserid[$val['id']]=$val;
  238. }
  239. }
  240. $_SESSION['assignedtouser']=json_encode($listofuserid);
  241. }
  242. else
  243. {
  244. if (!empty($_SESSION['assignedtouser']))
  245. {
  246. $listofuserid=json_decode($_SESSION['assignedtouser'], true);
  247. }
  248. }
  249. $listofcontactid=array(); // not used yet
  250. $listofotherid=array(); // not used yet
  251. print '<div class="assignedtouser">';
  252. print $form->select_dolusers_forevent('view', 'assignedtouser', 1, '', 0, '', '', 0, 0, 0, '', ($act->datep != $act->datef)?1:0, $listofuserid, $listofcontactid, $listofotherid);
  253. print '</div>';
  254. /*if (in_array($user->id,array_keys($listofuserid)))
  255. {
  256. print '<div class="myavailability">';
  257. 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
  258. print '</div>';
  259. }*/
  260. print ' </td></tr>';
  261. print '</table>';
  262. print '</div>';
  263. dol_fiche_end();
  264. }
  265. }
  266. // Specific to thirdparty module
  267. if (($element_id || $element_ref) && $element == 'societe')
  268. {
  269. $socstatic = fetchObjectByElement($element_id, $element, $element_ref);
  270. if (is_object($socstatic)) {
  271. $savobject = $object;
  272. $object = $socstatic;
  273. require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
  274. $head = societe_prepare_head($socstatic);
  275. dol_fiche_head($head, 'resources', $langs->trans("ThirdParty"), -1, 'company');
  276. dol_banner_tab($socstatic, 'socid', '', ($user->societe_id ? 0 : 1), 'rowid', 'nom', '', '&element='.$element);
  277. print '<div class="fichecenter">';
  278. print '<div class="underbanner clearboth"></div>';
  279. print '<table class="border" width="100%">';
  280. // Alias name (commercial, trademark or alias name)
  281. print '<tr><td class="titlefield">' . $langs->trans('AliasNames') . '</td><td colspan="3">';
  282. print $socstatic->name_alias;
  283. print "</td></tr>";
  284. print '</table>';
  285. print '</div>';
  286. dol_fiche_end();
  287. $object = $savobject;
  288. }
  289. }
  290. // Specific to fichinter module
  291. if (($element_id || $element_ref) && $element == 'fichinter')
  292. {
  293. require_once DOL_DOCUMENT_ROOT.'/core/lib/fichinter.lib.php';
  294. $fichinter = new Fichinter($db);
  295. $fichinter->fetch($element_id, $element_ref);
  296. $fichinter->fetch_thirdparty();
  297. if (is_object($fichinter))
  298. {
  299. $head=fichinter_prepare_head($fichinter);
  300. dol_fiche_head($head, 'resource', $langs->trans("InterventionCard"), -1, 'intervention');
  301. // Intervention card
  302. $linkback = '<a href="'.DOL_URL_ROOT.'/fichinter/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
  303. $morehtmlref='<div class="refidno">';
  304. // Ref customer
  305. //$morehtmlref.=$form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', 0, 1);
  306. //$morehtmlref.=$form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', null, null, '', 1);
  307. // Thirdparty
  308. $morehtmlref.=$langs->trans('ThirdParty') . ' : ' . $fichinter->thirdparty->getNomUrl(1);
  309. // Project
  310. if (! empty($conf->projet->enabled))
  311. {
  312. $langs->load("projects");
  313. $morehtmlref.='<br>'.$langs->trans('Project') . ' ';
  314. if ($user->rights->commande->creer)
  315. {
  316. if ($action != 'classify')
  317. //$morehtmlref.='<a href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $fichinter->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
  318. $morehtmlref.=' : ';
  319. if ($action == 'classify') {
  320. //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $fichinter->id, $fichinter->socid, $fichinter->fk_project, 'projectid', 0, 0, 1, 1);
  321. $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$fichinter->id.'">';
  322. $morehtmlref.='<input type="hidden" name="action" value="classin">';
  323. $morehtmlref.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  324. $morehtmlref.=$formproject->select_projects($fichinter->socid, $fichinter->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
  325. $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  326. $morehtmlref.='</form>';
  327. } else {
  328. $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $fichinter->id, $fichinter->socid, $fichinter->fk_project, 'none', 0, 0, 0, 1);
  329. }
  330. } else {
  331. if (! empty($fichinter->fk_project)) {
  332. $proj = new Project($db);
  333. $proj->fetch($fichinter->fk_project);
  334. $morehtmlref.='<a href="'.DOL_URL_ROOT.'/projet/card.php?id=' . $fichinter->fk_project . '" title="' . $langs->trans('ShowProject') . '">';
  335. $morehtmlref.=$proj->ref;
  336. $morehtmlref.='</a>';
  337. } else {
  338. $morehtmlref.='';
  339. }
  340. }
  341. }
  342. $morehtmlref.='</div>';
  343. dol_banner_tab($fichinter, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '&element='.$element, 0, '', '', 1);
  344. dol_fiche_end();
  345. }
  346. }
  347. // hook for other elements linked
  348. $parameters=array('element'=>$element, 'element_id'=>$element_id, 'element_ref'=>$element_ref);
  349. $reshook=$hookmanager->executeHooks('printElementTab',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  350. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  351. //print load_fiche_titre($langs->trans('ResourcesLinkedToElement'),'','');
  352. print '<br>';
  353. // Show list of resource links
  354. foreach ($object->available_resources as $modresources => $resources)
  355. {
  356. $resources=(array) $resources; // To be sure $resources is an array
  357. foreach($resources as $resource_obj)
  358. {
  359. $element_prop = getElementProperties($resource_obj);
  360. //print '/'.$modresources.'/class/'.$resource_obj.'.class.php<br>';
  361. $path = '';
  362. if(strpos($resource_obj,'@'))
  363. $path .= '/'.$element_prop['module'];
  364. $linked_resources = $object->getElementResources($element,$element_id,$resource_obj);
  365. // If we have a specific template we use it
  366. if(file_exists(dol_buildpath($path.'/core/tpl/resource_'.$element_prop['element'].'_add.tpl.php')))
  367. {
  368. $res=include dol_buildpath($path.'/core/tpl/resource_'.$element_prop['element'].'_add.tpl.php');
  369. }
  370. else
  371. {
  372. $res=include DOL_DOCUMENT_ROOT . '/core/tpl/resource_add.tpl.php';
  373. }
  374. if ($mode != 'add' || $resource_obj != $resource_type)
  375. {
  376. //print load_fiche_titre($langs->trans(ucfirst($element_prop['element']).'Singular'));
  377. // If we have a specific template we use it
  378. if(file_exists(dol_buildpath($path.'/core/tpl/resource_'.$element_prop['element'].'_view.tpl.php')))
  379. {
  380. $res=@include dol_buildpath($path.'/core/tpl/resource_'.$element_prop['element'].'_view.tpl.php');
  381. }
  382. else
  383. {
  384. $res=include DOL_DOCUMENT_ROOT . '/core/tpl/resource_view.tpl.php';
  385. }
  386. }
  387. }
  388. }
  389. }
  390. // End of page
  391. llxFooter();
  392. $db->close();