skeleton_list.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. /* Copyright (C) 2007-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
  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 dev/skeletons/skeleton_list.php
  20. * \ingroup mymodule othermodule1 othermodule2
  21. * \brief This file is an example of a php page
  22. * Put here some comments
  23. */
  24. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1');
  25. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
  26. //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  27. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  28. //if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); // Do not check anti CSRF attack test
  29. //if (! defined('NOSTYLECHECK')) define('NOSTYLECHECK','1'); // Do not check style html tag into posted data
  30. //if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Do not check anti POST attack test
  31. //if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no need to load and show top and left menu
  32. //if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php
  33. //if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  34. //if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)
  35. // Change this following line to use the correct relative path (../, ../../, etc)
  36. $res=0;
  37. if (! $res && file_exists("../main.inc.php")) $res=@include '../main.inc.php'; // to work if your module directory is into dolibarr root htdocs directory
  38. if (! $res && file_exists("../../main.inc.php")) $res=@include '../../main.inc.php'; // to work if your module directory is into a subdir of root htdocs directory
  39. if (! $res && file_exists("../../../dolibarr/htdocs/main.inc.php")) $res=@include '../../../dolibarr/htdocs/main.inc.php'; // Used on dev env only
  40. if (! $res && file_exists("../../../../dolibarr/htdocs/main.inc.php")) $res=@include '../../../../dolibarr/htdocs/main.inc.php'; // Used on dev env only
  41. if (! $res) die("Include of main fails");
  42. // Change this following line to use the correct relative path from htdocs
  43. require_once(DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php');
  44. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  45. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  46. dol_include_once('/mymodule/class/skeleton_class.class.php');
  47. // Load traductions files requiredby by page
  48. $langs->load("mymodule");
  49. $langs->load("other");
  50. // Get parameters
  51. $id = GETPOST('id','int');
  52. $action = GETPOST('action','alpha');
  53. $backtopage = GETPOST('backtopage');
  54. $myparam = GETPOST('myparam','alpha');
  55. $search_field1=GETPOST("search_field1");
  56. $search_field2=GETPOST("search_field2");
  57. $search_myfield=GETPOST('search_myfield');
  58. $optioncss = GETPOST('optioncss','alpha');
  59. // Load variable for pagination
  60. $limit = GETPOST("limit")?GETPOST("limit","int"):$conf->liste_limit;
  61. $sortfield = GETPOST('sortfield','alpha');
  62. $sortorder = GETPOST('sortorder','alpha');
  63. $page = GETPOST('page','int');
  64. if ($page == -1) { $page = 0; }
  65. $offset = $limit * $page;
  66. $pageprev = $page - 1;
  67. $pagenext = $page + 1;
  68. if (! $sortfield) $sortfield="t.rowid"; // Set here default search field
  69. if (! $sortorder) $sortorder="ASC";
  70. // Protection if external user
  71. $socid=0;
  72. if ($user->societe_id > 0)
  73. {
  74. $socid = $user->societe_id;
  75. //accessforbidden();
  76. }
  77. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
  78. $hookmanager->initHooks(array('skeletonlist'));
  79. $extrafields = new ExtraFields($db);
  80. // fetch optionals attributes and labels
  81. $extralabels = $extrafields->fetch_name_optionals_label('mymodule');
  82. $search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
  83. // Load object if id or ref is provided as parameter
  84. $object=new Skeleton_Class($db);
  85. if (($id > 0 || ! empty($ref)) && $action != 'add')
  86. {
  87. $result=$object->fetch($id,$ref);
  88. if ($result < 0) dol_print_error($db);
  89. }
  90. // Definition of fields for list
  91. $arrayfields=array(
  92. 't.field1'=>array('label'=>$langs->trans("Field1"), 'checked'=>1),
  93. 't.field2'=>array('label'=>$langs->trans("Field2"), 'checked'=>1),
  94. //'t.entity'=>array('label'=>$langs->trans("Entity"), 'checked'=>1, 'enabled'=>(! empty($conf->multicompany->enabled) && empty($conf->multicompany->transverse_mode))),
  95. 't.datec'=>array('label'=>$langs->trans("DateCreation"), 'checked'=>0, 'position'=>500),
  96. 't.tms'=>array('label'=>$langs->trans("DateModificationShort"), 'checked'=>0, 'position'=>500),
  97. //'t.statut'=>array('label'=>$langs->trans("Status"), 'checked'=>1, 'position'=>1000),
  98. );
  99. // Extra fields
  100. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
  101. {
  102. foreach($extrafields->attribute_label as $key => $val)
  103. {
  104. $arrayfields["ef.".$key]=array('label'=>$extrafields->attribute_label[$key], 'checked'=>$extrafields->attribute_list[$key], 'position'=>$extrafields->attribute_pos[$key], 'enabled'=>$extrafields->attribute_perms[$key]);
  105. }
  106. }
  107. /*******************************************************************
  108. * ACTIONS
  109. *
  110. * Put here all code to do according to value of "action" parameter
  111. ********************************************************************/
  112. if (GETPOST('cancel')) { $action='list'; $massaction=''; }
  113. if (! GETPOST('confirmmassaction')) { $massaction=''; }
  114. $parameters=array();
  115. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  116. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  117. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  118. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") ||GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
  119. {
  120. $search_field1='';
  121. $search_field2='';
  122. $search_date_creation='';
  123. $search_date_update='';
  124. $search_array_options=array();
  125. }
  126. if (empty($reshook))
  127. {
  128. // Mass actions. Controls on number of lines checked
  129. $maxformassaction=1000;
  130. if (! empty($massaction) && count($toselect) < 1)
  131. {
  132. $error++;
  133. setEventMessages($langs->trans("NoLineChecked"), null, "warnings");
  134. }
  135. if (! $error && count($toselect) > $maxformassaction)
  136. {
  137. setEventMessages($langs->trans('TooManyRecordForMassAction',$maxformassaction), null, 'errors');
  138. $error++;
  139. }
  140. // Action to delete
  141. if ($action == 'confirm_delete')
  142. {
  143. $result=$object->delete($user);
  144. if ($result > 0)
  145. {
  146. // Delete OK
  147. setEventMessages("RecordDeleted", null, 'mesgs');
  148. header("Location: ".dol_buildpath('/mymodule/list.php',1));
  149. exit;
  150. }
  151. else
  152. {
  153. if (! empty($object->errors)) setEventMessages(null,$object->errors,'errors');
  154. else setEventMessages($object->error,null,'errors');
  155. }
  156. }
  157. }
  158. /***************************************************
  159. * VIEW
  160. *
  161. * Put here all code to build page
  162. ****************************************************/
  163. llxHeader('','MyPageName','');
  164. $form=new Form($db);
  165. // Put here content of your page
  166. $title = $langs->trans('MyModuleListTitle');
  167. // Example : Adding jquery code
  168. print '<script type="text/javascript" language="javascript">
  169. jQuery(document).ready(function() {
  170. function init_myfunc()
  171. {
  172. jQuery("#myid").removeAttr(\'disabled\');
  173. jQuery("#myid").attr(\'disabled\',\'disabled\');
  174. }
  175. init_myfunc();
  176. jQuery("#mybutton").click(function() {
  177. init_myfunc();
  178. });
  179. });
  180. </script>';
  181. $sql = "SELECT";
  182. $sql.= " t.rowid,";
  183. $sql.= " t.field1,";
  184. $sql.= " t.field2";
  185. // Add fields for extrafields
  186. foreach ($extrafields->attribute_list as $key => $val) $sql.=",ef.".$key.' as options_'.$key;
  187. // Add fields from hooks
  188. $parameters=array();
  189. $reshook=$hookmanager->executeHooks('printFieldListSelect',$parameters); // Note that $action and $object may have been modified by hook
  190. $sql.=$hookmanager->resPrint;
  191. $sql.= " FROM ".MAIN_DB_PREFIX."mytable as t";
  192. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label)) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."mytable_extrafields as ef on (u.rowid = ef.fk_object)";
  193. $sql.= " WHERE 1 = 1";
  194. //$sql.= " WHERE u.entity IN (".getEntity('mytable',1).")";
  195. if ($search_field1) $sql.= natural_search("field1",$search_field1);
  196. if ($search_field2) $sql.= natural_search("field2",$search_field2);
  197. if ($sall) $sql.= natural_search(array_keys($fieldstosearchall), $sall);
  198. // Add where from extra fields
  199. foreach ($search_array_options as $key => $val)
  200. {
  201. $crit=$val;
  202. $tmpkey=preg_replace('/search_options_/','',$key);
  203. $typ=$extrafields->attribute_type[$tmpkey];
  204. $mode=0;
  205. if (in_array($typ, array('int','double'))) $mode=1; // Search on a numeric
  206. if ($val && ( ($crit != '' && ! in_array($typ, array('select'))) || ! empty($crit)))
  207. {
  208. $sql .= natural_search('ef.'.$tmpkey, $crit, $mode);
  209. }
  210. }
  211. // Add where from hooks
  212. $parameters=array();
  213. $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
  214. $sql.=$hookmanager->resPrint;
  215. $sql.=$db->order($sortfield,$sortorder);
  216. //$sql.= $db->plimit($conf->liste_limit+1, $offset);
  217. // Count total nb of records
  218. $nbtotalofrecords = 0;
  219. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  220. {
  221. $result = $db->query($sql);
  222. $nbtotalofrecords = $db->num_rows($result);
  223. }
  224. $sql.= $db->plimit($limit+1, $offset);
  225. dol_syslog($script_file, LOG_DEBUG);
  226. $resql=$db->query($sql);
  227. if ($resql)
  228. {
  229. $num = $db->num_rows($resql);
  230. $params='';
  231. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  232. if ($search_field1 != '') $params.= '&amp;search_field1='.urlencode($search_field1);
  233. if ($search_field2 != '') $params.= '&amp;search_field2='.urlencode($search_field2);
  234. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  235. // Add $param from extra fields
  236. foreach ($search_array_options as $key => $val)
  237. {
  238. $crit=$val;
  239. $tmpkey=preg_replace('/search_options_/','',$key);
  240. if ($val != '') $param.='&search_options_'.$tmpkey.'='.urlencode($val);
  241. }
  242. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $params, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_companies', 0, '', '', $limit);
  243. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  244. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  245. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  246. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  247. print '<input type="hidden" name="action" value="list">';
  248. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  249. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  250. if ($sall)
  251. {
  252. foreach($fieldstosearchall as $key => $val) $fieldstosearchall[$key]=$langs->trans($val);
  253. print $langs->trans("FilterOnInto", $all) . implode(', ',$fieldstosearchall);
  254. }
  255. $moreforfilter = '';
  256. $moreforfilter.='<div class="divsearchfield">';
  257. $moreforfilter.= $langs->trans('MyFilter') . ': <input type="text" name="search_myfield" value="'.dol_escpae_htmltag($search_myfield).'">';
  258. $moreforfilter.= '</div>';
  259. if (! empty($moreforfilter))
  260. {
  261. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  262. print $moreforfilter;
  263. $parameters=array();
  264. $reshook=$hookmanager->executeHooks('printFieldPreListTitle',$parameters); // Note that $action and $object may have been modified by hook
  265. print $hookmanager->resPrint;
  266. print '</div>';
  267. }
  268. $varpage=empty($contextpage)?$_SERVER["PHP_SELF"]:$contextpage;
  269. $selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  270. print '<table class="liste '.($moreforfilter?"listwithfilterbefore":"").'">';
  271. // Fields title
  272. print '<tr class="liste_titre">';
  273. if (! empty($arrayfields['t.field1']['checked'])) print_liste_field_titre($arrayfields['t.field1']['label'],$_SERVER['PHP_SELF'],'t.field1','',$param,'',$sortfield,$sortorder);
  274. if (! empty($arrayfields['t.field2']['checked'])) print_liste_field_titre($arrayfields['t.field2']['label'],$_SERVER['PHP_SELF'],'t.field2','',$param,'',$sortfield,$sortorder);
  275. // Extra fields
  276. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
  277. {
  278. foreach($extrafields->attribute_label as $key => $val)
  279. {
  280. if (! empty($arrayfields["ef.".$key]['checked']))
  281. {
  282. $align=$extrafields->getAlignFlag($key);
  283. print_liste_field_titre($extralabels[$key],$_SERVER["PHP_SELF"],"ef.".$key,"",$param,($align?'align="'.$align.'"':''),$sortfield,$sortorder);
  284. }
  285. }
  286. }
  287. // Hook fields
  288. $parameters=array('arrayfields'=>$arrayfields);
  289. $reshook=$hookmanager->executeHooks('printFieldListTitle',$parameters); // Note that $action and $object may have been modified by hook
  290. print $hookmanager->resPrint;
  291. if (! empty($arrayfields['t.datec']['checked'])) print_liste_field_titre($arrayfields['t.datec']['label'],$_SERVER["PHP_SELF"],"t.datec","",$param,'align="center" class="nowrap"',$sortfield,$sortorder);
  292. if (! empty($arrayfields['t.tms']['checked'])) print_liste_field_titre($arrayfields['t.tms']['label'],$_SERVER["PHP_SELF"],"t.tms","",$param,'align="center" class="nowrap"',$sortfield,$sortorder);
  293. //if (! empty($arrayfields['t.status']['checked'])) print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"t.status","",$param,'align="center"',$sortfield,$sortorder);
  294. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"],"",'','','align="right"',$sortfield,$sortorder,'maxwidthsearch ');
  295. print '</tr>'."\n";
  296. // Fields title search
  297. print '<tr class="liste_titre">';
  298. if (! empty($arrayfields['t.field1']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" name="search_field1" value="'.$search_field1.'" size="10"></td>';
  299. if (! empty($arrayfields['t.field2']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" name="search_field2" value="'.$search_field2.'" size="10"></td>';
  300. // Extra fields
  301. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
  302. {
  303. foreach($extrafields->attribute_label as $key => $val)
  304. {
  305. if (! empty($arrayfields["ef.".$key]['checked']))
  306. {
  307. $align=$extrafields->getAlignFlag($key);
  308. $typeofextrafield=$extrafields->attribute_type[$key];
  309. print '<td class="liste_titre'.($align?' '.$align:'').'">';
  310. if (in_array($typeofextrafield, array('varchar', 'int', 'double', 'select')))
  311. {
  312. $crit=$val;
  313. $tmpkey=preg_replace('/search_options_/','',$key);
  314. $searchclass='';
  315. if (in_array($typeofextrafield, array('varchar', 'select'))) $searchclass='searchstring';
  316. if (in_array($typeofextrafield, array('int', 'double'))) $searchclass='searchnum';
  317. print '<input class="flat'.($searchclass?' '.$searchclass:'').'" size="4" type="text" name="search_options_'.$tmpkey.'" value="'.dol_escape_htmltag($search_array_options['search_options_'.$tmpkey]).'">';
  318. }
  319. print '</td>';
  320. }
  321. }
  322. }
  323. // Fields from hook
  324. $parameters=array('arrayfields'=>$arrayfields);
  325. $reshook=$hookmanager->executeHooks('printFieldListOption',$parameters); // Note that $action and $object may have been modified by hook
  326. print $hookmanager->resPrint;
  327. if (! empty($arrayfields['t.datec']['checked']))
  328. {
  329. // Date creation
  330. print '<td class="liste_titre">';
  331. print '</td>';
  332. }
  333. if (! empty($arrayfields['t.tms']['checked']))
  334. {
  335. // Date modification
  336. print '<td class="liste_titre">';
  337. print '</td>';
  338. }
  339. /*if (! empty($arrayfields['u.statut']['checked']))
  340. {
  341. // Status
  342. print '<td class="liste_titre" align="center">';
  343. print $form->selectarray('search_statut', array('-1'=>'','0'=>$langs->trans('Disabled'),'1'=>$langs->trans('Enabled')),$search_statut);
  344. print '</td>';
  345. }*/
  346. // Action column
  347. print '<td class="liste_titre" align="right">';
  348. $searchpitco=$form->showFilterAndCheckAddButtons(0);
  349. print $searchpitco;
  350. print '</td>';
  351. print '</tr>'."\n";
  352. $i=0;
  353. $var=true;
  354. $totalarray=array();
  355. while ($i < min($num, $limit))
  356. {
  357. $obj = $db->fetch_object($resql);
  358. if ($obj)
  359. {
  360. // You can use here results
  361. print '<tr>';
  362. if (! empty($arrayfields['t.field1']['checked']))
  363. {
  364. print '<td>'.$obj->field1.'</td>';
  365. if (! $i) $totalarray['nbfield']++;
  366. }
  367. if (! empty($arrayfields['t.field2']['checked']))
  368. {
  369. print '<td>'.$obj->field2.'</td>';
  370. if (! $i) $totalarray['nbfield']++;
  371. }
  372. // Extra fields
  373. if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
  374. {
  375. foreach($extrafields->attribute_label as $key => $val)
  376. {
  377. if (! empty($arrayfields["ef.".$key]['checked']))
  378. {
  379. print '<td';
  380. $align=$extrafields->getAlignFlag($key);
  381. if ($align) print ' align="'.$align.'"';
  382. print '>';
  383. $tmpkey='options_'.$key;
  384. print $extrafields->showOutputField($key, $obj->$tmpkey, '', 1);
  385. print '</td>';
  386. if (! $i) $totalarray['nbfield']++;
  387. }
  388. }
  389. }
  390. // Fields from hook
  391. $parameters=array('arrayfields'=>$arrayfields, 'obj'=>$obj);
  392. $reshook=$hookmanager->executeHooks('printFieldListValue',$parameters); // Note that $action and $object may have been modified by hook
  393. print $hookmanager->resPrint;
  394. // Date creation
  395. if (! empty($arrayfields['t.datec']['checked']))
  396. {
  397. print '<td align="center">';
  398. print dol_print_date($db->jdate($obj->date_creation), 'dayhour');
  399. print '</td>';
  400. if (! $i) $totalarray['nbfield']++;
  401. }
  402. // Date modification
  403. if (! empty($arrayfields['t.tms']['checked']))
  404. {
  405. print '<td align="center">';
  406. print dol_print_date($db->jdate($obj->date_update), 'dayhour');
  407. print '</td>';
  408. if (! $i) $totalarray['nbfield']++;
  409. }
  410. // Status
  411. /*
  412. if (! empty($arrayfields['u.statut']['checked']))
  413. {
  414. $userstatic->statut=$obj->statut;
  415. print '<td align="center">'.$userstatic->getLibStatut(3).'</td>';
  416. }*/
  417. // Action column
  418. print '<td></td>';
  419. if (! $i) $totalarray['nbfield']++;
  420. print '</tr>';
  421. }
  422. $i++;
  423. }
  424. $db->free($resql);
  425. $parameters=array('sql' => $sql);
  426. $reshook=$hookmanager->executeHooks('printFieldListFooter',$parameters); // Note that $action and $object may have been modified by hook
  427. print $hookmanager->resPrint;
  428. print "</table>\n";
  429. print "</form>\n";
  430. $db->free($result);
  431. }
  432. else
  433. {
  434. $error++;
  435. dol_print_error($db);
  436. }
  437. // End of page
  438. llxFooter();
  439. $db->close();