[enh] adds documentation and partially rewrites m_variables for testing

1. Does echo by default for helpers but makes it optional and returns result

2. Comments $variables = $this->variable_merge(array(),$arr_var['DEFAULT'][NULL]);
                                 					  =======
as this failed to work here.
This commit is contained in:
alban 2014-03-22 15:47:14 +01:00
parent f17f18c476
commit ee71a5a001
1 changed files with 106 additions and 35 deletions

View File

@ -32,12 +32,15 @@
* @author Drupal Developpement Team * @author Drupal Developpement Team
* @link http://cvs.drupal.org/viewcvs/drupal/drupal/includes/bootstrap.inc?rev=1.38&view=auto * @link http://cvs.drupal.org/viewcvs/drupal/drupal/includes/bootstrap.inc?rev=1.38&view=auto
*/ */
class m_variables { class m_variables {
var $strata_order = array('DEFAULT','GLOBAL','FQDN_CREATOR','FQDN','CREATOR','MEMBER','DOMAIN'); var $strata_order = array('DEFAULT','GLOBAL','FQDN_CREATOR','FQDN','CREATOR','MEMBER','DOMAIN');
var $cache_variable_list = false; var $cache_variable_list = false;
var $replace_array = array(); var $replace_array = array();
/**
*
* @global type $L_FQDN
*/
function m_variables() { function m_variables() {
global $L_FQDN; global $L_FQDN;
$this->replace_array = array( $this->replace_array = array(
@ -45,7 +48,15 @@ class m_variables {
); );
} }
// used by get_impersonated to merge array. Son value overwrite father's value
/**
* used by get_impersonated to merge array. Son value overwrite father's value
*
* @param array $father
* @param array $son
* @return array
*/
private function variable_merge($father, $son) { private function variable_merge($father, $son) {
if (! is_array($son)) return $father; if (! is_array($son)) return $father;
foreach ($son as $k=>$v) { foreach ($son as $k=>$v) {
@ -60,6 +71,9 @@ class m_variables {
* The variable table is composed of values that have been saved in the table * The variable table is composed of values that have been saved in the table
* with variable_set() as well as those explicitly specified in the configuration * with variable_set() as well as those explicitly specified in the configuration
* file. * file.
*
* @global int $cuid
* @return array
*/ */
function variable_init() { function variable_init() {
global $cuid; global $cuid;
@ -86,6 +100,12 @@ class m_variables {
* *
* If $fqdn and $uid aren't specified, return the default value * If $fqdn and $uid aren't specified, return the default value
* *
* @global type $db
* @global type $err
* @param type $fqdn
* @param type $uid
* @param type $var
* @return array
*/ */
function get_impersonated($fqdn=null, $uid=null, $var=null) { function get_impersonated($fqdn=null, $uid=null, $var=null) {
global $db, $err; global $db, $err;
@ -111,7 +131,10 @@ class m_variables {
if (! isset($arr_var[$strata]) || !is_array($arr_var[$strata])) continue; if (! isset($arr_var[$strata]) || !is_array($arr_var[$strata])) continue;
switch($strata) { switch($strata) {
case 'DEFAULT': case 'DEFAULT':
$variables = $this->variable_merge(array(),$arr_var['DEFAULT'][NULL]);
// $variables = $this->variable_merge(array(),$arr_var['DEFAULT'][NULL]);
$variablesList = current($arr_var["DEFAULT"]);
$variables = $this->variable_merge(array(),$variablesList);
break; break;
case 'GLOBAL': case 'GLOBAL':
$variables = $this->variable_merge($variables, $arr_var['GLOBAL'][NULL]); $variables = $this->variable_merge($variables, $arr_var['GLOBAL'][NULL]);
@ -200,7 +223,20 @@ class m_variables {
return $default; return $default;
} }
// Create or update a variable. /**
* Create or update a variable
*
* @global type $db
* @global type $err
* @param type $var_name
* @param type $var_value
* @param type $strata
* @param null $strata_id
* @param null $var_id
* @param type $comment
* @param type $type
* @return boolean
*/
function variable_update_or_create($var_name, $var_value, $strata=null, $strata_id=null, $var_id=null, $comment=null, $type=null) { function variable_update_or_create($var_name, $var_value, $strata=null, $strata_id=null, $var_id=null, $comment=null, $type=null) {
global $db, $err; global $db, $err;
$err->log('variable', 'variable_update_or_create'); $err->log('variable', 'variable_update_or_create');
@ -246,77 +282,108 @@ class m_variables {
*/ */
function del($id) { function del($id) {
global $db; global $db;
$db->query("DELETE FROM `variable` WHERE id = '".intval($id)."'"); $result = $db->query("DELETE FROM `variable` WHERE id = '".intval($id)."'");
$this->variable_init_maybe(true); $this->variable_init_maybe(true);
return $result;
} }
// echo HTML code to display a variable passed in parameters /**
function display_valueraw_html($v,$varname) { * echo HTML code to display a variable passed in parameters
*
* @param type $v
* @param type $varname
* @param type $echo
* @return type
*/
function display_valueraw_html($v,$varname,$echo = true) {
$output = "";
if (is_array($v)) { if (is_array($v)) {
if (empty($v)) { if (empty($v)) {
echo "<em>"._("Empty array")."</em>"; $output .= "<em>"._("Empty array")."</em>";
} else { } else {
echo "<ul>"; $output .= "<ul>";
foreach ( $v as $k=>$l) { foreach ( $v as $k=>$l) {
echo "<li>"; $output .= "<li>";
if (! is_numeric($k)) { if (! is_numeric($k)) {
if (is_null($varname)) { if (is_null($varname)) {
echo "$k"; $output .= "$k";
} else { } else {
if ( !isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]) || is_array( $this->variables_list()['DEFAULT'][null][$varname]['type'][$k] ) ) { if ( !isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]) || is_array( $this->variables_list()['DEFAULT'][null][$varname]['type'][$k] ) ) {
if (isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'])) { if (isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'])) {
echo $this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc']; $output .= $this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'];
} else { } else {
echo $k; $output .= $k;
} }
} else { } else {
echo $this->variables_list()['DEFAULT'][null][$varname]['type'][$k]; $output .= $this->variables_list()['DEFAULT'][null][$varname]['type'][$k];
} }
} }
} else { } else {
if (isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'] )) { if (isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'] )) {
echo $this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc']; $output .= $this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'];
} }
} }
if (is_array($l)) { if (is_array($l)) {
echo "<ul>"; $output .= "<ul>";
foreach ($l as $m => $n ) { foreach ($l as $m => $n ) {
echo "<li>"; $output .= "<li>";
if ( is_numeric($m)) { if ( is_numeric($m)) {
echo "$m"; $output .= "$m";
} else { } else {
echo $this->variables_list()['DEFAULT'][null][$varname]['type'][$k][$m]['desc']; $output .= $this->variables_list()['DEFAULT'][null][$varname]['type'][$k][$m]['desc'];
} }
echo " => $n"; $output .= " => $n";
echo "</li>"; $output .= "</li>";
} }
echo "</ul>"; $output .= "</ul>";
} else { } else {
echo " => $l"; $output .= " => $l";
} }
echo "</li>"; $output .= "</li>";
} }
echo "</ul>"; $output .= "</ul>";
} // empty $v } // empty $v
} else if (empty($v) && $v != '0') { } else if (empty($v) && $v != '0') {
echo "<em>"._("Empty")."</em>"; $output .= "<em>"._("Empty")."</em>";
} else { } else {
echo $v; $output .= $v;
} }
if( $echo ){
echo $output;
}
return $output;
} }
// Display a variable if is set /**
function display_value_html($tab, $strata, $id, $varname) { * Display a variable if is set
*
* @param type $tab
* @param type $strata
* @param type $id
* @param type $varname
* @param type $echo
* @return string
*/
function display_value_html($tab, $strata, $id, $varname, $echo = TRUE) {
$output = "";
if (isset($tab[$strata][$id][$varname]['value'])) { if (isset($tab[$strata][$id][$varname]['value'])) {
$v = $tab[$strata][$id][$varname]['value']; $v = $tab[$strata][$id][$varname]['value'];
$this->display_valueraw_html($v, $varname); $output .= $this->display_valueraw_html($v, $varname);
} else { } else {
echo "<em>"._("None defined")."</em>"; $output .= "<em>"._("None defined")."</em>";
}
if( $echo){
echo $output;
}
return $output;
} }
} /**
* return hashtable with variable_name => comment for all the vars
// return hashtable with variable_name => comment for all the vars *
* @global type $db
* @return type
*/
function variables_list_name() { function variables_list_name() {
global $db; global $db;
@ -332,7 +399,12 @@ class m_variables {
return $t; return $t;
} }
// return a multidimensionnal array used to build vars /**
* return a multidimensionnal array used to build vars
*
* @global type $db
* @return type
*/
function variables_list() { function variables_list() {
global $db; global $db;
if ( ! $this->cache_variable_list ) { if ( ! $this->cache_variable_list ) {
@ -357,4 +429,3 @@ class m_variables {
} }
} /* Class m_variables */ } /* Class m_variables */
?>