Module Development using Module Builder

Dolibarr 8.0.2. Using Module Builder, I created a module and added an object (location) such that I can record a few hundred sites that my customer has. I’ve edited …/htdocs/customer/mymodule/location.class.php to setup the fields array as …

public $fields=array(
 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
 'ref' => array('type'=>'varchar(128)', 'label'=>'Name', 'enabled'=>1, 'visible'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Site/Location Name", 'showoncombobox'=>'1',),
 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>1, 'position'=>50, 'notnull'=>1, 'index'=>1, 'help'=>"LinkToThirparty",),
 'latitude' => array('type'=>'double(9,6)', 'label'=>'Latitude/y', 'enabled'=>1, 'visible'=>1, 'position'=>50, 'notnull'=>-1,),
 'longitude' => array('type'=>'double(9,6)', 'label'=>'Longitude/x', 'enabled'=>1, 'visible'=>1, 'position'=>51, 'notnull'=>-1,),
		....

Problem one…
When I list the locations or display the location card the coordinates are fine, eg.


But when I modify, the coordinates are truncated to two decimals, in the edit box (my coordinate sql column type is double(9,6).

Problem two…
When I list the locations I cannot correctly order by, or filter on, the third party. The box in the title of the list is taking the integer value for fk_soc rather than the result of indexing through llx_societe.rowid. I have tried adding ‘foreignkey’=>‘llx_societe.rowid’ attribute to the fk_soc definition but that seems to have no effect.

Solution to Problem One
The problem with loosing decimals place is caused by the code around core/class/commonobject.class.php@5345 which is not properly generic and needs improvement. Since I did not wish to change common code I added these this in my custom module as a work around (beginning at my_object_card.php@222)…

	dol_fiche_head();

	print '<table class="border centpercent">' . "\n";

// RJE 2018-10-29 ++
	/*
	 * The code around line core/class/commonobject.class.php@5345 is not properly generic and needs improvement, in
	 * the means time I've fiddled with MAIN_MAX_DECIMALS_SHOWN because I'm not happy to change common code
	 *
	 * 5345...
	 * 	elseif ($type == 'double')
	 * 	{
	 * 		if (!empty($value)) {		// $value in memory is a php numeric, we format it into user number format.
	 * 			$value=price($value);
	 *
	 */
	$nbdecimal_save = $conf->global->MAIN_MAX_DECIMALS_SHOWN;
	$conf->global->MAIN_MAX_DECIMALS_SHOWN = 6;
// RJE --
	// Common attributes
	include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_edit.tpl.php';

// RJE 2018-10-29 ++
	$conf->global->MAIN_MAX_DECIMALS_SHOWN = $nbdecimal_save;
// RJE --
	// Other attributes
	include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_edit.tpl.php';

	print '</table>';

	dol_fiche_end();

My additions between RJE ++ and RJE --.

Solution to Problem Two
This problem is caused by the module builder not fully handling third parties as columns in a list of objects. To solve this I made these additions around my_object_list.php@115

// Default sort order (if not yet defined by previous GETPOST)
if (!$sortfield)
	$sortfield = "t." . key($object->fields); // Set here default search field. By default 1st field in definition.


// RJE 2018-10-29 ++
if ($sortfield == 't.fk_soc')
	$sortfield = 's.nom';
// RJE --

if (!$sortorder)
	$sortorder = "ASC";

// Security check
$socid = 0;

… and around my_object_list.php@227 …

// Build and execute select
// --------------------------------------------------------------------
$sql = 'SELECT ';
foreach ($object->fields as $key => $val) {
	$sql .= 't.' . $key . ', ';
}
// Add fields from extrafields
if (!empty($extrafields->attributes[$object->table_element]['label']))
	foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val)
		$sql .= ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? "ef." . $key . ' as options_' . $key . ', ' : '');
// Add fields from hooks
// RJE 2018-10-29 ++
$sql .= 's.nom, ';
// RJE --

$parameters = array();
$reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object);  // Note that $action and $object may have been modified by hook
$sql .= $hookmanager->resPrint;
$sql = preg_replace('/, $/', '', $sql);
$sql .= " FROM " . MAIN_DB_PREFIX . $object->table_element . " as t";

// RJE 2018-10-29 ++
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON t.fk_soc = s.rowid";
// RJE --

if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label']))
	$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . $object->table_element . "_extrafields as ef on (t.rowid = ef.fk_object)";
if ($object->ismultientitymanaged == 1)
	$sql .= " WHERE t.entity IN (" . getEntity($object->element) . ")";
else
	$sql .= " WHERE 1 = 1";
foreach ($search as $key => $val) {
	if ($key == 'status' && $search[$key] == -1)
		continue;
	$mode_search = (($object->isInt($object->fields[$key]) || $object->isFloat($object->fields[$key])) ? 1 : 0);

// RJE 2018-10-29 ++
	if ($key == 'fk_soc') {
		$sql .= natural_search('s.nom', $search[$key], 0);
	} else
// RJE --

	if ($search[$key] != '')
		$sql .= natural_search($key, $search[$key], (($key == 'status') ? 2 : $mode_search));
}
if ($search_all)
	$sql .= natural_search(array_keys($fieldstosearchall), $search_all);
// Add where from extra fields

Hope that helps somebody !!! :cheer:

Hello I’ve question regrads the module development. I tried to set the module builder https://wiki.dolibarr.org/index.php/Module_development#Module_creation_with_the_module_builder
and already set the MAIN_FEATURES_LEVEL to 2 but no bug icon show beside the print button. Is there anything I miss? Thanks

I found it. seems it need module builder to be activated, and it will create new module. Thanks

Hello,

wich Dolibarr version do you use?