make sure we fetch users from sql
[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
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             } else {
63                 $containerArray['path'] = "personal/{$container->getId()}";
64             }
65             
66             $response[] = $containerArray;
67         }
68         
69         return $response;
70     }
71     
72     /**
73      * adds a container
74      * 
75      * @param   string $application
76      * @param   string $containerName
77      * @param   string $containerType
78      * @param   string $modelName
79      * @return  array new container
80      * @throws  Tinebase_Exception_InvalidArgument
81      */
82     public function addContainer($application, $name, $containerType, $modelName = '')
83     {
84         if (empty($modelName)) {
85             $modelName = Tinebase_Core::getApplicationInstance($application)->getDefaultModel();
86         } else {
87             $modelName = strstr($modelName, '_Model_') ? $modelName : $application . '_Model_' . $modelName;
88         }
89         
90         $newContainer = new Tinebase_Model_Container(array(
91             'name'              => $name,
92             'type'              => $containerType,
93             'backend'           => 'Sql',
94             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName($application)->getId(),
95             'model'             => $modelName
96         ));
97         
98         if($newContainer->type !== Tinebase_Model_Container::TYPE_PERSONAL and $newContainer->type !== Tinebase_Model_Container::TYPE_SHARED) {
99             throw new Tinebase_Exception_InvalidArgument('Can add personal or shared containers only');
100         }
101                 
102         $container = Tinebase_Container::getInstance()->addContainer($newContainer);
103         
104         $result = $container->toArray();
105         $result['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $container->getId())->toArray();
106         $result['path'] = $container->getPath();
107         return $result;
108     }
109     
110     /**
111      * deletes a container
112      * 
113      * @param   int     $containerId
114      * @return  string  success
115      */
116     public function deleteContainer($containerId)
117     {
118         Tinebase_Container::getInstance()->deleteContainer($containerId);
119         
120         return array(
121             'success'      => TRUE
122         );
123     }
124     
125     /**
126      * renames a container
127      * 
128      * @param  int      $containerId
129      * @param  string   $newName
130      * @return array    updated container
131      */
132     public function renameContainer($containerId, $newName)
133     {
134         $container = Tinebase_Container::getInstance()->setContainerName($containerId, $newName);
135         
136         $result = $container->toArray();
137         $result['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $container->getId())->toArray();
138         $result['path'] = $container->getPath();
139         return $result;
140     }
141     
142     /**
143      * sets color of a container
144      * 
145      * @param  int      $containerId
146      * @param  string   $color
147      * @return array    updated container
148      * @throws Tinebase_Exception
149      */
150     public function setContainerColor($containerId, $color)
151     {
152         try {
153             $container = Tinebase_Container::getInstance()->setContainerColor($containerId, $color);
154         } catch (Tinebase_Exception $e) {
155             throw new Tinebase_Exception('Container not found or permission to set containername denied!');
156         }
157         
158         $result = $container->toArray();
159         $result['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $container->getId())->toArray();
160         $result['path'] = $container->getPath();
161         return $result;
162     }
163     
164     /**
165      * returns container grants
166      * 
167      * @param   int     $containerId
168      * @return  array
169      */
170     public function getContainerGrants($containerId) 
171     {
172         $result = array(
173             'results'     => array(),
174             'totalcount'  => 0
175         );
176         
177         $result['results'] = Tinebase_Container::getInstance()->getGrantsOfContainer($containerId)->toArray();
178         $result['totalcount'] = count($result['results']);
179         $result['results'] = self::resolveAccounts($result['results']);
180         
181         return $result;
182     }
183     
184     /**
185      * resolve accounts in grants
186      * 
187      * @param array $_grants
188      * @return array
189      * @throws Tinebase_Exception_InvalidArgument
190      * 
191      * @todo think about resolving before converting to array
192      */
193     public static function resolveAccounts($_grants)
194     {
195         foreach($_grants as &$value) {
196             switch($value['account_type']) {
197                 case Tinebase_Acl_Rights::ACCOUNT_TYPE_USER:
198                     try {
199                         $account = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $value['account_id']);
200                     } catch (Tinebase_Exception_NotFound $e) {
201                         $account = Tinebase_User::getInstance()->getNonExistentUser();
202                     }
203                     $value['account_name'] = $account->toArray();
204                     break;
205                 case Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP:
206                     try {
207                         $group = Tinebase_Group::getInstance()->getGroupById($value['account_id']);
208                     } catch (Tinebase_Exception_Record_NotDefined $e) {
209                         $group = Tinebase_Group::getInstance()->getNonExistentGroup();
210                     }
211                     $value['account_name'] = $group->toArray();
212                     break;
213                 case Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE:
214                     $value['account_name'] = array('accountDisplayName' => 'Anyone');
215                     break;
216                 default:
217                     throw new Tinebase_Exception_InvalidArgument('Unsupported accountType.');
218                     break;
219             }
220         }
221         
222         return $_grants;
223     }
224     
225     /**
226      * sets new grants for given container
227      * 
228      * @param  int      $containerId
229      * @param  array    $grants
230      * @return array    see getContainerGrants
231      */
232     public function setContainerGrants($containerId, $grants)
233     {
234         $grants = ($grants) ? $grants : array();
235         
236         $newGrants = new Tinebase_Record_RecordSet('Tinebase_Model_Grants', $grants);
237         
238         Tinebase_Container::getInstance()->setGrants($containerId, $newGrants);
239                
240         return $this->getContainerGrants($containerId);
241     }
242     
243     /**
244      * move records to container
245      * 
246      * @param string $targetContainerId
247      * @param array  $filterData
248      * @param string $applicationName
249      * @param string $model
250      * @return array
251      */
252     public function moveRecordsToContainer($targetContainerId, $filterData, $applicationName, $model)
253     {
254         $filterModel = $applicationName . '_Model_' . $model . 'Filter';
255         $filter = new $filterModel(array());
256         $filter->setFromArrayInUsersTimezone($filterData);
257         
258         $recordController = Tinebase_Core::getApplicationInstance($applicationName, $model);
259         $result = $recordController->move($filter, $targetContainerId);
260         
261         return array(
262             'status'    => 'success',
263             'results'   => $result,
264         );
265     }
266 }