-
Notifications
You must be signed in to change notification settings - Fork 1
/
web-service.php
executable file
·266 lines (201 loc) · 7.68 KB
/
web-service.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* ProcessWire web service implementation using template
*
* Use template URL segment to identify what page should be returned.
* All output is generated in JSON format.
*
* @copyright Copyright (c) 2014, Christian Esperar
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
*
* ProcessWire 2.x
* Copyright (C) 2013 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class WebService
{
protected $host;
protected $fieldPrefix;
public function __construct($fieldPrefix)
{
$this->host = $this->getHost();
$this->fieldPrefix = $fieldPrefix;
}
public function getHost() {
$protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$domain = $_SERVER['SERVER_NAME'];
return $protocol . "://" . $domain;
}
public function constructURL($urlSegments) {
// REMOVE API KEY ON URL PREPARATION
unset($urlSegments[1]);
// COMBINE THE SEGMENT TO CONSTRUCT INTO URL FORMAT
$url = implode("/", $urlSegments);
return "/" . $url . "/";
}
public function pageToArray(Page $page, $data = array()) {
$url = wire()->config->urls->files;
$outputFormatting = $page->outputFormatting;
$page->setOutputFormatting(false);
$id = $page->id;
// CHECK IF PAGE IS EXISTING AND RETURN 404 STATUS IF NOT
if ( ! $id ) {
return array(
'status' => 404,
'statusText' => 'NOT FOUND',
);
}
$data = array(
'status' => 200,
'statusText' => 'OK',
'created' => $page->created,
'modified' => $page->modified,
'data' => $this->getAdditionalPageField($page, $data),
);
foreach( $page->template->fieldgroup as $field ) {
if($field->type instanceof FieldtypeFieldsetOpen) continue;
// HIDE FIELD PREFIX
$trimFieldName = str_replace($this->fieldPrefix, '', $field->name);
$value = $page->get($field->name);
switch ( $field->type ) {
// CONSTRUCT DATA FOR REPEATER FIELD
case 'FieldtypeRepeater':
// CONVERT STRING TO ARRAY OF REPEATER ID
$ids = explode("|", $value);
$data = $this->getRepeaterFieldInfo($data, $ids, $trimFieldName);
break;
// CONSTRUCT DATA FOR PAGE FIELD
case 'FieldtypePage':
$data = $this->getPageFieldInfo($data, $trimFieldName, $value);
break;
// CONSTRUCT DATA FOR IMAGE FIELD
case 'FieldtypeImage':
$images = $field->type->sleepValue($page, $field, $value);
$data = $this->getImageFieldInfo($data, $url, $id, $trimFieldName, $images);
break;
// CONSTRUCT DATA FOR COMMENTS FIELD
case 'FieldtypeComments':
// GET ALL LIST OF ID
$ids = str_replace('|', ',', $value);
$comments = $field->type->sleepValue($page, $field, $value);
$data = $this->getCommentsFieldInfo($data, $ids, $trimFieldName, $comments);
break;
// FALLBACK
default:
$data['data'][$trimFieldName] = $field->type->sleepValue($page, $field, $value);
break;
}
}
$page->setOutputFormatting($outputFormatting);
return $data;
}
public function getPageInfo($id, $data = array()) {
// GET PAGES INFO
$page = wire()->pages->get("$id");
$page = $this->pageToArray($page, $data);
return isset( $page['data'] ) ? $page['data'] : null;
}
public function getRepeaterFieldInfo($data, $ids, $trimFieldName) {
foreach ( $ids as $id ) {
// GET PAGES INFO
$page = $this->getPageInfo($id);
if ( $page ) {
foreach ($page as $key => $value) {
// CHECK IF REPEATER HAS SET VALUE
if ( $value ) {
// IF REPEATER FIELD IS IMAGE
if ($key == 'image') {
$data['data'][$trimFieldName][$id] = $value;
}
// FALLBACK
else {
$data['data'][$trimFieldName][$id][$key] = $value;
}
}
}
}
}
return $data;
}
public function getPageFieldInfo($data, $trimFieldName, $id) {
// CONVERT STRING TO ARRAY OF PAGE ID
$ids = explode("|", $id);
foreach ( $ids as $id ) {
// GET PAGES INFO
$page = $this->getPageInfo($id, ['path']);
if ( $page ) {
foreach ($page as $key => $value) {
$data['data'][$trimFieldName][$key] = $value;
}
}
}
return $data;
}
public function getImageFieldInfo($data, $url, $id, $trimFieldName, $images) {
foreach ( $images as $key => $value ) {
$data['data'][$trimFieldName][$key] = array(
'path' => $this->host . $url . $id . "/" . $value['data'],
'description' => $value['description']
);
}
return $data;
}
public function getCommentsFieldInfo($data, $ids, $trimFieldName, $comments) {
$total = 0;
$result = wire('db')->query(
"
SELECT comment_id, rating
FROM CommentRatings
WHERE comment_id IN ({$ids})
"
);
// GET ALL THE RATINGS AND AVERAGE
while ( $row = $result->fetch_assoc() ) {
$commentID = $row['comment_id'];
$rating = $row['rating'];
$total = $total + $row['rating'];
$ratings[$commentID] = $rating;
}
// ITERATE THROUGHT THE COMMENT LIBRRARIES AND INSERT THE RATINGS
foreach ( $comments as $key => $value ) {
$commentID = $comments[$key]['id'];
$comments[$key]['ratings'] = $ratings[$commentID];
}
$data['data']['average'] = $total / count($comments);
$data['data'][$trimFieldName] = $comments;
return $data;
}
public function getAdditionalPageField($page, $fields) {
$data = array();
foreach ($fields as $field) {
$data[$field] = $page->$field;
}
return $data;
}
}
// GET API KEY SET ON ADMIN CONFIGURATION
$api = $pages->get('/configuration');
if ( ! $api->secret_key ) {
echo 'No secret key has been set';
}
// CHECK IF SET API ON CONFIGURATION IS MATCH ON GIVEN KEY ON URL SEGMENT
elseif ( $input->urlSegment1 === $api->secret_key ) {
$urlSegments = $input->urlSegments;
if ( !$config->debug ) header('Content-type: application/json');
// GET SET FIELD PREFIX
$fieldPrefix = $api->field_prefix;
$webService = new WebService($fieldPrefix);
// RESERVE WORD FOR HOME PAGE
if ( $input->urlSegment2 === "home" ) {
$page = $pages->get("/");
} else {
$url = $webService->constructURL($urlSegments);
$page = $pages->get("$url");
}
echo json_encode($webService->pageToArray($page, array('path')));
} else {
echo 'Invalid secret key';
}