finalizing Filesystem crud notification email
authorPaul Mehrer <p.mehrer@metaways.de>
Tue, 6 Jun 2017 15:38:56 +0000 (17:38 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Wed, 7 Jun 2017 14:13:58 +0000 (16:13 +0200)
Change-Id: I69b2266aa64ca97cd7fc1fbea0d7400596de1cbe
Reviewed-on: http://gerrit.tine20.com/customers/4835
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Paul Mehrer <p.mehrer@metaways.de>
Tested-by: Paul Mehrer <p.mehrer@metaways.de>
tests/tine20/Tinebase/FileSystemTest.php
tine20/Filemanager/Model/Node.php
tine20/Tinebase/Export/Abstract.php
tine20/Tinebase/FileSystem.php
tine20/Tinebase/Model/Tree/Node.php

index f61387f..9ae3a52 100644 (file)
@@ -26,6 +26,7 @@ class Tinebase_FileSystemTest extends TestCase
     protected $_oldModLog;
     protected $_oldIndexContent;
     protected $_oldCreatePreview;
+    protected $_oldNotification;
 
     protected $_rmDir = array();
 
@@ -46,6 +47,7 @@ class Tinebase_FileSystemTest extends TestCase
         parent::setUp();
 
         $this->_rmDir = array();
+        $this->_oldNotification = Tinebase_Core::getConfig()->{Tinebase_Config::FILESYSTEM}->{Tinebase_Config::FILESYSTEM_ENABLE_NOTIFICATIONS};
         $this->_oldModLog = Tinebase_Core::getConfig()->{Tinebase_Config::FILESYSTEM}->{Tinebase_Config::FILESYSTEM_MODLOGACTIVE};
         $this->_oldIndexContent = Tinebase_Core::getConfig()->{Tinebase_Config::FILESYSTEM}->{Tinebase_Config::FILESYSTEM_INDEX_CONTENT};
         $this->_oldCreatePreview = Tinebase_Config::getInstance()->{Tinebase_Config::FILESYSTEM}->{Tinebase_Config::FILESYSTEM_CREATE_PREVIEWS};
@@ -68,6 +70,7 @@ class Tinebase_FileSystemTest extends TestCase
         $fsConfig->{Tinebase_Config::FILESYSTEM_MODLOGACTIVE} = $this->_oldModLog;
         $fsConfig->{Tinebase_Config::FILESYSTEM_INDEX_CONTENT} = $this->_oldIndexContent;
         $fsConfig->{Tinebase_Config::FILESYSTEM_CREATE_PREVIEWS} = $this->_oldCreatePreview;
+        $fsConfig->{Tinebase_Config::FILESYSTEM_ENABLE_NOTIFICATIONS} = $this->_oldNotification;
 
         Tinebase_FileSystem::getInstance()->resetBackends();
 
@@ -718,4 +721,42 @@ class Tinebase_FileSystemTest extends TestCase
         $dirObject = $fileObjectController->get($dirNode->object_id);
         static::assertEquals(0, $dirObject->size, 'direcotry size should not become negative, it should be set to 0 instead');
     }
+
+    public function testNotification()
+    {
+        $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
+        if (empty($smtpConfig)) {
+            $this->markTestSkipped('No SMTP config found: this is needed to send notifications.');
+        }
+
+        $mailer = Tinebase_Smtp::getDefaultTransport();
+        $mailer->flush();
+
+        Tinebase_Core::getConfig()->{Tinebase_Config::FILESYSTEM}->{Tinebase_Config::FILESYSTEM_ENABLE_NOTIFICATIONS} = true;
+        $this->_controller = new Tinebase_FileSystem();
+        Tinebase_FileSystem::getInstance()->resetBackends();
+
+        $testDir = '/shared/test';
+        $baseFolder = Filemanager_Controller_Node::getInstance()->createNodes(array($testDir), Tinebase_Model_Tree_FileObject::TYPE_FOLDER)->getFirstRecord();
+        $baseFolder->{Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION} = array(array(
+            Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_ID => Tinebase_Core::getUser()->getId(),
+            Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_TYPE => Tinebase_Acl_Rights::ACCOUNT_TYPE_USER,
+            Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACTIVE => true
+        ));
+        Filemanager_Controller_Node::getInstance()->update($baseFolder);
+
+        $handle = $this->_controller->fopen($this->_controller->getPathOfNode($baseFolder, true) . '/test.file', 'x');
+        $this->assertEquals('resource', gettype($handle), 'opening file failed');
+        $written = fwrite($handle, 'phpunit');
+        $this->assertEquals(7, $written);
+        $this->_controller->fclose($handle);
+
+        // mail foo?
+        // check mail
+        $messages = $mailer->getMessages();
+        $this->assertEquals(2, count($messages));
+        $headers = $messages[0]->getHeaders();
+        $this->assertEquals('filemanager notification', $headers['Subject'][0]);
+        $this->assertTrue(strpos($headers['To'][0], Tinebase_Core::getUser()->accountEmailAddress) !== false);
+    }
 }
index 0b6ef18..e1b440b 100644 (file)
@@ -50,4 +50,25 @@ class Filemanager_Model_Node extends Tinebase_Model_Tree_Node
     {
         return Tinebase_Model_Tree_FileObject::TYPE_FOLDER === $this->type;
     }
+
+    /**
+     * returns a URL with a deep link path to the node provided
+     *
+     * @param Tinebase_Model_Tree_Node $_record
+     * @return string
+     */
+    public static function getDeepLink($_record)
+    {
+        if (empty($_record->path)) {
+            $path = Tinebase_Model_Tree_Node_Path::createFromStatPath(Tinebase_FileSystem::getInstance()->getPathOfNode($_record, true));
+            $_record->path = $path->flatpath;
+        }
+
+        $path = explode('/', ltrim(Tinebase_Model_Tree_Node_Path::removeAppIdFromPath($_record->path, 'Filemanager'), '/'));
+        array_walk($path, function(&$val) {
+            $val = urlencode($val);
+        });
+
+        return Tinebase_Core::getUrl() . '#/Filemanager/showNode/' . join('/', $path);
+    }
 }
index 89c0110..d54b069 100644 (file)
@@ -520,6 +520,8 @@ abstract class Tinebase_Export_Abstract implements Tinebase_Record_IteratableInt
             'autoescape' => 'json',
             'cache' => false, //$cacheDir
         ));
+        /** @noinspection PhpUndefinedMethodInspection */
+        /** @noinspection PhpUnusedParameterInspection */
         $twig->getExtension('core')->setEscaper('json', function($twigEnv, $string, $charset) {
             return json_encode($string);
         });
@@ -590,6 +592,7 @@ abstract class Tinebase_Export_Abstract implements Tinebase_Record_IteratableInt
             if (null !== $this->_groupByProperty) {
                 $propertyValue = $record->{$this->_groupByProperty};
                 if (null !== $this->_groupByProcessor) {
+                    /** @var closure $fn */
                     $fn = $this->_groupByProcessor;
                     $fn($propertyValue);
                 }
index 5fc5e4a..493de80 100644 (file)
@@ -149,6 +149,8 @@ class Tinebase_FileSystem implements
         $config = Tinebase_Core::getConfig()->{Tinebase_Config::FILESYSTEM};
         $this->_modLogActive = true === $config->{Tinebase_Config::FILESYSTEM_MODLOGACTIVE};
         $this->_indexingActive = true === $config->{Tinebase_Config::FILESYSTEM_INDEX_CONTENT};
+        $this->_notificationActive = true === $config->{Tinebase_Config::FILESYSTEM_ENABLE_NOTIFICATIONS};
+        $this->_previewActive = true === $config->{Tinebase_Config::FILESYSTEM_CREATE_PREVIEWS};
 
         $this->_treeNodeBackend = null;
 
@@ -2929,8 +2931,8 @@ class Tinebase_FileSystem implements
         $fileSystem = Tinebase_FileSystem::getInstance();
 
         foreach($_accountIds as $accountId) {
-            //$locale = Tinebase_Translation::getLocale(Tinebase_Core::getPreference()->getValueForUser(Tinebase_Preference::LOCALE, $accountId));
-            //$translate = Tinebase_Translation::getTranslation('Filemanager', $locale);
+            $locale = Tinebase_Translation::getLocale(Tinebase_Core::getPreference()->getValueForUser(Tinebase_Preference::LOCALE, $accountId));
+            $translate = Tinebase_Translation::getTranslation('Filemanager', $locale);
 
             try {
                 $user = Tinebase_User::getInstance()->getFullUserById($accountId);
@@ -2938,32 +2940,35 @@ class Tinebase_FileSystem implements
                 continue;
             }
 
-            $messageBody = '<html><body>';
+            $translatedMsgHeader = $translate->_('The following files have changed:'); // _('The following files have changed:')
+            $fileStr = $translate->_('File'); // _('File')
+            $createdStr = $translate->_('has been created.'); // _('has been created.')
+            $updatedStr = $translate->_('has been changed.'); // _('has been changed.')
+            $deleteStr = $translate->_('has been deleted.'); // _('has been deleted.')
+
+            $messageBody = '<html><body><p>' . $translatedMsgHeader . '</p>';
             foreach($_crudActions as $fileNodeId => $changes) {
 
                 try {
                     $fileNode = $fileSystem->get($fileNodeId, true);
-                    $path = explode('/', ltrim($fileSystem->getPathOfNode($fileNode, true), '/'));
-                    array_walk($path, function(&$val) {
-                        $val = urldecode($val);
-                    });
-                    $path = '/' . join('/', $path);
                 } catch(Tinebase_Exception_NotFound $tenf) {
                     continue;
                 }
 
+                $path = Filemanager_Model_Node::getDeepLink($fileNode);
+
                 $messageBody .= '<p>';
 
                 foreach ($changes as $change => $foo) {
                     switch($change) {
                         case 'created':
-                            $messageBody .= 'File <a href="http://tine20.vagrant/#/Filemanager' . $path . '">' . $fileNode->name . '</a> has been created.<br/>';
+                            $messageBody .= $fileStr . ' <a href="' . $path . '">' . $fileNode->name . '</a> ' . $createdStr . '<br/>';
                             break;
                         case 'updated':
-                            $messageBody .= 'File <a href="http://tine20.vagrant/#/Filemanager' . $path . '">' . $fileNode->name . '</a> has been changed.<br/>';
+                            $messageBody .= $fileStr . ' <a href="' . $path . '">' . $fileNode->name . '</a> ' . $updatedStr . '<br/>';
                             break;
                         case 'deleted':
-                            $messageBody .= 'File <a href="http://tine20.vagrant/#/Filemanager' . $path . '">' . $fileNode->name . '</a> has been deleted.<br/>';
+                            $messageBody .= $fileStr . ' <a href="' . $path . '">' . $fileNode->name . '</a> ' . $deleteStr . '<br/>';
                             break;
                         default:
                             // should not happen!
@@ -2974,7 +2979,9 @@ class Tinebase_FileSystem implements
             }
             $messageBody .= '</body></html>';
 
-            Tinebase_Notification::getInstance()->send($accountId, $user->contact_id, 'filemanager notification', '', $messageBody);
+            $translatedSubject = $translate->_('filemanager notification'); // _('filemanager notification')
+
+            Tinebase_Notification::getInstance()->send($accountId, array($user->contact_id), $translatedSubject, '', $messageBody);
         }
     }
 
index f1e8fbc..e000558 100644 (file)
@@ -238,7 +238,7 @@ class Tinebase_Model_Tree_Node extends Tinebase_Record_Abstract
         }
 
         $invalid = array();
-        if (!empty($this->revisionProps)) {
+        if (!empty($this->xprops(static::XPROPS_REVISION))) {
             if (count($this->revisionProps) > 4 || !isset($this->revisionProps[static::XPROPS_REVISION_NODE_ID])
                     || !isset($this->revisionProps[static::XPROPS_REVISION_MONTH]) ||
                     !isset($this->revisionProps[static::XPROPS_REVISION_ON]) ||
@@ -246,7 +246,7 @@ class Tinebase_Model_Tree_Node extends Tinebase_Record_Abstract
                 $invalid[] = 'revisionProps';
             }
         }
-        if (!empty($this->notificationProps)) {
+        if (!empty($this->xprops(static::XPROPS_NOTIFICATION))) {
             foreach ($this->notificationProps as $val) {
                 foreach ($val as $key => $val1) {
                     if (!in_array($key, array(