From 3f4c1e83970222c71272f16ca33d8ad24ba51039 Mon Sep 17 00:00:00 2001 From: llaffont <llaffont@afi-sa.fr> Date: Thu, 26 Jun 2014 11:42:44 +0200 Subject: [PATCH] dev #12992 custom fields ordering of fields now works correctly --- .../controllers/CustomFieldsController.php | 18 ++---- library/Class/CustomField.php | 46 +++++++++++++++ .../CustomFieldsControllerTest.php | 56 ++++++++++++++++--- 3 files changed, 100 insertions(+), 20 deletions(-) diff --git a/application/modules/admin/controllers/CustomFieldsController.php b/application/modules/admin/controllers/CustomFieldsController.php index 53f42cc53f9..7435a9d7886 100644 --- a/application/modules/admin/controllers/CustomFieldsController.php +++ b/application/modules/admin/controllers/CustomFieldsController.php @@ -53,7 +53,6 @@ class Admin_CustomFieldsController extends ZendAfi_Controller_Action { $this->view->custom_fields_metas = Class_CustomField::getAvailableMeta($model); parent::addAction(); - } public function selectAction() { @@ -69,21 +68,16 @@ class Admin_CustomFieldsController extends ZendAfi_Controller_Action { public function upAction() { - $this->setPosition(-1); + if ($field = Class_CustomField::find($id = $this->_getParam('id'))) + $field->movePriorityUp(); + $this->_redirect('/admin/custom-fields/index/id/'.$id); } public function downAction() { - $this->setPosition(1); + if ($field = Class_CustomField::find($id = $this->_getParam('id'))) + $field->movePriorityDown(); + $this->_redirect('/admin/custom-fields/index/id/'.$id); } - - - protected function setPosition($move) { - $custom_field = Class_CustomField::find($this->_getParam('id')); - $custom_field->setPriority($custom_field->getPriority()+$move); - $custom_field->save(); - $this->_redirectToReferer(); - } - } ?> \ No newline at end of file diff --git a/library/Class/CustomField.php b/library/Class/CustomField.php index 10d7a689c1d..9112d0bbab4 100644 --- a/library/Class/CustomField.php +++ b/library/Class/CustomField.php @@ -34,6 +34,12 @@ class CustomFieldsLoader extends Storm_Model_Loader { return $availables_meta; } + + + public function initializePrioritiesAndSave($fields) { + foreach($fields as $priority => $field) + $field->setPriority($priority + 1)->save(); + } } @@ -109,5 +115,45 @@ class Class_CustomField extends Storm_Model_Abstract { 'field_type' => $this->getFieldType(), 'options_list' => $this->getOptionsList()]); } + + public function getFieldsOfSameModel() { + return Class_CustomField::getLoader()->findAllBy(['model' => $this->getModel(), + 'order' => 'priority']); + } + + public function movePriorityUp() { + $this + ->getLoader() + ->initializePrioritiesAndSave(array_reverse( + $this->moveMeDownInFieldList( + array_reverse( + $this->getFieldsOfSameModel())))); + return $this; + } + + + public function movePriorityDown() { + $this + ->getLoader() + ->initializePrioritiesAndSave($this->moveMeDownInFieldList( + $this->getFieldsOfSameModel())); + } + + + public function moveMeDownInFieldList($model_fields) { + $new_order = []; + + while($model_fields) { + $field = array_shift($model_fields); + if ($field->getId() !== $this->getId()) { + $new_order [] = $field; + continue; + } + $new_order [] = array_shift($model_fields); + $new_order [] = $field; + } + + return array_values(array_filter($new_order)); + } } ?> \ 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 7a04a483708..c95f3983aaf 100644 --- a/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php +++ b/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php @@ -36,21 +36,21 @@ abstract class CustomFieldControllerTestCase extends AbstractControllerTestCase $this->fixture('Class_CustomField', ['id' => 1, 'meta_id' => 1, - 'priority' => 2, + 'priority' => 1, 'model' => 'Article']); $this->fixture('Class_CustomField', ['id' => 2, 'meta_id' => 1, - 'priority' => 2, + 'priority' => 1, 'model' => 'Formation']); $this->fixture('Class_CustomField', ['id' => 3, 'meta_id' => 1, - 'priority' => 2, + 'priority' => 1, 'model' => 'UserGroup']); } } @@ -330,16 +330,56 @@ class CustomFieldsControllerSelectActionTest extends CustomFieldControllerTestCa -class CustomFieldsControllerUpActionTest extends CustomFieldControllerTestCase { +class CustomFieldsControllerUpDownActionTest extends CustomFieldControllerTestCase { public function setup() { parent::setup(); - $this->dispatch('admin/custom-fields/down/id/1', true); + + $this->fixture('Class_CustomField', + ['id' => 4, + 'priority' => 2, + 'label' => 'Age', + 'field_type' => Class_CustomField_Meta::TEXT_INPUT, + 'model' => 'UserGroup']); + + $this->fixture('Class_CustomField', + ['id' => 5, + 'priority' => 3, + 'label' => 'Status', + 'field_type' => Class_CustomField_Meta::TEXT_INPUT, + 'model' => 'UserGroup']); + } + + public function updownForFieldExpectedOrder() { + // at start we have: Address, Age, Status + return [ + ['down', 3, ['Age', 'Address', 'Status']], + ['down', 4, ['Address', 'Status', 'Age']], + ['down', 5, ['Address', 'Age', 'Status']], + ['down', 0, ['Address', 'Age', 'Status']], + + ['up', 0, ['Address', 'Age', 'Status']], + ['up', 4, ['Age', 'Address', 'Status']], + ['up', 5, ['Address', 'Status', 'Age']] + ]; } - /** @test */ - public function metaAdressShouldHavePriorityTree() { - $this->assertEquals(3, Class_CustomField::find(1)->getPriority()); + /** + * @dataProvider updownForFieldExpectedOrder + * @test + */ + public function userGroupFieldsShouldBeOrdererd($action, $id, $expected_order) { + $this->dispatch('admin/custom-fields/'.$action.'/id/'.$id, true); + + Class_CustomField::clearCache(); + + $fields = Class_CustomField::findAllBy(['model' => 'UserGroup', + 'order' => 'priority']); + $labels = array_map(function($field) {return $field->getLabel();}, + $fields); + + $this->assertEquals($expected_order, $labels); + $this->assertRedirectTo('/admin/custom-fields/index/id/'.$id); } } ?> \ No newline at end of file -- GitLab