Uniformize code and increase speed for extrafields

This commit is contained in:
Laurent Destailleur 2011-06-22 10:58:22 +00:00
parent cee57c2280
commit 041b7d2955
9 changed files with 151 additions and 113 deletions

View File

@ -423,11 +423,11 @@ class Adherent extends CommonObject
if (sizeof($this->array_options) > 0)
{
$sql_del = "DELETE FROM ".MAIN_DB_PREFIX."adherent_extrafields WHERE fk_member = ".$this->id;
$sql_del = "DELETE FROM ".MAIN_DB_PREFIX."adherent_extrafields WHERE fk_object = ".$this->id;
dol_syslog(get_class($this)."::update sql=".$sql_del);
$this->db->query($sql_del);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent_extrafields (fk_member";
$sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent_extrafields (fk_object";
foreach($this->array_options as $key => $value)
{
// Add field of attribut
@ -643,7 +643,7 @@ class Adherent extends CommonObject
$this->db->begin();
// Suppression options
$sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_extrafields WHERE fk_member = ".$rowid;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_extrafields WHERE fk_object = ".$rowid;
dol_syslog(get_class($this)."::delete sql=".$sql);
$resql=$this->db->query($sql);
@ -1092,54 +1092,6 @@ class Adherent extends CommonObject
}
/**
* Function to get extra fields of a member into $this->array_options
* @param rowid
* TODO Move this function into ExtraField class
* TODO rename field fk_member into fk_element
*/
function fetch_optionals($rowid)
{
require_once(DOL_DOCUMENT_ROOT."/core/class/extrafields.class.php");
$options = new ExtraFields($this->db);
$optionsArray = $options->fetch_name_optionals_label();
$tab=array();
$sql = "SELECT rowid";
foreach ($optionsArray as $name => $label)
{
$sql.= ", ".$name;
}
$sql.= " FROM ".MAIN_DB_PREFIX."adherent_extrafields";
$sql.= " WHERE fk_member=".$rowid;
dol_syslog(get_class($this)."::fetch_optionals sql=".$sql, LOG_DEBUG);
$result=$this->db->query( $sql);
if ($result)
{
if ($this->db->num_rows($result))
{
$tab = $this->db->fetch_array($result);
foreach ($tab as $key => $value)
{
if ($key != 'rowid' && $key != 'tms' && $key != 'fk_member')
{
// we can add this attribute to adherent object
$this->array_options["options_$key"]=$value;
}
}
}
}
else
{
dol_print_error($this->db);
}
}
/**
* Fonction qui insere la cotisation dans la base de donnees
* et eventuellement liens dans banques, mailman, etc...

View File

@ -47,8 +47,9 @@ $langs->load("users");
if (! $user->rights->adherent->lire) accessforbidden();
$adh = new Adherent($db);
$adho = new ExtraFields($db);
$errmsg='';
$extrafields = new ExtraFields($db);
$errmsg=''; $errmsgs=array();
$action=GETPOST("action");
$rowid=GETPOST("rowid");
@ -595,14 +596,15 @@ if ($user->rights->adherent->creer && $_POST["action"] == 'confirm_add_spip' &&
* View
*/
llxHeader('',$langs->trans("Member"),'EN:Module_Foundations|FR:Module_Adhérents|ES:Módulo_Miembros');
// fetch optionals attributes and labels
$extralabels=$extrafields->fetch_name_optionals_label('member');
$help_url='EN:Module_Foundations|FR:Module_Adhérents|ES:Módulo_Miembros';
llxHeader('',$langs->trans("Member"),$help_url);
$html = new Form($db);
$htmlcompany = new FormCompany($db);
// fetch optionals attributes and labels
$adho->fetch_name_optionals_label();
$countrynotdefined=$langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
if ($action == 'create')
@ -692,11 +694,11 @@ if ($action == 'create')
print $htmlcompany->select_civilite(isset($_POST["civilite_id"])?$_POST["civilite_id"]:$adh->civilite_id,'civilite_id').'</td>';
print '</tr>';
// Nom
// Lastname
print '<tr><td><span class="fieldrequired">'.$langs->trans("Lastname").'</span></td><td><input type="text" name="nom" value="'.(isset($_POST["nom"])?$_POST["nom"]:$adh->nom).'" size="40"></td>';
print '</tr>';
// Prenom
// Firstname
print '<tr><td>'.$langs->trans("Firstname").'</td><td><input type="text" name="prenom" size="40" value="'.(isset($_POST["prenom"])?$_POST["prenom"]:$adh->prenom).'"></td>';
print '</tr>';
@ -756,7 +758,7 @@ if ($action == 'create')
// EMail
print '<tr><td>'.($conf->global->ADHERENT_MAIL_REQUIRED?'<span class="fieldrequired">':'').$langs->trans("EMail").($conf->global->ADHERENT_MAIL_REQUIRED?'</span>':'').'</td><td><input type="text" name="member_email" size="40" value="'.(isset($_POST["member_email"])?$_POST["member_email"]:$adh->email).'"></td></tr>';
// Date naissance
// Birthday
print "<tr><td>".$langs->trans("Birthday")."</td><td>\n";
$html->select_date(($adh->naiss ? $adh->naiss : -1),'naiss','','',1,'formsoc');
print "</td></tr>\n";
@ -767,7 +769,7 @@ if ($action == 'create')
print "</td></tr>\n";
// Attribut optionnels
foreach($adho->attribute_label as $key=>$value)
foreach($extrafields->attribute_label as $key=>$value)
{
print "<tr><td>".$value.'</td><td><input type="text" name="options_'.$key.'" size="40" value="'.(isset($_POST["options_".$key])?$_POST["options_".$key]:'').'"></td></tr>'."\n";
}
@ -803,14 +805,11 @@ if ($action == 'edit')
*
********************************************/
$adho = new ExtraFields($db);
$adh = new Adherent($db);
$adh->id = $rowid;
$adh->fetch($rowid);
// fetch optionals value
$adh->fetch_optionals($rowid);
// fetch optionals attributes and labels
$adho->fetch_name_optionals_label();
$res=$adh->fetch($rowid);
if ($res < 0) { dol_print_error($db,$adh->error); exit; }
$res=$adh->fetch_optionals($rowid,$extralabels);
if ($res < 0) { dol_print_error($db); exit; }
$adht = new AdherentType($db);
$adht->fetch($adh->typeid);
@ -855,7 +854,7 @@ if ($action == 'edit')
$rowspan=17;
if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED)) $rowspan+=1;
$rowspan+=sizeof($adho->attribute_label);
$rowspan+=sizeof($extrafields->attribute_label);
if ($conf->societe->enabled) $rowspan++;
print '<form name="formsoc" action="'.$_SERVER["PHP_SELF"].'" method="post" enctype="multipart/form-data">';
@ -980,10 +979,10 @@ if ($action == 'edit')
print $html->selectyesno("public",(isset($_POST["public"])?$_POST["public"]:$adh->public),1);
print "</td></tr>\n";
// Attributs supplementaires
foreach($adho->attribute_label as $key=>$value)
// Other attributes
foreach($extrafields->attribute_label as $key=>$value)
{
print "<tr><td>$value</td><td><input type=\"text\" name=\"options_$key\" size=\"40\" value=\"".$adh->array_options["options_$key"]."\"></td></tr>\n";
print "<tr><td>".$value."</td><td><input type=\"text\" name=\"options_".$key."\" size=\"40\" value=\"".$adh->array_options["options_$key"]."\"></td></tr>\n";
}
// Third party Dolibarr
@ -1035,13 +1034,13 @@ if ($rowid && $action != 'edit')
$adh = new Adherent($db);
$res=$adh->fetch($rowid);
if ($res < 0) dol_print_error($db);
$res=$adh->fetch_optionals($rowid);
if ($res < 0) dol_print_error($db);
if ($res < 0) { dol_print_error($db,$adh->error); exit; }
$res=$adh->fetch_optionals($rowid,$extralabels);
if ($res < 0) { dol_print_error($db); exit; }
$adht = new AdherentType($db);
$res=$adht->fetch($adh->typeid);
if ($res < 0) dol_print_error($db);
if ($res < 0) { dol_print_error($db); exit; }
/*
@ -1194,7 +1193,7 @@ if ($rowid && $action != 'edit')
if ($ret == 'html') print '<br>';
}
$rowspan=19+sizeof($adho->attribute_label);
$rowspan=19+sizeof($extrafields->attribute_label);
if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED)) $rowspan+=1;
if ($conf->societe->enabled) $rowspan++;
@ -1285,10 +1284,10 @@ if ($rowid && $action != 'edit')
// Status
print '<tr><td>'.$langs->trans("Status").'</td><td class="valeur">'.$adh->getLibStatut(4).'</td></tr>';
// Other attributs
foreach($adho->attribute_label as $key=>$value)
// Other attributes
foreach($extrafields->attribute_label as $key=>$value)
{
print "<tr><td>$value</td><td>".$adh->array_options["options_$key"]."&nbsp;</td></tr>\n";
print "<tr><td>".$value."</td><td>".$adh->array_options["options_$key"]."&nbsp;</td></tr>\n";
}
// Third party Dolibarr

View File

@ -21,14 +21,14 @@
/**
* \file htdocs/core/class/commonobject.class.php
* \ingroup core
* \brief Fichier de la classe mere des classes metiers (facture, contrat, propal, commande, etc...)
* \brief File of parent class of all other business classes (invoices, contracts, proposals, orders, ...)
* \version $Id$
*/
/**
* \class CommonObject
* \brief Classe mere pour heritage des classes metiers
* \brief Class of all other business classes (invoices, contracts, proposals, orders, ...)
*/
class CommonObject
@ -1489,9 +1489,64 @@ class CommonObject
}
/**
* Function to get extra fields of a member into $this->array_options
* @param rowid
* @param optionsArray Array resulting of call of extrafields->fetch_name_optionals_label()
*/
function fetch_optionals($rowid,$optionsArray='')
{
if (! is_array($optionsArray))
{
// optionsArray not already loaded, so we load it
require_once(DOL_DOCUMENT_ROOT."/core/class/extrafields.class.php");
$extrafields = new ExtraFields($this->db);
$optionsArray = $extrafields->fetch_name_optionals_label();
}
// Request to get complementary values
if (sizeof($optionsArray) > 0)
{
$sql = "SELECT rowid";
foreach ($optionsArray as $name => $label)
{
$sql.= ", ".$name;
}
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element."_extrafields";
$sql.= " WHERE fk_object = ".$rowid;
dol_syslog(get_class($this)."::fetch_optionals sql=".$sql, LOG_DEBUG);
$resql=$this->db->query( $sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{
$tab = $this->db->fetch_array($resql);
foreach ($tab as $key => $value)
{
if ($key != 'rowid' && $key != 'tms' && $key != 'fk_member')
{
// we can add this attribute to adherent object
$this->array_options["options_$key"]=$value;
}
}
}
$this->db->free($resql);
}
else
{
dol_print_error($this->db);
}
}
}
// --------------------
// TODO: All functions here must be redesigned and moved as they are not business functions but output functions
// --------------------
/**
*
@ -1547,7 +1602,7 @@ class CommonObject
/**
* Show linked object block
* TODO Move this into html.class.php
* But for the moment we don't know if it'st possible as we keep a method available on overloaded objects.
* But for the moment we don't know if it's possible as we keep a method available on overloaded objects.
*/
function showLinkedObjectBlock()
{

View File

@ -202,7 +202,7 @@ class modAdherent extends DolibarrModules
// Fin complement
$this->export_sql_start[$r]='SELECT DISTINCT ';
$this->export_sql_end[$r] =' FROM ('.MAIN_DB_PREFIX.'adherent_type as ta, '.MAIN_DB_PREFIX.'adherent as a)';
$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'adherent_extrafields as ao ON a.rowid = ao.fk_member';
$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'adherent_extrafields as ao ON a.rowid = ao.fk_object';
$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'cotisation as c ON c.fk_adherent = a.rowid';
$this->export_sql_end[$r] .=' WHERE a.fk_adherent_type = ta.rowid';

View File

@ -383,12 +383,16 @@ alter table llx_propal add column tms timestamp after fk_projet;
create table llx_societe_extrafields
(
rowid integer PRIMARY KEY
rowid integer AUTO_INCREMENT PRIMARY KEY,
tms timestamp,
fk_object integer NOT NULL
) ENGINE=innodb;
create table llx_product_extrafields
(
rowid integer PRIMARY KEY
rowid integer AUTO_INCREMENT PRIMARY KEY,
tms timestamp,
fk_object integer NOT NULL
) ENGINE=innodb;
@ -397,6 +401,7 @@ alter table llx_adherent_options_label rename to llx_extrafields;
ALTER TABLE llx_extrafields ADD COLUMN elementtype varchar(64) NOT NULL DEFAULT 'member' AFTER entity;
ALTER TABLE llx_extrafields ADD UNIQUE INDEX uk_extrafields_name (name, entity, elementtype);
ALTER TABLE llx_adherent_options rename to llx_adherent_extrafields;
ALTER TABLE llx_adherent_extrafields CHANGE COLUMN fk_member fk_object integer NOT NULL;
-- drop tables renamed into llx_advanced_extra_xxx
drop table llx_extra_fields_options;

View File

@ -24,5 +24,5 @@ create table llx_adherent_extrafields
(
rowid integer AUTO_INCREMENT PRIMARY KEY,
tms timestamp,
fk_member integer NOT NULL -- member id
fk_object integer NOT NULL -- member id
)ENGINE=innodb;

View File

@ -20,5 +20,7 @@
create table llx_product_extrafields
(
rowid integer PRIMARY KEY
rowid integer AUTO_INCREMENT PRIMARY KEY,
tms timestamp,
fk_object integer NOT NULL
) ENGINE=innodb;

View File

@ -20,6 +20,8 @@
create table llx_societe_extrafields
(
rowid integer PRIMARY KEY
rowid integer AUTO_INCREMENT PRIMARY KEY,
tms timestamp,
fk_object integer NOT NULL
) ENGINE=innodb;

View File

@ -36,6 +36,7 @@ require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php");
require_once(DOL_DOCUMENT_ROOT."/core/class/html.formadmin.class.php");
require_once(DOL_DOCUMENT_ROOT."/core/class/html.formcompany.class.php");
require_once(DOL_DOCUMENT_ROOT."/core/class/html.formfile.class.php");
require_once(DOL_DOCUMENT_ROOT."/core/class/extrafields.class.php");
require_once(DOL_DOCUMENT_ROOT."/contact/class/contact.class.php");
if ($conf->adherent->enabled) require_once(DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php");
@ -74,6 +75,8 @@ else
$error=0; $errors=array();
$extrafields = new ExtraFields($db);
/*
* Actions
@ -461,6 +464,9 @@ else
* View
*/
// fetch optionals attributes and labels
$extralabels=$extrafields->fetch_name_optionals_label('company');
$help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
llxHeader('',$langs->trans("ThirdParty"),$help_url);
@ -817,14 +823,8 @@ else
if (empty($conf->global->SOCIETE_DISABLE_STATE))
{
print '<tr><td>'.$langs->trans('State').'</td><td colspan="3">';
if ($soc->pays_id)
{
$formcompany->select_departement($soc->departement_id,$soc->pays_code);
}
else
{
print $countrynotdefined;
}
if ($soc->pays_id) $formcompany->select_departement($soc->departement_id,$soc->pays_code);
else print $countrynotdefined;
print '</td></tr>';
}
@ -982,6 +982,12 @@ else
print '</td></tr>';
}
// Attribut optionnels
foreach($extrafields->attribute_label as $key=>$value)
{
print "<tr><td>".$value.'</td><td colspan="3"><input type="text" name="options_'.$key.'" size="40" value="'.(isset($_POST["options_".$key])?$_POST["options_".$key]:'').'"></td></tr>'."\n";
}
// Ajout du logo
print '<tr>';
print '<td>'.$langs->trans("Logo").'</td>';
@ -1006,6 +1012,12 @@ else
if ($socid)
{
$soc = new Societe($db);
$res=$soc->fetch($socid);
if ($res < 0) { dol_print_error($db,$soc->error); exit; }
$res=$soc->fetch_optionals($socid,$extralabels);
if ($res < 0) { dol_print_error($db); exit; }
// Load object modCodeTiers
$module=$conf->global->SOCIETE_CODECLIENT_ADDON;
if (! $module) dolibarr_error('',$langs->trans("ErrorModuleThirdPartyCodeInCompanyModuleNotDefined"));
@ -1034,14 +1046,14 @@ else
$prefixSupplierIsUsed = $modCodeFournisseur->verif_prefixIsUsed();
}
if (! $_POST["nom"])
{
$soc = new Societe($db);
$soc->fetch($socid);
}
else
{
$soc->id=$_POST["socid"];
// if (! $_POST["nom"])
// {
// $soc->fetch($socid);
// $soc->fetch_optionals($socid,$extralabels);
// }
// else
// {
// $soc->id=$_POST["socid"];
$soc->nom=$_POST["nom"];
$soc->prefix_comm=$_POST["prefix_comm"];
$soc->client=$_POST["client"];
@ -1093,7 +1105,7 @@ else
$soc->pays_code=$obj->code;
$soc->pays=$langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->libelle;
}
}
// }
dol_htmloutput_errors($error,$errors);
@ -1394,9 +1406,15 @@ else
print '</tr>';
}
// Other attributes
foreach($extrafields->attribute_label as $key=>$value)
{
print "<tr><td>".$value."</td><td colspan=\"3\"><input type=\"text\" name=\"options_".$key."\" size=\"40\" value=\"".$soc->array_options["options_$key"]."\"></td></tr>\n";
}
// Logo
print '<tr>';
print '<td>'.$langs->trans("Logo").'</span></td>';
print '<td>'.$langs->trans("Logo").'</td>';
print '<td colspan="3">';
if ($soc->logo) print $form->showphoto('societe',$soc,50);
$caneditfield=1;
@ -1430,12 +1448,11 @@ else
* View
*/
$soc = new Societe($db);
$result=$soc->fetch($socid);
if ($result < 0)
{
dol_print_error($db,$soc->error);
exit;
}
$res=$soc->fetch($socid);
if ($res < 0) { dol_print_error($db,$soc->error); exit; }
$res=$soc->fetch_optionals($socid,$extralabels);
if ($res < 0) { dol_print_error($db); exit; }
$head = societe_prepare_head($soc);
@ -1722,6 +1739,12 @@ else
print '</td></tr>';
}
// Other attributes
foreach($extrafields->attribute_label as $key=>$value)
{
print "<tr><td>".$value."</td><td>".$soc->array_options["options_$key"]."&nbsp;</td></tr>\n";
}
// Ban
if (empty($conf->global->SOCIETE_DISABLE_BANKACCOUNT))
{