boxes.php 16 KB

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