list.php 33 KB

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