|
1
|
|
|
<?php |
|
2
|
|
|
namespace nystudio107\recipe\integrations; |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
use Cake\Utility\Hash; |
|
|
|
|
|
|
5
|
|
|
use craft\feedme\base\Field; |
|
|
|
|
|
|
6
|
|
|
use craft\feedme\base\FieldInterface; |
|
|
|
|
|
|
7
|
|
|
use craft\feedme\helpers\DataHelper; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class RecipeFeedMeField extends Field implements FieldInterface |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
// Properties |
|
12
|
|
|
// ========================================================================= |
|
13
|
|
|
|
|
14
|
|
|
public static $name = 'Recipe'; |
|
15
|
|
|
public static $class = 'nystudio107\recipe\fields\Recipe'; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
// Templates |
|
19
|
|
|
// ========================================================================= |
|
20
|
|
|
|
|
21
|
|
|
public function getMappingTemplate() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
return 'recipe/_integrations/feed-me'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
// Public Methods |
|
28
|
|
|
// ========================================================================= |
|
29
|
|
|
|
|
30
|
|
|
public function parseField() |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$preppedData = []; |
|
33
|
|
|
|
|
34
|
|
|
$fields = Hash::get($this->fieldInfo, 'fields'); |
|
35
|
|
|
|
|
36
|
|
|
if (!$fields) { |
|
37
|
|
|
return null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
foreach ($fields as $subFieldHandle => $subFieldInfo) { |
|
41
|
|
|
// Check for sub-sub fields - bit dirty... |
|
42
|
|
|
$subfields = Hash::get($subFieldInfo, 'fields'); |
|
43
|
|
|
|
|
44
|
|
|
if ($subfields) { |
|
45
|
|
|
foreach ($subfields as $subSubFieldHandle => $subSubFieldInfo) { |
|
46
|
|
|
// Handle array data, man I hate Feed Me's data mapping now... |
|
47
|
|
|
$content = DataHelper::fetchValue($this->feedData, $subSubFieldInfo); |
|
48
|
|
|
|
|
49
|
|
|
if (is_array($content)) { |
|
50
|
|
|
foreach ($content as $key => $value) { |
|
51
|
|
|
$preppedData[$subFieldHandle][$key][$subSubFieldHandle] = $value; |
|
52
|
|
|
} |
|
53
|
|
|
} else { |
|
54
|
|
|
$preppedData[$subFieldHandle][$subSubFieldHandle] = $content; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} else { |
|
58
|
|
|
$preppedData[$subFieldHandle] = DataHelper::fetchValue($this->feedData, $subFieldInfo); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Protect against sending an empty array |
|
63
|
|
|
if (!$preppedData) { |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $preppedData; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |