1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\BaseApplicationComponent as BaseApplication; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Schematic Sources Service. |
10
|
|
|
* |
11
|
|
|
* Sync Craft Setups. |
12
|
|
|
* |
13
|
|
|
* @author Nerds & Company |
14
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
15
|
|
|
* @license MIT |
16
|
|
|
* |
17
|
|
|
* @link http://www.nerds.company |
18
|
|
|
*/ |
19
|
|
|
class Sources extends BaseApplication |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get sources based on the indexFrom attribute and return them with the indexTo attribute. |
23
|
|
|
* |
24
|
|
|
* @param string $fieldType |
25
|
|
|
* @param string|array $sources |
26
|
|
|
* @param string $indexFrom |
27
|
|
|
* @param string $indexTo |
28
|
|
|
* |
29
|
|
|
* @return array|string |
30
|
|
|
*/ |
31
|
|
|
public function getMappedSources($fieldType, $sources, $indexFrom, $indexTo) |
32
|
|
|
{ |
33
|
|
|
$mappedSources = $sources; |
34
|
|
|
if (is_array($sources)) { |
35
|
|
|
$mappedSources = []; |
36
|
|
|
foreach ($sources as $source) { |
37
|
|
|
$mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $mappedSources; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. |
46
|
|
|
* |
47
|
|
|
* @TODO Break up and simplify this method |
48
|
|
|
* |
49
|
|
|
* @param string $fieldType |
50
|
|
|
* @param string $source |
51
|
|
|
* @param string $indexFrom |
52
|
|
|
* @param string $indexTo |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getSource($fieldType, $source, $indexFrom, $indexTo) |
57
|
|
|
{ |
58
|
|
|
if ($source == 'singles' || $source == '*') { |
59
|
|
|
return $source; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var BaseElementModel $sourceObject */ |
63
|
|
|
$sourceObject = null; |
64
|
|
|
|
65
|
|
|
if (strpos($source, ':') > -1) { |
66
|
|
|
list($sourceType, $sourceFrom) = explode(':', $source); |
67
|
|
|
switch ($sourceType) { |
68
|
|
|
case 'section': |
69
|
|
|
$service = Craft::app()->sections; |
70
|
|
|
$method = 'getSectionBy'; |
71
|
|
|
break; |
72
|
|
|
case 'group': |
73
|
|
|
$service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories; |
74
|
|
|
$method = 'getGroupBy'; |
75
|
|
|
break; |
76
|
|
|
case 'folder': |
77
|
|
|
$service = Craft::app()->schematic_assetSources; |
78
|
|
|
$method = 'getSourceBy'; |
79
|
|
|
break; |
80
|
|
|
case 'taggroup': |
81
|
|
|
$service = Craft::app()->tags; |
82
|
|
|
$method = 'getTagGroupBy'; |
83
|
|
|
break; |
84
|
|
|
case 'field': |
85
|
|
|
$service = Craft::app()->fields; |
86
|
|
|
$method = 'getFieldBy'; |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
} elseif ($source !== 'singles') { |
90
|
|
|
//Backwards compatibility |
91
|
|
|
$sourceType = 'section'; |
92
|
|
|
$sourceFrom = $source; |
93
|
|
|
$service = Craft::app()->sections; |
94
|
|
|
$method = 'getSectionBy'; |
95
|
|
|
} |
96
|
|
|
var_dump($source); |
|
|
|
|
97
|
|
|
var_dump($service); |
|
|
|
|
98
|
|
|
var_dump($method); |
|
|
|
|
99
|
|
|
var_dump($sourceFrom); |
|
|
|
|
100
|
|
|
|
101
|
|
|
if (isset($service) && isset($method) && isset($sourceFrom)) { |
102
|
|
|
$method = $method.$indexFrom; |
103
|
|
|
$sourceObject = $service->$method($sourceFrom); |
104
|
|
|
var_dump($sourceObject); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($sourceObject && isset($sourceType)) { |
108
|
|
|
return $sourceType.':'.$sourceObject->$indexTo; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $source; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|