PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/boxes.php

https://github.com/asterix14/dolibarr
PHP | 419 lines | 299 code | 54 blank | 66 comment | 32 complexity | 8d932a6360b4bf26a7e3635fb97f343e MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
  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 2 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/boxes.php
  21. * \brief File of class to manage widget boxes
  22. * \author Rodolphe Qiedeville
  23. * \author Laurent Destailleur
  24. */
  25. /**
  26. * Show a HTML Tab with boxes of a particular area including personalized choices of user
  27. *
  28. * @param User $user Object User
  29. * @param String $areacode Code of area for pages (0=value for Home page)
  30. * @return int <0 if KO, Nb of boxes shown of OK (0 to n)
  31. */
  32. function printBoxesArea($user,$areacode)
  33. {
  34. global $conf,$langs,$db;
  35. $infobox=new InfoBox($db);
  36. $boxarray=$infobox->listboxes($areacode,$user);
  37. if (count($boxarray))
  38. {
  39. print load_fiche_titre($langs->trans("OtherInformationsBoxes"),'','','','otherboxes');
  40. print '<table width="100%" class="notopnoleftnoright">';
  41. print '<tr><td class="notopnoleftnoright">'."\n";
  42. print '<div class="fichehalfleft">';
  43. print "\n<!-- Box left container -->\n";
  44. print '<div id="left" class="connectedSortable">'."\n";
  45. $ii=0;
  46. foreach ($boxarray as $key => $box)
  47. {
  48. if (preg_match('/^A/i',$box->box_order)) // column A
  49. {
  50. $ii++;
  51. //print 'box_id '.$boxarray[$ii]->box_id.' ';
  52. //print 'box_order '.$boxarray[$ii]->box_order.'<br>';
  53. // Affichage boite key
  54. $box->loadBox($conf->box_max_lines);
  55. $box->showBox();
  56. }
  57. }
  58. $emptybox=new ModeleBoxes($db);
  59. $emptybox->box_id='A';
  60. $emptybox->info_box_head=array();
  61. $emptybox->info_box_contents=array();
  62. $emptybox->showBox(array(),array());
  63. print "</div>\n";
  64. print "<!-- End box container -->\n";
  65. print '</div><div class="fichehalfright"><div class="ficheaddleft">';
  66. print "\n<!-- Box right container -->\n";
  67. print '<div id="right" class="connectedSortable">'."\n";
  68. $ii=0;
  69. foreach ($boxarray as $key => $box)
  70. {
  71. if (preg_match('/^B/i',$box->box_order)) // colonne B
  72. {
  73. $ii++;
  74. //print 'box_id '.$boxarray[$ii]->box_id.' ';
  75. //print 'box_order '.$boxarray[$ii]->box_order.'<br>';
  76. // Affichage boite key
  77. $box->loadBox($conf->box_max_lines);
  78. $box->showBox();
  79. }
  80. }
  81. $emptybox=new ModeleBoxes($db);
  82. $emptybox->box_id='B';
  83. $emptybox->info_box_head=array();
  84. $emptybox->info_box_contents=array();
  85. $emptybox->showBox(array(),array());
  86. print "</div>\n";
  87. print "<!-- End box container -->\n";
  88. print '</div></div>';
  89. print "\n";
  90. print "</td></tr>";
  91. print "</table>";
  92. if ($conf->use_javascript_ajax)
  93. {
  94. print "\n";
  95. print '<script type="text/javascript" language="javascript">';
  96. print 'jQuery(function() {
  97. jQuery("#left, #right").sortable({
  98. /* placeholder: \'ui-state-highlight\', */
  99. handle: \'.boxhandle\',
  100. revert: \'invalid\',
  101. items: \'.box\',
  102. containment: \'.fiche\',
  103. connectWith: \'.connectedSortable\',
  104. stop: function(event, ui) {
  105. updateOrder();
  106. }
  107. });
  108. });
  109. ';
  110. print "\n";
  111. print 'function updateOrder(){'."\n";
  112. print 'var left_list = cleanSerialize(jQuery("#left").sortable("serialize" ));'."\n";
  113. print 'var right_list = cleanSerialize(jQuery("#right").sortable("serialize" ));'."\n";
  114. print 'var boxorder = \'A:\' + left_list + \'-B:\' + right_list;'."\n";
  115. //print 'alert(\'boxorder=\' + boxorder);';
  116. print 'var userid = \''.$user->id.'\';'."\n";
  117. print 'jQuery.get(\'core/ajax/box.php?boxorder=\'+boxorder+\'&userid=\'+'.$user->id.');'."\n";
  118. print '}'."\n";
  119. print '</script>'."\n";
  120. }
  121. }
  122. return count($boxarray);
  123. }
  124. /**
  125. * Class to manage boxes on pages
  126. */
  127. class InfoBox
  128. {
  129. var $db;
  130. /**
  131. * Constructor
  132. *
  133. * @param DoliDb $DB Database handler
  134. */
  135. function InfoBox($DB)
  136. {
  137. $this->db=$DB;
  138. }
  139. /**
  140. * Return array of boxes qualified for area and user
  141. *
  142. * @param string $zone Name or area (0 for Homepage, ...)
  143. * @param User $user Objet user
  144. * @return array Array of boxes
  145. */
  146. function listBoxes($zone,$user)
  147. {
  148. global $conf;
  149. $boxes=array();
  150. $confuserzone='MAIN_BOXES_'.$zone;
  151. if ($user->id && $user->conf->$confuserzone)
  152. {
  153. // Get list of boxes of a particular user (if this one has its own list)
  154. $sql = "SELECT b.rowid, b.box_id, b.position, b.box_order, b.fk_user,";
  155. $sql.= " d.file, d.note";
  156. $sql.= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as d";
  157. $sql.= " WHERE b.box_id = d.rowid";
  158. $sql.= " AND d.entity = ".$conf->entity;
  159. $sql.= " AND b.position = ".$zone;
  160. $sql.= " AND b.fk_user = ".$user->id;
  161. $sql.= " ORDER BY b.box_order";
  162. dol_syslog("InfoBox::listBoxes get user box list sql=".$sql, LOG_DEBUG);
  163. $result = $this->db->query($sql);
  164. if ($result)
  165. {
  166. $num = $this->db->num_rows($result);
  167. $j = 0;
  168. while ($j < $num)
  169. {
  170. $obj = $this->db->fetch_object($result);
  171. if (preg_match('/^([^@]+)@([^@]+)$/i',$obj->file,$regs))
  172. {
  173. $boxname = $regs[1];
  174. $module = $regs[2];
  175. $sourcefile = dol_buildpath("/".$module."/core/boxes/".$boxname.".php");
  176. }
  177. else
  178. {
  179. $boxname=preg_replace('/.php$/i','',$obj->file);
  180. $sourcefile = DOL_DOCUMENT_ROOT."/core/boxes/".$boxname.".php";
  181. }
  182. include_once($sourcefile);
  183. $box=new $boxname($this->db,$obj->note);
  184. $box->rowid=$obj->rowid;
  185. $box->box_id=$obj->box_id;
  186. $box->position=$obj->position;
  187. $box->box_order=$obj->box_order;
  188. $box->fk_user=$obj->fk_user;
  189. $enabled=true;
  190. if ($box->depends && count($box->depends) > 0)
  191. {
  192. foreach($box->depends as $module)
  193. {
  194. //print $module.'<br>';
  195. if (empty($conf->$module->enabled)) $enabled=false;
  196. }
  197. }
  198. if ($enabled) $boxes[]=$box;
  199. $j++;
  200. }
  201. }
  202. else
  203. {
  204. $this->error=$this->db->error();
  205. dol_syslog("InfoBox::listBoxes Error ".$this->error, LOG_ERR);
  206. return array();
  207. }
  208. }
  209. else
  210. {
  211. // Recupere liste des boites active par defaut pour tous
  212. $sql = "SELECT b.rowid, b.box_id, b.position, b.box_order, b.fk_user,";
  213. $sql.= " d.file, d.note";
  214. $sql.= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as d";
  215. $sql.= " WHERE b.box_id = d.rowid";
  216. $sql.= " AND d.entity = ".$conf->entity;
  217. $sql.= " AND b.position = ".$zone;
  218. $sql.= " AND b.fk_user = 0";
  219. $sql.= " ORDER BY b.box_order";
  220. dol_syslog("InfoBox::listBoxes get default box list sql=".$sql, LOG_DEBUG);
  221. $result = $this->db->query($sql);
  222. if ($result)
  223. {
  224. $num = $this->db->num_rows($result);
  225. $j = 0;
  226. while ($j < $num)
  227. {
  228. $obj = $this->db->fetch_object($result);
  229. if (preg_match('/^([^@]+)@([^@]+)$/i',$obj->file,$regs))
  230. {
  231. $boxname = $regs[1];
  232. $module = $regs[2];
  233. $sourcefile = "/".$module."/core/boxes/".$boxname.".php";
  234. }
  235. else
  236. {
  237. $boxname=preg_replace('/.php$/i','',$obj->file);
  238. $sourcefile = "/core/boxes/".$boxname.".php";
  239. }
  240. dol_include_once($sourcefile);
  241. $box=new $boxname($db,$obj->note);
  242. $box->rowid=$obj->rowid;
  243. $box->box_id=$obj->box_id;
  244. $box->position=$obj->position;
  245. $box->box_order=$obj->box_order;
  246. if (is_numeric($box->box_order))
  247. {
  248. if ($box->box_order % 2 == 1) $box->box_order='A'.$box->box_order;
  249. elseif ($box->box_order % 2 == 0) $box->box_order='B'.$box->box_order;
  250. }
  251. $box->fk_user=$obj->fk_user;
  252. $enabled=true;
  253. if ($box->depends && count($box->depends) > 0)
  254. {
  255. foreach($box->depends as $module)
  256. {
  257. //print $boxname.'-'.$module.'<br>';
  258. if (empty($conf->$module->enabled)) $enabled=false;
  259. }
  260. }
  261. if ($enabled) $boxes[]=$box;
  262. $j++;
  263. }
  264. }
  265. else
  266. {
  267. $this->error=$this->db->error();
  268. dol_syslog("InfoBox::listBoxes Error ".$this->error, LOG_ERR);
  269. return array();
  270. }
  271. }
  272. return $boxes;
  273. }
  274. /**
  275. * Save order of boxes for area and user
  276. *
  277. * @param string $zone Name of area (0 for Homepage, ...)
  278. * @param string $boxorder List of boxes with correct order 'A:123,456,...-B:789,321...'
  279. * @param int $userid Id of user
  280. * @return int <0 if KO, >= 0 if OK
  281. */
  282. function saveboxorder($zone,$boxorder,$userid=0)
  283. {
  284. global $conf;
  285. require_once(DOL_DOCUMENT_ROOT."/core/lib/functions2.lib.php");
  286. dol_syslog("InfoBoxes::saveboxorder zone=".$zone." user=".$userid);
  287. if (! $userid || $userid == 0) return 0;
  288. $user = new User($this->db);
  289. $user->id=$userid;
  290. $this->db->begin();
  291. // Sauve parametre indiquant que le user a une
  292. $confuserzone='MAIN_BOXES_'.$zone;
  293. $tab[$confuserzone]=1;
  294. if (dol_set_user_param($this->db, $conf, $user, $tab) < 0)
  295. {
  296. $this->error=$this->db->lasterror();
  297. $this->db->rollback();
  298. return -3;
  299. }
  300. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  301. $sql.= " USING ".MAIN_DB_PREFIX."boxes, ".MAIN_DB_PREFIX."boxes_def";
  302. $sql.= " WHERE ".MAIN_DB_PREFIX."boxes.box_id = ".MAIN_DB_PREFIX."boxes_def.rowid";
  303. $sql.= " AND ".MAIN_DB_PREFIX."boxes_def.entity = ".$conf->entity;
  304. $sql.= " AND ".MAIN_DB_PREFIX."boxes.fk_user = ".$userid;
  305. $sql.= " AND ".MAIN_DB_PREFIX."boxes.position = ".$zone;
  306. dol_syslog("InfoBox::saveboxorder sql=".$sql);
  307. $result = $this->db->query($sql);
  308. if ($result)
  309. {
  310. $colonnes=explode('-',$boxorder);
  311. foreach ($colonnes as $collist)
  312. {
  313. $part=explode(':',$collist);
  314. $colonne=$part[0];
  315. $list=$part[1];
  316. dol_syslog('InfoBox::saveboxorder column='.$colonne.' list='.$list);
  317. $i=0;
  318. $listarray=explode(',',$list);
  319. foreach ($listarray as $id)
  320. {
  321. if (is_numeric($id))
  322. {
  323. //dol_syslog("aaaaa".count($listarray));
  324. $i++;
  325. $ii=sprintf('%02d',$i);
  326. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes";
  327. $sql.= "(box_id, position, box_order, fk_user)";
  328. $sql.= " values (";
  329. $sql.= " ".$id.",";
  330. $sql.= " ".$zone.",";
  331. $sql.= " '".$colonne.$ii."',";
  332. $sql.= " ".$userid;
  333. $sql.= ")";
  334. dol_syslog("InfoBox::saveboxorder sql=".$sql);
  335. $result = $this->db->query($sql);
  336. if ($result < 0)
  337. {
  338. $error++;
  339. break;
  340. }
  341. }
  342. }
  343. }
  344. if ($error)
  345. {
  346. $this->error=$this->db->error();
  347. $this->db->rollback();
  348. return -2;
  349. }
  350. else
  351. {
  352. $this->db->commit();
  353. return 1;
  354. }
  355. }
  356. else
  357. {
  358. $this->error=$this->db->lasterror();
  359. $this->db->rollback();
  360. dol_syslog("InfoBox::saveboxorder ".$this->error);
  361. return -1;
  362. }
  363. }
  364. }
  365. ?>