Commit 642ff3f3 authored by Patrick Barroca's avatar Patrick Barroca 🐧

dev #44823 : auto subscribe to newsletters based on groups, admin side

parent b5391c4a
......@@ -60,6 +60,34 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
public function postDispatch() {
$this->_saveSession();
parent::postDispatch();
}
protected function _getPost() {
$post = parent::_getPost();
unset($post['id_categories']);
$ids = explode('-', $this->_getParam('subscribe_group_ids', ''));
$mapper = function($id) {
return Class_UserGroup::find((int)$id);
};
$post['user_groups'] = array_filter(array_map($mapper, $ids));
unset($post['subscribe_group_ids']);
return $post;
}
protected function _getFormValues($model) {
$values = parent::_getFormValues($model);
$ids = array_map(function($group) { return $group->getId(); },
$model->getUserGroups());
$values['subscribe_group_ids'] = implode('-', $ids);
return $values;
}
......
<?php
error_reporting(E_ERROR | E_PARSE);
define("PATCH_LEVEL","303");
define("PATCH_LEVEL","304");
define("APPLI","cosmogramme");
define("COSMOPATH", "/var/www/html/vhosts/opac2/www/htdocs");
......
<?php
$adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
$adapter->query('CREATE TABLE IF NOT EXISTS `newsletter_group_autosubscribe` ('
. 'id int(11) unsigned not null auto_increment,'
. 'user_group_id int(11) not null,'
. 'newsletter_id int(11) not null,'
. 'PRIMARY KEY (`id`),'
. 'KEY `newsletter_id` (`newsletter_id`),'
. 'KEY `user_group_id` (`user_group_id`)'
.') engine=MyISAM default charset=utf8');
\ No newline at end of file
......@@ -59,9 +59,18 @@ class Class_Newsletter extends Storm_Model_Abstract {
const URL_PLACEHOLDER = '{{URL}}';
protected $_table_name = 'newsletters';
protected $_has_many = ['subscriptions' => ['model' => 'Class_NewsletterSubscription',
'role' => 'newsletter',
'dependents' => 'delete']];
protected $_has_many =
['subscriptions' => ['model' => 'Class_NewsletterSubscription',
'role' => 'newsletter',
'dependents' => 'delete'],
'newsletter_group_autosubscribes' => ['model' => 'Class_NewsletterGroupAutosubscribe',
'role' => 'newsletter',
'dependents' => 'delete'],
'user_groups' => ['through' => 'newsletter_group_autosubscribes']];
protected $_notices_finder;
protected $_recipent_size = 20;
protected $_default_attribute_values = ['titre' => '',
......
<?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 Class_NewsletterGroupAutosubscribe extends Storm_Model_Abstract {
protected $_table_name = 'newsletter_group_autosubscribe';
protected $_belongs_to = ['user_group' => ['model' => 'Class_UserGroup'],
'newsletter' => ['model' => 'Class_Newsletter']];
}
......@@ -22,19 +22,28 @@
class Class_UserGroup extends Storm_Model_Abstract {
use Trait_TreeViewableItem, Trait_HasManyDomaines;
protected $_table_name = 'user_groups';
protected $_has_many = ['user_group_memberships' => ['model' => 'Class_UserGroupMembership',
'role' => 'user_group',
'dependents' => 'delete'],
protected $_has_many =
['user_group_memberships' => ['model' => 'Class_UserGroupMembership',
'role' => 'user_group',
'dependents' => 'delete'],
'users' => ['through' => 'user_group_memberships',
'unique' => true],
'users' => ['through' => 'user_group_memberships',
'unique' => true],
'user_group_permissions' => ['model' => 'Class_UserGroup_Permission',
'role' => 'group',
'dependents' => 'delete'],
'user_group_permissions' => ['model' => 'Class_UserGroup_Permission',
'role' => 'group',
'dependents' => 'delete'],
'permissions' => ['through' => 'user_group_permissions',
'unique' => true]];
'permissions' => ['through' => 'user_group_permissions',
'unique' => true],
'newsletter_group_autosubscribes' => ['model' => 'Class_NewsletterGroupAutosubscribe',
'role' => 'user_group',
'dependents' => 'delete'],
'newsletter_autosubscribes' => ['through' => 'newsletter_group_autosubscribes',
'unique' => true]
];
protected $_belongs_to = ['categorie' => ['model' => 'Class_UserGroupCategorie',
'referenced_in' => 'id_cat'],
......
......@@ -40,7 +40,12 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
->addElement('checkbox', 'draft',
['label' => $this->_('Brouillon ?')])
->addElement('checkbox', 'auto_subscribe', ['label' => $this->_("Inscrire automatiquement les nouveaux lecteurs à la lettre d'information")])
->addElement('userGroup', 'subscribe_group_ids',
['label' => '',
'categories_selectable' => false,
'url' => $this->getView()->url(['module' => 'admin',
'controller' => 'usergroup',
'action' => 'list.json'])])
->addElement('ckeditor', 'contenu',
['required' => true,
......@@ -68,9 +73,10 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
'letter',
['legend' => $this->_('Lettre')])
->addDisplayGroup(['auto_subscribe'],
->addDisplayGroup(['auto_subscribe',
'subscribe_group_ids'],
'add_reader',
['legend' => $this->_('Inscription automatique')])
['legend' => $this->_('Inscription automatique des nouveaux utilisateurs')])
->addDisplayGroup(['contenu'],
'contenu_html',
......
......@@ -63,6 +63,15 @@ abstract class Admin_NewsletterControllerTestCase extends Admin_AbstractControll
->beStrict();
$this->fixture('Class_UserGroup',
['id' => 14,
'libelle' => 'My Group']);
$this->fixture('Class_UserGroup',
['id' => 15,
'libelle' => 'My Other Group']);
$this->fixture('Class_Newsletter',
['id' => 1,
'titre' => 'Nouveautés classique',
......@@ -263,14 +272,8 @@ class Admin_NewsletterControllerAddActionTest extends Admin_NewsletterController
/** @test **/
public function autoAddNewUsersCheckboxShouldBeDisplay() {
$this->assertXPath("//input[@type='checkbox'][@name='auto_subscribe']",$this->_response->getBody());
}
/** @test **/
public function autoAddNewGuestsCheckboxShouldBeDisplay() {
$this->assertXPath("//input[@type='checkbox'][@name='auto_subscribe_guest']");
public function autoAddNewUsersByGroupsShouldBePresent() {
$this->assertXPath("//input[@name='subscribe_group_ids']");
}
}
......@@ -327,8 +330,10 @@ class Admin_NewsletterControllerEditActionTest extends Admin_NewsletterControlle
$this->fixture('Class_Newsletter',
['id' => 53,
'titre' => 'Nouveautés',
'contenu'=>'Notre sélection du mois',
'expediteur' => 'laurent@free.fr']);
'contenu'=>'Notre sélection du mois',
'expediteur' => 'laurent@free.fr',
'user_groups' => [Class_UserGroup::find(14),
Class_UserGroup::find(15)]]);
Class_Newsletter::find(53)->setLastDistributionDateWithFormat();
......@@ -381,6 +386,12 @@ class Admin_NewsletterControllerEditActionTest extends Admin_NewsletterControlle
public function testSubmitButton() {
$this->assertXPath("//div[contains(@onclick,\"submit()\")]",$this->_response->getBody());
}
/** @test **/
public function autoAddNewUsersByGroupsShouldBePresentWithValue() {
$this->assertXPath("//input[@name='subscribe_group_ids'][@value='14-15']");
}
}
......@@ -396,7 +407,8 @@ class Admin_NewsletterControllerAddActionPostTest extends Admin_NewsletterContro
'contenu' => 'Plein les yeux',
'id_catalogue' => '',
'id_panier' => '',
'nb_notices' => 2 ];
'nb_notices' => 2,
'subscribe_group_ids' => '15'];
$this->postDispatch('/admin/newsletter/add', $data);
$this->_new = Class_Newsletter::findFirstBy(['titre' => 'Fêtes du lac']);
......@@ -413,6 +425,13 @@ class Admin_NewsletterControllerAddActionPostTest extends Admin_NewsletterContro
public function shouldRedirectToEdit() {
$this->assertRedirectTo('/admin/newsletter/edit/id/' . $this->_new->getId());
}
/** @test */
public function shouldAutosubscribeMyOtherGroup() {
$this->assertEquals('My Other Group',
$this->_new->getUserGroups()[0]->getLibelle());
}
}
......
......@@ -837,4 +837,22 @@ class UpgradeDB_303_Test extends UpgradeDBTestCase {
public function locationIdShouldBeIndexed() {
$this->assertIndex('bib_c_site', 'id_lieu', 'BTREE');
}
}
class UpgradeDB_304_Test extends UpgradeDBTestCase {
public function prepare() {
try {
$this->query('DROP TABLE `newsletter_group_autosubscribe`');
} catch(Exception $e) {
}
}
/** @test */
public function tableNewsletterGroupAutosubscribeShouldExists() {
$this->assertTable('newsletter_group_autosubscribe');
}
}
\ No newline at end of file
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