From bfd1fa3984234f5bff98c467b9910f47843ba4d6 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 25 Jul 2017 02:56:19 +0200 Subject: [PATCH] Enhance modulebuilder --- htdocs/core/actions_massactions.inc.php | 2 +- htdocs/core/class/commonobject.class.php | 6 +- htdocs/langs/en_US/modulebuilder.lang | 2 +- htdocs/modulebuilder/index.php | 11 +-- .../template/class/myobject.class.php | 49 +++++++++++++- .../template/lib/mymodule.lib.php | 6 +- .../template/lib/myobject.lib.php | 67 +++++++++++++++++++ .../modulebuilder/template/myobject_card.php | 30 ++++----- 8 files changed, 144 insertions(+), 29 deletions(-) create mode 100644 htdocs/modulebuilder/template/lib/myobject.lib.php diff --git a/htdocs/core/actions_massactions.inc.php b/htdocs/core/actions_massactions.inc.php index be6f13a22c7..f4a55352f96 100644 --- a/htdocs/core/actions_massactions.inc.php +++ b/htdocs/core/actions_massactions.inc.php @@ -594,7 +594,7 @@ if (! $error && $massaction == 'delete' && $permtodelete) $nbok = 0; foreach($toselect as $toselectid) { - $result=$objecttmp->fetch($toselectid); + $result=$objecttmp->fetch($toselectid); if ($result > 0) { if (in_array($objecttmp->element, array('societe','member'))) $result = $objecttmp->delete($objecttmp->id, $user, 1); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 9e4b9b0bf18..80eb7d31cfb 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -5011,7 +5011,7 @@ abstract class CommonObject if (!$notrigger) { // Call triggers - $result=$this->call_trigger(strtoupper(get_class(self)).'_CREATE',$user); + $result=$this->call_trigger(strtoupper(get_class($this)).'_CREATE',$user); if ($result < 0) { $error++; } // End call triggers } @@ -5173,7 +5173,7 @@ abstract class CommonObject if (! $error && ! $notrigger) { // Call triggers - $result=$this->call_trigger(strtoupper(get_class(self)).'_MODIFY',$user); + $result=$this->call_trigger(strtoupper(get_class($this)).'_MODIFY',$user); if ($result < 0) { $error++; } //Do also here what you must do to rollback action if trigger fail // End call triggers } @@ -5204,7 +5204,7 @@ abstract class CommonObject if (! $error) { if (! $notrigger) { // Call triggers - $result=$this->call_trigger(strtoupper(get_class(self)).'_DELETE', $user); + $result=$this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user); if ($result < 0) { $error++; } // Do also here what you must do to rollback action if trigger fail // End call triggers } diff --git a/htdocs/langs/en_US/modulebuilder.lang b/htdocs/langs/en_US/modulebuilder.lang index 4fc9b54bfa3..9b3ad61a310 100644 --- a/htdocs/langs/en_US/modulebuilder.lang +++ b/htdocs/langs/en_US/modulebuilder.lang @@ -31,7 +31,7 @@ DescriptionLong=Long description EditorName=Name of editor EditorUrl=URL of editor DescriptorFile=Descriptor file of module -ClassFile=File for PHP class +ClassFile=File for PHP DAO CRUD class ApiClassFile=File for PHP API class PageForList=PHP page for list of record PageForCreateEditView=PHP page to create/edit/view a record diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index f6a89f43444..5c5a900f44c 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -109,6 +109,7 @@ if ($dirins && $action == 'initmodule' && $modulename) // Delete some files dol_delete_file($destdir.'/myobject_card.php'); dol_delete_file($destdir.'/myobject_list.php'); + dol_delete_file($destdir.'/lib/myobject.lib.php'); dol_delete_file($destdir.'/test/phpunit/MyObjectTest.php'); dol_delete_file($destdir.'/sql/llx_myobject.sql'); dol_delete_file($destdir.'/sql/llx_myobject_extrafields.sql'); @@ -132,7 +133,7 @@ if ($dirins && $action == 'initmodule' && $modulename) 'MyModule'=>$modulename, 'MYMODULE'=>strtoupper($modulename), 'My module'=>$modulename, - 'htdocs/modulebuilder/template/'=>'', + 'htdocs/modulebuilder/template/'=>strtolower($modulename), '---Put here your own copyright and developer email---'=>dol_print_date($now,'%Y').' '.$user->getFullName($langs).($user->email?' <'.$user->email.'>':'') ); @@ -189,7 +190,8 @@ if ($dirins && $action == 'initobject' && $module && $objectname) $filetogenerate = array( 'myobject_card.php'=>strtolower($objectname).'_card.php', 'myobject_list.php'=>strtolower($objectname).'_list.php', - 'test/phpunit/MyObjectTest.php'=>'test/phpunit/'.$objectname.'Test.php', + 'lib/myobject.lib.php'=>'lib/'.strtolower($objectname).'.lib.php', + 'test/phpunit/MyObjectTest.php'=>'test/phpunit/'.$objectname.'Test.php', 'sql/llx_myobject.sql'=>'sql/llx_'.strtolower($objectname).'.sql', 'sql/llx_myobject_extrafields.sql'=>'sql/llx_'.strtolower($objectname).'_extrafields.sql', 'sql/llx_myobject.key.sql'=>'sql/llx_'.strtolower($objectname).'.key.sql', @@ -276,7 +278,7 @@ if ($dirins && $action == 'initobject' && $module && $objectname) 'MyModule'=>$module, 'MYMODULE'=>strtoupper($module), 'My module'=>$module, - 'htdocs/modulebuilder/template/'=>'', + 'htdocs/modulebuilder/template/'=>strtolower($modulename), 'myobject'=>strtolower($objectname), 'MyObject'=>$objectname ); @@ -378,7 +380,8 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) $filetogenerate = array( 'myobject_card.php'=>strtolower($objectname).'_card.php', 'myobject_list.php'=>strtolower($objectname).'_list.php', - 'test/phpunit/MyObjectTest.php'=>'test/phpunit/'.$objectname.'Test.php', + 'lib/myobject.lib.php'=>'lib/'.strtolower($objectname).'.lib.php', + 'test/phpunit/MyObjectTest.php'=>'test/phpunit/'.$objectname.'Test.php', 'sql/llx_myobject.sql'=>'sql/llx_'.strtolower($objectname).'.sql', 'sql/llx_myobject_extrafields.sql'=>'sql/llx_'.strtolower($objectname).'_extrafields.sql', 'sql/llx_myobject.key.sql'=>'sql/llx_'.strtolower($objectname).'.key.sql', diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index 7b3d675e9be..756db6e8e9c 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -128,6 +128,53 @@ class MyObject extends CommonObject $this->db = $db; } + /** + * Create object into database + * + * @param User $user User that creates + * @param bool $notrigger false=launch triggers after, true=disable triggers + * @return int <0 if KO, Id of created object if OK + */ + public function create(User $user, $notrigger = false) + { + return $this->createCommon($user, $notrigger); + } + + /** + * Load object in memory from the database + * + * @param int $id Id object + * @param string $ref Ref + * @return int <0 if KO, 0 if not found, >0 if OK + */ + public function fetch($id, $ref = null) + { + return $this->fetchCommon($id, $ref); + } + + /** + * Update object into database + * + * @param User $user User that modifies + * @param bool $notrigger false=launch triggers after, true=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function update(User $user, $notrigger = false) + { + return $this->updateCommon($user, $notrigger); + } + + /** + * Delete object in database + * + * @param User $user User that deletes + * @param bool $notrigger false=launch triggers after, true=disable triggers + * @return int <0 if KO, >0 if OK + */ + public function delete(User $user, $notrigger = false) + { + return $this->deleteCommon($user, $trigger); + } /** * Return a link to the object card (with optionaly the picto) @@ -153,7 +200,7 @@ class MyObject extends CommonObject $label.= '
'; $label.= '' . $langs->trans('Ref') . ': ' . $this->ref; - $url = $url = dol_buildpath('/mymodule/m_card.php',1).'?id='.$this->id; + $url = $url = dol_buildpath('/mymodule/myobject_card.php',1).'?id='.$this->id; $linkclose=''; if (empty($notooltip)) diff --git a/htdocs/modulebuilder/template/lib/mymodule.lib.php b/htdocs/modulebuilder/template/lib/mymodule.lib.php index 3c88bcdea67..cec23bd3c11 100644 --- a/htdocs/modulebuilder/template/lib/mymodule.lib.php +++ b/htdocs/modulebuilder/template/lib/mymodule.lib.php @@ -16,11 +16,9 @@ */ /** - * \file lib/mymodule.lib.php + * \file htdocs/modulebuilder/template/lib/mymodule.lib.php * \ingroup mymodule - * \brief Example module library. - * - * Put detailed description here. + * \brief Library files with common functions for MyModule */ /** diff --git a/htdocs/modulebuilder/template/lib/myobject.lib.php b/htdocs/modulebuilder/template/lib/myobject.lib.php new file mode 100644 index 00000000000..d1822793ede --- /dev/null +++ b/htdocs/modulebuilder/template/lib/myobject.lib.php @@ -0,0 +1,67 @@ +. + */ + +/** + * \file htdocs/modulebuilder/template/lib/myobject.lib.php + * \ingroup mymodule + * \brief Library files with common functions for MyObject + */ + +/** + * Prepare array of tabs for MyObject + * + * @param MyObject $object MyObject + * @return array Array of tabs + */ +function myobjectPrepareHead($object) +{ + global $langs, $conf; + + $langs->load("mymodule@mymodule"); + + $h = 0; + $head = array(); + + $head[$h][0] = dol_buildpath("/mymodule/myobject_card.php", 1).'?id='.$object->id; + $head[$h][1] = $langs->trans("Card"); + $head[$h][2] = 'card'; + $h++; + $head[$h][0] = dol_buildpath("/mymodule/myobject_note.php", 1).'?id='.$object->id; + $head[$h][1] = $langs->trans("Notes"); + $head[$h][2] = 'note'; + $h++; + $head[$h][0] = dol_buildpath("/mymodule/myobject_document.php", 1).'?id='.$object->id; + $head[$h][1] = $langs->trans("Documents"); + $head[$h][2] = 'document'; + $h++; + $head[$h][0] = dol_buildpath("/mymodule/myobject_agenda.php", 1).'?id='.$object->id; + $head[$h][1] = $langs->trans("Events"); + $head[$h][2] = 'agenda'; + $h++; + + // Show more tabs from modules + // Entries must be declared in modules descriptor with line + //$this->tabs = array( + // 'entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__' + //); // to add new tab + //$this->tabs = array( + // 'entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__' + //); // to remove a tab + complete_head_from_modules($conf, $langs, $object, $head, $h, 'myobject@mymodule'); + + return $head; +} diff --git a/htdocs/modulebuilder/template/myobject_card.php b/htdocs/modulebuilder/template/myobject_card.php index e8cc7e33735..4151553f2bd 100644 --- a/htdocs/modulebuilder/template/myobject_card.php +++ b/htdocs/modulebuilder/template/myobject_card.php @@ -52,7 +52,9 @@ if (! $res && file_exists("../../../main.inc.php")) $res=@include("../../../main if (! $res) die("Include of main fails"); include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'); +include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'); dol_include_once('/mymodule/class/myobject.class.php'); +dol_include_once('/mymodule/lib/myobject.lib.php'); // Load traductions files requiredby by page $langs->loadLangs(array("mymodule","other")); @@ -226,6 +228,7 @@ if (empty($reshook)) */ $form=new Form($db); +$formfile=new FormFile($db); llxHeader('','MyObject',''); @@ -311,14 +314,14 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea { $res = $object->fetch_optionals($object->id, $extralabels); - $head = mymodule_prepare_head($object); - dol_fiche_head($head, 'order', $langs->trans("CustomerOrder"), -1, 'order'); + $head = myobjectPrepareHead($object); + dol_fiche_head($head, 'card', $langs->trans("MyObject"), -1, 'myobject@mymodule'); $formconfirm = ''; // Confirmation to delete if ($action == 'delete') { - $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteOrder'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete', '', 0, 1); + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteMyObject'), $langs->trans('ConfirmDeleteMyObject'), 'confirm_delete', '', 0, 1); } // Confirmation of action xxxx @@ -346,12 +349,9 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea print $formconfirm; - // Object card // ------------------------------------------------------------ - - $linkback = '' . $langs->trans("BackToList") . ''; - + $linkback = '' . $langs->trans("BackToList") . ''; $morehtmlref='
'; /* @@ -477,7 +477,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Show links to link elements - $linktoelem = $form->showLinkToObjectBlock($object, null, array('order')); + $linktoelem = $form->showLinkToObjectBlock($object, null, array('myobject')); $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem); @@ -486,7 +486,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // List of actions on element include_once DOL_DOCUMENT_ROOT . '/core/class/html.formactions.class.php'; $formactions = new FormActions($db); - $somethingshown = $formactions->showactions($object, 'order', $socid); + $somethingshown = $formactions->showactions($object, 'myobject', $socid); print '
'; } @@ -534,7 +534,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea print '
'; print '
'; print '
'; - print load_fiche_titre($langs->trans('SendOrderByMail')); + print load_fiche_titre($langs->trans('SendMyObjectByMail')); dol_fiche_head(''); @@ -562,9 +562,9 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea $formmail->withtocc = $liste; $formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC; if (empty($object->ref_client)) { - $formmail->withtopic = $outputlangs->trans('SendOrderRef', '__ORDERREF__'); + $formmail->withtopic = $outputlangs->trans('SendMyObjectRef', '__REF__'); } else if (! empty($object->ref_client)) { - $formmail->withtopic = $outputlangs->trans('SendOrderRef', '__ORDERREF__ (__REFCLIENT__)'); + $formmail->withtopic = $outputlangs->trans('SendMyObjectRef', '__REF__ (__REFCLIENT__)'); } $formmail->withfile = 2; $formmail->withbody = 1; @@ -572,7 +572,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea $formmail->withcancel = 1; // Tableau des substitutions $formmail->setSubstitFromObject($object); - $formmail->substit ['__ORDERREF__'] = $object->ref; + $formmail->substit ['__REF__'] = $object->ref; $custcontact = ''; $contactarr = array(); @@ -596,9 +596,9 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Tableau des parametres complementaires $formmail->param['action'] = 'send'; - $formmail->param['models'] = 'order_send'; + $formmail->param['models'] = 'myobject_send'; $formmail->param['models_id']=GETPOST('modelmailselected','int'); - $formmail->param['orderid'] = $object->id; + $formmail->param['myobjectid'] = $object->id; $formmail->param['returnurl'] = $_SERVER["PHP_SELF"] . '?id=' . $object->id; // Init list of files