Commit f22547e2 authored by Patrick Barroca's avatar Patrick Barroca

dev #46014 : display newsletters via group subscriptions

parent 5e5e8d0e
......@@ -53,12 +53,21 @@
*
*/
class NewsletterLoader extends Storm_Model_Loader {
public function getPublishedNewsletters() {
return Class_Newsletter::findAllBy(['draft' => 0]);
}
}
class Class_Newsletter extends Storm_Model_Abstract {
use Trait_TimeSource, Trait_Translator;
const URL_PLACEHOLDER = '{{URL}}';
protected $_table_name = 'newsletters';
protected $_loader_class = 'NewsletterLoader';
protected $_has_many =
[
......@@ -84,12 +93,6 @@ class Class_Newsletter extends Storm_Model_Abstract {
}
public function getPublishedNewsletters() {
return Class_Newsletter::findAllBy(['draft' => 0]);
}
public function send() {
if (!$dispatch = $this->getLastDispatchInProgress())
$dispatch = Class_Newsletter_Dispatch::newFrom($this);
......@@ -141,9 +144,11 @@ 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;
if ($group->hasUser($user))
return true;
return false;
}
......@@ -370,6 +375,15 @@ class Class_Newsletter extends Storm_Model_Abstract {
});
return $groups;
}
public function addUser($user) {
$this->getDedicatedGroup()
->addUser($user)
->save();
return $this;
}
}
......
......@@ -800,11 +800,14 @@ class Class_Users extends Storm_Model_Abstract {
return $titres;
}
public function getNewsletters() {
return array_filter(Class_Newsletter::getPublishedNewsletters(),
function($newsletter) {
$newsletter->hasRecipient($this);
});
$newsletters = [];
foreach(Class_Newsletter::getPublishedNewsletters() as $newsletter)
if ($newsletter->hasRecipient($this))
$newsletters[] = $newsletter;
return $newsletters;
}
......@@ -1812,4 +1815,9 @@ class Class_Users extends Storm_Model_Abstract {
? $labels[$title]
: '';
}
public function subscribeNewsletter($newsletter) {
$newsletter->addUser($this);
}
}
......@@ -23,24 +23,30 @@ class ZendAfi_View_Helper_Abonne_Newsletters extends ZendAfi_View_Helper_Abonne_
if (count(Class_Newsletter::findAll()) == 0)
return '';
$newsletter_info = $this->view->_("Vous n'êtes abonné à aucune lettre d'information");
if (count($user->getNewsletters()) > 0) {
$titres = $user->getTitresNewsletters();
$newsletter_info = $this->view->_('Vous êtes abonné');
$newsletter_info .= count($titres) > 1 ? $this->view->_(" aux lettres d'information: ") : $this->view->_(" à la lettre d'information: ");
$newsletter_info .= implode(', ', $titres);
}
$newsletter_info = $this->_newsletterInfo($user);
$action_url = $this->view->url(['controller' => 'abonne', 'action' => 'edit']);
return $this->tagFicheAbonne(
'<p>'.$newsletter_info.'</p>'.
'<p>' . $newsletter_info . '</p>'.
$this->view->tagAnchor($action_url,
$this->view->_('Modifier mes abonnements')),
'newsletter',
$action_url);
}
}
?>
\ No newline at end of file
protected function _newsletterInfo($user) {
if (!$newsletters = $user->getNewsletters())
return $this->view->_("Vous n'êtes abonné à aucune lettre d'information");
$titres = [];
foreach($newsletters as $newsletter) {
$titres[] = $newsletter->getTitre();
}
$newsletter_info = $this->view->_('Vous êtes abonné');
$newsletter_info .= count($titres) > 1 ? $this->view->_(" aux lettres d'information: ") : $this->view->_(" à la lettre d'information: ");
return $newsletter_info .= implode(', ', $titres);
}
}
\ No newline at end of file
o<?php
<?php
/**
* Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
*
......
......@@ -25,7 +25,7 @@ abstract class AbstractAbonneControllerNewslettersTestCase extends AbstractContr
protected $_storm_default_to_volatile=true;
public function setUp() {
parent::setUp();
Class_UserGroup::setMemoryCleaner(function() {});
Class_AdminVar::newInstanceWithId('CHAMPS_FICHE_UTILISATEUR',
['valeur' => 'pseudo;nom;prenom;mail;password']);
......@@ -54,7 +54,7 @@ abstract class AbstractAbonneControllerNewslettersTestCase extends AbstractContr
abstract class AbonneControllerWithTwoNewslettersTestCase extends AbstractAbonneControllerNewslettersTestCase {
public function setUp() {
parent::setUp();
(new NewsletterFixtures())->createUsersAndNewsletter($this);
$this->concerts = $this->fixture('Class_Newsletter',
['id' => 12,
'titre' =>'Concerts',
......@@ -66,7 +66,7 @@ abstract class AbonneControllerWithTwoNewslettersTestCase extends AbstractAbonne
'contenu' => 'du patrimoine']);
$this->marcus->setNewsletters([$this->concerts])->save();
$this->marcus->subscribeNewsletter($this->concerts);
}
}
......@@ -256,31 +256,33 @@ class AbonneControllerNewslettersEditActionTest extends AbonneControllerWithTwoN
class AbonneControllerNewsletterPostNewsletterTest extends AbonneControllerWithTwoNewslettersTestCase {
class AbonneControllerNewslettersPostNewsletterTest 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]]);
$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 {
class AbonneControllerNewslettersPostNewsletterNoSubscriptionTest extends AbonneControllerWithTwoNewslettersTestCase {
public function setUp() {
parent::setUp();
$this->postDispatch('/opac/abonne/edit', ['nom' => 'marcus',
......@@ -307,19 +309,13 @@ class AbonneControllerNewslettersSaveActionTest extends AbonneControllerWithTwoN
public function setUp() {
parent::setUp();
$data = ['nom' => 'MILLER',
'prenom' => 'MARCUS',
'mail' => 'marcus@free.fr',
'pseudo' => 'M2',
'subscriptions' => [14],
'password' => 'amstramgram',
'confirm_password' => 'amstramgram'];
$this->getRequest()
->setMethod('POST')
->setPost($data);
$this->dispatch('/opac/abonne/edit', true);
$this->postDispatch('/opac/abonne/edit', ['nom' => 'MILLER',
'prenom' => 'MARCUS',
'mail' => 'marcus@free.fr',
'pseudo' => 'M2',
'subscriptions' => [14],
'password' => 'amstramgram',
'confirm_password' => 'amstramgram']);
}
/** @test */
......@@ -357,6 +353,7 @@ class AbonneControllerNewslettersSaveActionTest extends AbonneControllerWithTwoN
}
class AbonneControllerNewslettersSaveWithEmptyDataTest extends AbonneControllerWithTwoNewslettersTestCase {
public function setUp() {
parent::setUp();
......
......@@ -990,14 +990,4 @@ class UpgradeDB_306_Test extends UpgradeDBTestCase {
$this->assertTable('newsletter_dispatch_user');
}
/** @test */
public function tableNewsletterUsersShouldBeMigratedToGroup() {
foreach (Class_Newsletter::findAll() as $newsletter) {
$group = $newsletter->getDedicatedGroup();
$this->assertEquals(Class_UserGroupMembership::countBy(['user_group_id' => $group->getId()]),
Class_NewsletterSubscription::countAvailableUserForNewsletter($newsletter->getId()));
}
}
}
......@@ -30,6 +30,7 @@ class Class_Migration_NewsletterSubscriptionSimpleTest extends ModelTestCase {
'titre' => 'Nouveautés classique',
'contenu' => 'Notre sélection du mois',
'id_catalogue' => '',
'auto_subscribe' => 0,
'id_panier' => '',
]);
......
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