1
|
|
|
<?php |
2
|
|
|
namespace Route4Me; |
3
|
|
|
|
4
|
|
|
use Route4Me\Common; |
5
|
|
|
use Route4Me\Enum\Endpoint; |
6
|
|
|
|
7
|
|
|
class AddressBookLocation extends Common |
8
|
|
|
{ |
9
|
|
|
public $address_id; |
10
|
|
|
public $address_group; |
11
|
|
|
public $address_alias; |
12
|
|
|
public $address_1; |
13
|
|
|
public $address_2; |
14
|
|
|
public $first_name; |
15
|
|
|
public $last_name; |
16
|
|
|
public $address_email; |
17
|
|
|
public $address_phone_number; |
18
|
|
|
public $address_city; |
19
|
|
|
public $address_state_id; |
20
|
|
|
public $address_country_id; |
21
|
|
|
public $address_zip; |
22
|
|
|
public $cached_lat; |
23
|
|
|
public $cached_lng; |
24
|
|
|
public $curbside_lat; |
25
|
|
|
public $curbside_lng; |
26
|
|
|
public $color; |
27
|
|
|
public $address_custom_data; |
28
|
|
|
public $schedule; |
29
|
|
|
|
30
|
|
|
public $created_timestamp; |
31
|
|
|
public $member_id; |
32
|
|
|
public $schedule_blacklist; |
33
|
|
|
public $in_route_count; |
34
|
|
|
public $last_visited_timestamp; |
35
|
|
|
public $last_routed_timestamp; |
36
|
|
|
public $local_time_window_start; |
37
|
|
|
public $local_time_window_end; |
38
|
|
|
public $local_time_window_start_2; |
39
|
|
|
public $local_time_window_end_2; |
40
|
|
|
public $service_time; |
41
|
|
|
public $local_timezone_string; |
42
|
|
|
public $address_icon; |
43
|
|
|
public $address_stop_type; |
44
|
|
|
public $address_cube; |
45
|
|
|
public $address_pieces; |
46
|
|
|
public $address_reference_no; |
47
|
|
|
public $address_revenue; |
48
|
|
|
public $address_weight; |
49
|
|
|
public $address_priority; |
50
|
|
|
public $address_customer_po; |
51
|
|
|
|
52
|
|
|
public static function fromArray(array $params) |
53
|
|
|
{ |
54
|
|
|
$addressbooklocation = new AddressBookLocation(); |
55
|
|
|
|
56
|
|
|
foreach ($params as $key => $value) { |
57
|
|
|
if (property_exists($addressbooklocation, $key)) { |
58
|
|
|
$addressbooklocation->{$key} = $value; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $addressbooklocation; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
View Code Duplication |
public static function getAddressBookLocation($addressId) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$ablocations = Route4Me::makeRequst(array( |
69
|
|
|
'url' => Endpoint::ADDRESS_BOOK_V4, |
70
|
|
|
'method' => 'GET', |
71
|
|
|
'query' => array( |
72
|
|
|
'query' => $addressId, |
73
|
|
|
'limit' => 30 |
74
|
|
|
) |
75
|
|
|
)); |
76
|
|
|
|
77
|
|
|
return $ablocations; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public static function searchAddressBookLocations($params) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$allQueryFields = array('display', 'query', 'fields', 'limit', 'offset'); |
83
|
|
|
|
84
|
|
|
$result = Route4Me::makeRequst(array( |
85
|
|
|
'url' => Endpoint::ADDRESS_BOOK_V4, |
86
|
|
|
'method' => 'GET', |
87
|
|
|
'query' => Route4Me::generateRequestParameters($allQueryFields, $params) |
88
|
|
|
)); |
89
|
|
|
|
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function getAddressBookLocations($params) |
94
|
|
|
{ |
95
|
|
|
$allQueryFields = array('limit', 'offset', 'address_id'); |
96
|
|
|
|
97
|
|
|
$ablocations = Route4Me::makeRequst(array( |
98
|
|
|
'url' => Endpoint::ADDRESS_BOOK_V4, |
99
|
|
|
'method' => 'GET', |
100
|
|
|
'query' => Route4Me::generateRequestParameters($allQueryFields, $params) |
101
|
|
|
)); |
102
|
|
|
|
103
|
|
|
return $ablocations; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public static function getRandomAddressBookLocation($params) |
107
|
|
|
{ |
108
|
|
|
$ablocations = self::getAddressBookLocations($params); |
109
|
|
|
|
110
|
|
|
if (isset($ablocations["results"])) { |
111
|
|
|
$locationsSize = sizeof($ablocations["results"]); |
112
|
|
|
|
113
|
|
|
if ($locationsSize>0) { |
114
|
|
|
$randomLocationIndex = rand(0, $locationsSize - 1); |
115
|
|
|
return $ablocations["results"][$randomLocationIndex]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param AddressBookLocation $params |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public static function addAdressBookLocation($params) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$allBodyFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('address_id', 'in_route_count')); |
128
|
|
|
|
129
|
|
|
$response = Route4Me::makeRequst(array( |
130
|
|
|
'url' => Endpoint::ADDRESS_BOOK_V4, |
131
|
|
|
'method' => 'POST', |
132
|
|
|
'body' => Route4Me::generateRequestParameters($allBodyFields, $params) |
133
|
|
|
)); |
134
|
|
|
|
135
|
|
|
return $response; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function deleteAdressBookLocation($address_ids) |
139
|
|
|
{ |
140
|
|
|
$result = Route4Me::makeRequst(array( |
141
|
|
|
'url' => Endpoint::ADDRESS_BOOK_V4, |
142
|
|
|
'method' => 'DELETEARRAY', |
143
|
|
|
'query' => array( |
144
|
|
|
'address_ids' => $address_ids |
145
|
|
|
) |
146
|
|
|
)); |
147
|
|
|
|
148
|
|
|
return $result; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function updateAddressBookLocation($params) |
152
|
|
|
{ |
153
|
|
|
$allBodyFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('in_route_count')); |
154
|
|
|
|
155
|
|
|
$response = Route4Me::makeRequst(array( |
156
|
|
|
'url' => Endpoint::ADDRESS_BOOK_V4, |
157
|
|
|
'method' => 'PUT', |
158
|
|
|
'body' => Route4Me::generateRequestParameters($allBodyFields, $params) |
159
|
|
|
)); |
160
|
|
|
|
161
|
|
|
return $response; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public static function validateScheduleMode($scheduleMode) |
165
|
|
|
{ |
166
|
|
|
$schedMmodes = array("daily", "weekly", "monthly", "annually"); |
167
|
|
|
|
168
|
|
|
if (in_array($scheduleMode, $schedMmodes)) { |
169
|
|
|
return TRUE; |
170
|
|
|
} else { |
171
|
|
|
return FALSE; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public static function validateScheduleEnable($scheduleEnabled) |
176
|
|
|
{ |
177
|
|
|
$schedEnables = array(TRUE, FALSE); |
178
|
|
|
|
179
|
|
|
if (in_array($scheduleEnabled, $schedEnables)) { |
180
|
|
|
return TRUE; |
181
|
|
|
} else { |
182
|
|
|
return FALSE; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public static function validateScheduleEvery($scheduleEvery) |
187
|
|
|
{ |
188
|
|
|
if (is_numeric($scheduleEvery)) { |
189
|
|
|
return TRUE; |
190
|
|
|
} else { |
191
|
|
|
return FALSE; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
View Code Duplication |
public static function validateScheduleWeekDays($scheduleWeekDays) |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
$weekdays = explode(',', $scheduleWeekDays); |
198
|
|
|
$weekdaysSize = sizeof($weekdays); |
199
|
|
|
|
200
|
|
|
if ($weekdaysSize<1) { |
201
|
|
|
return FALSE; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$isValid = TRUE; |
205
|
|
|
|
206
|
|
|
for ($i = 0; $i<$weekdaysSize; $i++) { |
207
|
|
|
if (is_numeric($weekdays[$i])) { |
208
|
|
|
$wday = intval($weekdays[$i]); |
209
|
|
|
if ($wday<1 || $wday>7) { |
210
|
|
|
$isValid = FALSE; |
211
|
|
|
} |
212
|
|
|
} else { |
213
|
|
|
$isValid = FALSE; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $isValid; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public static function validateScheduleMonthlyMode($scheduleMonthlyMode) |
221
|
|
|
{ |
222
|
|
|
$schedMonthlyMmodes = array("dates", "nth"); |
223
|
|
|
|
224
|
|
|
if (in_array($scheduleMonthlyMode, $schedMonthlyMmodes)) { |
225
|
|
|
return TRUE; |
226
|
|
|
} else { |
227
|
|
|
return FALSE; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
View Code Duplication |
public static function validateScheduleMonthlyDates($scheduleMonthlyDates) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$monthlyDates = explode(',', $scheduleMonthlyDates); |
234
|
|
|
$monthlyDatesSize = sizeof($monthlyDates); |
235
|
|
|
|
236
|
|
|
if ($monthlyDatesSize<1) { |
237
|
|
|
return FALSE; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$isValid = TRUE; |
241
|
|
|
|
242
|
|
|
for ($i = 0; $i<$monthlyDatesSize; $i++) { |
243
|
|
|
if (is_numeric($monthlyDates[$i])) { |
244
|
|
|
$mday = intval($monthlyDates[$i]); |
245
|
|
|
if ($mday<1 || $mday>31) { |
246
|
|
|
$isValid = FALSE; |
247
|
|
|
} |
248
|
|
|
} else { |
249
|
|
|
$isValid = FALSE; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $isValid; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public static function validateScheduleNthN($scheduleNthN) |
257
|
|
|
{ |
258
|
|
|
if (!is_numeric($scheduleNthN)) { |
259
|
|
|
return FALSE; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$schedNthNs = array(1, 2, 3, 4, 5, -1); |
263
|
|
|
|
264
|
|
|
if (in_array($scheduleNthN, $schedNthNs)) { |
265
|
|
|
return TRUE; |
266
|
|
|
} else { |
267
|
|
|
return FALSE; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public static function validateScheduleNthWhat($scheduleNthWhat) |
272
|
|
|
{ |
273
|
|
|
if (!is_numeric($scheduleNthWhat)) { |
274
|
|
|
return FALSE; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$schedNthWhats = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
278
|
|
|
|
279
|
|
|
if (in_array($scheduleNthWhat, $schedNthWhats)) { |
280
|
|
|
return TRUE; |
281
|
|
|
} else { |
282
|
|
|
return FALSE; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** Function adds the locations (with/without schedule) from the CSV file. |
287
|
|
|
* $csvFileHandle - a file handler. |
288
|
|
|
* Returns array $results which contains two arrays: fail and succes. |
289
|
|
|
*/ |
290
|
|
|
public function addLocationsFromCsvFile($csvFileHandle, $locationsFieldsMapping) |
291
|
|
|
{ |
292
|
|
|
$max_line_length = 512; |
293
|
|
|
$delemietr = ','; |
294
|
|
|
|
295
|
|
|
$results = array(); |
296
|
|
|
$results['fail'] = array(); |
297
|
|
|
$results['success'] = array(); |
298
|
|
|
|
299
|
|
|
$columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr); |
300
|
|
|
|
301
|
|
|
$addressBookFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('address_id', 'in_route_count')); |
302
|
|
|
|
303
|
|
|
if (empty($columns)) { |
304
|
|
|
array_push($results['fail'], 'Empty CSV table'); |
305
|
|
|
return ($results); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$iRow = 1; |
309
|
|
|
|
310
|
|
|
while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr))!==false) { |
311
|
|
|
if (!isset($rows[$locationsFieldsMapping['cached_lat']]) || !isset($rows[$locationsFieldsMapping['cached_lng']]) |
312
|
|
|
|| !isset($rows[$locationsFieldsMapping['address_1']]) || array(null)==$rows) { |
313
|
|
|
continue; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
$curSchedule = ""; |
317
|
|
|
$mode = ""; |
318
|
|
|
|
319
|
|
|
$failCount = sizeof($results['fail']); |
320
|
|
|
|
321
|
|
View Code Duplication |
if (isset($rows[$locationsFieldsMapping['schedule_mode']])) { |
|
|
|
|
322
|
|
|
if ($this->validateScheduleMode($rows[$locationsFieldsMapping['schedule_mode']])) { |
323
|
|
|
$curSchedule = '"mode":"'.$rows[$locationsFieldsMapping['schedule_mode']].'",'; |
324
|
|
|
$mode = $rows[$locationsFieldsMapping['schedule_mode']]; |
325
|
|
|
} else { |
326
|
|
|
array_push($results['fail'], "$iRow --> Wrong schedule mode parameter"); |
327
|
|
|
} |
328
|
|
|
} else { |
329
|
|
|
array_push($results['fail'], "$iRow --> The schedule mode parameter is not set"); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
if (isset($rows[$locationsFieldsMapping['schedule_enabled']])) { |
333
|
|
|
if ($this->validateScheduleEnable($rows[$locationsFieldsMapping['schedule_enabled']])) { |
334
|
|
|
$curSchedule .= '"enabled":'.$rows[$locationsFieldsMapping['schedule_enabled']].','; |
335
|
|
|
} else { |
336
|
|
|
array_push($results['fail'], "$iRow --> The schedule enabled parameter is not set "); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
if (isset($rows[$locationsFieldsMapping['schedule_every']])) { |
341
|
|
|
if ($this->validateScheduleEvery($rows[$locationsFieldsMapping['schedule_every']])) { |
342
|
|
|
$curSchedule .= '"'.$mode.'":{'.'"every":'.$rows[$locationsFieldsMapping['schedule_every']].','; |
343
|
|
|
if ($mode=='daily') { |
344
|
|
|
$curSchedule = trim($curSchedule, ','); |
345
|
|
|
$curSchedule .= '}'; |
346
|
|
|
} |
347
|
|
|
} else { |
348
|
|
|
array_push($results['fail'], "$iRow --> The parameter sched_every is not set"); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
if ($mode!='daily') { |
353
|
|
|
switch ($mode) { |
354
|
|
|
case 'weekly': |
355
|
|
View Code Duplication |
if (isset($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
|
|
|
|
356
|
|
|
if ($this->validateScheduleWeekDays($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
357
|
|
|
$curSchedule .= '"weekdays":['.$rows[$locationsFieldsMapping['schedule_weekdays']].']}'; |
358
|
|
|
} else { |
359
|
|
|
array_push($results['fail'], "$iRow --> Wrong weekdays"); |
360
|
|
|
} |
361
|
|
|
} else { |
362
|
|
|
array_push($results['fail'], "$iRow --> The parameters sched_weekdays is not set"); |
363
|
|
|
} |
364
|
|
|
break; |
365
|
|
|
case 'monthly': |
366
|
|
|
$monthlyMode = ""; |
367
|
|
View Code Duplication |
if (isset($rows[$locationsFieldsMapping['monthly_mode']])) { |
|
|
|
|
368
|
|
|
if ($this->validateScheduleMonthlyMode($rows[$locationsFieldsMapping['monthly_mode']])) { |
369
|
|
|
$monthlyMode = $rows[$locationsFieldsMapping['monthly_mode']]; |
370
|
|
|
$curSchedule .= '"mode": "'.$rows[$locationsFieldsMapping['monthly_mode']].'",'; |
371
|
|
|
} else { |
372
|
|
|
array_push($results['fail'], "$iRow --> Wrong monthly mode"); |
373
|
|
|
} |
374
|
|
|
} else { |
375
|
|
|
array_push($results['fail'], "$iRow --> The parameter sched_monthly_mode is not set"); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
if ($monthlyMode!="") { |
379
|
|
|
switch ($monthlyMode) { |
380
|
|
|
case 'dates': |
381
|
|
|
if (isset($rows[$locationsFieldsMapping['monthly_dates']])) { |
382
|
|
|
if ($this->validateScheduleMonthlyDates($rows[$locationsFieldsMapping['monthly_dates']])) { |
383
|
|
|
$curSchedule .= '"dates":['.$rows[$locationsFieldsMapping['monthly_dates']].']}'; |
384
|
|
|
} else { |
385
|
|
|
array_push($results['fail'], "$iRow --> Wrong monthly dates"); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
break; |
389
|
|
|
case 'nth': |
390
|
|
View Code Duplication |
if (isset($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
|
|
|
|
391
|
|
|
if ($this->validateScheduleNthN($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
392
|
|
|
$curSchedule .= '"nth":{"n":'.$rows[$locationsFieldsMapping['monthly_nth_n']].','; |
393
|
|
|
} else { |
394
|
|
|
array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_n"); |
395
|
|
|
} |
396
|
|
|
} else { |
397
|
|
|
array_push($results['fail'], "$iRow --> The parameter sched_nth_n is not set"); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
View Code Duplication |
if ($curSchedule!="") { |
|
|
|
|
401
|
|
|
if (isset($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) { |
402
|
|
|
if ($this->validateScheduleNthWhat($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) { |
403
|
|
|
$curSchedule .= '"what":'.$rows[$locationsFieldsMapping['monthly_nth_wwhat']].'}}'; |
404
|
|
|
} else { |
405
|
|
|
array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_what"); |
406
|
|
|
} |
407
|
|
|
} else { |
408
|
|
|
array_push($results['fail'], "$iRow --> The parameter sched_nth_what is not set"); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
break; |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
break; |
415
|
|
|
default: |
416
|
|
|
$curSchedule = ""; |
417
|
|
|
break; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
if (sizeof($results['fail'])>$failCount) { |
422
|
|
|
$curSchedule = ""; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
if (($mode=='daily' || $mode=='weekly' || $mode=='monthy') && $curSchedule=="") { |
426
|
|
|
$iRow++; |
427
|
|
|
continue; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
$curSchedule = strtolower($curSchedule); |
431
|
|
|
|
432
|
|
|
$curSchedule = '[{'.$curSchedule.'}]'; |
|
|
|
|
433
|
|
|
|
434
|
|
|
$parametersArray = array(); |
435
|
|
|
|
436
|
|
|
foreach ($addressBookFields as $addressBookField) { |
437
|
|
|
if (isset($locationsFieldsMapping[$addressBookField])) { |
438
|
|
|
$parametersArray[$addressBookField] = $rows[$locationsFieldsMapping[$addressBookField]]; |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
$AdressBookLocationParameters = AddressBookLocation::fromArray($parametersArray); |
443
|
|
|
|
444
|
|
|
$abContacts = new AddressBookLocation(); |
445
|
|
|
|
446
|
|
|
$abcResults = $abContacts->addAdressBookLocation($AdressBookLocationParameters); //temporarry |
447
|
|
|
|
448
|
|
|
array_push($results['success'], "The schedule location with address_id = ".strval($abcResults["address_id"])." added successfuly."); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
return $results; |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.