Clean code of hooks.

Extrafield index on fk_object must be unique.
This commit is contained in:
Laurent Destailleur 2015-03-13 20:27:54 +01:00
parent f090893807
commit ba2c0a2651
6 changed files with 35 additions and 10 deletions

View File

@ -596,7 +596,7 @@ class ExtraFields
$this->attribute_pos[$tab->name]=$tab->pos;
$this->attribute_alwayseditable[$tab->name]=$tab->alwayseditable;
$this->attribute_perms[$tab->name]=$tab->perms;
$this->attribute_perms[$tab->name]=$tab->list;
$this->attribute_list[$tab->name]=$tab->list;
}
}
}

View File

@ -128,10 +128,8 @@ class HookManager
$parameters['context']=join(':',$this->contextarray);
dol_syslog(get_class($this).'::executeHooks method='.$method." action=".$action." context=".$parameters['context']);
// Define type of hook ('output', 'returnvalue' or 'addreplace'). 'addreplace' should be type for all hooks. 'output' and 'returnvalue' are deprecated.
// Define type of hook ('output' or 'addreplace'. 'returnvalue' is deprecated).
$hooktype='output';
if (preg_match('/^pdf_/',$method)) $hooktype='returnvalue'; // pdf_xxx except pdf_writelinedesc are returnvalue hooks. When there is 2 hooks of this type, only last one win.
if ($method =='insertExtraFields') $hooktype='returnvalue';
if (in_array(
$method,
array(
@ -149,6 +147,13 @@ class HookManager
'formatEvent'
)
)) $hooktype='addreplace';
// Deprecated hook types
if (preg_match('/^pdf_/',$method) && $method != 'pdf_writelinedesc') $hooktype='returnvalue'; // pdf_xxx except pdf_writelinedesc are 'returnvalue' hooks. When there is 2 hooks of this type, only last one win. TODO Move them into 'output' or 'addreplace' hooks.
if ($method == 'insertExtraFields')
{
$hooktype='returnvalue'; // deprecated. TODO Remove all code with "executeHooks('insertExtraFields'" as soon as there is a trigger available.
dol_syslog("Warning: The hook 'insertExtraFields' is deprecated and must not be used. Use instead trigger on CRUD event (ask it to dev team if not implemented)", LOG_WARNING);
}
// Loop on each hook to qualify modules that have declared context
$modulealreadyexecuted=array();
@ -201,8 +206,6 @@ class HookManager
if (! empty($actionclassinstance->results) && is_array($actionclassinstance->results)) $this->resArray =array_merge($this->resArray, $actionclassinstance->results);
if (! empty($actionclassinstance->resprints)) $this->resPrint.=$actionclassinstance->resprints;
// TODO dead code to remove (do not enable this, but fix hook instead)
//if (is_array($result)) $this->resArray = array_merge($this->resArray, $result);
// TODO dead code to remove (do not enable this, but fix hook instead): result must not be a string. we must use $actionclassinstance->resprints to return a string
if (! is_array($result) && ! is_numeric($result))
{
@ -221,8 +224,8 @@ class HookManager
}
// TODO remove this. When there is something to print for an output hook, ->resPrint is filled.
if ($hooktype == 'output') return $this->resPrint;
if ($hooktype == 'returnvalue') return $result;
//if ($hooktype == 'output') return $this->resPrint;
//if ($hooktype == 'returnvalue') return $result;
return ($error?-1:$resaction);
}

View File

@ -31,6 +31,7 @@ require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
if (! empty($conf->adherent->enabled)) require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
$langs->load('suppliers');
@ -49,6 +50,7 @@ if ($user->societe_id) $id=$user->societe_id;
$result = restrictedArea($user, 'societe&fournisseur', $id, '&societe');
$object = new Fournisseur($db);
$extrafields = new ExtraFields($db);
// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
$hookmanager->initHooks(array('suppliercard','globalcard'));

View File

@ -27,6 +27,7 @@
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
$langs->load("suppliers");
$langs->load("orders");
@ -59,6 +60,7 @@ if (! $sortfield) $sortfield="nom";
// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
$hookmanager->initHooks(array('supplierlist'));
$extrafields = new ExtraFields($db);
if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both test are required to be compatible with all browsers
{
@ -73,6 +75,9 @@ if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both
$catid="";
}
$extrafields->fetch_name_optionals_label('thirdparty');
/*
* Actions
*/
@ -95,7 +100,14 @@ llxHeader('',$langs->trans("ThirdParty"),$help_url);
$sql = "SELECT s.rowid as socid, s.nom as name, s.zip, s.town, s.datec, st.libelle as stcomm, s.prefix_comm, s.status as status, ";
$sql.= "code_fournisseur, code_compta_fournisseur";
if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user ";
// Add fields for extrafields
foreach ($extrafields->attribute_list as $key => $val) $sql.=",ef.".$key;
// Add fields from hooks
$parameters=array();
$hookmanager->executeHooks('printFieldListSelect',$parameters); // Note that $action and $object may have been modified by hook
$sql.=$hookmanager->resPrint;
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as ef ON ef.fk_object = s.rowid";
if (! empty($search_categ) || ! empty($catid)) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_fournisseur as cf ON s.rowid = cf.fk_societe"; // We need this table joined to the select in order to filter by categ
$sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st";
if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
@ -122,6 +134,9 @@ if ($catid > 0) $sql.= " AND cf.fk_categorie = ".$catid;
if ($catid == -2) $sql.= " AND cf.fk_categorie IS NULL";
if ($search_categ > 0) $sql.= " AND cf.fk_categorie = ".$search_categ;
if ($search_categ == -2) $sql.= " AND cf.fk_categorie IS NULL";
// Add where from hooks
$parameters=array();
$sql.=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
// Count total nb of records
$nbtotalofrecords = 0;
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
@ -131,6 +146,7 @@ if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
}
$sql.= $db->order($sortfield,$sortorder);
$sql.= $db->plimit($conf->liste_limit+1, $offset);
//print $sql;
dol_syslog('fourn/list.php:', LOG_DEBUG);
$resql = $db->query($sql);
@ -200,7 +216,7 @@ if ($resql)
print '<td class="liste_titre" align="right"><input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans("Search"),'search.png','','',1).'" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
print '<input type="image" class="liste_titre" name="button_removefilter" src="'.img_picto($langs->trans("Search"),'searchclear.png','','',1).'" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
print "</td></tr>\n";
$parameters=array();
$formconfirm=$hookmanager->executeHooks('printFieldListOption',$parameters); // Note that $action and $object may have been modified by hook

View File

@ -366,3 +366,7 @@ INSERT INTO llx_c_incoterms (code, libelle, active) VALUES ('CIP', 'Carriage and
INSERT INTO llx_c_incoterms (code, libelle, active) VALUES ('DAT', 'Delivered At Terminal, marchandises (déchargées) livrées sur quai, dans un terminal maritime, fluvial, aérien, routier ou ferroviaire désigné (dédouanement import, et post-acheminement payés par l''acheteur)', 1);
INSERT INTO llx_c_incoterms (code, libelle, active) VALUES ('DAP', 'Delivered At Place, marchandises (non déchargées) mises à disposition de l''acheteur dans le pays d''importation au lieu précisé dans le contrat (déchargement, dédouanement import payé par l''acheteur)', 1);
INSERT INTO llx_c_incoterms (code, libelle, active) VALUES ('DDP', 'Delivered Duty Paid, marchandises (non déchargées) livrées à destination finale, dédouanement import et taxes à la charge du vendeur ; l''acheteur prend en charge uniquement le déchargement (si exclusion des taxes type TVA, le préciser clairement)', 1);
-- Extrafields fk_object must be unique (1-1 relation)
ALTER TABLE llx_societe_extrafields DROP INDEX idx_societe_extrafields
ALTER TABLE llx_societe_extrafields ADD UNIQUE INDEX uk_societe_extrafields (fk_object);

View File

@ -17,4 +17,4 @@
-- ===================================================================
ALTER TABLE llx_societe_extrafields ADD INDEX idx_societe_extrafields (fk_object);
ALTER TABLE llx_societe_extrafields ADD UNIQUE INDEX uk_societe_extrafields (fk_object);