From e3c96e643128877f7c2eea79cf57444b2acc82b8 Mon Sep 17 00:00:00 2001 From: Algeron Waterschoot Date: Wed, 1 Oct 2014 20:44:35 +0200 Subject: [PATCH] Issue #8: implement feedback from klaasvw@Codelab42*CrossCheck: "use the same method as the node migration for importing PHP code instead of defining an intermediary XML format" --- .../baseline_content.panelizer.default.inc | 5 +- .../baseline_content.panelizer.entity.inc | 5 +- plugins/panelizer.default.inc | 141 +++++++----------- plugins/panelizer.entity.inc | 105 +++++-------- 4 files changed, 98 insertions(+), 158 deletions(-) diff --git a/migrations/baseline_content.panelizer.default.inc b/migrations/baseline_content.panelizer.default.inc index 065221d..0f88a4f 100644 --- a/migrations/baseline_content.panelizer.default.inc +++ b/migrations/baseline_content.panelizer.default.inc @@ -26,6 +26,8 @@ abstract class BaselineContentPanelizerDefaultMigration extends XMLMigration { $fields = $this->getSourceFields(); $this->source = new MigrateSourceXML($file, '/panelizer/panelize', 'description', $fields); $this->destination = new MigrateDestinationPanelizerDefault(); + + $this->addFieldMapping('panelizer_defaults_export', 'panelizer_defaults_export')->xpath('panelizer_defaults_export'); } /** @@ -34,8 +36,7 @@ abstract class BaselineContentPanelizerDefaultMigration extends XMLMigration { protected function getSourceFields() { return array( 'description' => t('Description'), - 'panel_settings' => t('Panel settings'), - 'panelizer_settings' => t('Panelizer settings'), + 'panelizer_defaults_export' => t('Panelizer Defaults Export'), ); } } diff --git a/migrations/baseline_content.panelizer.entity.inc b/migrations/baseline_content.panelizer.entity.inc index 371bf5f..12e3078 100644 --- a/migrations/baseline_content.panelizer.entity.inc +++ b/migrations/baseline_content.panelizer.entity.inc @@ -26,6 +26,8 @@ abstract class BaselineContentPanelizerEntityMigration extends XMLMigration { $fields = $this->getSourceFields(); $this->source = new MigrateSourceXML($file, '/panelizer/panelize', 'description', $fields); $this->destination = new MigrateDestinationPanelizerEntity(); + + $this->addFieldMapping('panelizer_entity_export', 'panelizer_entity_export')->xpath('panelizer_entity_export'); } /** @@ -35,8 +37,7 @@ abstract class BaselineContentPanelizerEntityMigration extends XMLMigration { return array( 'description' => t('Description'), 'entity_title' => t('Entity title'), - 'panel_settings' => t('Panel settings'), - 'panelizer_settings' => t('Panelizer settings'), + 'panelizer_entity_export' => t('Panelizer Entity Export'), ); } } diff --git a/plugins/panelizer.default.inc b/plugins/panelizer.default.inc index fdb90d4..cc733dd 100755 --- a/plugins/panelizer.default.inc +++ b/plugins/panelizer.default.inc @@ -26,107 +26,57 @@ class MigrateDestinationPanelizerDefault extends MigrateDestination { /** * Delete Panelizer Default settings. */ - public function rollback(array $panel_ids) { - foreach ($panel_ids as $panel_id) { + public function rollback(array $panelizer_names) { + foreach ($panelizer_names as $panelizer_name) { // Unset any variables that may have been set during import. - $variable_string = variable_get('baseline_content_variables_for_panel_' . $panel_id, ''); - if (!empty($variable_string)) { - $variables = explode(',', $variable_string); - foreach ($variables as $variable) { - variable_del($variable); - } - variable_del('baseline_content_variables_for_panel_' . $panel_id); - } - db_delete('panelizer_defaults') - ->condition('did', $panel_id) - ->execute(); - db_delete('panels_pane') - ->condition('did', $panel_id) - ->execute(); - db_delete('panels_display') - ->condition('did', $panel_id) - ->execute(); + // panelizer_defaults_node_article + $variable_key_parts = explode(":", $panelizer_name); + $variable_key = 'panelizer_defaults_' . $variable_key_parts[0] . '_' . $variable_key_parts[1]; + variable_del($variable_key); + $this->delete_panelizer_defaults($panelizer_name); } } /** - * Import a single panel. + * Import a single panelizer default configuration. */ public function import(stdClass $object, stdClass $row) { - // Updates aren't entirely supported yet, so rollback any previous - // migrations first. - if (!empty($row->migrate_map_destid1)) { - $this->rollback(array($row->migrate_map_destid1)); - } - $panelizer_description = '' . $row->xml->description; - - $migration = Migration::currentMigration(); - - $this->prepare($object, $row); - - $fields = array( - 'panels_display' => array(), - 'panels_pane' => array(), - 'panelizer_defaults' => array(), - ); - // Insert the panel display. - $panels_display_columns = (array) $row->xml->panel_settings->panels_display; - foreach ($panels_display_columns as $column_name => $value) { - $fields['panels_display'][$column_name] = $value; - } - $did = db_insert('panels_display') - ->fields($fields['panels_display']) - ->execute(); - - // This gives us a panel ID which we need for the panel panes. - $panes = (array) $row->xml->panel_settings->panels_pane; - foreach ($panes['pane'] as $pane) { - $id = count($fields['panels_pane']); - $fields['panels_pane'][$id] = array(); - $pane_fields = (array) $pane; - foreach ($pane as $column_name => $value) { - $fields['panels_pane'][$id][$column_name] = $value; - } - $fields['panels_pane'][$id]['did'] = $did; - db_insert('panels_pane') - ->fields($fields['panels_pane'][$id]) - ->execute(); - } - // The panel ID also lets us insert the Panelizer defaults. - $panelizer_defaults_columns = (array) $row->xml->panelizer_settings->panelizer_defaults; - foreach ($panelizer_defaults_columns as $column_name => $value) { - $fields['panelizer_defaults'][$column_name] = $value; - } - $fields['panelizer_defaults']['did'] = $did; - db_insert('panelizer_defaults') - ->fields($fields['panelizer_defaults']) - ->execute(); - - // Set the panel's default settings. - if (!empty($row->xml->variables)) { - foreach ($row->xml->variables as $variables) { - $array = (array) $variables; - foreach ($array as $name => $variable) { - $variable = unserialize($variable); - variable_set($name, $variable); - - // Set a variable to point to the imported panel's variables. This - // is needed to unset these variables upon rollback. - // TODO: refactor so an extra variable isn't needed. - $old_variable = variable_get('baseline_content_variables_for_panel_' . $did, ''); - if (!empty($old_variable)) { - $old_variable = $old_variable . ','; + $panelizer_name = '' . $row->xml->description; + + // Get the exported panelizer and append it with "return $page" to assign it to a variable. + $panelizer_code = $row->panelizer_defaults_export . ' return $panelizer;'; + // Remove newlines. + // TODO: do not remove newlines in custom textfields. + $panelizer_code = trim(preg_replace('/\s\s+/', ' ', $panelizer_code)); + // Execute the exported panel as PHP code, giving us an object that represents the panel. + $panelizer = eval($panelizer_code); + + $this->prepare($panelizer, $row); + // If the page already exists, delete it. + $panelizer_deleted = $this->delete_panelizer_defaults($panelizer_name); + if (!empty($panelizer)) { + // save the updated page object into DB. + panelizer_export_save_callback($panelizer); + // TODO: figure out why this variable isn't exported through the export UI, and see if there's a way + // to remove it from the import file. + if (!empty($row->xml->variables)) { + foreach ($row->xml->variables as $variables) { + $array = (array) $variables; + foreach ($array as $name => $variable) { + $variable = unserialize($variable); + variable_set($name, $variable); } - $new_variable = $old_variable . $name; - variable_set('baseline_content_variables_for_panel_' . $did, $new_variable); } } + if (!$panelizer_deleted) { + $this->numCreated++; + } + else { + $this->numUpdated++; + } } - - $this->numCreated++; - - return array($did); + return array($panelizer_name); } public function prepare(stdClass $object, stdClass $row) { @@ -153,4 +103,17 @@ class MigrateDestinationPanelizerDefault extends MigrateDestination { public function __toString() { return 'MigrateDestinationPanelizerDefault'; } + + /** + * @param $panelizer_name + * @return boolean Whether or not the delete was successful. + */ + private function delete_panelizer_defaults($panelizer_name) { + $existing_panelizer = panelizer_context_cache_get('default', $panelizer_name); + if (!empty($existing_panelizer->pnid)) { + panelizer_export_delete_callback($existing_panelizer); + return TRUE; + } + return FALSE; + } } diff --git a/plugins/panelizer.entity.inc b/plugins/panelizer.entity.inc index 3a5a88e..0b295a0 100755 --- a/plugins/panelizer.entity.inc +++ b/plugins/panelizer.entity.inc @@ -14,14 +14,13 @@ class MigrateDestinationPanelizerEntity extends MigrateDestination { return array( 'description' => t('Description'), 'entity_title' => t('Entity title'), - 'panel_settings' => t('Panel settings'), - 'panelizer_settings' => t('Panelizer settings'), + 'panelizer_entity_export' => t('Panelizer Entity Export'), ); } static public function getKeySchema() { return array( - 'description' => array( + 'entity_title' => array( 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, @@ -32,81 +31,57 @@ class MigrateDestinationPanelizerEntity extends MigrateDestination { /** * Delete Panelizer settings for an entity. */ - public function rollback(array $panel_ids) { - foreach ($panel_ids as $panel_id) { - db_delete('panelizer_entity') - ->condition('did', $panel_id) - ->execute(); - db_delete('panels_pane') - ->condition('did', $panel_id) - ->execute(); - db_delete('panels_display') - ->condition('did', $panel_id) - ->execute(); + public function rollback(array $node_ids) { + foreach ($node_ids as $node_id) { + $panelizer_deleted = $this->delete_panelizer_entity(node_load($node_id)); } } /** - * Import a single panelized entity. + * Import a single panelizer entity configuration. */ public function import(stdClass $object, stdClass $row) { $node_id = $object->entity_title; $node = node_load($node_id); - $revision_id = $node->vid; - // Updates aren't entirely supported yet, so rollback any previous - // migrations first. - if (!empty($row->migrate_map_destid1)) { - $this->rollback(array($row->migrate_map_destid1)); - } - $panelizer_description = '' . $row->xml->description; - - $migration = Migration::currentMigration(); + $panelizer_name = '' . $row->xml->description; - $this->prepare($object, $row); + // Get the exported panelizer and append it with "return $page" to assign it to a variable. + $panelizer_code = $row->panelizer_entity_export . ' return $panelizer;'; + // Remove newlines. + // TODO: do not remove newlines in custom textfields. + $panelizer_code = trim(preg_replace('/\s\s+/', ' ', $panelizer_code)); + // Execute the exported panel as PHP code, giving us an object that represents the panel. + $panelizer = eval($panelizer_code); - $fields = array( - 'panels_display' => array(), - 'panels_pane' => array(), - 'panelizer_entity' => array(), - ); - // Insert the panel display. - $panels_display_columns = (array) $row->xml->panel_settings->panels_display; - foreach ($panels_display_columns as $column_name => $value) { - $fields['panels_display'][$column_name] = $value; - } - $did = db_insert('panels_display') - ->fields($fields['panels_display']) - ->execute(); - - // This gives us a panel ID which we need for the panel panes. - $panes = (array) $row->xml->panel_settings->panels_pane; - foreach ($panes['pane'] as $pane) { - $id = count($fields['panels_pane']); - $fields['panels_pane'][$id] = array(); - $pane_fields = (array) $pane; - foreach ($pane as $column_name => $value) { - $fields['panels_pane'][$id][$column_name] = $value; + $this->prepare($panelizer, $row); + // If the page already exists, delete it. + $panelizer_deleted = $this->delete_panelizer_entity($node); + if (!empty($panelizer)) { + // Name needs to be unset, else $panelizer->did will be 0, and the panel panes will not get created correctly. + // Don't ask why. + unset($panelizer->name); + $node->panelizer = $panelizer; + panelizer_entity_insert($node, 'node'); + if (!$panelizer_deleted) { + $this->numCreated++; + } + else { + $this->numUpdated++; } - $fields['panels_pane'][$id]['did'] = $did; - db_insert('panels_pane') - ->fields($fields['panels_pane'][$id]) - ->execute(); - } - // The panel ID also lets us insert the Panelizer entity. - $panelizer_entity_columns = (array) $row->xml->panelizer_settings->panelizer_entity; - foreach ($panelizer_entity_columns as $column_name => $value) { - $fields['panelizer_entity'][$column_name] = $value; } - $fields['panelizer_entity']['did'] = $did; - $fields['panelizer_entity']['entity_id'] = $node_id; - $fields['panelizer_entity']['revision_id'] = $revision_id; - db_insert('panelizer_entity') - ->fields($fields['panelizer_entity']) - ->execute(); - - $this->numCreated++; + return array($node_id); + } - return array($did); + /** + * @param $panelizer_name + * @return boolean Whether or not the delete was successful. + */ + private function delete_panelizer_entity($node) { + if (empty($node->panelizer['page_manager']->name) && !empty($node->panelizer['page_manager']->display)) { + panelizer_entity_delete($node, 'node'); + return TRUE; + } + return FALSE; } public function prepare(stdClass $object, stdClass $row) {