list.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /* Copyright (C) 2013-2014 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
  4. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file resource/index.php
  21. * \ingroup resource
  22. * \brief Page to manage resource objects
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
  26. // Load translation files required by the page
  27. $langs->loadLangs(array("resource","companies","other"));
  28. // Get parameters
  29. $id = GETPOST('id','int');
  30. $action = GETPOST('action','alpha');
  31. $lineid = GETPOST('lineid','int');
  32. $element = GETPOST('element','alpha');
  33. $element_id = GETPOST('element_id','int');
  34. $resource_id = GETPOST('resource_id','int');
  35. $sortorder = GETPOST('sortorder','alpha');
  36. $sortfield = GETPOST('sortfield','alpha');
  37. // Initialize context for list
  38. $contextpage=GETPOST('contextpage','aZ')?GETPOST('contextpage','aZ'):'resourcelist';
  39. // Initialize technical objects
  40. $object = new Dolresource($db);
  41. $extrafields = new ExtraFields($db);
  42. // fetch optionals attributes and labels
  43. $extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
  44. $search_array_options=$extrafields->getOptionalsFromPost($object->table_element,'','search_');
  45. if (! is_array($search_array_options)) $search_array_options = array();
  46. $search_ref=GETPOST("search_ref");
  47. $search_type=GETPOST("search_type");
  48. $filter=array();
  49. if ($search_ref != ''){
  50. $param.='&search_ref='.$search_ref;
  51. $filter['t.ref']=$search_ref;
  52. }
  53. if ($search_type != ''){
  54. $param.='&search_type='.$search_type;
  55. $filter['ty.label']=$search_type;
  56. }
  57. if ($search_label != '') $param.='&search_label='.$search_label;
  58. // Add $param from extra fields
  59. foreach ($search_array_options as $key => $val)
  60. {
  61. $crit=$val;
  62. $tmpkey=preg_replace('/search_options_/','',$key);
  63. $typ=$extrafields->attribute_type[$tmpkey];
  64. if ($val != '') {
  65. $param.='&search_options_'.$tmpkey.'='.urlencode($val);
  66. }
  67. $mode_search=0;
  68. if (in_array($typ, array('int','double','real'))) $mode_search=1; // Search on a numeric
  69. if (in_array($typ, array('sellist','link')) && $crit != '0' && $crit != '-1') $mode_search=2; // Search on a foreign key int
  70. if ($crit != '' && (! in_array($typ, array('select','sellist')) || $crit != '0') && (! in_array($typ, array('link')) || $crit != '-1'))
  71. {
  72. $filter['ef.'.$tmpkey] = natural_search('ef.'.$tmpkey, $crit, $mode_search);
  73. }
  74. }
  75. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  76. $hookmanager->initHooks(array('resourcelist'));
  77. if (empty($sortorder)) $sortorder="ASC";
  78. if (empty($sortfield)) $sortfield="t.ref";
  79. if (empty($arch)) $arch = 0;
  80. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  81. $page = GETPOST("page");
  82. $page = is_numeric($page) ? $page : 0;
  83. $page = $page == -1 ? 0 : $page;
  84. $offset = $limit * $page ;
  85. $pageprev = $page - 1;
  86. $pagenext = $page + 1;
  87. if( ! $user->rights->resource->read) {
  88. accessforbidden();
  89. }
  90. $arrayfields = array(
  91. 't.ref' => array(
  92. 'label' => $langs->trans("Ref"),
  93. 'checked' => 1
  94. ),
  95. 'ty.label' => array(
  96. 'label' => $langs->trans("ResourceType"),
  97. 'checked' => 1
  98. ),
  99. );
  100. // Extra fields
  101. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label)) {
  102. foreach ( $extrafields->attribute_label as $key => $val ) {
  103. $typeofextrafield=$extrafields->attribute_type[$key];
  104. if ($typeofextrafield!='separate') {
  105. $arrayfields["ef." . $key] = array(
  106. 'label' => $extrafields->attribute_label[$key],
  107. 'checked' => $extrafields->attribute_list[$key],
  108. 'position' => $extrafields->attribute_pos[$key],
  109. 'enabled' => $extrafields->attribute_perms[$key]
  110. );
  111. }
  112. }
  113. }
  114. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  115. // Do we click on purge search criteria ?
  116. if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter','alpha')) // Both test are required to be compatible with all browsers
  117. {
  118. $search_ref="";
  119. $search_label="";
  120. $search_type="";
  121. $search_array_options=array();
  122. $filter=array();
  123. }
  124. /*
  125. * Action
  126. */
  127. $parameters=array();
  128. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  129. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  130. /*
  131. * View
  132. */
  133. $form=new Form($db);
  134. $pagetitle=$langs->trans('ResourcePageIndex');
  135. llxHeader('',$pagetitle,'');
  136. // Confirmation suppression resource line
  137. if ($action == 'delete_resource')
  138. {
  139. print $form->formconfirm($_SERVER['PHP_SELF']."?element=".$element."&element_id=".$element_id."&lineid=".$lineid,$langs->trans("DeleteResource"),$langs->trans("ConfirmDeleteResourceElement"),"confirm_delete_resource",'','',1);
  140. }
  141. $varpage=empty($contextpage)?$_SERVER["PHP_SELF"]:$contextpage;
  142. $selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage);
  143. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  144. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  145. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  146. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  147. print '<input type="hidden" name="action" value="list">';
  148. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  149. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  150. print '<input type="hidden" name="page" value="'.$page.'">';
  151. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  152. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  153. {
  154. $ret = $object->fetch_all('', '', 0, 0, $filter);
  155. if($ret == -1) {
  156. dol_print_error($db,$object->error);
  157. exit;
  158. } else {
  159. $nbtotalofrecords = $ret;
  160. }
  161. }
  162. // Load object list
  163. $ret = $object->fetch_all($sortorder, $sortfield, $limit, $offset, $filter);
  164. if($ret == -1) {
  165. dol_print_error($db,$object->error);
  166. exit;
  167. } else {
  168. $newcardbutton='';
  169. if ($user->rights->resource->write)
  170. {
  171. $newcardbutton='<a class="butActionNew" href="'.DOL_URL_ROOT.'/resource/card.php?action=create"><span class="valignmiddle">'.$langs->trans('MenuResourceAdd').'</span>';
  172. $newcardbutton.= '<span class="fa fa-plus-circle valignmiddle"></span>';
  173. $newcardbutton.= '</a>';
  174. }
  175. print_barre_liste($pagetitle, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $ret+1, $nbtotalofrecords,'title_generic.png', 0, $newcardbutton, '', $limit);
  176. }
  177. $moreforfilter = '';
  178. print '<div class="div-table-responsive">';
  179. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  180. print '<tr class="liste_titre_filter">';
  181. if (! empty($arrayfields['t.ref']['checked']))
  182. {
  183. print '<td class="liste_titre">';
  184. print '<input type="text" class="flat" name="search_ref" value="'.$search_ref.'" size="6">';
  185. print '</td>';
  186. }
  187. if (! empty($arrayfields['ty.label']['checked']))
  188. {
  189. print '<td class="liste_titre">';
  190. print '<input type="text" class="flat" name="search_type" value="'.$search_type.'" size="6">';
  191. print '</td>';
  192. }
  193. // Extra fields
  194. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_input.tpl.php';
  195. // Action column
  196. print '<td class="liste_titre" align="right">';
  197. $searchpicto=$form->showFilterAndCheckAddButtons(0);
  198. print $searchpicto;
  199. print '</td>';
  200. print "</tr>\n";
  201. print '<tr class="liste_titre">';
  202. if (! empty($arrayfields['t.ref']['checked'])) print_liste_field_titre($arrayfields['t.ref']['label'],$_SERVER["PHP_SELF"],"t.ref","",$param,"",$sortfield,$sortorder);
  203. if (! empty($arrayfields['ty.label']['checked'])) print_liste_field_titre($arrayfields['ty.label']['label'],$_SERVER["PHP_SELF"],"ty.label","",$param,"",$sortfield,$sortorder);
  204. // Extra fields
  205. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_title.tpl.php';
  206. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"],"",'','','align="center"',$sortfield,$sortorder,'maxwidthsearch ');
  207. print "</tr>\n";
  208. if ($ret)
  209. {
  210. foreach ($object->lines as $resource)
  211. {
  212. print '<tr class="oddeven">';
  213. if (! empty($arrayfields['t.ref']['checked']))
  214. {
  215. print '<td>';
  216. print $resource->getNomUrl(5);
  217. print '</td>';
  218. if (! $i) $totalarray['nbfield']++;
  219. }
  220. if (! empty($arrayfields['ty.label']['checked']))
  221. {
  222. print '<td>';
  223. print $resource->type_label;
  224. print '</td>';
  225. if (! $i) $totalarray['nbfield']++;
  226. }
  227. // Extra fields
  228. $obj = (Object) $resource->array_options;
  229. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php';
  230. print '<td align="center">';
  231. print '<a href="./card.php?action=edit&id='.$resource->id.'">';
  232. print img_edit();
  233. print '</a>';
  234. print '&nbsp;';
  235. print '<a href="./card.php?action=delete&id='.$resource->id.'">';
  236. print img_delete();
  237. print '</a>';
  238. print '</td>';
  239. if (! $i) $totalarray['nbfield']++;
  240. print '</tr>';
  241. }
  242. }
  243. else
  244. {
  245. $colspan=1;
  246. foreach($arrayfields as $key => $val) { if (! empty($val['checked'])) $colspan++; }
  247. print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoRecordFound").'</td></tr>';
  248. }
  249. print '</table>';
  250. print "</form>\n";
  251. // End of page
  252. llxFooter();
  253. $db->close();