source: extensions/ContactForm/classes/cf_config.class.php @ 6547

Last change on this file since 6547 was 6547, checked in by Gotcha, 14 years ago

v1.0.9

File size: 4.4 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/**
5 * CF_Config class
6 */
7class CF_Config {
8  static $default_config = array();
9 
10  protected $config_values;
11  protected $db_key = null;
12  protected $db_comment = null;
13  protected $config_lang;
14 
15  /* ************************ */
16  /* ** Constructor        ** */
17  /* ************************ */
18
19  function CF_Config() {
20  }
21 
22  /* ************************ */
23  /* ** Accessors          ** */
24  /* ************************ */
25 
26  function get_value($key) {
27      if (isset($this->config_values[$key])) {
28          return $this->config_values[$key];
29      }
30      return null;
31  }
32
33  function set_value($key, $value) {
34      $this->config_values[$key] = $value;
35  }
36
37  function get_version() {
38    if (isset($this->config_values[CF_CFG_VERSION])) {
39      return $this->config_values[CF_CFG_VERSION];
40    }
41    return CF_VERSION;
42  }
43
44  function set_db_key($key) {
45    $this->db_key = $key;
46  }
47 
48  function get_config_lang() {
49    return $this->config_lang;
50  }
51 
52  function get_lang_value($item, $language=null) {
53    if (null == $language) {
54      global $user;
55      $language = $user['language'];
56    }
57    $value = $this->config_lang->get_value($language, $item);
58    if (empty($value)) {
59      cf_switch_to_default_lang();
60      $value = l10n($item);
61      cf_switch_back_to_user_lang();
62    }
63    return $value;
64  }
65 
66  /* ************************ */
67  /* ** Loading methods    ** */
68  /* ************************ */
69 
70  function load_config() {
71    $this->config_lang = null;
72    if (null != $this->db_key) {
73      $query = '
74          SELECT value
75          FROM '.CONFIG_TABLE.'
76          WHERE param = \''. $this->db_key .'\'
77          ;';
78      $result = pwg_query($query);
79      if($result) {
80        $row = mysql_fetch_row($result);
81        if(is_string($row[0])) {
82          $this->config_values = unserialize($row[0]);
83          if (isset($this->config_values['config_lang'])) {
84            $this->config_lang = $this->config_values['config_lang'];
85            $this->config_values['config_lang'] = null;
86          }
87        }
88      }
89    }
90//    CF_Log::add_debug($this->config_lang, 'CF_Config::load_config');
91    $this->load_default_config();
92  }
93 
94  protected function load_default_config() {
95    if (null == $this->config_lang) {
96      $this->config_lang = new CF_Config_Lang();
97      $this->config_values['config_lang'] = null;
98      CF_Log::add_debug($this->config_lang,'CF_Config::load_default_config');
99    }
100    $this->config_lang->set_default_values();
101    $this->config_lang->update_keys();
102    foreach (CF_Config::$default_config as $key => $value) {
103      if (!isset($this->config_values[$key])) {
104        $this->config_values[$key] = $value;
105      }
106    }
107  }
108 
109  /* ************************ */
110  /* ** Saving  methods    ** */
111  /* ************************ */
112
113  function save_config() {
114    if (null == $this->db_key) {
115      return false;
116    }
117    $this->config_values['config_lang'] = $this->config_lang;
118    if (!isset($this->config_values[CF_CFG_COMMENT])) {
119      $this->set_value(CF_CFG_COMMENT, CF_CFG_DB_COMMENT);
120    }
121    $db_comment = sprintf($this->config_values[CF_CFG_COMMENT],
122                          $this->db_key,
123                          $this->get_version());
124    $query = '
125        REPLACE INTO '.CONFIG_TABLE.'
126        VALUES(
127          \''. $this->db_key .'\',
128          \''.serialize($this->config_values).'\',
129          \''. $db_comment . '\')
130        ;';
131    $result = pwg_query($query);
132    if($result) {
133      return true;
134    } else {
135      return false;
136    }
137  }
138 
139  /* ************************ */
140  /* ** Maintain  methods  ** */
141  /* ************************ */
142 
143  static function install($plugin_id) {
144    $config = new CF_Config();
145    $config->set_db_key($plugin_id);
146    $config->load_config();
147    $default_config = CF_Config::$default_config;
148    if (isset($default_config[CF_CFG_VERSION])) {
149      // Override version
150      $config->set_value(CF_CFG_VERSION, $default_config[CF_CFG_VERSION]);
151    }
152    if (isset($default_config[CF_CFG_COMMENT])) {
153      // Override comment
154      $config->set_value(CF_CFG_COMMENT, $default_config[CF_CFG_COMMENT]);
155    }
156    $result = $config->save_config();
157    return $result;
158  }
159
160  static function uninstall($plugin_id) {
161    $query = '
162        DELETE FROM '.CONFIG_TABLE.'
163        WHERE param = \'' . $plugin_id . '\'
164        ;';
165    $result = pwg_query($query);
166    if($result) {
167      return true;
168    } else {
169      return false;
170    }
171  } 
172}
173?>
Note: See TracBrowser for help on using the repository browser.