Skip to content

Commit

Permalink
Move resource module functions into commonobject :
Browse files Browse the repository at this point in the history
- new method to get an array with object properties
- new method to fetch an object only with id and element_type
  • Loading branch information
jfefe committed May 10, 2014
1 parent d9c567c commit 71c44d8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 82 deletions.
79 changes: 79 additions & 0 deletions htdocs/core/class/commonobject.class.php
Expand Up @@ -3277,6 +3277,85 @@ function displayMarginInfos($force_price=false)
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
*
Expand Down
84 changes: 2 additions & 82 deletions htdocs/resource/class/resource.class.php
Expand Up @@ -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
*/
Expand Down Expand Up @@ -49,8 +49,6 @@ class Resource extends CommonObject
var $tms='';




/**
* Constructor
*
Expand Down Expand Up @@ -375,84 +373,6 @@ function update($user=0, $notrigger=0)
}


/**
*
*
* @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
*
Expand Down

0 comments on commit 71c44d8

Please sign in to comment.