/** * Add an subscription. If the subscriber is disabled, throw an exception * If there is no subscriber, add one. * If there is no subscription for the content, add one * @param $email: user's email * @return void */ public function addSubscription($email, $user, $contentID, $languageID, $subscriptionType, $currentTime, $activate = true) { //1. insert into subscriber $ezcommentsINI = eZINI::instance('ezcomments.ini'); $subscriber = ezcomSubscriber::fetchByEmail($email); //if there is no data in subscriber for same email, save it if (is_null($subscriber)) { $subscriber = ezcomSubscriber::create(); $subscriber->setAttribute('user_id', $user->attribute('contentobject_id')); $subscriber->setAttribute('email', $email); if ($user->isAnonymous()) { $util = ezcomUtility::instance(); $hashString = $util->generateSusbcriberHashString($subscriber); $subscriber->setAttribute('hash_string', $hashString); } $subscriber->store(); eZDebugSetting::writeNotice('extension-ezcomments', 'Subscriber does not exist, added one', __METHOD__); $subscriber = ezcomSubscriber::fetchByEmail($email); } else { if ($subscriber->attribute('enabled') == false) { throw new Exception('Subscription can not be added because the subscriber is disabled.', self::ERROR_SUBSCRIBER_DISABLED); } } //3 insert into subscription table // if there is no data in ezcomment_subscription with given contentobject_id and subscriber_id $hasSubscription = ezcomSubscription::exists($contentID, $languageID, $subscriptionType, $email); if ($hasSubscription === false) { $subscription = ezcomSubscription::create(); $subscription->setAttribute('user_id', $user->attribute('contentobject_id')); $subscription->setAttribute('subscriber_id', $subscriber->attribute('id')); $subscription->setAttribute('subscription_type', $subscriptionType); $subscription->setAttribute('content_id', $contentID); $subscription->setAttribute('language_id', $languageID); $subscription->setAttribute('subscription_time', $currentTime); $defaultActivated = $ezcommentsINI->variable('CommentSettings', 'SubscriptionActivated'); if ($user->isAnonymous() && $defaultActivated !== 'true' && $activate === true) { $subscription->setAttribute('enabled', 0); $utility = ezcomUtility::instance(); $subscription->setAttribute('hash_string', $utility->generateSubscriptionHashString($subscription)); $subscription->store(); $result = ezcomSubscriptionManager::sendActivationEmail(eZContentObject::fetch($contentID), $subscriber, $subscription); if (!$result) { eZDebug::writeError("Error sending mail to '{$email}'", __METHOD__); } } else { $subscription->setAttribute('enabled', 1); $subscription->store(); } eZDebugSetting::writeNotice('extension-ezcomments', 'No existing subscription for this content and user, added one', __METHOD__); } }
/** * Test exists method */ public function testExists() { $subscriptionType = 'ezcomcomment'; //insert a subscriber object and subscription object $time = time(); $testemail = '*****@*****.**'; $subscriber = ezcomSubscriber::create(); $subscriber->setAttribute('email', $testemail); $subscriber->setAttribute('user_id', 10); $subscriber->setAttribute('enabled', 0); $subscriber->store(); $subscription = ezcomSubscription::create(); $subscription->setAttribute('user_id', 15); $subscription->setAttribute('subscriber_id', $subscriber->attribute('id')); $subscription->setAttribute('subscription_type', $subscriptionType); $subscription->setAttribute('content_id', '210'); $subscription->setAttribute('subscription_time', $time); $subscription->setAttribute('enabled', 1); $subscription->setAttribute('language_id', 2); $subscription->store(); //1. test if the subscription exists by contentID $exists = ezcomSubscription::exists('210', '2', $subscriptionType); $this->assertTrue($exists); $exists = ezcomSubscription::exists('20009', '2', $subscriptionType); $this->assertFalse($exists); $exists = ezcomSubscription::exists('20009', '2', 'othertypesssss'); $this->assertFalse($exists); $exists = ezcomSubscription::exists('20009', '2', 'othertypesssss', null, null); $this->assertNull($exists); //2. test if the subsription exists by contenetID and enabled $exists = ezcomSubscription::exists('210', '2', $subscriptionType, null, 0); $this->assertTrue($exists); $exists = ezcomSubscription::exists('210', '2', $subscriptionType, null, 1); $this->assertFalse($exists); $exists = ezcomSubscription::exists('210', '2', $subscriptionType, null, 12); $this->assertNull($exists); //3. test if the subscription exists by contentID and email $exists = ezcomSubscription::exists('210', '2', $subscriptionType, $testemail); $this->assertTrue($exists); $exists = ezcomSubscription::exists('210', '2', $subscriptionType, '*****@*****.**'); $this->assertFalse($exists); //4. test if the subscription exists by contentID, email and enabled $exists = ezcomSubscription::exists('210', '2', $subscriptionType, $testemail, 0); $this->assertTrue($exists); $exists = ezcomSubscription::exists('210', '2', $subscriptionType, $testemail, 1); $this->assertFalse($exists); $exists = ezcomSubscription::exists('210', '2', $subscriptionType, '*****@*****.**', 0); $this->assertFalse($exists); $exists = ezcomSubscription::exists('210', '2', $subscriptionType, $testemail, 46); $this->assertNull($exists); }
$changeNotification = true; } else { //email is disabled in setting but user logged in if (is_null($email) && !$user->isAnonymous()) { $changeNotification = true; $email = $user->attribute('email'); $comment->setAttribute('email', $email); } } } $commentManager = ezcomCommentManager::instance(); $commentManager->tpl = $tpl; $existingNotification = false; $addingResult = false; if ($changeNotification) { $existingNotification = ezcomSubscription::exists($contentObjectId, $languageId, 'ezcomcomment', $email); if (!$existingNotification) { $addingResult = $commentManager->addComment($comment, $user, null, true); } else { $addingResult = $commentManager->addComment($comment, $user); } } else { $addingResult = $commentManager->addComment($comment, $user); } if ($addingResult !== true) { $tpl->setVariable('error_message', $addingResult); $Result['content'] = $tpl->fetch('design:comment/add.tpl'); return $Result; } $tpl->setVariable('success', true); // add additional success message
$canEdit = $canEditResult['result']; $tpl->setVariable('can_edit', $canEdit); if (!$canEdit) { $Result['path'] = array(array('url' => false, 'text' => ezpI18n::tr('ezcomments/comment/edit', 'Edit comment'))); $Result['content'] = $tpl->fetch('design:comment/edit.tpl'); return $Result; } $contentID = $comment->attribute('contentobject_id'); // get if notification is enabled and notification value $ini = eZINI::instance('ezcomments.ini'); $formSettings = $ini->variable('FormSettings', 'AvailableFields'); $notificationEnabled = in_array('notificationField', $formSettings); $emailEnabled = in_array('email', $formSettings); $notified = null; if ($notificationEnabled) { $notified = ezcomSubscription::exists($contentID, $languageID, 'ezcomcomment', $comment->attribute('email')); $tpl->setVariable('notified', $notified); } if ($Module->isCurrentAction('UpdateComment')) { // Validate given input date against form setup $formTool = ezcomEditCommentTool::instance(); $formStatus = $formTool->checkVars(); if (!$formStatus) { // missing form data $tpl->setVariable('error_message', ezpI18n::tr('ezcomments/comment/add/form', 'There is a problem with your comment form ')); $tpl->setVariable('validation_messages', $formTool->messages()); return showComment($comment, $tpl); } //TODO: code from 93 can be implement in a class, see another TODO in add.php $formTool->fillObject($comment); $time = time();
/** * clean up subscription after updating comment * @see extension/ezcomments/classes/ezcomCommentManager#afterUpdatingComment($comment, $notified) */ public function afterUpdatingComment($comment, $notified, $time) { $user = eZUser::fetch($comment->attribute('user_id')); // if notified is true, add subscription, else cleanup the subscription on the user and content $contentID = $comment->attribute('contentobject_id'); $languageID = $comment->attribute('language_id'); $subscriptionType = 'ezcomcomment'; if (!is_null($notified)) { $subscriptionManager = ezcomSubscriptionManager::instance(); if ($notified === true) { //add subscription but not send activation try { $subscriptionManager->addSubscription($comment->attribute('email'), $user, $contentID, $languageID, $subscriptionType, $time, false); } catch (Exception $e) { eZDebug::writeError($e->getMessage(), __METHOD__); switch ($e->getCode()) { case ezcomSubscriptionManager::ERROR_SUBSCRIBER_DISABLED: return 'The subscriber is disabled.'; default: return false; } } } else { $subscriptionManager->deleteSubscription($comment->attribute('email'), $comment->attribute('contentobject_id'), $comment->attribute('language_id')); } } //3. update queue. If there is subscription, add one record into queue table // if there is subcription on this content, add one item into queue if (ezcomSubscription::exists($contentID, $languageID, $subscriptionType)) { $notification = ezcomNotification::create(); $notification->setAttribute('contentobject_id', $comment->attribute('contentobject_id')); $notification->setAttribute('language_id', $comment->attribute('language_id')); $notification->setAttribute('comment_id', $comment->attribute('id')); $notification->store(); eZDebugSetting::writeNotice('extension-ezcomments', 'There are subscriptions, added an update notification to the queue.', __METHOD__); } else { // todo: if there is no subscription on this content, consider to clean up the queue } return true; }