1<?php
2/**
3 * Caches share parameters.
4 *
5 * PHP version 5
6 *
7 * @category Kolab
8 * @package  Kolab_Storage
9 * @author   Gunnar Wrobel <wrobel@pardus.de>
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 */
12
13/**
14 * Caches share parameters.
15 *
16 * Copyright 2011-2017 Horde LLC (http://www.horde.org/)
17 *
18 * See the enclosed file COPYING for license information (LGPL). If you
19 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
20 *
21 * @category Kolab
22 * @package  Kolab_Storage
23 * @author   Gunnar Wrobel <wrobel@pardus.de>
24 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
25 */
26class Horde_Kolab_Storage_List_Query_Share_Cache
27extends Horde_Kolab_Storage_List_Query_Share
28implements Horde_Kolab_Storage_List_Manipulation_Listener,
29Horde_Kolab_Storage_List_Synchronization_Listener
30{
31    /** The share description */
32    const DESCRIPTIONS = 'SHARE_DESCRIPTIONS';
33
34    /** The share parameters */
35    const PARAMETERS = 'SHARE_PARAMETERS';
36
37    /**
38     * The underlying Share query.
39     *
40     * @param Horde_Kolab_Storage_List_Query_Share
41     */
42    private $_query;
43
44    /**
45     * The list cache.
46     *
47     * @var Horde_Kolab_Storage_Cache_List
48     */
49    private $_list_cache;
50
51    /**
52     * The cached share descriptions.
53     *
54     * @var array
55     */
56    private $_descriptions;
57
58    /**
59     * The cached share parameters.
60     *
61     * @var array
62     */
63    private $_parameters;
64
65    /**
66     * Constructor.
67     *
68     * @param Horde_Kolab_Storage_List_Query_Share $query The underlying share query.
69     * @param Horde_Kolab_Storage_List_Cache $cache The list cache.
70     */
71    public function __construct(Horde_Kolab_Storage_List_Query_Share $query,
72                                Horde_Kolab_Storage_List_Cache $cache)
73    {
74        $this->_query = $query;
75        $this->_list_cache = $cache;
76        if ($this->_list_cache->hasQuery(self::DESCRIPTIONS)) {
77            $this->_descriptions = $this->_list_cache->getQuery(self::DESCRIPTIONS);
78        } else {
79            $this->_descriptions = array();
80        }
81        if ($this->_list_cache->hasLongTerm(self::PARAMETERS)) {
82            $this->_parameters = $this->_list_cache->getLongTerm(self::PARAMETERS);
83        } else {
84            $this->_parameters = array();
85        }
86    }
87
88    /**
89     * Returns the share description.
90     *
91     * @param string $folder The folder name.
92     *
93     * @return string The folder/share description.
94     */
95    public function getDescription($folder)
96    {
97        if (!isset($this->_descriptions[$folder])) {
98            $this->_descriptions[$folder] = $this->_query->getDescription($folder);
99            $this->_list_cache->setQuery(self::DESCRIPTIONS, $this->_descriptions);
100            $this->_list_cache->save();
101        }
102        return $this->_descriptions[$folder];
103    }
104
105    /**
106     * Returns the share parameters.
107     *
108     * @param string $folder The folder name.
109     *
110     * @return string The folder/share parameters.
111     */
112    public function getParameters($folder)
113    {
114        if (!isset($this->_parameters[$folder])) {
115            $this->_parameters[$folder] = $this->_query->getParameters($folder);
116            //@todo: This would only be long term data in case the IMAP is made private on the IMAP server
117            $this->_list_cache->setLongTerm(self::PARAMETERS, $this->_parameters);
118            $this->_list_cache->save();
119        }
120        return $this->_parameters[$folder];
121    }
122
123    /**
124     * Returns the share description.
125     *
126     * @param string $folder      The folder name.
127     * @param string $description The share description.
128     *
129     * @return string The folder/share description.
130     */
131    public function setDescription($folder, $description)
132    {
133        $this->_query->setDescription($folder, $description);
134        $this->_descriptions[$folder] = $description;
135        $this->_list_cache->setQuery(self::DESCRIPTIONS, $this->_descriptions);
136        $this->_list_cache->save();
137    }
138
139    /**
140     * Returns the share parameters.
141     *
142     * @param string $folder     The folder name.
143     * @param array  $parameters The share parameters.
144     *
145     * @return string The folder/share parameters.
146     */
147    public function setParameters($folder, array $parameters)
148    {
149        $this->_query->setParameters($folder, $parameters);
150        $this->_parameters[$folder] = $parameters;
151        $this->_list_cache->setLongTerm(self::PARAMETERS, $this->_parameters);
152        $this->_list_cache->save();
153    }
154
155    /**
156     * Update the listener after creating a new folder.
157     *
158     * @param string $folder The path of the folder that has been created.
159     * @param string $type   An optional type for the folder.
160     *
161     * @return NULL
162     */
163    public function updateAfterCreateFolder($folder, $type = null)
164    {
165    }
166
167    /**
168     * Update the listener after deleting folder.
169     *
170     * @param string $folder The path of the folder that has been deleted.
171     *
172     * @return NULL
173     */
174    public function updateAfterDeleteFolder($folder)
175    {
176        unset($this->_descriptions[$folder]);
177        unset($this->_parameters[$folder]);
178        $this->_list_cache->setQuery(self::DESCRIPTIONS, $this->_descriptions);
179        $this->_list_cache->setLongTerm(self::PARAMETERS, $this->_parameters);
180        $this->_list_cache->save();
181    }
182
183    /**
184     * Update the listener after renaming a folder.
185     *
186     * @param string $old The old path of the folder.
187     * @param string $new The new path of the folder.
188     *
189     * @return NULL
190     */
191    public function updateAfterRenameFolder($old, $new)
192    {
193        if (isset($this->_descriptions[$old])) {
194            $this->_descriptions[$new] = $this->_descriptions[$old];
195            unset($this->_descriptions[$old]);
196            $this->_list_cache->setQuery(self::DESCRIPTIONS, $this->_descriptions);
197        }
198        if (isset($this->_parameters[$old])) {
199            $this->_parameters[$new] = $this->_parameters[$old];
200            unset($this->_parameters[$old]);
201            $this->_list_cache->setLongTerm(self::PARAMETERS, $this->_parameters);
202        }
203        $this->_list_cache->save();
204    }
205
206    /**
207     * Synchronize the ACL information with the information from the backend.
208     *
209     * @param array $params Additional parameters.
210     *
211     * @return NULL
212     */
213    public function synchronize($params = array())
214    {
215        $this->_descriptions = array();
216    }
217}