1<?php
2/**
3 * Turba directory driver implementation for Horde Preferences - very simple,
4 * lightweight container.
5 *
6 * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
7 *
8 * See the enclosed file LICENSE for license information (ASL).  If you did
9 * did not receive this file, see http://www.horde.org/licenses/apache.
10 *
11 * @author   Chuck Hagenbuch <chuck@horde.org>
12 * @category Horde
13 * @license  http://www.horde.org/licenses/apache ASL
14 * @package  Turba
15 */
16class Turba_Driver_Prefs extends Turba_Driver
17{
18    /**
19     * Returns all entries - searching isn't implemented here for now. The
20     * parameters are simply ignored.
21     *
22     * @param array $criteria    Array containing the search criteria.
23     * @param array $fields      List of fields to return.
24     * @param array $blobFields  Array of fields containing binary data.
25     *
26     * @return array  Hash containing the search results.
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     * Reads the given data from the preferences and returns the result's
35     * fields.
36     *
37     * @param string $key        The primary key field to use.
38     * @param mixed $ids         The ids of the contacts to load.
39     * @param string $owner      Only return contacts owned by this user.
40     * @param array $fields      List of fields to return.
41     * @param array $blobFields  Array of fields containing binary data.
42     * @param array $dateFields  Array of fields containing date data.
43     *                           @since 4.2.0
44     *
45     */
46    protected function _read($key, $ids, $owner, array $fields,
47                             array $blobFields = array(),
48                             array $dateFields = array())
49    {
50        $book = $this->_getAddressBook();
51
52        $results = array();
53        if (!is_array($ids)) {
54            $ids = array($ids);
55        }
56
57        foreach ($ids as $id) {
58            if (isset($book[$id])) {
59                $results[] = $book[$id];
60            }
61        }
62
63        return $results;
64    }
65
66    /**
67     * Adds the specified contact to the addressbook.
68     *
69     * @param array $attributes  The attribute values of the contact.
70     * @param array $blob_fields  Fields that represent binary data.
71     * @param array $date_fields  Fields that represent dates. @since 4.2.0
72     *
73     * @throws Turba_Exception
74     */
75    protected function _add(array $attributes, array $blob_fields = array(), array $date_fields = array())
76    {
77        $book = $this->_getAddressBook();
78        $book[$attributes['id']] = $attributes;
79        $this->_setAddressbook($book);
80    }
81
82    /**
83     * TODO
84     */
85    protected function _canAdd()
86    {
87        return true;
88    }
89
90    /**
91     * Deletes the specified object from the preferences.
92     *
93     * @param string $object_key TODO
94     * @param string $object_id  TODO
95     */
96    protected function _delete($object_key, $object_id)
97    {
98        $book = $this->_getAddressBook();
99        unset($book[$object_id]);
100        $this->_setAddressbook($book);
101    }
102
103    /**
104     * Saves the specified object in the preferences.
105     *
106     * @param Turba_Object $object TODO
107     */
108    function _save($object)
109    {
110        list(,$object_id) = each($this->toDriverKeys(array('__key' => $object->getValue('__key'))));
111        $attributes = $this->toDriverKeys($object->getAttributes());
112
113        $book = $this->_getAddressBook();
114        $book[$object_id] = $attributes;
115        $this->_setAddressBook($book);
116    }
117
118    /**
119     * TODO
120     *
121     * @return TODO
122     */
123    protected function _getAddressBook()
124    {
125        global $prefs;
126
127        $val = $prefs->getValue('prefbooks');
128        if (!empty($val)) {
129            $prefbooks = unserialize($val);
130            return $prefbooks[$this->_params['name']];
131        }
132
133        return array();
134    }
135
136    /**
137     * TODO
138     *
139     * @param $addressbook TODO
140     *
141     * @return TODO
142     */
143    protected function _setAddressBook($addressbook)
144    {
145        global $prefs;
146
147        $val = $prefs->getValue('prefbooks');
148        $prefbooks = empty($val)
149            ? array()
150            : unserialize($val);
151
152        $prefbooks[$this->_params['name']] = $addressbook;
153        $prefs->setValue('prefbooks', serialize($prefbooks));
154        $prefs->store();
155    }
156
157}
158