2003-02-13 17:57:47 +01:00
|
|
|
<?php
|
2011-10-24 18:56:00 +02:00
|
|
|
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
|
2012-03-19 15:51:55 +01:00
|
|
|
* Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
|
2012-12-30 15:11:07 +01:00
|
|
|
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
|
2012-03-19 14:06:21 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2013-01-16 15:36:08 +01:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2012-03-19 14:06:21 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2003-02-13 17:57:47 +01:00
|
|
|
|
2005-03-05 17:51:26 +01:00
|
|
|
/**
|
2012-04-16 12:01:32 +02:00
|
|
|
* \file htdocs/core/class/infobox.class.php
|
2011-06-17 21:14:47 +02:00
|
|
|
* \brief File of class to manage widget boxes
|
2009-02-11 03:18:27 +01:00
|
|
|
*/
|
2004-08-13 22:11:58 +02:00
|
|
|
|
2005-03-05 17:51:26 +01:00
|
|
|
/**
|
2014-08-01 00:53:29 +02:00
|
|
|
* Class to manage boxes on pages. This is an utility class (all is static)
|
2009-02-11 03:18:27 +01:00
|
|
|
*/
|
|
|
|
|
class InfoBox
|
2003-02-13 17:57:47 +01:00
|
|
|
{
|
2014-08-01 00:53:29 +02:00
|
|
|
/**
|
|
|
|
|
* Name of positions 0=Home, 1=...
|
|
|
|
|
*
|
2015-04-06 12:25:30 +02:00
|
|
|
* @return string[] Array with list of zones
|
2014-08-01 00:53:29 +02:00
|
|
|
*/
|
|
|
|
|
static function getListOfPagesForBoxes()
|
|
|
|
|
{
|
|
|
|
|
return array(0=>'Home');
|
|
|
|
|
}
|
2013-04-23 16:18:26 +02:00
|
|
|
|
2012-03-17 14:04:16 +01:00
|
|
|
/**
|
|
|
|
|
* Return array of boxes qualified for area and user
|
|
|
|
|
*
|
2014-11-23 23:22:49 +01:00
|
|
|
* @param DoliDB $db Database handler
|
|
|
|
|
* @param string $mode 'available' or 'activated'
|
|
|
|
|
* @param string $zone Name or area (-1 for all, 0 for Homepage, 1 for xxx, ...)
|
|
|
|
|
* @param User|null $user Object user to filter
|
|
|
|
|
* @param array $excludelist Array of box id (box.box_id = boxes_def.rowid) to exclude
|
2017-06-12 14:09:00 +02:00
|
|
|
* @param int $includehidden Include also hidden boxes
|
2014-11-23 23:22:49 +01:00
|
|
|
* @return array Array of boxes
|
2012-03-17 14:04:16 +01:00
|
|
|
*/
|
2017-06-12 14:09:00 +02:00
|
|
|
static function listBoxes($db, $mode, $zone, $user=null, $excludelist=array(), $includehidden=1)
|
2012-03-17 14:04:16 +01:00
|
|
|
{
|
|
|
|
|
global $conf;
|
|
|
|
|
|
|
|
|
|
$boxes=array();
|
|
|
|
|
|
|
|
|
|
$confuserzone='MAIN_BOXES_'.$zone;
|
2014-10-27 21:48:28 +01:00
|
|
|
if ($mode == 'activated') // activated
|
2012-03-17 14:04:16 +01:00
|
|
|
{
|
|
|
|
|
$sql = "SELECT b.rowid, b.position, b.box_order, b.fk_user,";
|
|
|
|
|
$sql.= " d.rowid as box_id, d.file, d.note, d.tms";
|
|
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as d";
|
|
|
|
|
$sql.= " WHERE b.box_id = d.rowid";
|
2017-06-06 07:57:03 +02:00
|
|
|
$sql.= " AND b.entity IN (0,".$conf->entity.")";
|
2012-03-17 14:04:16 +01:00
|
|
|
if ($zone >= 0) $sql.= " AND b.position = ".$zone;
|
2013-01-15 11:35:10 +01:00
|
|
|
if (is_object($user)) $sql.= " AND b.fk_user IN (0,".$user->id.")";
|
2012-03-17 14:04:16 +01:00
|
|
|
else $sql.= " AND b.fk_user = 0";
|
|
|
|
|
$sql.= " ORDER BY b.box_order";
|
|
|
|
|
}
|
2014-10-27 21:48:28 +01:00
|
|
|
else // available
|
2013-04-23 16:18:26 +02:00
|
|
|
{
|
2012-03-17 14:04:16 +01:00
|
|
|
$sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
|
|
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX."boxes_def as d";
|
2017-06-06 07:57:03 +02:00
|
|
|
$sql.= " WHERE d.entity IN (0,".$conf->entity.")";
|
2012-03-17 14:04:16 +01:00
|
|
|
}
|
2017-06-06 07:57:03 +02:00
|
|
|
|
2014-06-12 11:31:53 +02:00
|
|
|
dol_syslog(get_class()."::listBoxes get default box list for mode=".$mode." userid=".(is_object($user)?$user->id:'')."", LOG_DEBUG);
|
2012-03-19 15:51:55 +01:00
|
|
|
$resql = $db->query($sql);
|
2012-03-17 14:04:16 +01:00
|
|
|
if ($resql)
|
|
|
|
|
{
|
2012-03-19 15:51:55 +01:00
|
|
|
$num = $db->num_rows($resql);
|
2012-03-17 14:04:16 +01:00
|
|
|
$j = 0;
|
|
|
|
|
while ($j < $num)
|
|
|
|
|
{
|
2012-03-19 15:51:55 +01:00
|
|
|
$obj = $db->fetch_object($resql);
|
2012-03-17 14:04:16 +01:00
|
|
|
|
|
|
|
|
if (! in_array($obj->box_id, $excludelist))
|
|
|
|
|
{
|
2014-10-27 21:48:28 +01:00
|
|
|
|
2012-03-17 14:04:16 +01:00
|
|
|
if (preg_match('/^([^@]+)@([^@]+)$/i',$obj->file,$regs))
|
|
|
|
|
{
|
2013-06-03 12:46:25 +02:00
|
|
|
$boxname = preg_replace('/\.php$/i','',$regs[1]);
|
2012-03-17 14:04:16 +01:00
|
|
|
$module = $regs[2];
|
|
|
|
|
$relsourcefile = "/".$module."/core/boxes/".$boxname.".php";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-03 12:46:25 +02:00
|
|
|
$boxname=preg_replace('/\.php$/i','',$obj->file);
|
2012-03-17 14:04:16 +01:00
|
|
|
$relsourcefile = "/core/boxes/".$boxname.".php";
|
2013-06-03 11:33:13 +02:00
|
|
|
}
|
2012-03-17 14:04:16 +01:00
|
|
|
|
2014-10-27 21:48:28 +01:00
|
|
|
//print $obj->box_id.'-'.$boxname.'-'.$relsourcefile.'<br>';
|
|
|
|
|
|
2013-06-03 11:33:13 +02:00
|
|
|
// 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
|
2013-04-23 16:18:26 +02:00
|
|
|
// the "enabled" condition for modules forbidden for external users and the depends condition can be done.
|
2014-10-27 21:48:28 +01:00
|
|
|
// Goal is to avoid making a "new" done for each boxes returned by select.
|
2012-03-17 14:04:16 +01:00
|
|
|
dol_include_once($relsourcefile);
|
|
|
|
|
if (class_exists($boxname))
|
|
|
|
|
{
|
2013-06-30 23:01:28 +02:00
|
|
|
$box=new $boxname($db,$obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
|
2013-04-23 16:18:26 +02:00
|
|
|
//$box=new stdClass();
|
2013-06-30 23:01:28 +02:00
|
|
|
|
2012-03-17 14:04:16 +01:00
|
|
|
// box properties
|
2013-01-15 11:35:10 +01:00
|
|
|
$box->rowid = (empty($obj->rowid) ? '' : $obj->rowid);
|
|
|
|
|
$box->id = (empty($obj->box_id) ? '' : $obj->box_id);
|
2013-04-23 16:18:26 +02:00
|
|
|
$box->position = ($obj->position == '' ? '' : $obj->position); // '0' must staty '0'
|
2013-01-15 11:35:10 +01:00
|
|
|
$box->box_order = (empty($obj->box_order) ? '' : $obj->box_order);
|
|
|
|
|
$box->fk_user = (empty($obj->fk_user) ? 0 : $obj->fk_user);
|
2013-04-23 16:18:26 +02:00
|
|
|
$box->sourcefile= $relsourcefile;
|
|
|
|
|
$box->class = $boxname;
|
2013-06-30 23:01:28 +02:00
|
|
|
|
|
|
|
|
if ($mode == 'activated' && ! is_object($user)) // List of activated box was not yet personalized into database
|
2012-03-17 14:04:16 +01:00
|
|
|
{
|
|
|
|
|
if (is_numeric($box->box_order))
|
|
|
|
|
{
|
|
|
|
|
if ($box->box_order % 2 == 1) $box->box_order='A'.$box->box_order;
|
|
|
|
|
elseif ($box->box_order % 2 == 0) $box->box_order='B'.$box->box_order;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// box_def properties
|
2013-01-15 11:35:10 +01:00
|
|
|
$box->box_id = (empty($obj->box_id) ? '' : $obj->box_id);
|
|
|
|
|
$box->note = (empty($obj->note) ? '' : $obj->note);
|
2013-06-30 23:01:28 +02:00
|
|
|
|
2013-08-02 14:54:22 +02:00
|
|
|
// Filter on box->enabled (used for example by box_comptes)
|
|
|
|
|
// Filter also on box->depends. Example: array("product|service") or array("contrat", "service")
|
2013-01-14 13:06:08 +01:00
|
|
|
$enabled=$box->enabled;
|
2012-07-09 18:09:44 +02:00
|
|
|
if (isset($box->depends) && count($box->depends) > 0)
|
2012-03-17 14:04:16 +01:00
|
|
|
{
|
2013-08-02 14:54:22 +02:00
|
|
|
foreach($box->depends as $moduleelem)
|
2012-03-17 14:04:16 +01:00
|
|
|
{
|
2013-08-02 14:54:22 +02:00
|
|
|
$arrayelem=explode('|',$moduleelem);
|
|
|
|
|
$tmpenabled=0; // $tmpenabled is used for the '|' test (OR)
|
|
|
|
|
foreach($arrayelem as $module)
|
2014-08-01 00:53:29 +02:00
|
|
|
{
|
2013-08-02 14:54:22 +02:00
|
|
|
$tmpmodule=preg_replace('/@[^@]+/','',$module);
|
2014-08-01 00:53:29 +02:00
|
|
|
if (! empty($conf->$tmpmodule->enabled)) $tmpenabled=1;
|
2013-08-02 14:54:22 +02:00
|
|
|
//print $boxname.'-'.$module.'-module enabled='.(empty($conf->$tmpmodule->enabled)?0:1).'<br>';
|
|
|
|
|
}
|
2016-03-26 12:26:27 +01:00
|
|
|
if (empty($tmpenabled)) // We found at least one module required that is disabled
|
2013-08-02 14:54:22 +02:00
|
|
|
{
|
|
|
|
|
$enabled=0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-03-17 14:04:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-08-02 14:54:22 +02:00
|
|
|
//print '=>'.$boxname.'-enabled='.$enabled.'<br>';
|
2014-08-01 00:53:29 +02:00
|
|
|
|
2013-01-14 13:06:08 +01:00
|
|
|
//print 'xx module='.$module.' enabled='.$enabled;
|
2017-06-12 14:09:00 +02:00
|
|
|
if ($enabled && ($includehidden || empty($box->hidden))) $boxes[]=$box;
|
2013-04-23 16:18:26 +02:00
|
|
|
else unset($box);
|
2014-10-27 21:48:28 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dol_syslog("Failed to load box '".$boxname."' into file '".$relsourcefile."'", LOG_WARNING);
|
|
|
|
|
}
|
2012-03-17 14:04:16 +01:00
|
|
|
}
|
|
|
|
|
$j++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-10-27 21:48:28 +01:00
|
|
|
{
|
|
|
|
|
dol_syslog($db->lasterror(),LOG_ERR);
|
|
|
|
|
return array('error'=>$db->lasterror());
|
2012-03-17 14:04:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $boxes;
|
|
|
|
|
}
|
2006-11-05 19:21:28 +01:00
|
|
|
|
|
|
|
|
|
2011-07-08 19:59:55 +02:00
|
|
|
/**
|
2012-03-17 21:32:10 +01:00
|
|
|
* Save order of boxes for area and user
|
2011-07-08 19:59:55 +02:00
|
|
|
*
|
2012-03-19 17:18:11 +01:00
|
|
|
* @param DoliDB $db Database handler
|
|
|
|
|
* @param string $zone Name of area (0 for Homepage, ...)
|
|
|
|
|
* @param string $boxorder List of boxes with correct order 'A:123,456,...-B:789,321...'
|
|
|
|
|
* @param int $userid Id of user
|
2017-03-09 13:22:43 +01:00
|
|
|
* @return int <0 if KO, 0=Nothing done, > 0 if OK
|
2011-07-08 19:59:55 +02:00
|
|
|
*/
|
2012-03-19 15:51:55 +01:00
|
|
|
static function saveboxorder($db, $zone,$boxorder,$userid=0)
|
2012-03-17 14:04:16 +01:00
|
|
|
{
|
|
|
|
|
global $conf;
|
2009-10-29 02:12:53 +01:00
|
|
|
|
2012-03-17 14:04:16 +01:00
|
|
|
$error=0;
|
2012-02-19 13:12:11 +01:00
|
|
|
|
2012-08-22 23:11:24 +02:00
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
|
2009-02-11 03:18:27 +01:00
|
|
|
|
2012-04-11 14:44:54 +02:00
|
|
|
dol_syslog(get_class()."::saveboxorder zone=".$zone." userid=".$userid);
|
2006-11-05 19:21:28 +01:00
|
|
|
|
2012-03-17 14:04:16 +01:00
|
|
|
if (! $userid || $userid == 0) return 0;
|
2009-02-11 03:18:27 +01:00
|
|
|
|
2012-03-19 15:51:55 +01:00
|
|
|
$user = new User($db);
|
2010-08-18 09:44:45 +02:00
|
|
|
$user->id=$userid;
|
2006-11-05 19:21:28 +01:00
|
|
|
|
2012-03-19 15:51:55 +01:00
|
|
|
$db->begin();
|
2012-03-17 14:04:16 +01:00
|
|
|
|
2014-10-04 17:20:17 +02:00
|
|
|
// Save parameters to say user has a dedicated setup
|
|
|
|
|
$tab=array();
|
2012-03-17 14:04:16 +01:00
|
|
|
$confuserzone='MAIN_BOXES_'.$zone;
|
|
|
|
|
$tab[$confuserzone]=1;
|
2012-03-19 15:51:55 +01:00
|
|
|
if (dol_set_user_param($db, $conf, $user, $tab) < 0)
|
2012-03-17 14:04:16 +01:00
|
|
|
{
|
2014-04-23 14:54:34 +02:00
|
|
|
$error=$db->lasterror();
|
2012-03-19 15:51:55 +01:00
|
|
|
$db->rollback();
|
2012-03-17 14:04:16 +01:00
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-17 21:32:10 +01:00
|
|
|
// Delete all lines
|
2012-03-17 14:04:16 +01:00
|
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
|
2012-09-07 17:16:47 +02:00
|
|
|
$sql.= " WHERE entity = ".$conf->entity;
|
|
|
|
|
$sql.= " AND fk_user = ".$userid;
|
|
|
|
|
$sql.= " AND position = ".$zone;
|
2012-03-17 14:04:16 +01:00
|
|
|
|
2014-06-12 11:31:53 +02:00
|
|
|
dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
|
2012-03-19 15:51:55 +01:00
|
|
|
$result = $db->query($sql);
|
2012-03-17 14:04:16 +01:00
|
|
|
if ($result)
|
|
|
|
|
{
|
|
|
|
|
$colonnes=explode('-',$boxorder);
|
|
|
|
|
foreach ($colonnes as $collist)
|
|
|
|
|
{
|
|
|
|
|
$part=explode(':',$collist);
|
|
|
|
|
$colonne=$part[0];
|
|
|
|
|
$list=$part[1];
|
2012-04-11 14:44:54 +02:00
|
|
|
dol_syslog(get_class()."::saveboxorder column=".$colonne.' list='.$list);
|
2012-03-17 14:04:16 +01:00
|
|
|
|
|
|
|
|
$i=0;
|
|
|
|
|
$listarray=explode(',',$list);
|
|
|
|
|
foreach ($listarray as $id)
|
|
|
|
|
{
|
|
|
|
|
if (is_numeric($id))
|
|
|
|
|
{
|
|
|
|
|
//dol_syslog("aaaaa".count($listarray));
|
|
|
|
|
$i++;
|
|
|
|
|
$ii=sprintf('%02d',$i);
|
|
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes";
|
2012-09-07 17:16:47 +02:00
|
|
|
$sql.= "(box_id, position, box_order, fk_user, entity)";
|
2012-03-17 14:04:16 +01:00
|
|
|
$sql.= " values (";
|
|
|
|
|
$sql.= " ".$id.",";
|
|
|
|
|
$sql.= " ".$zone.",";
|
|
|
|
|
$sql.= " '".$colonne.$ii."',";
|
2012-09-07 17:16:47 +02:00
|
|
|
$sql.= " ".$userid.",";
|
|
|
|
|
$sql.= " ".$conf->entity;
|
2012-03-17 14:04:16 +01:00
|
|
|
$sql.= ")";
|
|
|
|
|
|
2014-06-12 11:31:53 +02:00
|
|
|
dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
|
2012-03-19 15:51:55 +01:00
|
|
|
$result = $db->query($sql);
|
2012-03-17 14:04:16 +01:00
|
|
|
if ($result < 0)
|
|
|
|
|
{
|
|
|
|
|
$error++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($error)
|
|
|
|
|
{
|
2012-09-10 14:40:32 +02:00
|
|
|
$error=$db->error();
|
2012-03-19 15:51:55 +01:00
|
|
|
$db->rollback();
|
2012-03-17 14:04:16 +01:00
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-03-19 15:51:55 +01:00
|
|
|
$db->commit();
|
2012-03-17 14:04:16 +01:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-09-10 14:40:32 +02:00
|
|
|
$error=$db->lasterror();
|
2012-03-19 15:51:55 +01:00
|
|
|
$db->rollback();
|
2012-09-10 14:40:32 +02:00
|
|
|
dol_syslog(get_class()."::saveboxorder ".$error);
|
2012-03-17 14:04:16 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-04-20 20:23:55 +02:00
|
|
|
|
2003-02-13 17:57:47 +01:00
|
|
|
}
|
2010-04-20 20:23:55 +02:00
|
|
|
|