-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from itmundi/fields-service-refactor
Fields service refactor
- Loading branch information
Showing
16 changed files
with
828 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ public function getName() | |
*/ | ||
public function getVersion() | ||
{ | ||
return '1.3.0'; | ||
return '1.4.0'; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.