import mtweb.0.4.1
[mtweb] / web / app / data / modules / sql / mw_data_config.php
1 <?php
2
3   class mw_data_config extends mw_data
4   {
5
6     function config($key = null)
7     { $sgbd = $this->sgbd();
8       $value = false;
9       if(isset($key))
10       { $sql =
11          "SELECT `value` FROM #--config"
12         ." WHERE `key`=".$this->eq($key);
13         $rst = $sgbd->query($sql);
14         if(!isset($rst)) return false;
15         if($v_rst = $sgbd->fetch_assoc($rst)) $value = $v_rst["value"];
16         else $value = "";
17         $sgbd->free_result($rst);
18       }
19       else
20       { $value = array();
21         $sql =
22          "SELECT * FROM #--config";
23         $rst = $sgbd->query($sql);
24         if(!isset($rst)) return false;
25         while($v_rst = $sgbd->fetch_assoc($rst)) $value[$v_rst["key"]] = $v_rst["value"];
26         $sgbd->free_result($rst);
27       }
28       return $value;
29     }
30
31     function config_exists($key)
32     { $sgbd = $this->sgbd();
33       $exists = false;
34       $sql = "SELECT count(*) as n FROM #--config"
35       ." WHERE `key`=".$this->eq($key);
36       $rst = $sgbd->query($sql);
37       if(!isset($rst)) return false;
38       if($v_rst = $sgbd->fetch_assoc($rst)) $exists = $v_rst["n"];
39       $sgbd->free_result($rst);
40       return $exists;
41     }
42
43     function set_config($key, $value)
44     { $sgbd = $this->sgbd();
45       if($this->config_exists($key)) $sql =
46        "UPDATE #--config"
47       ." SET `value`=".$this->eq($value)
48       ." WHERE `key`=".$this->eq($key);
49       else $sql =
50        "INSERT INTO #--config"
51       ." VALUES(NULL, ".$this->eq($key).", ".$this->eq($value).")";
52       $rst = $sgbd->query($sql);
53       if(!isset($rst)) return false;
54       return true;
55     }
56
57     function del_config($key)
58     { $sgbd = $this->sgbd();
59       return $sgbd->query("DELETE FROM #--config WHERE `key`=".$this->eq($key)) ? true : false;
60     }
61
62   }
63
64 ?>