From 202d36d58279a2c6dd4f24f464bc543650c24fda Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 5 Nov 2017 18:15:59 +0100 Subject: [PATCH] Work on modulebuilder --- htdocs/api/index.php | 45 +++++++++++-------- htdocs/core/lib/functions.lib.php | 8 ++-- htdocs/core/lib/functions2.lib.php | 16 ++++--- htdocs/modulebuilder/index.php | 8 ++-- ...bject.class.php => api_mymodule.class.php} | 35 +++++++++------ 5 files changed, 66 insertions(+), 46 deletions(-) rename htdocs/modulebuilder/template/class/{api_myobject.class.php => api_mymodule.class.php} (90%) diff --git a/htdocs/api/index.php b/htdocs/api/index.php index fb9498f3592..e10ebba0e62 100644 --- a/htdocs/api/index.php +++ b/htdocs/api/index.php @@ -182,10 +182,15 @@ if (! empty($reg[1]) && $reg[1] == 'explorer' && ($reg[2] == '/resources.json' | $classname = ucwords($regapi[1]); $classname = str_replace('_', '', $classname); require_once $dir_part.$file_searched; - if (class_exists($classname)) + if (class_exists($classname.'Api')) { - //dol_syslog("Found API by index.php: classname=".$classname." for module ".$dir." into ".$dir_part.$file_searched); - $listofapis[] = $classname; + dol_syslog("Found API by index.php: classname=".$classname."Api for module ".$dir." into ".$dir_part.$file_searched); + $listofapis[strtolower($classname.'Api')] = $classname.'Api'; + } + elseif (class_exists($classname)) + { + dol_syslog("Found API by index.php: classname=".$classname." for module ".$dir." into ".$dir_part.$file_searched); + $listofapis[strtolower($classname)] = $classname; } else { @@ -202,10 +207,10 @@ if (! empty($reg[1]) && $reg[1] == 'explorer' && ($reg[2] == '/resources.json' | // Sort the classes before adding them to Restler. // The Restler API Explorer shows the classes in the order they are added and it's a mess if they are not sorted. - sort($listofapis); - foreach ($listofapis as $classname) + asort($listofapis); + foreach ($listofapis as $apiname => $classname) { - $api->r->addAPIClass($classname); + $api->r->addAPIClass($classname, $apiname); } } @@ -218,25 +223,26 @@ if (! empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/resources.json' $module = $regbis[1]; } - // Load a dedicated API file - dol_syslog("Load a dedicated API file"); - $module=strtolower($module); $moduledirforclass = getModuleDirForApiClass($module); + // Load a dedicated API file + dol_syslog("Load a dedicated API file moduledirforclass=".$moduledirforclass); + if (in_array($module, array('category','contact','customer','invoice','order','product','thirdparty','user'))) // Old Apis { $classfile = $module; if ($module == 'customer') { $classfile = 'thirdparty'; } if ($module == 'order') { $classfile = 'commande'; } - $dir_part_file = dol_buildpath('/'.$moduledirforclass.'/class/api_deprecated_'.$classfile.'.class.php'); + $dir_part_file = dol_buildpath('/'.$moduledirforclass.'/class/api_deprecated_'.$classfile.'.class.php', 0, 2); $classname=ucwords($module); if ($module == 'customer') { $classname='Thirdparty'; } if ($module == 'order') { $classname='Commande'; } //var_dump($classfile);var_dump($classname);exit; - $res = include_once $dir_part_file; - if (! $res) + $res = false; + if ($dir_part_file) $res = include_once $dir_part_file; + if (! $res) { print 'API not found (failed to include API file)'; header('HTTP/1.1 501 API not found (failed to include API file)'); @@ -246,21 +252,24 @@ if (! empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/resources.json' } else { - $classfile = str_replace('_', '', $module); - if ($module == 'contracts') $moduledirforclass = 'contrat'; + $classfile = str_replace('_', '', preg_replace('/api$/i','', $module)); if ($module == 'supplierinvoices') $classfile = 'supplier_invoices'; if ($module == 'supplierorders') $classfile = 'supplier_orders'; - $dir_part_file = dol_buildpath('/'.$moduledirforclass.'/class/api_'.$classfile.'.class.php'); + $dir_part_file = dol_buildpath('/'.$moduledirforclass.'/class/api_'.$classfile.'.class.php', 0, 2); + $classname=ucwords($module); - $res = include_once $dir_part_file; - if (! $res) + dol_syslog('Search /'.$moduledirforclass.'/class/api_'.$classfile.'.class.php => dir_part_file='.$dir_part_file.' classname='.$classname); + + $res = false; + if ($dir_part_file) $res = include_once $dir_part_file; + if (! $res) { print 'API not found (failed to include API file)'; header('HTTP/1.1 501 API not found (failed to include API file)'); exit(0); } - + if (class_exists($classname)) $api->r->addAPIClass($classname); } } diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index e3c68ebdd60..b7d333452e4 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -653,7 +653,9 @@ function dol_include_once($relpath, $classname='') * * @param string $path Relative path to file (if mode=0) or relative url (if mode=1). Ie: mydir/myfile, ../myfile * @param int $type 0=Used for a Filesystem path, 1=Used for an URL path (output relative), 2=Used for an URL path (output full path using same host that current url), 3=Used for an URL path (output full path using host defined into $dolibarr_main_url_root of conf file) - * @param int $returnemptyifnotfound If path==0 and if file was not found, do not return default path but an empty string + * @param int $returnemptyifnotfound 0:If $type==0 and if file was not found into alternate dir, return default path into main dir (no test on it) + * 1:If $type==0 and if file was not found into alternate dir, return empty string + * 2:If $type==0 and if file was not found into alternate dir, test into main dir, return default path if found, empty string if not found * @return string Full filesystem path (if path=0), Full url path (if mode=1) */ function dol_buildpath($path, $type=0, $returnemptyifnotfound=0) @@ -674,9 +676,9 @@ function dol_buildpath($path, $type=0, $returnemptyifnotfound=0) return $res; } } - if ($returnemptyifnotfound) // Not found, we return empty string + if ($returnemptyifnotfound) // Not found into alternate dir { - return ''; + if ($returnemptyifnotfound == 1 || ! file_exists($res)) return ''; } } else // For an url path diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index 7697c4e56a9..4f1ed15d676 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -1607,7 +1607,7 @@ function getListOfModels($db,$type,$maxfilenamelength=0) if (is_dir($tmpdir)) { // all type of template is allowed - $tmpfiles=dol_dir_list($tmpdir, 'files', 0, '', '', 'name', SORT_ASC, 0); + $tmpfiles=dol_dir_list($tmpdir, 'files', 0, '', '', 'name', SORT_ASC, 0); if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles); } } @@ -2192,19 +2192,21 @@ function cartesianArray(array $input) { */ function getModuleDirForApiClass($module) { - $moduledirforclass=$module; + $moduledirforclass=preg_replace('/api$/i','',$module); - if (in_array($module, array('login', 'access', 'status', 'documents'))) { + if ($module == 'contracts') { + $moduledirforclass = 'contrat'; + } + elseif (in_array($module, array('login', 'access', 'status', 'documents'))) { $moduledirforclass = 'api'; } - if (preg_match('/^dictionary/', $module)) { + elseif (preg_match('/^dictionary/', $module)) { $moduledirforclass = 'api'; } - - if ($module == 'contact' || $module == 'contacts' || $module == 'customer' || $module == 'thirdparty' || $module == 'thirdparties') { + elseif ($module == 'contact' || $module == 'contacts' || $module == 'customer' || $module == 'thirdparty' || $module == 'thirdparties') { $moduledirforclass = 'societe'; } - if ($module == 'propale' || $module == 'proposals') { + elseif ($module == 'propale' || $module == 'proposals') { $moduledirforclass = 'comm/propal'; } elseif ($module == 'agenda' || $module == 'agendaevents') { diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 90ce90859f5..b655fddbbde 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -132,7 +132,7 @@ if ($dirins && $action == 'initmodule' && $modulename) dol_delete_file($destdir.'/scripts/myobject.php'); dol_delete_file($destdir.'/img/object_myobject.png'); dol_delete_file($destdir.'/class/myobject.class.php'); - dol_delete_file($destdir.'/class/api_myobject.class.php'); + dol_delete_file($destdir.'/class/api_mymodule.class.php'); } // Edit PHP files @@ -226,7 +226,7 @@ if ($dirins && $action == 'initobject' && $module && $objectname) 'scripts/myobject.php'=>'scripts/'.strtolower($objectname).'.php', 'img/object_myobject.png'=>'img/object_'.strtolower($objectname).'.png', 'class/myobject.class.php'=>'class/'.strtolower($objectname).'.class.php', - 'class/api_myobject.class.php'=>'class/api_'.strtolower($objectname).'.class.php' + 'class/api_mymodule.class.php'=>'class/api_'.strtolower($module).'.class.php' ); foreach($filetogenerate as $srcfile => $destfile) @@ -513,7 +513,7 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) 'scripts/myobject.php'=>'scripts/'.strtolower($objectname).'.php', 'img/object_myobject.png'=>'img/object_'.strtolower($objectname).'.png', 'class/myobject.class.php'=>'class/'.strtolower($objectname).'.class.php', - 'class/api_myobject.class.php'=>'class/api_'.strtolower($objectname).'.class.php' + 'class/api_myobject.class.php'=>'class/api_'.strtolower($module).'.class.php' ); $resultko = 0; @@ -1415,7 +1415,7 @@ elseif (! empty($module)) { try { $pathtoclass = strtolower($module).'/class/'.strtolower($tabobj).'.class.php'; - $pathtoapi = strtolower($module).'/class/api_'.strtolower($tabobj).'.class.php'; + $pathtoapi = strtolower($module).'/class/api_'.strtolower($module).'.class.php'; $pathtoagenda = strtolower($module).'/'.strtolower($tabobj).'_agenda.php'; $pathtocard = strtolower($module).'/'.strtolower($tabobj).'_card.php'; $pathtodocument = strtolower($module).'/'.strtolower($tabobj).'_document.php'; diff --git a/htdocs/modulebuilder/template/class/api_myobject.class.php b/htdocs/modulebuilder/template/class/api_mymodule.class.php similarity index 90% rename from htdocs/modulebuilder/template/class/api_myobject.class.php rename to htdocs/modulebuilder/template/class/api_mymodule.class.php index 29596df2d96..a068715b521 100644 --- a/htdocs/modulebuilder/template/class/api_myobject.class.php +++ b/htdocs/modulebuilder/template/class/api_mymodule.class.php @@ -18,8 +18,12 @@ use Luracast\Restler\RestException; +dol_include_once('/mymodule/class/myobject.class.php'); + + + /** - * \file htdocs/modulebuilder/template/class/api_myobject.class.php + * \file htdocs/modulebuilder/template/class/api_mymodule.class.php * \ingroup mymodule * \brief File for API management of myobject. */ @@ -31,7 +35,7 @@ use Luracast\Restler\RestException; * @access protected * @class DolibarrApiAccess {@requires user,external} */ -class MyObjectApi extends DolibarrApi +class MyModuleApi extends DolibarrApi { /** * @var array $FIELDS Mandatory fields, checked when create and update object @@ -40,6 +44,7 @@ class MyObjectApi extends DolibarrApi 'name' ); + /** * @var MyObject $myobject {@type MyObject} */ @@ -48,7 +53,7 @@ class MyObjectApi extends DolibarrApi /** * Constructor * - * @url GET myobject/ + * @url GET / * */ function __construct() @@ -66,7 +71,7 @@ class MyObjectApi extends DolibarrApi * @param int $id ID of myobject * @return array|mixed data without useless information * - * @url GET myobject/{id} + * @url GET myobjects/{id} * @throws RestException */ function get($id) @@ -87,22 +92,24 @@ class MyObjectApi extends DolibarrApi return $this->_cleanObjectDatas($this->myobject); } + /** * List myobjects * * Get a list of myobjects * - * @param int $mode Use this param to filter list - * @param string $sortfield Sort field - * @param string $sortorder Sort order - * @param int $limit Limit for list - * @param int $page Page number - * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101') or (t.import_key:=:'20160101')" - * @return array Array of myobject objects + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Limit for list + * @param int $page Page number + * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" + * @return array Array of order objects + * + * @throws RestException * * @url GET /myobjects/ */ - function index($mode, $sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '') { + function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '') { global $db, $conf; $obj_ret = array(); @@ -183,7 +190,7 @@ class MyObjectApi extends DolibarrApi * @param array $request_data Request datas * @return int ID of myobject * - * @url POST myobject/ + * @url POST myobjects/ */ function post($request_data = NULL) { @@ -209,7 +216,7 @@ class MyObjectApi extends DolibarrApi * @param array $request_data Datas * @return int * - * @url PUT myobject/{id} + * @url PUT myobjects/{id} */ function put($id, $request_data = NULL) {