boxes.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. } else {
  75. setEventMessages($db->lasterror(), null, 'errors');
  76. $error++;
  77. }
  78. }
  79. $distinctfkuser['0'] = '0'; // Add entry for fk_user = 0. We must use string as key and val
  80. foreach ($distinctfkuser as $fk_user)
  81. {
  82. if (!$error && $fk_user != '')
  83. {
  84. $arrayofexistingboxid = array();
  85. $nbboxonleft = $nbboxonright = 0;
  86. $sql = "SELECT box_id, box_order FROM ".MAIN_DB_PREFIX."boxes";
  87. $sql .= " 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. $arrayofexistingboxid[$obj->box_id] = 1;
  98. }
  99. } else dol_print_error($db);
  100. if (empty($arrayofexistingboxid[$boxid['value']])) {
  101. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (";
  102. $sql .= "box_id, position, box_order, fk_user, entity";
  103. $sql .= ") values (";
  104. $sql .= $boxid['value'].", ".$pos.", '".(($nbboxonleft > $nbboxonright) ? 'B01' : 'A01')."', ".$fk_user.", ".$conf->entity;
  105. $sql .= ")";
  106. dol_syslog("boxes.php activate box", LOG_DEBUG);
  107. $resql = $db->query($sql);
  108. if (!$resql)
  109. {
  110. setEventMessages($db->lasterror(), null, 'errors');
  111. $error++;
  112. }
  113. } else {
  114. dol_syslog("boxes.php activate box - already exists in database", LOG_DEBUG);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. if (!$error)
  122. {
  123. $db->commit();
  124. $action = '';
  125. } else {
  126. $db->rollback();
  127. }
  128. }
  129. if ($action == 'delete')
  130. {
  131. $sql = "SELECT box_id FROM ".MAIN_DB_PREFIX."boxes";
  132. $sql .= " WHERE rowid=".$rowid;
  133. $resql = $db->query($sql);
  134. $obj = $db->fetch_object($resql);
  135. if (!empty($obj->box_id))
  136. {
  137. $db->begin();
  138. // Remove all personalized setup when a box is activated or disabled (why removing all ? We removed only removed boxes)
  139. // $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_param";
  140. // $sql.= " WHERE param LIKE 'MAIN_BOXES_%'";
  141. // $resql = $db->query($sql);
  142. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  143. $sql .= " WHERE entity = ".$conf->entity;
  144. $sql .= " AND box_id=".$obj->box_id;
  145. $resql = $db->query($sql);
  146. $db->commit();
  147. }
  148. }
  149. if ($action == 'switch')
  150. {
  151. // We switch values of field box_order for the 2 lines of table boxes
  152. $db->begin();
  153. $objfrom = new ModeleBoxes($db);
  154. $objfrom->fetch($_GET["switchfrom"]);
  155. $objto = new ModeleBoxes($db);
  156. $objto->fetch($_GET["switchto"]);
  157. $resultupdatefrom = 0;
  158. $resultupdateto = 0;
  159. if (is_object($objfrom) && is_object($objto))
  160. {
  161. $newfirst = $objto->box_order;
  162. $newsecond = $objfrom->box_order;
  163. if ($newfirst == $newsecond)
  164. {
  165. $newsecondchar = preg_replace('/[0-9]+/', '', $newsecond);
  166. $newsecondnum = preg_replace('/[a-zA-Z]+/', '', $newsecond);
  167. $newsecond = sprintf("%s%02d", $newsecondchar ? $newsecondchar : 'A', $newsecondnum + 1);
  168. }
  169. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$newfirst."' WHERE rowid=".$objfrom->rowid;
  170. dol_syslog($sql);
  171. $resultupdatefrom = $db->query($sql);
  172. if (!$resultupdatefrom) { dol_print_error($db); }
  173. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$newsecond."' WHERE rowid=".$objto->rowid;
  174. dol_syslog($sql);
  175. $resultupdateto = $db->query($sql);
  176. if (!$resultupdateto) { dol_print_error($db); }
  177. }
  178. if ($resultupdatefrom && $resultupdateto)
  179. {
  180. $db->commit();
  181. } else {
  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. } elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1)))
  249. {
  250. $box_order = "B0".$record['box_order'];
  251. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  252. $resql = $db->query($sql);
  253. }
  254. } elseif (dol_strlen($record['box_order']) == 2)
  255. {
  256. if (preg_match("/[13579]{1}/", substr($record['box_order'], -1)))
  257. {
  258. $box_order = "A".$record['box_order'];
  259. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  260. $resql = $db->query($sql);
  261. } elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1)))
  262. {
  263. $box_order = "B".$record['box_order'];
  264. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  265. $resql = $db->query($sql);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. $db->free($resql);
  272. }
  273. // Available boxes to activate
  274. $boxtoadd = InfoBox::listBoxes($db, 'available', -1, null, $actives);
  275. // Activated boxes
  276. $boxactivated = InfoBox::listBoxes($db, 'activated', -1, null);
  277. print "<br>\n";
  278. print "\n\n".'<!-- Boxes Available -->'."\n";
  279. print load_fiche_titre($langs->trans("BoxesAvailable"), '', '');
  280. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
  281. print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
  282. print '<input type="hidden" name="action" value="add">'."\n";
  283. print '<div class="div-table-responsive-no-min">';
  284. print '<table class="tagtable liste centpercent">'."\n";
  285. print '<tr class="liste_titre">';
  286. print '<td width="300">'.$langs->trans("Box").'</td>';
  287. print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
  288. print '<td>'.$langs->trans("SourceFile").'</td>';
  289. print '<td width="160" class="center">'.$langs->trans("ActivateOn").'</td>';
  290. print "</tr>\n";
  291. foreach ($boxtoadd as $box)
  292. {
  293. if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg))
  294. {
  295. $logo = $box->boximg;
  296. } else {
  297. $logo = preg_replace("/^object_/i", "", $box->boximg);
  298. }
  299. print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
  300. print '<tr class="oddeven">'."\n";
  301. print '<td>'.img_object("", $logo, 'height="14px"').' '.$langs->transnoentitiesnoconv($box->boxlabel);
  302. if (!empty($box->class) && preg_match('/graph_/', $box->class)) print ' ('.$langs->trans("Graph").')';
  303. print '</td>'."\n";
  304. print '<td>';
  305. if ($box->note == '(WarningUsingThisBoxSlowDown)')
  306. {
  307. $langs->load("errors");
  308. print $langs->trans("WarningUsingThisBoxSlowDown");
  309. } else print ($box->note ? $box->note : '&nbsp;');
  310. print '</td>'."\n";
  311. print '<td>'.$box->sourcefile.'</td>'."\n";
  312. // For each possible position, an activation link is displayed if the box is not already active for that position
  313. print '<td class="center">';
  314. print $form->selectarray("boxid[".$box->box_id."][pos]", $pos_name, -1, 1, 0, 0, '', 1)."\n";
  315. print '<input type="hidden" name="boxid['.$box->box_id.'][value]" value="'.$box->box_id.'">'."\n";
  316. print '</td>';
  317. print '</tr>'."\n";
  318. }
  319. if (!count($boxtoadd) && count($boxactivated))
  320. {
  321. print '<tr><td class="opacitymedium" colspan="4">'.$langs->trans("AllWidgetsWereEnabled").'</td></tr>';
  322. }
  323. print '</table>'."\n";
  324. print '</div>';
  325. print '<div class="right">';
  326. print '<input type="submit" class="button"'.(count($boxtoadd) ? '' : ' disabled').' value="'.$langs->trans("Activate").'">';
  327. print '</div>'."\n";
  328. print '</form>';
  329. print "\n".'<!-- End Boxes Available -->'."\n";
  330. //var_dump($boxactivated);
  331. print "<br>\n\n";
  332. print load_fiche_titre($langs->trans("BoxesActivated"), '', '');
  333. print '<div class="div-table-responsive-no-min">';
  334. print '<table class="tagtable liste">'."\n";
  335. print '<tr class="liste_titre">';
  336. print '<td width="300">'.$langs->trans("Box").'</td>';
  337. print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
  338. print '<td class="center" width="160">'.$langs->trans("ActiveOn").'</td>';
  339. print '<td class="center" width="60" colspan="2">'.$langs->trans("PositionByDefault").'</td>';
  340. print '<td class="center" width="80">'.$langs->trans("Disable").'</td>';
  341. print '</tr>'."\n";
  342. $box_order = 1;
  343. $foundrupture = 1;
  344. foreach ($boxactivated as $key => $box)
  345. {
  346. if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg))
  347. {
  348. $logo = $box->boximg;
  349. } else {
  350. $logo = preg_replace("/^object_/i", "", $box->boximg);
  351. }
  352. print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
  353. print '<tr class="oddeven">';
  354. print '<td>'.img_object("", $logo, 'height="14px"').' '.$langs->transnoentitiesnoconv($box->boxlabel);
  355. if (!empty($box->class) && preg_match('/graph_/', $box->class)) print ' ('.$langs->trans("Graph").')';
  356. print '</td>';
  357. print '<td>';
  358. if ($box->note == '(WarningUsingThisBoxSlowDown)')
  359. {
  360. $langs->load("errors");
  361. print img_warning('', 0).' '.$langs->trans("WarningUsingThisBoxSlowDown");
  362. } else print ($box->note ? $box->note : '&nbsp;');
  363. print '</td>';
  364. print '<td class="center">'.(empty($pos_name[$box->position]) ? '' : $langs->trans($pos_name[$box->position])).'</td>';
  365. $hasnext = ($key < (count($boxactivated) - 1));
  366. $hasprevious = ($key != 0);
  367. print '<td class="center">'.($key + 1).'</td>';
  368. print '<td class="center">';
  369. print ($hasnext ? '<a href="boxes.php?action=switch&amp;switchfrom='.$box->rowid.'&amp;switchto='.$boxactivated[$key + 1]->rowid.'">'.img_down().'</a>&nbsp;' : '');
  370. print ($hasprevious ? '<a href="boxes.php?action=switch&amp;switchfrom='.$box->rowid.'&amp;switchto='.$boxactivated[$key - 1]->rowid.'">'.img_up().'</a>' : '');
  371. print '</td>';
  372. print '<td class="center">';
  373. print '<a href="boxes.php?rowid='.$box->rowid.'&amp;action=delete">'.img_delete().'</a>';
  374. print '</td>';
  375. print '</tr>'."\n";
  376. }
  377. print '</table>';
  378. print '</div>';
  379. print '<br>';
  380. // Other parameters
  381. print "\n\n".'<!-- Other Const -->'."\n";
  382. print load_fiche_titre($langs->trans("Other"), '', '');
  383. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  384. print '<input type="hidden" name="token" value="'.newToken().'">';
  385. print '<input type="hidden" name="action" value="addconst">';
  386. print '<table class="noborder centpercent">';
  387. print '<tr class="liste_titre">';
  388. print '<td class="liste_titre">'.$langs->trans("Parameter").'</td>';
  389. print '<td class="liste_titre">'.$langs->trans("Value").'</td>';
  390. print '</tr>';
  391. print '<tr class="oddeven">';
  392. print '<td>';
  393. print $langs->trans("MaxNbOfLinesForBoxes");
  394. print '</td>'."\n";
  395. print '<td>';
  396. print '<input type="text" class="flat" size="6" name="MAIN_BOXES_MAXLINES" value="'.$conf->global->MAIN_BOXES_MAXLINES.'">';
  397. print '</td>';
  398. print '</tr>';
  399. // Activate FileCache - Developement
  400. if ($conf->global->MAIN_FEATURES_LEVEL == 2 || !empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
  401. print '<tr class="oddeven"><td width="35%">'.$langs->trans("EnableFileCache").'</td><td>';
  402. print $form->selectyesno('MAIN_ACTIVATE_FILECACHE', $conf->global->MAIN_ACTIVATE_FILECACHE, 1);
  403. print '</td>';
  404. print '</tr>';
  405. }
  406. print '</table>';
  407. print '<br>';
  408. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Save").'" name="Button"></div>';
  409. print '<br>';
  410. print '</form>';
  411. print "\n".'<!-- End Other Const -->'."\n";
  412. // End of page
  413. llxFooter();
  414. $db->close();