| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* Copyright (C) 2024       Rafael San José         <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This program is free software; you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * the Free Software Foundation; either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * You should have received a copy of the GNU General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * along with this program. If not, see <https://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | namespace Dolibarr\Lib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | abstract class Misc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Loads a model class given the name and an alternative parameter to be passed to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * the model during creation (usually a db instance is passed to it). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param $param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return mixed|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public static function loadModel($name, $param = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $classes = static::getClassPaths(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (!isset($classes[$name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if ($param === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             return new $classes[$name](); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return new $classes[$name]($param); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Same as createClassPaths, but trying to use a file cache. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param $searchPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param $folderName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return array|false|mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public static function getClassPaths($searchPath = '/../Dolibarr/Modules', $folderName = 'Model') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $path = realpath(BASE_PATH . '/../tmp'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (!is_dir($path) && !mkdir($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             // Failed to create directory for cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             return static::createClassPaths(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $file = $path . '/classpaths.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (file_exists($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return json_decode(file_get_contents($file), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $data = static::createClassPaths(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         file_put_contents($file, json_encode($data)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * It scans the $folderName folders that are within the folders that exist in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * $searchPath, creating an associative array in which the index is the name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * of the class, and the value is its full namespace. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * For example, with the default values it would return something like this... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * ^ array [▼ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      *   "Accountancy" => "\DoliModules\Accounting\Model\Accountancy" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      *   "AccountancyCategory" => "\DoliModules\Accounting\Model\AccountancyCategory" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      *   ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param $searchPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param $folderName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     private static function createClassPaths($searchPath = '/../Dolibarr/Modules', $folderName = 'Model') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $path = realpath(BASE_PATH . $searchPath) . DIRECTORY_SEPARATOR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $folders = scandir($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         foreach ($folders as $folder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $folderPath = realpath($path . $folder . DIRECTORY_SEPARATOR . $folderName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             if (!is_dir($folderPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $classes = scandir($folderPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             foreach ($classes as $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 if (!str_ends_with($class, '.php') || str_ends_with($class, '.class.php')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $className = substr($class, 0, -4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $result[$className] = "\\DoliModules\\$folder\\$folderName\\$className"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Returns a Dolibarr/Code/X/Classes instance by name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return mixed|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public static function getCodeLibClass($name, $params = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $path = realpath(constant('BASE_PATH') . '/../Dolibarr/Code'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $folders = scandir($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         foreach ($folders as $folder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $folderPath = realpath($path . '/' . $folder . '/Classes'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             if (!is_dir($folderPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $classes = scandir($folderPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             foreach ($classes as $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 if (!str_ends_with($class, '.php') || str_ends_with($class, '.class.php')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $className = substr($class, 0, -4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 $result[$className] = "\\Dolibarr\\Code\\$folder\\Classes\\$className"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $fullClassName = $result[$name] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         if (isset($fullClassName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             return new $fullClassName($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |     public static function getModuleClass($name, $params = null) | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |         $path = realpath(constant('BASE_PATH') . '/../Dolibarr/Modules'); | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |         $folders = scandir($path); | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |         foreach ($folders as $class) { | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |             if (!str_ends_with($class, '.php') || str_ends_with($class, '.class.php')) { | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |                 continue; | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |             $className = substr($class, 0, -4); | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |             $result[$className] = "\\Dolibarr\\Modules\\$className"; | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |         $fullClassName = $result[$name] ?? null; | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |         if (isset($fullClassName)) { | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |             return new $fullClassName($params); | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |         return null; | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 163 |  |  | } |