Skip to content
Snippets Groups Projects
Commit 934ac006 authored by llaffont's avatar llaffont
Browse files

Consolidation paniers: filtre sur type doc ssi doublons sans type doc

parent c9668e15
Branches
Tags 7.5.19
No related merge requests found
......@@ -47,11 +47,20 @@ class Class_Notice_ClefAlpha {
public function getTome() {
return $this->_clef[self::POS_TOME];
return isset($this->_clef[self::POS_TOME]) ? $this->_clef[self::POS_TOME] : '';
}
public function getUniqueNoticeWithSameTitleTomeTypeDoc() {
$notices = Class_Notice::findAllBy(['where' => 'clef_alpha like "'.$this->getClefTitre().self::SEPARATOR.'%"',
'tome_alpha' => $this->getTome()]);
if (!$notices)
return null;
if (count($notices) === 1)
return $notices[0];
$notices = Class_Notice::findAllBy(['where' => 'clef_alpha like "'.$this->getClefTitre().self::SEPARATOR.'%"',
'tome_alpha' => $this->getTome(),
'type_doc' => $this->getTypeDoc()]);
......
......@@ -74,164 +74,166 @@ class Class_PanierNotice extends Storm_Model_Abstract {
$_default_attribute_values = ['notices' => ''];
public static function getLoader() {
return self::getLoaderFor(__CLASS__);
}
public static function getLoader() {
return self::getLoaderFor(__CLASS__);
}
public static function fixLostClesNoticesForAll() {
$paniers = Class_PanierNotice::findAll();
foreach($paniers as $panier) {
if ($panier->numberOfLostNotices() == 0)
continue;
$panier->tryToFindLostClesNotices()->save();
}
}
public static function fixLostClesNoticesForAll() {
$paniers = Class_PanierNotice::findAll();
foreach($paniers as $panier) {
if ($panier->numberOfLostNotices() == 0)
continue;
$panier->tryToFindLostClesNotices()->save();
}
}
public function getClesNotices() {
$cles = array();
public function getClesNotices() {
$cles = array();
$notices_field = $this->_get('notices');
if (empty($notices_field)) return $cles;
$notices_field = $this->_get('notices');
if (empty($notices_field)) return $cles;
$exploded_cles = explode(self::NOTICES_SEPARATOR, $notices_field);
foreach($exploded_cles as $cle) {
if (empty($cle) || !trim($cle)) continue;
$exploded_cles = explode(self::NOTICES_SEPARATOR, $notices_field);
foreach($exploded_cles as $cle) {
if (empty($cle) || !trim($cle)) continue;
$cles []= $cle;
}
$cles []= $cle;
}
return array_filter(array_values(array_unique($cles)));
}
return array_filter(array_values(array_unique($cles)));
}
/*
* @param $cles_notices array
*/
public function setClesNotices($cles_notices) {
return $this->_set('notices', implode(self::NOTICES_SEPARATOR, $cles_notices));
}
/*
* @param $cles_notices array
*/
public function setClesNotices($cles_notices) {
return $this->_set('notices', implode(self::NOTICES_SEPARATOR, $cles_notices));
}
/**
* getClesNotices -> retournes les clés notices enregistrer dans la base dans le champs notices de la table panier
* return les clés qui existe dans la base
*
*/
public function getNoticesAsArray() {
if (!$cles = $this->getClesNotices())
return [];
/**
* getClesNotices -> retournes les clés notices enregistrer dans la base dans le champs notices de la table panier
* return les clés qui existe dans la base
*
*/
public function getNoticesAsArray() {
if (!$cles = $this->getClesNotices())
return [];
return Class_Notice::getLoader()->findAllBy(['clef_alpha' => $cles,
'order' => 'FIELD(clef_alpha, "'.implode('","', $cles).'")']);
}
return Class_Notice::getLoader()->findAllBy(['clef_alpha' => $cles,
'order' => 'FIELD(clef_alpha, "'.implode('","', $cles).'")']);
}
public function getNoticesOnlyVignettes($only_vignettes) {
$notices = $this->getNoticesAsArray();
if (!$only_vignettes) return $notices;
public function getNoticesOnlyVignettes($only_vignettes) {
$notices = $this->getNoticesAsArray();
if (!$only_vignettes) return $notices;
return array_filter_by_method($notices, 'hasVignette', array());
}
return array_filter_by_method($notices, 'hasVignette', array());
}
public function numberOfNotices() {
return count($this->getClesNotices());
}
public function numberOfNotices() {
return count($this->getClesNotices());
}
public function numberOfLostNotices() {
return $this->numberOfNotices() - count($this->getNoticesAsArray());
}
public function numberOfLostNotices() {
return $this->numberOfNotices() - count($this->getNoticesAsArray());
}
public function getExistingClesNotices() {
$notices = $this->getNoticesAsArray();
$cles_trouvees = [];
foreach($notices as $notice)
$cles_trouvees []= $notice->getClefAlpha();
public function getExistingClesNotices() {
$notices = $this->getNoticesAsArray();
$cles_trouvees = [];
foreach($notices as $notice)
$cles_trouvees []= $notice->getClefAlpha();
return $cles_trouvees;
}
return $cles_trouvees;
}
public function getLostClesNotices() {
return array_values(array_diff($this->getClesNotices(),
$this->getExistingClesNotices()));
}
public function getLostClesNotices() {
return array_values(array_diff($this->getClesNotices(),
$this->getExistingClesNotices()));
}
public function tryToFindLostClesNotices() {
$lost_cles_notices = $this->getLostClesNotices();
$new_cles_notices = [];
foreach($lost_cles_notices as $cle) {
if ($notice = (new Class_Notice_ClefAlpha($cle))->getUniqueNoticeWithSameTitleTomeTypeDoc())
$new_cles_notices []= $notice->getClefAlpha();
}
public function tryToFindLostClesNotices() {
$lost_cles_notices = $this->getLostClesNotices();
$new_cles_notices = $this->getClesNotices();
foreach($lost_cles_notices as $cle) {
if (!$notice = (new Class_Notice_ClefAlpha($cle))->getUniqueNoticeWithSameTitleTomeTypeDoc())
continue;
$index = array_search($cle, $new_cles_notices);
$new_cles_notices[$index] = $notice->getClefAlpha();
}
$this->setClesNotices(array_merge($this->getExistingClesNotices(), $new_cles_notices));
return $this;
}
$this->setClesNotices($new_cles_notices);
return $this;
}
public function beforeSave() {
$this->setDateMaj(date('Y-m-d'));
}
public function beforeSave() {
$this->setDateMaj(date('Y-m-d'));
}
/**
* @param Class_Notice notice la notice à retirer du panier
* @return Class_PanierNotice
*/
public function removeNotice($notice) {
return $this->setClesNotices(array_diff($this->getClesNotices(), [$notice->getClefAlpha()]));
}
/**
* @param Class_Notice notice la notice à retirer du panier
* @return Class_PanierNotice
*/
public function removeNotice($notice) {
return $this->setClesNotices(array_diff($this->getClesNotices(), [$notice->getClefAlpha()]));
}
/**
* @param Class_Notice notice la notice à ajouter au panier
* @return Class_PanierNotice
*/
public function addNotice($notice) {
return $this->setClesNotices(array_merge($this->getClesNotices(), [$notice->getClefAlpha()]));
}
/**
* @param Class_Notice notice la notice à ajouter au panier
* @return Class_PanierNotice
*/
public function addNotice($notice) {
return $this->setClesNotices(array_merge($this->getClesNotices(), [$notice->getClefAlpha()]));
}
public function toUnimarcISO2709() {
$data = '';
$notices = $this->getNoticesAsArray();
foreach($notices as $notice)
$data .= $notice->toUnimarcISO2709();
return $data;
}
public function toUnimarcISO2709() {
$data = '';
$notices = $this->getNoticesAsArray();
foreach($notices as $notice)
$data .= $notice->toUnimarcISO2709();
return $data;
}
public function toTabbedList() {
$data = '';
$notices = $this->getNoticesAsArray();
foreach($notices as $notice)
$data .= $notice->toTabbedList();
return $data;
}
public function toTabbedList() {
$data = '';
$notices = $this->getNoticesAsArray();
foreach($notices as $notice)
$data .= $notice->toTabbedList();
return $data;
}
public function getLibelleForAdmins() {
return $this->getLibelle().' - '.$this->getUserNomComplet();
}
public function getLibelleForAdmins() {
return $this->getLibelle().' - '.$this->getUserNomComplet();
}
public function getUserNomComplet() {
if (!$user = $this->getUser())
return '';
return $user->getNomComplet();
}
public function getUserNomComplet() {
if (!$user = $this->getUser())
return '';
return $user->getNomComplet();
}
public function isUserEquals($user) {
return $this->getUser()->getId() == $user->getId();
}
public function isUserEquals($user) {
return $this->getUser()->getId() == $user->getId();
}
//-------------------------------------------------------------------------------
......
......@@ -71,9 +71,9 @@ class PanierNoticeWithoutNoticesTest extends ModelTestCase {
['notices' => ' ']);
Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
->whenCalled('findAllBy')
->never()
->beStrict();
->whenCalled('findAllBy')
->never()
->beStrict();
}
......@@ -91,7 +91,7 @@ class PanierNoticeWithThreeNoticesTest extends ModelTestCase {
$this->fictions = new Class_PanierNotice();
$this->fictions->updateAttributes(array('id' => 4,
'libelle' => 'Fictions',
'notices' => ';STARWARS;;INDIANAJONES;SPIDERMAN;INDIANAJONES;TEMPLEPERDU---3--1985-1'));
'notices' => ';STARWARS;;INDIANAJONES;SPIDERMAN;INDIANAJONES;TEMPLEPERDU---3--1985-1;ZORK'));
$this->star_wars = Class_Notice::newInstanceWithId(3,
......@@ -108,10 +108,20 @@ class PanierNoticeWithThreeNoticesTest extends ModelTestCase {
Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
->whenCalled('findAllBy')
->with(['clef_alpha' => ['STARWARS', 'INDIANAJONES', 'SPIDERMAN', 'TEMPLEPERDU---3--1985-1'],
'order' => 'FIELD(clef_alpha, "STARWARS","INDIANAJONES","SPIDERMAN","TEMPLEPERDU---3--1985-1")'])
->answers([$this->star_wars, $this->indiana_jones, $this->spiderman]);
->whenCalled('findAllBy')
->with(['clef_alpha' => ['STARWARS', 'INDIANAJONES', 'SPIDERMAN', 'TEMPLEPERDU---3--1985-1', 'ZORK'],
'order' => 'FIELD(clef_alpha, "STARWARS","INDIANAJONES","SPIDERMAN","TEMPLEPERDU---3--1985-1","ZORK")'])
->answers([$this->star_wars, $this->indiana_jones, $this->spiderman])
->whenCalled('findAllBy')
->with(['where' => 'clef_alpha like "ZORK-%"',
'tome_alpha' => ''])
->answers([null]);
Storm_Test_ObjectWrapper::onLoaderOfModel('Class_PanierNotice')
->whenCalled('findAll')->answers([$this->fictions])
->whenCalled('save')->answers(true);
}
......@@ -137,34 +147,29 @@ class PanierNoticeWithThreeNoticesTest extends ModelTestCase {
/** @test */
public function numberOfLostNoticesShouldBeOne() {
$this->assertEquals(1, $this->fictions->numberOfLostNotices());
public function numberOfLostNoticesShouldBeTwo() {
$this->assertEquals(2, $this->fictions->numberOfLostNotices());
}
/** @test */
public function getLostClesNoticesShouldBeTEMPLEPERDU() {
$this->assertEquals(['TEMPLEPERDU---3--1985-1'], $this->fictions->getLostClesNotices());
public function getLostClesNoticesShouldBeTEMPLEPERDUAndZORK() {
$this->assertEquals(['TEMPLEPERDU---3--1985-1', 'ZORK'], $this->fictions->getLostClesNotices());
}
/** @test */
public function afterTryToFindLostClesNoticesShouldUpdateClesNoticeWithTEMPLERETROUVE() {
Storm_Test_ObjectWrapper::onLoaderOfModel('Class_PanierNotice')
->whenCalled('findAll')->answers([$this->fictions])
->whenCalled('save')->answers(true);
Class_Notice::getLoader()
->whenCalled('findAllBy')
->whenCalled('findAllBy')
->with(['where' => 'clef_alpha like "TEMPLEPERDU-%"',
'tome_alpha' => '3',
'type_doc' => 1])
->answers([Class_Notice::newInstanceWithId(54, ['clef_alpha' => 'TEMPLERETROUVE'])])
'tome_alpha' => '3'])
->answers([Class_Notice::newInstanceWithId(54, ['clef_alpha' => 'TEMPLERETROUVE'])])
->beStrict();
Class_PanierNotice::fixLostClesNoticesForAll();
$this->assertEquals(['STARWARS', 'INDIANAJONES', 'SPIDERMAN', 'TEMPLERETROUVE'],
$this->assertEquals(['STARWARS', 'INDIANAJONES', 'SPIDERMAN', 'TEMPLERETROUVE', 'ZORK'],
$this->fictions->getClesNotices());
return Class_PanierNotice::getLoader();
......@@ -178,6 +183,31 @@ class PanierNoticeWithThreeNoticesTest extends ModelTestCase {
public function paniersShouldBeSavedAfterFix($panier_loader) {
$this->assertTrue($panier_loader->methodHasBeenCalled('save'));
}
/** @test */
public function afterTryToFindLostClesNoticesShouldUpdateClesNoticeWithTEMPLERETROUVEWithDoublons() {
Class_Notice::getLoader()
->whenCalled('findAllBy')
->with(['where' => 'clef_alpha like "TEMPLEPERDU-%"',
'tome_alpha' => '3'])
->answers([Class_Notice::newInstanceWithId(54, ['clef_alpha' => 'TEMPLERETROUVE','type_doc' => 1]),
Class_Notice::newInstanceWithId(56, ['clef_alpha' => 'TEMPLERETROUVE','type_doc' => 3])])
->whenCalled('findAllBy')
->with(['where' => 'clef_alpha like "TEMPLEPERDU-%"',
'tome_alpha' => '3',
'type_doc' => '1'])
->answers([Class_Notice::newInstanceWithId(56, ['clef_alpha' => 'TEMPLERETROUVE','type_doc' => 1])])
->beStrict();
Class_PanierNotice::fixLostClesNoticesForAll();
$this->assertEquals(['STARWARS', 'INDIANAJONES', 'SPIDERMAN', 'TEMPLERETROUVE', 'ZORK'],
$this->fictions->getClesNotices());
return Class_PanierNotice::getLoader();
}
}
......
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