Commit e7d52f4d authored by Ghislain Loas's avatar Ghislain Loas

dev #46014 fix rt comments

parent 83e0662a
......@@ -66,25 +66,11 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
$model->getTitre());
$this->view->newsletter = $model;
$groups = $model->getRecipientsGroups();
usort(
$groups,
function($a, $b) {
if ($a->isDedicated())
return -1;
if ($b->isDedicated())
return 1;
return strcmp($a->getLibelle(), $b->getLibelle());
});
$this->view->groups = $groups;
$this->view->groups = $model->getSortedRecipientsByDedicatedAndLabel();
}
public function sendAction() {
$this->_helper->viewRenderer->setNoRender();
if ($newsletter = Class_Newsletter::find((int)$this->_getParam('id')))
$newsletter->send();
......@@ -93,10 +79,8 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
public function sendProgressAction() {
$this->_helper->viewRenderer->setNoRender();
$newsletter = Class_Newsletter::find((int)$this->_getParam('id'));
$this->getResponse()->setHeader('Content-Type', 'application/json; charset=utf-8');
$this->getResponse()->setBody($this->view->getSendProgressJsonFor($newsletter));
$this->_helper->json($this->view->getSendProgressJsonFor($newsletter));
}
......@@ -237,9 +221,7 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
$subscription = Class_NewsletterGroupSubscription::findFirstBy(['newsletter_id' => $model->getId(),
'user_group_id' => (int)$this->_getParam('id')]);
if ($subscription
&& ((!$group = $subscription->getUserGroup())
|| !$group->isDedicated()))
if ($subscription && !$subscription->hasDedicatedGroup())
$subscription->delete();
$this->_redirect($this->view->url(['module' => 'admin',
......
......@@ -252,21 +252,13 @@ class Admin_UsergroupController extends ZendAfi_Controller_Action {
public function listJsonAction() {
foreach(Class_UserGroupCategorie::getTopCategories() as $group) {
if (Class_UserGroup::TYPE_DYNAMIC == $this->_getParam('type'))
$group->beOnlyDynamic();
$json_groups[]= $group->toJSON(true);
}
$jsons = '[{'.
'"id":0,'.
'"label": "Groupes",'.
'"categories": ['.implode(",", $json_groups).'],'.
'"items": []}]';
$this->_helper->viewRenderer->setNoRender();
$this->getResponse()->setHeader('Content-Type', 'application/json; charset=utf-8');
$this->getResponse()->setBody($jsons);
$json_groups = [];
foreach(Class_UserGroupCategorie::getTopCategories() as $group_category)
$json_groups[] = json_decode($group_category->toJSON(true), true);
$this->_helper->json([['id' => 0,
'label' => 'Groupes',
'categories' => $json_groups,
'items' => []]]);
}
}
......@@ -354,6 +354,22 @@ class Class_Newsletter extends Storm_Model_Abstract {
public function getLastDispatchInProgress() {
return Class_Newsletter_Dispatch::lastDispatchInProgressFor($this);
}
public function getSortedRecipientsByDedicatedAndLabel() {
$groups = $this->getRecipientsGroups();
usort(
$groups,
function($a, $b) {
if ($a->isDedicated())
return -1;
if ($b->isDedicated())
return 1;
return strcmp($a->getLibelle(), $b->getLibelle());
});
return $groups;
}
}
......
......@@ -68,6 +68,8 @@ class Class_Newsletter_Dispatch extends Storm_Model_Abstract {
public function beforeSave() {
if ($this->isNew())
$this->setCreatedOn($this->getCurrentDateTime());
return $this;
}
......@@ -81,13 +83,15 @@ class Class_Newsletter_Dispatch extends Storm_Model_Abstract {
(new Class_Batch_SendNewsletters($this))
->sendAllBy($recipient_size);
return $this;
}
public function collectRecipients() {
if ($this->getCollected())
return;
$blacklist_mails = Class_Newsletter_Blacklist::getMailsForNewsletter($this->getNewsletter()->getId());
$closure = function($model) use ($blacklist_mails) {
if (!$model->hasMail())
......
......@@ -30,7 +30,7 @@ class Newsletter_DispatchUserLoader extends Storm_Model_Loader {
public function beAllSent($models) {
foreach($models as $model)
$model->setSent(1)->save();
$model->beSent();
}
}
......@@ -44,4 +44,10 @@ class Class_Newsletter_DispatchUser extends Storm_Model_Abstract {
'user' => ['model' => 'Class_Users']];
protected $_default_attribute_values = ['sent' => 0];
public function beSent() {
$this->setSent(1)->save();
return $this;
}
}
......@@ -40,4 +40,11 @@ class Class_NewsletterGroupSubscription extends Storm_Model_Abstract {
protected $_belongs_to = ['user_group' => ['model' => 'Class_UserGroup'],
'newsletter' => ['model' => 'Class_Newsletter']];
public function hasDedicatedGroup() {
return ($group = $this->getUserGroup())
&& $group->isDedicated();
}
}
......@@ -27,13 +27,13 @@ class UserGroupLoader extends Storm_Model_Loader {
$closure = function($model) use (&$ids) {
$ids[] = $model->getId();
};
Class_UserGroup::withUsersOfDo($group, $closure);
Class_UserGroup::usersPaginatedDo($group, $closure);
return $ids;
}
public function withUsersOfDo($group, $closure) {
public function usersPaginatedDo($group, $closure) {
$page = 1;
while ($users = $group->getUsersPage($page, 1000)) {
$page++;
......@@ -251,17 +251,15 @@ 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;
}
if ($this->isManual() && in_array($user->getId(), $this->getUsersIds()))
return false;
}
if ($user->getRoleLevel() != $this->getRoleLevel())
return false;
if ($library = $this->getLibrary())
return ($this->getLibrary() == $user->getLibrary());
return true;
return ($library = $this->getLibrary())
? ($this->getLibrary() == $user->getLibrary())
: true;
}
......@@ -591,7 +589,7 @@ class Class_UserGroup extends Storm_Model_Abstract {
public function withUsersDo($closure) {
$this->getLoader()->withUsersOfDo($this, $closure);
$this->getLoader()->usersPaginatedDo($this, $closure);
return $this;
}
}
\ No newline at end of file
......@@ -64,8 +64,6 @@ class Class_UserGroupCategorie extends Storm_Model_Abstract {
protected $_belongs_to = ['parent_categorie' => ['model' => 'Class_UserGroupCategorie',
'referenced_in' => 'parent_id']];
protected $_is_only_dynamic = false;
/** [[file:~/public_html/afi-opac3/library/Trait/TreeNode.php::trait%20Trait_TreeNode%20{][voir Trait_TreeNode]] */
public function getParent() {
......@@ -84,31 +82,16 @@ class Class_UserGroupCategorie extends Storm_Model_Abstract {
* @return array
*/
public function getItems() {
if ($this->_is_only_dynamic)
return Class_UserGroup::findAllBy(['group_type' => Class_UserGroup::TYPE_DYNAMIC,
'id_cat' => $this->getId()]);
return $this->getUserGroups();
}
public function getSousCategories() {
$categories = parent::_get('sous_categories');
if ($this->_is_only_dynamic)
foreach($categories as $category)
$category->beOnlyDynamic();
return $categories;
return parent::_get('sous_categories');
}
public function getIdCatMere() {
return $this->getParentId();
}
public function beOnlyDynamic() {
$this->_is_only_dynamic = true;
return $this;
}
}
\ No newline at end of file
......@@ -801,15 +801,13 @@ class Class_Users extends Storm_Model_Abstract {
}
public function getNewsletters() {
$newsletters = [];
foreach (Class_Newsletter::getPublishedNewsletters() as $newsletter) {
if ($newsletter->hasRecipient($this))
$newsletters[]=$newsletter;
}
return $newsletters;
return array_filter(Class_Newsletter::getPublishedNewsletters(),
function($newsletter) {
$newsletter->hasRecipient($this);
});
}
/**
* @return array
*/
......@@ -1579,10 +1577,7 @@ 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) {
foreach($this->getNewsletters() as $newsletter) {
if (in_array($newsletter,$newsletters)) {
$this->addNewsletter($newsletter);
continue;
......
......@@ -21,7 +21,6 @@
class ZendAfi_View_Helper_getSendProgressJsonFor extends ZendAfi_View_Helper_BaseHelper {
public function getSendProgressJsonFor($newsletter) {
if(!$newsletter)
return $this->toJson($this->_('Newsletter inconnue'));
......@@ -37,15 +36,15 @@ class ZendAfi_View_Helper_getSendProgressJsonFor extends ZendAfi_View_Helper_Bas
if (!$dispatch->getCollected())
return $this->toJson($this->_('en cours'));
return json_encode(['status' => $this->_('en cours'),
'done' => $dispatch->numberOfDoneUsers(),
'total' => $dispatch->numberOfDispatchUsers()]);
return ['status' => $this->_('en cours'),
'done' => $dispatch->numberOfDoneUsers(),
'total' => $dispatch->numberOfDispatchUsers()];
}
protected function toJson($data) {
return json_encode(['status' => $data]);
return ['status' => $data];
}
}
?>
\ No newline at end of file
......@@ -20,8 +20,7 @@
*/
require_once 'tests/fixtures/NewsletterFixtures.php';
abstract class Admin_NewsletterControllerTestCase
extends Admin_AbstractControllerTestCase {
abstract class Admin_NewsletterControllerTestCase extends Admin_AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
......@@ -859,57 +858,74 @@ class Admin_NewsletterControllerScriptTest extends Admin_NewsletterControllerTes
class Admin_NewsletterControllerSendProgressWithWrongNewsletterIdTest extends Admin_AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
abstract class Admin_NewsletterControllerSendProgressTestCase extends Admin_NewsletterControllerTestCase {
protected
$_storm_default_to_volatile = true,
$_json;
public function setUp() {
parent::setUp();
$this->dispatch('admin/newsletter/send-progress/id/1', true);
$this->_dispatch();
$this->_json = json_decode($this->_response->getBody());
}
/** @test */
public function progressShouldReturnJsonUnknownNewsletter() {
$this->assertEquals('{"status":"Newsletter inconnue"}', $this->_response->getBody());
protected function _dispatch() {
$this->dispatch('admin/newsletter/send-progress/id/1', true);
}
}
class Admin_NewsletterControllerSendProgressEndTest extends Admin_NewsletterControllerTestCase {
class Admin_NewsletterControllerSendProgressWithWrongNewsletterIdTest extends Admin_NewsletterControllerSendProgressTestCase {
public function setUp() {
parent::setUp();
$dispatch=Class_Newsletter_Dispatch::newFrom(Class_Newsletter::find(1))
->assertSave();
$this->dispatch('admin/newsletter/send-progress/id/1', true);
}
/** @test */
public function progressShouldReturnEnCours() {
$this->assertEquals('{"status":"en cours"}', $this->_response->getBody());
public function progressShouldReturnJsonUnknownNewsletter() {
$this->assertEquals('Newsletter inconnue', $this->_json->status);
}
protected function _dispatch() {
$this->dispatch('admin/newsletter/send-progress/id/10', true);
}
}
class Admin_NewsletterControllerSendProgressTest extends Admin_NewsletterControllerTestCase {
public function setUp() {
parent::setUp();
class Admin_NewsletterControllerSendProgressEndTest extends Admin_NewsletterControllerSendProgressTestCase {
/** @test */
public function progressShouldReturnEnCours() {
$this->assertEquals('en cours', $this->_json->status);
}
$dispatch=Class_Newsletter_Dispatch::newFrom(Class_Newsletter::find(1));
$dispatch->assertSave();
$dispatch->collectRecipients();
$this->dispatch('admin/newsletter/send-progress/id/1', true);
protected function _dispatch() {
Class_Newsletter_Dispatch::newFrom(Class_Newsletter::find(1))->assertSave();
parent::_dispatch();
}
}
class Admin_NewsletterControllerSendProgressTest extends Admin_NewsletterControllerSendProgressTestCase {
/** @test */
public function progressShouldReturnSending() {
$this->assertEquals('{"status":"en cours","done":0,"total":2}', $this->_response->getBody());
$this->assertEquals('{"status":"en cours","done":0,"total":3}', $this->_response->getBody());
}
protected function _dispatch() {
$dispatch = Class_Newsletter_Dispatch::newFrom(Class_Newsletter::find(1));
$dispatch->assertSave();
$dispatch->collectRecipients();
parent::_dispatch();
}
}
......
......@@ -205,7 +205,7 @@ class Admin_UserGroupControllerListJsonActionTest extends Admin_UserGroupControl
parent::setUp();
$this->dispatch('admin/usergroup/list.json', true);
$this->_json = json_decode($this->_response->getBody());
$this->_json = json_decode($this->_response->getBody(), false);
}
......
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