1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class Turba_Driver_Prefs extends Turba_Driver
17: {
18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: protected function _search(array $criteria, array $fields, array $blobFields = array(), $count_only = false)
29: {
30: return $count_only ? count($this->_getAddressBook()) : array_values($this->_getAddressBook());
31: }
32:
33: 34: 35: 36:
37: protected function _read($key, $ids, $owner, array $fields,
38: array $blobFields = array())
39: {
40: $book = $this->_getAddressBook();
41:
42: $results = array();
43: if (!is_array($ids)) {
44: $ids = array($ids);
45: }
46:
47: foreach ($ids as $id) {
48: if (isset($book[$id])) {
49: $results[] = $book[$id];
50: }
51: }
52:
53: return $results;
54: }
55:
56: 57: 58: 59: 60: 61: 62: 63:
64: protected function _add(array $attributes, array $blob_fields = array())
65: {
66: $book = $this->_getAddressBook();
67: $book[$attributes['id']] = $attributes;
68: $this->_setAddressbook($book);
69: }
70:
71: 72: 73:
74: protected function _canAdd()
75: {
76: return true;
77: }
78:
79: 80: 81: 82: 83: 84:
85: protected function _delete($object_key, $object_id)
86: {
87: $book = $this->_getAddressBook();
88: unset($book[$object_id]);
89: $this->_setAddressbook($book);
90: }
91:
92: 93: 94: 95: 96:
97: function _save($object)
98: {
99: list($object_key, $object_id) = each($this->toDriverKeys(array('__key' => $object->getValue('__key'))));
100: $attributes = $this->toDriverKeys($object->getAttributes());
101:
102: $book = $this->_getAddressBook();
103: $book[$object_id] = $attributes;
104: $this->_setAddressBook($book);
105: }
106:
107: 108: 109: 110: 111:
112: protected function _getAddressBook()
113: {
114: global $prefs;
115:
116: $val = $prefs->getValue('prefbooks');
117: if (!empty($val)) {
118: $prefbooks = unserialize($val);
119: return $prefbooks[$this->_params['name']];
120: }
121:
122: return array();
123: }
124:
125: 126: 127: 128: 129: 130: 131:
132: protected function _setAddressBook($addressbook)
133: {
134: global $prefs;
135:
136: $val = $prefs->getValue('prefbooks');
137: $prefbooks = empty($val)
138: ? array()
139: : unserialize($val);
140:
141: $prefbooks[$this->_params['name']] = $addressbook;
142: $prefs->setValue('prefbooks', serialize($prefbooks));
143: $prefs->store();
144: }
145:
146: }
147: