Qual: Clean code of box management

This commit is contained in:
Laurent Destailleur 2013-04-23 16:18:26 +02:00
parent 203d4e3ca8
commit 854b9df96d
13 changed files with 250 additions and 68 deletions

View File

@ -133,7 +133,7 @@ class modMyModule extends DolibarrModules
$this->tabs = array();
// Dictionnaries
if (! isset($conf->mymodule->enabled))
if (! isset($conf->mymodule->enabled))
{
$conf->mymodule=new stdClass();
$conf->mymodule->enabled=0;
@ -158,14 +158,8 @@ class modMyModule extends DolibarrModules
// Boxes
// Add here list of php file(s) stored in core/boxes that contains class to show a box.
$this->boxes = array(); // List of boxes
$r=0;
// Example:
/*
$this->boxes[$r][1] = "myboxa.php";
$r++;
$this->boxes[$r][1] = "myboxb.php";
$r++;
*/
//$this->boxes=array(array(0=>array('file'=>'myboxa.php','note'=>'','enabledbydefaulton'=>'Home'),1=>array('file'=>'myboxb.php','note'=>''),2=>array('file'=>'myboxc.php','note'=>'')););
// Permissions
$this->rights = array(); // Permission array used by this module

View File

@ -36,9 +36,8 @@ $rowid = GETPOST('rowid','int');
$action = GETPOST('action','alpha');
$errmesg='';
// Definition des positions possibles pour les boites
$pos_array = array(0); // Positions possibles pour une boite (0,1,2,...)
$pos_name = array(0=>$langs->trans("Home")); // Nom des positions 0=Homepage, 1=...
// Define possible position of boxes
$pos_name = getStaticMember('InfoBox','listOfPages');
$boxes = array();
@ -360,7 +359,7 @@ foreach($boxtoadd as $box)
// Pour chaque position possible, on affiche un lien d'activation si boite non deja active pour cette position
print '<td>';
print $form->selectarray("pos",$pos_name);
print $form->selectarray("pos",$pos_name,0,0,0,0,'',1);
print '<input type="hidden" name="action" value="add">';
print '<input type="hidden" name="boxid" value="'.$box->box_id.'">';
print ' <input type="submit" class="button" name="button" value="'.$langs->trans("Activate").'">';
@ -406,7 +405,9 @@ foreach($boxactivated as $key => $box)
print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
print '<tr '.$bc[$var].'>';
print '<td>'.img_object("",$logo).' '.$langs->transnoentitiesnoconv($box->boxlabel).'</td>';
print '<td>'.img_object("",$logo).' '.$langs->transnoentitiesnoconv($box->boxlabel);
//if (! empty($box->graph)) print ' ('.$langs->trans("Graph").')';
print '</td>';
print '<td>';
if ($box->note == '(WarningUsingThisBoxSlowDown)')
{
@ -415,7 +416,7 @@ foreach($boxactivated as $key => $box)
}
else print ($box->note?$box->note:'&nbsp;');
print '</td>';
print '<td align="center">' . (isset($pos_name[$box->position])?$pos_name[$box->position]:'') . '</td>';
print '<td align="center">' . (empty($pos_name[$box->position])?'':$langs->trans($pos_name[$box->position])) . '</td>';
$hasnext=($key < (count($boxactivated)-1));
$hasprevious=($key != 0);
print '<td align="center">'.($key+1).'</td>';

View File

@ -68,7 +68,6 @@ if ($boxorder && $zone != '' && $userid > 0)
// boxorder value is the target order: "A:idboxA1,idboxA2,A-B:idboxB1,idboxB2,B"
dol_syslog("AjaxBox boxorder=".$boxorder." zone=".$zone." userid=".$userid, LOG_DEBUG);
//$infobox=new InfoBox($db);
$result=InfoBox::saveboxorder($db,$zone,$boxorder,$userid);
}

View File

@ -41,9 +41,23 @@ class box_activity extends ModeleBoxes
var $info_box_head = array();
var $info_box_contents = array();
// FIXME: Use a cache to save data because this slow down too much main home page. This box slow down too seriously software.
// FIXME: Removed number_format (not compatible with all languages)
// FIXME: Pb into some status
/**
* Constructor
*
* @param DoliDB $db Database handler
* @param string $param More parameters
*/
function __construct($db,$param)
{
global $conf;
$this->db=$db;
// FIXME: Use a cache to save data because this slow down too much main home page. This box slow down too seriously software.
// FIXME: Removed number_format (not compatible with all languages)
// FIXME: Pb into some status
$this->enabled=$conf->global->MAIN_FEATURES_LEVEL; // Not enabled by default due to bugs (see previous FIXME)
}
/**
* Charge les donnees en memoire pour affichage ulterieur

View File

@ -39,22 +39,23 @@ class box_comptes extends ModeleBoxes
var $db;
var $param;
var $enabled = 1;
var $info_box_head = array();
var $info_box_contents = array();
/**
* Constructor
* Constructor
*
* @param DoliDB $db Database handler
* @param DoliDB $db Database handler
* @param string $param More parameters
*/
function __construct($db)
function __construct($db,$param='')
{
global $conf, $user;
$this->db = $db;
// disable module for such cases
$listofmodulesforexternal=explode(',',$conf->global->MAIN_MODULES_FOR_EXTERNAL);
if (! in_array('banque',$listofmodulesforexternal) && ! empty($user->societe_id)) $this->enabled=0; // disabled for external users

View File

@ -39,11 +39,12 @@ class box_external_rss extends ModeleBoxes
var $depends = array("externalrss");
var $db;
var $param;
var $paramdef; // Params of box definition (not user params)
var $info_box_head = array();
var $info_box_contents = array();
/**
* Constructor
*
@ -53,7 +54,7 @@ class box_external_rss extends ModeleBoxes
function __construct($db,$param)
{
$this->db=$db;
$this->param=$param;
$this->paramdef=$param;
}
/**
@ -71,7 +72,7 @@ class box_external_rss extends ModeleBoxes
$this->max=$max;
// On recupere numero de param de la boite
preg_match('/^([0-9]+) /',$this->param,$reg);
preg_match('/^([0-9]+) /',$this->paramdef,$reg);
$site=$reg[1];
// Create dir nor required

View File

@ -0,0 +1,147 @@
<?php
/* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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
* the Free Software Foundation; either version 3 of the License, or
* (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/>.
*/
/**
* \file htdocs/core/boxes/box_invoice_permonth.php
* \ingroup factures
* \brief Box to show graph of invoices per month
*/
include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
/**
* Class to manage the box to show last invoices
*/
class box_graph_invoices_permonth extends ModeleBoxes
{
var $boxcode="invoicespermonth";
var $boximg="object_bill";
var $boxlabel="BoxInvoicesPerMonth";
var $depends = array("facture");
var $db;
var $info_box_head = array();
var $info_box_contents = array();
/**
* Constructor
*
* @param DoliDB $db Database handler
* @param string $param More parameters
*/
function __construct($db,$param)
{
global $conf;
$this->db=$db;
$this->enabled=$conf->global->MAIN_FEATURES_LEVEL;
}
/**
* Load data into info_box_contents array to show array later.
*
* @param int $max Maximum number of records to load
* @return void
*/
function loadBox($max=5)
{
global $conf, $user, $langs, $db;
$this->max=$max;
include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
$facturestatic=new Facture($db);
$text = $langs->trans("BoxInvoicesPerMonth",$max);
$this->info_box_head = array(
'text' => $text,
'limit'=> dol_strlen($text)
);
if ($user->rights->facture->lire)
{
$sql = "SELECT f.rowid as facid, f.facnumber, f.type, f.amount, f.datef as df";
$sql.= ", f.paye, f.fk_statut, f.datec, f.tms";
$sql.= ", s.nom, s.rowid as socid";
$sql.= ", f.date_lim_reglement as datelimite";
$sql.= " FROM (".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f";
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= ")";
$sql.= " WHERE f.fk_soc = s.rowid";
$sql.= " AND f.entity = ".$conf->entity;
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
if($user->societe_id) $sql.= " AND s.rowid = ".$user->societe_id;
$sql.= " ORDER BY f.tms DESC";
$sql.= $db->plimit($max, 0);
$result = $db->query($sql);
if ($result)
{
$num = $db->num_rows($result);
$now=dol_now();
$i = 0;
$l_due_date = $langs->trans('Late').' ('.strtolower($langs->trans('DateEcheance')).': %s)';
while ($i < $num)
{
$objp = $db->fetch_object($result);
$datelimite=$db->jdate($objp->datelimite);
$datec=$db->jdate($objp->datec);
$picto='bill';
if ($objp->type == 1) $picto.='r';
if ($objp->type == 2) $picto.='a';
$late = '';
if ($objp->paye == 0 && ($objp->fk_statut != 2 && $objp->fk_statut != 3) && $datelimite < ($now - $conf->facture->client->warning_delay)) { $late = img_warning(sprintf($l_due_date,dol_print_date($datelimite,'day')));}
$i++;
}
$this->info_box_contents[0][0] = array('td' => 'align="center"','text2'=>'xxxxxxx');
}
else
{
$this->info_box_contents[0][0] = array( 'td' => 'align="left"',
'maxlength'=>500,
'text' => ($db->error().' sql='.$sql));
}
}
else {
$this->info_box_contents[0][0] = array('td' => 'align="left"',
'text' => $langs->trans("ReadPermissionNotAllowed"));
}
}
/**
* Method to show box
*
* @param array $head Array with properties of box title
* @param array $contents Array with properties of box lines
* @return void
*/
function showBox($head = null, $contents = null)
{
parent::showBox($this->info_box_head, $this->info_box_contents);
}
}
?>

View File

@ -39,25 +39,26 @@ class box_members extends ModeleBoxes
var $db;
var $param;
var $enabled = 1;
var $info_box_head = array();
var $info_box_contents = array();
/**
* Constructor
* Constructor
*
* @param DoliDB $db Database handler
* @param DoliDB $db Database handler
* @param string $param More parameters
*/
function __construct($db)
function __construct($db,$param='')
{
global $conf, $user;
$this->db = $db;
// disable module for such cases
$listofmodulesforexternal=explode(',',$conf->global->MAIN_MODULES_FOR_EXTERNAL);
if (! in_array('banque',$listofmodulesforexternal) && ! empty($user->societe_id)) $this->enabled=0; // disabled for external users
if (! in_array('adherent',$listofmodulesforexternal) && ! empty($user->societe_id)) $this->enabled=0; // disabled for external users
}
/**

View File

@ -40,6 +40,7 @@ class ModeleBoxes // Can't be abtract as it is instanciated to build "empty"
var $box_order;
var $fk_user;
var $sourcefile;
var $class;
var $box_id;
var $note;
@ -47,9 +48,10 @@ class ModeleBoxes // Can't be abtract as it is instanciated to build "empty"
/**
* Constructor
*
* @param DoliDB $db Database handler
* @param DoliDB $db Database handler
* @param string $param More parameters
*/
function __construct($db)
function __construct($db,$param='')
{
$this->db=$db;
}

View File

@ -886,7 +886,9 @@ class FormOther
foreach($boxactivated as $box)
{
if (! empty($boxidactivatedforuser[$box->id])) continue; // Already visible for user
$arrayboxtoactivatelabel[$box->id]=$langs->transnoentitiesnoconv($box->boxlabel); // We keep only boxes not shown for user, to show into combo list
$label=$langs->transnoentitiesnoconv($box->boxlabel);
if (preg_match('/graph/',$box->class)) $label.=' ('.$langs->trans("Graph").')';
$arrayboxtoactivatelabel[$box->id]=$label; // We keep only boxes not shown for user, to show into combo list
}
$form=new Form($db);
@ -897,7 +899,7 @@ class FormOther
if (! empty($conf->use_javascript_ajax))
{
print '<script type="text/javascript" language="javascript">
// To update list of activated boxes
function updateBoxOrder(closing) {
var left_list = cleanSerialize(jQuery("#left").sortable("serialize"));
@ -920,7 +922,7 @@ class FormOther
});
}
}
jQuery(document).ready(function() {
jQuery("#boxcombo").change(function() {
var boxid=jQuery("#boxcombo").val();
@ -937,7 +939,7 @@ class FormOther
});';
if (! count($arrayboxtoactivatelabel)) print 'jQuery("#boxcombo").hide();';
print '
jQuery("#left, #right").sortable({
/* placeholder: \'ui-state-highlight\', */
handle: \'.boxhandle\',
@ -958,9 +960,9 @@ class FormOther
// TODO Add id, label into combo list
updateBoxOrder(1);
});
});'."\n";
print '</script>'."\n";
}

View File

@ -27,6 +27,9 @@
*/
class InfoBox
{
static $listOfPages = array(0=>'Home'); // Nom des positions 0=Home, 1=...
/**
* Return array of boxes qualified for area and user
*
@ -57,17 +60,16 @@ class InfoBox
$sql.= " ORDER BY b.box_order";
}
else
{
{
$sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
$sql.= " FROM ".MAIN_DB_PREFIX."boxes_def as d";
if (! empty($conf->multicompany->enabled) && ! empty($conf->multicompany->transverse_mode)) {
if (! empty($conf->multicompany->enabled) && ! empty($conf->multicompany->transverse_mode))
{
$sql.= " WHERE entity IN (1,".$conf->entity.")"; // TODO add method for define another master entity
} else {
}
else
{
$sql.= " WHERE entity = ".$conf->entity;
}
}
@ -95,18 +97,24 @@ class InfoBox
$relsourcefile = "/core/boxes/".$boxname.".php";
}
// 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
// the "enabled" condition for modules forbidden for external users and the depends condition can be done.
// Goal is to avoid making a new instance for each boxes returned by select.
dol_include_once($relsourcefile);
if (class_exists($boxname))
{
$box=new $boxname($db,$obj->note);
$box=new $boxname($db,$obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
//$box=new stdClass();
// box properties
$box->rowid = (empty($obj->rowid) ? '' : $obj->rowid);
$box->id = (empty($obj->box_id) ? '' : $obj->box_id);
$box->position = (empty($obj->position) ? '' : $obj->position);
$box->position = ($obj->position == '' ? '' : $obj->position); // '0' must staty '0'
$box->box_order = (empty($obj->box_order) ? '' : $obj->box_order);
$box->fk_user = (empty($obj->fk_user) ? 0 : $obj->fk_user);
$box->sourcefile=$relsourcefile;
$box->sourcefile= $relsourcefile;
$box->class = $boxname;
if ($mode == 'activated' && ! is_object($user)) // List of activated box was not yet personalized into database
{
if (is_numeric($box->box_order))
@ -119,6 +127,8 @@ class InfoBox
$box->box_id = (empty($obj->box_id) ? '' : $obj->box_id);
$box->note = (empty($obj->note) ? '' : $obj->note);
// Filter on box->enabled (fused for example by box_comptes) and box->depends
//$enabled=1;
$enabled=$box->enabled;
if (isset($box->depends) && count($box->depends) > 0)
{
@ -131,6 +141,7 @@ class InfoBox
//print 'xx module='.$module.' enabled='.$enabled;
if ($enabled) $boxes[]=$box;
else unset($box);
}
}
$j++;

View File

@ -502,7 +502,9 @@ abstract class DolibarrModules
*/
function insert_boxes()
{
global $conf;
require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
global $conf;
$err=0;
@ -510,9 +512,12 @@ abstract class DolibarrModules
{
foreach ($this->boxes as $key => $value)
{
//$titre = $this->boxes[$key][0];
$file = isset($this->boxes[$key][1])?$this->boxes[$key][1]:'';
$note = isset($this->boxes[$key][2])?$this->boxes[$key][2]:'';
$file = isset($this->boxes[$key]['file'])?$this->boxes[$key]['file']:'';
$note = isset($this->boxes[$key]['note'])?$this->boxes[$key]['note']:'';
$enabledbydefaulton = isset($this->boxes[$key]['enabledbydefaulton'])?$this->boxes[$key]['enabledbydefaulton']:'Home';
if (empty($file)) $file = isset($this->boxes[$key][1])?$this->boxes[$key][1]:''; // For backward compatibility
if (empty($note)) $note = isset($this->boxes[$key][2])?$this->boxes[$key][2]:''; // For backward compatibility
$sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
$sql.= " WHERE file = '".$this->db->escape($file)."'";
@ -544,12 +549,19 @@ abstract class DolibarrModules
{
$lastid=$this->db->last_insert_id(MAIN_DB_PREFIX."boxes_def","rowid");
$sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (box_id,position,box_order,fk_user,entity)";
$sql.= " VALUES (".$lastid.", 0, '0', 0, ".$conf->entity.")";
$pos_name = getStaticMember('InfoBox','listOfPages');
foreach ($pos_name as $key2 => $val2)
{
//print 'key2='.$key2.'-val2='.$val2."<br>\n";
if ($enabledbydefaulton && $val2 != $enabledbydefaulton) continue; // Not enabled by default onto this page.
dol_syslog(get_class($this)."::insert_boxes sql=".$sql);
$resql=$this->db->query($sql);
if (! $resql) $err++;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (box_id,position,box_order,fk_user,entity)";
$sql.= " VALUES (".$lastid.", ".$key2.", '0', 0, ".$conf->entity.")";
dol_syslog(get_class($this)."::insert_boxes onto page ".$key2."=".$val2." sql=".$sql);
$resql=$this->db->query($sql);
if (! $resql) $err++;
}
}
if (! $err)
@ -563,9 +575,10 @@ abstract class DolibarrModules
$this->db->rollback();
}
}
// else box already registered into database
}
else
{
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::insert_boxes ".$this->error, LOG_ERR);
$err++;

View File

@ -97,12 +97,8 @@ class modFacture extends DolibarrModules
$r++;
// Boxes
$this->boxes = array();
$r=0;
$this->boxes[$r][1] = "box_factures_imp.php";
$r++;
$this->boxes[$r][1] = "box_factures.php";
$r++;
//$this->boxes = array(0=>array(1=>'box_factures_imp.php'),1=>array(1=>'box_factures.php'));
$this->boxes = array(0=>array('file'=>'box_factures_imp.php','enabledbydefaulton'=>'Home'),1=>array('file'=>'box_factures.php','enabledbydefaulton'=>'Home'),2=>array('file'=>'box_graph_invoices_permonth.php','enabledbydefaulton'=>'Home'));
// Permissions
$this->rights = array();