From ee832a93868b6895fb2c001931bdb7c60da32a83 Mon Sep 17 00:00:00 2001 From: Alan Garcia Date: Fri, 31 Jan 2014 13:50:15 +0000 Subject: [PATCH] =?UTF-8?q?On=20peux=20stocker=20et=20=C3=A9diter=20des=20?= =?UTF-8?q?tableau=20dans=20la=20table=20des=20variables=20!=20Et=20meme?= =?UTF-8?q?=20qu'on=20peut=20mettre=20un=20label=20!=20Yepee=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bureau/admin/adm_var_edit.php | 61 ++++++++++++---- bureau/admin/adm_variables.php | 22 +++--- bureau/class/functions.php | 4 +- bureau/class/m_variables.php | 124 +++++++++++++++++++++------------ install/mysql.sql | 1 + install/upgrades/3.3.0~a.sql | 1 + 6 files changed, 141 insertions(+), 72 deletions(-) diff --git a/bureau/admin/adm_var_edit.php b/bureau/admin/adm_var_edit.php index e0ecbbdc..fd75aafe 100644 --- a/bureau/admin/adm_var_edit.php +++ b/bureau/admin/adm_var_edit.php @@ -13,6 +13,7 @@ $fields = array ( "var" => array ("get", "string", null), "var_id" => array ("post", "integer", null), "var_value" => array ("post", "string", null), + "var_value_arr" => array ("post", "array", null), "var_name" => array ("post", "string", null), "strata" => array ("post", "string", null), "strata_id" => array ("post", "integer", null), @@ -20,17 +21,27 @@ $fields = array ( ); getFields($fields); + +variable_get("aaa_test2", array("ns"=>"", "enabled"=>""), "This is a test!", array("ns"=>"ns name", "ip"=>"ip address", "enabled"=>"enabled")); + + if (empty($var)) { echo "

";__("Missing var name");echo "

"; include_once("foot.php"); } +// Which one between var_value and var_value_arr ? +$var_v = null; +if (!is_null($var_value)) $var_v = $var_value; +if (!is_null($var_value_arr)) $var_v = $var_value_arr; + + if ( $var_id && $delete ) { $variables->del($var_id); -} else if ( $strata && $var_name && $var_value ) { - $variables->variable_update_or_create($var_name, $var_value, $strata, $strata_id); -} else if ( $var_id && $var_value ) { - $variables->variable_update_or_create($var_name, $var_value, null, null, $var_id); +} else if ( $strata && $var_name && $var_v ) { + $variables->variable_update_or_create($var_name, $var_v, $strata, $strata_id); +} else if ( $var_id && $var_v ) { + $variables->variable_update_or_create($var_name, $var_v, null, null, $var_id); } echo "

";echo sprintf(_("Edition of var %s"), $var); echo "

"; @@ -57,9 +68,22 @@ echo ""; echo "
"; function edit_var($var_arr) { + global $allvars; echo "
"; echo ""; - echo ""; + if (is_array( $allvars['DEFAULT'][null][$var_arr['name']]['type'] )) { + echo "
    "; + foreach ($allvars['DEFAULT'][null][$var_arr['name']]['type'] as $kk => $vv) { + echo "
  • "; + echo ""; + echo ""; + echo "
  • "; + } + echo "
"; + } else { + echo ""; + } + echo "
"; echo ""; echo ""; @@ -70,7 +94,7 @@ function edit_var($var_arr) { } function add_var($stratatata, $stratatata_arr=null) { - global $var; + global $var, $allvars; echo "
"; echo ""; echo ""; @@ -79,7 +103,18 @@ function add_var($stratatata, $stratatata_arr=null) { eoption($stratatata_arr, null); echo " "; } - echo ""; + if (is_array( $allvars['DEFAULT'][null][$var]['type'] )) { + echo "
    "; + foreach ($allvars['DEFAULT'][null][$var]['type'] as $kk => $vv) { + echo "
  • "; + echo ""; + echo ""; + echo "
  • "; + } + echo "
"; + } else { + echo ""; + } echo "
"; echo ""; echo ""; @@ -95,12 +130,12 @@ foreach ( $variables->strata_order as $strata) { echo ""; __($strata); echo ""; switch($strata) { case 'DEFAULT': - echo "".$allvars['DEFAULT'][null][$var]['value'].""; + echo ""; $variables->display_value_html($allvars, 'DEFAULT', null, $var); echo ""; break; case 'GLOBAL': echo ""; if ( isset($allvars['GLOBAL'][null][$var]) && is_array($allvars['GLOBAL'][null][$var])){ - echo "".$allvars['GLOBAL'][null][$var]['value'].""; + echo ""; $variables->display_value_html($allvars, 'GLOBAL', null, $var); echo ""; edit_var($allvars['GLOBAL'][null][$var]); } else { echo ""._("Add").""; @@ -114,7 +149,7 @@ foreach ( $variables->strata_order as $strata) { foreach ($allvars['FQDN_CREATOR'] as $ttk => $ttv ) { if ( isset($ttv[$var]) && is_array( $ttv[$var])) { echo sprintf(_("Overwritted by %s"), $members[$ttk]['login'])." → "; - echo "".$ttv[$var]['value'].""; + echo ""; echo $variables->display_valueraw_html($ttv[$var]['value'], $var);echo ""; edit_var($ttv[$var]); } echo "
"; @@ -130,7 +165,7 @@ foreach ( $variables->strata_order as $strata) { foreach ($allvars['FQDN'] as $ttk => $ttv ) { if ( isset($ttv[$var]) && is_array( $ttv[$var])) { echo sprintf(_("Overwritted by %s"), $panel_url[$ttk])." → "; - echo "".$ttv[$var]['value'].""; + echo ""; echo $variables->display_valueraw_html($ttv[$var]['value'], $var);echo ""; edit_var($ttv[$var]); } echo "
"; @@ -146,7 +181,7 @@ foreach ( $variables->strata_order as $strata) { foreach ($allvars['CREATOR'] as $ttk => $ttv ) { if ( isset($ttv[$var]) && is_array( $ttv[$var])) { echo sprintf(_("Overwritted by %s"), $members[$ttk]['login'])." → "; - echo "".$ttv[$var]['value'].""; + echo ""; echo $variables->display_valueraw_html($ttv[$var]['value'], $var);echo ""; edit_var($ttv[$var]); } echo "
"; @@ -162,7 +197,7 @@ foreach ( $variables->strata_order as $strata) { foreach ($allvars['MEMBER'] as $ttk => $ttv ) { if ( isset($ttv[$var]) && is_array( $ttv[$var])) { echo sprintf(_("Overwritted by %s"), $members[$ttk]['login'])." → "; - echo "".$ttv[$var]['value'].""; + echo ""; echo $variables->display_valueraw_html($ttv[$var]['value'], $var);echo ""; edit_var($ttv[$var]); } echo "
"; diff --git a/bureau/admin/adm_variables.php b/bureau/admin/adm_variables.php index 4ed1a40c..6c2704a6 100644 --- a/bureau/admin/adm_variables.php +++ b/bureau/admin/adm_variables.php @@ -69,9 +69,9 @@ foreach( $variables->variables_list_name() as $varname => $varcomment) { ?> - - - + display_value_html($allvars, 'DEFAULT', NULL, $varname);?> + display_value_html($allvars, 'GLOBAL', NULL, $varname);?> + display_valueraw_html($global_conf[$varname]['value'], $varname); } ?> @@ -117,14 +117,14 @@ echo ""; foreach( $variables->variables_list_name() as $varname => $varcomment) { ?> - - - - - - - - + display_value_html($allvars, 'DEFAULT', NULL, $varname); ?> + display_value_html($allvars, 'GLOBAL', NULL, $varname); ?> + display_value_html($allvars, 'FQDN_CREATOR', $sub_infos['member_id'], $varname); ?> + display_value_html($allvars, 'FQDN', $sub_infos['id'], $varname); ?> + display_value_html($allvars, 'CREATOR', $creator, $varname); ?> + display_value_html($allvars, 'MEMBER', $member_id, $varname); ?> + display_value_html($allvars, 'DOMAIN', 'FIXME', $varname); ?> + display_valueraw_html($impersonated_conf[$varname]['value'], $varname); ?> variable_get($name, $default, $createit_comment); + return $variables->variable_get($name, $default, $createit_comment, $type); } /* diff --git a/bureau/class/m_variables.php b/bureau/class/m_variables.php index 13bdd426..a88799c8 100644 --- a/bureau/class/m_variables.php +++ b/bureau/class/m_variables.php @@ -35,6 +35,7 @@ class m_variables { var $strata_order = array('DEFAULT','GLOBAL','FQDN_CREATOR','FQDN','CREATOR','MEMBER','DOMAIN'); + var $cache_variable_list = false; // used by get_impersonated to merge array. Son value overwrite father's value private function variable_merge($father, $son) { @@ -59,6 +60,8 @@ class m_variables { } else { $mid = null; } + + // In case we launch it in a script, there is no $_SERVER if (isset($_SERVER['HTTP_HOST'])) { $host=$_SERVER['HTTP_HOST']; } else { @@ -67,7 +70,15 @@ class m_variables { return $this->get_impersonated($host, $mid); } - + /** + * Return the var for a specific environnement : + * * logged via $fqdn url + * * the user is $uid + * * $var if we want only 1 var instead of all of them + * + * If $fqdn and $uid aren't specified, return the default value + * + */ function get_impersonated($fqdn=null, $uid=null, $var=null) { global $db, $err; @@ -140,6 +151,7 @@ class m_variables { function variable_init_maybe($force=false) { global $conf; if ($force || !isset($conf)) { + $this->cache_variable_list = false; $conf = $this->variable_init(); } } @@ -159,7 +171,7 @@ class m_variables { * @global $conf * A cache of the configuration. */ - function variable_get($name, $default = null, $createit_comment = null) { + function variable_get($name, $default = null, $createit_comment = null, $type=null) { global $conf; $this->variable_init_maybe(); @@ -167,48 +179,21 @@ class m_variables { if (isset($conf[$name])) { return $conf[$name]['value']; } elseif (!is_null($createit_comment)) { - $this->variable_update_or_create($name, $default, 'DEFAULT', 'null', 'null', $createit_comment); + $this->variable_update_or_create($name, $default, 'DEFAULT', 'null', 'null', $createit_comment, $type); } return $default; } - /** - * Set a persistent variable. - * - * @param $name - * The name of the variable to set. - * @param $value - * The value to set. This can be any PHP data type; these functions take care - * of serialization as necessary. - */ - function variable_set($name, $value, $comment=null) { - global $conf, $db, $err; - $err->log('variable', 'variable_set', '+'.serialize($value).'+'.$comment.'+'); - - $conf[$name] = $value; - if (is_object($value) || is_array($value)) { - $value = serialize($value); - } - - if ( empty($comment) ) { - $query = "INSERT INTO variable (name, value) values ('".$name."', '".$value."') on duplicate key update name='$name', value='$value';"; - } else { - $comment=mysql_real_escape_string($comment); - $query = "INSERT INTO variable (name, value, comment) values ('".$name."', '".$value."', '$comment') on duplicate key update name='$name', value='$value', comment='$comment';"; - } - - # $db->query("$query"); - printvar($query); - - $this->variable_init(); - } - - function variable_update_or_create($var_name, $var_value, $strata=null, $strata_id=null, $var_id=null, $comment=null) { + // Create or update a variable. + function variable_update_or_create($var_name, $var_value, $strata=null, $strata_id=null, $var_id=null, $comment=null, $type=null) { global $db, $err; $err->log('variable', 'variable_update_or_create'); if ( strtolower($var_id) == 'null' ) $var_id = null; if ( strtolower($strata_id) == 'null' ) $strata_id = null; + if (is_object($type) || is_array($type)) { + $type = serialize($type); + } if (is_object($var_value) || is_array($var_value)) { $var_value = serialize($var_value); } @@ -221,13 +206,14 @@ class m_variables { return false; } $sql="INSERT INTO - variable (name, value, strata, strata_id, comment) + variable (name, value, strata, strata_id, comment, type) VALUES ( '".mysql_real_escape_string($var_name)."', '".mysql_real_escape_string($var_value)."', '".mysql_real_escape_string($strata)."', ".( is_null($strata_id)?'NULL':"'".mysql_real_escape_string($strata_id)."'").", - '".mysql_real_escape_string($comment)."' );"; + '".mysql_real_escape_string($comment)."', + '".mysql_real_escape_string($type)."' );"; } $db->query("$sql"); @@ -248,6 +234,45 @@ class m_variables { $this->variable_init_maybe(true); } + // echo HTML code to display a variable passed in parameters + function display_valueraw_html($v,$varname) { + if (is_array($v)) { + if (empty($v)) { + echo ""._("Empty array").""; + } else { + echo "
    "; + foreach ( $v as $k=>$l) { + echo "
  • "; + if (! is_numeric($k)) { + if (is_null($varname)) { + echo "$k =>"; + } else { + echo $this->variables_list()['DEFAULT'][null][$varname]['type'][$k]. " => "; + } + } + echo "$l
  • "; + } + echo "
"; + } // empty $v + } else if (empty($v)) { + echo ""._("Empty").""; + } else { + echo $v; + } + } + + // Display a variable if is set + function display_value_html($tab, $strata, $id, $varname) { + if (isset($tab[$strata][$id][$varname]['value'])) { + $v = $tab[$strata][$id][$varname]['value']; + $this->display_valueraw_html($v, $varname); + } else { + echo ""._("None defined").""; + } + + } + + // return hashtable with variable_name => comment for all the vars function variables_list_name() { global $db; @@ -263,21 +288,28 @@ class m_variables { return $t; } + // return a multidimensionnal array used to build vars function variables_list() { global $db; + if ( ! $this->cache_variable_list ) { - $result = $db->query('SELECT * FROM `variable`'); + $result = $db->query('SELECT * FROM `variable`'); - $arr_var=array(); - while ($db->next_record($result)) { - // Unserialize value if needed - if ( ($value = @unserialize($db->f('value'))) === FALSE) { - $value=$db->f('value'); + $arr_var=array(); + while ($db->next_record($result)) { + // Unserialize value if needed + if ( ($value = @unserialize($db->f('value'))) === FALSE) { + $value=$db->f('value'); + } + if ( ($type = @unserialize($db->f('type'))) === FALSE) { + $type=$db->f('type'); + } + $arr_var[$db->f('strata')][$db->f('strata_id')][$db->f('name')] = array('id'=>$db->f('id') ,'name'=>$db->f('name'), 'value'=>$value, 'comment'=>$db->f('comment'), 'type'=>$type); } - $arr_var[$db->f('strata')][$db->f('strata_id')][$db->f('name')] = array('id'=>$db->f('id') ,'name'=>$db->f('name'), 'value'=>$value, 'comment'=>$db->f('comment')); + $this->cache_variable_list = $arr_var; } - - return $arr_var; + + return $this->cache_variable_list; } } /* Class m_variables */ diff --git a/install/mysql.sql b/install/mysql.sql index 3636306f..61d86e54 100644 --- a/install/mysql.sql +++ b/install/mysql.sql @@ -393,6 +393,7 @@ CREATE TABLE `variable` ( `comment` mediumtext, `strata` enum('DEFAULT','GLOBAL','FQDN','FQDN_CREATOR','CREATOR','MEMBER','DOMAIN') NOT NULL DEFAULT 'DEFAULT', `strata_id` bigint(20) DEFAULT NULL, + `type` text, PRIMARY KEY (`id`), UNIQUE KEY `name_2` (`name`,`strata`,`strata_id`), KEY `name` (`name`) diff --git a/install/upgrades/3.3.0~a.sql b/install/upgrades/3.3.0~a.sql index 806617d5..e389487d 100644 --- a/install/upgrades/3.3.0~a.sql +++ b/install/upgrades/3.3.0~a.sql @@ -3,6 +3,7 @@ ALTER TABLE variable DROP PRIMARY KEY; ALTER TABLE variable ADD id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST; ALTER TABLE variable ADD strata enum('DEFAULT','GLOBAL','FQDN', 'FQDN_CREATOR', 'CREATOR', 'MEMBER', 'DOMAIN') NOT NULL DEFAULT 'DEFAULT'; ALTER TABLE variable ADD strata_id bigint DEFAULT NULL; +ALTER TABLE variable ADD type text DEFAULT ''; ALTER TABLE variable ADD UNIQUE (name, strata, strata_id);