-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
160 lines (142 loc) · 3.47 KB
/
code.power
File metadata and controls
160 lines (142 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* The Import Mapper Class.
*
* @var Mapper
* @since 5.0.2
*/
protected Mapper $mapper;
/**
* The Import Item Class.
*
* @var ImportItem
* @since 5.0.2
*/
protected ImportItem $importitem;
/**
* The Data Class.
*
* @var Data
* @since 5.0.2
*/
protected Data $data;
/**
* The Item Class.
*
* @var Item
* @since 5.0.2
*/
protected Item $item;
/**
* The Load Class.
*
* @var Load
* @since 5.0.2
*/
protected Load $load;
/**
* Constructor.
*
* @param Mapper $mapper The Import Mapper Class.
* @param ImportItem $importitem The Import Item Class.
* @param Data $data The Data Class.
* @param Item $item The Item Class.
* @param Load $load The Load Class.
*
* @since 5.0.2
*/
public function __construct(Mapper $mapper, ImportItem $importitem, Data $data,
Item $item, Load $load)
{
$this->mapper = $mapper;
$this->importitem = $importitem;
$this->data = $data;
$this->item = $item;
$this->load = $load;
}
/**
* Process the join tables and save the corresponding data.
*
* @param string $parentKeyValue The parent key.
* @param mixed $parentLinkValue The parent link value.
* @param array $joinFields The current join tables key fields map.
*
* @return void
* @since 5.0.2
*/
public function set(string $parentJoinKey, $parentLinkValue, array $joinFields = []): void
{
foreach ($this->mapper->getJoin() as $table => $columns)
{
$link_fields = $joinFields[$table]['link_fields'] ?? null;
if ($link_fields === null)
{
continue;
}
while ($item = $this->importitem->get($table, $columns))
{
if (empty($item))
{
break;
}
$item[$parentJoinKey] = $parentLinkValue;
if ($this->isJoinedItemReady($item, $link_fields, $table))
{
$this->saveJoinedItem($item, $link_fields, $table);
}
}
}
}
/**
* Check if the item is ready to be processed.
*
* @param array $item The item to check.
* @param array $keyFields Key fields for the table.
* @param string $table Table name.
*
* @return bool
* @since 5.0.2
*/
private function isJoinedItemReady(array $item, array $keyFields, string $table): bool
{
$ready = true;
foreach ($keyFields as $key_field)
{
if (empty($item[$key_field]))
{
$ready = false;
}
}
return $ready;
}
/**
* Save the item (either insert or update).
*
* @param array $item The item to save.
* @param array $keyFields Key fields for the table.
* @param string $table The table name.
*
* @return void
* @since 5.0.2
*/
private function saveJoinedItem(array $item, array $keyFields, string $table): void
{
$where = [];
foreach ($keyFields as $key_field)
{
$where['a.' . $key_field] = $item[$key_field];
}
$guid = $this->load->value(['a.guid' => 'guid'], ['a' => $table], $where);
if ($guid === null)
{
$guid = GuidHelper::get();
$action = 'insert';
$item['created_by'] ??= $this->data->get('import.created_by', 0);
}
else
{
$action = 'update';
$item['modified_by'] ??= $this->data->get('import.created_by', 0); // must be created by :)
}
$item['guid'] = $guid;
$this->item->table($table)->set((object)$item, 'guid', $action);
}