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

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

Fix bug on plugin update

  • Property svn:eol-style set to LF
File size: 3.2 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    $db_comment = sprintf($this->config_values[CE_CFG_COMMENT],
87                          $this->db_key,
88                          $this->getVersion());
89    $query = '
90        REPLACE INTO '.CONFIG_TABLE.'
91        VALUES(
92          \''. $this->db_key .'\',
93          \''.serialize($this->config_values).'\',
94          \''. $db_comment . '\')
95        ;';
96    $result = pwg_query($query);
97    if($result) {
98      return true;
99    } else {
100      return false;
101    }
102  }
103
104  /* ************************ */
105  /* ** Maintain  methods  ** */
106  /* ************************ */
107
108  static function install($plugin_id, $default_config = array()) {
109    $config = new CE_Config($default_config);
110    $config->setDBKey($plugin_id);
111    $config->loadConfig();
112    if (isset($default_config[CE_CFG_VERSION])) {
113      // Override version
114      $config->config_values[CE_CFG_VERSION] = $default_config[CE_CFG_VERSION];
115    }
116    if (isset($default_config[CE_CFG_COMMENT])) {
117      // Override comment
118      $config->config_values[CE_CFG_COMMENT] = $default_config[CE_CFG_COMMENT];
119    }
120    $result = $config->saveConfig();
121    return $result;
122  }
123
124  static function uninstall($plugin_id) {
125    $query = '
126        DELETE FROM '.CONFIG_TABLE.'
127        WHERE param = \'' . $plugin_id . '\'
128        ;';
129    $result = pwg_query($query);
130    if($result) {
131      return true;
132    } else {
133      return false;
134    }
135  }
136}
137?>
Note: See TracBrowser for help on using the repository browser.