Commit 42c1db46 authored by Patrick Barroca's avatar Patrick Barroca

dev #46014 : newsletters subscription and unsubscription for user

parent f22547e2
......@@ -553,8 +553,10 @@ class AbonneController extends ZendAfi_Controller_Action {
return ;
if (!$form->isValid($_POST))
return;
$newsletters = Class_Newsletter::findAllBy(['id' => $this->_request->getParam('subscriptions', [])]);
$subscriptions = $this->_request->getParam('subscriptions', []);
$newsletters = $subscriptions ?
Class_Newsletter::findAllBy(['id' => $subscriptions]) :
[];
try {
$password = $this->_request->getParam('password');
......
......@@ -145,7 +145,10 @@ class Class_Newsletter extends Storm_Model_Abstract {
}
public function hasRecipient($user) {
public function hasRecipient($user, $with_blacklist = true) {
if ($with_blacklist && Class_Newsletter_Blacklist::isBlackListed($user->getMail(),$this->getId()))
return false;
foreach($this->getRecipientsGroups() as $group)
if ($group->hasUser($user))
return true;
......@@ -303,6 +306,32 @@ class Class_Newsletter extends Storm_Model_Abstract {
return $title;
}
public function unsubscribeUser($user) {
Class_UserGroupMembership::deleteBy(['user_id' => $user->getId(),
'user_group_id' => $this->getDedicatedGroup()->getId()]);
$this->unsubscribeByMail($user->getMail());
}
protected function subscribeToDedicatedGroup($user) {
$this->getDedicatedGroup()->addUser($user);
$this->save();
return $this;
}
public function subscribeUser($user) {
$is_recipient = $this->hasRecipient($user,false);
if ( $is_recipient
&& Class_Newsletter_Blacklist::isBlackListed($user->getMail(),$this->getId())) {
Class_Newsletter_Blacklist::deleteBy(['newsletter_id' => $this->getId(),
'mail' => $user->getMail()]);
return $this;
}
if (!$is_recipient)
return $this->subscribeToDedicatedGroup($user);
return $this;
}
public function unsubscribeByMail($mail) {
if (!$mail)
......
......@@ -33,6 +33,14 @@ class Newsletter_BlacklistLoader extends Storm_Model_Loader {
'newsletter_id' => $newsletter_id]);
return $blacklist->save();
}
public function isBlackListed($mail, $newsletter_id) {
if (!$mail)
return false;
return Class_Newsletter_Blacklist::findFirstBy(['newsletter_id' => $newsletter_id,
'mail' => $mail]);
}
}
......
......@@ -1589,32 +1589,25 @@ class Class_Users extends Storm_Model_Abstract {
public function setNewsletters($newsletters) {
foreach($this->getNewsletters() as $newsletter) {
if (in_array($newsletter,$newsletters)) {
$this->addNewsletter($newsletter);
continue;
}
$this->unsubscribeNewsletter($newsletter);
$my_newsletters = (new Storm_Model_Collection($this->getNewsletters()))
->collect('id')->getArrayCopy();
$new_newsletters = (new Storm_Model_Collection($newsletters))
->collect('id')->getArrayCopy();
$to_delete = array_diff($my_newsletters, $new_newsletters);
$to_add = array_diff($new_newsletters, $my_newsletters);
foreach ($to_delete as $newsletter_id) {
Class_Newsletter::find($newsletter_id)->unsubscribeUser($this);
}
return $this;
}
public function addNewsletter($newsletter) {
if ($this->isAlreadyRegisteredTo($newsletter))
return $this;
$newsletter->getDedicatedGroup()->addUser($this);
$newsletter->save();
foreach ($to_add as $newsletter_id) {
Class_Newsletter::find($newsletter_id)->subscribeUser($this);
}
return $this;
}
public function unsubscribeNewsletter($newsletter) {
Class_UserGroupMembership::deleteBy(['user_id' => $this->getId(),
'user_group_id' => $newsletter->getDedicatedGroup()->getId()]);
Class_Newsletter_Blacklist::unsubscribeByMail($this->getMail(), $newsletter->getId());
public function addNewsletter($newsletter) {
$newsletter->subscribeUser($this);
return $this;
}
......@@ -1629,12 +1622,6 @@ class Class_Users extends Storm_Model_Abstract {
}
public function isAlreadyRegisteredTo($newsletter) {
foreach($this->getDispatchs() as $dispatch)
if ($newsletter->getId() == $dispatch->getNewslettersId())
return true;
return false;
}
public function getLoginOrFullName() {
......@@ -1816,8 +1803,15 @@ class Class_Users extends Storm_Model_Abstract {
: '';
}
public function subscribeNewsletter($newsletter) {
$newsletter->addUser($this);
$newsletter->subscribeUser($this);
}
public function unsubscribeNewsletter($newsletter) {
$newsletter->unsubscribeUser($this);
}
}
......@@ -482,6 +482,12 @@ class AbonneControllerNewslettersUnsubscriptionTest extends AbonneControllerWith
}
/** @test */
public function unsubscribeActionShouldBlacklistUser() {
$this->assertCount(1,Class_Newsletter_Blacklist::findAllBy(['mail' => $this->marcus->getMail(),
'newsletter_id' => 14]));
}
/** @test **/
public function unsubscribeActionShouldAddNewsletterToUser() {
$this->assertNotContains($this->visites, $this->marcus->getNewsletters());
......@@ -559,7 +565,8 @@ class AbonneControllerNewslettersSubscriptionTwiceTest extends AbstractControlle
$user = $this->fixture('Class_Users',
['id' => 156,
'login' => 'tomato',
'password' => 'pwd']);
'password' => 'pwd',
'mail' => 'tomato@apo.fr']);
......@@ -567,13 +574,22 @@ class AbonneControllerNewslettersSubscriptionTwiceTest extends AbstractControlle
->logUser($user);
(new NewsletterFixtures())->createUsersAndNewsletter($this);
$user->addNewsletter(Class_Newsletter::find(1));
$this->dispatch('/opac/abonne/subscribe-newsletter/id/1',true);
ZendAfi_Auth::getInstance()->logUser($user);
}
/** @test **/
public function dailyNewsSubscribersShouldContainsTomatoOnlyOne() {
$this->assertEquals(1 , Class_UserGroupMembership::countBy(['user_id' => 156]));
$this->dispatch('/opac/abonne/subscribe-newsletter/id/1',true);
$this->assertTrue(Class_Newsletter::find(1)->hasRecipient(Class_Users::find(156)));
}
/** @test */
public function dynamicGroupShouldUnsubscribeUser() {
$this->dispatch('/opac/abonne/unsubscribe-newsletter/id/1',true);
$this->assertCount(1,Class_Newsletter_Blacklist::findAllBy(['mail' => Class_Users::find(156)->getMail(),
'newsletter_id' => 1]));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment