boxes.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2022 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. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
  28. include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('admin', 'boxes', 'accountancy'));
  31. if (!$user->admin) {
  32. accessforbidden();
  33. }
  34. $rowid = GETPOST('rowid', 'int');
  35. $action = GETPOST('action', 'aZ09');
  36. // Define possible position of boxes
  37. $arrayofhomepages = InfoBox::getListOfPagesForBoxes();
  38. $boxes = array();
  39. /*
  40. * Actions
  41. */
  42. if ($action == 'addconst') {
  43. dolibarr_set_const($db, "MAIN_BOXES_MAXLINES", GETPOST("MAIN_BOXES_MAXLINES", 'int'), '', 0, '', $conf->entity);
  44. dolibarr_set_const($db, "MAIN_ACTIVATE_FILECACHE", GETPOST("MAIN_ACTIVATE_FILECACHE", 'alpha'), 'chaine', 0, '', $conf->entity);
  45. }
  46. if ($action == 'add') {
  47. $error = 0;
  48. $boxids = GETPOST('boxid', 'array');
  49. $db->begin();
  50. if (is_array($boxids)) {
  51. foreach ($boxids as $boxid) {
  52. if (is_numeric($boxid['pos']) && $boxid['pos'] >= 0) { // 0=Home, 1=...
  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. $sql = "SELECT fk_user";
  58. $sql .= " FROM ".MAIN_DB_PREFIX."user_param";
  59. $sql .= " WHERE param = 'MAIN_BOXES_".$db->escape($pos)."' AND value = '1'";
  60. $sql .= " AND entity = ".$conf->entity;
  61. dol_syslog("boxes.php search fk_user to activate box for", LOG_DEBUG);
  62. $resql = $db->query($sql);
  63. if ($resql) {
  64. $num = $db->num_rows($resql);
  65. $i = 0;
  66. while ($i < $num) {
  67. $obj = $db->fetch_object($resql);
  68. $distinctfkuser[$obj->fk_user] = $obj->fk_user;
  69. $i++;
  70. }
  71. } else {
  72. setEventMessages($db->lasterror(), null, 'errors');
  73. $error++;
  74. }
  75. }
  76. $distinctfkuser['0'] = '0'; // Add entry for fk_user = 0. We must use string as key and val
  77. foreach ($distinctfkuser as $fk_user) {
  78. if (!$error && $fk_user != '') {
  79. $arrayofexistingboxid = array();
  80. $nbboxonleft = $nbboxonright = 0;
  81. $sql = "SELECT box_id, box_order FROM ".MAIN_DB_PREFIX."boxes";
  82. $sql .= " WHERE position = ".((int) $pos)." AND fk_user = ".((int) $fk_user)." AND entity = ".((int) $conf->entity);
  83. dol_syslog("boxes.php activate box", LOG_DEBUG);
  84. $resql = $db->query($sql);
  85. if ($resql) {
  86. while ($obj = $db->fetch_object($resql)) {
  87. $boxorder = $obj->box_order;
  88. if (preg_match('/A/', $boxorder)) {
  89. $nbboxonleft++;
  90. }
  91. if (preg_match('/B/', $boxorder)) {
  92. $nbboxonright++;
  93. }
  94. $arrayofexistingboxid[$obj->box_id] = 1;
  95. }
  96. } else {
  97. dol_print_error($db);
  98. }
  99. if (empty($arrayofexistingboxid[$boxid['value']])) {
  100. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (";
  101. $sql .= "box_id, position, box_order, fk_user, entity";
  102. $sql .= ") VALUES (";
  103. $sql .= ((int) $boxid['value']).", ".((int) $pos).", '".(($nbboxonleft > $nbboxonright) ? 'B01' : 'A01')."', ".((int) $fk_user).", ".$conf->entity;
  104. $sql .= ")";
  105. dol_syslog("boxes.php activate box", LOG_DEBUG);
  106. $resql = $db->query($sql);
  107. if (!$resql) {
  108. setEventMessages($db->lasterror(), null, 'errors');
  109. $error++;
  110. }
  111. } else {
  112. dol_syslog("boxes.php activate box - already exists in database", LOG_DEBUG);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. if (!$error) {
  120. $db->commit();
  121. $action = '';
  122. } else {
  123. $db->rollback();
  124. }
  125. }
  126. if ($action == 'delete') {
  127. $sql = "SELECT box_id FROM ".MAIN_DB_PREFIX."boxes";
  128. $sql .= " WHERE rowid=".((int) $rowid);
  129. $resql = $db->query($sql);
  130. $obj = $db->fetch_object($resql);
  131. if (!empty($obj->box_id)) {
  132. $db->begin();
  133. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  134. $sql .= " WHERE entity = ".$conf->entity;
  135. $sql .= " AND box_id=".((int) $obj->box_id);
  136. $resql = $db->query($sql);
  137. $db->commit();
  138. }
  139. }
  140. if ($action == 'switch') {
  141. // We switch values of field box_order for the 2 lines of table boxes
  142. $db->begin();
  143. $objfrom = new ModeleBoxes($db);
  144. $objfrom->fetch(GETPOST("switchfrom", 'int'));
  145. $objto = new ModeleBoxes($db);
  146. $objto->fetch(GETPOST('switchto', 'int'));
  147. $resultupdatefrom = 0;
  148. $resultupdateto = 0;
  149. if (is_object($objfrom) && is_object($objto)) {
  150. $newfirst = $objto->box_order;
  151. $newsecond = $objfrom->box_order;
  152. if ($newfirst == $newsecond) {
  153. $newsecondchar = preg_replace('/[0-9]+/', '', $newsecond);
  154. $newsecondnum = preg_replace('/[a-zA-Z]+/', '', $newsecond);
  155. $newsecond = sprintf("%s%02d", $newsecondchar ? $newsecondchar : 'A', $newsecondnum + 1);
  156. }
  157. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$db->escape($newfirst)."' WHERE rowid=".((int) $objfrom->rowid);
  158. dol_syslog($sql);
  159. $resultupdatefrom = $db->query($sql);
  160. if (!$resultupdatefrom) {
  161. dol_print_error($db);
  162. }
  163. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$db->escape($newsecond)."' WHERE rowid=".((int) $objto->rowid);
  164. dol_syslog($sql);
  165. $resultupdateto = $db->query($sql);
  166. if (!$resultupdateto) {
  167. dol_print_error($db);
  168. }
  169. }
  170. if ($resultupdatefrom && $resultupdateto) {
  171. $db->commit();
  172. } else {
  173. $db->rollback();
  174. }
  175. }
  176. /*
  177. * View
  178. */
  179. $form = new Form($db);
  180. llxHeader('', $langs->trans("Boxes"));
  181. print load_fiche_titre($langs->trans("Boxes"), '', 'title_setup');
  182. print '<span class="opacitymedium">'.$langs->trans("BoxesDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."</span><br>\n";
  183. /*
  184. * Search for the default active boxes for each possible position
  185. * We store the active boxes by default in $boxes[position][id_boite]=1
  186. */
  187. $actives = array();
  188. $sql = "SELECT b.rowid, b.box_id, b.position, b.box_order,";
  189. $sql .= " bd.rowid as boxid";
  190. $sql .= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as bd";
  191. $sql .= " WHERE b.box_id = bd.rowid";
  192. $sql .= " AND b.entity IN (0,".$conf->entity.")";
  193. $sql .= " AND b.fk_user=0";
  194. $sql .= " ORDER by b.position, b.box_order";
  195. //print $sql;
  196. dol_syslog("Search available boxes", LOG_DEBUG);
  197. $resql = $db->query($sql);
  198. if ($resql) {
  199. $num = $db->num_rows($resql);
  200. // Check record to know if we must recalculate sort order
  201. $i = 0;
  202. $decalage = 0;
  203. while ($i < $num) {
  204. $obj = $db->fetch_object($resql);
  205. $boxes[$obj->position][$obj->box_id] = 1;
  206. $i++;
  207. array_push($actives, $obj->box_id);
  208. if ($obj->box_order == '' || $obj->box_order == '0' || $decalage) {
  209. $decalage++;
  210. }
  211. // We renumber the order of the boxes if one of them is in ''
  212. // This occurs just after an insert.
  213. if ($decalage) {
  214. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$db->escape($decalage)."' WHERE rowid=".((int) $obj->rowid);
  215. $db->query($sql);
  216. }
  217. }
  218. if ($decalage) {
  219. // If we have renumbered, we correct the field box_order
  220. // This occurs just after an insert.
  221. $sql = "SELECT box_order";
  222. $sql .= " FROM ".MAIN_DB_PREFIX."boxes";
  223. $sql .= " WHERE entity = ".$conf->entity;
  224. $sql .= " AND LENGTH(box_order) <= 2";
  225. dol_syslog("Execute requests to renumber box order", LOG_DEBUG);
  226. $result = $db->query($sql);
  227. if ($result) {
  228. while ($record = $db->fetch_array($result)) {
  229. if (dol_strlen($record['box_order']) == 1) {
  230. if (preg_match("/[13579]{1}/", substr($record['box_order'], -1))) {
  231. $box_order = "A0".$record['box_order'];
  232. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$db->escape($box_order)."' WHERE entity = ".$conf->entity." AND box_order = '".$db->escape($record['box_order'])."'";
  233. $resql = $db->query($sql);
  234. } elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1))) {
  235. $box_order = "B0".$record['box_order'];
  236. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$db->escape($box_order)."' WHERE entity = ".$conf->entity." AND box_order = '".$db->escape($record['box_order'])."'";
  237. $resql = $db->query($sql);
  238. }
  239. } elseif (dol_strlen($record['box_order']) == 2) {
  240. if (preg_match("/[13579]{1}/", substr($record['box_order'], -1))) {
  241. $box_order = "A".$record['box_order'];
  242. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$db->escape($box_order)."' WHERE entity = ".$conf->entity." AND box_order = '".$db->escape($record['box_order'])."'";
  243. $resql = $db->query($sql);
  244. } elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1))) {
  245. $box_order = "B".$record['box_order'];
  246. $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$db->escape($box_order)."' WHERE entity = ".$conf->entity." AND box_order = '".$db->escape($record['box_order'])."'";
  247. $resql = $db->query($sql);
  248. }
  249. }
  250. }
  251. }
  252. }
  253. $db->free($resql);
  254. }
  255. // Available boxes to activate
  256. $boxtoadd = InfoBox::listBoxes($db, 'available', -1, null, $actives);
  257. // Activated boxes
  258. $boxactivated = InfoBox::listBoxes($db, 'activated', -1, null);
  259. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
  260. print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
  261. print '<input type="hidden" name="action" value="add">'."\n";
  262. print '<br>';
  263. print '<div class="div-table-responsive-no-min">';
  264. print '<table class="tagtable liste centpercent">'."\n";
  265. print '<tr class="liste_titre">';
  266. print '<td>'.$langs->trans("Box").'</td>';
  267. print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
  268. print '<td></td>';
  269. print '<td class="center" width="160">'.$langs->trans("ActivatableOn").'</td>';
  270. print '<td class="center" width="60" colspan="2">'.$langs->trans("PositionByDefault").'</td>';
  271. print '<td class="center" width="80">'.$langs->trans("Disable").'</td>';
  272. print '</tr>'."\n";
  273. print "\n\n".'<!-- Boxes Available -->'."\n";
  274. foreach ($boxtoadd as $box) {
  275. if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg)) {
  276. $logo = $box->boximg;
  277. } else {
  278. $logo = preg_replace("/^object_/i", "", $box->boximg);
  279. }
  280. print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
  281. print '<tr class="oddeven" style="height:3em !important;">'."\n";
  282. print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv($box->boxlabel)).'">'.img_object("", $logo, 'class="pictofixedwidth" height="14px"').' '.$langs->transnoentitiesnoconv($box->boxlabel);
  283. if (!empty($box->class) && preg_match('/graph_/', $box->class)) {
  284. print img_picto('', 'graph', 'class="paddingleft"');
  285. }
  286. if (!empty($box->version)) {
  287. if ($box->version == 'experimental') {
  288. print ' <span class="opacitymedium">('.$langs->trans("Experimental").')</span>';
  289. } elseif ($box->version == 'development') {
  290. print ' <span class="opacitymedium">('.$langs->trans("Development").')</span>';
  291. }
  292. }
  293. print '</td>'."\n";
  294. print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($box->note).'">';
  295. if ($box->note == '(WarningUsingThisBoxSlowDown)') {
  296. $langs->load("errors");
  297. print $langs->trans("WarningUsingThisBoxSlowDown");
  298. } else {
  299. print ($box->note ? $box->note : '&nbsp;');
  300. }
  301. print '</td>'."\n";
  302. print '<td>';
  303. print $form->textwithpicto('', $langs->trans("SourceFile").' : '.$box->sourcefile);
  304. print '</td>'."\n";
  305. // For each possible position, an activation link is displayed if the box is not already active for that position
  306. print '<td class="center">';
  307. print $form->selectarray("boxid[".$box->box_id."][pos]", $arrayofhomepages, -1, 1, 0, 0, '', 1, 0, 0, '', 'minwidth75', 1)."\n";
  308. print '<input type="hidden" name="boxid['.$box->box_id.'][value]" value="'.$box->box_id.'">'."\n";
  309. print '</td>';
  310. print '<td>';
  311. print '</td>';
  312. print '<td>';
  313. print '</td>';
  314. print '<td>';
  315. print '<input type="submit" class="button small smallpaddingimp" value="'.$langs->trans("Activate").'">';
  316. print '</td>';
  317. print '</tr>'."\n";
  318. }
  319. print "\n".'<!-- End Boxes Available -->'."\n";
  320. $box_order = 1;
  321. $foundrupture = 1;
  322. foreach ($boxactivated as $key => $box) {
  323. if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg)) {
  324. $logo = $box->boximg;
  325. } else {
  326. $logo = preg_replace("/^object_/i", "", $box->boximg);
  327. }
  328. print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
  329. print '<tr class="oddeven" style="height:3em !important;">';
  330. print '<td>'.img_object("", $logo, 'class="pictofixedwidth" height="14px"').' '.$langs->transnoentitiesnoconv($box->boxlabel);
  331. if (!empty($box->class) && preg_match('/graph_/', $box->class)) {
  332. print img_picto('', 'graph', 'class="paddingleft"');
  333. }
  334. if (!empty($box->version)) {
  335. if ($box->version == 'experimental') {
  336. print ' <span class="opacitymedium">('.$langs->trans("Experimental").')</span>';
  337. } elseif ($box->version == 'development') {
  338. print ' <span class="opacitymedium">('.$langs->trans("Development").')</span>';
  339. }
  340. }
  341. print '</td>';
  342. $langs->load("errors");
  343. print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($box->note == '(WarningUsingThisBoxSlowDown)' ? $langs->trans("WarningUsingThisBoxSlowDown") : $box->note).'">';
  344. if ($box->note == '(WarningUsingThisBoxSlowDown)') {
  345. print img_warning('', 0).' '.$langs->trans("WarningUsingThisBoxSlowDown");
  346. } else {
  347. print ($box->note ? $box->note : '&nbsp;');
  348. }
  349. print '</td>';
  350. print '<td>';
  351. print $form->textwithpicto('', $langs->trans("SourceFile").' : '.$box->sourcefile);
  352. print '</td>'."\n";
  353. print '<td class="center">'.(empty($arrayofhomepages[$box->position]) ? '' : $langs->trans($arrayofhomepages[$box->position])).'</td>';
  354. $hasnext = ($key < (count($boxactivated) - 1));
  355. $hasprevious = ($key != 0);
  356. print '<td class="center">'.($key + 1).'</td>';
  357. print '<td class="center nowraponall">';
  358. print ($hasnext ? '<a class="reposition" href="boxes.php?action=switch&token='.newToken().'&switchfrom='.$box->rowid.'&switchto='.$boxactivated[$key + 1]->rowid.'">'.img_down().'</a>&nbsp;' : '');
  359. print ($hasprevious ? '<a class="reposition" href="boxes.php?action=switch&token='.newToken().'&switchfrom='.$box->rowid.'&switchto='.$boxactivated[$key - 1]->rowid.'">'.img_up().'</a>' : '');
  360. print '</td>';
  361. print '<td class="center">';
  362. print '<a class="reposition" href="boxes.php?rowid='.$box->rowid.'&action=delete&token='.newToken().'">'.img_delete().'</a>';
  363. print '</td>';
  364. print '</tr>'."\n";
  365. }
  366. print '</table>';
  367. print '</div>';
  368. print '<br>';
  369. print '</form>';
  370. // Other parameters
  371. print "\n\n".'<!-- Other Const -->'."\n";
  372. print load_fiche_titre($langs->trans("Other"), '', '');
  373. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  374. print '<input type="hidden" name="token" value="'.newToken().'">';
  375. print '<input type="hidden" name="action" value="addconst">';
  376. print '<input type="hidden" name="page_y" value="">';
  377. print '<div class="div-table-responsive-no-min">';
  378. print '<table class="noborder centpercent">';
  379. print '<tr class="liste_titre">';
  380. print '<td class="liste_titre">'.$langs->trans("Parameter").'</td>';
  381. print '<td class="liste_titre">'.$langs->trans("Value").'</td>';
  382. print '</tr>';
  383. print '<tr class="oddeven">';
  384. print '<td>';
  385. print $langs->trans("MaxNbOfLinesForBoxes");
  386. print '</td>'."\n";
  387. print '<td>';
  388. print '<input type="text" class="flat" size="6" name="MAIN_BOXES_MAXLINES" value="'.(getDolGlobalString('MAIN_BOXES_MAXLINES')).'">';
  389. print '</td>';
  390. print '</tr>';
  391. // Activate FileCache (so content of file boxes are stored into a cache file int boxes/temp for 3600 seconds)
  392. print '<tr class="oddeven"><td>'.$langs->trans("EnableFileCache").'</td><td>';
  393. print $form->selectyesno('MAIN_ACTIVATE_FILECACHE', getDolGlobalInt('MAIN_ACTIVATE_FILECACHE', 0), 1);
  394. print '</td>';
  395. print '</tr>';
  396. print '</table>';
  397. print '</div>';
  398. print $form->buttonsSaveCancel("Save", '', array(), 0, 'reposition');
  399. print '</form>';
  400. print "\n".'<!-- End Other Const -->'."\n";
  401. // End of page
  402. llxFooter();
  403. $db->close();