Qual: Mutualize code

New: Can use extrafields on contacts/addresses.
This commit is contained in:
Laurent Destailleur 2012-09-17 01:39:30 +02:00
parent 2170801fd2
commit 3953be53a7
9 changed files with 144 additions and 32 deletions

View File

@ -29,6 +29,7 @@ For users:
- New: Add link to third party into sells and purchase journal.
- New: Suggest a method to generate a backup file for user with no access
to mysqldump binary.
- New: Can use extrafields on contacts/addresses.
- New: Extra fields supports more types (now int, string, double, date, datetime).
- New: Can correct stock of a warehouse from warehouse card.
- New: [ task #185 ]: Can input amount when correcting stock to recalculate PMP.

View File

@ -148,7 +148,7 @@ if ($action == 'create')
print $form->selectarray('type',$type2label,GETPOST('type'));
print '</td></tr>';
// Size
print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'255').'"></td></tr>';
print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input id="size" type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'').'"></td></tr>';
print '</table>';

View File

@ -263,6 +263,22 @@ class Contact extends CommonObject
unset($this->state_code);
unset($this->state);
// Actions on extra fields (by external module or standard code)
include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
$hookmanager=new HookManager($this->db);
$hookmanager->initHooks(array('contactdao'));
$parameters=array('socid'=>$this->id);
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
else if ($reshook < 0) $error++;
if (! $error && ! $notrigger)
{
// Appel des triggers
@ -809,11 +825,11 @@ class Contact extends CommonObject
$sql.= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
$sql.= " WHERE mc.email = '".$this->db->escape($this->email)."'";
$sql.= " AND mc.statut NOT IN (-1,0)"; // -1 erreur, 0 non envoye, 1 envoye avec succes
dol_syslog(get_class($this)."::getNbOfEMailings sql=".$sql, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);

View File

@ -31,6 +31,7 @@ require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/contact.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
$langs->load("companies");
$langs->load("users");
@ -47,6 +48,7 @@ $socid = GETPOST('socid','int');
if ($user->societe_id) $socid=$user->societe_id;
$object = new Contact($db);
$extrafields = new ExtraFields($db);
// Get object canvas (By default, this is not defined, so standard usage of dolibarr)
$object->getCanvas($id);
@ -74,6 +76,7 @@ $hookmanager->initHooks(array('contactcard'));
$parameters=array('id'=>$id, 'objcanvas'=>$objcanvas);
$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
$error=$hookmanager->error; $errors=array_merge($errors, (array) $hookmanager->errors);
if (empty($reshook))
{
@ -154,6 +157,15 @@ if (empty($reshook))
$object->birthday = dol_mktime(0,0,0,$_POST["birthdaymonth"],$_POST["birthdayday"],$_POST["birthdayyear"]);
$object->birthday_alert = $_POST["birthday_alert"];
// Get extra fields
foreach($_POST as $key => $value)
{
if (preg_match("/^options_/",$key))
{
$object->array_options[$key]=GETPOST($key);
}
}
if (! $_POST["lastname"])
{
$error++; $errors[]=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Lastname").' / '.$langs->transnoentities("Label"));
@ -242,6 +254,15 @@ if (empty($reshook))
$object->priv = $_POST["priv"];
$object->note = $_POST["note"];
// Get extra fields
foreach($_POST as $key => $value)
{
if (preg_match("/^options_/",$key))
{
$object->array_options[$key]=GETPOST($key);
}
}
$result = $object->update($_POST["contactid"], $user);
if ($result > 0)
@ -264,6 +285,9 @@ if (empty($reshook))
* View
*/
// fetch optionals attributes and labels
$extralabels=$extrafields->fetch_name_optionals_label('contact');
$help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
llxHeader('',$langs->trans("ContactsAddresses"),$help_url);
@ -314,12 +338,9 @@ else
{
// Si edition contact deja existant
$object = new Contact($db);
$return=$object->fetch($id, $user);
if ($return <= 0)
{
dol_print_error('',$object->error);
$id=0;
}
$res=$object->fetch($id, $user);
if ($res < 0) { dol_print_error($db,$object->error); exit; }
$res=$object->fetch_optionals($object->id,$extralabels);
// Show tabs
$head = contact_prepare_head($object);
@ -471,6 +492,20 @@ else
// Note
print '<tr><td valign="top">'.$langs->trans("Note").'</td><td colspan="3" valign="top"><textarea name="note" cols="70" rows="'.ROWS_3.'">'.(isset($_POST["note"])?$_POST["note"]:$object->note).'</textarea></td></tr>';
// Other attributes
$parameters=array('colspan' => ' colspan="3"');
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
foreach($extrafields->attribute_label as $key=>$label)
{
$value=(isset($_POST["options_".$key])?$_POST["options_".$key]:(isset($object->array_options["options_".$key])?$object->array_options["options_".$key]:''));
print '<tr><td>'.$label.'</td><td colspan="3">';
print $extrafields->showInputField($key,$value);
print '</td></tr>'."\n";
}
}
print "</table><br>";
@ -503,10 +538,6 @@ else
}
print '</tr>';
// Other attributes
$parameters=array('colspan' => ' colspan="3"');
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
print "</table><br><br>";
@ -651,11 +682,26 @@ else
print $form->selectarray('priv',$selectarray,$object->priv,0);
print '</td></tr>';
// Note
print '<tr><td valign="top">'.$langs->trans("Note").'</td><td colspan="3">';
print '<textarea name="note" cols="70" rows="'.ROWS_3.'">';
print isset($_POST["note"])?$_POST["note"]:$object->note;
print '</textarea></td></tr>';
// Other attributes
$parameters=array('colspan' => ' colspan="3"');
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
foreach($extrafields->attribute_label as $key=>$label)
{
$value=(isset($_POST["options_".$key])?$_POST["options_".$key]:$object->array_options["options_".$key]);
print '<tr><td>'.$label.'</td><td colspan="3">';
print $extrafields->showInputField($key,$value);
print "</td></tr>\n";
}
}
$object->load_ref_elements();
if (! empty($conf->commande->enabled))
@ -697,10 +743,6 @@ else
else print $langs->trans("NoDolibarrAccess");
print '</td></tr>';
// Other attributes
$parameters=array('colspan' => ' colspan="3"');
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
print '</table><br>';
print '<center>';
@ -851,10 +893,25 @@ else
print $object->LibPubPriv($object->priv);
print '</td></tr>';
// Note
print '<tr><td valign="top">'.$langs->trans("Note").'</td><td colspan="3">';
print nl2br($object->note);
print '</td></tr>';
// Other attributes
$parameters=array('socid'=>$socid, 'colspan' => ' colspan="3"');
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
foreach($extrafields->attribute_label as $key=>$label)
{
$value=(isset($_POST["options_".$key])?$_POST["options_".$key]:(isset($object->array_options['options_'.$key])?$object->array_options['options_'.$key]:''));
print '<tr><td>'.$label.'</td><td colspan="3">';
print $extrafields->showOutputField($key,$value);
print "</td></tr>\n";
}
}
$object->load_ref_elements();
if (! empty($conf->commande->enabled))
@ -895,10 +952,6 @@ else
else print $langs->trans("NoDolibarrAccess");
print '</td></tr>';
// Other attributes
$parameters=array('colspan' => ' colspan="3"');
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
print "</table>";
print "</div>";

View File

@ -197,13 +197,10 @@ function societe_admin_prepare_head($object)
$head[$h][2] = 'attributes';
$h++;
if ($conf->global->MAIN_FEATURES_LEVEL >= 2)
{
$head[$h][0] = DOL_URL_ROOT.'/societe/admin/contact_extrafields.php';
$head[$h][1] = $langs->trans("ExtraFieldsContacts");
$head[$h][2] = 'attributes_contacts';
$h++;
}
$head[$h][0] = DOL_URL_ROOT.'/societe/admin/contact_extrafields.php';
$head[$h][1] = $langs->trans("ExtraFieldsContacts");
$head[$h][2] = 'attributes_contacts';
$h++;
complete_head_from_modules($conf,$langs,$object,$head,$h,'company_admin','remove');

View File

@ -0,0 +1,39 @@
<?php
/* Copyright (C) 2010-2012 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 2 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/>.
*/
?>
<!-- BEGIN PHP TEMPLATE admin_extrafields.tpl.php -->
<script type="text/javascript">
jQuery(document).ready(function() {
function init_typeoffields()
{
if (jQuery("#type").val() == 'date') { jQuery("#size").val(''); jQuery("#size").attr('disabled','disabled'); }
else if (jQuery("#type").val() == 'datetime') { jQuery("#size").val(''); jQuery("#size").attr('disabled','disabled'); }
else if (jQuery("#type").val() == 'double') { jQuery("#size").val('24,8'); jQuery("#size").removeAttr('disabled'); }
else if (jQuery("#type").val() == 'int') { jQuery("#size").val('10'); jQuery("#size").removeAttr('disabled'); }
else if (jQuery("#type").val() == 'text') { jQuery("#size").val('2000'); jQuery("#size").removeAttr('disabled'); }
else if (jQuery("#type").val() == 'varchar') { jQuery("#size").val('255'); jQuery("#size").removeAttr('disabled'); }
else if (jQuery("#type").val() == '') { jQuery("#size").val(''); jQuery("#size").attr('disabled','disabled'); }
else jQuery("#size").attr('disabled','disabled');
}
init_typeoffields();
jQuery("#type").change(function() {
init_typeoffields();
});
});
</script>
<!-- END PHP TEMPLATE admin_extrafields.tpl.php -->

View File

@ -146,6 +146,8 @@ if ($action == 'create')
print "<br>";
print_titre($langs->trans('NewAttribute'));
require DOL_DOCUMENT_ROOT.'/core/tpl/admin_extrafields.tpl.php';
print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<table summary="listofattributes" class="border" width="100%">';
@ -161,7 +163,7 @@ if ($action == 'create')
print $form->selectarray('type',$type2label,GETPOST('type'));
print '</td></tr>';
// Size
print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'255').'"></td></tr>';
print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input id="size" type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'').'"></td></tr>';
print "</table>\n";

View File

@ -133,6 +133,8 @@ if ($action == 'create')
print "<br>";
print_titre($langs->trans('NewAttribute'));
require DOL_DOCUMENT_ROOT.'/core/tpl/admin_extrafields.tpl.php';
print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<table summary="listofattributes" class="border" width="100%">';
@ -148,7 +150,7 @@ if ($action == 'create')
print $form->selectarray('type',$type2label,GETPOST('type'));
print '</td></tr>';
// Size
print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'255').'"></td></tr>';
print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input id="size" type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'').'"></td></tr>';
print "</table>\n";

View File

@ -133,6 +133,8 @@ if ($action == 'create')
print "<br>";
print_titre($langs->trans('NewAttribute'));
require DOL_DOCUMENT_ROOT.'/core/tpl/admin_extrafields.tpl.php';
print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<table summary="listofattributes" class="border" width="100%">';
@ -148,7 +150,7 @@ if ($action == 'create')
print $form->selectarray('type',$type2label,GETPOST('type'));
print '</td></tr>';
// Size
print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'255').'"></td></tr>';
print '<tr><td class="fieldrequired" required>'.$langs->trans("Size").'</td><td><input id="size" type="text" name="size" size="5" value="'.(GETPOST('size')?GETPOST('size'):'').'"></td></tr>';
print "</table>\n";