From db5bf2e96775af08197300c34eda69af468e612c Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Thu, 26 Jun 2014 17:31:47 +0200
Subject: [PATCH] dev #12992 #14706 custom fields

fix toggle visibility of select in meta form + adding missing files
---
 library/Class/CustomField/ModelValues.php     | 43 ++++++++++++++++
 library/ZendAfi/Form/Admin/CustomFields.php   |  2 +-
 .../Admin/CustomFields/CustomFieldMeta.php    |  3 +-
 .../Form/Admin/CustomFields/ModelValues.php   | 49 +++++++++++++++++++
 public/admin/css/global.css                   |  5 --
 .../CustomFieldsControllerTest.php            | 15 ++++--
 6 files changed, 105 insertions(+), 12 deletions(-)
 create mode 100644 library/Class/CustomField/ModelValues.php
 create mode 100644 library/ZendAfi/Form/Admin/CustomFields/ModelValues.php

diff --git a/library/Class/CustomField/ModelValues.php b/library/Class/CustomField/ModelValues.php
new file mode 100644
index 00000000000..1f3e9ec275e
--- /dev/null
+++ b/library/Class/CustomField/ModelValues.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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).
+ *
+ * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+
+class Class_CustomField_ModelValues {
+	protected 
+		$_real_instance,
+		$_model;
+
+	public function __construct($real_instance, $model) {
+		$this->_real_instance = $real_instance; // ex: instance of Class_Article, Class_UserGroup
+		$this->_model = $model;
+	}
+
+
+	public function getLabel() {
+		return $this->_real_instance->getLabel();
+	}
+
+
+	public function getFields() {
+		return $this->_model->getFields();
+	}
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/Form/Admin/CustomFields.php b/library/ZendAfi/Form/Admin/CustomFields.php
index 9615716bf31..681ba733e9d 100644
--- a/library/ZendAfi/Form/Admin/CustomFields.php
+++ b/library/ZendAfi/Form/Admin/CustomFields.php
@@ -58,7 +58,7 @@ class ZendAfi_Form_Admin_CustomFields extends ZendAfi_Form {
 	
 	protected function _toggleOptionsList() {
 		return Class_ScriptLoader::getInstance()
-			->addJQueryBackEnd('if($("#field_type").val() =="SELECT") $("#fieldset-options").removeClass("invisible");$("#field_type").change(function(event) {if(this.value=="SELECT") {$("#fieldset-options").removeClass("invisible"); return false} if(!$("#fieldset-options").hasClass("invisible")) {$("#fieldset-options").addClass("invisible"); return false;}});');
+			->addJQueryBackEnd('formSelectToggleVisibilityForElement("#field_type", "#fieldset-options", "'.Class_CustomField_Meta::SELECT.'");');
 	}
 
 }
diff --git a/library/ZendAfi/Form/Admin/CustomFields/CustomFieldMeta.php b/library/ZendAfi/Form/Admin/CustomFields/CustomFieldMeta.php
index 942f3d047e3..1aab8c753dd 100644
--- a/library/ZendAfi/Form/Admin/CustomFields/CustomFieldMeta.php
+++ b/library/ZendAfi/Form/Admin/CustomFields/CustomFieldMeta.php
@@ -26,8 +26,7 @@ class ZendAfi_Form_Admin_CustomFields_CustomFieldMeta extends ZendAfi_Form_Admin
 		$form
 			->populate($custom_field_meta_array)
 			->addDisplayGroup(['label', 'field_type'],'base',['legend' => ''])
-			->addDisplayGroup(['options_list'], 'options', ['legend' => $form->_('Options'),
-																											'class' => 'invisible']);
+			->addDisplayGroup(['options_list'], 'options', ['legend' => $form->_('Options')]);
 		return $form;
 	}
 }
diff --git a/library/ZendAfi/Form/Admin/CustomFields/ModelValues.php b/library/ZendAfi/Form/Admin/CustomFields/ModelValues.php
new file mode 100644
index 00000000000..4126fd18e4b
--- /dev/null
+++ b/library/ZendAfi/Form/Admin/CustomFields/ModelValues.php
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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).
+ *
+ * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+
+class ZendAfi_Form_Admin_CustomFields_ModelValues extends ZendAfi_Form {
+	protected 
+		$_model_values;
+
+	public function setModel_Values($model_values) {
+		$this->_model_values = $model_values;
+	}
+
+	public function init() {
+		parent::init();
+
+		$field_names = [];
+		foreach($this->_model_values->getFields() as $item) {
+			$field_name = 'field_'.$item->getId();
+			$field_names []= $field_name;
+			$this->addElement($item->getFieldType(), 
+												$field_name, 
+												['label' => $item->getLabel()]);
+		}
+
+		$this->addDisplayGroup($field_names,
+													 'custom_fields',
+													 []);
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/public/admin/css/global.css b/public/admin/css/global.css
index 3ef3c76f3ff..7ce464dc97b 100644
--- a/public/admin/css/global.css
+++ b/public/admin/css/global.css
@@ -1178,8 +1178,3 @@ span.ui-dialog-title {
 div#reader {
 		min-width: 700px;
 }
-
-
-.invisible {
-    visibility: hidden;
-}
\ No newline at end of file
diff --git a/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php b/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php
index 4d4d985b679..46b3102d545 100644
--- a/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php
+++ b/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php
@@ -65,6 +65,7 @@ abstract class CustomFieldControllerTestCase extends AbstractControllerTestCase
 										'priority' => 3,
 										'label' => 'Status',
 										'field_type' => Class_CustomField_Meta::SELECT,
+										'options_list' => 'enabled; disabled; ',
 										'model' => 'UserGroup']);
 	}
 }
@@ -239,7 +240,7 @@ class CustomFieldsControllerPostAddActionTest extends AbstractControllerTestCase
 
 		$this->postDispatch('admin/custom-fields/add/model/Article', 
 												['label' => 'public',
-												'field_type' => 'TEXT_INPUT',
+												'field_type' => 'text',
 												'model' => 'Article']);
 	}
 	
@@ -259,7 +260,7 @@ class CustomFieldsControllerPostAddActionTest extends AbstractControllerTestCase
 
 	/** @test */
 	public function customFieldMetaShouldBeText() {
-		$this->assertEquals('TEXT_INPUT', Class_CustomField_Meta::find(1)->getFieldType());
+		$this->assertEquals('text', Class_CustomField_Meta::find(1)->getFieldType());
 	}
 
 
@@ -286,7 +287,7 @@ class CustomFieldsControllerEditIndexTest extends CustomFieldControllerTestCase
 	
 	/** @test */
 	public function fieldTypeShouldBeText() {
-		$this->assertXPath('//select/option[@value="TEXT_INPUT"][@selected="selected"]');
+		$this->assertXPath('//select/option[@value="text"][@selected="selected"]');
 	}
 
 
@@ -298,7 +299,7 @@ class CustomFieldsControllerEditIndexTest extends CustomFieldControllerTestCase
 	
 	/** @test */
 	public function scriptToggleOptionsListShouldBePresent() {
-		$this->assertXPathContentContains('//script', '$("#fieldset-options").removeClass("invisible")');
+		$this->assertXPathContentContains('//script', 'formSelectToggleVisibilityForElement("#field_type", "#fieldset-options", "'.Class_CustomField_Meta::SELECT.'");');
 	}
 }
 
@@ -418,6 +419,12 @@ class CustomFieldsControllerValuesForUserGroupTeachersTest extends CustomFieldCo
 	 public function textAreaForAgeShouldBePresent() {
 		 $this->assertXPath('//form//fieldset//textarea[@name="field_4"]');
 	 }
+
+	 
+	 /** @test */
+	 public function selectOptionsShouldBePresent() {
+		 $this->assertXPathContentContains('//form//fieldset//select//option[@value="1"]', 'enabled');
+	 }
  }
 
 
-- 
GitLab