summaryrefslogtreecommitdiff
path: root/manager/library/config/Config_Writer_Db.php
blob: 132c456952a29137073b989e77cce06e16bc0e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Config
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Config_Writer_Db.php 43 2009-12-21 14:12:34Z markus $
 */

/**
 * Usage:
 * require_once(LIBRARY_PATH . '/config/Config_Writer_Db.php');
 * $writer = new Config_Writer_Db();
 * $writer->setTableName('system_config');
 * $writer->write(Zend_Registry::get('config_dbc'), Zend_Registry::get('config'));
 *
 * $writer = new Config_Writer_Db();
 * $writer->setTableName('dnssec_org_param');
 * $writer->write(Zend_Registry::get('config_dbc'), dnssec_org_conf, 'dnssec_org_id="2"');
 */

/**
 * @see Zend_Config_Writer
 */
require_once 'Zend/Config/Writer.php';

/**
 * @category   Zend
 * @package    Zend_Config
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Config_Writer_Db extends Zend_Config_Writer
{
    /**
     * String that separates nesting levels of configuration data identifiers
     *
     * @var string
     */
    protected $_nestSeparator = '.';

    protected $_set = null;

    protected $_tableName = null;

    /**
     * Set the nest separator
     *
     * @param  string $filename
     * @return Zend_Config_Writer_Ini
     */
    public function setNestSeparator($separator)
    {
        $this->_nestSeparator = $separator;

        return $this;
    }

    public function setTableName($name)
    {
        $this->_tableName = $name;

        return $this;
    }

    /**
     * Defined by Zend_Config_Writer
     *
     * use set to limit impact when a shared config file is used (i.e. config per item using foreign keys)
     *
     * @param  string $filename
     * @param  Config_Db $config
     * @param  string $set
     * @return void
     */
    public function write($db = null, $config = null, $set = null) {
    	$this->_set = $set;

    	// this method is specialized for writing back Config objects (which hold config_db objects)
        if ($config !== null) {
        	if ($config instanceof Config)
            	$this->setConfig($config->getConfig());
            else {
            	$this->setConfig($config);
            }
        }

        if ($this->_config === null) {
            require_once 'Zend/Config/Exception.php';
            throw new Zend_Config_Exception('No config was set');
        }

        if ($db === null) {
            require_once 'Zend/Config/Exception.php';
            throw new Zend_Config_Exception('No db was set');
        }

        $sql = array();

        $string = 'delete from ' . $this->_tableName;
        if ($this->_set !== null) {
			$string .= ' where ' . $this->_set;
        }

		$sql[] = $string;

        $iniString   = '';
        $extends     = $this->_config->getExtends();
        $sectionName = $this->_config->getSectionName();

        foreach ($this->_config as $key => $data) {
     		$sql= array_merge($sql, $this->addEntry($sectionName, $key, $data));
	    }

	    try {
	    	$db->beginTransaction();
    		foreach ($sql as $command) {
    			#Log::Log()->debug($command);
	    		$db->query($command);
			}
			$db->commit();
	    } catch (Exception $e) {
			$db->rollBack();
			Log::Log()->err($e);
			throw $e;
	    }
	}

	/**
	 * build key value pairs, key is created by recursively adding section names, delimited by "."
	 * @param string $prefix
	 * @param string $key
	 * @param mixed $data
	 */
    protected function addEntry($prefix, $key, $data) {
    	$sql = array();

    	if ($data instanceof Zend_Config) {
			if ($prefix != '')
    			$prefix .= '.';
    		$prefix .= $key;
    		foreach ($data as $k => $v) {
    			$sql = array_merge($sql, $this->addEntry($prefix, $k, $v));
    		}
    	}
    	else {
    		$string = 'insert into ' . $this->_tableName . ' set ';
    		$pkey = $prefix;
    		if ($pkey != '')
    			$pkey .= '.';
    		$pkey .= $key;
    		$string .= 'config_key=' . $this->_prepareValue($pkey) . ', ';
    		$string .= 'config_value=' . $this->_prepareValue($data);
    		if ($this->_set !== null)
    			$string .= ', ' . $this->_set;

    		$sql[] = $string;
    	}

    	return $sql;
	}

    /**
     * Add a branch to an INI string recursively
     *
     * @param  Zend_Config $config
     * @return void
     */
    protected function _addBranch(Zend_Config $config, $parents = array())
    {
        $iniString = '';

        foreach ($config as $key => $value) {
            $group = array_merge($parents, array($key));

            if ($value instanceof Zend_Config) {
                $iniString .= $this->_addBranch($value, $group);
            } else {
                $iniString .= implode($this->_nestSeparator, $group)
                           .  ' = '
                           .  $this->_prepareValue($value)
                           .  "\n";
            }
        }

        return $iniString;
    }

    /**
     * Prepare a value for INI
     *
     * @param  mixed $value
     * @return string
     */
    protected function _prepareValue($value)
    {
        if (is_integer($value) || is_float($value)) {
            return $value;
        } elseif (is_bool($value)) {
            return ($value ? 'true' : 'false');
        } else {
            return '"' . addslashes($value) .  '"';
        }
    }
}