2b317fca28f6bd37db247f154c1ff86883dd0e30
[tine20] / tine20 / Calendar / Controller / Resource.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Calendar
6  * @subpackage  Controller
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Cornelius Weiss <c.weiss@metaways.de>
9  * @copyright   Copyright (c) 2009-2014 Metaways Infosystems GmbH (http://www.metaways.de)
10  */
11
12 /**
13  * Calendar Resources Controller
14  * 
15  * @package Calendar
16  * @subpackage  Controller
17  */
18 class Calendar_Controller_Resource extends Tinebase_Controller_Record_Abstract
19 {
20     /**
21      * @var boolean
22      * 
23      * just set is_delete=1 if record is going to be deleted
24      */
25     protected $_purgeRecords = FALSE;
26     
27     /**
28      * check for container ACLs?
29      *
30      * @var boolean
31      */
32     protected $_doContainerACLChecks = TRUE;
33     
34     /**
35      * @var Calendar_Controller_Resource
36      */
37     private static $_instance = NULL;
38     
39     /**
40      * the constructor
41      *
42      * don't use the constructor. use the singleton 
43      */
44     private function __construct() {
45         $this->_applicationName = 'Calendar';
46         $this->_modelName       = 'Calendar_Model_Resource';
47         
48         $this->_backend         = new Tinebase_Backend_Sql(array(
49             'modelName' => $this->_modelName, 
50             'tableName' => 'cal_resources'
51         ));
52         $this->_backend->setModlogActive(TRUE);
53     }
54
55     /**
56      * don't clone. Use the singleton.
57      */
58     private function __clone() 
59     {
60         
61     }
62     
63     /**
64      * singleton
65      *
66      * @return Calendar_Controller_Resource
67      */
68     public static function getInstance() 
69     {
70         if (self::$_instance === NULL) {
71             self::$_instance = new Calendar_Controller_Resource();
72         }
73         return self::$_instance;
74     }
75     
76     /**
77      * add one record
78      *
79      * @param   Tinebase_Record_Interface $_record
80      * @return  Tinebase_Record_Interface
81      * @throws  Tinebase_Exception_AccessDenied
82      */
83     public function create(Tinebase_Record_Interface $_record)
84     {
85         // create a calendar for this resource
86         $container = Tinebase_Container::getInstance()->addContainer(new Tinebase_Model_Container(array(
87             'name'              => $_record->name,
88             'color'             => '#333399',
89             'type'              => Tinebase_Model_Container::TYPE_SHARED,
90             'backend'           => $this->_backend->getType(),
91             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName($this->_applicationName)->getId(),
92             'model'             => 'Calendar_Model_Event'
93         )), NULL, TRUE);
94         
95         if ($_record->grants instanceof Tinebase_Record_RecordSet) {
96             Tinebase_Container::getInstance()->setGrants($container->getId(), $_record->grants, TRUE, FALSE);
97         }
98         
99         $_record->container_id = $container->getId();
100         return parent::create($_record);
101     }
102     
103     /**
104      * update one record
105      *
106      * @param   Tinebase_Record_Interface $_record
107      * @return  Tinebase_Record_Interface
108      * @throws  Tinebase_Exception_AccessDenied
109      */
110     public function update(Tinebase_Record_Interface $_record)
111     {
112         $container = Tinebase_Container::getInstance()->getContainerById($_record->container_id);
113         $container->name = $_record->name;
114         Tinebase_Container::getInstance()->update($container);
115         
116         if ($_record->grants instanceof Tinebase_Record_RecordSet) {
117             Tinebase_Container::getInstance()->setGrants($container->getId(), $_record->grants, TRUE, FALSE);
118         }
119         
120         return parent::update($_record);
121     }
122     
123     protected function _checkGrant($_record, $_action, $_throw = TRUE, $_errorMessage = 'No Permission.', $_oldRecord = NULL)
124     {
125         $this->doContainerACLChecks($this->_doContainerACLChecks && ! Tinebase_Core::getUser()->hasRight('Calendar', Calendar_Acl_Rights::MANAGE_RESOURCES));
126         
127         return parent::_checkGrant($_record, $_action, $_throw, $_errorMessage, $_oldRecord);
128     }
129     
130     /**
131      * check if user has the right to manage resources
132      * 
133      * @param string $_action {get|create|update|delete}
134      * @return void
135      * @throws Tinebase_Exception_AccessDenied
136      */
137     protected function _checkRight($_action)
138     {
139         $this->doContainerACLChecks($this->_doContainerACLChecks && ! Tinebase_Core::getUser()->hasRight('Calendar', Calendar_Acl_Rights::MANAGE_RESOURCES));
140         
141         switch ($_action) {
142             case 'create':
143             case 'update':
144             case 'delete':
145                 if (! Tinebase_Core::getUser()->hasRight('Calendar', Calendar_Acl_Rights::MANAGE_RESOURCES)) {
146                     throw new Tinebase_Exception_AccessDenied("You don't have the right to manage resources");
147                 }
148                 break;
149             default;
150                break;
151         }
152     }
153     
154     /**
155      * delete linked objects (notes, relations, ...) of record
156      *
157      * @param Tinebase_Record_Interface $_record
158      */
159     protected function _deleteLinkedObjects(Tinebase_Record_Interface $_record)
160     {
161         try {
162             Tinebase_Container::getInstance()->deleteContainer($_record->container_id, true);
163         } catch (Tinebase_Exception_NotFound $tenf) {
164             Tinebase_Exception::log($tenf, false, $_record->toArray());
165         }
166         return parent::_deleteLinkedObjects($_record);
167     }
168
169     /**
170      * returns recipients for a resource notification
171      *
172      *  users who are allowed to edit a resource, should receive a notification
173      *
174      * @param  Calendar_Model_Resource $_lead
175      * @return array          array of int|Addressbook_Model_Contact
176      */
177     public function getNotificationRecipients(Calendar_Model_Resource $resource, $_notificationLevel)
178     {
179         $recipients = array();
180
181         $relations = Tinebase_Relations::getInstance()->getRelations('Calendar_Model_Resource', 'Sql', $resource->getId(), true);
182
183         foreach ($relations as $relation) {
184             if ($relation->related_model == 'Addressbook_Model_Contact' && $relation->type == 'RESPONSIBLE') {
185                 $recipients[] = $relation->related_record;
186             }
187         }
188
189         if (empty($recipients)) {
190             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__CLASS__ . '::' . __METHOD__ . '::' . __LINE__ . ' no responsibles found for calendar resource: ' .
191                 $resource->getId() . ' sending notification to all people having edit access to container ' . $resource->container_id);
192
193             $containerGrants = Tinebase_Container::getInstance()->getGrantsOfContainer($resource->container_id, TRUE);
194
195             foreach ($containerGrants as $grant) {
196                 if ($grant['account_type'] == Tinebase_Acl_Rights::ACCOUNT_TYPE_USER && $grant[Tinebase_Model_Grants::GRANT_EDIT] == 1) {
197                     try {
198                         $recipient = Addressbook_Controller_Contact::getInstance()->getContactByUserId($grant['account_id'], TRUE);
199                         $sendLevel = Tinebase_Core::getPreference('Calendar')->getValueForUser(Calendar_Preference::NOTIFICATION_LEVEL, $grant['account_id']);
200                         // only add recipients that want that kind of notification (consider the send Level)
201                         if ($sendLevel >= $_notificationLevel) {
202                             $recipients[] = $recipient;
203                         }
204                     } catch (Addressbook_Exception_NotFound $aenf) {
205                         if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) Tinebase_Core::getLogger()->notice(__CLASS__ . '::' . __METHOD__ . '::' . __LINE__
206                             . ' Do not send notification to non-existant user: ' . $aenf->getMessage());
207                     }
208                 }
209             }
210         }
211         return $recipients;
212     }
213 }