98d4607caf5e80a83055616864377a09b82e8dc3
[mtweb] / web / app / data / modules / xml / 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($rst = $sgbd->open_data("config"))
10       { if(isset($key))
11         { while($v_rst = $sgbd->fetch_data($rst))
12           { if(isset($v_rst))
13             { if($v_rst["key"] == $key)
14               { $value = $v_rst["value"];
15               }
16             }
17             else $value = null;
18           }
19         }
20         else
21         { $value = array();
22           while($v_rst = $sgbd->fetch_data($rst))
23           { if(isset($v_rst))
24             { if(is_array($v_rst)) foreach($v_rst as $key => $_value)
25               { $value[$key] = $_value;
26                 break;
27               }
28             }
29             else $value = null;
30           }
31         }
32         $sgbd->close_data($rst);
33       }
34       if(!isset($value)) return false;
35       return $value;
36     }
37
38     function config_exists($key)
39     { $sgbd = $this->sgbd();
40       $exists = 0;
41       if($rst = $sgbd->open_data("config"))
42       { while($v_rst = $sgbd->fetch_data($rst))
43         { if(isset($v_rst))
44           { if(isset($v_rst[$key])) $exists++;
45           }
46           else
47           { $exists = false;
48             break;
49           }
50         }
51         $sgbd->close_data($rst);
52       }
53       else $exists = false;
54       return $exists;
55     }
56
57     function set_config($key, $value)
58     { $sgbd = $this->sgbd();
59       $FOUND = false;
60       if($rst = $sgbd->open_data("config"))
61       { while($v_rst = $sgbd->fetch_data($rst))
62         { if(isset($v_rst))
63           { if(array_key_exists($key, $v_rst))
64             { $FOUND = $sgbd->set_data("config", $v_rst["id"], array($key => $value));
65               break;
66             }
67           }
68           else
69           { $FOUND = null;
70             break;
71           }
72         }
73         $sgbd->close_data($rst);
74       }
75       else $FOUND = null;
76       if(isset($FOUND))
77       { if($FOUND) return true;
78         else
79         { if($sgbd->add_data("config", array($key => $value))) return true;
80         }
81       }
82       return false;
83     }
84
85     function del_config($key)
86     { $ids = array();
87       $sgbd = $this->sgbd();
88       if($rst = $sgbd->open_data("config"))
89       { while($v_rst = $sgbd->fetch_data($rst))
90         { if(isset($v_rst))
91           { if(isset($v_rst[$key]) && isset($v_rst["id"]))
92             { $ids[] = $v_rst["id"];
93             }
94           }
95           else $ids = false;
96         }
97         $sgbd->close_data($rst);
98       }
99       if($ids === false) return false;
100       foreach($ids as $id) if(!$sgbd->del_data("config", $id)) return false;
101       return true;
102     }
103
104   }
105
106 ?>