diff --git a/README.md b/README.md index 2b727eb..2e4f27e 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,12 @@ Thanks go to [@brandonkelly](https://github.com/brandonkelly) and [@benparizek]( ## Changelog +#### 0.3.5 + +- Change to content table naming when inside Matrix field. When two Super Table fields in different Matrix fields had the same handle, when one ST field was deleted, content for both would be deleted. Now prefixes tables with Matrix field id - ie: `supertablecontent_matrixId_fieldhandle`. +- Fix for some UI elements not initializing for Matrix > Super Table > Matrix layout [#28](https://github.com/engram-design/SuperTable/issues/28). + + #### 0.3.4 - Minor visual fix for Row layout and table overflow [#24](https://github.com/engram-design/SuperTable/issues/24). diff --git a/supertable/SuperTablePlugin.php b/supertable/SuperTablePlugin.php index b783f96..3d60cfd 100755 --- a/supertable/SuperTablePlugin.php +++ b/supertable/SuperTablePlugin.php @@ -14,7 +14,7 @@ public function getName() public function getVersion() { - return '0.3.4'; + return '0.3.5'; } public function getDeveloper() diff --git a/supertable/migrations/m150901_144609_superTable_fixForContentTables.php b/supertable/migrations/m150901_144609_superTable_fixForContentTables.php new file mode 100644 index 0000000..1ce060b --- /dev/null +++ b/supertable/migrations/m150901_144609_superTable_fixForContentTables.php @@ -0,0 +1,83 @@ +fields->getAllFields(); + $superTableFields = array(); + + foreach ($fields as $field) { + if ($field->type == 'Matrix') { + $blockTypes = craft()->matrix->getBlockTypesByFieldId($field->id); + + foreach ($blockTypes as $blockType) { + foreach ($blockType->getFields() as $blockTypeField) { + if ($blockTypeField->type == 'SuperTable') { + $superTableFields[] = array( + 'field' => $blockTypeField, + 'parentFieldId' => $blockType->id, + ); + } + } + } + } + } + + // Now, we need to create new tables which incorporate the Matrix field this Super Table is sitting inside. + // This will mean the supertablecontent_fieldhandle tables will now include the Matrix field id. + // + // So, we need to duplicate each table structure and content into new tables. We aren't removing the old tables + // so as not to be destructive, and potentially loose content somwehere along the way. + foreach ($superTableFields as $options) { + $field = $options['field']; + $parentFieldId = $options['parentFieldId']; + + // The latest code will actually mean this points to the new table already! + $newContentTable = craft()->superTable->getContentTableName($field); + + if (!craft()->db->tableExists($newContentTable)) { + $oldContentTable = str_replace('_'.$parentFieldId, '', $newContentTable); + + // Grab all existing data from old table + $tableData = craft()->db->createCommand() + ->select('*') + ->from($oldContentTable) + ->queryAll(); + + // Get the table creation raw SQL + $tableSchema = craft()->db->createCommand('SHOW CREATE TABLE craft_' . $oldContentTable)->queryRow(); + $newTableSql = $tableSchema['Create Table']; + + // Create the new table + $newTableSql = str_replace($oldContentTable, $newContentTable, $newTableSql); + craft()->db->createCommand($newTableSql)->execute(); + + // Copy the existing data into newly created table + if ($tableData) { + $columns = array_keys($tableData[0]); + $rows = array(); + + foreach ($tableData as $key => $row) { + $rows[] = array_values($row); + } + + foreach ($columns as $key => $column) { + + // Craft does these fields for us. + if ($column == 'dateCreated' || $column == 'dateUpdated' || $column == 'uid') { + unset($columns[$key]); + } + } + + // In the new content goes! + craft()->db->createCommand()->insertAll($newContentTable, $columns, $rows); + } + } + } + + return true; + } +} diff --git a/supertable/resources/js/MatrixConfiguratorAlt.js b/supertable/resources/js/MatrixConfiguratorAlt.js index f88c83c..9d50de1 100644 --- a/supertable/resources/js/MatrixConfiguratorAlt.js +++ b/supertable/resources/js/MatrixConfiguratorAlt.js @@ -516,7 +516,7 @@ var BlockType = Garnish.Base.extend( }); -Field = Garnish.Base.extend( +var Field = Garnish.Base.extend( { configurator: null, blockType: null, @@ -580,6 +580,8 @@ Field = Garnish.Base.extend( { this.selectedFieldType = this.$typeSelect.val(); this.initializedFieldTypeSettings[this.selectedFieldType] = this.$typeSettingsContainer.children(); + + this.setFieldType(this.selectedFieldType); } if (!this.$handleInput.val()) @@ -670,12 +672,13 @@ Field = Garnish.Base.extend( var firstTime = (typeof this.initializedFieldTypeSettings[type] == 'undefined'); + var info = this.configurator.getFieldTypeInfo(type), + bodyHtml = this.getParsedFieldTypeHtml(info.settingsBodyHtml), + footHtml = this.getParsedFieldTypeHtml(info.settingsFootHtml); + if (firstTime) { - var info = this.configurator.getFieldTypeInfo(type), - bodyHtml = this.getParsedFieldTypeHtml(info.settingsBodyHtml), - footHtml = this.getParsedFieldTypeHtml(info.settingsFootHtml), - $body = $('
'+bodyHtml+'
'); + var $body = $('
'+bodyHtml+'
'); this.initializedFieldTypeSettings[type] = $body; } @@ -686,11 +689,8 @@ Field = Garnish.Base.extend( $body.appendTo(this.$typeSettingsContainer); - if (firstTime) - { - Craft.initUiElements($body); - Garnish.$bod.append(footHtml); - } + Craft.initUiElements($body); + Garnish.$bod.append(footHtml); }, getParsedFieldTypeHtml: function(html) diff --git a/supertable/resources/js/SuperTableConfigurator.js b/supertable/resources/js/SuperTableConfigurator.js index e57f06e..e5a436f 100755 --- a/supertable/resources/js/SuperTableConfigurator.js +++ b/supertable/resources/js/SuperTableConfigurator.js @@ -274,13 +274,23 @@ Craft.SuperTableSettingsModal = Garnish.Modal.extend({ this.$fieldSettings = this.$settingsContainer.appendTo($main); - Craft.initUiElements(this.$fieldSettings); - Garnish.$bod.append(this.fieldTypeFootHtml); + // Give the modal window some time to get it together + setTimeout($.proxy(function() { + Craft.initUiElements(this.$fieldSettings); + Garnish.$bod.append(this.fieldTypeFootHtml); + }, this), 1); this.addListener(this.$closeBtn, 'activate', 'closeModal'); }, restoreSettingsToTable: function() { + + // Special case for Matrix - reset field back to defaults, otherwise causes UI havok + this.$fieldSettings.find('.matrixconfigitem.sel').removeClass('sel'); + this.$fieldSettings.find('.mc-sidebar.fields .col-inner-container').addClass('hidden'); + this.$fieldSettings.find('.field-settings .col-inner-container').addClass('hidden'); + this.$fieldSettings.find('.field-settings .col-inner-container .items div[data-id]').addClass('hidden'); + this.field.restoreSettingsHtml(this.$fieldSettings); }, diff --git a/supertable/services/SuperTableService.php b/supertable/services/SuperTableService.php index 41049d9..b9ed782 100755 --- a/supertable/services/SuperTableService.php +++ b/supertable/services/SuperTableService.php @@ -417,6 +417,7 @@ public function deleteSuperTableField(FieldModel $superTableField) public function getContentTableName(FieldModel $superTableField, $useOldHandle = false) { $name = ''; + $parentFieldId = ''; do { if ($useOldHandle) { @@ -429,10 +430,23 @@ public function getContentTableName(FieldModel $superTableField, $useOldHandle = $handle = $superTableField->handle; } + // Check if this field is inside a Matrix - we need to prefix this content table if so. + if ($superTableField->context != 'global') { + $parentFieldContext = explode(':', $superTableField->context); + + if ($parentFieldContext[0] == 'matrixBlockType') { + $parentFieldId = $parentFieldContext[1]; + } + } + $name = '_'.StringHelper::toLowerCase($handle).$name; } while ($superTableField = $this->getParentSuperTableField($superTableField)); + if ($parentFieldId) { + $name = '_'.$parentFieldId.$name; + } + return 'supertablecontent'.$name; }