boxes.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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@capnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/admin/boxes.php
  21. * \brief Page to setup boxes
  22. */
  23. require '../main.inc.php';
  24. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. $langs->load("admin");
  28. $langs->load("boxes");
  29. if (! $user->admin) accessforbidden();
  30. $rowid = GETPOST('rowid','int');
  31. $action = GETPOST('action','alpha');
  32. $errmesg='';
  33. // Define possible position of boxes
  34. $pos_name = getStaticMember('InfoBox','listOfPages');
  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. }
  43. if ($action == 'add')
  44. {
  45. $error=0;
  46. $db->begin();
  47. // Initialize distinct fkuser with all already existing values of fk_user (user that use a personalized view of boxes for page "pos")
  48. $distinctfkuser=array();
  49. if (! $error)
  50. {
  51. $sql = "SELECT fk_user";
  52. $sql.= " FROM ".MAIN_DB_PREFIX."user_param";
  53. $sql.= " WHERE param = 'MAIN_BOXES_".$db->escape(GETPOST("pos","alpha"))."' AND value = '1'";
  54. $sql.= " AND entity = ".$conf->entity;
  55. $resql = $db->query($sql);
  56. dol_syslog("boxes.php search fk_user to activate box for sql=".$sql);
  57. if ($resql)
  58. {
  59. $num = $db->num_rows($resql);
  60. $i=0;
  61. while ($i < $num)
  62. {
  63. $obj=$db->fetch_object($resql);
  64. $distinctfkuser[$obj->fk_user]=$obj->fk_user;
  65. $i++;
  66. }
  67. }
  68. else
  69. {
  70. $errmesg=$db->lasterror();
  71. $error++;
  72. }
  73. }
  74. $distinctfkuser['0']='0'; // Add entry for fk_user = 0. We must use string as key and val
  75. foreach($distinctfkuser as $fk_user)
  76. {
  77. if (! $error && $fk_user != '')
  78. {
  79. $nbboxonleft=$nbboxonright=0;
  80. $sql = "SELECT box_order FROM ".MAIN_DB_PREFIX."boxes WHERE position = ".GETPOST("pos","alpha")." AND fk_user = ".$fk_user." AND entity = ".$conf->entity;
  81. dol_syslog("boxes.php activate box sql=".$sql);
  82. $resql = $db->query($sql);
  83. if ($resql)
  84. {
  85. while($obj = $db->fetch_object($resql))
  86. {
  87. $boxorder=$obj->box_order;
  88. if (preg_match('/A/',$boxorder)) $nbboxonleft++;
  89. if (preg_match('/B/',$boxorder)) $nbboxonright++;
  90. }
  91. }
  92. else dol_print_error($db);
  93. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (";
  94. $sql.= "box_id, position, box_order, fk_user, entity";
  95. $sql.= ") values (";
  96. $sql.= GETPOST("boxid","int").", ".GETPOST("pos","alpha").", '".(($nbboxonleft > $nbboxonright) ? 'B01' : 'A01')."', ".$fk_user.", ".$conf->entity;
  97. $sql.= ")";
  98. dol_syslog("boxes.php activate box sql=".$sql);
  99. $resql = $db->query($sql);
  100. if (! $resql)
  101. {
  102. $errmesg=$db->lasterror();
  103. $error++;
  104. }
  105. }
  106. }
  107. if (! $error)
  108. {
  109. header("Location: boxes.php");
  110. $db->commit();
  111. exit;
  112. }
  113. else
  114. {
  115. $db->rollback();
  116. }
  117. }
  118. if ($action == 'delete')
  119. {
  120. $sql = "SELECT box_id FROM ".MAIN_DB_PREFIX."boxes";
  121. $sql.= " WHERE rowid=".$rowid;
  122. $resql = $db->query($sql);
  123. $obj=$db->fetch_object($resql);
  124. if (! empty($obj->box_id))
  125. {
  126. $db->begin();
  127. // Remove all personalized setup when a box is activated or disabled (why removing all ? We removed only removed boxes)
  128. // $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_param";
  129. // $sql.= " WHERE param LIKE 'MAIN_BOXES_%'";
  130. // $resql = $db->query($sql);
  131. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  132. $sql.= " WHERE entity = ".$conf->entity;
  133. $sql.= " AND box_id=".$obj->box_id;
  134. $resql = $db->query($sql);
  135. $db->commit();
  136. }
  137. }
  138. if ($action == 'switch')
  139. {
  140. // On permute les valeur du champ box_order des 2 lignes de la table boxes
  141. $db->begin();
  142. $objfrom=new ModeleBoxes($db);
  143. $objfrom->fetch($_GET["switchfrom"]);
  144. $objto=new ModeleBoxes($db);
  145. $objto->fetch($_GET["switchto"]);
  146. $resultupdatefrom=0;
  147. $resultupdateto=0;
  148. if (is_object($objfrom) && is_object($objto))
  149. {
  150. $newfirst=$objto->box_order;
  151. $newsecond=$objfrom->box_order;
  152. if ($newfirst == $newsecond)
  153. {
  154. $newsecondchar=preg_replace('/[0-9]+/','',$newsecond);
  155. $newsecondnum=preg_replace('/[a-zA-Z]+/','',$newsecond);
  156. $newsecond=sprintf("%s%02d",$newsecondchar?$newsecondchar:'A',$newsecondnum+1);
  157. }
  158. $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$newfirst."' WHERE rowid=".$objfrom->rowid;
  159. dol_syslog($sql);
  160. $resultupdatefrom = $db->query($sql);
  161. if (! $resultupdatefrom) { dol_print_error($db); }
  162. $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$newsecond."' WHERE rowid=".$objto->rowid;
  163. dol_syslog($sql);
  164. $resultupdateto = $db->query($sql);
  165. if (! $resultupdateto) { dol_print_error($db); }
  166. }
  167. if ($resultupdatefrom && $resultupdateto)
  168. {
  169. $db->commit();
  170. }
  171. else
  172. {
  173. $db->rollback();
  174. }
  175. }
  176. /*
  177. * View
  178. */
  179. $form=new Form($db);
  180. llxHeader('',$langs->trans("Boxes"));
  181. print_fiche_titre($langs->trans("Boxes"),'','setup');
  182. print $langs->trans("BoxesDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."<br>\n";
  183. dol_htmloutput_errors($errmesg);
  184. /*
  185. * Recherche des boites actives par defaut pour chaque position possible
  186. * On stocke les boites actives par defaut dans $boxes[position][id_boite]=1
  187. */
  188. $actives = array();
  189. $sql = "SELECT b.rowid, b.box_id, b.position, b.box_order,";
  190. $sql.= " bd.rowid as boxid";
  191. $sql.= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as bd";
  192. $sql.= " WHERE b.entity = ".$conf->entity;
  193. $sql.= " AND b.box_id = bd.rowid";
  194. $sql.= " AND b.fk_user=0";
  195. $sql.= " ORDER by b.position, b.box_order";
  196. dol_syslog("Search available boxes sql=".$sql, LOG_DEBUG);
  197. $resql = $db->query($sql);
  198. if ($resql)
  199. {
  200. $num = $db->num_rows($resql);
  201. $i = 0;
  202. $decalage=0;
  203. $var=false;
  204. while ($i < $num)
  205. {
  206. $var = ! $var;
  207. $obj = $db->fetch_object($resql);
  208. $boxes[$obj->position][$obj->box_id]=1;
  209. $i++;
  210. array_push($actives,$obj->box_id);
  211. if ($obj->box_order == '' || $obj->box_order == '0' || $decalage) $decalage++;
  212. // On renumerote l'ordre des boites si l'une d'elle est a ''
  213. // This occurs just after an insert.
  214. if ($decalage)
  215. {
  216. $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$decalage."' WHERE rowid=".$obj->rowid;
  217. $db->query($sql);
  218. }
  219. }
  220. if ($decalage)
  221. {
  222. // Si on a renumerote, on corrige champ box_order
  223. // This occurs just after an insert.
  224. $sql = "SELECT box_order";
  225. $sql.= " FROM ".MAIN_DB_PREFIX."boxes";
  226. $sql.= " WHERE entity = ".$conf->entity;
  227. $sql.= " AND LENGTH(box_order) <= 2";
  228. dol_syslog("Execute requests to renumber box order sql=".$sql);
  229. $result = $db->query($sql);
  230. if ($result)
  231. {
  232. while ($record = $db->fetch_array($result))
  233. {
  234. if (dol_strlen($record['box_order']) == 1)
  235. {
  236. if (preg_match("/[13579]{1}/",substr($record['box_order'],-1)))
  237. {
  238. $box_order = "A0".$record['box_order'];
  239. $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  240. $resql = $db->query($sql);
  241. }
  242. else if (preg_match("/[02468]{1}/",substr($record['box_order'],-1)))
  243. {
  244. $box_order = "B0".$record['box_order'];
  245. $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  246. $resql = $db->query($sql);
  247. }
  248. }
  249. else if (dol_strlen($record['box_order']) == 2)
  250. {
  251. if (preg_match("/[13579]{1}/",substr($record['box_order'],-1)))
  252. {
  253. $box_order = "A".$record['box_order'];
  254. $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  255. $resql = $db->query($sql);
  256. }
  257. else if (preg_match("/[02468]{1}/",substr($record['box_order'],-1)))
  258. {
  259. $box_order = "B".$record['box_order'];
  260. $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
  261. $resql = $db->query($sql);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. $db->free($resql);
  268. }
  269. // Available boxes to activate
  270. $boxtoadd=InfoBox::listBoxes($db,'available',-1,null,$actives);
  271. print "<br>\n";
  272. print_titre($langs->trans("BoxesAvailable"));
  273. print '<table class="noborder" width="100%">';
  274. print '<tr class="liste_titre">';
  275. print '<td width="300">'.$langs->trans("Box").'</td>';
  276. print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
  277. print '<td>'.$langs->trans("SourceFile").'</td>';
  278. print '<td width="160">'.$langs->trans("ActivateOn").'</td>';
  279. print "</tr>\n";
  280. $var=true;
  281. foreach($boxtoadd as $box)
  282. {
  283. $var=!$var;
  284. if (preg_match('/^([^@]+)@([^@]+)$/i',$box->boximg))
  285. {
  286. $logo = $box->boximg;
  287. }
  288. else
  289. {
  290. $logo=preg_replace("/^object_/i","",$box->boximg);
  291. }
  292. print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
  293. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  294. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  295. print '<tr '.$bc[$var].'>';
  296. print '<td>'.img_object("",$logo).' '.$langs->transnoentitiesnoconv($box->boxlabel);
  297. if (! empty($box->class) && preg_match('/graph_/',$box->class)) print ' ('.$langs->trans("Graph").')';
  298. print '</td>';
  299. print '<td>';
  300. if ($box->note == '(WarningUsingThisBoxSlowDown)')
  301. {
  302. $langs->load("errors");
  303. print $langs->trans("WarningUsingThisBoxSlowDown");
  304. }
  305. else print ($box->note?$box->note:'&nbsp;');
  306. print '</td>';
  307. print '<td>' . $box->sourcefile . '</td>';
  308. // Pour chaque position possible, on affiche un lien d'activation si boite non deja active pour cette position
  309. print '<td>';
  310. print $form->selectarray("pos",$pos_name,0,0,0,0,'',1);
  311. print '<input type="hidden" name="action" value="add">';
  312. print '<input type="hidden" name="boxid" value="'.$box->box_id.'">';
  313. print ' <input type="submit" class="button" name="button" value="'.$langs->trans("Activate").'">';
  314. print '</td>';
  315. print '</tr>';
  316. print '</form>';
  317. }
  318. print '</table>';
  319. // Activated boxes
  320. $boxactivated=InfoBox::listBoxes($db,'activated',-1,null);
  321. print "<br>\n\n";
  322. print_titre($langs->trans("BoxesActivated"));
  323. print '<table class="noborder" width="100%">';
  324. print '<tr class="liste_titre">';
  325. print '<td width="300">'.$langs->trans("Box").'</td>';
  326. print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
  327. print '<td align="center" width="160">'.$langs->trans("ActiveOn").'</td>';
  328. print '<td align="center" width="60" colspan="2">'.$langs->trans("PositionByDefault").'</td>';
  329. print '<td align="center" width="80">'.$langs->trans("Disable").'</td>';
  330. print '</tr>'."\n";
  331. $var=true;
  332. $box_order=1;
  333. $foundrupture=1;
  334. foreach($boxactivated as $key => $box)
  335. {
  336. $var = ! $var;
  337. if (preg_match('/^([^@]+)@([^@]+)$/i',$box->boximg))
  338. {
  339. $logo = $box->boximg;
  340. }
  341. else
  342. {
  343. $logo=preg_replace("/^object_/i","",$box->boximg);
  344. }
  345. print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
  346. print '<tr '.$bc[$var].'>';
  347. print '<td>'.img_object("",$logo).' '.$langs->transnoentitiesnoconv($box->boxlabel);
  348. if (! empty($box->class) && preg_match('/graph_/',$box->class)) print ' ('.$langs->trans("Graph").')';
  349. print '</td>';
  350. print '<td>';
  351. if ($box->note == '(WarningUsingThisBoxSlowDown)')
  352. {
  353. $langs->load("errors");
  354. print img_warning('',0).' '.$langs->trans("WarningUsingThisBoxSlowDown");
  355. }
  356. else print ($box->note?$box->note:'&nbsp;');
  357. print '</td>';
  358. print '<td align="center">' . (empty($pos_name[$box->position])?'':$langs->trans($pos_name[$box->position])) . '</td>';
  359. $hasnext=($key < (count($boxactivated)-1));
  360. $hasprevious=($key != 0);
  361. print '<td align="center">'.($key+1).'</td>';
  362. print '<td align="center">';
  363. print ($hasnext?'<a href="boxes.php?action=switch&switchfrom='.$box->rowid.'&switchto='.$boxactivated[$key+1]->rowid.'">'.img_down().'</a>&nbsp;':'');
  364. print ($hasprevious?'<a href="boxes.php?action=switch&switchfrom='.$box->rowid.'&switchto='.$boxactivated[$key-1]->rowid.'">'.img_up().'</a>':'');
  365. print '</td>';
  366. print '<td align="center">';
  367. print '<a href="boxes.php?rowid='.$box->rowid.'&amp;action=delete">'.img_delete().'</a>';
  368. print '</td>';
  369. print '</tr>'."\n";
  370. }
  371. print '</table><br>';
  372. // Other parameters
  373. print_titre($langs->trans("Other"));
  374. print '<table class="noborder" width="100%">';
  375. $var=false;
  376. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  377. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  378. print '<input type="hidden" name="action" value="addconst">';
  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 '<td class="liste_titre"></td>';
  383. print '</tr>';
  384. print '<tr '.$bc[$var].'>';
  385. print '<td>';
  386. print $langs->trans("MaxNbOfLinesForBoxes");
  387. print '</td>'."\n";
  388. print '<td>';
  389. print '<input type="text" class="flat" size="6" name="MAIN_BOXES_MAXLINES" value="'.$conf->global->MAIN_BOXES_MAXLINES.'">';
  390. print '</td>';
  391. print '<td align="right">';
  392. print '<input type="submit" class="button" value="'.$langs->trans("Save").'" name="Button">';
  393. print '</td>'."\n";
  394. print '</tr>';
  395. print '</form>';
  396. print '</table>';
  397. llxFooter();
  398. $db->close();