From 60d6cc580600da0b3cd86cf225518fc48e40fc9b Mon Sep 17 00:00:00 2001
From: jfefe <jfefe@aternatik.fr>
Date: Sun, 3 May 2015 19:43:49 +0200
Subject: [PATCH] Modify search of API classes for module

A module can now bring several API classes.
By example, thirdparty module can have api_thirdparty.class.php for thirdparty and api_contact.class.php for contacts.

Classes must be named <Object>API (ie ThirdpartyApi or ContactApi)
---
 htdocs/public/api/index.php                   | 26 +++++++++++++------
 htdocs/societe/class/api_thirdparty.class.php |  4 ++-
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/htdocs/public/api/index.php b/htdocs/public/api/index.php
index 709b4436337..832ab4e6c69 100644
--- a/htdocs/public/api/index.php
+++ b/htdocs/public/api/index.php
@@ -88,19 +88,29 @@ foreach ($modulesdir as $dir)
                     /*
                      * If exists, load the API class for enable module
                      *
-                     * Search a file api_<object>.class.php into /htdocs/<module>/class directory
+                     * Search files named api_<object>.class.php into /htdocs/<module>/class directory
                      *
                      * @todo : take care of externals module!
-                     * @todo : use getElementProperties() function
+                     * @todo : use getElementProperties() function ?
                      */
-                    $file = DOL_DOCUMENT_ROOT.'/'.$part."/class/api_".$obj.".class.php";
-
-                    $classname = ucwords($obj).'Api';
-                    if (file_exists($file))
+                    $dir_part = DOL_DOCUMENT_ROOT.'/'.$part.'/class/';
+                        
+                    $handle_part=@opendir(dol_osencode($dir_part));
+                    if (is_resource($handle_part))
                     {
-                        require_once $file;
-                        $api->r->addAPIClass($classname,'');
+                        while (($file_searched = readdir($handle_part))!==false)
+                        {
+                            if (is_readable($dir_part.$file_searched) && preg_match("/^(api_.*)\.class\.php$/i",$file_searched,$reg))
+                            {
+                                $classname=$reg[1];
+                                $classname = str_replace('Api_','',ucwords($reg[1])).'Api';
+                                require_once $dir_part.$file_searched;
+                                if(class_exists($classname))
+                                    $api->r->addAPIClass($classname,'');
+                            }
+                        }
                     }
+
                 }
             }
         }
diff --git a/htdocs/societe/class/api_thirdparty.class.php b/htdocs/societe/class/api_thirdparty.class.php
index d21bee80c41..8fcc8b539d4 100644
--- a/htdocs/societe/class/api_thirdparty.class.php
+++ b/htdocs/societe/class/api_thirdparty.class.php
@@ -90,7 +90,9 @@ class ThirdpartyApi extends DolibarrApi {
     }
 
     /**
-     * Fetch a list of thirdparties
+     * List thirdparties
+     * 
+     * Get a list of thirdparties
      *
      * @url	GET /thirdparties/
      * 
-- 
GitLab