/** * Unit test for eZSubtreeNotificationRule::fetchUserList() */ public function testFetchUserList() { // Add a notification rule for admin on root $adminUserID = eZUser::fetchByName( 'admin' )->attribute( 'contentobject_id' ); $rule = new eZSubtreeNotificationRule( array( 'user_id' => $adminUserID, 'use_digest' => 0, 'node_id' => 2 ) ); $rule->store(); // Create a content object below node #2 $article = new ezpObject( 'article', 2 ); $article->title = __FUNCTION__; $article->publish(); $articleContentObject = $article->object; $list = eZSubtreeNotificationRule::fetchUserList( array( 2, 43 ), $articleContentObject ); $this->assertInternalType( 'array', $list, "Return value should have been an array" ); $this->assertEquals( 1, count( $list ), "Return value should have one item" ); $this->assertInternalType( 'array', $list[0] ); $this->assertArrayHasKey( 'user_id', $list[0] ); $this->assertArrayHasKey( 'use_digest', $list[0] ); $this->assertArrayHasKey( 'address', $list[0] ); $this->assertEquals( 14, $list[0]['user_id'] ); $this->assertEquals( 0, $list[0]['use_digest'] ); $this->assertEquals( '*****@*****.**', $list[0]['address'] ); }
function handlePublishEvent($event, &$parameters) { $versionObject = $event->attribute('content'); if (!$versionObject) { return eZNotificationEventHandler::EVENT_SKIPPED; } $contentObject = $versionObject->attribute('contentobject'); if (!$contentObject) { return eZNotificationEventHandler::EVENT_SKIPPED; } $contentNode = $contentObject->attribute('main_node'); if (!$contentNode) { return eZNotificationEventHandler::EVENT_SKIPPED; } // Notification should only be sent out when the object is published (is visible) if ($contentNode->attribute('is_invisible') == 1) { return eZNotificationEventHandler::EVENT_SKIPPED; } $contentClass = $contentObject->attribute('content_class'); if (!$contentClass) { return eZNotificationEventHandler::EVENT_SKIPPED; } if ($versionObject->attribute('version') != $contentObject->attribute('current_version')) { return eZNotificationEventHandler::EVENT_SKIPPED; } $tpl = eZTemplate::factory(); $tpl->resetVariables(); $parentNode = $contentNode->attribute('parent'); if (!$parentNode instanceof eZContentObjectTreeNode) { eZDebug::writeError('DB corruption: Node id ' . $contentNode->attribute('node_id') . ' is missing parent node.', __METHOD__); return eZNotificationEventHandler::EVENT_SKIPPED; } $parentContentObject = $parentNode->attribute('object'); if (!$parentContentObject instanceof eZContentObject) { eZDebug::writeError('DB corruption: Node id ' . $parentNode->attribute('node_id') . ' is missing object.', __METHOD__); return eZNotificationEventHandler::EVENT_SKIPPED; } $parentContentClass = $parentContentObject->attribute('content_class'); if (!$parentContentClass instanceof eZContentClass) { eZDebug::writeError('DB corruption: Object id ' . $parentContentObject->attribute('id') . ' is missing class object.', __METHOD__); return eZNotificationEventHandler::EVENT_SKIPPED; } $res = eZTemplateDesignResource::instance(); $res->setKeys(array(array('object', $contentObject->attribute('id')), array('node', $contentNode->attribute('node_id')), array('class', $contentObject->attribute('contentclass_id')), array('class_identifier', $contentClass->attribute('identifier')), array('parent_node', $contentNode->attribute('parent_node_id')), array('parent_class', $parentContentObject->attribute('contentclass_id')), array('parent_class_identifier', $parentContentClass != null ? $parentContentClass->attribute('identifier') : 0), array('depth', $contentNode->attribute('depth')), array('url_alias', $contentNode->attribute('url_alias')))); $tpl->setVariable('object', $contentObject); $notificationINI = eZINI::instance('notification.ini'); $emailSender = $notificationINI->variable('MailSettings', 'EmailSender'); $ini = eZINI::instance(); if (!$emailSender) { $emailSender = $ini->variable('MailSettings', 'EmailSender'); } if (!$emailSender) { $emailSender = $ini->variable("MailSettings", "AdminEmail"); } $tpl->setVariable('sender', $emailSender); $result = $tpl->fetch('design:notification/handler/ezsubtree/view/plain.tpl'); $subject = $tpl->variable('subject'); if ($tpl->hasVariable('message_id')) { $parameters['message_id'] = $tpl->variable('message_id'); } if ($tpl->hasVariable('references')) { $parameters['references'] = $tpl->variable('references'); } if ($tpl->hasVariable('reply_to')) { $parameters['reply_to'] = $tpl->variable('reply_to'); } if ($tpl->hasVariable('from')) { $parameters['from'] = $tpl->variable('from'); } if ($tpl->hasVariable('content_type')) { $parameters['content_type'] = $tpl->variable('content_type'); } $collection = eZNotificationCollection::create($event->attribute('id'), self::NOTIFICATION_HANDLER_ID, self::TRANSPORT); $collection->setAttribute('data_subject', $subject); $collection->setAttribute('data_text', $result); $collection->store(); $assignedNodes = $contentObject->parentNodes(true); $nodeIDList = array(); foreach ($assignedNodes as $node) { if ($node) { $pathString = $node->attribute('path_string'); $pathString = ltrim(rtrim($pathString, '/'), '/'); $nodeIDListPart = explode('/', $pathString); $nodeIDList = array_merge($nodeIDList, $nodeIDListPart); } } $nodeIDList[] = $contentNode->attribute('node_id'); $nodeIDList = array_unique($nodeIDList); $userList = eZSubtreeNotificationRule::fetchUserList($nodeIDList, $contentObject); $locale = eZLocale::instance(); $weekDayNames = $locale->attribute('weekday_name_list'); $weekDaysByName = array_flip($weekDayNames); foreach ($userList as $subscriber) { $item = $collection->addItem($subscriber['address']); if ($subscriber['use_digest'] == 0) { $settings = eZGeneralDigestUserSettings::fetchByUserId($subscriber['user_id']); if ($settings !== null && $settings->attribute('receive_digest') == 1) { $time = $settings->attribute('time'); $timeArray = explode(':', $time); $hour = $timeArray[0]; if ($settings->attribute('digest_type') == eZGeneralDigestUserSettings::TYPE_DAILY) { eZNotificationSchedule::setDateForItem($item, array('frequency' => 'day', 'hour' => $hour)); } else { if ($settings->attribute('digest_type') == eZGeneralDigestUserSettings::TYPE_WEEKLY) { $weekday = $weekDaysByName[$settings->attribute('day')]; eZNotificationSchedule::setDateForItem($item, array('frequency' => 'week', 'day' => $weekday, 'hour' => $hour)); } else { if ($settings->attribute('digest_type') == eZGeneralDigestUserSettings::TYPE_MONTHLY) { eZNotificationSchedule::setDateForItem($item, array('frequency' => 'month', 'day' => $settings->attribute('day'), 'hour' => $hour)); } } } $item->store(); } } } return eZNotificationEventHandler::EVENT_HANDLED; }