Skip to content

Commit

Permalink
Merge pull request #6 from muskie9/refactor/classNameUpdateToolSupport
Browse files Browse the repository at this point in the history
REFACTOR use class name update tool
  • Loading branch information
muskie9 authored Jun 14, 2018
2 parents c9bad5d + 117ee7a commit 2e6c54b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 41 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"require": {
"dnadesign/silverstripe-elemental": "^2@dev",
"sheadawson/silverstripe-blocks": "^2.0"
"sheadawson/silverstripe-blocks": "^2.0",
"dynamic/silverstripe-classname-update-tasks": "^1.0@dev"
},
"require-dev": {
"phpunit/PHPUnit": "^5.7",
Expand Down
130 changes: 90 additions & 40 deletions src/Tasks/BlocksToElementsTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

use DNADesign\Elemental\Models\ElementalArea;
use Dynamic\BlockMigration\Traits\BlockMigrationConfigurationTrait;
use Dynamic\ClassNameUpdate\BuildTasks\DatabaseClassNameUpdateTask;
use SheaDawson\Blocks\Model\Block;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Debug;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Queries\SQLSelect;

/**
Expand Down Expand Up @@ -37,18 +42,59 @@ class BlocksToElementsTask extends BuildTask
*/
private static $segment = 'MigrateBlocksTask';

/**
* @var
*/
private $block_class_mapping;

/**
* @var
*/
private $migration_mapping;

/**
* @param \SilverStripe\Control\HTTPRequest $request
*/
public function run($request)
{
foreach ($this->getBlockTypes() as $blockType => $configInformation) {
$this->updateBlocksClassName($configInformation);
$mappings = $this->getBlockClassMapping();
DatabaseClassNameUpdateTask::singleton()
->run(Controller::curr()->getRequest(), $mappings);

$this->processBlockRecords($configInformation['NewName']::get(), $configInformation['Element'], $configInformation['Relations']);
$migrationMapping = $this->config()->get('migration_mapping');

foreach ($migrationMapping as $block => $mapping) {
$this->processBlockRecords($block::get(), $mapping['Element'], $mapping['Relations']);
}
}

/**
* @return mixed
*/
public function getBlockClassMapping()
{
if (!$this->block_class_mapping) {
$this->setBlockClassMapping();
}

return $this->block_class_mapping;
}

/**
* @return $this
*/
public function setBlockClassMapping()
{
$this->block_class_mapping = $this->parseMapping('mappings');

return $this;
}

protected function parseMapping($key = '')
{
return $this->config()->get($key);
}

/**
* @param $configInformation
*/
Expand Down Expand Up @@ -94,38 +140,42 @@ protected function processBlockRecords($records, $elementType, $relations)
protected function generateBlockElement(SiteTree $page, Block $record, $elementType, $relations)
{
if ($record) {
$element = $record->newClassInstance($elementType);
foreach ($page->getElementalRelations() as $relation) {
$areaID = $relation . 'ID';
if (!$page->$areaID) {
$area = ElementalArea::create();
$area->OwnerClassName = $page->ClassName;
$area->write();
$page->$areaID = $area->ID;
$page->write();
} elseif ($area = ElementalArea::get()->filter('ID', $page->$areaID)->first()) {
$area->write();
}
}
$area = $page->ElementalArea();
$element->ParentID = $area->ID;
$element->write();

static::write_it("Element of type {$elementType::singleton()->getType()} created with Title: \"{$element->Title}\".", false);

foreach ($relations as $relationType => $relationConfiguration) {
foreach ($relationConfiguration as $relation) {
switch ($relationType) {
case 'ManyMany':
case 'HasMany':
$this->processManyRelation($element, $record, $relation);
break;
case 'HasOne':
$this->processOneRelation($element, $record, $relation);
break;
if (array_search(DataObject::class, ClassInfo::ancestry($elementType))) {
$element = $record->newClassInstance($elementType);
foreach ($page->getElementalRelations() as $relation) {
$areaID = $relation . 'ID';
if (!$page->$areaID) {
$area = ElementalArea::create();
$area->OwnerClassName = $page->ClassName;
$area->write();
$page->$areaID = $area->ID;
$page->write();
} elseif ($area = ElementalArea::get()->filter('ID', $page->$areaID)->first()) {
$area->write();
}
}
$area = $page->ElementalArea();
$element->ParentID = $area->ID;
$element->write();

static::write_it("Element of type {$elementType::singleton()->getType()} created with Title: \"{$element->Title}\".", false);

foreach ($relations as $relationType => $relationConfiguration) {
foreach ($relationConfiguration as $relation) {
switch ($relationType) {
case 'ManyMany':
case 'HasMany':
$this->processManyRelation($element, $record, $relations['HasMany']);
break;
case 'HasOne':
$this->processOneRelation($element, $record, $relations['HasOne']);
break;
}

}
}
} else {
static::write_it("Couldn't migrate {$record->Title} to an Element of type {$elementType}. Please make sure you have the proper mapping and module installed to support that element type", false);
}
}
}
Expand All @@ -152,19 +202,19 @@ protected function processManyRelation($element, $legacyRecord, $relation = [])
$this->migrateObsoleteData($relation);
}

$newRelationName = $relation['NewRelationName'];
$newRelationName = $relation['ElementRelationName'];
$newRelationList = $element->$newRelationName();

$legacyRelationName = $relation['LegacyRelationName'];
$legacyRelationName = $relation['BlockRelationName'];
$legacyRelationList = $legacyRecord->$legacyRelationName();

foreach ($legacyRelationList as $legacyObject) {
if (!$object = $relation['NewObject']::get()->filter('ID', $legacyObject->ID)->first()) {
$object = $legacyObject->newClassInstance($relation['NewObject']);
if (!$object = $relation['ElementRelationObject']::get()->filter('ID', $legacyObject->ID)->first()) {
$object = $legacyObject->newClassInstance($relation['ElementRelationObject']);
$object->write();
}
$newRelationList->add($object);
static::write_it("New {$relation['NewObject']::singleton()->singular_name()} created with the Title: \"{$object->Title}\" and linked to Element with the Title: \"{$element->Title}\".");
static::write_it("New {$relation['ElementRelationObject']::singleton()->singular_name()} created with the Title: \"{$object->Title}\" and linked to Element with the Title: \"{$element->Title}\".");
}

}
Expand All @@ -186,18 +236,18 @@ protected function processOneRelation($element, $legacyRecord, $relation = [])
*/
protected function migrateObsoleteData($relation)
{
$table = "_obsolete_{$relation['LegacyObjectClassName']}";
$table = "_obsolete_{$relation['BlockLegacyRelationObjectClassName']}";

$query = new SQLSelect();
$query->setFrom("\"{$table}\"");
$results = $query->execute();

foreach ($results as $result) {
$newObject = Injector::inst()->create($relation['LegacyObject']);
$newObject = Injector::inst()->create($relation['BlockRelatedObject']);
foreach ($result as $key => $val) {
$newObject->$key = $val;
}
$newObject->ClassName = $relation['LegacyObject'];
$newObject->ClassName = $relation['BlockRelatedObject'];
$newObject->write();
}
}
Expand Down

0 comments on commit 2e6c54b

Please sign in to comment.