1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: class Beatnik_Driver_sql extends Beatnik_Driver
13: {
14: 15: 16: 17: 18:
19: var $_params = array();
20:
21: 22: 23: 24: 25:
26: var $_db;
27:
28: 29: 30: 31: 32: 33:
34: var $_write_db;
35:
36: 37: 38: 39: 40:
41: var $_connected = false;
42:
43: 44: 45: 46: 47:
48: function Beatnik_Driver_sql($params = array())
49: {
50: parent::Beatnik_Driver($params);
51: $this->_connect();
52: }
53:
54: 55: 56: 57: 58:
59: function getRecDriverTypes()
60: {
61: return array();
62: }
63:
64:
65: 66: 67: 68: 69: 70: 71:
72: function getRecDriverFields($type) {
73:
74: return array();
75: }
76:
77: 78: 79: 80: 81: 82: 83:
84: function _getDomains()
85: {
86: $query = 'SELECT * FROM beatnik_soa ORDER BY zonename';
87: return $this->_db->getAll($query, null, DB_FETCHMODE_ASSOC);
88: }
89:
90: 91: 92: 93: 94: 95: 96:
97: function getDomain($domainname)
98: {
99: $query = 'SELECT * FROM beatnik_soa WHERE zonename = ? ORDER BY zonename';
100: return $this->_db->getRow($query, array($domainname), DB_FETCHMODE_ASSOC);
101: }
102:
103: 104: 105: 106: 107: 108: 109:
110: function getRecords($domain)
111: {
112: $zonedata = array();
113: $params = array($domain);
114:
115: foreach (array_keys(Beatnik::getRecTypes()) as $type) {
116: if ($type == 'soa') {
117: continue;
118: }
119: if ($type == 'mx') {
120: $order = 'pointer';
121: } else {
122: $order = 'hostname';
123: }
124:
125: $query = 'SELECT * FROM beatnik_' . $type . ' WHERE zonename = ? ORDER BY ' . $order . ' ASC';
126: $result = $this->_db->getAll($query, $params, DB_FETCHMODE_ASSOC);
127: if (is_a($result, 'PEAR_Error') || empty($result)) {
128: continue;
129: }
130:
131: $zonedata[$type] = $result;
132: }
133:
134: return $zonedata;
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145:
146: function _saveRecord($info)
147: {
148: $fields = array_keys(Beatnik::getRecFields($info['rectype']));
149: $params = array();
150: foreach ($fields as $i => $key) {
151: if (!isset($info[$key])) {
152: unset($fields[$i]);
153: continue;
154: }
155: $params[$key] = $info[$key];
156: }
157:
158: if (isset($params['id']) && $params['id']) {
159: unset($params['id'], $fields[0]);
160: $query = 'UPDATE beatnik_' . $info['rectype'] . ' SET ';
161: foreach ($fields as $key) {
162: $query .= $key . ' = ?, ';
163: $params[$key] = $info[$key];
164: }
165: $query = substr($query, 0, -2) . ' WHERE id = ?';
166: $params['id'] = $info['id'];
167: } else {
168: unset($params['id'], $fields[0]);
169: if ($info['rectype'] != 'soa') {
170: $fields[] = 'zonename';
171: $params['zonename'] = $_SESSION['beatnik']['curdomain']['zonename'];
172: }
173: $query = 'INSERT INTO beatnik_' . $info['rectype'] . ' (' . implode(', ', $fields) . ') ' .
174: ' VALUES (' . substr(str_repeat('?, ', sizeof($params)), 0, -2) . ')';
175: }
176:
177: return $this->_write_db->query($query, $params);
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187: 188:
189: function _deleteRecord($data)
190: {
191:
192: if ($data['rectype'] != 'soa') {
193: return $this->_write_db->query('DELETE FROM beatnik_' . $data['rectype'] . ' WHERE id = ?', array($data['id']));
194: }
195:
196:
197: $params = array($data['curdomain']);
198: foreach (array_keys(Beatnik::getRecTypes()) as $type) {
199: if ($type == 'soa') {
200: continue;
201: }
202: $result = $this->_write_db->query('DELETE FROM beatnik_' . $type . ' WHERE zonename = ?', $params);
203: if (is_a($result, 'PEAR_Error')) {
204: return $result;
205: }
206: }
207:
208:
209: return $this->_write_db->query('DELETE FROM beatnik_soa WHERE zonename = ?', $params);
210: }
211:
212: 213: 214: 215: 216: 217: 218:
219: function _connect()
220: {
221: if ($this->_connected) {
222: return true;
223: }
224:
225: $this->_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('read', 'beatnik', 'storage');
226: $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('rw', 'beatnik', 'storage');
227:
228: return true;
229: }
230: }
231: