Merge branch '2014.11' into 2014.11-develop
[tine20] / tine20 / Calendar / Controller / EventNotifications.php
index e3d0d1f..8d4d061 100644 (file)
                     }
                 }
                 break;
+            case 'booked':
             case 'created':
             case 'deleted':
                 foreach($_event->attendee as $attender) {
      * @param string                     $_notificationLevel
      * @param array                      $_updates
      * @return void
+     *
+     * TODO this needs major refactoring
      */
     public function sendNotificationToAttender(Calendar_Model_Attender $_attender, $_event, $_updater, $_action, $_notificationLevel, $_updates = NULL)
     {
                 $sendLevel = is_object($organizer) && $_attender->getEmail() == $organizer->getPreferedEmailAddress() ? 40 : 30;
                 $sendOnOwnActions = false;
             }
-            
+
+            $recipients = array($attendee);
+
+            $this->_handleResourceEditors($_attender, $_notificationLevel, $recipients, $_action, $sendLevel);
+
             // check if user wants this notification NOTE: organizer gets mails unless she set notificationlevel to NONE
             // NOTE prefUser is organizer for external notifications
             if (($attendeeAccountId == $_updater->getId() && ! $sendOnOwnActions) 
                     . " Preferred notification level not reached -> skipping notification for {$_attender->getEmail()}");
                 return;
             }
-            
+
             $method = NULL; // NOTE $method gets set in _getSubject as referenced param
-            $messageSubject = $this->_getSubject($_event, $_notificationLevel, $_action, $_updates, $timezone, $locale, $translate, $method);
-            
+            $messageSubject = $this->_getSubject($_event, $_notificationLevel, $_action, $_updates, $timezone, $locale, $translate, $method, $_attender);
+
             // we don't send iMIP parts to external attendee if config is active
             if (Calendar_Config::getInstance()->get(Calendar_Config::DISABLE_EXTERNAL_IMIP) && ! $attendeeAccountId) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
                     . " External iMIP is disabled.");
                 $method = NULL;
             }
-            
+
             $view = new Zend_View();
             $view->setScriptPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views');
             
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " body: $messageBody");
 
             $sender = $_action == 'alarm' ? $prefUser : $_updater;
-
-            $recipients = array($attendee);
-            if ($_attender->user_type == Calendar_Model_Attender::USERTYPE_RESOURCE
-                && Calendar_Config::getInstance()->get(Calendar_Config::RESOURCE_MAIL_FOR_EDITORS)
-            ) {
-                $recipients = array_merge($recipients,
-                    Calendar_Controller_Resource::getInstance()->getNotificationRecipients(
-                        Calendar_Controller_Resource::getInstance()->get($_attender->user_id)
-                    )
-                );
-            }
             Tinebase_Notification::getInstance()->send($sender, $recipients, $messageSubject, $messageBody, $calendarPart, $attachments);
         } catch (Exception $e) {
             Tinebase_Exception::log($e);
             return;
         }
     }
+
+     /**
+      * @param $attender
+      * @param $_notificationLevel
+      * @param $recipients
+      * @param $action
+      * @param $sendLevel
+      * @return bool
+      */
+     protected function _handleResourceEditors($attender, $_notificationLevel, &$recipients, &$action, &$sendLevel)
+     {
+         // Add additional recipients for resources
+         if ($attender->user_type !== Calendar_Model_Attender::USERTYPE_RESOURCE
+         || ! Calendar_Config::getInstance()->get(Calendar_Config::RESOURCE_MAIL_FOR_EDITORS)) {
+             
+             return true;
+         }
+         
+         $resource = Calendar_Controller_Resource::getInstance()->get($attender->user_id);
+         if ($resource->suppress_notification) {
+             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                     . " Do not send Notifications for this resource: ". $resource->name);
+             return true;
+         }
+
+         if ($action == 'created') {
+             $action = 'booked';
+         }
+
+         // Consider all notification level (Resource Users have a send level of 30)
+         $sendLevel = self::NOTIFICATION_LEVEL_ATTENDEE_STATUS_UPDATE;
+         $recipients = array_merge($recipients,
+             Calendar_Controller_Resource::getInstance()->getNotificationRecipients(
+                 Calendar_Controller_Resource::getInstance()->get($attender->user_id),
+                 $_notificationLevel
+             )
+         );
+     }
     
     /**
      * get notification subject and method
      * @param Zend_Locale $locale
      * @param Zend_Translate $translate
      * @param atring $method
+     * @param Calendar_Model_Attender
      * @return string
+     * @throws Tinebase_Exception_UnexpectedValue
      */
-    protected function _getSubject($_event, $_notificationLevel, $_action, $_updates, $timezone, $locale, $translate, &$method)
+    protected function _getSubject($_event, $_notificationLevel, $_action, $_updates, $timezone, $locale, $translate, &$method, Calendar_Model_Attender $attender)
     {
         $startDateString = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_event->dtstart, $timezone, $locale);
         $endDateString = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_event->dtend, $timezone, $locale);
                 $messageSubject = sprintf($translate->_('Event invitation "%1$s" at %2$s'), $_event->summary, $startDateString);
                 $method = Calendar_Model_iMIP::METHOD_REQUEST;
                 break;
+            case 'booked':
+                if ($attender->user_type !== Calendar_Model_Attender::USERTYPE_RESOURCE) {
+                    throw new Tinebase_Exception_UnexpectedValue('not a resource');
+                }
+                $resource = Calendar_Controller_Resource::getInstance()->get($attender->user_id);
+                $messageSubject = sprintf(
+                    $translate->_('Resource "%1$s" was booked for "%2$s" at %3$s'),
+                    $resource->name,
+                    $_event->summary,
+                    $startDateString
+                );
+                $method = Calendar_Model_iMIP::METHOD_REQUEST;
+                break;
             case 'deleted':
                 $messageSubject = sprintf($translate->_('Event "%1$s" at %2$s has been canceled' ), $_event->summary, $startDateString);
                 $method = Calendar_Model_iMIP::METHOD_CANCEL;
         $attachments = array($attachment);
         
         // add other attachments (only on invitation)
-        if ($_action == 'created') {
+        if ($_action == 'created' || $_action == 'booked') {
             $eventAttachments = $this->_getEventAttachments($event);
             $attachments = array_merge($attachments, $eventAttachments);
         }