diff --git a/htdocs/core/lib/modulebuilder.lib.php b/htdocs/core/lib/modulebuilder.lib.php index 282a02835c3..23d6597b227 100644 --- a/htdocs/core/lib/modulebuilder.lib.php +++ b/htdocs/core/lib/modulebuilder.lib.php @@ -34,7 +34,7 @@ * @param string $readdir Directory source (use $destdir when not defined) * @param string $addfieldentry Array of the field entry to add array('key'=>,'type'=>,''label'=>,'visible'=>,'enabled'=>,'position'=>,'notnull'=>','index'=>,'searchall'=>,'comment'=>,'help'=>,'isameasure') * @param string $delfieldentry Id of field to remove - * @return int <=0 if KO, >0 if OK + * @return int|object <=0 if KO, Object if OK */ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir='', $addfieldentry=array() ,$delfieldentry='') { @@ -43,6 +43,12 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir=' if (empty($objectname)) return -1; if (empty($readdir)) $readdir=$destdir; + if (! empty($addfieldentry['arrayofkeyval']) && ! is_array($addfieldentry['arrayofkeyval'])) + { + dol_print_error('', 'Bad parameter addfieldentry with a property arrayofkeyval defined but that is not an array.'); + return -1; + } + // Check parameters if (count($addfieldentry) > 0) { @@ -92,8 +98,9 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir=' { if (is_array($addfieldentry) && count($addfieldentry)) { - $name=$addfieldentry['name']; + $name=$addfieldentry['name']; unset($addfieldentry['name']); + $object->fields[$name]=$addfieldentry; } if (! empty($delfieldentry)) @@ -114,6 +121,7 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir=' if (count($object->fields)) { + foreach($object->fields as $key => $val) { $i++; @@ -127,11 +135,24 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir=' if ($val['comment']) $texttoinsert.= " 'comment'=>'".$val['comment']."',"; if ($val['isameasure']) $texttoinsert.= " 'isameasure'=>'".$val['isameasure']."',"; if ($val['help']) $texttoinsert.= " 'help'=>'".$val['help']."',"; + if ($val['arrayofkeyval']) + { + $texttoinsert.= " 'arrayofkeyval'=>array("; + $i=0; + foreach($val['arrayofkeyval'] as $key2 => $val2) + { + if ($i) $texttoinsert.=", "; + $texttoinsert.="'".$key2."'=>'".$val2."'"; + $i++; + } + $texttoinsert.= ")"; + } $texttoinsert.= "),\n"; } } $texttoinsert.= "\t".');'."\n"; + //print ($texttoinsert);exit; if (count($object->fields)) { @@ -162,7 +183,7 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir=' file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass); @chmod($pathoffiletoedittarget, octdec($newmask)); - return 1; + return $object; } catch(Exception $e) { @@ -179,12 +200,15 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir=' * @param string $objectname Name of object * @param string $newmask New mask * @param string $readdir Directory source (use $destdir when not defined) + * @param Object $object If object was already loaded/known, it is pass to avaoid another include and new. * @return int <=0 if KO, >0 if OK */ -function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='') +function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='', $object=null) { global $db, $langs; + $error = 0; + if (empty($objectname)) return -1; if (empty($readdir)) $readdir=$destdir; @@ -200,11 +224,15 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='') return -1; } + // Load object from myobject.class.php try { - include_once $pathoffiletoclasssrc; - if (class_exists($objectname)) $object=new $objectname($db); - else return -1; + if (! is_object($object)) + { + include_once $pathoffiletoclasssrc; + if (class_exists($objectname)) $object=new $objectname($db); + else return -1; + } } catch(Exception $e) { @@ -235,9 +263,15 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='') $contentsql = preg_replace('/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql); - file_put_contents($pathoffiletoedittarget, $contentsql); - @chmod($pathoffiletoedittarget, octdec($newmask)); - + $result = file_put_contents($pathoffiletoedittarget, $contentsql); + if ($result) + { + @chmod($pathoffiletoedittarget, octdec($newmask)); + } + else + { + $error++; + } // Edit .key.sql file $pathoffiletoeditsrc=$destdir.'/sql/llx_'.strtolower($objectname).'.key.sql'; @@ -265,10 +299,17 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='') dol_mkdir(dirname($pathoffiletoedittarget)); - file_put_contents($pathoffiletoedittarget, $contentsql); - @chmod($pathoffiletoedittarget, octdec($newmask)); + $result2 = file_put_contents($pathoffiletoedittarget, $contentsql); + if ($result) + { + @chmod($pathoffiletoedittarget, octdec($newmask)); + } + else + { + $error++; + } - return 1; + return $error ? -1 : 1; } diff --git a/htdocs/langs/en_US/modulebuilder.lang b/htdocs/langs/en_US/modulebuilder.lang index 0f62870b299..891faa8e134 100644 --- a/htdocs/langs/en_US/modulebuilder.lang +++ b/htdocs/langs/en_US/modulebuilder.lang @@ -51,6 +51,8 @@ DatabaseIndex=Database index FileAlreadyExists=File %s already exists TriggersFile=File for triggers code HooksFile=File for hooks code +ArrayOfKeyValues=Array of key-val +ArrayOfKeyValuesDesc=Array of keys and values if field is a combo list with fixed values WidgetFile=Widget file ReadmeFile=Readme file ChangeLog=ChangeLog file diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 48bfb1731ae..5ff69549182 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -337,13 +337,13 @@ if ($dirins && $action == 'initobject' && $module && $objectname) if (! $error) { // Edit the class file to write properties - $result=rebuildObjectClass($destdir, $module, $objectname, $newmask); - if ($result < 0) $error++; + $object=rebuildObjectClass($destdir, $module, $objectname, $newmask); + if (is_numeric($object) && $object < 0) $error++; } if (! $error) { // Edit sql with new properties - $result=rebuildObjectSql($destdir, $module, $objectname, $newmask); + $result=rebuildObjectSql($destdir, $module, $objectname, $newmask, '', $object); if ($result < 0) $error++; } @@ -362,25 +362,28 @@ if ($dirins && $action == 'addproperty' && !empty($module) && ! empty($tabobj)) dol_mkdir($destdir); $addfieldentry = array( - 'name'=>GETPOST('propname','aZ09'),'type'=>GETPOST('proptype','alpha'),'label'=>GETPOST('proplabel','alpha'),'visible'=>GETPOST('propvisible','int'),'enabled'=>GETPOST('propenabled','int'), + 'name'=>GETPOST('propname','aZ09'),'label'=>GETPOST('proplabel','alpha'),'type'=>GETPOST('proptype','alpha'), + 'arrayofkeyval'=>GETPOST('proparrayofkeyval','none'), // Example json string '{"0":"Draft","1":"Active","-1":"Cancel"}' + 'visible'=>GETPOST('propvisible','int'),'enabled'=>GETPOST('propenabled','int'), 'position'=>GETPOST('propposition','int'),'notnull'=>GETPOST('propnotnull','int'),'index'=>GETPOST('propindex','int'),'searchall'=>GETPOST('propsearchall','int'), 'isameasure'=>GETPOST('propisameasure','int'), 'comment'=>GETPOST('propcomment','alpha'),'help'=>GETPOST('prophelp')); + if (! empty($addfieldentry['arrayofkeyval']) && ! is_array($addfieldentry['arrayofkeyval'])) + { + $addfieldentry['arrayofkeyval'] = dol_json_decode($addfieldentry['arrayofkeyval'], true); + } + // Edit the class file to write properties if (! $error) { - $result=rebuildObjectClass($destdir, $module, $objectname, $newmask, $srcdir, $addfieldentry); - // var_dump($result);exit; - if ($result <= 0) - { - $error++; - } + $object=rebuildObjectClass($destdir, $module, $objectname, $newmask, $srcdir, $addfieldentry); + if (is_numeric($result) && $result <= 0) $error++; } // Edit sql with new properties if (! $error) { - $result=rebuildObjectSql($destdir, $module, $objectname, $newmask, $srcdir); + $result=rebuildObjectSql($destdir, $module, $objectname, $newmask, $srcdir, $object); if ($result <= 0) { $error++; @@ -412,14 +415,14 @@ if ($dirins && $action == 'confirm_deleteproperty' && $propertykey) // Edit the class file to write properties if (! $error) { - $result=rebuildObjectClass($destdir, $module, $objectname, $newmask, $srcdir, array(), $propertykey); - if ($result <= 0) $error++; + $object=rebuildObjectClass($destdir, $module, $objectname, $newmask, $srcdir, array(), $propertykey); + if (is_numeric($object) && $object <= 0) $error++; } // Edit sql with new properties if (! $error) { - $result=rebuildObjectSql($destdir, $module, $objectname, $newmask, $srcdir); + $result=rebuildObjectSql($destdir, $module, $objectname, $newmask, $srcdir, $object); if ($result <= 0) $error++; } @@ -1410,28 +1413,28 @@ elseif (! empty($module)) print '
'; print ' '.$langs->trans("ClassFile").' : '.($realpathtoclass?'':'').$pathtoclass.($realpathtoclass?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '
'; print ' '.$langs->trans("ApiClassFile").' : '.($realpathtoapi?'':'').$pathtoapi.($realpathtoapi?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '   '.$langs->trans("GoToApiExplorer").''; print '
'; print ' '.$langs->trans("TestClassFile").' : '.($realpathtophpunit?'':'').$pathtophpunit.($realpathtophpunit?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '
'; print '
'; print ' '.$langs->trans("SqlFile").' : '.($realpathtosql?'':'').$pathtosql.($realpathtosql?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '   '.$langs->trans("DropTableIfEmpty").''; //print '   '.$langs->trans("RunSql").''; print '
'; print ' '.$langs->trans("SqlFileExtraFields").' : '.($realpathtosqlextra?'':'').$pathtosqlextra.($realpathtosqlextra?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; //print '   '.$langs->trans("RunSql").''; print '
'; print ' '.$langs->trans("SqlFileKey").' : '.($realpathtosqlkey?'':'').$pathtosqlkey.($realpathtosqlkey?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; //print '   '.$langs->trans("RunSql").''; print '
'; @@ -1443,19 +1446,19 @@ elseif (! empty($module)) print '
'; print ' '.$langs->trans("PageForList").' : '.($realpathtosql?'':'').$pathtolist.($realpathtosql?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '
'; print ' '.$langs->trans("PageForCreateEditView").' : '.($realpathtocard?'':'').$pathtocard.($realpathtocard?'':'').'?action=create'; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '
'; print ' '.$langs->trans("PageForAgendaTab").' : '.($realpathtoagenda?'':'').$pathtoagenda.($realpathtoagenda?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '
'; print ' '.$langs->trans("PageForDocumentTab").' : '.($realpathtodocument?'':'').$pathtodocument.($realpathtodocument?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '
'; print ' '.$langs->trans("PageForNoteTab").' : '.($realpathtonote?'':'').$pathtonote.($realpathtonote?'':'').''; - print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '
'; print '


'; @@ -1506,6 +1509,7 @@ elseif (! empty($module)) print $form->textwithpicto($langs->trans("Label"), $langs->trans("YouCanUseTranslationKey")); print ''; print ''.$langs->trans("Type").''; + print ''.$form->textwithpicto($langs->trans("ArrayOfKeyValues"), $langs->trans("ArrayOfKeyValuesDesc")).''; print ''.$langs->trans("NotNull").''; //print ''.$langs->trans("DefaultValue").''; print ''.$langs->trans("DatabaseIndex").''; @@ -1524,18 +1528,19 @@ elseif (! empty($module)) { // Line to add a property print ''; - print ''; - print ''; - print ''; - print ''; + print ''; + print ''; + print ''; + print ''; + print ''; //print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; print ''; print ''; print ''; @@ -1559,6 +1564,7 @@ elseif (! empty($module)) $propname=$propkey; $proplabel=$propval['label']; $proptype=$propval['type']; + $proparrayofkeyval=$propval['arrayofkeyval']; $propnotnull=$propval['notnull']; $propsearchall=$propval['searchall']; //$propdefault=$propval['default']; @@ -1580,6 +1586,12 @@ elseif (! empty($module)) print ''; print $proptype; print ''; + print ''; + if ($proparrayofkeyval) + { + print json_encode($proparrayofkeyval); + } + print ''; print ''; print $propnotnull; print ''; @@ -1652,6 +1664,7 @@ elseif (! empty($module)) print ''; print ''; print ''; + print ''; print ''; $doleditor=new DolEditor('editfilecontent', $content, '', '300', 'Full', 'In', true, false, 'ace', 0, '99%'); diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index 546c404eb49..259db698024 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -78,19 +78,19 @@ class MyObject extends CommonObject * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. */ public $fields=array( - 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'), - 'ref' =>array('type'=>'varchar(64)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'), - 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'index'=>1, 'position'=>20), + 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'), + 'ref' =>array('type'=>'varchar(64)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'), + 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'index'=>1, 'position'=>20), 'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1), 'amount' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>1, 'position'=>40, 'searchall'=>0, 'isameasure'=>1, 'help'=>'Amount'), - 'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>1, 'position'=>1000), - 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500), - 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500), + 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500), + 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500), //'date_valid' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>500), - 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500), + 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500), 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-1, 'notnull'=>-1, 'position'=>500), //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValid', 'enabled'=>1, 'visible'=>-1, 'position'=>500), 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'notnull'=>-1, 'index'=>1, 'position'=>1000), + 'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>1, 'position'=>1000, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Active', -1=>'Cancel')), ); public $rowid; diff --git a/htdocs/modulebuilder/template/myobject_card.php b/htdocs/modulebuilder/template/myobject_card.php index 97662c150da..c4826cf2792 100644 --- a/htdocs/modulebuilder/template/myobject_card.php +++ b/htdocs/modulebuilder/template/myobject_card.php @@ -270,22 +270,30 @@ if ($action == 'create') foreach($object->fields as $key => $val) { if (in_array($key, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat', 'fk_user_modif', 'import_key'))) continue; - print ''; + print ''.$langs->trans($val['label']).''; + print '>'; + print $langs->trans($val['label']); + print ''; print ''; if ($val['type'] == 'text') { print ''; + } + elseif (is_array($val['arrayofkeyval'])) + { + print $form->selectarray($key, $val['arrayofkeyval'], GETPOST($key, 'int')); } else { - print ''; + $cssforinput = 'minwidth100'; + print ''; } print ''; print ''; @@ -329,10 +337,15 @@ if (($id || $ref) && $action == 'edit') print ''; + } + elseif (is_array($val['arrayofkeyval'])) + { + print $form->selectarray($key, $val['arrayofkeyval'], GETPOST($key, 'int')); } else { - print ''; + $cssforinput = 'minwidth100'; + print ''; } print ''; print '';