From 85f9076e107f535a02691b853b2b9325f01164d9 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Tue, 3 Feb 2015 12:28:19 +0100
Subject: [PATCH] dev #19573 : import typo 3 : add custom field to Sitotheque,
 ArticleCategorie and Catologue, store uid_typo3 for these classes

# Conflicts:
#	library/Class/Article.php
#	library/Class/ArticleCategorie.php
#	library/Class/Catalogue.php
#	library/Class/Sitotheque.php
#	library/Trait/CustomFields.php
#	scripts/import_typo3.php
---
 library/Class/Article.php                     |  4 ++
 library/Class/ArticleCategorie.php            | 18 +++++-
 library/Class/Catalogue.php                   | 15 ++++-
 .../ModelConfiguration/ArticleCategorie.php   | 33 ++++++++++
 .../ModelConfiguration/Catalogue.php          | 33 ++++++++++
 .../ModelConfiguration/Sitotheque.php         | 33 ++++++++++
 library/Class/Import/Typo3.php                | 18 ++++--
 library/Class/Sitotheque.php                  | 16 ++++-
 library/Trait/CustomFields.php                | 61 +++++++++----------
 scripts/import_typo3.php                      |  6 +-
 tests/library/Class/Import/Typo3Test.php      | 23 ++++++-
 11 files changed, 212 insertions(+), 48 deletions(-)
 create mode 100644 library/Class/CustomField/ModelConfiguration/ArticleCategorie.php
 create mode 100644 library/Class/CustomField/ModelConfiguration/Catalogue.php
 create mode 100644 library/Class/CustomField/ModelConfiguration/Sitotheque.php

diff --git a/library/Class/Article.php b/library/Class/Article.php
index e755c1f006a..cefcd9e1aab 100644
--- a/library/Class/Article.php
+++ b/library/Class/Article.php
@@ -1180,6 +1180,10 @@ class Class_Article extends Storm_Model_Abstract {
 		return $date->toString('YYYY-MM-dd HH:mm');
 	}
 
+
+	public function afterSave() {
+		$this->afterSaveCustomField();
+	}
 }
 
 ?>
diff --git a/library/Class/ArticleCategorie.php b/library/Class/ArticleCategorie.php
index cf6a97fa650..b726acfc4a3 100644
--- a/library/Class/ArticleCategorie.php
+++ b/library/Class/ArticleCategorie.php
@@ -39,8 +39,9 @@ class ArticleCategorieLoader extends Storm_Model_Loader {
 class Class_ArticleCategorie extends Storm_Model_Abstract {
 	use
 		Trait_TreeViewableCategorie,
-		Trait_TreeNode;
-//	use Trait_CustomFields;
+		Trait_TreeNode,
+    Trait_CustomFields;
+
 	protected
 		$_table_name = 'cms_categorie',
 		$_table_primary = 'ID_CAT',
@@ -140,5 +141,18 @@ class Class_ArticleCategorie extends Storm_Model_Abstract {
 
 		return '['.implode(',', $data).']';
 	}
+
+
+	public function _set($field, $value) {
+		return $this->_setCustomField($field,$value);
+	}
+
+	public function _get($field) {
+		return $this->_getCustomField($field);
+	}
+
+	public function afterSave() {
+		return $this->afterSaveCustomField();
+	}
 }
 ?>
diff --git a/library/Class/Catalogue.php b/library/Class/Catalogue.php
index 3d7489787e1..6742b743739 100644
--- a/library/Class/Catalogue.php
+++ b/library/Class/Catalogue.php
@@ -515,8 +515,8 @@ class CatalogueLoader extends Storm_Model_Loader {
 
 
 class Class_Catalogue extends Storm_Model_Abstract {
-	use Trait_TreeNode, Trait_Translator;
-//	use Trait_CustomFields;
+	use Trait_TreeNode, Trait_Translator, Trait_CustomFields;
+
 	protected
 		$_table_name = 'catalogue',
 		$_table_primary = 'ID_CATALOGUE',
@@ -925,6 +925,7 @@ class Class_Catalogue extends Storm_Model_Abstract {
 
 	public function afterSave(){
 		$this->saveThesaurus();
+		$this->afterSaveCustomField();
 	}
 
 
@@ -1111,6 +1112,16 @@ class Class_Catalogue extends Storm_Model_Abstract {
 																	'multipleSelection' => false]];
 
 	}
+
+
+	public function _set($field, $value) {
+		return $this->_setCustomField($field,$value);
+	}
+
+	public function _get($field) {
+		return $this->_getCustomField($field);
+	}
+
 }
 
 
diff --git a/library/Class/CustomField/ModelConfiguration/ArticleCategorie.php b/library/Class/CustomField/ModelConfiguration/ArticleCategorie.php
new file mode 100644
index 00000000000..d1d62ddcbaf
--- /dev/null
+++ b/library/Class/CustomField/ModelConfiguration/ArticleCategorie.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, 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_CustomField_ModelConfiguration_ArticleCategorie extends Class_CustomField_ModelConfiguration {
+    public function __construct() {
+        $this->_class_name = 'ArticleCategorie';
+        $this->_label = $this->_('ArticleCategorie');
+        $this->_edit_url = [
+            'module' => 'admin',
+            'controller' => 'cms',
+            'action' => 'edit'];
+    }
+}
+?>
\ No newline at end of file
diff --git a/library/Class/CustomField/ModelConfiguration/Catalogue.php b/library/Class/CustomField/ModelConfiguration/Catalogue.php
new file mode 100644
index 00000000000..cf2d2a6f796
--- /dev/null
+++ b/library/Class/CustomField/ModelConfiguration/Catalogue.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, 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_CustomField_ModelConfiguration_Catalogue extends Class_CustomField_ModelConfiguration {
+    public function __construct() {
+        $this->_class_name = 'Catalogue';
+        $this->_label = $this->_('Catalogue');
+        $this->_edit_url = [
+            'module' => 'admin',
+            'controller' => 'cms',
+            'action' => 'edit'];
+    }
+}
+?>
\ No newline at end of file
diff --git a/library/Class/CustomField/ModelConfiguration/Sitotheque.php b/library/Class/CustomField/ModelConfiguration/Sitotheque.php
new file mode 100644
index 00000000000..3462bb1560f
--- /dev/null
+++ b/library/Class/CustomField/ModelConfiguration/Sitotheque.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, 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_CustomField_ModelConfiguration_Sitotheque extends Class_CustomField_ModelConfiguration {
+    public function __construct() {
+        $this->_class_name = 'Sitotheque';
+        $this->_label = $this->_('Sitotheque');
+        $this->_edit_url = [
+            'module' => 'admin',
+            'controller' => 'cms',
+            'action' => 'edit'];
+    }
+}
+?>
\ No newline at end of file
diff --git a/library/Class/Import/Typo3.php b/library/Class/Import/Typo3.php
index e7cd665f360..a18b5ed1196 100644
--- a/library/Class/Import/Typo3.php
+++ b/library/Class/Import/Typo3.php
@@ -60,7 +60,11 @@ class Class_Import_Typo3 {
   }
 
 	public function createCustomField() {
-		return Class_Article::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT);
+		Class_Article::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT);
+		Class_ArticleCategorie::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT);
+		Class_Catalogue::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT);
+		Class_Sitotheque::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT);
+
 	}
 
 
@@ -166,12 +170,14 @@ class Class_Import_Typo3 {
                                                 'tags' => str_replace(', ', ';', $new['tx_danpextendnews_tags']),
                                                 'description' => $new['short']]);
 
+			$element->setUidTypo3($new['uid']);
       if (!$element->save()) {
         $this->report['sitotheque_errors']++;
         Class_Import_Typo3_Logs::getInstance()->addErrorRow("site with uid : " . $new['uid'] . ' (' . $new['title'] . ') - ' . implode(', ', $element->getErrors()));
         return $element;
       }
 
+
 			$foreign_uids = $this->t3db->findAllForeignUidForNewsCat($new['uid']);
 			$this->sites_categories_map->setDomainsFor($element, $foreign_uids);
 
@@ -539,12 +545,11 @@ class Class_Import_Typo3 {
 																					 'titre' => $new['header']]);
 
 
+		$element->setUidTypo3($new['uid']);
 		if (!$element->save()) {
 			$this->report['pages_errors']++;
 			Class_Import_Typo3_Logs::getInstance()->addErrorRow('pages with uid: ' . $new['uid'] . ' (' . $new['title'] . ') - ' . implode(', ', $element->getErrors()));
 		}
-		$element->setUidTypo3($new['uid']);
-		$element->save();
 		return $element;
 	}
 
@@ -684,6 +689,7 @@ abstract class CategoriesMap {
 
   public function newCategory($title, $t3uid, $t3pid) {
     $category = $this->_buildNewCategory($title, $this->getParentCatId($t3pid));
+		$category->setUidTypo3($t3uid);
     if(!$category->save())
 			return $this->addError($category, $title, $t3uid, $t3pid);
 
@@ -724,7 +730,7 @@ abstract class CategoriesMap {
 		$domains = [];
 		foreach ($t3_domain_ids as $t3_domain) {
 			$category = $this->find($t3_domain['uid_foreign']);
-			$domains[] = $this->domaine_map->findOrCreateDomaine($category);
+			$domains[] = $this->domaine_map->findOrCreateDomaine($category,$t3_domain['uid_foreign']);
 		}
 
 		if (isset($category)) {
@@ -740,7 +746,7 @@ abstract class CategoriesMap {
 class DomaineMap {
   protected $map = [];
 
-  public function findOrCreateDomaine($category) {
+  public function findOrCreateDomaine($category,$uid=0) {
     if (!$category)
       return null;
 
@@ -753,6 +759,8 @@ class DomaineMap {
     $catalogue = Class_Catalogue::newInstance(['libelle' => $category->getLibelle(),
                                                'parent_id' => $parent_domaine ? $parent_domaine->getId() : 0]);
 
+		if (!$uid)
+			$catalogue->setUidTypo3($uid);
 		if (!$catalogue->save()) {
 			Class_Import_Typo3_Logs::getInstance()->incrementDomainsRejected($catalogue);
 			return;
diff --git a/library/Class/Sitotheque.php b/library/Class/Sitotheque.php
index 16c45c34e99..95d3e0f3047 100644
--- a/library/Class/Sitotheque.php
+++ b/library/Class/Sitotheque.php
@@ -89,10 +89,8 @@ class SitothequeLoader extends Storm_Model_Loader {
 
 
 
-
 class Class_Sitotheque extends Storm_Model_Abstract {
-	use Trait_Translator, Trait_TreeViewableItem, Trait_HasManyDomaines;
-//	use Trait_CustomFields;
+		use Trait_Translator, Trait_TreeViewableItem, Trait_HasManyDomaines, Trait_CustomFields;
 	protected $_loader_class = 'SitothequeLoader';
 	protected $_table_name = 'sito_url';
 	protected $_table_primary = 'ID_SITO';
@@ -217,5 +215,17 @@ class Class_Sitotheque extends Storm_Model_Abstract {
 	}
 
 
+	public function _set($field, $value) {
+		return $this->_setCustomField($field,$value);
+	}
+
+	public function _get($field) {
+		return $this->_getCustomField($field);
+	}
+
+
+	public function afterSave() {
+		return $this->afterSaveCustomField();
+	}
 }
 ?>
\ No newline at end of file
diff --git a/library/Trait/CustomFields.php b/library/Trait/CustomFields.php
index fc7878db102..e634c615969 100644
--- a/library/Trait/CustomFields.php
+++ b/library/Trait/CustomFields.php
@@ -19,24 +19,21 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
-
 /**
- * To use this Trait in a model  you need to :
- * Register you model : cf Class_CustomField_Model::register
- * Redefine this 2 methods :
-	public function _set($field, $value) {
-    	return $this->_setCustomField($field,$value);
+ * To use this trait, please redefine
+  public function _set($field, $value) {
+		return $this->_setCustomField($field,$value);
 	}
 
 	public function _get($field) {
-	    return  $this->_getCustomField($field);
-  }
-
- *	WARN : this trait redefine afterSave() and beforeSave() method
- *
- */
+		return $this->_getCustomField($field);
+	}
 
+	public function afterSave() {
+		return $this->afterSaveCustomField();
+	}
 
+ **/
 
 trait Trait_CustomFields {
 	protected $custom_fields=[],
@@ -125,26 +122,6 @@ trait Trait_CustomFields {
 
 
 
-	public function afterSave() {
-		if (!$this->isRegistered())
-			return true;
-
-		foreach ($this->custom_fields as $field => $value) {
-
-			if (!$meta =self::getMeta($field))
-				continue;
-			$custom_field=Class_CustomField::findFirstBy(['meta_id' => $meta->getId(),
-																										'model'=> $this->getModelName()]);
-
-			if (!$cf_value = Class_CustomField_Value::findFirstBy(['model_id' => $this->getId(),
-																														 'custom_field_id'=> $custom_field->getId()]))
-				$cf_value = Class_CustomField_Value::newInstance(['model_id' => $this->getId() ,
-																													'custom_field_id' =>$custom_field->getId()]);
-
-			return 	$cf_value->setValue($value)->save();
-		}
-	}
-
 
 	public function findCustomFieldValue($custom_field_name) {
 		$meta=self::findMetaForLabel($custom_field_name);
@@ -200,6 +177,26 @@ trait Trait_CustomFields {
 		return $this->custom_fields[$field];
 	}
 
+	public function afterSaveCustomField() {
+		if (!$this->isRegistered())
+			return true;
+
+		foreach ($this->custom_fields as $field => $value) {
+
+			if (!$meta =self::getMeta($field))
+				continue;
+			$custom_field=Class_CustomField::findFirstBy(['meta_id' => $meta->getId(),
+																										'model'=> $this->getModelName()]);
+
+			if (!$cf_value = Class_CustomField_Value::findFirstBy(['model_id' => $this->getId(),
+																														 'custom_field_id'=> $custom_field->getId()]))
+				$cf_value = Class_CustomField_Value::newInstance(['model_id' => $this->getId() ,
+																													'custom_field_id' =>$custom_field->getId()]);
+
+			return 	$cf_value->setValue($value)->save();
+		}
+	}
+
 
 	public function afterDelete() {
 		if (!$this->isRegistered())
diff --git a/scripts/import_typo3.php b/scripts/import_typo3.php
index 442bb38a5f0..56062d2d76f 100644
--- a/scripts/import_typo3.php
+++ b/scripts/import_typo3.php
@@ -13,8 +13,10 @@ $toRun = $argv[1];
 
 Class_Import_Typo3_Logs::getInstance()->activateOutput();
 Class_CustomField_Model::registerAll([
-																			new Class_CustomField_ModelConfiguration_Article()
-																			]);
+																			new Class_CustomField_ModelConfiguration_Article(),
+																			new Class_CustomField_ModelConfiguration_Sitotheque(),
+																			new Class_CustomField_ModelConfiguration_Catalogue(),
+																			new Class_CustomField_ModelConfiguration_ArticleCategorie()]);
 
 $migration = new Class_Import_Typo3();
 $migration->run($toRun);
diff --git a/tests/library/Class/Import/Typo3Test.php b/tests/library/Class/Import/Typo3Test.php
index f0b14d6f1eb..cb913a4deff 100644
--- a/tests/library/Class/Import/Typo3Test.php
+++ b/tests/library/Class/Import/Typo3Test.php
@@ -39,7 +39,9 @@ abstract class Import_Typo3TestCase extends ModelTestCase {
     Class_SitothequeCategorie::beVolatile();
 
 		Class_CustomField_Model::registerAll([
-																					new Class_CustomField_ModelConfiguration_Article()
+																					new Class_CustomField_ModelConfiguration_Article(),
+																					new Class_CustomField_ModelConfiguration_Sitotheque(),
+																					new Class_CustomField_ModelConfiguration_ArticleCategorie()
 
 																					])
 			;
@@ -133,6 +135,16 @@ class Import_Typo3CategoryTest extends Import_Typo3TestCase {
 		$this->assertNotNull($parent);
 	}
 
+	/** @test */
+	public function sitoCategorieShouldHaveUidTypo3() {
+		$this->assertEquals('80',Class_SitothequeCategorie::findFirstBy(['libelle' => 'Musique & Cinéma'])->getUidTypo3());
+	}
+
+	/** @test */
+	public function categoryShouldHaveUidTypo3() {
+		$this->assertEquals(99,Class_ArticleCategorie::findFirstBy(['libelle' => 'Musique & Cinéma'])->getParent()->getUidTypo3());
+
+	}
 
 	/** @test */
 	public function categoryMusiqueCinemaShouldBeChildOfArt() {
@@ -552,12 +564,19 @@ class Import_Typo3SitothequeTest extends Import_Typo3TestCase {
 												$this->sito->getCategorie()->getPath());
 	}
 
-/** @test */
+  /** @test */
 	public function ouestCanadienShouldContainsUrlBokehSearch() {
 		$this->assertEquals('/miop-test.net/recherche/viewnotice/clef/OUEST_CANADIEN', Class_Sitotheque::findFirstBy(['titre' => 'L\'ouest canadien'])->getUrl());
 
 	}
 
+
+  /** @test */
+	public function ouestCanadienShouldStoreUidTypo3() {
+		$this->assertEquals(14479, Class_Sitotheque::findFirstBy(['titre' => 'L\'ouest canadien'])->getUidTypo3());
+
+	}
+
 }
 
 
-- 
GitLab