From ece324c2bcf2c724af630c9d2e4d9d5143535b0f Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Thu, 5 Nov 2015 16:58:09 +0100
Subject: [PATCH] rel #32494 album web site: automatic creation of website
 category

---
 .../modules/admin/controllers/SitoController.php     |  5 ++++-
 library/Class/AlbumCategorie.php                     | 12 ++++++++++++
 .../admin/controllers/SitothequeControllerTest.php   |  6 ++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/application/modules/admin/controllers/SitoController.php b/application/modules/admin/controllers/SitoController.php
index 392b4c6cf66..21bb76910ce 100644
--- a/application/modules/admin/controllers/SitoController.php
+++ b/application/modules/admin/controllers/SitoController.php
@@ -71,7 +71,10 @@ class Admin_SitoController extends ZendAfi_Controller_Action {
     $html = (new Class_WebService_SimpleWebClient())->open_url($url);
     $dom = new Zend_Dom_Query($html);
 
-    $album = Class_Album::newInstance(['type_doc_id' => Class_TypeDoc::WEBSITE]);
+    $category = Class_AlbumCategorie::getOrCreateRootCategory('Sites web');
+
+    $album = Class_Album::newInstance(['type_doc_id' => Class_TypeDoc::WEBSITE,
+                                       'categorie' => $category]);
     $album->setTitre($dom->queryXpath('//head/title')->current()->textContent);
 
     $description_node = $dom->queryXpath('//head/meta[@name="description"]')->current();
diff --git a/library/Class/AlbumCategorie.php b/library/Class/AlbumCategorie.php
index 6c0c8d7f322..a9320e85d74 100644
--- a/library/Class/AlbumCategorie.php
+++ b/library/Class/AlbumCategorie.php
@@ -20,6 +20,18 @@
  */
 
 class AlbumCategorieLoader extends Storm_Model_Loader {
+
+  public function getOrCreateRootCategory($label) {
+    if ($category = Class_AlbumCategorie::findFirstBy(['libelle' => $label,
+                                                        'parent_id' => 0]))
+      return $category;
+
+    $category = Class_AlbumCategorie::newInstance(['libelle' => $label]);
+    $category->save();
+    return $category;
+  }
+
+
   /**
    * @return array
    */
diff --git a/tests/application/modules/admin/controllers/SitothequeControllerTest.php b/tests/application/modules/admin/controllers/SitothequeControllerTest.php
index 75e83551047..2d144df706f 100644
--- a/tests/application/modules/admin/controllers/SitothequeControllerTest.php
+++ b/tests/application/modules/admin/controllers/SitothequeControllerTest.php
@@ -634,6 +634,12 @@ class SitothequeControllerImportFromUrlTest extends AbstractControllerTestCase {
   }
 
 
+  /** @test */
+  public function albumCategorieShouldBeWebsite() {
+    $this->assertEquals('Sites web', $this->_cned->getCategorie()->getLibelle());
+  }
+
+
   /** @test */
   public function albumShouldHaveOneResourceWithUrlCnedDotFr() {
     $this->assertNotNull($resource = $this->_cned->getRessources()[0]);
-- 
GitLab