boxes.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/admin/boxes.php
  22. * \brief Page to setup boxes
  23. */
  24. require '../main.inc.php';
  25. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array('admin', 'boxes', 'accountancy'));
  30. if (!$user->admin) accessforbidden();
  31. $rowid = GETPOST('rowid', 'int');
  32. $action = GETPOST('action', 'alpha');
  33. // Define possible position of boxes
  34. $pos_name = InfoBox::getListOfPagesForBoxes();
  35. $boxes = array();
  36. /*
  37. * Actions
  38. */
  39. if ($action == 'addconst')
  40. {
  41. dolibarr_set_const($db, "MAIN_BOXES_MAXLINES", $_POST["MAIN_BOXES_MAXLINES"], '', 0, '', $conf->entity);
  42. dolibarr_set_const($db, "MAIN_ACTIVATE_FILECACHE", $_POST["MAIN_ACTIVATE_FILECACHE"], 'chaine', 0, '', $conf->entity);
  43. }
  44. if ($action == 'add') {
  45. $error = 0;
  46. $db->begin();
  47. if (isset($_POST['boxid']) && is_array($_POST['boxid']))
  48. {
  49. foreach ($_POST['boxid'] as $boxid)
  50. {
  51. if (is_numeric($boxid['pos']) && $boxid['pos'] >= 0) // 0=Home, 1=...
  52. {
  53. $pos = $boxid['pos'];
  54. // Initialize distinct fk_user with all already existing values of fk_user (user that use a personalized view of boxes for page "pos")
  55. $distinctfkuser = array();
  56. if (!$error)
  57. {
  58. $sql = "SELECT fk_user";
  59. $sql .= " FROM ".MAIN_DB_PREFIX."user_param";
  60. $sql .= " WHERE param = 'MAIN_BOXES_".$db->escape($pos)."' AND value = '1'";
  61. $sql .= " AND entity = ".$conf->entity;
  62. dol_syslog("boxes.php search fk_user to activate box for", LOG_DEBUG);
  63. $resql = $db->query($sql);
  64. if ($resql)
  65. {
  66. $num = $db->num_rows($resql);
  67. $i = 0;
  68. while ($i < $num)
  69. {
  70. $obj = $db->fetch_object($resql);
  71. $distinctfkuser[$obj->fk_user] = $obj->fk_user;
  72. $i++;
  73. }
  74. }
  75. else
  76. {
  77. setEventMessages($db->lasterror(), null, 'errors');
  78. $error++;
  79. }
  80. }
  81. $distinctfkuser['0'] = '0'; // Add entry for fk_user = 0. We must use string as key and val
  82. foreach ($distinctfkuser as $fk_user)
  83. {
  84. if (!$error && $fk_user != '')
  85. {
  86. $nbboxonleft = $nbboxonright = 0;
  87. $sql = "SELECT box_order FROM ".MAIN_DB_PREFIX."boxes WHERE position = ".$pos." AND fk_user = ".$fk_user." AND entity = ".$conf->entity;
  88. dol_syslog("boxes.php activate box", LOG_DEBUG);
  89. $resql = $db->query($sql);
  90. if ($resql)
  91. {
  92. while ($obj = $db->fetch_object($resql))
  93. {
  94. $boxorder = $obj->box_order;
  95. if (preg_match('/A/', $boxorder)) $nbboxonleft++;
  96. if (preg_match('/B/', $boxorder)) $nbboxonright++;
  97. }
  98. }
  99. else dol_print_error($db);
  100. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (";
  101. $sql .= "box_id, position, box_order, fk_user, entity";
  102. $sql .= ") values (";
  103. $sql .= $boxid['value'].", ".$pos.", '".(($nbboxonleft > $nbboxonright) ? 'B01' : 'A01')."', ".$fk_user.", ".$conf->entity;
  104. $sql .= ")";
  105. dol_syslog("boxes.php activate box", LOG_DEBUG);
  106. $resql = $db->query($sql);
  107. if (!$resql)
  108. {
  109. setEventMessages($db->lasterror(), null, 'errors');
  110. $error++;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. if (!$error)
  118. {
  119. $db->commit();
  120. $action = '';
  121. }
  122. else
  123. {
  124. $db->rollback();
  125. }
  126. }
  127. if ($action == 'delete')
  128. {
  129. $sql = "SELECT box_id FROM ".MAIN_DB_PREFIX."boxes";
  130. $sql .= " WHERE rowid=".$rowid;
  131. $resql = $db->query($sql);
  132. $obj = $db->fetch_object($resql);
  133. if (!empty($obj->box_id))
  134. {
  135. $db->begin();
  136. // Remove all personalized setup when a box is activated or disabled (why removing all ? We removed only removed boxes)
  137. // $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_param";
  138. // $sql.= " WHERE param LIKE 'MAIN_BOXES_%'";
  139. // $resql = $db->query($sql);
  140. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  141. $sql .= " WHERE entity = ".$conf->entity;
  142. $sql .= " AND box_id=".$obj->box_id;
  143. $resql = $db->query($sql);
  144. $db->commit();
  145. }
  146. }
  147. if ($action == 'switch')
  148. {
  149. // We switch values of field box_order for the 2 lines of table boxes
  150. $db->begin();
  151. $objfrom = new ModeleBoxes($db);
  152. $objfrom->fetch($_GET["switchfrom"]);
  153. $objto = new ModeleBoxes($db);
  154. $objto->fetch($_GET["switchto"]);
  155. $resultupdatefrom = 0;
  156. $resultupdateto = 0;
  157. if (is_object($objfrom) && is_object($objto))
  158. {
  159. $newfirst = $objto->box_order;
  160. $newsecond = $objfrom->box_order;
  161. if ($newfirst == $newsecond)
  162. {
  163. $newsecondchar = preg_replace('/[0-9]+/', '', $newsecond);
  164. $newsecondnum = preg_replace('/[a-zA-Z]+/', '', $newsecond);
  165. $newsecond = sprintf("%s%02d", $newsecondchar ? $newsecondchar : 'A', $newsecondnum + 1);
  166. }
  167. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$newfirst."' WHERE rowid=".$objfrom->rowid;
  168. dol_syslog($sql);
  169. $resultupdatefrom = $db->query($sql);
  170. if (!$resultupdatefrom) { dol_print_error($db); }
  171. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$newsecond."' WHERE rowid=".$objto->rowid;
  172. dol_syslog($sql);
  173. $resultupdateto = $db->query($sql);
  174. if (!$resultupdateto) { dol_print_error($db); }
  175. }
  176. if ($resultupdatefrom && $resultupdateto)
  177. {
  178. $db->commit();
  179. }
  180. else
  181. {
  182. $db->rollback();
  183. }
  184. }
  185. /*
  186. * View
  187. */
  188. $form = new Form($db);
  189. llxHeader('', $langs->trans("Boxes"));
  190. print load_fiche_titre($langs->trans("Boxes"), '', 'title_setup');
  191. print '<span class="opacitymedium">'.$langs->trans("BoxesDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."</span><br>\n";
  192. /*
  193. * Search for the default active boxes for each possible position
  194. * We store the active boxes by default in $boxes[position][id_boite]=1
  195. */
  196. $actives = array();
  197. $sql = "SELECT b.rowid, b.box_id, b.position, b.box_order,";
  198. $sql .= " bd.rowid as boxid";
  199. $sql .= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as bd";
  200. $sql .= " WHERE b.box_id = bd.rowid";
  201. $sql .= " AND b.entity IN (0,".$conf->entity.")";
  202. $sql .= " AND b.fk_user=0";
  203. $sql .= " ORDER by b.position, b.box_order";
  204. dol_syslog("Search available boxes", LOG_DEBUG);
  205. $resql = $db->query($sql);
  206. if ($resql)
  207. {
  208. $num = $db->num_rows($resql);
  209. // Check record to know if we must recalculate sort order
  210. $i = 0;
  211. $decalage = 0;
  212. while ($i < $num)
  213. {
  214. $obj = $db->fetch_object($resql);
  215. $boxes[$obj->position][$obj->box_id] = 1;
  216. $i++;
  217. array_push($actives, $obj->box_id);
  218. if ($obj->box_order == '' || $obj->box_order == '0' || $decalage) $decalage++;
  219. // We renumber the order of the boxes if one of them is in ''
  220. // This occurs just after an insert.
  221. if ($decalage)
  222. {
  223. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$decalage."' WHERE rowid=".$obj->rowid;
  224. $db->query($sql);
  225. }
  226. }
  227. if ($decalage)
  228. {
  229. // If we have renumbered, we correct the field box_order
  230. // This occurs just after an insert.
  231. $sql = "SELECT box_order";
  232. $sql .= " FROM ".MAIN_DB_PREFIX."boxes";
  233. $sql .= " WHERE entity = ".$conf->entity;
  234. $sql .= " AND LENGTH(box_order) <= 2";
  235. dol_syslog("Execute requests to renumber box order", LOG_DEBUG);
  236. $result = $db->query($sql);
  237. if ($result)
  238. {
  239. while ($record = $db->fetch_array($result))
  240. {
  241. if (dol_strlen($record['box_order']) == 1)
  242. {
  243. if (preg_match("/[13579]{1}/", substr($record['box_order'], -1)))
  244. {
  245. $box_order = "A0".$record['box_order'];
  246. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  247. $resql = $db->query($sql);
  248. }
  249. elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1)))
  250. {
  251. $box_order = "B0".$record['box_order'];
  252. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  253. $resql = $db->query($sql);
  254. }
  255. }
  256. elseif (dol_strlen($record['box_order']) == 2)
  257. {
  258. if (preg_match("/[13579]{1}/", substr($record['box_order'], -1)))
  259. {
  260. $box_order = "A".$record['box_order'];
  261. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  262. $resql = $db->query($sql);
  263. }
  264. elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1)))
  265. {
  266. $box_order = "B".$record['box_order'];
  267. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  268. $resql = $db->query($sql);
  269. }
  270. }
  271. }
  272. }
  273. }
  274. $db->free($resql);
  275. }
  276. // Available boxes to activate
  277. $boxtoadd = InfoBox::listBoxes($db, 'available', -1, null, $actives);
  278. // Activated boxes
  279. $boxactivated = InfoBox::listBoxes($db, 'activated', -1, null);
  280. print "<br>\n";
  281. print "\n\n".'<!-- Boxes Available -->'."\n";
  282. print load_fiche_titre($langs->trans("BoxesAvailable"));
  283. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
  284. print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
  285. print '<input type="hidden" name="action" value="add">'."\n";
  286. print '<div class="div-table-responsive-no-min">';
  287. print '<table class="tagtable liste centpercent">'."\n";
  288. print '<tr class="liste_titre">';
  289. print '<td width="300">'.$langs->trans("Box").'</td>';
  290. print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
  291. print '<td>'.$langs->trans("SourceFile").'</td>';
  292. print '<td width="160" class="center">'.$langs->trans("ActivateOn").'</td>';
  293. print "</tr>\n";
  294. foreach ($boxtoadd as $box)
  295. {
  296. if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg))
  297. {
  298. $logo = $box->boximg;
  299. }
  300. else
  301. {
  302. $logo = preg_replace("/^object_/i", "", $box->boximg);
  303. }
  304. print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
  305. print '<tr class="oddeven">'."\n";
  306. print '<td>'.img_object("", $logo).' '.$langs->transnoentitiesnoconv($box->boxlabel);
  307. if (!empty($box->class) && preg_match('/graph_/', $box->class)) print ' ('.$langs->trans("Graph").')';
  308. print '</td>'."\n";
  309. print '<td>';
  310. if ($box->note == '(WarningUsingThisBoxSlowDown)')
  311. {
  312. $langs->load("errors");
  313. print $langs->trans("WarningUsingThisBoxSlowDown");
  314. }
  315. else print ($box->note ? $box->note : '&nbsp;');
  316. print '</td>'."\n";
  317. print '<td>'.$box->sourcefile.'</td>'."\n";
  318. // For each possible position, an activation link is displayed if the box is not already active for that position
  319. print '<td class="center">';
  320. print $form->selectarray("boxid[".$box->box_id."][pos]", $pos_name, 0, 1, 0, 0, '', 1)."\n";
  321. print '<input type="hidden" name="boxid['.$box->box_id.'][value]" value="'.$box->box_id.'">'."\n";
  322. print '</td>';
  323. print '</tr>'."\n";
  324. }
  325. if (!count($boxtoadd) && count($boxactivated))
  326. {
  327. print '<tr><td class="opacitymedium" colspan="4">'.$langs->trans("AllWidgetsWereEnabled").'</td></tr>';
  328. }
  329. print '</table>'."\n";
  330. print '</div>';
  331. print '<div class="right">';
  332. print '<input type="submit" class="button"'.(count($boxtoadd) ? '' : ' disabled').' value="'.$langs->trans("Activate").'">';
  333. print '</div>'."\n";
  334. print '</form>';
  335. print "\n".'<!-- End Boxes Available -->'."\n";
  336. //var_dump($boxactivated);
  337. print "<br>\n\n";
  338. print load_fiche_titre($langs->trans("BoxesActivated"));
  339. print '<div class="div-table-responsive-no-min">';
  340. print '<table class="tagtable liste">'."\n";
  341. print '<tr class="liste_titre">';
  342. print '<td width="300">'.$langs->trans("Box").'</td>';
  343. print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
  344. print '<td class="center" width="160">'.$langs->trans("ActiveOn").'</td>';
  345. print '<td class="center" width="60" colspan="2">'.$langs->trans("PositionByDefault").'</td>';
  346. print '<td class="center" width="80">'.$langs->trans("Disable").'</td>';
  347. print '</tr>'."\n";
  348. $box_order = 1;
  349. $foundrupture = 1;
  350. foreach ($boxactivated as $key => $box)
  351. {
  352. if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg))
  353. {
  354. $logo = $box->boximg;
  355. }
  356. else
  357. {
  358. $logo = preg_replace("/^object_/i", "", $box->boximg);
  359. }
  360. print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
  361. print '<tr class="oddeven">';
  362. print '<td>'.img_object("", $logo).' '.$langs->transnoentitiesnoconv($box->boxlabel);
  363. if (!empty($box->class) && preg_match('/graph_/', $box->class)) print ' ('.$langs->trans("Graph").')';
  364. print '</td>';
  365. print '<td>';
  366. if ($box->note == '(WarningUsingThisBoxSlowDown)')
  367. {
  368. $langs->load("errors");
  369. print img_warning('', 0).' '.$langs->trans("WarningUsingThisBoxSlowDown");
  370. }
  371. else print ($box->note ? $box->note : '&nbsp;');
  372. print '</td>';
  373. print '<td class="center">'.(empty($pos_name[$box->position]) ? '' : $langs->trans($pos_name[$box->position])).'</td>';
  374. $hasnext = ($key < (count($boxactivated) - 1));
  375. $hasprevious = ($key != 0);
  376. print '<td class="center">'.($key + 1).'</td>';
  377. print '<td class="center">';
  378. print ($hasnext ? '<a href="boxes.php?action=switch&amp;switchfrom='.$box->rowid.'&amp;switchto='.$boxactivated[$key + 1]->rowid.'">'.img_down().'</a>&nbsp;' : '');
  379. print ($hasprevious ? '<a href="boxes.php?action=switch&amp;switchfrom='.$box->rowid.'&amp;switchto='.$boxactivated[$key - 1]->rowid.'">'.img_up().'</a>' : '');
  380. print '</td>';
  381. print '<td class="center">';
  382. print '<a href="boxes.php?rowid='.$box->rowid.'&amp;action=delete">'.img_delete().'</a>';
  383. print '</td>';
  384. print '</tr>'."\n";
  385. }
  386. print '</table>';
  387. print '</div>';
  388. print '<br>';
  389. // Other parameters
  390. print "\n\n".'<!-- Other Const -->'."\n";
  391. print load_fiche_titre($langs->trans("Other"));
  392. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  393. print '<input type="hidden" name="token" value="'.newToken().'">';
  394. print '<input type="hidden" name="action" value="addconst">';
  395. print '<table class="noborder centpercent">';
  396. print '<tr class="liste_titre">';
  397. print '<td class="liste_titre">'.$langs->trans("Parameter").'</td>';
  398. print '<td class="liste_titre">'.$langs->trans("Value").'</td>';
  399. print '</tr>';
  400. print '<tr class="oddeven">';
  401. print '<td>';
  402. print $langs->trans("MaxNbOfLinesForBoxes");
  403. print '</td>'."\n";
  404. print '<td>';
  405. print '<input type="text" class="flat" size="6" name="MAIN_BOXES_MAXLINES" value="'.$conf->global->MAIN_BOXES_MAXLINES.'">';
  406. print '</td>';
  407. print '</tr>';
  408. // Activate FileCache - Developement
  409. if ($conf->global->MAIN_FEATURES_LEVEL == 2 || !empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
  410. print '<tr class="oddeven"><td width="35%">'.$langs->trans("EnableFileCache").'</td><td>';
  411. print $form->selectyesno('MAIN_ACTIVATE_FILECACHE', $conf->global->MAIN_ACTIVATE_FILECACHE, 1);
  412. print '</td>';
  413. print '</tr>';
  414. }
  415. print '</table>';
  416. print '<br>';
  417. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Save").'" name="Button"></div>';
  418. print '<br>';
  419. print '</form>';
  420. print "\n".'<!-- End Other Const -->'."\n";
  421. // End of page
  422. llxFooter();
  423. $db->close();