From fe6dcb22697e38cd46bc8ffa616325d95c25d8dc Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 27 Sep 2022 13:37:38 +0200 Subject: [PATCH] NEW ModuleBuilder can generate code of class from an existing SQL table --- htdocs/langs/en_US/errors.lang | 1 + htdocs/langs/en_US/modulebuilder.lang | 9 +- htdocs/modulebuilder/index.php | 619 ++++++++++-------- .../template/class/myobject.class.php | 10 +- 4 files changed, 350 insertions(+), 289 deletions(-) diff --git a/htdocs/langs/en_US/errors.lang b/htdocs/langs/en_US/errors.lang index e53220e661d..d0e32e480d9 100644 --- a/htdocs/langs/en_US/errors.lang +++ b/htdocs/langs/en_US/errors.lang @@ -294,6 +294,7 @@ ErrorFailedToLoadThirdParty=Failed to find/load thirdparty from id=%s, email=%s, ErrorThisPaymentModeIsNotSepa=This payment mode is not a bank account ErrorStripeCustomerNotFoundCreateFirst=Stripe customer is not set for this thirdparty (or set to a value deleted on Stripe side). Create (or re-attach) it first. ErrorCharPlusNotSupportedByImapForSearch=IMAP search is not able to search into sender or recipient for a string containing the character + +ErrorTableNotFound=Table %s not found # Warnings WarningParamUploadMaxFileSizeHigherThanPostMaxSize=Your PHP parameter upload_max_filesize (%s) is higher than PHP parameter post_max_size (%s). This is not a consistent setup. diff --git a/htdocs/langs/en_US/modulebuilder.lang b/htdocs/langs/en_US/modulebuilder.lang index c7ca26ee149..cab7667484f 100644 --- a/htdocs/langs/en_US/modulebuilder.lang +++ b/htdocs/langs/en_US/modulebuilder.lang @@ -133,9 +133,9 @@ UseSpecificEditorURL = Use a specific editor URL UseSpecificFamily = Use a specific family UseSpecificAuthor = Use a specific author UseSpecificVersion = Use a specific initial version -IncludeRefGeneration=The reference of object must be generated automatically by custom numbering rules +IncludeRefGeneration=The reference of this object must be generated automatically by custom numbering rules IncludeRefGenerationHelp=Check this if you want to include code to manage the generation of the reference automatically using custom numbering rules -IncludeDocGeneration=I want to generate some documents from templates for the object +IncludeDocGeneration=I want the feature to generate some documents (PDF, ODT) from templates for this object IncludeDocGenerationHelp=If you check this, some code will be generated to add a "Generate document" box on the record. ShowOnCombobox=Show value into combobox KeyForTooltip=Key for tooltip @@ -156,4 +156,7 @@ LinkToParentMenu=Parent menu (fk_xxxxmenu) ListOfTabsEntries=List of tab entries TabsDefDesc=Define here the tabs provided by your module TabsDefDescTooltip=The tabs provided by your module/application are defined into the array $this->tabs into the module descriptor file. You can edit manually this file or use the embedded editor. -BadValueForType=Bad value for type %s \ No newline at end of file +BadValueForType=Bad value for type %s +DefinePropertiesFromExistingTable=Define properties from an existing table +DefinePropertiesFromExistingTableDesc=If a table in the database (for the object to create) already exists, you can use it to define the properties of the object. +DefinePropertiesFromExistingTableDesc2=Keep empty if not table exists. The code generator will use different kinds of fields to build an example of table that you can edit later. diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index b41342abc13..1c30d709576 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -894,238 +894,7 @@ if ($dirins && $action == 'confirm_removefile' && !empty($module)) { } } - -// Build the $fields array from SQL table (initfromtablename) -if ($dirins && $action == 'initobject' && $module && GETPOST('createtablearray', 'alpha')) { - $tablename = GETPOST('initfromtablename', 'alpha'); - $_results = $db->DDLDescTable($tablename); - if (empty($_results)) { - setEventMessages($langs->trans("ErrorTableNotFound", $tablename), null, 'errors'); - } else { - /** - * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter[:Sortfield]]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password') - * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)" - * 'label' the translation key. - * 'picto' is code of a picto to show before value in forms - * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM' or 'isModEnabled("multicurrency")' ...) - * 'position' is the sort order of field. - * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0). - * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing) - * 'noteditable' says if field is not editable (1 or 0) - * 'alwayseditable' says if field can be modified also when status is not draft ('1' or '0') - * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created. - * 'index' if we want an index in database. - * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...). - * 'searchall' is 1 if we want to search in this field when making a search from the quick search button. - * 'isameasure' must be set to 1 or 2 if field can be used for measure. Field type must be summable like integer or double(24,8). Use 1 in most cases, or 2 if you don't want to see the column total into list (for example for percentage) - * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'css'=>'minwidth300 maxwidth500 widthcentpercentminusx', 'cssview'=>'wordbreak', 'csslist'=>'tdoverflowmax200' - * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click. - * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record - * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code. - * 'arrayofkeyval' to set a list of values if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel"). Note that type can be 'integer' or 'varchar' - * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1. - * 'comment' is not used. You can store here any text of your choice. It is not used by application. - * 'validate' is 1 if need to validate with $this->validateField() - * 'copytoclipboard' is 1 or 2 to allow to add a picto to copy value into clipboard (1=picto after label, 2=picto after value) - */ - - /*public $fields=array( - 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'), - 'ref' =>array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'), - 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>20), - 'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1, 'css'=>'minwidth200', 'help'=>'Help text', 'alwayseditable'=>'1'), - 'amount' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>1, 'default'=>'null', 'position'=>40, 'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text'), - 'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=>1, 'enabled'=>1, 'position'=>50, 'notnull'=>-1, 'index'=>1, 'searchall'=>1, 'help'=>'LinkToThirdparty'), - 'description' =>array('type'=>'text', 'label'=>'Descrption', 'enabled'=>1, 'visible'=>0, 'position'=>60), - 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>61), - 'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>62), - 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>500), - 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>501), - //'date_valid' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502), - 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>510), - 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511), - //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512), - 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>0, 'position'=>1000), - 'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'default'=>0, 'index'=>1, 'position'=>1000, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Active', -1=>'Cancel')), - );*/ - - $string = 'public $fields=array('."\n"; - $string .= "
"; - $i = 10; - while ($obj = $db->fetch_object($_results)) { - // fieldname - $fieldname = $obj->Field; - // type - $type = $obj->Type; - if ($type == 'int(11)') { - $type = 'integer'; - } - if ($type == 'float') { - $type = 'real'; - } - if (strstr($type, 'tinyint')) { - $type = 'integer'; - } - if ($obj->Field == 'fk_soc') { - $type = 'integer:Societe:societe/class/societe.class.php'; - } - if (preg_match('/^fk_proj/', $obj->Field)) { - $type = 'integer:Project:projet/class/project.class.php:1:fk_statut=1'; - } - if (preg_match('/^fk_prod/', $obj->Field)) { - $type = 'integer:Product:product/class/product.class.php:1'; - } - if ($obj->Field == 'fk_warehouse') { - $type = 'integer:Entrepot:product/stock/class/entrepot.class.php'; - } - if (preg_match('/^(fk_user|fk_commercial)/', $obj->Field)) { - $type = 'integer:User:user/class/user.class.php'; - } - - // notnull - $notnull = ($obj->Null == 'YES' ? 0 : 1); - if ($fieldname == 'fk_user_modif') { - $notnull = -1; - } - // label - $label = preg_replace('/_/', '', ucfirst($fieldname)); - if ($fieldname == 'rowid') { - $label = 'TechnicalID'; - } - if ($fieldname == 'import_key') { - $label = 'ImportId'; - } - if ($fieldname == 'fk_soc') { - $label = 'ThirdParty'; - } - if ($fieldname == 'tms') { - $label = 'DateModification'; - } - if ($fieldname == 'datec') { - $label = 'DateCreation'; - } - if ($fieldname == 'date_valid') { - $label = 'DateValidation'; - } - if ($fieldname == 'datev') { - $label = 'DateValidation'; - } - if ($fieldname == 'note_private') { - $label = 'NotePublic'; - } - if ($fieldname == 'note_public') { - $label = 'NotePrivate'; - } - if ($fieldname == 'fk_user_creat') { - $label = 'UserAuthor'; - } - if ($fieldname == 'fk_user_modif') { - $label = 'UserModif'; - } - if ($fieldname == 'fk_user_valid') { - $label = 'UserValidation'; - } - // visible - $visible = -1; - if ($fieldname == 'entity') { - $visible = -2; - } - if ($fieldname == 'import_key') { - $visible = -2; - } - if ($fieldname == 'fk_user_creat') { - $visible = -2; - } - if ($fieldname == 'fk_user_modif') { - $visible = -2; - } - if (in_array($fieldname, array('ref_ext', 'model_pdf', 'note_public', 'note_private'))) { - $visible = 0; - } - // enabled - $enabled = 1; - // default - $default = ''; - if ($fieldname == 'entity') { - $default = 1; - } - // position - $position = $i; - if (in_array($fieldname, array('status', 'statut', 'fk_status', 'fk_statut'))) { - $position = 500; - } - if ($fieldname == 'import_key') { - $position = 900; - } - // index - $index = 0; - if ($fieldname == 'entity') { - $index = 1; - } - // css, cssview, csslist - $css = ''; - $cssview = ''; - $csslist = ''; - if (preg_match('/^fk_/', $fieldname)) { - $css = 'maxwidth500 widthcentpercentminusxx'; - } - if ($fieldname == 'label') { - $css = 'minwidth300'; - $cssview = 'wordbreak'; - } - if (in_array($fieldname, array('note_public', 'note_private'))) { - $cssview = 'wordbreak'; - } - - // type - $picto = $obj->Picto; - if ($obj->Field == 'fk_soc') { - $picto = 'company'; - } - if (preg_match('/^fk_proj/', $obj->Field)) { - $picto = 'project'; - } - - // Build the property string - $string .= "'".$obj->Field."'=>array('type'=>'".$type."', 'label'=>'".$label."',"; - if ($default != '') { - $string .= " 'default'=>".$default.","; - } - $string .= " 'enabled'=>".$enabled.","; - $string .= " 'visible'=>".$visible; - if ($notnull) { - $string .= ", 'notnull'=>".$notnull; - } - if ($fieldname == 'ref' || $fieldname == 'code') { - $string .= ", 'showoncombobox'=>1"; - } - $string .= ", 'position'=>".$position; - if ($index) { - $string .= ", 'index'=>".$index; - } - if ($picto) { - $string .= ", 'picto'=>'".$picto."'"; - } - if ($css) { - $string .= ", 'css'=>".$css; - } - if ($cssview) { - $string .= ", 'cssview'=>".$cssview; - } - if ($csslist) { - $string .= ", 'csslist'=>".$csslist; - } - $string .= "),\n"; - $string .= "
"; - $i += 5; - } - $string .= ');'."\n"; - $string .= "
"; - print $string; - exit; - } -} - +// Init an object if ($dirins && $action == 'initobject' && $module && $objectname) { $objectname = ucfirst($objectname); @@ -1169,6 +938,247 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { } } + // If we must reuse a table for properties, define $stringforproperties + $stringforproperties = ''; + $tablename = GETPOST('initfromtablename', 'alpha'); + if ($tablename) { + $_results = $db->DDLDescTable($tablename); + if (empty($_results)) { + $error++; + $langs->load("errors"); + setEventMessages($langs->trans("ErrorTableNotFound", $tablename), null, 'errors'); + } else { + /** + * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter[:Sortfield]]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password') + * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)" + * 'label' the translation key. + * 'picto' is code of a picto to show before value in forms + * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM' or 'isModEnabled("multicurrency")' ...) + * 'position' is the sort order of field. + * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0). + * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing) + * 'noteditable' says if field is not editable (1 or 0) + * 'alwayseditable' says if field can be modified also when status is not draft ('1' or '0') + * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created. + * 'index' if we want an index in database. + * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...). + * 'searchall' is 1 if we want to search in this field when making a search from the quick search button. + * 'isameasure' must be set to 1 or 2 if field can be used for measure. Field type must be summable like integer or double(24,8). Use 1 in most cases, or 2 if you don't want to see the column total into list (for example for percentage) + * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'css'=>'minwidth300 maxwidth500 widthcentpercentminusx', 'cssview'=>'wordbreak', 'csslist'=>'tdoverflowmax200' + * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click. + * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record + * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code. + * 'arrayofkeyval' to set a list of values if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel"). Note that type can be 'integer' or 'varchar' + * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1. + * 'comment' is not used. You can store here any text of your choice. It is not used by application. + * 'validate' is 1 if need to validate with $this->validateField() + * 'copytoclipboard' is 1 or 2 to allow to add a picto to copy value into clipboard (1=picto after label, 2=picto after value) + */ + + /*public $fields=array( + 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'), + 'ref' =>array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'), + 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>20), + 'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1, 'css'=>'minwidth200', 'help'=>'Help text', 'alwayseditable'=>'1'), + 'amount' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>1, 'default'=>'null', 'position'=>40, 'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text'), + 'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'visible'=>1, 'enabled'=>1, 'position'=>50, 'notnull'=>-1, 'index'=>1, 'searchall'=>1, 'help'=>'LinkToThirdparty'), + 'description' =>array('type'=>'text', 'label'=>'Descrption', 'enabled'=>1, 'visible'=>0, 'position'=>60), + 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>61), + 'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>62), + 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>500), + 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>501), + //'date_valid' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502), + 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>510), + 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511), + //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512), + 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>0, 'position'=>1000), + 'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'default'=>0, 'index'=>1, 'position'=>1000, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Active', -1=>'Cancel')), + );*/ + + $stringforproperties = '// BEGIN MODULEBUILDER PROPERTIES'."\n"; + $stringforproperties .= 'public $fields=array('."\n"; + $i = 10; + while ($obj = $db->fetch_object($_results)) { + // fieldname + $fieldname = $obj->Field; + // type + $type = $obj->Type; + if ($type == 'int(11)') { + $type = 'integer'; + } + if ($type == 'float') { + $type = 'real'; + } + if (strstr($type, 'tinyint')) { + $type = 'integer'; + } + if ($obj->Field == 'fk_soc') { + $type = 'integer:Societe:societe/class/societe.class.php'; + } + if (preg_match('/^fk_proj/', $obj->Field)) { + $type = 'integer:Project:projet/class/project.class.php:1:fk_statut=1'; + } + if (preg_match('/^fk_prod/', $obj->Field)) { + $type = 'integer:Product:product/class/product.class.php:1'; + } + if ($obj->Field == 'fk_warehouse') { + $type = 'integer:Entrepot:product/stock/class/entrepot.class.php'; + } + if (preg_match('/^(fk_user|fk_commercial)/', $obj->Field)) { + $type = 'integer:User:user/class/user.class.php'; + } + + // notnull + $notnull = ($obj->Null == 'YES' ? 0 : 1); + if ($fieldname == 'fk_user_modif') { + $notnull = -1; + } + // label + $label = preg_replace('/_/', '', ucfirst($fieldname)); + if ($fieldname == 'rowid') { + $label = 'TechnicalID'; + } + if ($fieldname == 'import_key') { + $label = 'ImportId'; + } + if ($fieldname == 'fk_soc') { + $label = 'ThirdParty'; + } + if ($fieldname == 'tms') { + $label = 'DateModification'; + } + if ($fieldname == 'datec') { + $label = 'DateCreation'; + } + if ($fieldname == 'date_valid') { + $label = 'DateValidation'; + } + if ($fieldname == 'datev') { + $label = 'DateValidation'; + } + if ($fieldname == 'note_private') { + $label = 'NotePublic'; + } + if ($fieldname == 'note_public') { + $label = 'NotePrivate'; + } + if ($fieldname == 'fk_user_creat') { + $label = 'UserAuthor'; + } + if ($fieldname == 'fk_user_modif') { + $label = 'UserModif'; + } + if ($fieldname == 'fk_user_valid') { + $label = 'UserValidation'; + } + // visible + $visible = -1; + if ($fieldname == 'entity') { + $visible = -2; + } + if ($fieldname == 'import_key') { + $visible = -2; + } + if ($fieldname == 'fk_user_creat') { + $visible = -2; + } + if ($fieldname == 'fk_user_modif') { + $visible = -2; + } + if (in_array($fieldname, array('ref_ext', 'model_pdf', 'note_public', 'note_private'))) { + $visible = 0; + } + // enabled + $enabled = 1; + // default + $default = ''; + if ($fieldname == 'entity') { + $default = 1; + } + // position + $position = $i; + if (in_array($fieldname, array('status', 'statut', 'fk_status', 'fk_statut'))) { + $position = 500; + } + if ($fieldname == 'import_key') { + $position = 900; + } + // $alwayseditable + if ($fieldname == 'label') { + $alwayseditable = 1; + } + // index + $index = 0; + if ($fieldname == 'entity') { + $index = 1; + } + // css, cssview, csslist + $css = ''; + $cssview = ''; + $csslist = ''; + if (preg_match('/^fk_/', $fieldname)) { + $css = 'maxwidth500 widthcentpercentminusxx'; + } + if ($fieldname == 'label') { + $css = 'minwidth300'; + $cssview = 'wordbreak'; + } + if (in_array($fieldname, array('note_public', 'note_private'))) { + $cssview = 'wordbreak'; + } + if (in_array($fieldname, array('ref', 'label')) || preg_match('/integer:/', $type)) { + $csslist = 'tdoverflowmax150'; + } + + // type + $picto = $obj->Picto; + if ($obj->Field == 'fk_soc') { + $picto = 'company'; + } + if (preg_match('/^fk_proj/', $obj->Field)) { + $picto = 'project'; + } + + // Build the property string + $stringforproperties .= "'".$obj->Field."'=>array('type'=>'".$type."', 'label'=>'".$label."',"; + if ($default != '') { + $stringforproperties .= " 'default'=>".$default.","; + } + $stringforproperties .= " 'enabled'=>".$enabled.","; + $stringforproperties .= " 'visible'=>".$visible; + if ($notnull) { + $stringforproperties .= ", 'notnull'=>".$notnull; + } + if ($alwayseditable) { + $stringforproperties .= ", 'alwayseditable'=>1"; + } + if ($fieldname == 'ref' || $fieldname == 'code') { + $stringforproperties .= ", 'showoncombobox'=>1"; + } + $stringforproperties .= ", 'position'=>".$position; + if ($index) { + $stringforproperties .= ", 'index'=>".$index; + } + if ($picto) { + $stringforproperties .= ", 'picto'=>'".$picto."'"; + } + if ($css) { + $stringforproperties .= ", 'css'=>'".$css."'"; + } + if ($cssview) { + $stringforproperties .= ", 'cssview'=>'".$cssview."'"; + } + if ($csslist) { + $stringforproperties .= ", 'csslist'=>'".$csslist."'"; + } + $stringforproperties .= "),\n"; + $i += 5; + } + $stringforproperties .= ');'."\n"; + $stringforproperties .= '// END MODULEBUILDER PROPERTIES'."\n"; + } + } + if (!$error) { // Copy some files $filetogenerate = array( @@ -1209,20 +1219,33 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { ); } - foreach ($filetogenerate as $srcfile => $destfile) { - $result = dol_copy($srcdir.'/'.$srcfile, $destdir.'/'.$destfile, $newmask, 0); - if ($result <= 0) { - if ($result < 0) { - $error++; - $langs->load("errors"); - setEventMessages($langs->trans("ErrorFailToCopyFile", $srcdir.'/'.$srcfile, $destdir.'/'.$destfile), null, 'errors'); - } else { - // $result == 0 - setEventMessages($langs->trans("FileAlreadyExists", $destfile), null, 'warnings'); + + if (!$error) { + foreach ($filetogenerate as $srcfile => $destfile) { + $result = dol_copy($srcdir.'/'.$srcfile, $destdir.'/'.$destfile, $newmask, 0); + if ($result <= 0) { + if ($result < 0) { + $error++; + $langs->load("errors"); + setEventMessages($langs->trans("ErrorFailToCopyFile", $srcdir.'/'.$srcfile, $destdir.'/'.$destfile), null, 'errors'); + } else { + // $result == 0 + setEventMessages($langs->trans("FileAlreadyExists", $destfile), null, 'warnings'); + } } } } + // Replace property section with $stringforproperties + if (!$error && $stringforproperties) { + //var_dump($stringforproperties);exit; + $arrayreplacement = array( + '/\/\/ BEGIN MODULEBUILDER PROPERTIES.*\/\/ END MODULEBUILDER PROPERTIES/ims' => $stringforproperties + ); + + dolReplaceInFile($destdir.'/class/'.strtolower($objectname).'.class.php', $arrayreplacement, '', 0, 0, 1); + } + // Edit the class 'class/'.strtolower($objectname).'.class.php' if (GETPOST('includerefgeneration', 'aZ09')) { // Replace 'visible'=>1, 'noteditable'=>0, 'default'=>'' @@ -1405,6 +1428,8 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { if (!$error) { setEventMessages($langs->trans('FilesForObjectInitialized', $objectname), null); $tabobj = $objectname; + } else { + $tabobj = 'newobject'; } } @@ -1489,26 +1514,26 @@ if ($dirins && $action == 'addproperty' && empty($cancel) && !empty($module) && if (!$error && !GETPOST('regenerateclasssql')&& !GETPOST('regeneratemissing')) { $addfieldentry = array( - 'name'=>GETPOST('propname', 'aZ09'), - 'label'=>GETPOST('proplabel', 'alpha'), - 'type'=>GETPOST('proptype', 'alpha'), - 'arrayofkeyval'=>GETPOST('proparrayofkeyval', 'restricthtml'), // 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', 'alpha'), - 'css'=>GETPOST('propcss', 'alpha'), // Can be 'maxwidth500 widthcentpercentminusxx' for example - 'cssview'=>GETPOST('propcssview', 'alpha'), - 'csslist'=>GETPOST('propcsslist', 'alpha'), - 'default'=>GETPOST('propdefault', 'restricthtml'), - 'noteditable'=>intval(GETPOST('propnoteditable', 'int')), - 'alwayseditable'=>intval(GETPOST('propalwayseditable', 'int')), - 'validate' => GETPOST('propvalidate', 'int') + 'name'=>GETPOST('propname', 'aZ09'), + 'label'=>GETPOST('proplabel', 'alpha'), + 'type'=>GETPOST('proptype', 'alpha'), + 'arrayofkeyval'=>GETPOST('proparrayofkeyval', 'restricthtml'), // 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', 'alpha'), + 'css'=>GETPOST('propcss', 'alpha'), // Can be 'maxwidth500 widthcentpercentminusxx' for example + 'cssview'=>GETPOST('propcssview', 'alpha'), + 'csslist'=>GETPOST('propcsslist', 'alpha'), + 'default'=>GETPOST('propdefault', 'restricthtml'), + 'noteditable'=>intval(GETPOST('propnoteditable', 'int')), + 'alwayseditable'=>intval(GETPOST('propalwayseditable', 'int')), + 'validate' => GETPOST('propvalidate', 'int') ); @@ -2285,6 +2310,8 @@ if ($module == 'initmodule') { $head2[$h][2] = 'buildpackage'; $h++; + $MAXTABFOROBJECT = 15; + print ''; // Note module is inside $dirread @@ -2296,7 +2323,7 @@ if ($module == 'initmodule') { $pathtochangelog = $modulelowercase.'/ChangeLog.md'; if ($action != 'editfile' || empty($file)) { - print dol_get_fiche_head($head2, $tab, '', -1, '', 0, '', '', 0, 'formodulesuffix'); // Description - level 2 + print dol_get_fiche_head($head2, $tab, '', -1, '', 0, '', '', $MAXTABFOROBJECT, 'formodulesuffix'); // Description - level 2 print ''.$langs->trans("ModuleBuilderDesc".$tab).''; $infoonmodulepath = ''; @@ -2456,7 +2483,7 @@ if ($module == 'initmodule') { print ''; } } else { - print dol_get_fiche_head($head2, $tab, '', -1, '', 0, '', '', 0, 'formodulesuffix'); // Level 2 + print dol_get_fiche_head($head2, $tab, '', -1, '', 0, '', '', $MAXTABFOROBJECT, 'formodulesuffix'); // Level 2 } if ($tab == 'languages') { @@ -2603,12 +2630,38 @@ if ($module == 'initmodule') { print ''.$langs->trans("EnterNameOfObjectDesc").'

'; - print '
'; - print '
'; + print '
'; + + print '
'; + print ''.$langs->trans("ObjectKey").'   '; + print '
'; + print '
'; + print '
'; + + print '
'; + print ''.$langs->trans("Picto").'   '; + print '
'; + print ''; + print $form->textwithpicto('', $langs->trans("Example").': fa-generic, fa-globe, ... any font awesome code.
Advanced syntax is fa-fakey[_faprefix[_facolor[_fasize]]]'); + print '
'; + + print '
'; + print ''.$langs->trans("DefinePropertiesFromExistingTable").'   '; + print '
'; + print ''; + print $form->textwithpicto('', $langs->trans("DefinePropertiesFromExistingTableDesc").'
'.$langs->trans("DefinePropertiesFromExistingTableDesc2")); + print '
'; + + print '
'; + + print '
'; + print '
'; print '
'; - print ''; + print '
'; + print ''; print '
'; print '
'; + /* print '
'; print ''.$langs->trans("or").''; print '
'; @@ -2618,6 +2671,7 @@ if ($module == 'initmodule') { print ''; print ''; print '
'; + */ print ''; } elseif ($tabobj == 'deleteobject') { @@ -2679,7 +2733,7 @@ if ($module == 'initmodule') { } if (class_exists($tabobj)) { try { - $tmpobjet = @new $tabobj($db); + $tmpobject = @new $tabobj($db); } catch (Exception $e) { dol_syslog('Failed to load Constructor of class: '.$e->getMessage(), LOG_WARNING); } @@ -2752,13 +2806,22 @@ if ($module == 'initmodule') { $urlofcard = dol_buildpath('/'.$pathtocard, 1); - - - + print ''; print '
'; // Main DAO class file print ' '.$langs->trans("ClassFile").' : '.(dol_is_file($realpathtoclass) ? '' : '').preg_replace('/^'.strtolower($module).'\//', '', $pathtoclass).(dol_is_file($realpathtoclass) ? '' : '').''; print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print '
'; + // Image + if (dol_is_file($realpathtopicto)) { + print ' '.$langs->trans("Image").' : '.(dol_is_file($realpathtopicto) ? '' : '').preg_replace('/^'.strtolower($module).'\//', '', $pathtopicto).(dol_is_file($realpathtopicto) ? '' : '').''; + //print ' '.img_picto($langs->trans("Edit"), 'edit').''; + print '
'; + } elseif (!empty($tmpobject)) { + print ' '.$langs->trans("Image").' : '.img_picto('', $tmpobject->picto, 'class="pictofixedwidth"'); + print '
'; + } + // API file print '
'; print ' '.$langs->trans("ApiClassFile").' : '.(dol_is_file($realpathtoapi) ? '' : '').preg_replace('/^'.strtolower($module).'\//', '', $pathtoapi).(dol_is_file($realpathtoapi)?'':'').''; @@ -2795,12 +2858,6 @@ if ($module == 'initmodule') { print ' '.$langs->trans("PageForObjLib").' : '.(dol_is_file($realpathtoobjlib) ? '' : '').preg_replace('/^'.strtolower($module).'\//', '', $pathtoobjlib).(dol_is_file($realpathtoobjlib) ? '' : '').''; print ' '.img_picto($langs->trans("Edit"), 'edit').''; print '
'; - // Image - if (dol_is_file($realpathtopicto)) { - print ' '.$langs->trans("Image").' : '.(dol_is_file($realpathtopicto) ? '' : '').preg_replace('/^'.strtolower($module).'\//', '', $pathtopicto).(dol_is_file($realpathtopicto) ? '' : '').''; - //print ' '.img_picto($langs->trans("Edit"), 'edit').''; - print '
'; - } print '
'; print ' '.$langs->trans("SqlFile").' : '.(dol_is_file($realpathtosql) ? '' : '').preg_replace('/^'.strtolower($module).'\//', '', $pathtosql).(dol_is_file($realpathtosql) ? '' : '').''; @@ -2888,7 +2945,7 @@ if ($module == 'initmodule') { print '


'; - if (!empty($tmpobjet)) { + if (!empty($tmpobject)) { $reflector = new ReflectionClass($tabobj); $reflectorproperties = $reflector->getProperties(); // Can also use get_object_vars $reflectorpropdefault = $reflector->getDefaultProperties(); // Can also use get_object_vars @@ -2942,9 +2999,9 @@ if ($module == 'initmodule') { print ''; print ''; - // We must use $reflectorpropdefault['fields'] to get list of fields because $tmpobjet->fields may have been + // We must use $reflectorpropdefault['fields'] to get list of fields because $tmpobject->fields may have been // modified during the constructor and we want value into head of class before constructor is called. - //$properties = dol_sort_array($tmpobjet->fields, 'position'); + //$properties = dol_sort_array($tmpobject->fields, 'position'); $properties = dol_sort_array($reflectorpropdefault['fields'], 'position'); if (!empty($properties)) { @@ -2984,7 +3041,7 @@ if ($module == 'initmodule') { { $propname=$propval->getName(); $comment=$propval->getDocComment(); - $type=gettype($tmpobjet->$propname); + $type=gettype($tmpobject->$propname); $default=$propdefault[$propname]; // Discard generic properties if (in_array($propname, array('element', 'childtables', 'table_element', 'table_element_line', 'class_element_line', 'ismultientitymanaged'))) continue; diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index 430f004eecb..b30391becc7 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -116,19 +116,19 @@ class MyObject extends CommonObject 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id', 'css'=>'left'), 'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>10), 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'noteditable'=>0, 'default'=>'', 'notnull'=> 1, 'showoncombobox'=>1, 'index'=>1, 'position'=>20, 'searchall'=>1, 'comment'=>'Reference of object', 'validate'=>1), - 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>'Help text', 'showoncombobox'=>2, 'validate'=>1), + 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>'Help text', 'showoncombobox'=>2, 'validate'=>1, 'alwayseditable'=>1), 'amount' => array('type'=>'price', 'label'=>'Amount', 'enabled'=>1, 'visible'=>1, 'default'=>'null', 'position'=>40, 'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text for amount', 'validate'=>1), 'qty' => array('type'=>'real', 'label'=>'Qty', 'enabled'=>1, 'visible'=>1, 'default'=>'0', 'position'=>45, 'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text for quantity', 'css'=>'maxwidth75imp', 'validate'=>1), - 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php:1:status=1 AND entity IN (__SHARED_ENTITIES__)', 'picto'=>'company', 'label'=>'ThirdParty', 'visible'=> 1, 'enabled'=>'$conf->societe->enabled', 'position'=>50, 'notnull'=>-1, 'index'=>1, 'help'=>'OrganizationEventLinkToThirdParty', 'validate'=>1, 'css'=>'maxwidth500 widthcentpercentminusxx'), - 'fk_project' => array('type'=>'integer:Project:projet/class/project.class.php:1', 'label'=>'Project', 'picto'=>'project', 'enabled'=>'$conf->project->enabled', 'visible'=>-1, 'position'=>52, 'notnull'=>-1, 'index'=>1, 'validate'=>1, 'css'=>'maxwidth500 widthcentpercentminusxx'), + 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php:1:status=1 AND entity IN (__SHARED_ENTITIES__)', 'picto'=>'company', 'label'=>'ThirdParty', 'visible'=> 1, 'enabled'=>'$conf->societe->enabled', 'position'=>50, 'notnull'=>-1, 'index'=>1, 'help'=>'OrganizationEventLinkToThirdParty', 'validate'=>1, 'css'=>'maxwidth500 widthcentpercentminusxx', 'csslist'=>'tdoverflowmax150'), + 'fk_project' => array('type'=>'integer:Project:projet/class/project.class.php:1', 'label'=>'Project', 'picto'=>'project', 'enabled'=>'$conf->project->enabled', 'visible'=>-1, 'position'=>52, 'notnull'=>-1, 'index'=>1, 'validate'=>1, 'css'=>'maxwidth500 widthcentpercentminusxx', 'csslist'=>'tdoverflowmax150'), 'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>3, 'position'=>60, 'validate'=>1), 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>61, 'validate'=>1, 'cssview'=>'wordbreak'), 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>62, 'validate'=>1, 'cssview'=>'wordbreak'), 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>500), 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 0, 'position'=>501), //'date_validation ' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502), - 'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'picto'=>'user', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>510, 'foreignkey'=>'user.rowid'), - 'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'picto'=>'user', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511), + 'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'picto'=>'user', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>510, 'foreignkey'=>'user.rowid', 'csslist'=>'tdoverflowmax150'), + 'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'picto'=>'user', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511, 'csslist'=>'tdoverflowmax150'), //'fk_user_valid' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512), 'last_main_doc' => array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>1, 'visible'=>0, 'notnull'=>0, 'position'=>600), 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>0, 'position'=>1000),