Commit 6c131e02 authored by Ghislain Loas's avatar Ghislain Loas

dev #69586 : refacto and add article categories field

parent ecdc71aa
Pipeline #6139 passed with stage
in 32 minutes and 36 seconds
'69586' =>
['Label' => $this->_('Inclure des articles dans les newsletters'),
'Desc' => '',
['Label' => $this->_('Inclure des articles dans les lettres d/'information'),
'Desc' => $this->_('Bokeh permet d\'envoyer des articles dans les lettres d\'information.'),
'Image' => '',
'Video' => '',
'Category' => '',
'Category' => $this->_('Rédaction'),
'Right' => function($feature_description, $user) {return true;},
'Wiki' => '',
'Wiki' => 'http://wiki.bokeh-library-portal.org/index.php?title=Lettre_d\'information',
'Test' => '',
'Date' => '2018-01-10'],
\ No newline at end of file
<?php
(new Class_Migration_CircularUserGroupCategories())->run();
\ No newline at end of file
(new Class_Migration_CircularUserGroupCategories())->run();
<?php
$adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
try {
$adapter->query('ALTER TABLE newsletters ADD articles_ids varchar(255) NULL default NULL');
} catch(Exception $e) {}
try {
$adapter->query('ALTER TABLE newsletters ADD articles_categories_ids varchar(255) NULL default NULL');
} catch(Exception $e) {}
<?php
/**
* Copyright (c) 2012-2019, 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_Article_MailRenderer {
use Trait_Translator;
public function renderTextInto($article, &$data) {
if (!$article)
return;
$data[] = '- ' . $article->getLabel();
$data[] = strip_tags($article->getFullContent());
$data[] = $this->_('Lien: ') . Class_Url::absolute(['controller' => 'cms',
'action' => 'viewarticle',
'id' => $article->getId()], null, true);
$data[] = "\n";
}
public function renderHtml($article) {
if (!$article)
return '';
return '<div style="padding:5px">' .
'<h1>' . $article->getLabel() . '</h1>' .
'<div>' . $article->getFullContent() . '</div>' .
'<div style="clear:both"></div>'.
'</div>';
}
}
......@@ -90,8 +90,8 @@ class Class_Newsletter extends Storm_Model_Abstract {
protected $_recipent_size = 20;
protected $_default_attribute_values = ['titre' => '',
'draft' => 0,
'id_categorie' => '',
'id_items' => ''];
'articles_ids' => '',
'articles_categories_ids' => ''];
public function describeAssociationsOn($associations) {
......@@ -223,7 +223,14 @@ class Class_Newsletter extends Storm_Model_Abstract {
public function getArticles() {
return Class_Article::findAllBy(['id_article' => $this->getIdItems()]);
$articles = [];
foreach (Class_ArticleCategorie::findAllBy(['id_cat' => $this->getArticlesCategoriesIds()]) as $categorie)
$articles = array_merge($articles,
$categorie->getArticles());
return array_merge($articles,
Class_Article::findAllBy(['id_article' => $this->getArticlesIds()]));
}
......@@ -261,8 +268,9 @@ class Class_Newsletter extends Storm_Model_Abstract {
foreach($records as $record)
$renderer->renderTextInto($record, $lines);
$renderer = new Class_Article_MailRenderer();
foreach($articles as $article)
$this->_addBodyTextArticleInto($article, $lines);
$renderer->renderTextInto($article, $lines);
$lines[] = $this->_getUnsubscribeText();
......@@ -270,16 +278,6 @@ class Class_Newsletter extends Storm_Model_Abstract {
}
protected function _addBodyTextArticleInto($article, &$data) {
if (!$article)
return;
$data[] = '- ' . $article->getTitre();
$data[] = strip_tags($article->getContenu());
$data[] = "\n";
}
/**
* @return string
*/
......@@ -302,8 +300,9 @@ class Class_Newsletter extends Storm_Model_Abstract {
foreach($records as $record)
$html .= $renderer->renderHtml($record);
$renderer = new Class_Article_MailRenderer();
foreach($articles as $article)
$html .= $this->_getBodyHTMLArticle($article);
$html .= $renderer->renderHtml($article);
$html .= $this->_getUnsubscribeHTML();
......@@ -311,15 +310,6 @@ class Class_Newsletter extends Storm_Model_Abstract {
}
protected function _getBodyHTMLArticle($article) {
return '<div style="padding:5px">' .
'<h1>' . $article->getLabel() . '</h1>' .
'<div>' . $article->getFullContent() . '</div>' .
'<div style="clear:both"></div>'.
'</div>';
}
public function unsubscribeUser($user) {
$this->unsubscribeByMail($user->getMail());
}
......
......@@ -80,26 +80,33 @@ class ZendAfi_Form_Admin_Newsletter extends ZendAfi_Form {
public function populate($datas) {
parent::populate($datas);
$this->addElement('treeSelect',
'articles_selector',
['IdItems' => isset($datas['id_items']) ? $datas['id_items'] : '',
'ItemRenderer' => function($id)
{
if ($model = Class_Article::find($id))
return $model->getTitre();
},
'IdCategories' => isset($datas['id_categorie']) ? $datas['id_categorie'] : '',
'CategoryRenderer' => function($id)
{
if ($model = Class_ArticleCategorie::find($id))
return implode(' > ', $model->getPathParts());
},
'UrlDataSource' => Class_Url::assemble(['module' => 'admin',
'controller' => 'bib',
'action' => 'articles'])]);
$this->addDisplayGroup(['articles_selector'], 'articles',
$this
->addElement('treeSelect',
'articles_selector',
['UrlDataSource' => Class_Url::assemble(['module' => 'admin',
'controller' => 'bib',
'action' => 'articles']),
'InputIdItemsName' => 'articles_ids',
'IdItems' => isset($datas['articles_ids']) ? $datas['articles_ids'] : '',
'ItemRenderer' => function($id)
{
if ($model = Class_Article::find($id))
return $model->getTitre();
},
'InputIdCategoriesName' => 'articles_categories_ids',
'IdCategories' => isset($datas['articles_categories_ids']) ? $datas['articles_categories_ids'] : '',
'CategoryRenderer' => function($id)
{
if ($model = Class_ArticleCategry::find($id))
return $model->getLibelle();
},
]);
$this->addDisplayGroup(['articles_selector'],
'articles',
['legend' => $this->_('Articles')]);
return $this;
}
......
......@@ -316,8 +316,7 @@ class Admin_NewsletterControllerEditActionTest extends Admin_NewsletterControlle
'titre' => 'Nouveautés',
'contenu'=>'Notre sélection du mois',
'expediteur' => 'laurent@free.fr',
'id_categorie' => '2',
'id_items' => '6-42',
'articles_ids' => '6-42',
'user_groups' => [Class_UserGroup::find(14),
Class_UserGroup::find(15)]]);
......@@ -371,8 +370,8 @@ class Admin_NewsletterControllerEditActionTest extends Admin_NewsletterControlle
/** @test */
public function articleSelectionFieldsShouldBePresent() {
$this->assertXPath('//form//input[@name="id_categorie"][@value="2"]');
$this->assertXPath('//form//input[@name="id_items"][@value="6-42"]');
$this->assertXPath('//form//input[@name="articles_ids"][@value="6-42"]');
$this->assertXPath('//form//input[@name="articles_categories_ids"][@value=""]');
}
}
......@@ -390,8 +389,8 @@ class Admin_NewsletterControllerAddActionPostTest extends Admin_NewsletterContro
'id_catalogue' => '',
'id_panier' => '',
'nb_notices' => 2,
'id_categorie' => '667-34-22',
'id_items' => '6-42'];
'articles_ids' => '6-42',
'articles_categories_ids' => '3-4'];
$this->postDispatch('/admin/newsletter/add', $data);
$this->_new = Class_Newsletter::findFirstBy(['titre' => 'Fêtes du lac']);
......@@ -424,14 +423,13 @@ class Admin_NewsletterControllerAddActionPostTest extends Admin_NewsletterContro
/** @test */
public function idCategorieShouldBe667_34_22() {
$this->assertEquals('667-34-22', $this->_new->getIdCategorie());
public function articlesIdsShouldBe6_42() {
$this->assertEquals('6-42', $this->_new->getArticlesIds());
}
/** @test */
public function idItemsShouldBe6_42() {
$this->assertEquals('6-42', $this->_new->getIdItems());
public function articlesCategoriesIdsShouldBe3And4() {
$this->assertEquals('3-4', $this->_new->getArticlesCategoriesIds());
}
}
......@@ -866,6 +864,27 @@ class Admin_NewsletterControllerPreviewActionTest extends Admin_NewsletterContro
'libelle' => 'Mon catalogue',
'type_doc' => 5]);
$septante_millions = $this->fixture('Class_Article',
['id' => 35,
'titre' => '70 millions …',
'contenu' => 'Ils sont 70 millions prêts à prendre sa place.']);
$le_roi_des_cons = $this->fixture('Class_Article',
['id' => 36,
'titre' => 'Le Roi des cons',
'contenu' => 'Il est beau, il est fier sur son throne.']);
$l_arnaque = $this->fixture('Class_Article',
['id' => 12,
'titre' => 'L\'arnaque',
'contenu' => 'La révolution a oublié de décapiter la misère et l\'exploitation.']);
$this->fixture('Class_ArticleCategorie',
['id' => 89,
'libelle' => 'La France',
'articles' => [$le_roi_des_cons,
$septante_millions]]);
$nouveautes = $this->fixture('Class_Newsletter',
['id' => 3,
'titre' => 'Nouveautés',
......@@ -873,6 +892,8 @@ class Admin_NewsletterControllerPreviewActionTest extends Admin_NewsletterContro
'id_catalogue' => 1,
'nb_notices' => 0,
'id_panier' => null,
'articles_categories_ids' => '89-99',
'articles_ids' => '12',
'last_distribution_date' => '2012-03-02']);
......@@ -883,18 +904,22 @@ class Admin_NewsletterControllerPreviewActionTest extends Admin_NewsletterContro
$this->dispatch('/admin/newsletter/preview/id/3', true);
}
public function testFrom() {
$this->assertQueryContentContains('p', 'laurent@afi-sa.net');
}
public function testSubject() {
$this->assertQueryContentContains('p', 'Nouveautés');
}
public function testBodyText() {
$this->assertQueryContentContains('p', 'Notre sélection du mois Hoho');
}
public function testBodyHtml() {
$this->assertXPath('//div//img[@src="zork.jpg"]', $this->_response->getBody());
}
......@@ -905,16 +930,29 @@ class Admin_NewsletterControllerPreviewActionTest extends Admin_NewsletterContro
$this->assertQueryContentContains('p', 'Martine à la plage', $this->_response->getBody());
}
/** @test */
public function noticeMartineALaPlageUrlShouldBeRechercheViewNotice42() {
$this->assertXPath('//a[@href="' . Class_Url::absolute('/recherche/viewnotice/id/42') . '"]');
}
/** @test */
public function templateShouldNotCreateNewsletterDispatch() {
$this->assertEmpty(Class_Newsletter_Dispatch::findAll());
}
/** @test */
public function leRoiDesConsShouldBeDisplay() {
$this->assertXpath('//div', 'Le Roi des cons', $this->_response->getBody());
}
/** @test */
public function lArnaqueShouldBeDisplay() {
$this->assertXpath('//div', 'L\'arnaque', $this->_response->getBody());
}
}
......
......@@ -2443,10 +2443,16 @@ class UpgradeDB_363_Test extends UpgradeDBTestCase {
class UpgradeDB_364_Test extends UpgradeDBTestCase {
public function prepare() {
$this->silentQuery('ALTER TABLE newsletters DROP column articles_ids');
$this->silentQuery('ALTER TABLE newsletters DROP column articles_categories_ids');
}
/** @test **/
public function idItemsShouldBePresent() {
public function articlesIdsShouldBePresent() {
$this->assertColumn('newsletters', 'articles_ids');
}
/** @test **/
public function articlesCategoriesShouldBePresent() {
$this->assertColumn('newsletters', 'articles_categories_ids');
}
}
\ 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