Skip to content
Snippets Groups Projects
Commit 4b39f06e authored by Sebastien ANDRE's avatar Sebastien ANDRE
Browse files

hotline : #143141 : tools to clear unexist codif on album

parent a836c817
Branches
Tags 8.0.61
2 merge requests!4289Hotline,!4284hotline : #143141 : tools to clear unexist codif on album
Pipeline #15317 passed with stage
in 27 minutes and 10 seconds
- ticket #143141 : Admin : Album netoyer les PNB pour supprimer les facettes associés à des codifications supprimés
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2021, 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_Migration_AlbumClearCodif {
protected
$_cat_id,
$_list_codif_status;
public function __construct($cat_id) {
$this->_cat_id = $cat_id;
$this->_list_codif_status = new Storm_Collection();
}
public function run() {
try {
if (!$this->_cat_id
|| !$category = Class_AlbumCategorie::find($this->_cat_id))
return;
$albums = Class_Album::findAllBy(['cat_id' => $category->getSousCategoriesIds()]);
array_map(function ($album) {
$this->_withAlbumDo($album);
}, $albums);
(new Storm_Cache())->clean();
} catch (Exception $e) {
return;
}
}
protected function _withAlbumDo($album) {
$must_save = $this->_checkGenre($album);
$must_save |= $this->_checkSection($album);
$must_save |= $this->_checkAnnexe($album);
$must_save |= $this->_checkMatiere($album);
if ($must_save) {
$album->save();
$album->index();
}
}
protected function _checkGenre($album) {
return $this->_checkCodif($album, 'genre', Class_CodifGenre::class);
}
protected function _checkSection($album) {
return $this->_checkCodif($album, 'sections', Class_CodifSection::class);
}
protected function _checkAnnexe($album) {
return $this->_checkCodif($album, 'annexes', Class_CodifAnnexe::class);
}
protected function _checkMatiere($album) {
return $this->_checkCodif($album, 'matiere', Class_CodifMatiere::class);
}
protected function _checkCodif($album, $attrib, $class_name) {
$is_updated = false;
$list_codif = [];
foreach (explode(';', $album->$attrib) as $codif_id)
$is_updated |= $this->_isUpdatedListCodif($class_name, $codif_id, $list_codif);
if ($is_updated)
$album->$attrib = implode(';', $list_codif);
return $is_updated;
}
protected function _isUpdatedListCodif($class_name, $codif_id, &$list_codif) {
$valid = $this->_existForClassName($class_name, $codif_id);
if ($valid)
$list_codif [] = $codif_id;
return !$valid;
}
protected function _existForClassName($class_name, $codif_id) {
$codif_for_type = $this->_list_codif_status
->detect(function ($codif_status) use ($class_name, $codif_id) {
return $class_name == $codif_status->getClassName()
&& $codif_id == $codif_status->getCodifId();
});
if (!$codif_for_type)
$codif_for_type = $this->_addNewCodif($class_name, $codif_id);
return $codif_for_type->isExist();
}
protected function _addNewCodif($type, $codif_id) {
$new_codif = (new CodifStatus($type, $codif_id))->init();
if ($new_codif->isValid())
$this->_list_codif_status->add($new_codif);
return $new_codif;
}
}
class CodifStatus {
protected
$_exist = false,
$_class_name,
$_codif_id;
public function __construct($class_name, $codif_id) {
$this->_class_name = $class_name;
$this->_codif_id = $codif_id;
}
public function isValid() {
return $this->_class_name && $this->_codif_id;
}
public function init() {
if (!$this->isValid())
return $this;
$codif = $this->_class_name::find($this->_codif_id);
if ($codif)
$this->_exist = true;
return $this;
}
public function getClassName() {
return $this->_class_name;
}
public function getCodifId() {
return $this->_codif_id;
}
public function isExist() {
return $this->_exist;
}
}
<?php
require __DIR__ . '/../console.php';
(new Class_Migration_AlbumClearCodif($argv[1]))->run();
<?php
/**
* Copyright (c) 2012-2021, 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
*/
/* hotline: #143141 */
class AlbumClearCodifTest extends ModelTestCase {
protected $_storm_default_to_volatile = true;
public function setUp() {
parent::setUp();
$this->fixture(Class_CodifGenre::class,
['id' => 1,
'libelle' => 'Adulte'
]);
$this->fixture(Class_CodifAnnexe::class,
['id' => 4,
'libelle' => 'Biviers'
]);
$this->fixture(Class_CodifAnnexe::class,
['id' => 7,
'libelle' => 'Froges'
]);
$this->fixture(Class_CodifMatiere::class,
['id' => 8,
'libelle' => 'Documentaire Guides'
]);
$this->fixture(Class_CodifMatiere::class,
['id' => 9,
'libelle' => 'Documentaire Histoire'
]);
$this->fixture(Class_CodifMatiere::class,
['id' => 12,
'libelle' => 'Documentaire Humour'
]);
$this->fixture(Class_AlbumCategorie::class,
['id' => 100,
'parent_id' => 0
]);
$this->fixture(Class_AlbumCategorie::class,
['id' => 101,
'parent_id' => 0
]);
$this->fixture(Class_Album::class,
['id' => 200,
'cat_id' => 100,
'genre' => '1;5',
'sections' => '2',
'annexes' => '4;6;7',
'matiere' => '8;9;10;11;12',
'titre' => 'Amuse-bouche'
]);
$this->fixture(Class_Album::class,
['id' => 201,
'cat_id' => 101,
'genre' => '1;5',
'sections' => '2',
'annexes' => '4;6;7',
'matiere' => '8;9;10;11;12',
'titre' => 'Animal'
]);
(new Class_Migration_AlbumClearCodif(100))->run();
}
/** @test */
public function album200ShouldHaveLostGenreId5() {
$this->assertEquals('1', Class_Album::find(200)->getGenre());
}
/** @test */
public function album201ShouldNotHaveLostGenreIds() {
$this->assertEquals('1;5', Class_Album::find(201)->getGenre());
}
/** @test */
public function album200ShouldHaveLostSectionId2() {
$this->assertEquals('', Class_Album::find(200)->getSections());
}
/** @test */
public function album201ShouldNotHaveLostSectionIds() {
$this->assertEquals('2', Class_Album::find(201)->getSections());
}
/** @test */
public function album200ShouldHaveLostAnnexeId6() {
$this->assertEquals('4;7', Class_Album::find(200)->getAnnexes());
}
/** @test */
public function album201ShouldNotHaveLostAnnexeIds() {
$this->assertEquals('4;6;7', Class_Album::find(201)->getAnnexes());
}
/** @test */
public function album200ShouldHaveLostMatiereId10And11() {
$this->assertEquals('8;9;12', Class_Album::find(200)->getMatiere());
}
/** @test */
public function album201ShouldNotHaveLostMatiereIds() {
$this->assertEquals('8;9;10;11;12', Class_Album::find(201)->getMatiere());
}
}
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