moveRecordsToContainer might be a long running request
[tine20] / tine20 / Tinebase / Frontend / Json / Container.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Tinebase
6  * @subpackage  Container
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @copyright   Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Cornelius Weiss <c.weiss@metaways.de>
10  */
11
12 /**
13  * Json Container class
14  * 
15  * @package     Tinebase
16  * @subpackage  Container
17  */
18 class Tinebase_Frontend_Json_Container extends  Tinebase_Frontend_Json_Abstract
19 {
20    /**
21      * gets container / container folder
22      * 
23      * Backend function for containerTree widget
24      * 
25      * @todo move getOtherUsers to own function
26      * 
27      * @param  string $application
28      * @param  string $containerType
29      * @param  string $owner
30      * @param  array $requiredGrants
31      * @return array
32      */
33     public function getContainer($application, $containerType, $owner, $requiredGrants = NULL)
34     {
35         if (!$requiredGrants) {
36             $requiredGrants = Tinebase_Model_Grants::GRANT_READ;
37         }
38         switch($containerType) {
39             case Tinebase_Model_Container::TYPE_PERSONAL:
40                 $containers = Tinebase_Container::getInstance()->getPersonalContainer(Tinebase_Core::getUser(), $application, $owner, $requiredGrants);
41                 break;
42                 
43             case Tinebase_Model_Container::TYPE_SHARED:
44                 $containers = Tinebase_Container::getInstance()->getSharedContainer(Tinebase_Core::getUser(), $application, $requiredGrants);
45                 break;
46                 
47             case Tinebase_Model_Container::TYPE_OTHERUSERS:
48                 $containers = Tinebase_Container::getInstance()->getOtherUsers(Tinebase_Core::getUser(), $application, $requiredGrants);
49                 break;
50                 
51             default:
52                 throw new Exception('no such NodeType');
53         }
54         
55         $response = array();
56         foreach ($containers as $container) {
57             $containerArray = $container->toArray();
58             
59             if ($container instanceof Tinebase_Model_Container) {
60                 $containerArray['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $container->getId())->toArray();
61                 $containerArray['path'] = $container->getPath();
62                 $ownerId = $container->getOwner();
63             } else {
64                 $containerArray['path'] = "personal/{$container->getId()}";
65                 $ownerId = $container->getId();
66             }
67             if (! empty($ownerId)) {
68                 try {
69                     $containerArray['ownerContact'] = Addressbook_Controller_Contact::getInstance()->getContactByUserId($ownerId, true)->toArray();
70                 } catch (Exception $e) {
71                     Tinebase_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . " can't resolve ownerContact: " . $e);
72                 }
73             }
74             
75             $response[] = $containerArray;
76         }
77         
78         return $response;
79     }
80     
81     /**
82      * adds a container
83      * 
84      * @param   string $application
85      * @param   string $containerName
86      * @param   string $containerType
87      * @param   string $modelName
88      * @return  array new container
89      * @throws  Tinebase_Exception_InvalidArgument
90      */
91     public function addContainer($application, $name, $containerType, $modelName = '')
92     {
93         if (empty($modelName)) {
94             $modelName = Tinebase_Core::getApplicationInstance($application)->getDefaultModel();
95         } else {
96             $modelName = strstr($modelName, '_Model_') ? $modelName : $application . '_Model_' . $modelName;
97         }
98         
99         $newContainer = new Tinebase_Model_Container(array(
100             'name'              => $name,
101             'type'              => $containerType,
102             'backend'           => 'Sql',
103             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName($application)->getId(),
104             'model'             => $modelName
105         ));
106         
107         if($newContainer->type !== Tinebase_Model_Container::TYPE_PERSONAL and $newContainer->type !== Tinebase_Model_Container::TYPE_SHARED) {
108             throw new Tinebase_Exception_InvalidArgument('Can add personal or shared containers only');
109         }
110                 
111         $container = Tinebase_Container::getInstance()->addContainer($newContainer);
112         
113         $result = $container->toArray();
114         $result['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $container->getId())->toArray();
115         $result['path'] = $container->getPath();
116         return $result;
117     }
118     
119     /**
120      * deletes a container
121      * 
122      * @param   int     $containerId
123      * @return  string  success
124      */
125     public function deleteContainer($containerId)
126     {
127         Tinebase_Container::getInstance()->deleteContainer($containerId);
128         
129         return array(
130             'success'      => TRUE
131         );
132     }
133     
134     /**
135      * renames a container
136      * 
137      * @param  int      $containerId
138      * @param  string   $newName
139      * @return array    updated container
140      */
141     public function renameContainer($containerId, $newName)
142     {
143         $container = Tinebase_Container::getInstance()->setContainerName($containerId, $newName);
144         
145         $result = $container->toArray();
146         $result['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $container->getId())->toArray();
147         $result['path'] = $container->getPath();
148         return $result;
149     }
150     
151     /**
152      * sets color of a container
153      * 
154      * @param  int      $containerId
155      * @param  string   $color
156      * @return array    updated container
157      * @throws Tinebase_Exception
158      */
159     public function setContainerColor($containerId, $color)
160     {
161         try {
162             $container = Tinebase_Container::getInstance()->setContainerColor($containerId, $color);
163         } catch (Tinebase_Exception $e) {
164             throw new Tinebase_Exception('Container not found or permission to set containername denied!');
165         }
166         
167         $result = $container->toArray();
168         $result['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $container->getId())->toArray();
169         $result['path'] = $container->getPath();
170         return $result;
171     }
172     
173     /**
174      * returns container grants
175      * 
176      * @param   int     $containerId
177      * @return  array
178      */
179     public function getContainerGrants($containerId) 
180     {
181         $result = array(
182             'results'     => array(),
183             'totalcount'  => 0
184         );
185         
186         $result['results'] = Tinebase_Container::getInstance()->getGrantsOfContainer($containerId)->toArray();
187         $result['totalcount'] = count($result['results']);
188         $result['results'] = self::resolveAccounts($result['results']);
189         
190         return $result;
191     }
192     
193     /**
194      * resolve accounts in grants
195      * 
196      * @param array $_grants
197      * @return array
198      * @throws Tinebase_Exception_InvalidArgument
199      * 
200      * @todo think about resolving before converting to array
201      */
202     public static function resolveAccounts($_grants)
203     {
204         foreach($_grants as &$value) {
205             switch($value['account_type']) {
206                 case Tinebase_Acl_Rights::ACCOUNT_TYPE_USER:
207                     try {
208                         $account = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $value['account_id']);
209                     } catch (Tinebase_Exception_NotFound $e) {
210                         $account = Tinebase_User::getInstance()->getNonExistentUser();
211                     }
212                     $value['account_name'] = $account->toArray();
213                     break;
214                 case Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP:
215                     try {
216                         $group = Tinebase_Group::getInstance()->getGroupById($value['account_id']);
217                     } catch (Tinebase_Exception_Record_NotDefined $e) {
218                         $group = Tinebase_Group::getInstance()->getNonExistentGroup();
219                     }
220                     $value['account_name'] = $group->toArray();
221                     break;
222                 case Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE:
223                     $value['account_name'] = array('accountDisplayName' => Tinebase_Translation::getTranslation('Tinebase')->_('Anyone'));
224                     break;
225                 default:
226                     throw new Tinebase_Exception_InvalidArgument('Unsupported accountType.');
227                     break;
228             }
229         }
230         
231         return $_grants;
232     }
233     
234     /**
235      * sets new grants for given container
236      * 
237      * @param  int      $containerId
238      * @param  array    $grants
239      * @return array    see getContainerGrants
240      */
241     public function setContainerGrants($containerId, $grants)
242     {
243         $grants = ($grants) ? $grants : array();
244         
245         $newGrants = new Tinebase_Record_RecordSet('Tinebase_Model_Grants', $grants);
246         
247         Tinebase_Container::getInstance()->setGrants($containerId, $newGrants);
248                
249         return $this->getContainerGrants($containerId);
250     }
251     
252     /**
253      * move records to container
254      * 
255      * @param string $targetContainerId
256      * @param array  $filterData
257      * @param string $applicationName
258      * @param string $model
259      * @return array
260      */
261     public function moveRecordsToContainer($targetContainerId, $filterData, $applicationName, $model)
262     {
263         $filterModel = $applicationName . '_Model_' . $model . 'Filter';
264         $filter = new $filterModel(array());
265         $filter->setFromArrayInUsersTimezone($filterData);
266
267         $this->_longRunningRequest();
268         
269         $recordController = Tinebase_Core::getApplicationInstance($applicationName, $model);
270         $result = $recordController->move($filter, $targetContainerId);
271         
272         return array(
273             'status'    => 'success',
274             'results'   => $result,
275         );
276     }
277 }