From fdb50ab116697e0a6aaf53d222bb330133999197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Ferry?= Date: Sat, 15 Mar 2014 16:29:33 +0100 Subject: [PATCH] Move generic function from commonobject.class to functions2.lib.php (work on resource module) --- htdocs/core/class/commonobject.class.php | 78 --------------- htdocs/core/lib/functions2.lib.php | 121 +++++++++++++++++++++++ htdocs/resource/element_resource.php | 18 ++-- 3 files changed, 130 insertions(+), 87 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index cf0b52ad11a..379b955cd0d 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -3277,84 +3277,6 @@ abstract class CommonObject print ''; } - - /** - * 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 '
1. element : '.$element.' - module : '.$module .'
'; - 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; - } /** * Add resources to the current object diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index efa0af07843..6fcca4636d9 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -1656,3 +1656,124 @@ function cleanCorruptedTree($db, $tabletocleantree, $fieldfkparent) return $totalnb; } } + +/** + * 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 '
1. element : '.$element.' - module : '.$module .'
'; + 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'; + $module = 'agenda'; + } + + // To work with non standard path + if ($element_type == 'facture' || $element_type == 'invoice') { + $classpath = 'compta/facture/class'; + $module='facture'; + $subelement='facture'; + } + if ($element_type == 'commande' || $element_type == 'order') { + $classpath = 'commande/class'; + $module='commande'; + $subelement='commande'; + } + if ($element_type == 'propal') { + $classpath = 'comm/propal/class'; + } + if ($element_type == 'shipping') { + $classpath = 'expedition/class'; + $subelement = 'expedition'; + $module = 'expedition_bon'; + } + if ($element_type == 'delivery') { + $classpath = 'livraison/class'; + $subelement = 'livraison'; + $module = 'livraison_bon'; + } + if ($element_type == 'contract') { + $classpath = 'contrat/class'; + $module='contrat'; + $subelement='contrat'; + } + if ($element_type == 'member') { + $classpath = 'adherents/class'; + $module='adherent'; + $subelement='adherent'; + } + if ($element_type == 'cabinetmed_cons') { + $classpath = 'cabinetmed/class'; + $module='cabinetmed'; + $subelement='cabinetmedcons'; + } + if ($element_type == 'fichinter') { + $classpath = 'fichinter/class'; + $module='ficheinter'; + $subelement='fichinter'; + } + $classfile = strtolower($subelement); + $classname = ucfirst($subelement); + + $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 = 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; +} diff --git a/htdocs/resource/element_resource.php b/htdocs/resource/element_resource.php index ab4ce383953..95abaf31031 100644 --- a/htdocs/resource/element_resource.php +++ b/htdocs/resource/element_resource.php @@ -50,7 +50,7 @@ $sortfield = GETPOST('sortfield','alpha'); $page = GETPOST('page','int'); */ -if( ! $user->rights->place->read) +if( ! $user->rights->resource->read) accessforbidden(); $object=new Resource($db); @@ -100,7 +100,7 @@ else { require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php'; - $act = $object->fetchObjectByElement($element_id,$element); + $act = fetchObjectByElement($element_id,$element); if(is_object($act)) { $head=actions_prepare_head($act); @@ -135,7 +135,7 @@ else */ if($element_id && $element == 'societe') { - $socstatic = $object->fetchObjectByElement($element_id,$element); + $socstatic = fetchObjectByElement($element_id,$element); if(is_object($socstatic)) { require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; $head = societe_prepare_head($socstatic); @@ -148,11 +148,11 @@ else //$linkback = ''.$langs->trans("BackToList").''; // Name - print ''.$langs->trans('ThirdPartyName').''; - print ''; - print $form->showrefnav($socstatic, 'socid', '', ($user->societe_id?0:1), 'rowid', 'nom'); - print ''; - print ''; + print ''.$langs->trans('ThirdPartyName').''; + print ''; + print $form->showrefnav($socstatic, 'socid', '', ($user->societe_id?0:1), 'rowid', 'nom'); + print ''; + print ''; print ''; print ''; @@ -173,7 +173,7 @@ else $resources=(array) $resources; // To be sure $resources is an array foreach($resources as $resource_obj) { - $element_prop = $object->getElementProperties($resource_obj); + $element_prop = getElementProperties($resource_obj); //var_dump($element_prop); print_titre($langs->trans(ucfirst($element_prop['element']).'Singular'));