From 2f710d6518ccfad163cfb2fba6fcd3105e001840 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Tue, 24 Jan 2017 20:35:50 +0000 Subject: [PATCH] FIX Improve publish performance for formfields (#538) * FIX Improve publish performance for formfields * FIX Travis builds broken with external code coverage --- .travis.yml | 6 ++-- .../UserFormFieldEditorExtension.php | 36 +++++++++++++------ .../editableformfields/EditableFormField.php | 22 ++++++++++-- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6367e79..4f1409f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,9 @@ before_script: script: - vendor/bin/phpunit --coverage-clover coverage.clover userforms/tests + +after_script: + - mv coverage.clover ~/build/$TRAVIS_REPO_SLUG/ + - cd ~/build/$TRAVIS_REPO_SLUG - wget https://scrutinizer-ci.com/ocular.phar - - git remote rm origin - - git remote add origin git@github.com:silverstripe/silverstripe-userforms.git - php ocular.phar code-coverage:upload --format=php-clover coverage.clover diff --git a/code/extensions/UserFormFieldEditorExtension.php b/code/extensions/UserFormFieldEditorExtension.php index 0fd1e55..2532456 100644 --- a/code/extensions/UserFormFieldEditorExtension.php +++ b/code/extensions/UserFormFieldEditorExtension.php @@ -136,19 +136,33 @@ class UserFormFieldEditorExtension extends DataExtension { * @return void */ public function onAfterPublish($original) { - // Remove fields on the live table which could have been orphaned. - $live = Versioned::get_by_stage("EditableFormField", "Live") - ->filter('ParentID', $original->ID); + // store IDs of fields we've published + $seenIDs = array(); - if($live) { - foreach($live as $field) { - $field->doDeleteFromStage('Live'); - } - } + foreach ($this->owner->Fields() as $field) { + // store any IDs of fields we publish so we don't unpublish them + $seenIDs[] = $field->ID; + $field->doPublish('Stage', 'Live'); + $field->destroy(); + } - foreach($this->owner->Fields() as $field) { - $field->doPublish('Stage', 'Live'); - } + // fetch any orphaned live records + $live = Versioned::get_by_stage("EditableFormField", "Live") + ->filter(array( + 'ParentID' => $original->ID, + )); + + if (!empty($seenIDs)) { + $live = $live->exclude(array( + 'ID' => $seenIDs, + )); + } + + // delete orphaned records + foreach ($live as $field) { + $field->doDeleteFromStage('Live'); + $field->destroy(); + } } /** diff --git a/code/model/editableformfields/EditableFormField.php b/code/model/editableformfields/EditableFormField.php index 1ddcca0..13b1baf 100755 --- a/code/model/editableformfields/EditableFormField.php +++ b/code/model/editableformfields/EditableFormField.php @@ -86,7 +86,7 @@ class EditableFormField extends DataObject { "RightTitle" => "Varchar(255)", // from CustomSettings "ShowOnLoad" => "Boolean(1)", // from CustomSettings ); - + private static $defaults = array( 'ShowOnLoad' => true, ); @@ -371,8 +371,8 @@ class EditableFormField extends DataObject { if($parent && $parent->exists()) { return $parent->canEdit($member) && !$this->isReadonly(); } else if (!$this->exists() && Controller::has_curr()) { - // This is for GridFieldOrderableRows support as it checks edit permissions on - // singleton of the class. Allows editing of User Defined Form pages by + // This is for GridFieldOrderableRows support as it checks edit permissions on + // singleton of the class. Allows editing of User Defined Form pages by // 'Content Authors' and those with permission to edit the UDF page. (ie. CanEditType/EditorGroups) // This is to restore User Forms 2.x backwards compatibility. $controller = Controller::curr(); @@ -474,10 +474,26 @@ class EditableFormField extends DataObject { public function doPublish($fromStage, $toStage, $createNewVersion = false) { $this->publish($fromStage, $toStage, $createNewVersion); + $seenIDs = array(); + // Don't forget to publish the related custom rules... foreach ($this->DisplayRules() as $rule) { + $seenIDs[] = $rule->ID; $rule->doPublish($fromStage, $toStage, $createNewVersion); + $rule->destroy(); } + + // remove any orphans from the "fromStage" + $rules = Versioned::get_by_stage('EditableCustomRule', $toStage) + ->filter('ParentID', $this->ID); + + if (!empty($seenIDs)) { + $rules = $rules->exclude('ID', $seenIDs); + } + + foreach ($rules as $rule) { + $rule->deleteFromStage($toStage); + } } /**