From 71c44d82ab3fe569ecd098bb0ec5e03f861e96d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Ferry?= <jfefe@aternatik.fr> Date: Sat, 15 Mar 2014 07:31:49 +0100 Subject: [PATCH] Move resource module functions into commonobject : - new method to get an array with object properties - new method to fetch an object only with id and element_type --- htdocs/core/class/commonobject.class.php | 79 ++++++++++++++++++++++ htdocs/resource/class/resource.class.php | 84 +----------------------- 2 files changed, 81 insertions(+), 82 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index c2fb6d6d08d..54028db20af 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -3277,6 +3277,85 @@ abstract class CommonObject print '</table>'; } + + /** + * Get an array with properties of an element + * + * @param string $element_type Element type. ex : project_task or object@modulext or object_under@module + * @return array (module, classpath, element, subelement, classfile, classname) + */ + function getElementProperties($element_type) + { + // Parse element/subelement (ex: project_task) + $module = $element = $subelement = $element_type; + + // If we ask an resource form external module (instead of default path) + if (preg_match('/^([^@]+)@([^@]+)$/i',$element_type,$regs)) + { + $element = $subelement = $regs[1]; + $module = $regs[2]; + } + + //print '<br />1. element : '.$element.' - module : '.$module .'<br />'; + if ( preg_match('/^([^_]+)_([^_]+)/i',$element,$regs)) + { + $module = $element = $regs[1]; + $subelement = $regs[2]; + } + + $classfile = strtolower($subelement); + $classname = ucfirst($subelement); + $classpath = $module.'/class'; + + // For compat + if($element_type == "action") { + $classpath = 'comm/action/class'; + $subelement = 'Actioncomm'; + $classfile = strtolower($subelement); + $classname = ucfirst($subelement); + $module = 'agenda'; + } + // TODO : add other elements + + $element_properties = array( + 'module' => $module, + 'classpath' => $classpath, + 'element' => $element, + 'subelement' => $subelement, + 'classfile' => $classfile, + 'classname' => $classname + ); + return $element_properties; + } + + /** + * Fetch an object with element_type and its id + * Inclusion classes is automatic + * + * @param int $element_id + * @param string $element_type + * @return object || 0 || -1 if error + */ + function fetchObjectByElement($element_id,$element_type) { + + global $conf; + + $element_prop = $this->getElementProperties($element_type); + if (is_array($element_prop) && $conf->$element_prop['module']->enabled) + { + dol_include_once('/'.$element_prop['classpath'].'/'.$element_prop['classfile'].'.class.php'); + + $objectstat = new $element_prop['classname']($this->db); + $ret = $objectstat->fetch($element_id); + if ($ret >= 0) + { + return $objectstat; + } + } + return 0; + } + + /** * Overwrite magic function to solve problem of cloning object that are kept as references * diff --git a/htdocs/resource/class/resource.class.php b/htdocs/resource/class/resource.class.php index edbf4bc0ad6..f422749847e 100644 --- a/htdocs/resource/class/resource.class.php +++ b/htdocs/resource/class/resource.class.php @@ -16,8 +16,8 @@ */ /** - * \file place/class/resource.class.php - * \ingroup place + * \file resource/class/resource.class.php + * \ingroup resource * \brief Class file for resource object */ @@ -49,8 +49,6 @@ class Resource extends CommonObject var $tms=''; - - /** * Constructor * @@ -375,84 +373,6 @@ class Resource extends CommonObject } - /** - * - * - * @param string $element_type Element type project_task - * @return array - */ - function getElementProperties($element_type) - { - // Parse element/subelement (ex: project_task) - $module = $element = $subelement = $element_type; - - // If we ask an resource form external module (instead of default path) - if (preg_match('/^([^@]+)@([^@]+)$/i',$element_type,$regs)) - { - $element = $subelement = $regs[1]; - $module = $regs[2]; - } - - //print '<br />1. element : '.$element.' - module : '.$module .'<br />'; - - if ( preg_match('/^([^_]+)_([^_]+)/i',$element,$regs)) - { - $module = $element = $regs[1]; - $subelement = $regs[2]; - } - - $classfile = strtolower($subelement); - $classname = ucfirst($subelement); - $classpath = $module.'/class'; - - - // For compat - if($element_type == "action") { - $classpath = 'comm/action/class'; - $subelement = 'Actioncomm'; - $classfile = strtolower($subelement); - $classname = ucfirst($subelement); - $module = 'agenda'; - } - - - $element_properties = array( - 'module' => $module, - 'classpath' => $classpath, - 'element' => $element, - 'subelement' => $subelement, - 'classfile' => $classfile, - 'classname' => $classname - ); - return $element_properties; - } - - /** - * Fetch an object with element_type and his id - * Inclusion classes is automatic - * - * - */ - function fetchObjectByElement($element_id,$element_type) { - - global $conf; - - $element_prop = $this->getElementProperties($element_type); - - if (is_array($element_prop) && $conf->$element_prop['module']->enabled) - { - dol_include_once('/'.$element_prop['classpath'].'/'.$element_prop['classfile'].'.class.php'); - - $objectstat = new $element_prop['classname']($this->db); - $ret = $objectstat->fetch($element_id); - if ($ret >= 0) - { - return $objectstat; - } - } - return 0; - } - /** * Add resources to the actioncom object * -- GitLab