Commit 09105e51 authored by Patrick Barroca's avatar Patrick Barroca 🐧

rel #46014 : group subscription in dedicated screen, auto dedicated manual group for newsletter

parent e87d843e
......@@ -22,9 +22,6 @@
class Admin_NewsletterController extends ZendAfi_Controller_Action {
const SESSION_NAMESPACE = 'Admin_NewsletterController';
protected $_session_namespace = [];
public function getRessourceDefinitions() {
return ['model' => ['class' => 'Class_Newsletter',
'name' => 'newsletter',
......@@ -46,56 +43,6 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
}
public function preDispatch() {
parent::preDispatch();
$this->_session_namespace = Zend_Registry::get('session')->{self::SESSION_NAMESPACE};
if (!is_array($this->_session_namespace))
$this->_session_namespace = [];
if (!isset($this->_session_namespace['search']))
$this->_session_namespace['search'] = '';
}
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;
}
protected function _saveSession() {
Zend_Registry::get('session')->{self::SESSION_NAMESPACE} = $this->_session_namespace;
}
public function duplicateAction() {
$this->_redirectToIndex();
......@@ -110,45 +57,29 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
public function editSubscribersAction() {
if (!$newsletter = Class_Newsletter::find($this->_getParam('id'))) {
if (!$model = Class_Newsletter::find($this->_getParam('id'))) {
$this->_redirectToIndex();
return;
}
$redirect_url = 'admin/newsletter/edit-subscribers/id/' . $newsletter->getId()
. '/page/' . $this->_getParam('page', 1);
$this->view->titre = $this->_('Destinataires de la lettre : %s',
$model->getTitre());
if ($delete = $this->_getParam('delete')){
if ($subscription = Class_NewsletterSubscription::find((int)$delete))
$subscription->delete();
$this->_redirect($redirect_url);
return;
}
$this->view->newsletter = $model;
if ($this->_request->isPost()
&& null !== ($search = $this->_request->getPost('search'))) {
$this->_session_namespace['search'] = $search;
$this->_saveSession();
$this->_redirect($redirect_url);
return;
}
if ($this->_request->isPost()
&& ($ids_users_to_add = $this->_request->getPost('users'))) {
foreach ($ids_users_to_add as $id){
$newsletter->addSubscription(Class_NewsletterSubscription::newWith($newsletter, Class_Users::find((int) $id)));
}
$newsletter->save();
$this->_redirect($redirect_url);
return;
}
$groups = $model->getRecipientsGroups();
usort(
$groups,
function($a, $b) {
if ($a->isDedicated())
return -1;
if ($b->isDedicated())
return 1;
$this->view->subview = $this->view->partial('newsletter/edit-subscribers.phtml',
['newsletter' => $newsletter,
'search' => $this->_session_namespace['search'],
'page' => $this->_getParam('page')]);
$this->_forward('index');
return;
return strcmp($a->getLibelle(), $b->getLibelle());
});
$this->view->groups = $groups;
}
......@@ -244,4 +175,77 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
$this->_forward('index');
}
public function addGroupAction() {
if (!$model = Class_Newsletter::find((int)$this->_getParam('id'))) {
$this->_redirectToIndex();
return;
}
$this->view->titre = $this->_('Groupes destinataires');
$ids = array_map(function($group) { return $group->getId(); },
$model->getUserGroups());
$value = implode('-', $ids);
$form = ZendAfi_Form::newWithOptions(['action' => $this->view->url(),
'method' => Zend_Form::METHOD_POST])
->addElement('userGroup', 'subscribe_group_ids',
['label' => '',
'categories_selectable' => false,
'url' => $this->view->url(['module' => 'admin',
'controller' => 'usergroup',
'action' => 'list.json']),
'value' => $value])
->addDisplayGroup(['subscribe_group_ids'],
'groups',
['legend' => $this->_('Groupes destinataires')]);
if ($this->_request->isPost()
&& $form->isValid($this->_request->getPost())) {
$ids = explode('-', $this->_getParam('subscribe_group_ids', ''));
$mapper = function($id) {
return Class_UserGroup::find((int)$id);
};
$model
->setUserGroups(array_filter(array_map($mapper, $ids)))
->save();
$this->_redirectClose($this->view->url(['module' => 'admin',
'controller' => 'newsletter',
'action' => 'edit-subscribers',
'id' => $model->getId()], null, true),
['prependBase' => false]);
return;
}
$this->view->form = $form;
}
public function removeGroupAction() {
if (!$model = Class_Newsletter::find($this->_getParam('newsletter_id'))) {
$this->_redirectToIndex();
return;
}
$subscription = Class_NewsletterGroupSubscription::findFirstBy(['newsletter_id' => $model->getId(),
'user_group_id' => (int)$this->_getParam('id')]);
if ($subscription
&& ((!$group = $subscription->getUserGroup())
|| !$group->isDedicated()))
$subscription->delete();
$this->_redirect($this->view->url(['module' => 'admin',
'controller' => 'newsletter',
'action' => 'edit-subscribers',
'id' => $model->getId()], null, true),
['prependBase' => false]);
}
}
\ No newline at end of file
<h1><?php echo $this->_('Abonnés de la lettre: %s',
$this->newsletter->getTitre()); ?></h1>
<?php
$skin = Class_Admin_Skin::current();
<?php
echo $this->newsletterSubscribers($this->newsletter, $this->search, $this->page);
?>
echo $this->tag('a',
$skin->renderActionIconOn('add_user', $this)
. ' ' . $this->_('Gérer les groupes'),
['href' => $this->url(['module' => 'admin',
'controller' => 'newsletter',
'action' => 'add-group',
'id' => $this->newsletter->getId()],
null, true),
'data-popup' => 'true']);
$build_url = function($action, $controller) {
return $this->url(['module' => 'admin',
'controller'=> $controller,
'action' => $action,
'newsletter_id' => $this->newsletter->getId()], null, true)
. '/id/%s';
};
$actions = [['url' => $build_url('editmembers', 'usergroup'),
'icon' => 'users',
'label' => $this->_('Membres'),
'caption' => 'formatedCount'],
['url' => $build_url('remove-group', 'newsletter'),
'icon' => 'cancel',
'label' => $this->_('Désinscrire ce groupe'),
'anchorOptions' => ['onclick' => "return confirm('Etes-vous sûr de vouloir Désinscrire ce groupe ?')"],
'condition' => function($model) { return !$model->isDedicated(); }]];
$actions = function($group) use($actions) {
return $this->modelActions($group, $actions);
};
echo $this->tagModelTable($this->groups,
[$this->_('Nom')],
['libelle'],
[$actions],
'newsletter_user_groups');
......@@ -4,6 +4,5 @@ echo $this->userGroupMemberShip($this->group_id,$this->search, $this->page);
echo $this->bouton('id=close',
'picto=back',
'texte='.$this->_('Retour'),
'url='.$this->url(array('module' => 'admin',
'controller' => 'usergroup'), null, true));
'url='. $this->back_url);
?>
......@@ -8,4 +8,15 @@ $adapter->query('CREATE TABLE IF NOT EXISTS `newsletter_group_subscription` ('
. '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
.') engine=MyISAM default charset=utf8');
try {
$adapter->query('ALTER TABLE user_groups '
. 'ADD COLUMN model_class varchar(255) null default \'\', '
. 'ADD COLUMN model_id int(11) unsigned null default null, '
. 'ADD KEY `model_class` (`model_class`), '
. 'ADD KEY `model_id` (`model_id`)');
} catch(Exception $e) {
}
\ No newline at end of file
......@@ -76,6 +76,12 @@ class Class_Newsletter extends Storm_Model_Abstract {
protected $_default_attribute_values = ['titre' => '',
'draft' => 0];
public function describeAssociationsOn($associations) {
$associations
->add(new Class_Newsletter_DedicatedGroupAssociation('dedicated_group'));
}
public function getSubscriptionsPage($page=0, $items_by_page=20) {
return Class_NewsletterSubscription::findAllBy(['newsletter_id' => $this->getId(),
'limitPage' => [$page, $items_by_page]]);
......@@ -142,7 +148,7 @@ class Class_Newsletter extends Storm_Model_Abstract {
public function getNumberOfUsers() {
$ids = [];
foreach($this->getUserGroups() as $group)
foreach($this->getRecipientsGroups() as $group)
foreach($group->getUsersIds() as $id)
$ids[] = $id;
......@@ -150,6 +156,15 @@ class Class_Newsletter extends Storm_Model_Abstract {
}
public function getRecipientsGroups() {
$groups = [$this->getDedicatedGroup()];
foreach($this->getUserGroups() as $group)
$groups[] = $group;
return $groups;
}
public function generateMails($recipient_size) {
(new Class_Batch_SendNewsletters($this))
->sendAllBy($recipient_size);
......@@ -348,3 +363,41 @@ class Class_Newsletter extends Storm_Model_Abstract {
return $this->getTitre();
}
}
class Class_Newsletter_DedicatedGroupAssociation extends Storm_Model_Association_Abstract{
use Trait_Translator;
public function canHandle($method) {
return in_array($method, ['getDedicatedGroup']);
}
public function perform($model, $method, $args) {
$params = ['model_class' => get_class($model),
'model_id' => $model->getId()];
return ($group = Class_UserGroup::findFirstBy($params))
? $group
: Class_UserGroup::newInstance(array_merge($params, ['id_cat' => 0]));
}
public function save($model) {
$model
->getDedicatedGroup()
->setLibelle($this->_('Groupe manuel pour la lettre "%s"', $model->getTitre()))
->save();
}
public function saveBefore($model) {
}
public function delete($model) {
$model->getDedicatedGroup()->delete();
}
}
\ No newline at end of file
......@@ -150,7 +150,9 @@ class Class_UserGroup extends Storm_Model_Abstract {
protected $_default_attribute_values = ['rights_token' => 0,
'group_type' => 0,
'id_bib' => 0];
'id_bib' => 0,
'model_class' => null,
'model_id' => null];
public static function getRightDefinitionList() {
......@@ -531,6 +533,9 @@ class Class_UserGroup extends Storm_Model_Abstract {
if (!$this->hasRight(static::RIGHT_USER_ACCES_ARTICLES))
Class_Permission::denyAllCmsTo($this);
}
}
?>
\ No newline at end of file
public function isDedicated() {
return $this->hasModelClass() && $this->hasModelId();
}
}
\ No newline at end of file
......@@ -64,15 +64,16 @@ class ZendAfi_Controller_Action extends Zend_Controller_Action {
protected function _javascriptRedirectToReferrer() {
$result = ['result' => 'CONTENT',
'title' => $this->view->_('Sauvegarde en cours'),
'content' =>
'<script>window.location="'
.$this->_request->getServer('HTTP_REFERER').'"</script><p>'.$this->view->_('Veuillez patienter...')
.'</p><img style="display: block;margin:10px auto;" src="'.URL_ADMIN_IMG.'patience.gif">'];
$this->getHelper('ViewRenderer')->setNoRender();
$this->getResponse()->setHeader('Content-Type', 'application/json');
$this->getResponse()->setBody(json_encode(
['result' => 'CONTENT',
'title' => $this->view->_('Sauvegarde en cours'),
'content' =>
'<script>window.location="'
.$this->_request->getServer('HTTP_REFERER').'"</script><p>'.$this->view->_('Veuillez patienter...')
.'</p><img style="display: block;margin:10px auto;" src="'.URL_ADMIN_IMG.'patience.gif">']));
$this->getResponse()->setBody(json_encode($result));
}
......@@ -368,12 +369,19 @@ class ZendAfi_Controller_Action extends Zend_Controller_Action {
public function renderPopupResult($title, $content, $attribs = []) {
$result = array_merge($attribs,
['result' => 'CONTENT',
'title' => $title,
'content' => $content]);
if ('admin' == $this->_request->getModuleName())
$result['position'] = ['my' => 'top',
'at' => 'bottom+15px',
'of' => '.main .barre_nav'];
$this->getHelper('ViewRenderer')->setNoRender();
$this->getResponse()->setHeader('Content-Type', 'application/json');
$this->getResponse()->setBody(json_encode(array_merge($attribs,
['result' => 'CONTENT',
'title' => $title,
'content' => $content])));
$this->getResponse()->setBody(json_encode($result));
}
......
......@@ -120,10 +120,20 @@ class ZendAfi_Controller_Action_Helper_ViewRenderer extends Zend_Controller_Acti
$this->getResponse()->appendBody($layoutContent, $this->getResponseSegment());
$this->setNoRender();
if ($this->_render_popup)
$this->getResponse()->setBody(json_encode(['result' => 'CONTENT',
'title' => $this->view->getMainTitle(),
'content' => $this->getResponse()->getBody()]));
if ($this->_render_popup) {
$result = ['result' => 'CONTENT',
'title' => $this->view->getMainTitle(),
'content' => $this->getResponse()->getBody()];
if ('admin' == $this->getRequest()->getModuleName()) {
$result['position'] = ['my' => 'top',
'at' => 'bottom+15px',
'of' => '.main .barre_nav'];
$result['show_modal'] = 'true';
}
$this->getResponse()->setBody(json_encode($result));
}
}
}
......
......@@ -40,13 +40,6 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
->addElement('checkbox', 'draft',
['label' => $this->_('Brouillon ?')])
->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,
'allowEmpty' => false])
......@@ -73,10 +66,6 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
'letter',
['legend' => $this->_('Lettre')])
->addDisplayGroup(['subscribe_group_ids'],
'groups_subscriptions',
['legend' => $this->_('Destinataires')])
->addDisplayGroup(['contenu'],
'contenu_html',
['legend' => $this->_('Contenu HTML')])
......
......@@ -53,15 +53,21 @@ class ZendAfi_Form_Admin_UserGroup extends ZendAfi_Form {
['legend' => $this->_('Filtre')]);
$this->displayGroupFiltreVisibleOnlyOnDynamicGroup();
$this->displayRightsGroupVisibleOnlyOnDynamicAndManuelGroup();
$rights = Class_UserGroup::getActivatedRightDefinitionList();
if (($user = Class_Users::getIdentity())
&& $user->isAdmin()) {
$this->displayRightsGroupVisibleOnlyOnDynamicAndManuelGroup();
asort($rights);
$rights = Class_UserGroup::getActivatedRightDefinitionList();
asort($rights);
$this
->addElement('multiCheckbox', 'rights', ['label' => '', 'multiOptions' => $rights])
->addDisplayGroup(['rights'], 'rights_group', ['legend' => $this->_('Droits')]);
$this
->addElement('multiCheckbox', 'rights',
['label' => '', 'multiOptions' => $rights])
->addDisplayGroup(['rights'],
'rights_group',
['legend' => $this->_('Droits')]);
}
if (Class_AdminVar::isMultimediaEnabled()) {
$this->addRequiredTextNamed('max_day')
......
......@@ -20,7 +20,7 @@
*/
class ZendAfi_View_Helper_ModelActions extends Zend_View_Helper_Abstract {
class ZendAfi_View_Helper_ModelActions extends ZendAfi_View_Helper_BaseHelper {
public function modelActions($model, $actions, $inline=false) {
if (!$model || !$actions)
return '';
......
......@@ -4,7 +4,7 @@
"articles":"icons/menu/articles_24.png",
"domains":"icons/menu/domaines_24.png",
"rss":"icons/menu/rss_24.png",
"agendas":"icons/menu/agenda_24.png",
"agendas":"icons/menu/agenda_24.png",
"websites":"icons/menu/sitotheque_24.png",
"moderation":"icons/menu/moderation_24.png",
"subscription_requests": "icons/menu/demande_inscri_24.png",
......@@ -77,6 +77,7 @@
"add_category": "icons/actions/add_cat_24.png",
"add_profil": "icons/actions/page_add_16.png",
"add_box": "icons/actions/add_box_16.png",
"add_user": "icons/menu/demande_inscri_24.png",
"edit": "icons/actions/edit_16.png",
"delete": "icons/actions/delete_16.png",
......@@ -129,7 +130,8 @@
"restricted_profil": "icons/actions/ecran_rouge_16.png",
"profil": "icons/actions/ecran_16.png",
"card": "icons/actions/export_16.png",
"sort": "icons/actions/sort_16.png"
"sort": "icons/actions/sort_16.png",
"cancel": "icons/actions/cancel_24.png"
},
"buttons":
......
......@@ -127,8 +127,10 @@ abstract class UpgradeDBTestCase extends PHPUnit_Framework_TestCase {
protected function assertIndex($table, $name, $type, $message='') {
$keys = [];
try {
foreach($this->query(sprintf('show keys in `%s`', $table))->fetchAll() as $row) {
$keys = $this->query(sprintf('show keys in `%s`', $table))->fetchAll();
foreach($keys as $row) {
if ($name == $row['Key_name'] && $type == $row['Index_type'])
return true;
}
......@@ -137,7 +139,8 @@ abstract class UpgradeDBTestCase extends PHPUnit_Framework_TestCase {
$message = $message
? $message
: sprintf('Failed asserting that "%s" table CONTAINS an index "%s" of type "%s".',
$table, $name, $type);
$table, $name, $type)
. "\n" . json_encode($keys, JSON_PRETTY_PRINT);
$this->fail($message);
}
......@@ -153,7 +156,7 @@ abstract class UpgradeDBTestCase extends PHPUnit_Framework_TestCase {
}
protected function assertField($table, $column, $expected, $field, $message = '') {
protected function assertField($table, $column, $expected, $field, $message = '') {
try {
foreach($this->query(sprintf('show fields from `%s` where field=\'%s\'', $table, $column))->fetchAll() as $row) {
if ($expected == $row[$field])
......@@ -170,6 +173,26 @@ abstract class UpgradeDBTestCase extends PHPUnit_Framework_TestCase {
$this->fail($message);
}
protected function dropTable($name) {
try {
$this->query('DROP TABLE `' . $name . '`');
} catch(Exception $e) {}
return $this;
}
protected function dropIndexedFieldFrom($table, $field) {
try {
$this->query('ALTER TABLE ' . $table . ' '
. 'DROP KEY ' . $field . ', '
. 'DROP COLUMN ' . $field);
} catch(Exception $e) {}
return $this;
}
}
......@@ -843,11 +866,10 @@ class UpgradeDB_303_Test extends UpgradeDBTestCase {
class UpgradeDB_304_Test extends UpgradeDBTestCase {
public function prepare() {
try {
$this->query('DROP TABLE `newsletter_group_subscription`');
} catch(Exception $e) {
}
$this
->dropTable('newsletter_group_subscription')
->dropIndexedFieldFrom('user_groups', 'model_class')
->dropIndexedFieldFrom('user_groups', 'model_id');
}
......@@ -855,4 +877,28 @@ class UpgradeDB_304_Test extends UpgradeDBTestCase {
public function tableNewsletterGroupAutosubscribeShouldExists() {
$this->assertTable('newsletter_group_subscription');
}
/** @test */
public function userGroupModelClassShouldBePresent() {
$this->assertFieldType('user_groups', 'model_class', 'varchar(255)');
}
/** @test */
public function userGroupModelClassShouldBeIndexed() {
$this->assertIndex('user_groups', 'model_class', 'BTREE');
}
/** @test */
public function userGroupModelIdShouldBePresent() {
$this->assertFieldType('user_groups', 'model_id', 'int(11) unsigned');
}
/** @test */
public function userGroupModelIdShouldBeIndexed() {
$this->assertIndex('user_groups', 'model_id', 'BTREE');
}
}
\ 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