-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_func.php
162 lines (153 loc) · 4.78 KB
/
_func.php
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
161
162
<?php namespace ProcessWire;
function setPageModules($page, $data, $contentPage = false) {
$page->of(false);
$pages = $page->pageModules;
//$page->pageModules->removeAll();
// foreach ($pages as $pageModule) {
// if ($pageModule)
// $pageModule->delete(true);
// }
// page deletion has to be made as a second call BEFORE creating page with same name again.
// $page->save();
if (!$contentPage) {
$contentPage = wire('pages')->get('template=page-contents');
}
foreach ($data as $key => $moduleData) {
if(isset($moduleData->id)) {
$module = wire('pages')->get($moduleData->id);
$module->of(false);
setFieldValues($moduleData, $module);
$module->save();
} else {
$module = new Page();
$module->parent = $contentPage;
$module->of(false);
$module->template = $moduleData->template;
$module->title = $page->title.'_'.$moduleData->template;
$module->save();
$module->of(false);
setFieldValues($moduleData, $module);
$module->save();
$moduleData->id = $module->id;
$page->pageModules->add($module);
}
}
$page->save();
$page->of(false);
foreach($data as $key => $moduleData) {
// TODO: SORTING!
// $module = wire('pages')->get($moduleData->id);
// $module->sort = $key + 1;
// $module->save();
}
$page->pageModules->save();
foreach ($page->pageModules as $module) {
$index = array_filter($data, function($item) use ($module) {
return $item->id == $module->id;
});
if (!count($index)) {
$module->delete(true);
}
}
$page->save();
}
function setFieldValues($data, $page) {
return array_map(
function($key, $value) use ($page) {
if ($page->{$key} instanceof RepeaterPageArray) {
$page->save();
$repeaterItem = $page->{$key}->getNew();
//$repeaterItem->of(false);
array_map(function($val) use ($repeaterItem) {
setFieldValues($val, $repeaterItem);
}, $value);
//$repeaterItem->parent = $page;
$repeaterItem->save();
$page->{$key}->add($repeaterItem);
//$page->
} else {
$page->{$key} = html_entity_decode($value);
}
},
array_keys((array) $data),
(array) $data
);
}
function getFieldValues($page, $fields, $jsonSafeTransport = true) {
return array_map(
function($field) use ($page, $jsonSafeTransport) {
if ($field->type instanceof FieldtypeImage) {
$image = $page->{$field->name};
if($image->count) {
$image = $image instanceof Pageimages ? $image->first->httpUrl : $image->httpUrl;
} else {
$image = '';
}
return [$field->name => $image];
}
if ($field->type instanceof FieldtypePage) {
return [$field->name => $page->{$field->name}->title];
}
if ($field->type instanceof FieldtypeRepeater) {
$repeaterPages = $page->{$field->name};
$repeaterFields = array_map(
function($repeaterPage) use($jsonSafeTransport) {
$fields = getFieldValues($repeaterPage, $repeaterPage->fields->getArray(), $jsonSafeTransport);
return call_user_func_array('array_merge', $fields);
},
$repeaterPages->getArray()
);
return [$field->name => $repeaterFields];
}
return [$field->name => $jsonSafeTransport ? htmlentities($page->{$field->name}) : $page->{$field->name}];
},
$fields
);
}
function sendNotification($notification, $receiver, $context) {
if (!isset($eventsystemUser)) {
$eventsystemUser = \ProcessWire\Wire('users')->get('name=eventsystem');
}
$footer = processText($notification->parent->footer, $context);
$message = new WireData();
$message->setArray([
'title' => processText($notification->title, $context),
'text' => processText($notification->text, $context).$footer,
'sender' => $eventsystemUser,
'read' => false,
]);
return $receiver->sendMessage($message);
}
function processText($text, $context) {
preg_match_all('/(\{\{[\w\.]+?\}\})/m', $text, $matches);
foreach ($matches[0] as $match) {
if (count($match)) {
$attributes = explode('.', preg_replace('/[\{\}]/m', '', $match));
$attribute = $context->{$attributes[0]}->{$attributes[1]};
$text = str_replace($match, $attribute, $text);
}
}
return $text;
}
function getAttendeeStatusLabel($status) {
$labels = [
'new' => 'Neu',
'pending' => 'Zahlung ausstehend',
'accepted' => 'Bezahlt',
'waiting' => 'Warteliste',
'signedoff' => 'Abgemeldet',
'dismissed' => 'Verbannt',
];
return $labels[$status];
}
function getAttendeeStatusColor($status) {
$labels = [
'new' => 'success',
'pending' => 'warning',
'accepted' => 'success',
'waiting' => 'warning',
'signedoff' => 'error',
'dismissed' => 'error',
];
return $labels[$status];
}