Skip to content
Snippets Groups Projects
Commit 17a4208e authored by Patrick Barroca's avatar Patrick Barroca
Browse files

rel #34944 : tag linking to record under test, stormisation, refactos

parent e39eedd9
Branches
Tags
3 merge requests!1659Master,!1587Master,!1560Hotline#34944 tags notices disfonctionnement de l affichage
......@@ -311,39 +311,34 @@ class AbonneController extends ZendAfi_Controller_Action {
public function tagnoticeAction() {
$this->view->titre = $this->_('Proposer des tags pour cette notice');
if ($this->_request->isPost()) {
$filter = new Zend_Filter_StripTags();
$abonneTag1 = trim($filter->filter($this->_request->getPost('abonneTag1')));
$abonneTag2 = trim($filter->filter($this->_request->getPost('abonneTag2')));
$abonneTag3 = trim($filter->filter($this->_request->getPost('abonneTag3')));
$id = trim($filter->filter($this->_request->getPost('id')));
if ($abonneTag1 == '' && $abonneTag2 == '' && $abonneTag3 == '')
{
$this->getResponse()->setHeader('Content-Type', 'text/html;charset=utf-8');
$this->getResponse()->setBody("<script>window.top.hidePopWin(false);</script>");
$viewRenderer = $this->getHelper('ViewRenderer');
$viewRenderer->setNoRender();
}
else
{
$tag= new Class_TagNotice();
$tag->creer_tag($abonneTag1,$id);
$tag->creer_tag($abonneTag2,$id);
$tag->creer_tag($abonneTag3,$id);
xdebug_break();
$this->_javascriptRedirectToReferrer();
return;
}
}
else
{
if (!$this->_request->isPost()) {
$this->view->titre = $this->_('Proposer des tags pour cette notice');
$this->view->id = $this->_request->getParam('id_notice', 0);
$viewRenderer = $this->getHelper('ViewRenderer');
$viewRenderer->setLayoutScript('subModal.phtml');
return;
}
$filter = new Zend_Filter_StripTags();
$abonneTag1 = trim($filter->filter($this->_request->getPost('abonneTag1')));
$abonneTag2 = trim($filter->filter($this->_request->getPost('abonneTag2')));
$abonneTag3 = trim($filter->filter($this->_request->getPost('abonneTag3')));
$id = trim($filter->filter($this->_request->getPost('id')));
if ($abonneTag1 == '' && $abonneTag2 == '' && $abonneTag3 == '') {
$this->getResponse()->setHeader('Content-Type', 'text/html;charset=utf-8');
$this->getResponse()->setBody("<script>window.top.hidePopWin(false);</script>");
$viewRenderer = $this->getHelper('ViewRenderer');
$viewRenderer->setNoRender();
return;
}
Class_TagNotice::multiForRecord([$abonneTag1, $abonneTag2, $abonneTag3], $id);
$this->_javascriptRedirectToReferrer();
}
......
<?php
/**
* Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
* Copyright (c) 2012, 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
......@@ -18,97 +18,143 @@
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//////////////////////////////////////////////////////////////////////////////////////////
// OPAC3 - Tags utilisateurs
//////////////////////////////////////////////////////////////////////////////////////////
class Class_TagNoticeLoader extends Storm_Model_Loader {
const SUGGEST_STARTS_WITH = '1';
const SUGGEST_CONTAINS = '2';
public function multiForRecord($labels, $record_id) {
foreach($labels as $label)
Class_TagNotice::newForRecord($label, $record_id);
}
public function newForRecord($label, $record_id) {
if ((2 > mb_strlen($label,'UTF-8')) || !$record = Class_Notice::find($record_id))
return;
if (!$tag = Class_TagNotice::findByLabel($label)) {
$tag = Class_TagNotice::newInstance(['libelle' => $label,
'code_alpha' => $this->_codeFromLabel($label)]);
$tag->save();
}
$facet = $tag->asFacet();
foreach(Class_Notice_Facette::parseFacettesFromNoticeField($record->getFacettes()) as $existing) {
if ($facet == $existing->getCle())
return;
}
$record->updateFacette($facet)->save();
$tag->linkTo($record)->save();
}
public function findByLabel($label) {
return Class_TagNotice::findFirstBy(['code_alpha' => $this->_codeFromLabel($label)]);
}
public function suggestFrom($term, $type, $limit) {
$condition = $this->_conditionOfType($this->_codeFromLabel($term), $type);
$req = "select id_tag, libelle from codif_tags where code_alpha "
. $condition . " order by code_alpha limit " . $limit;
return fetchAll($req, true);
}
protected function _codeFromLabel($label) {
return (new Class_Indexation())->alphaMaj($label);
}
protected function _conditionOfType($type, $term) {
if (static::SUGGEST_STARTS_WITH == $type)
return "like '" . $term . "%'";
if (static::SUGGEST_CONTAINS == $type)
return "like '%" . $term . "%'";
return " = '" . $term . "'";
}
}
class Class_TagNotice extends Storm_Model_Abstract {
use Trait_Facetable;
const CODE_FACETTE = 'Z';
const RECORDS_SEPARATOR = ';';
protected $_table_name = 'codif_tags';
protected $_table_primary = 'id_tag';
protected $_loader_class = 'Class_TagNoticeLoader';
protected $_default_attribute_values = ['notices' => '', 'a_moderer' => ''];
// ----------------------------------------------------------------
// Rend une liste pour un champ suggestion
// ----------------------------------------------------------------
public function getListeSuggestion($recherche,$mode,$limite_resultat)
{
// Transformer en code alpha
$ix=new Class_Indexation();
$recherche=$ix->alphaMaj($recherche);
// Lancer la recherche
if($mode=="1") $condition="like '".$recherche."%'";
if($mode=="2") $condition="like '%".$recherche."%'";
$req="select id_tag,libelle from codif_tags where code_alpha ".$condition." order by code_alpha limit ".$limite_resultat;
$resultat=fetchAll($req,true);
return $resultat;
public function getListeSuggestion($recherche, $mode, $limite_resultat) {
return $this->getLoader()->suggestFrom($recherche, $mode, $limite_resultat);
}
//------------------------------------------------------------------------------------------------------
// Creer un nouveau tag
//------------------------------------------------------------------------------------------------------
public function creer_tag($tag, $id_notice)
{
if(mb_strlen($tag,'UTF-8')< 2) return false;
// Controle table de codif
$id_tag=$this->getIdTag($tag);
$code_facette=" Z".$id_tag;
public function linkTo($record) {
$this
->addRecord($record)
->addModerateRecord($record);
// Controle si deja dans la notice
$req="select facettes from notices where id_notice=$id_notice";
$facettes=fetchOne($req);
if(strpos($facettes,$code_facette) !== false) return;
return $this;
}
// Ecrire dans notices
$facettes.=$code_facette;
sqlExecute("update notices set facettes='$facettes' where id_notice=$id_notice");
// ecrire dans codif_tags
$code_notice=";".$id_notice.";";
$enreg=fetchEnreg("select notices,a_moderer from codif_tags where id_tag=$id_tag");
$notices=$enreg["notices"];
$a_moderer=$enreg["a_moderer"];
if(strpos($notices,$code_notice) === false) if(!$notices) $notices.=$code_notice; else $notices.=$id_notice.";";
if(strpos($a_moderer,$code_notice) === false) if(!$a_moderer) $a_moderer.=$code_notice; else $a_moderer.=$id_notice.";";
sqlExecute("update codif_tags set notices='$notices', a_moderer='$a_moderer' where id_tag=$id_tag");
public function addRecord($record) {
$this->_addToRecordsField($record, 'notices');
return $this;
}
//------------------------------------------------------------------------------------------------------
// Get id tag dans table de codif
//------------------------------------------------------------------------------------------------------
private function getIdTag($tag)
{
$ix=new Class_Indexation();
$code=$ix->alphaMaj($tag);
$id=fetchOne("select id_tag from codif_tags where code_alpha='$code'");
// Si pas trouv on le cree
if(!$id)
{
$tag=addslashes($tag);
$req="insert into codif_tags(libelle,code_alpha) Values('$tag','$code')";
sqlExecute($req);
$id=fetchOne("select id_tag from codif_tags where code_alpha='$code'");
}
return $id;
public function addModerateRecord($record) {
$this->_addToRecordsField($record, 'a_moderer');
return $this;
}
//------------------------------------------------------------------------------------------------------
// Valider tag (moderation)
//------------------------------------------------------------------------------------------------------
public function validerTagNotice($id_tag,$id_notice)
{
protected function _addToRecordsField($record, $field) {
$records = $this->getRecordsFromField($field);
if (in_array($record->getId(), $records))
return;
$records[] = $record->getId();
$this->setRecordsIntoField($records, $field);
}
public function getRecordsFromField($field) {
return array_filter(explode(static::RECORDS_SEPARATOR,
$this->callGetterByAttributeName($field)));
}
public function setRecordsIntoField($records, $field) {
$this->callSetterByAttributeName($field,
static::RECORDS_SEPARATOR
. implode(static::RECORDS_SEPARATOR, $records)
. static::RECORDS_SEPARATOR);
return $this;
}
public function validerTagNotice($id_tag,$id_notice) {
$a_moderer=fetchOne("select a_moderer from codif_tags where id_tag=$id_tag");
$a_moderer=str_replace(";".$id_notice.";",";",$a_moderer);
if(trim($a_moderer)==";")$a_moderer="";
sqlExecute("update codif_tags set a_moderer='$a_moderer' where id_tag=$id_tag");
}
//------------------------------------------------------------------------------------------------------
// Supprimer tag (moderation)
//------------------------------------------------------------------------------------------------------
public function supprimerTagNotice($id_tag,$id_notice)
{
public function supprimerTagNotice($id_tag,$id_notice) {
// Supprimer dans codif_tags
$enreg=fetchEnreg("select * from codif_tags where id_tag=$id_tag");
$a_moderer=$enreg["a_moderer"];
......
......@@ -19,7 +19,13 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class AbonneControllerTagNoticeAddTagPopupTest extends AbstractControllerTestCase {
abstract class AbonneControllerTagNoticeTestCase extends AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
}
class AbonneControllerTagNoticeAddTagPopupTest extends AbonneControllerTagNoticeTestCase {
public function setUp() {
parent::setUp();
......@@ -49,7 +55,7 @@ class AbonneControllerTagNoticeAddTagPopupTest extends AbstractControllerTestCas
class AbonneControllerTagNoticeAddTest extends AbstractControllerTestCase {
class AbonneControllerTagNoticeAddTest extends AbonneControllerTagNoticeTestCase {
public function setUp() {
parent::setUp();
$this->fixture('Class_Notice',
......@@ -76,7 +82,7 @@ class AbonneControllerTagNoticeAddTest extends AbstractControllerTestCase {
class AbonneControllerTagNoticePopupAddTest extends AbstractControllerTestCase {
class AbonneControllerTagNoticePopupAddTest extends AbonneControllerTagNoticeTestCase {
protected $_json, $_xpath;
public function setUp() {
......@@ -105,4 +111,58 @@ class AbonneControllerTagNoticePopupAddTest extends AbstractControllerTestCase {
}
}
?>
class AbonneControllerTagNoticePopupAddValidPostTest extends AbonneControllerTagNoticeTestCase {
protected $cool_tag, $better_tag;
public function setUp() {
parent::setUp();
$this->fixture('Class_Notice', ['id' => 23]);
$this->postDispatch('/abonne/tagnotice/render/popup/id_notice/23',
['abonneTag1' => 'My cool tag',
'abonneTag2' => 'My better tag',
'abonneTag3' => '',
'id' => 23]);
$this->cool_tag = Class_TagNotice::FindFirstBy(['libelle' => 'My cool tag']);
$this->better_tag = Class_TagNotice::FindFirstBy(['libelle' => 'My better tag']);
}
public function provider() {
return [$this->cool_tag, $this->better_tag];
}
/** @test */
public function shouldHaveCreated2Tags() {
$this->assertEquals(2, Class_TagNotice::count());
}
/** @test */
public function tagsShouldBeCreated() {
$this->_withTagsDo(function($tag) { $this->assertNotNull($tag); });
}
/** @test */
public function tagsShouldHaveLinkToRecord() {
$this->_withTagsDo(function($tag) { $this->assertContains(';23;', $tag->getNotices()); });
}
/** @test */
public function tagsShouldHaveLinkToModerateRecord() {
$this->_withTagsDo(function($tag) { $this->assertContains(';23;', $tag->getAModerer()); });
}
protected function _withTagsDo($closure) {
foreach($this->provider() as $tag)
$closure($tag);
}
}
\ No newline at end of file
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