diff --git a/library/Class/Profil.php b/library/Class/Profil.php index f788818c172c8a76ffaa39aa38bac0cb638a62d6..d3090dc8319e242232d5ce74c035a13b1204ea87 100644 --- a/library/Class/Profil.php +++ b/library/Class/Profil.php @@ -59,14 +59,15 @@ class Class_Profil extends Storm_Model_Abstract { protected $_table_name = 'bib_admin_profil'; protected $_table_primary = 'ID_PROFIL'; - protected $_belongs_to = array('bib' => array('model' => 'Class_Bib', - 'referenced_in' => 'id_site'), - 'parent_profil' => array('model' => 'Class_Profil', - 'referenced_in' => 'parent_id')); + protected $_belongs_to = ['bib' => ['model' => 'Class_Bib', + 'referenced_in' => 'id_site'], - protected $_has_many = array('sub_profils' => array('model' => 'Class_Profil', - 'role' => 'parent', - 'dependents' => 'delete')); + 'parent_profil' => ['model' => 'Class_Profil', + 'referenced_in' => 'parent_id']]; + + protected $_has_many = ['sub_profils' => ['model' => 'Class_Profil', + 'role' => 'parent_profil', + 'dependents' => 'delete']]; protected static $_current_profil; protected static $DEFAULT_VALUES, $CFG_SITE_KEYS, $FORWARDED_ATTRIBUTES; @@ -1122,19 +1123,35 @@ class Class_Profil extends Storm_Model_Abstract { /** + * Créé une copie profil (non récursif) * @return Class_Profil */ public function copy() { - $clone = new Class_Profil(); + $copy = new Class_Profil(); $attributes = $this->_attributes; unset($attributes['id']); unset($attributes['id_profil']); - return $clone + + return $copy ->updateAttributes($attributes) ->setLibelle('** Nouveau Profil **'); } + /** + * Créé une copie du profil et de ses sous-pages + * @return Class_Profil + */ + public function deepCopy() { + $clone = $this->copy()->setSubProfils([]); + $pages = $this->getSubProfils(); + foreach($pages as $page) + $clone->addSubProfil($page->copy()->setLibelle($page->getLibelle())); + + return $clone; + } + + /** * @param Class_Profil $new_parent * @return Class_Profil @@ -1184,9 +1201,18 @@ class Class_Profil extends Storm_Model_Abstract { * @return array */ public function getSubProfils() { - $sub_profils = array(); - foreach(parent::_get('sub_profils') as $profil) - $sub_profils[$profil->getLibelle()] = $profil; + $sub_profils = []; + foreach(parent::_get('sub_profils') as $profil) { + $libelle = $profil->getLibelle(); + + if (isset($sub_profils[$libelle])) { + $i = 1; + while (isset($sub_profils[$libelle.' ('.$i.')'])) $i++; + $libelle = $libelle.' ('.$i.')'; + } + + $sub_profils[$libelle] = $profil; + } ksort($sub_profils); return $sub_profils; diff --git a/scripts/opac3.el b/scripts/opac3.el index c891f5d4607a1a8f3fef5e2a67e7b5d3286a1063..fc2b6c82461bca7447f5982ee84d5360623e7aee 100644 --- a/scripts/opac3.el +++ b/scripts/opac3.el @@ -39,10 +39,12 @@ (setq opac3-mode-hook '(opac3-php-mode)) + (defun opac3-php-mode() (require 'geben) (require 'phpunit) (require 'auto-complete) + (require 'auto-complete-etags) (auto-complete-mode t) ;; (setq ac-sources '(ac-source-etags ac-source-words-in-same-mode-buffers)) (setq ac-sources '(ac-source-etags ac-source-words-in-buffer)) diff --git a/tests/library/Class/ProfilTest.php b/tests/library/Class/ProfilTest.php index 18cd3f32caebe810ff506d9283a6a85b601842c4..ff7d3b2013e26fc148e6983d5a4ea0e57baf9b4a 100644 --- a/tests/library/Class/ProfilTest.php +++ b/tests/library/Class/ProfilTest.php @@ -272,7 +272,7 @@ class ProfilJeunesseAstrolabeTest extends ModelTestCase { */ public function profilLoaderShouldHaveFindAllByCalledWithParentIdOfAstrolabe($loader) { $param = $loader->getFirstAttributeForLastCallOn('findAllBy'); - $this->assertEquals('parent', $param['role']); + $this->assertEquals('parent_profil', $param['role']); $this->assertEquals($this->profil_astro->toArray(), $param['model']->toArray()); @@ -900,4 +900,98 @@ class ProfilPortailTest extends ModelTestCase { public function copyShouldNotHaveIdProfil() { $this->assertEmpty($this->_profil->copy()->id_profil); } -} \ No newline at end of file +} + + + + +class ProfilWithPagesCopyTest extends Storm_Test_ModelTestCase { + protected $_clone; + + public function setUp() { + parent::setUp(); + + $id = 100; + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Profil') + ->whenCalled('findAllBy') + ->answers([]) + ->whenCalled('save') + ->willDo(function ($model) use (&$id) { + $model->setId($id++); + } ); + + + $profil = Class_Profil::newInstanceWithId(3) + ->setSubProfils([ Class_Profil::newInstanceWithId(31), + Class_Profil::newInstanceWithId(32), + Class_Profil::newInstanceWithId(33)->setLibelle('CD'), + Class_Profil::newInstanceWithId(34) + ]); + + $this->_clone = $profil->deepCopy(); + $this->_clone->save(); + } + + + /** @test */ + public function cloneShouldHaveFourPages() { + $this->assertEquals(4, $this->_clone->numberOfSubProfils()); + } + + + /** @test */ + public function cloneFirstPageLibelleShouldBeIndexedAtNouveauProfil() { + $this->assertEquals('** nouveau profil **', $this->_clone->getSubProfils()['** nouveau profil **']->getLibelle()); + } + + + /** @test */ + public function cloneSecondPageLibelleShouldBeIndexedAtNouveauProfil1() { + $this->assertEquals('** nouveau profil **', $this->_clone->getSubProfils()['** nouveau profil ** (1)']->getLibelle()); + } + + + /** @test */ + public function cloneThirdPageLibelleShouldBeIndexedAtCD() { + $this->assertEquals('CD', $this->_clone->getSubProfils()['CD']->getLibelle()); + } + + + /** @test */ + public function cloneFourthPageLibelleShouldBeIndexedAtNouveauProfil2() { + $this->assertEquals('** nouveau profil **', $this->_clone->getSubProfils()['** nouveau profil ** (2)']->getLibelle()); + } + + + /** @test */ + public function cloneIdShouldBe100() { + $this->assertEquals(100, $this->_clone->getId()); + } + + + /** @test */ + public function cloneFirstPageIdShouldBe101() { + $this->assertEquals(101, array_values($this->_clone->getSubProfils())[0]->getId()); + } + + + /** @test */ + public function cloneFirstPageParentIdShouldBe100() { + $this->assertEquals(100, array_values($this->_clone->getSubProfils())[0]->getParentId()); + } + + + /** @test */ + public function cloneLastPageIdShouldBe104() { + $this->assertEquals(104, array_values($this->_clone->getSubProfils())[3]->getId()); + } + + /** @test */ + public function cloneLastPageParentIdShouldBe100() { + $this->assertEquals(100, array_values($this->_clone->getSubProfils())[3]->getParentId()); + } + +} + + +?> \ No newline at end of file