Skip to content

Commit

Permalink
Merge pull request #63 from itmundi/sync-tag-groups
Browse files Browse the repository at this point in the history
Sync tag groups and fix to related field source
  • Loading branch information
bvangennep authored Jun 16, 2016
2 parents 314343a + 596cccb commit 05f115d
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 39 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ This project has been licensed under the MIT License (MIT). Please see [License

## Changelog

###3.4.0###
- Schematic now also exports and imports tag groups

###3.3.2###
- Allow 'singles' as a source

Expand Down
3 changes: 3 additions & 0 deletions src/Console/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ private function _setSchematicComponents()
'schematic_categoryGroups' => [
'class' => Service\CategoryGroups::class,
],
'schematic_tagGroups' => [
'class' => Service\TagGroups::class,
],
];

// Element index settings are supported from Craft 2.5
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @property array $elementIndexSettings
* @property array $pluginData
* @property array $categoryGroups
* @property array $tagGroups
*/
class Data extends Base
{
Expand All @@ -52,6 +53,7 @@ protected function defineAttributes()
'elementIndexSettings' => [AttributeType::Mixed, 'default' => []],
'pluginData' => [AttributeType::Mixed, 'default' => []],
'categoryGroups' => [AttributeType::Mixed, 'default' => []],
'tagGroups' => [AttributeType::Mixed, 'default' => []],
];
}

Expand Down
46 changes: 9 additions & 37 deletions src/Models/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,6 @@ protected function getFieldFactory()
return Craft::app()->schematic_fields->getFieldFactory();
}

/**
* @return SectionsService
*/
private function getSectionsService()
{
return Craft::app()->sections;
}

/**
* @return UserGroupsService
*/
private function getUserGroupsService()
{
return Craft::app()->userGroups;
}

/**
* @return AssetSources
*/
private function getAssetSourcesService()
{
return Craft::app()->schematic_assetSources;
}

/**
* @return CategoriesService
*/
private function getCategoriesService()
{
return Craft::app()->categories;
}

/**
* @param FieldModel $field
* @param $includeContext
Expand Down Expand Up @@ -161,7 +129,7 @@ private function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
*/
private function getSource($fieldType, $source, $indexFrom, $indexTo)
{
if ($source == 'singles') {
if ($source == 'singles' || $source == '*') {
return $source;
}

Expand All @@ -172,23 +140,27 @@ private function getSource($fieldType, $source, $indexFrom, $indexTo)
list($sourceType, $sourceFrom) = explode(':', $source);
switch ($sourceType) {
case 'section':
$service = $this->getSectionsService();
$service = Craft::app()->sections;
$method = 'getSectionBy';
break;
case 'group':
$service = $fieldType == 'Users' ? $this->getUserGroupsService() : $this->getCategoriesService();
$service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
$method = 'getGroupBy';
break;
case 'folder':
$service = $this->getAssetSourcesService();
$service = Craft::app()->assetSources;
$method = 'getSourceTypeBy';
break;
case 'taggroup':
$service = Craft::app()->tags;
$method = 'getTagGroupBy';
break;
}
} elseif ($source !== 'singles') {
//Backwards compatibility
$sourceType = 'section';
$sourceFrom = $source;
$service = $this->getSectionsService();
$service = Craft::app()->sections;
$method = 'getSectionBy';
}

Expand Down
4 changes: 4 additions & 0 deletions src/Services/Schematic.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private function importDataModel(Data $model, $force)
$globalSetsImportResult = Craft::app()->schematic_globalSets->import($model->getAttribute('globalSets'), $force);
$sectionImportResult = Craft::app()->schematic_sections->import($model->getAttribute('sections'), $force);
$categoryGroupImportResult = Craft::app()->schematic_categoryGroups->import($model->getAttribute('categoryGroups'), $force);
$tagGroupImportResult = Craft::app()->schematic_tagGroups->import($model->getAttribute('tagGroups'), $force);
$userGroupImportResult = Craft::app()->schematic_userGroups->import($model->getAttribute('userGroups'), $force);
$userImportResult = Craft::app()->schematic_users->import($model->getAttribute('users'), true);
$fieldImportResultFinal = Craft::app()->schematic_fields->import($model->getAttribute('fields'), $force);
Expand All @@ -119,6 +120,7 @@ private function importDataModel(Data $model, $force)
$result->consume($globalSetsImportResult);
$result->consume($sectionImportResult);
$result->consume($categoryGroupImportResult);
$result->consume($tagGroupImportResult);
$result->consume($userGroupImportResult);
$result->consume($userImportResult);
$result->consume($fieldImportResultFinal);
Expand Down Expand Up @@ -167,6 +169,7 @@ private function exportDataModel()
$globals = Craft::app()->globals->getAllSets();
$userGroups = Craft::app()->userGroups->getAllGroups();
$categoryGroups = Craft::app()->categories->getAllGroups();
$tagGroups = Craft::app()->tags->getAllTagGroups();

$export = [
'locales' => Craft::app()->schematic_locales->export(),
Expand All @@ -178,6 +181,7 @@ private function exportDataModel()
'userGroups' => Craft::app()->schematic_userGroups->export($userGroups),
'users' => Craft::app()->schematic_users->export(),
'categoryGroups' => Craft::app()->schematic_categoryGroups->export($categoryGroups),
'tagGroups' => Craft::app()->schematic_tagGroups->export($tagGroups),
];

// Element index settings are supported from Craft 2.5
Expand Down
112 changes: 112 additions & 0 deletions src/Services/TagGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace NerdsAndCompany\Schematic\Services;

use Craft\Craft;
use Craft\TagGroupModel;

/**
* Schematic TagGroups Service.
*
* Sync Craft Setups.
*
* @author Nerds & Company
* @copyright Copyright (c) 2015-2016, Nerds & Company
* @license MIT
*
* @link http://www.nerds.company
*/
class TagGroups extends Base
{
/**
* Export taggroups.
*
* @param TagGroupModel[] $tagGroups
*
* @return array
*/
public function export(array $tagGroups = [])
{
Craft::log(Craft::t('Exporting TagGroups'));

$tagGroupDefinitions = [];

foreach ($tagGroups as $tagGroup) {
$tagGroupDefinitions[$tagGroup->handle] = $this->getTagGroupDefinition($tagGroup);
}

return $tagGroupDefinitions;
}

/**
* Get tagGroup definition.
*
* @param TagGroupModel $tagGroup
*
* @return array
*/
private function getTagGroupDefinition(TagGroupModel $tagGroup)
{
return [
'name' => $tagGroup->name,
'fieldLayout' => Craft::app()->schematic_fields->getFieldLayoutDefinition($tagGroup->getFieldLayout()),
];
}

/**
* Attempt to import tagGroups.
*
* @param array $tagGroupDefinitions
* @param bool $force If set to true tagGroups not included in the import will be deleted
*
* @return Result
*/
public function import(array $tagGroupDefinitions, $force = false)
{
Craft::log(Craft::t('Importing TagGroups'));

$tagGroups = Craft::app()->tags->getAllTagGroups('handle');

foreach ($tagGroupDefinitions as $tagGroupHandle => $tagGroupDefinition) {
$tagGroup = array_key_exists($tagGroupHandle, $tagGroups)
? $tagGroups[$tagGroupHandle]
: new TagGroupModel();

unset($tagGroups[$tagGroupHandle]);

$this->populateTagGroup($tagGroup, $tagGroupDefinition, $tagGroupHandle);

if (!Craft::app()->tags->saveTagGroup($tagGroup)) { // Save taggroup via craft
$this->addErrors($tagGroup->getAllErrors());

continue;
}
}

if ($force) {
foreach ($tagGroups as $tagGroup) {
Craft::app()->tags->deleteTagGroupById($tagGroup->id);
}
}

return $this->getResultModel();
}

/**
* Populate taggroup.
*
* @param TagGroupModel $tagGroup
* @param array $tagGroupDefinition
* @param string $tagGroupHandle
*/
private function populateTagGroup(TagGroupModel $tagGroup, array $tagGroupDefinition, $tagGroupHandle)
{
$tagGroup->setAttributes([
'handle' => $tagGroupHandle,
'name' => $tagGroupDefinition['name'],
]);

$fieldLayout = Craft::app()->schematic_fields->getFieldLayout($tagGroupDefinition['fieldLayout']);
$tagGroup->setFieldLayout($fieldLayout);
}
}
23 changes: 21 additions & 2 deletions tests/Services/SchematicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace NerdsAndCompany\Schematic\Services;

use Craft\BaseTest;
use Craft\CategoriesService;
use Craft\Craft;
use Craft\BaseTest;
use Craft\FieldsService;
use Craft\GlobalsService;
use Craft\SectionsService;
use Craft\PluginsService;
use Craft\SectionsService;
use Craft\TagsService;
use Craft\UserGroupsService;
use NerdsAndCompany\Schematic\Models\Result;
use PHPUnit_Framework_MockObject_MockObject as Mock;
Expand Down Expand Up @@ -201,6 +202,20 @@ public function getMockCategoriesService()
return $mock;
}

/**
* @return Mock|TagsService
*/
public function getMockTagsService()
{
$mock = $this->getMockBuilder(TagsService::class)
->disableOriginalConstructor()
->getMock();

$mock->expects($this->exactly(1))->method('getAllTagGroups')->willReturn([]);

return $mock;
}

/**
* Mock all required services.
*/
Expand All @@ -215,6 +230,7 @@ private function mockServices()
$this->createMockService(UserGroups::class, 'schematic_userGroups');
$this->createMockService(Users::class, 'schematic_users');
$this->createMockService(CategoryGroups::class, 'schematic_categoryGroups');
$this->createMockService(TagGroups::class, 'schematic_tagGroups');
$this->createMockService(ElementIndexSettings::class, 'schematic_elementIndexSettings');

$mockPluginsService = $this->getMockPluginsService();
Expand Down Expand Up @@ -264,6 +280,9 @@ private function prepExportMockServices()

$mockCategoriesService = $this->getMockCategoriesService();
$this->setCraftComponent('categories', $mockCategoriesService);

$mockTagsService = $this->getMockTagsService();
$this->setCraftComponent('tags', $mockTagsService);
}

/**
Expand Down
Loading

0 comments on commit 05f115d

Please sign in to comment.