Difference between revisions of "Extrafields"

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
Line 33: Line 33:
 
<source lang="php">
 
<source lang="php">
 
$extrafields = new ExtraFields($db);
 
$extrafields = new ExtraFields($db);
$extralabels=$extrafields->fetch_name_optionals_label('''ObjectClassName''');
+
$extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
 
</source>
 
</source>
  
$extralabels will be an array with code and label of extrafields.  
+
$extralabels will be an array with code and label of extrafields.
 
 
  
 
== Load extrafield into object ==
 
== Load extrafield into object ==

Revision as of 08:10, 26 April 2013

Extrafields

Extrafields allow to add others datas to Dolibarr standard database schema. Introduced in version 3.2, extrafields are now implemented in businesses object :

  • Thirdparties & contacts & users
  • Events
  • Customer Invoices
  • Proposal
  • Products & services
  • Member & member type
  • Orders
  • Projects & project tasks (since 3.4)

Technical implementation

Each object have his table in database to store values of extrafields.

Datatables extrafield tables models

 llx_BaseTableNAme_extrafields
 rowid                     integer AUTO_INCREMENT PRIMARY KEY,
 tms                       timestamp,
 fk_object                 integer NOT NULL,
 import_key                varchar(14)

Load class for extrafields

To use extrafields all pages need include

require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';

Load extrafield array in object

$extrafields = new ExtraFields($db);
$extralabels=$extrafields->fetch_name_optionals_label($object->table_element);

$extralabels will be an array with code and label of extrafields.

Load extrafield into object

Extrafields are loaded in $object->array_options array.

$object->fetch($rowid);
$object->fetch_optionals($rowid,$extralabels);

Before call $object->create() or $object->update()

Values of extrafields sent by form need to be retrieve. For do that, use this method :

$ret = $extrafields->setOptionalsFromPost($extralabels,$object);


Inside edit page to display extrafields

$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$act,$action);    // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
      print $object->showOptionals($extrafields,'edit');
}

Inside view page to display extrafields

$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$act,$action);    // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
      print $object->showOptionals($extrafields);
}

After fetch method of this object class

require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
$extrafields=new ExtraFields($this->db);
$extralabels=$extrafields->fetch_name_optionals_label('company',true);
if (count($extralabels)>0) {
	$this->array_options = array();
}
foreach($extrafields->attribute_label as $key=>$label)
{
	$this->array_options['options_'.$key]=$label;
}

After Update method of this object class :

// Actions on extra fields (by external module or standard code)
// FIXME le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('HookModuleNamedao'));
$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))
{
	if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
	{
            $result=$this->insertExtraFields();
            if ($result < 0)
            {
            	$error++;
            }
        }
}
else if ($reshook < 0) $error++;