infobox.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 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/core/class/infobox.class.php
  21. * \brief File of class to manage widget boxes
  22. */
  23. /**
  24. * Class to manage boxes on pages. This is an utility class (all is static)
  25. */
  26. class InfoBox
  27. {
  28. /**
  29. * Name of positions 0=Home, 1=...
  30. *
  31. * @return string[] Array with list of zones
  32. */
  33. static function getListOfPagesForBoxes()
  34. {
  35. return array(0=>'Home');
  36. }
  37. /**
  38. * Return array of boxes qualified for area and user
  39. *
  40. * @param DoliDB $db Database handler
  41. * @param string $mode 'available' or 'activated'
  42. * @param string $zone Name or area (-1 for all, 0 for Homepage, 1 for xxx, ...)
  43. * @param User|null $user Object user to filter
  44. * @param array $excludelist Array of box id (box.box_id = boxes_def.rowid) to exclude
  45. * @return array Array of boxes
  46. */
  47. static function listBoxes($db, $mode, $zone, $user=null, $excludelist=array())
  48. {
  49. global $conf;
  50. $boxes=array();
  51. $confuserzone='MAIN_BOXES_'.$zone;
  52. if ($mode == 'activated') // activated
  53. {
  54. $sql = "SELECT b.rowid, b.position, b.box_order, b.fk_user,";
  55. $sql.= " d.rowid as box_id, d.file, d.note, d.tms";
  56. $sql.= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as d";
  57. $sql.= " WHERE b.box_id = d.rowid";
  58. $sql.= " AND b.entity IN (0,".(! empty($conf->multicompany->enabled) && ! empty($conf->multicompany->transverse_mode)?"1,":"").$conf->entity.")";
  59. if ($zone >= 0) $sql.= " AND b.position = ".$zone;
  60. if (is_object($user)) $sql.= " AND b.fk_user IN (0,".$user->id.")";
  61. else $sql.= " AND b.fk_user = 0";
  62. $sql.= " ORDER BY b.box_order";
  63. }
  64. else // available
  65. {
  66. $sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
  67. $sql.= " FROM ".MAIN_DB_PREFIX."boxes_def as d";
  68. $sql.= " WHERE d.entity IN (0,".(! empty($conf->multicompany->enabled) && ! empty($conf->multicompany->transverse_mode)?"1,":"").$conf->entity.")";
  69. }
  70. dol_syslog(get_class()."::listBoxes get default box list for mode=".$mode." userid=".(is_object($user)?$user->id:'')."", LOG_DEBUG);
  71. $resql = $db->query($sql);
  72. if ($resql)
  73. {
  74. $num = $db->num_rows($resql);
  75. $j = 0;
  76. while ($j < $num)
  77. {
  78. $obj = $db->fetch_object($resql);
  79. if (! in_array($obj->box_id, $excludelist))
  80. {
  81. if (preg_match('/^([^@]+)@([^@]+)$/i',$obj->file,$regs))
  82. {
  83. $boxname = preg_replace('/\.php$/i','',$regs[1]);
  84. $module = $regs[2];
  85. $relsourcefile = "/".$module."/core/boxes/".$boxname.".php";
  86. }
  87. else
  88. {
  89. $boxname=preg_replace('/\.php$/i','',$obj->file);
  90. $relsourcefile = "/core/boxes/".$boxname.".php";
  91. }
  92. //print $obj->box_id.'-'.$boxname.'-'.$relsourcefile.'<br>';
  93. // TODO PERF Do not make "dol_include_once" here, nor "new" later. This means, we must store a 'depends' field to store modules list, then
  94. // the "enabled" condition for modules forbidden for external users and the depends condition can be done.
  95. // Goal is to avoid making a "new" done for each boxes returned by select.
  96. dol_include_once($relsourcefile);
  97. if (class_exists($boxname))
  98. {
  99. $box=new $boxname($db,$obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
  100. //$box=new stdClass();
  101. // box properties
  102. $box->rowid = (empty($obj->rowid) ? '' : $obj->rowid);
  103. $box->id = (empty($obj->box_id) ? '' : $obj->box_id);
  104. $box->position = ($obj->position == '' ? '' : $obj->position); // '0' must staty '0'
  105. $box->box_order = (empty($obj->box_order) ? '' : $obj->box_order);
  106. $box->fk_user = (empty($obj->fk_user) ? 0 : $obj->fk_user);
  107. $box->sourcefile= $relsourcefile;
  108. $box->class = $boxname;
  109. if ($mode == 'activated' && ! is_object($user)) // List of activated box was not yet personalized into database
  110. {
  111. if (is_numeric($box->box_order))
  112. {
  113. if ($box->box_order % 2 == 1) $box->box_order='A'.$box->box_order;
  114. elseif ($box->box_order % 2 == 0) $box->box_order='B'.$box->box_order;
  115. }
  116. }
  117. // box_def properties
  118. $box->box_id = (empty($obj->box_id) ? '' : $obj->box_id);
  119. $box->note = (empty($obj->note) ? '' : $obj->note);
  120. // Filter on box->enabled (used for example by box_comptes)
  121. // Filter also on box->depends. Example: array("product|service") or array("contrat", "service")
  122. $enabled=$box->enabled;
  123. if (isset($box->depends) && count($box->depends) > 0)
  124. {
  125. foreach($box->depends as $moduleelem)
  126. {
  127. $arrayelem=explode('|',$moduleelem);
  128. $tmpenabled=0; // $tmpenabled is used for the '|' test (OR)
  129. foreach($arrayelem as $module)
  130. {
  131. $tmpmodule=preg_replace('/@[^@]+/','',$module);
  132. if (! empty($conf->$tmpmodule->enabled)) $tmpenabled=1;
  133. //print $boxname.'-'.$module.'-module enabled='.(empty($conf->$tmpmodule->enabled)?0:1).'<br>';
  134. }
  135. if (empty($tmpenabled)) // We found at least one module required that is disabled
  136. {
  137. $enabled=0;
  138. break;
  139. }
  140. }
  141. }
  142. //print '=>'.$boxname.'-enabled='.$enabled.'<br>';
  143. //print 'xx module='.$module.' enabled='.$enabled;
  144. if ($enabled) $boxes[]=$box;
  145. else unset($box);
  146. }
  147. else
  148. {
  149. dol_syslog("Failed to load box '".$boxname."' into file '".$relsourcefile."'", LOG_WARNING);
  150. }
  151. }
  152. $j++;
  153. }
  154. }
  155. else
  156. {
  157. dol_syslog($db->lasterror(),LOG_ERR);
  158. return array('error'=>$db->lasterror());
  159. }
  160. return $boxes;
  161. }
  162. /**
  163. * Save order of boxes for area and user
  164. *
  165. * @param DoliDB $db Database handler
  166. * @param string $zone Name of area (0 for Homepage, ...)
  167. * @param string $boxorder List of boxes with correct order 'A:123,456,...-B:789,321...'
  168. * @param int $userid Id of user
  169. * @return int <0 if KO, 0=Nothing done, > 0 if OK
  170. */
  171. static function saveboxorder($db, $zone,$boxorder,$userid=0)
  172. {
  173. global $conf;
  174. $error=0;
  175. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  176. dol_syslog(get_class()."::saveboxorder zone=".$zone." userid=".$userid);
  177. if (! $userid || $userid == 0) return 0;
  178. $user = new User($db);
  179. $user->id=$userid;
  180. $db->begin();
  181. // Save parameters to say user has a dedicated setup
  182. $tab=array();
  183. $confuserzone='MAIN_BOXES_'.$zone;
  184. $tab[$confuserzone]=1;
  185. if (dol_set_user_param($db, $conf, $user, $tab) < 0)
  186. {
  187. $error=$db->lasterror();
  188. $db->rollback();
  189. return -3;
  190. }
  191. // Delete all lines
  192. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  193. $sql.= " WHERE entity = ".$conf->entity;
  194. $sql.= " AND fk_user = ".$userid;
  195. $sql.= " AND position = ".$zone;
  196. dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
  197. $result = $db->query($sql);
  198. if ($result)
  199. {
  200. $colonnes=explode('-',$boxorder);
  201. foreach ($colonnes as $collist)
  202. {
  203. $part=explode(':',$collist);
  204. $colonne=$part[0];
  205. $list=$part[1];
  206. dol_syslog(get_class()."::saveboxorder column=".$colonne.' list='.$list);
  207. $i=0;
  208. $listarray=explode(',',$list);
  209. foreach ($listarray as $id)
  210. {
  211. if (is_numeric($id))
  212. {
  213. //dol_syslog("aaaaa".count($listarray));
  214. $i++;
  215. $ii=sprintf('%02d',$i);
  216. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes";
  217. $sql.= "(box_id, position, box_order, fk_user, entity)";
  218. $sql.= " values (";
  219. $sql.= " ".$id.",";
  220. $sql.= " ".$zone.",";
  221. $sql.= " '".$colonne.$ii."',";
  222. $sql.= " ".$userid.",";
  223. $sql.= " ".$conf->entity;
  224. $sql.= ")";
  225. dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
  226. $result = $db->query($sql);
  227. if ($result < 0)
  228. {
  229. $error++;
  230. break;
  231. }
  232. }
  233. }
  234. }
  235. if ($error)
  236. {
  237. $error=$db->error();
  238. $db->rollback();
  239. return -2;
  240. }
  241. else
  242. {
  243. $db->commit();
  244. return 1;
  245. }
  246. }
  247. else
  248. {
  249. $error=$db->lasterror();
  250. $db->rollback();
  251. dol_syslog(get_class()."::saveboxorder ".$error);
  252. return -1;
  253. }
  254. }
  255. }