Skip to content

Commit

Permalink
Merge pull request #93 from nerds-and-company/element-index-sources
Browse files Browse the repository at this point in the history
Element index sources
  • Loading branch information
bvangennep authored Apr 14, 2017
2 parents ea1049b + 7557073 commit be7ccc3
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 101 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
###3.7.2###
- Use handles for element index settings

###3.7.1###
- Removed Asset Transform dimensionChangeTime attribute from schema

Expand Down
5 changes: 4 additions & 1 deletion src/Console/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function init()
if ($appId = $this->config->get('appId')) {
$this->setId($appId);
}

// Initialize Cache and LogRouter right away (order is important)
$this->getComponent('cache');
$this->getComponent('log');
Expand Down Expand Up @@ -340,6 +340,9 @@ private function _setSchematicComponents()
'schematic_tagGroups' => [
'class' => Service\TagGroups::class,
],
'schematic_sources' => [
'class' => Service\Sources::class,
],
];

// Element index settings are supported from Craft 2.5
Expand Down
92 changes: 4 additions & 88 deletions src/Models/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function getDefinition(FieldModel $field, $includeContext)
}

if (isset($definition['settings']['sources'])) {
$definition['settings']['sources'] = $this->getMappedSources($field->type, $definition['settings']['sources'], 'id', 'handle');
$definition['settings']['sources'] = Craft::app()->schematic_sources->getMappedSources($field->type, $definition['settings']['sources'], 'id', 'handle');
}

if (isset($definition['settings']['source'])) {
$definition['settings']['source'] = $this->getSource($field->type, $definition['settings']['source'], 'id', 'handle');
$definition['settings']['source'] = Craft::app()->schematic_sources->getSource($field->type, $definition['settings']['source'], 'id', 'handle');
}

return $definition;
Expand Down Expand Up @@ -82,98 +82,14 @@ public function populate(array $fieldDefinition, FieldModel $field, $fieldHandle

if (isset($fieldDefinition['settings']['sources'])) {
$settings = $fieldDefinition['settings'];
$settings['sources'] = $this->getMappedSources($field->type, $settings['sources'], 'handle', 'id');
$settings['sources'] = Craft::app()->schematic_sources->getMappedSources($field->type, $settings['sources'], 'handle', 'id');
$field->settings = $settings;
}

if (isset($fieldDefinition['settings']['source'])) {
$settings = $fieldDefinition['settings'];
$settings['source'] = $this->getSource($field->type, $settings['source'], 'handle', 'id');
$settings['source'] = Craft::app()->schematic_sources->getSource($field->type, $settings['source'], 'handle', 'id');
$field->settings = $settings;
}
}

/**
* Get sources based on the indexFrom attribute and return them with the indexTo attribute.
*
* @param string $fieldType
* @param string|array $sources
* @param string $indexFrom
* @param string $indexTo
*
* @return array|string
*/
private function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
{
$mappedSources = $sources;
if (is_array($sources)) {
$mappedSources = [];
foreach ($sources as $source) {
$mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
}
}

return $mappedSources;
}

/**
* Gets a source by the attribute indexFrom, and returns it with attribute $indexTo.
*
* @TODO Break up and simplify this method
*
* @param string $fieldType
* @param string $source
* @param string $indexFrom
* @param string $indexTo
*
* @return string
*/
private function getSource($fieldType, $source, $indexFrom, $indexTo)
{
if ($source == 'singles' || $source == '*') {
return $source;
}

/** @var BaseElementModel $sourceObject */
$sourceObject = null;

if (strpos($source, ':') > -1) {
list($sourceType, $sourceFrom) = explode(':', $source);
switch ($sourceType) {
case 'section':
$service = Craft::app()->sections;
$method = 'getSectionBy';
break;
case 'group':
$service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
$method = 'getGroupBy';
break;
case 'folder':
$service = Craft::app()->schematic_assetSources;
$method = 'getSourceBy';
break;
case 'taggroup':
$service = Craft::app()->tags;
$method = 'getTagGroupBy';
break;
}
} elseif ($source !== 'singles') {
//Backwards compatibility
$sourceType = 'section';
$sourceFrom = $source;
$service = Craft::app()->sections;
$method = 'getSectionBy';
}

if (isset($service) && isset($method) && isset($sourceFrom)) {
$method = $method.$indexFrom;
$sourceObject = $service->$method($sourceFrom);
}

if ($sourceObject && isset($sourceType)) {
return $sourceType.':'.$sourceObject->$indexTo;
}

return '';
}
}
30 changes: 28 additions & 2 deletions src/Services/ElementIndexSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function import(array $settingDefinitions, $force = false)
Craft::log(Craft::t('Importing Element Index Settings'));

foreach ($settingDefinitions as $elementType => $settings) {
if (!$this->getElementIndexesService()->saveSettings($elementType, $settings)) {
$mappedSettings = $this->getMappedSettings($settings, 'handle', 'id');
if (!$this->getElementIndexesService()->saveSettings($elementType, $mappedSettings)) {
$this->addError(Craft::t('Element Index Settings for {elementType} could not be installed', ['elementType' => $elementType]));
}
}
Expand Down Expand Up @@ -79,10 +80,35 @@ public function export(array $data = [])
if (is_array($settings)) {

// Group by element type and add to definitions
$settingDefinitions[$elementTypeName] = $settings;
$mappedSettings = $this->getMappedSettings($settings, 'id', 'handle');
$settingDefinitions[$elementTypeName] = $mappedSettings;
}
}

return $settingDefinitions;
}

/**
* Get mapped element index settings, converting source ids to handles or back again
*
* @param array $settings
* @param string $fromIndex
* @param string $toIndex
*
* @return array
*/
private function getMappedSettings(array $settings, $fromIndex, $toIndex)
{
$mappedSettings = ['sources' => []];
foreach ($settings['sources'] as $source => $sourceSettings) {
$mappedSource = Craft::app()->schematic_sources->getSource(false, $source, $fromIndex, $toIndex);
$tableAttributesSettings = [];
foreach ($sourceSettings['tableAttributes'] as $index => $columnSource) {
$mappedColumnSource = Craft::app()->schematic_sources->getSource(false, $columnSource, $fromIndex, $toIndex);
$tableAttributesSettings[$index] = $mappedColumnSource;
}
$mappedSettings['sources'][$mappedSource] = ['tableAttributes' => $tableAttributesSettings];
}
return $mappedSettings;
}
}
108 changes: 108 additions & 0 deletions src/Services/Sources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace NerdsAndCompany\Schematic\Services;

use Craft\Craft;
use Craft\BaseApplicationComponent as BaseApplication;

/**
* Schematic Sources Service.
*
* Sync Craft Setups.
*
* @author Nerds & Company
* @copyright Copyright (c) 2015-2017, Nerds & Company
* @license MIT
*
* @link http://www.nerds.company
*/
class Sources extends BaseApplication
{
/**
* Get sources based on the indexFrom attribute and return them with the indexTo attribute.
*
* @param string $fieldType
* @param string|array $sources
* @param string $indexFrom
* @param string $indexTo
*
* @return array|string
*/
public function getMappedSources($fieldType, $sources, $indexFrom, $indexTo)
{
$mappedSources = $sources;
if (is_array($sources)) {
$mappedSources = [];
foreach ($sources as $source) {
$mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo);
}
}

return $mappedSources;
}

/**
* Gets a source by the attribute indexFrom, and returns it with attribute $indexTo.
*
* @TODO Break up and simplify this method
*
* @param string $fieldType
* @param string $source
* @param string $indexFrom
* @param string $indexTo
*
* @return string
*/
public function getSource($fieldType, $source, $indexFrom, $indexTo)
{
if ($source == 'singles' || $source == '*') {
return $source;
}

/** @var BaseElementModel $sourceObject */
$sourceObject = null;

if (strpos($source, ':') > -1) {
list($sourceType, $sourceFrom) = explode(':', $source);
switch ($sourceType) {
case 'section':
$service = Craft::app()->sections;
$method = 'getSectionBy';
break;
case 'group':
$service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories;
$method = 'getGroupBy';
break;
case 'folder':
$service = Craft::app()->schematic_assetSources;
$method = 'getSourceBy';
break;
case 'taggroup':
$service = Craft::app()->tags;
$method = 'getTagGroupBy';
break;
case 'field':
$service = Craft::app()->fields;
$method = 'getFieldBy';
break;
}
} elseif ($source !== 'singles') {
//Backwards compatibility
$sourceType = 'section';
$sourceFrom = $source;
$service = Craft::app()->sections;
$method = 'getSectionBy';
}

if (isset($service) && isset($method) && isset($sourceFrom)) {
$method = $method.$indexFrom;
$sourceObject = $service->$method($sourceFrom);
}

if ($sourceObject && isset($sourceType)) {
return $sourceType.':'.$sourceObject->$indexTo;
}

return $source;
}
}
Loading

0 comments on commit be7ccc3

Please sign in to comment.