Skip to content

Commit

Permalink
Merge pull request #18 from itmundi/fields-service-refactor
Browse files Browse the repository at this point in the history
Fields service refactor
  • Loading branch information
bvangennep committed Nov 3, 2015
2 parents f4d4441 + 24a0672 commit aa23d6a
Show file tree
Hide file tree
Showing 16 changed files with 828 additions and 338 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,24 @@ public function registerMigrationService()
}
```

* Has a hook to add mappings for custom field types, the Plugin_CustomSchematicFieldModel must extend the Schematic_FieldModel

```php
public function registerSchematicFieldModels()
{
return array(
'fieldType' => Plugin_CustomSchematicFieldModel
);
}
```

## Changelog

###1.4.0###
- Reworked importing and exporting of fields
- Added hook to allow the addition of custom logic for importing and exporting fields
- Permissions are now sorted

###1.3.0###
- Added the ability to use an override file

Expand Down
2 changes: 1 addition & 1 deletion SchematicPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getName()
*/
public function getVersion()
{
return '1.3.0';
return '1.4.0';
}

/**
Expand Down
56 changes: 56 additions & 0 deletions models/Schematic_FieldFactoryModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Craft;

/**
* Schematic Field Factory Model.
*
* Provides a schematic field model for mapping data
*
* @author Nerds & Company
* @copyright Copyright (c) 2015, Nerds & Company
* @license MIT
*
* @link http://www.nerds.company
*/
class Schematic_FieldFactoryModel
{
/**
* @return PluginsService
*/
private function getPluginsService()
{
return craft()->plugins;
}

/**
* @param $fieldType
* @return Schematic_FieldModel
*/
public function build($fieldType)
{
$fieldModel = new Schematic_FieldModel();
$classNames = array();
$customFieldMappings = $this->getPluginsService()->call('registerSchematicFieldModels');

foreach ($customFieldMappings as $mappings) {
if (array_key_exists($fieldType, $mappings)) {
$classNames[] = $mappings[$fieldType];
}
}

$classNames[] = 'Craft\Schematic_' . ucfirst($fieldType) . 'FieldModel';

foreach($classNames as $className){
if (class_exists($className)) {
$tmpFieldModel = new $className;
if($tmpFieldModel instanceof Schematic_FieldModel){
$fieldModel = $tmpFieldModel;
break;
}
}
}

return $fieldModel;
}
}
175 changes: 175 additions & 0 deletions models/Schematic_FieldModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

namespace Craft;

/**
* Schematic Field Model.
*
* A schematic field model for mapping data
*
* @author Nerds & Company
* @copyright Copyright (c) 2015, Nerds & Company
* @license MIT
*
* @link http://www.nerds.company
*/
class Schematic_FieldModel
{
/**
* @return Schematic_FieldFactoryModel
*/
protected function getFieldFactory()
{
return craft()->schematic_fields->getFieldFactory();
}

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

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

/**
* @return Schematic_AssetsService
*/
private function getAssetsService()
{
return craft()->schematic_assets;
}

/**
* @param FieldModel $field
* @param $includeContext
* @return array
*/
public function getDefinition(FieldModel $field, $includeContext)
{
$definition = array(
'name' => $field->name,
'required' => $field->required,
'instructions' => $field->instructions,
'translatable' => $field->translatable,
'type' => $field->type,
'settings' => $field->settings,
);

if ($includeContext) {
$definition['context'] = $field->context;
}

if (isset($definition['settings']['sources'])) {
$definition['settings']['sources'] = $this->getMappedSources($definition['settings']['sources'], 'id', 'handle');
}

return $definition;
}

/**
* @param array $fieldDefinition
* @param FieldModel $field
* @param string $fieldHandle
* @param FieldGroupModel|null $group
*/
public function populate(array $fieldDefinition, FieldModel $field, $fieldHandle, FieldGroupModel $group = null)
{
$field->name = $fieldDefinition['name'];
$field->handle = $fieldHandle;
$field->required = $fieldDefinition['required'];
$field->translatable = $fieldDefinition['translatable'];
$field->instructions = $fieldDefinition['instructions'];
$field->type = $fieldDefinition['type'];
$field->settings = $fieldDefinition['settings'];

if ($group) {
$field->groupId = $group->id;
}

if (isset($definition['settings']['sources'])) {
$settings = $fieldDefinition['settings'];
$settings['sources'] = $this->getMappedSources($settings['sources'], 'handle', 'id');
$field->settings = $settings;
}
}

/**
* Get sources based on the indexFrom attribute and return them with the indexTo attribute
*
* @param string|array $sources
*
* @param string $indexFrom
* @param string $indexTo
* @return array|string
*/
private function getMappedSources($sources, $indexFrom, $indexTo)
{
$mappedSources = $sources;
if (is_array($sources)) {
$mappedSources = array();
foreach ($sources as $source) {
$mappedSources[] = $this->getSource($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 $source
* @param string $indexFrom
* @param string $indexTo
* @return string
*/
private function getSource($source, $indexFrom, $indexTo)
{
/** @var BaseElementModel $sourceObject */
$sourceObject = null;

if (strpos($source, ':') > -1) {
list($sourceType, $sourceFrom) = explode(':', $source);
switch ($sourceType) {
case 'section':
$service = $this->getSectionsService();
$method = 'getSectionBy';
break;
case 'group':
$service = $this->getUserGroupsService();
$method = 'getGroupBy';
break;
case 'folder':
$service = $this->getAssetsService();
$method = 'getSourceTypeBy';
break;
}
} elseif ($source !== 'singles') {
//Backwards compatibility
$sourceType = 'section';
$sourceFrom = $source;
$service = $this->getSectionsService();
$method = 'getSectionBy';
}

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

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

return $source;
}
}
Loading

0 comments on commit aa23d6a

Please sign in to comment.