Commit c55476a2 authored by efalcy's avatar efalcy

dev #46014: subscriber page : subscribe/unsubscribe to newsletters

parent d8d3ef3b
......@@ -109,7 +109,7 @@ class AbonneController extends ZendAfi_Controller_Action {
}
$this->_user->removeNewsletter($newsletter);
$this->_user->unsubscribeNewsletter($newsletter);
if ($this->_user->save()) {
$this->_helper->notify(sprintf('Vous êtes désinscrit de la liste de diffusion: %s',
......@@ -520,14 +520,14 @@ class AbonneController extends ZendAfi_Controller_Action {
$newsletters = Class_Newsletter::getPublishedNewsletters();
xdebug_break();
if (count($newsletters)>0) {
foreach($newsletters as $nl)
$checked_subscriptions = [];
foreach($newsletters as $nl) {
$subscriptions->addMultiOption($nl->getId(), $nl->getTitre());
$checked_subscriptions = array();
foreach($user->getNewsletters() as $nl)
$checked_subscriptions []= $nl->getId();
if ($nl->hasRecipient($user))
$checked_subscriptions []= $nl->getId();
}
$subscriptions->setValue($checked_subscriptions);
$form
->addElement($subscriptions)
......@@ -546,46 +546,48 @@ class AbonneController extends ZendAfi_Controller_Action {
public function editAction() {
$form = $this->_userForm($this->_user);
$this->view->form = $form;
$this->view->help = nl2br(Class_AdminVar::get('AIDE_FICHE_ABONNE'));
if ($this->getRequest()->isPost() && $form->isValid($_POST)) {
$newsletters = [];
if (!$this->getRequest()->isPost())
return ;
if (!$form->isValid($_POST))
return;
$newsletters = [];
$newsletters_id = $this->_request->getParam('subscriptions', []);
foreach($newsletters_id as $nl_id)
$newsletters []= Class_Newsletter::find($nl_id);
$newsletters_id = $this->_request->getParam('subscriptions', []);
foreach($newsletters_id as $nl_id)
$newsletters []= Class_Newsletter::find($nl_id);
try {
$password = $this->_request->getParam('password');
if (empty($password))
$password = $this->_user->getPassword();
try {
$password = $this->_request->getParam('password');
if (empty($password))
$password = $this->_user->getPassword();
$this->_user->updateSIGBOnSave();
$this->_user->updateSIGBOnSave();
$fields_to_save = Class_AdminVar::getChampsFicheUtilisateur();
$attributes = [];
foreach($fields_to_save as $field)
$attributes[$field] = $this->_request->getParam($field);
$fields_to_save = Class_AdminVar::getChampsFicheUtilisateur();
$attributes = [];
foreach($fields_to_save as $field)
$attributes[$field] = $this->_request->getParam($field);
$this->_user
->updateAttributes($attributes)
->setPassword($password)
->setNewsletters($newsletters);
$this->_user
->updateAttributes($attributes)
->setPassword($password)
->setNewsletters($newsletters);
if ($this->_user->save()) {
$this->_redirect('/abonne/fiche');
}
if ($this->_user->save()) {
$this->_redirect('/abonne/fiche');
}
$form->addDecorator('Errors');
foreach($this->_user->getErrors() as $error)
$form->addError($error);
} catch(Exception $e) {
$form->addError($e->getMessage());
$form->addDecorator('Errors');
$form->addDecorator('Errors');
foreach($this->_user->getErrors() as $error)
$form->addError($error);
} catch(Exception $e) {
$form->addError($e->getMessage());
$form->addDecorator('Errors');
}
}
$this->view->form = $form;
$this->view->help = nl2br(Class_AdminVar::get('AIDE_FICHE_ABONNE'));
}
......
......@@ -141,6 +141,13 @@ class Class_Newsletter extends Storm_Model_Abstract {
return array_unique($ids);
}
public function hasRecipient($user) {
foreach($this->getRecipientsGroups() as $group)
if ($group->hasUser($user)) return true;
return false;
}
public function getRecipientsGroups() {
$groups = [$this->getDedicatedGroup()];
......
......@@ -54,11 +54,4 @@ class Class_Newsletter_Blacklist extends Storm_Model_Abstract {
protected $_belongs_to = ['newsletter' => ['model' => 'Class_Newsletter']];
protected $_default_attribute_values = ['collected' => 0,
'ended_on' => null];
}
......@@ -250,6 +250,21 @@ class Class_UserGroup extends Storm_Model_Abstract {
}
public function hasUser($user) {
if ($this->isManual()) {
foreach($this->getUsersIds() as $id) {
if ($id==$user->getId()) return true;
}
return false;
}
if ($user->getRoleLevel() != $this->getRoleLevel())
return false;
if ($library = $this->getLibrary())
return ($this->getLibrary() == $user->getLibrary());
return true;
}
public function numberOfDynamicUsers() {
return $this->isDynamic() ?
Class_Users::countBy($this->_getDynamicParams()) : 0;
......
......@@ -327,11 +327,6 @@ class Class_Users extends Storm_Model_Abstract {
$_table_primary = 'ID_USER',
$_loader_class = 'UsersLoader',
$_has_many = [
'subscriptions' => ['model' => 'Class_NewsletterSubscription',
'role' => 'user',
'dependents' => 'delete'],
'newsletters' => ['through' => 'subscriptions'],
'dispatch_users' => ['model' => 'Class_Newsletter_DispatchUser',
'role' => 'user'],
......@@ -805,7 +800,15 @@ class Class_Users extends Storm_Model_Abstract {
return $titres;
}
public function getNewsletters() {
$newsletters = [];
foreach (Class_Newsletter::getPublishedNewsletters() as $newsletter) {
if ($newsletter->hasRecipient($this))
$newsletters[]=$newsletter;
}
return $newsletters;
}
/**
* @return array
......@@ -1575,14 +1578,49 @@ class Class_Users extends Storm_Model_Abstract {
}
public function setNewsletters($newsletters) {
Class_Newsletter_Blacklist::deleteBy(['mail' =>$this->getMail(),'newsletter_id' => -1]);
foreach(Class_Newsletter::findAll() as $newsletter) {
if (in_array($newsletter,$newsletters)) {
$this->addNewsletter($newsletter);
continue;
}
$this->unsubscribeNewsletter($newsletter);
}
return $this;
}
public function addNewsletter($newsletter) {
return $this->isAlreadyRegisteredTo($newsletter) ? $this : parent::addNewsletter($newsletter);
if ($this->isAlreadyRegisteredTo($newsletter))
return $this;
$newsletter->getDedicatedGroup()->addUser($this);
$newsletter->save();
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());
return $this;
}
public function unsubscribeAllNewsletters() {
foreach (Class_Newsletter::findAll() as $newsletter)
Class_UserGroupMembership::deleteBy(['user_id' => $this->getId(),
'user_group_id' => $newsletter->getDedicatedGroup()->getId()]);
Class_Newsletter_Blacklist::unsubscribeByMail($this->getMail(),-1);
return $this;
}
public function isAlreadyRegisteredTo($newsletter) {
return 0 < Class_NewsletterSubscription::countBy(['user_id' => $this->getId(),
'newsletter_id' => $newsletter->getId()]);
foreach($this->getDispatchs() as $dispatch)
if ($newsletter->getId() == $dispatch->getNewslettersId())
return true;
return false;
}
......
......@@ -16,11 +16,11 @@
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_View_Helper_Abonne_Newsletters extends ZendAfi_View_Helper_Abonne_Abstract {
public function abonne_newsletters($user) {
if (count(Class_Newsletter::getLoader()->findAll()) == 0)
if (count(Class_Newsletter::findAll()) == 0)
return '';
$newsletter_info = $this->view->_("Vous n'êtes abonné à aucune lettre d'information");
......
......@@ -16,7 +16,7 @@
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_View_Helper_Admin_SubscribeUsers extends ZendAfi_View_Helper_BaseHelper {
protected $_users = [];
......@@ -91,7 +91,7 @@ class ZendAfi_View_Helper_Admin_SubscribeUsers extends ZendAfi_View_Helper_BaseH
$this->searchForm($this->_search);
}
protected function searchForm($search) {
return $this->_findUsersForm($this->_search)->render()
.$this->_subscribeUsersForm($this->_search)->render();
......@@ -110,9 +110,9 @@ class ZendAfi_View_Helper_Admin_SubscribeUsers extends ZendAfi_View_Helper_BaseH
protected function _deleteLinkOf($user) {
return $this->isReadOnly() ?
'' :
$this->view->tagAnchor($this->view->url(['delete' => $user->getId(),
return $this->isReadOnly() ?
'' :
$this->view->tagAnchor($this->view->url(['delete' => $user->getId(),
'search' => $this->_search]),
$this->view->boutonIco('type=del'));
}
......@@ -126,7 +126,7 @@ class ZendAfi_View_Helper_Admin_SubscribeUsers extends ZendAfi_View_Helper_BaseH
->setAttrib('class' , 'form');
$user_checkboxes = new Zend_Form_Element_MultiCheckbox('users');
foreach($users_found as $user)
foreach($users_found as $user)
$user_checkboxes->addMultiOption($user->getId(),
sprintf('%s %s - %s',
$user->getNom(),
......@@ -135,11 +135,11 @@ class ZendAfi_View_Helper_Admin_SubscribeUsers extends ZendAfi_View_Helper_BaseH
return $form
->addElement($user_checkboxes)
->addElement('button', 'select_all',
->addElement('button', 'select_all',
['label' => $this->view->_('Tout sélectionner'),
'onclick' => '$(\'#user_subscribe input\').attr(\'checked\', true);'])
->addElement('submit', 'submit', ['label' => $this->_getSubmitLabel()]);
}
......
......@@ -19,7 +19,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once 'tests/fixtures/NewsletterFixtures.php';
abstract class Admin_NewsletterControllerTestCase
extends Admin_AbstractControllerTestCase {
......@@ -27,74 +27,19 @@ abstract class Admin_NewsletterControllerTestCase
public function setUp() {
parent::setup();
$newsletter_fixtures = new NewsletterFixtures();
$newsletter_fixtures->createUsersAndNewsletter($this);
$this->mock_transport = new MockMailTransport();
Zend_Mail::setDefaultTransport($this->mock_transport);
Class_Newsletter::setTimeSource(new TimeSourceForTest('2005-03-27 12:30:00'));
Class_UserGroup::setMemoryCleaner(function() {});
$this->onLoaderOfModel('Class_PanierNotice')
->whenCalled('findAllBelongsToAdmin')
->answers([]);
$pat = $this->fixture('Class_Users',
['id' => 23,
'login' => 'pat',
'password' => 'tap',
'mail' => 'pat@pat.info']);
$laurent = $this->fixture('Class_Users',
['id' => 22,
'login' => 'lla',
'password' => 'all',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
'idabon' => '0007',
'id_site' => 1,
'mail' => 'lla@all.info']);
$anonymous = $this->fixture('Class_Users',
['id' => 66,
'login' => 'anonymous',
'password' => 'leavemealone',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
'idabon' => '0009',
'id_site' => 1,
'mail' => 'anonymous@all.info']);
$my_group = $this->fixture('Class_UserGroup',
['id' => 14,
'libelle' => 'My Group',
'users' => [$pat, $laurent]]);
$other_group = $this->fixture('Class_UserGroup',
['id' => 15,
'libelle' => 'My Other Group',
'group_type' => Class_UserGroup::TYPE_DYNAMIC,
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB]);
$this->fixture('Class_Newsletter',
['id' => 1,
'titre' => 'Nouveautés classique',
'contenu' => 'Notre sélection du mois',
'id_catalogue' => '',
'id_panier' => '',
'user_groups' => [$my_group, $other_group]]);
Class_Newsletter::find(1)->setLastDistributionDateWithFormat();
$this->fixture('Class_Newsletter',
['id' => 2,
'titre' => 'Animations',
'contenu' => 'Pour les jeunes',
'last_distribution_date' => null,
'id_catalogue' => '',
'id_panier' => '',
'expediteur' => 'bokehrulez@linuxfr.org',
'user_groups' => [$my_group]]);
}
}
......
......@@ -19,7 +19,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once 'AbstractControllerTestCase.php';
require_once 'tests/fixtures/NewsletterFixtures.php';
abstract class AbstractAbonneControllerNewslettersTestCase extends AbstractControllerTestCase {
protected $_storm_default_to_volatile=true;
......@@ -29,7 +29,6 @@ abstract class AbstractAbonneControllerNewslettersTestCase extends AbstractContr
Class_AdminVar::newInstanceWithId('CHAMPS_FICHE_UTILISATEUR',
['valeur' => 'pseudo;nom;prenom;mail;password']);
$this->marcus = $this->fixture('Class_Users',['id'=> 10,
'prenom' => 'Marcus',
'nom' => 'Miller',
......@@ -55,9 +54,8 @@ abstract class AbstractAbonneControllerNewslettersTestCase extends AbstractContr
abstract class AbonneControllerWithTwoNewslettersTestCase extends AbstractAbonneControllerNewslettersTestCase {
public function setUp() {
parent::setUp();
Class_NewsletterSubscription::beVolatile();
xdebug_break();
(new NewsletterFixtures())->createUsersAndNewsletter($this);
$this->concerts = $this->fixture('Class_Newsletter',
['id' => 12,
'titre' =>'Concerts',
......@@ -77,15 +75,12 @@ abstract class AbonneControllerWithTwoNewslettersTestCase extends AbstractAbonne
class AbonneControllerNewslettersFicheActionWithNoExistingNewslettersTest extends AbstractAbonneControllerNewslettersTestCase {
public function setUp() {
parent::setUp();
$this->marcus->setNewsletters([]);
$this->dispatch('/opac/abonne');
}
public function testInfoAbonnementNewsletterConcerts () {
$this->assertNotQueryContentContains('div.abonneFiche',
"Vous n'êtes abonné à aucune lettre d'information");
"Vous n'êtes abonné à aucune lettre d'information",$this->_response->getBody());
}
public function testLinkToEditInformationsInfoAbonnements() {
......@@ -125,7 +120,7 @@ class AbonneControllerNewslettersFicheActionWithOneSubscriptionTest extends Abon
public function testInfoAbonnementNewsletterConcerts () {
$this->assertQueryContentContains('div.abonneFiche',
"Vous êtes abonné à la lettre d'information: Concerts");
"Vous êtes abonné à la lettre d'information: Concerts",$this->_response->getBody());
}
......@@ -262,6 +257,52 @@ class AbonneControllerNewslettersEditActionTest extends AbonneControllerWithTwoN
class AbonneControllerNewsletterPostNewsletterTest extends AbonneControllerWithTwoNewslettersTestCase {
public function setUp() {
parent::setUp();
$this->postDispatch('/opac/abonne/edit', ['nom' => 'marcus',
'prenom' => '',
'pseudo' => 'M2',
'password' => '',
'confirm_password' => '',
'mail' => 'marcus@free.fr',
'subscriptions' => [14]]);
}
public function testConcertsIsNotChecked() {
$this->assertFalse($this->concerts->hasRecipient($this->marcus));
}
public function testVisitesIsChecked() {
$this->assertTrue($this->visites->hasRecipient($this->marcus));
}
}
class AbonneControllerNewsletterPostNewsletterNoSubscriptionTest extends AbonneControllerWithTwoNewslettersTestCase {
public function setUp() {
parent::setUp();
$this->postDispatch('/opac/abonne/edit', ['nom' => 'marcus',
'prenom' => '',
'pseudo' => 'M2',
'password' => '',
'confirm_password' => '',
'mail' => 'marcus@free.fr',
'subscriptions' => []]);
}
public function testConcertsIsNotChecked() {
$this->assertFalse($this->concerts->hasRecipient($this->marcus));
}
public function testVisitesIsNotChecked() {
$this->assertFalse($this->visites->hasRecipient($this->marcus));
}
}
class AbonneControllerNewslettersSaveActionTest extends AbonneControllerWithTwoNewslettersTestCase {
public function setUp() {
......
......@@ -143,7 +143,7 @@ class NewsletterControllerValidUnsubscribeConfirmTest extends NewsletterControll
/** @test */
public function harlockShouldBeUnsubscribed() {
$this->assertEquals(['c.harlock@arcadia.net'],Class_Newsletter_Blacklist::getMailForNewsletter(34));
$this->assertEquals(['c.harlock@arcadia.net'],Class_Newsletter_Blacklist::getMailsForNewsletter(34));
}
}
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class NewsletterFixtures {
public function createUsersAndNewsletter($controller) {
Class_Newsletter::setTimeSource(new TimeSourceForTest('2005-03-27 12:30:00'));
Class_UserGroup::setMemoryCleaner(function() {});
$pat = $controller->fixture('Class_Users',
['id' => 23,
'login' => 'pat',
'password' => 'tap',
'mail' => 'pat@pat.info']);
$laurent = $controller->fixture('Class_Users',
['id' => 22,
'login' => 'lla',
'password' => 'all',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
'idabon' => '0007',
'id_site' => 1,
'mail' => 'lla@all.info']);
$anonymous = $controller->fixture('Class_Users',
['id' => 66,
'login' => 'anonymous',
'password' => 'leavemealone',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
'idabon' => '0009',
'id_site' => 1,
'mail' => 'anonymous@all.info']);
Class_Newsletter_Blacklist::unsubscribeByMail($anonymous->getMail(),-1);
$my_group = $controller->fixture('Class_UserGroup',
['id' => 14,
'libelle' => 'My Group',
'users' => [$pat, $laurent]]);
$other_group = $controller->fixture('Class_UserGroup',
['id' => 15,
'libelle' => 'My Other Group',
'group_type' => Class_UserGroup::TYPE_DYNAMIC,
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB]);
$controller->fixture('Class_Newsletter',
['id' => 1,
'titre' => 'Nouveautés classique',
'contenu' => 'Notre sélection du mois',
'id_catalogue' => '',
'id_panier' => '',
'user_groups' => [$my_group, $other_group]]);
Class_Newsletter::find(1)->setLastDistributionDateWithFormat();
$controller->fixture('Class_Newsletter',
['id' => 2,
'titre' => 'Animations',
'contenu' => 'Pour les jeunes',
'last_distribution_date' => null,
'id_catalogue' => '',
'id_panier' => '',
'expediteur' => 'bokehrulez@linuxfr.org',
'user_groups' => [$my_group]]);
}
}
?>
\ No newline at end of file
......@@ -21,7 +21,7 @@
require_once 'Class/Newsletter.php';
require_once 'ModelTestCase.php';
class NewsletterFixtures {
class NewsletterFixture {
public static function nouveautesClassique() {
return array('id' => 1,
'titre' => 'Nouveautés classique',
......@@ -44,7 +44,7 @@ class NewsletterFixtures {
class NewsletterTestFindAll extends ModelTestCase {
public function setUp() {
parent::setUp();
$this->_setFindAllExpectation('Class_Newsletter', NewsletterFixtures::all());
$this->_setFindAllExpectation('Class_Newsletter', NewsletterFixture::all());
$this->newsletters = Class_Newsletter::getLoader()->findAll();
}
......@@ -69,14 +69,14 @@ class NewsletterTestFindAll extends ModelTestCase {
class NewsletterTestFindById extends ModelTestCase {
public function testFindByIdOneReturnsNouveautes() {
$this->_setFindExpectation('Class_Newsletter', NewsletterFixtures::nouveautesClassique(), 1);
$this->_setFindExpectation('Class_Newsletter', NewsletterFixture::nouveautesClassique(), 1);
$nouveaute = Class_Newsletter::getLoader()->find(1);
$this->assertEquals(1, $nouveaute->getId());
}
public function testFindByIdTwoReturnsAnimations() {
$this->_setFindExpectation('Class_Newsletter', NewsletterFixtures::animations(), 2);
$this->_setFindExpectation('Class_Newsletter', NewsletterFixture::animations(), 2);
$animations = Class_Newsletter::getLoader()->find(2);
$this->assertEquals(2, $animations->getId());
}
......
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