2
0
forked from Wavyzz/dolibarr

Work on modulebuilder

This commit is contained in:
Laurent Destailleur
2017-11-05 18:15:59 +01:00
parent e4ba6cfa10
commit 202d36d582
5 changed files with 66 additions and 46 deletions

View File

@@ -182,10 +182,15 @@ if (! empty($reg[1]) && $reg[1] == 'explorer' && ($reg[2] == '/resources.json' |
$classname = ucwords($regapi[1]); $classname = ucwords($regapi[1]);
$classname = str_replace('_', '', $classname); $classname = str_replace('_', '', $classname);
require_once $dir_part.$file_searched; 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); dol_syslog("Found API by index.php: classname=".$classname."Api for module ".$dir." into ".$dir_part.$file_searched);
$listofapis[] = $classname; $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 else
{ {
@@ -202,10 +207,10 @@ if (! empty($reg[1]) && $reg[1] == 'explorer' && ($reg[2] == '/resources.json' |
// Sort the classes before adding them to Restler. // 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. // 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); asort($listofapis);
foreach ($listofapis as $classname) foreach ($listofapis as $apiname => $classname)
{ {
$api->r->addAPIClass($classname); $api->r->addAPIClass($classname, $apiname);
} }
} }
@@ -218,24 +223,25 @@ if (! empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/resources.json'
$module = $regbis[1]; $module = $regbis[1];
} }
// Load a dedicated API file
dol_syslog("Load a dedicated API file");
$module=strtolower($module); $module=strtolower($module);
$moduledirforclass = getModuleDirForApiClass($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 if (in_array($module, array('category','contact','customer','invoice','order','product','thirdparty','user'))) // Old Apis
{ {
$classfile = $module; $classfile = $module;
if ($module == 'customer') { $classfile = 'thirdparty'; } if ($module == 'customer') { $classfile = 'thirdparty'; }
if ($module == 'order') { $classfile = 'commande'; } 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); $classname=ucwords($module);
if ($module == 'customer') { $classname='Thirdparty'; } if ($module == 'customer') { $classname='Thirdparty'; }
if ($module == 'order') { $classname='Commande'; } if ($module == 'order') { $classname='Commande'; }
//var_dump($classfile);var_dump($classname);exit; //var_dump($classfile);var_dump($classname);exit;
$res = include_once $dir_part_file; $res = false;
if ($dir_part_file) $res = include_once $dir_part_file;
if (! $res) if (! $res)
{ {
print 'API not found (failed to include API file)'; print 'API not found (failed to include API file)';
@@ -246,14 +252,17 @@ if (! empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/resources.json'
} }
else else
{ {
$classfile = str_replace('_', '', $module); $classfile = str_replace('_', '', preg_replace('/api$/i','', $module));
if ($module == 'contracts') $moduledirforclass = 'contrat';
if ($module == 'supplierinvoices') $classfile = 'supplier_invoices'; if ($module == 'supplierinvoices') $classfile = 'supplier_invoices';
if ($module == 'supplierorders') $classfile = 'supplier_orders'; 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); $classname=ucwords($module);
$res = include_once $dir_part_file; 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) if (! $res)
{ {
print 'API not found (failed to include API file)'; print 'API not found (failed to include API file)';

View File

@@ -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 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 $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) * @return string Full filesystem path (if path=0), Full url path (if mode=1)
*/ */
function dol_buildpath($path, $type=0, $returnemptyifnotfound=0) function dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
@@ -674,9 +676,9 @@ function dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
return $res; 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 else // For an url path

View File

@@ -2192,19 +2192,21 @@ function cartesianArray(array $input) {
*/ */
function getModuleDirForApiClass($module) 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'; $moduledirforclass = 'api';
} }
if (preg_match('/^dictionary/', $module)) { elseif (preg_match('/^dictionary/', $module)) {
$moduledirforclass = 'api'; $moduledirforclass = 'api';
} }
elseif ($module == 'contact' || $module == 'contacts' || $module == 'customer' || $module == 'thirdparty' || $module == 'thirdparties') {
if ($module == 'contact' || $module == 'contacts' || $module == 'customer' || $module == 'thirdparty' || $module == 'thirdparties') {
$moduledirforclass = 'societe'; $moduledirforclass = 'societe';
} }
if ($module == 'propale' || $module == 'proposals') { elseif ($module == 'propale' || $module == 'proposals') {
$moduledirforclass = 'comm/propal'; $moduledirforclass = 'comm/propal';
} }
elseif ($module == 'agenda' || $module == 'agendaevents') { elseif ($module == 'agenda' || $module == 'agendaevents') {

View File

@@ -132,7 +132,7 @@ if ($dirins && $action == 'initmodule' && $modulename)
dol_delete_file($destdir.'/scripts/myobject.php'); dol_delete_file($destdir.'/scripts/myobject.php');
dol_delete_file($destdir.'/img/object_myobject.png'); dol_delete_file($destdir.'/img/object_myobject.png');
dol_delete_file($destdir.'/class/myobject.class.php'); 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 // Edit PHP files
@@ -226,7 +226,7 @@ if ($dirins && $action == 'initobject' && $module && $objectname)
'scripts/myobject.php'=>'scripts/'.strtolower($objectname).'.php', 'scripts/myobject.php'=>'scripts/'.strtolower($objectname).'.php',
'img/object_myobject.png'=>'img/object_'.strtolower($objectname).'.png', 'img/object_myobject.png'=>'img/object_'.strtolower($objectname).'.png',
'class/myobject.class.php'=>'class/'.strtolower($objectname).'.class.php', '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) foreach($filetogenerate as $srcfile => $destfile)
@@ -513,7 +513,7 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname)
'scripts/myobject.php'=>'scripts/'.strtolower($objectname).'.php', 'scripts/myobject.php'=>'scripts/'.strtolower($objectname).'.php',
'img/object_myobject.png'=>'img/object_'.strtolower($objectname).'.png', 'img/object_myobject.png'=>'img/object_'.strtolower($objectname).'.png',
'class/myobject.class.php'=>'class/'.strtolower($objectname).'.class.php', '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; $resultko = 0;
@@ -1415,7 +1415,7 @@ elseif (! empty($module))
{ {
try { try {
$pathtoclass = strtolower($module).'/class/'.strtolower($tabobj).'.class.php'; $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'; $pathtoagenda = strtolower($module).'/'.strtolower($tabobj).'_agenda.php';
$pathtocard = strtolower($module).'/'.strtolower($tabobj).'_card.php'; $pathtocard = strtolower($module).'/'.strtolower($tabobj).'_card.php';
$pathtodocument = strtolower($module).'/'.strtolower($tabobj).'_document.php'; $pathtodocument = strtolower($module).'/'.strtolower($tabobj).'_document.php';

View File

@@ -18,8 +18,12 @@
use Luracast\Restler\RestException; 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 * \ingroup mymodule
* \brief File for API management of myobject. * \brief File for API management of myobject.
*/ */
@@ -31,7 +35,7 @@ use Luracast\Restler\RestException;
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
*/ */
class MyObjectApi extends DolibarrApi class MyModuleApi extends DolibarrApi
{ {
/** /**
* @var array $FIELDS Mandatory fields, checked when create and update object * @var array $FIELDS Mandatory fields, checked when create and update object
@@ -40,6 +44,7 @@ class MyObjectApi extends DolibarrApi
'name' 'name'
); );
/** /**
* @var MyObject $myobject {@type MyObject} * @var MyObject $myobject {@type MyObject}
*/ */
@@ -48,7 +53,7 @@ class MyObjectApi extends DolibarrApi
/** /**
* Constructor * Constructor
* *
* @url GET myobject/ * @url GET /
* *
*/ */
function __construct() function __construct()
@@ -66,7 +71,7 @@ class MyObjectApi extends DolibarrApi
* @param int $id ID of myobject * @param int $id ID of myobject
* @return array|mixed data without useless information * @return array|mixed data without useless information
* *
* @url GET myobject/{id} * @url GET myobjects/{id}
* @throws RestException * @throws RestException
*/ */
function get($id) function get($id)
@@ -87,22 +92,24 @@ class MyObjectApi extends DolibarrApi
return $this->_cleanObjectDatas($this->myobject); return $this->_cleanObjectDatas($this->myobject);
} }
/** /**
* List myobjects * List myobjects
* *
* Get a list of myobjects * Get a list of myobjects
* *
* @param int $mode Use this param to filter list * @param string $sortfield Sort field
* @param string $sortfield Sort field * @param string $sortorder Sort order
* @param string $sortorder Sort order * @param int $limit Limit for list
* @param int $limit Limit for list * @param int $page Page number
* @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')"
* @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 order objects
* @return array Array of myobject objects *
* @throws RestException
* *
* @url GET /myobjects/ * @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; global $db, $conf;
$obj_ret = array(); $obj_ret = array();
@@ -183,7 +190,7 @@ class MyObjectApi extends DolibarrApi
* @param array $request_data Request datas * @param array $request_data Request datas
* @return int ID of myobject * @return int ID of myobject
* *
* @url POST myobject/ * @url POST myobjects/
*/ */
function post($request_data = NULL) function post($request_data = NULL)
{ {
@@ -209,7 +216,7 @@ class MyObjectApi extends DolibarrApi
* @param array $request_data Datas * @param array $request_data Datas
* @return int * @return int
* *
* @url PUT myobject/{id} * @url PUT myobjects/{id}
*/ */
function put($id, $request_data = NULL) function put($id, $request_data = NULL)
{ {