source: extensions/CommentEditor/classes/ce_config.class.php @ 3486

Last change on this file since 3486 was 3482, checked in by Criss, 15 years ago

Fix another bug on plugin update...

  • Property svn:eol-style set to LF
File size: 3.3 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/**
5 * CE_Config class
6 */
7class CE_Config {
8
9  var $config_values;
10  var $default_config;
11  var $db_key = null;
12  var $db_comment = null;
13
14  /* ************************ */
15  /* ** Constructor        ** */
16  /* ************************ */
17
18  function CE_Config($default_config = array()) {
19    $this->default_config = $default_config;
20  }
21
22  /* ************************ */
23  /* ** Get/Set methods    ** */
24  /* ************************ */
25
26  function getValue($key) {
27      if (isset($this->config_values[$key])) {
28          return $this->config_values[$key];
29      }
30      return null;
31  }
32
33  function setValue($key, $value) {
34      $this->config_values[$key] = $value;
35  }
36
37  function getVersion() {
38    if (isset($this->config_values[CE_CFG_VERSION])) {
39      return $this->config_values[CE_CFG_VERSION];
40    }
41    return CE_VERSION;
42  }
43
44  function setDBKey($key) {
45    $this->db_key = $key;
46  }
47
48  /* ************************ */
49  /* ** Loading methods    ** */
50  /* ************************ */
51
52  function loadConfig() {
53    if (null != $this->db_key) {
54      $query = '
55          SELECT value
56          FROM '.CONFIG_TABLE.'
57          WHERE param = \''. $this->db_key .'\'
58          ;';
59      $result = pwg_query($query);
60      if($result) {
61        $row = mysql_fetch_row($result);
62        if(is_string($row[0])) {
63          $this->config_values = unserialize($row[0]);
64        }
65      }
66    }
67    $this->loadDefaultConfig();
68  }
69
70  private function loadDefaultConfig() {
71    foreach ($this->default_config as $key => $value) {
72      if (!isset($this->config_values[$key])) {
73        $this->config_values[$key] = $value;
74      }
75    }
76  }
77
78  /* ************************ */
79  /* ** Saving  methods    ** */
80  /* ************************ */
81
82  function saveConfig() {
83    if (null == $this->db_key) {
84      return false;
85    }
86
87    if (!isset($this->config_values[CE_CFG_COMMENT])) {
88      $this->setValue(CE_CFG_COMMENT, CE_CFG_DB_COMMENT);
89    }
90    $db_comment = sprintf($this->config_values[CE_CFG_COMMENT],
91                          $this->db_key,
92                          $this->getVersion());
93    $query = '
94        REPLACE INTO '.CONFIG_TABLE.'
95        VALUES(
96          \''. $this->db_key .'\',
97          \''.serialize($this->config_values).'\',
98          \''. $db_comment . '\')
99        ;';
100    $result = pwg_query($query);
101    if($result) {
102      return true;
103    } else {
104      return false;
105    }
106  }
107
108  /* ************************ */
109  /* ** Maintain  methods  ** */
110  /* ************************ */
111
112  static function install($plugin_id, $default_config = array()) {
113    $config = new CE_Config($default_config);
114    $config->setDBKey($plugin_id);
115    $config->loadConfig();
116    if (isset($default_config[CE_CFG_VERSION])) {
117      // Override version
118      $config->setValue(CE_CFG_VERSION, $default_config[CE_CFG_VERSION]);
119    }
120    if (isset($default_config[CE_CFG_COMMENT])) {
121      // Override comment
122      $config->setValue(CE_CFG_COMMENT, $default_config[CE_CFG_COMMENT]);
123    }
124    $result = $config->saveConfig();
125    return $result;
126  }
127
128  static function uninstall($plugin_id) {
129    $query = '
130        DELETE FROM '.CONFIG_TABLE.'
131        WHERE param = \'' . $plugin_id . '\'
132        ;';
133    $result = pwg_query($query);
134    if($result) {
135      return true;
136    } else {
137      return false;
138    }
139  }
140}
141?>
Note: See TracBrowser for help on using the repository browser.