Merge branch 'master' of http://git.tine20.org/git/Syncope
[tine20] / tine20 / library / Syncope / lib / Syncope / Backend / Content.php
1 <?php
2 /**
3  * Tine 2.0
4  *
5  * @package     Syncope
6  * @subpackage  Backend
7  * @license     http://www.tine20.org/licenses/lgpl.html LGPL Version 3
8  * @author      Lars Kneschke <l.kneschke@metaways.de>
9  * @copyright   Copyright (c) 2009-2012 Metaways Infosystems GmbH (http://www.metaways.de)
10  * 
11  */
12
13 /**
14  * sql backend class for the folder state
15  *
16  * @package     Syncope
17  * @subpackage  Backend
18  */
19 class Syncope_Backend_Content implements Syncope_Backend_IContent
20 {
21     /**
22      * the database adapter
23      *
24      * @var Zend_Db_Adapter_Abstract
25      */
26     protected $_db;
27     
28     protected $_tablePrefix;
29     
30     public function __construct(Zend_Db_Adapter_Abstract $_db, $_tablePrefix = 'syncope_')
31     {
32         $this->_db          = $_db;
33         $this->_tablePrefix = $_tablePrefix;
34     }
35     
36     /**
37      * create new content state
38      *
39      * @param Syncope_Model_IContent $_state
40      * @return Syncope_Model_IContent
41      */
42     public function create(Syncope_Model_IContent $_state)
43     {
44         $id = sha1(mt_rand(). microtime());
45         
46         $deviceId = $_state->device_id instanceof Syncope_Model_IDevice ? $_state->device_id->id : $_state->device_id;
47         $folderId = $_state->folder_id instanceof Syncope_Model_IFolder ? $_state->folder_id->id : $_state->folder_id;
48         
49         $this->_db->insert($this->_tablePrefix . 'content', array(
50             'id'               => $id, 
51             'device_id'        => $deviceId,
52             'folder_id'        => $folderId,
53             'contentid'        => $_state->contentid,
54             'creation_time'    => $_state->creation_time->format('Y-m-d H:i:s'),
55             'creation_synckey' => $_state->creation_synckey,
56             'is_deleted'       => isset($_state->is_deleted) ? (int)!!$_state->is_deleted : 0
57         ));
58         
59         return $this->get($id);
60     }
61     
62     /**
63      * mark state as deleted. The state gets removed finally, 
64      * when the synckey gets validated during next sync.
65      * 
66      * @param Syncope_Model_IContent|string $_id
67      */
68     public function delete($_id)
69     {
70         $id = $_id instanceof Syncope_Model_IContent ? $_id->id : $_id;
71         
72         $this->_db->update($this->_tablePrefix . 'content', array(
73             'is_deleted' => 1
74         ), array(
75             'id = ?' => $id
76         ));
77         
78     }
79     
80     /**
81      * @param string  $_id
82      * @throws Syncope_Exception_NotFound
83      * @return Syncope_Model_IContent
84      */
85     public function get($_id)
86     {
87         $select = $this->_db->select()
88             ->from($this->_tablePrefix . 'content')
89             ->where('id = ?', $_id);
90     
91         $stmt = $this->_db->query($select);
92         $state = $stmt->fetchObject('Syncope_Model_Content');
93         
94         if (! $state instanceof Syncope_Model_IContent) {
95             throw new Syncope_Exception_NotFound('id not found');
96         }
97         
98         if (!empty($state->creation_time)) {
99             $state->creation_time = new DateTime($state->creation_time, new DateTimeZone('utc'));
100         }
101     
102         return $state;
103     }
104     
105     /**
106      * @param Syncope_Model_IDevice|string $_deviceId
107      * @param Syncope_Model_IFolder|string $_folderId
108      * @param string $_contentId
109      * @return Syncope_Model_IContent
110      */
111     public function getContentState($_deviceId, $_folderId, $_contentId)
112     {
113         $deviceId = $_deviceId instanceof Syncope_Model_IDevice ? $_deviceId->id : $_deviceId;
114         $folderId = $_folderId instanceof Syncope_Model_IFolder ? $_folderId->id : $_folderId;
115     
116         $select = $this->_db->select()
117             ->from($this->_tablePrefix . 'content')
118             ->where($this->_db->quoteIdentifier('device_id')  . ' = ?', $deviceId)
119             ->where($this->_db->quoteIdentifier('folder_id')  . ' = ?', $folderId)
120             ->where($this->_db->quoteIdentifier('contentid')  . ' = ?', $_contentId)
121             ->where($this->_db->quoteIdentifier('is_deleted') . ' = ?', 0);
122     
123         $stmt = $this->_db->query($select);
124         $state = $stmt->fetchObject('Syncope_Model_Content');
125         
126         if (! $state instanceof Syncope_Model_IContent) {
127             throw new Syncope_Exception_NotFound('id not found');
128         }
129         
130         if (!empty($state->creation_time)) {
131             $state->creation_time = new DateTime($state->creation_time, new DateTimeZone('utc'));
132         }
133     
134         return $state;
135     }
136     
137     /**
138      * get array of ids which got send to the client for a given class
139      *
140      * @param Syncope_Model_IDevice|string $_deviceId
141      * @param Syncope_Model_IFolder|string $_folderId
142      * @return array
143      */
144     public function getFolderState($_deviceId, $_folderId)
145     {
146         $deviceId = $_deviceId instanceof Syncope_Model_IDevice ? $_deviceId->id : $_deviceId;
147         $folderId = $_folderId instanceof Syncope_Model_IFolder ? $_folderId->id : $_folderId;
148                 
149         $select = $this->_db->select()
150             ->from($this->_tablePrefix . 'content', 'contentid')
151             ->where($this->_db->quoteIdentifier('device_id')  . ' = ?', $deviceId)
152             ->where($this->_db->quoteIdentifier('folder_id')  . ' = ?', $folderId)
153             ->where($this->_db->quoteIdentifier('is_deleted') . ' = ?', 0);
154         
155         $stmt = $this->_db->query($select);
156         $result = $stmt->fetchAll(Zend_Db::FETCH_COLUMN);
157     
158         return $result;
159     }
160     
161     /**
162      * reset list of stored id
163      *
164      * @param Syncope_Model_IDevice|string $_deviceId
165      * @param Syncope_Model_IFolder|string $_folderId
166      */
167     public function resetState($_deviceId, $_folderId)
168     {
169         $deviceId = $_deviceId instanceof Syncope_Model_IDevice ? $_deviceId->id : $_deviceId;
170         $folderId = $_folderId instanceof Syncope_Model_IFolder ? $_folderId->id : $_folderId;
171          
172         $where = array(
173             $this->_db->quoteInto($this->_db->quoteIdentifier('device_id') . ' = ?', $deviceId),
174             $this->_db->quoteInto($this->_db->quoteIdentifier('folder_id') . ' = ?', $folderId)
175         );
176         
177         $this->_db->delete($this->_tablePrefix . 'content', $where);
178     }
179 }