@@ -14,700 +14,700 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class AddressBookLocation extends Common |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * A territory shape name the contact belongs. |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - public $address_id; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * A group the contact belongs. |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - public $address_group; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The contact's alias. |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - public $address_alias; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The geographic address of the contact. |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - public $address_1; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Second geographic address of the contact. |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - public $address_2; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The first name of the contact person. |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - public $first_name; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The last name of the contact person. |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - public $last_name; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The contact's email. |
|
| 61 | - * @var string |
|
| 62 | - */ |
|
| 63 | - public $address_email; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * The contact's phone number. |
|
| 67 | - * @var string |
|
| 68 | - */ |
|
| 69 | - public $address_phone_number; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * A city the contact belongs. |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - public $address_city; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * The ID of the state the contact belongs. |
|
| 79 | - * @var string |
|
| 80 | - */ |
|
| 81 | - public $address_state_id; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * The ID of the country the contact belongs. |
|
| 85 | - * @var string |
|
| 86 | - */ |
|
| 87 | - public $address_country_id; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * The contact's ZIP code. |
|
| 91 | - * @var string |
|
| 92 | - */ |
|
| 93 | - public $address_zip; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * A latitude of the contact's cached position. |
|
| 97 | - * @var double |
|
| 98 | - */ |
|
| 99 | - public $cached_lat; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * A longitude of the contact's cached position. |
|
| 103 | - * @var double |
|
| 104 | - */ |
|
| 105 | - public $cached_lng; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * A latitude of the contact's curbside. |
|
| 109 | - * @var double |
|
| 110 | - */ |
|
| 111 | - public $curbside_lat; |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * A longitude of the contact's curbside. |
|
| 115 | - * @var double |
|
| 116 | - */ |
|
| 117 | - public $curbside_lng; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * The contact's color on the map. |
|
| 121 | - * @var string |
|
| 122 | - */ |
|
| 123 | - public $color; |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * An array of the contact's custom field-value pairs. |
|
| 127 | - * @var array |
|
| 128 | - */ |
|
| 129 | - public $address_custom_data; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * An array of the contact's schedules. |
|
| 133 | - * @var Schedule[] |
|
| 134 | - */ |
|
| 135 | - public $schedule; |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Time when the contact was created. |
|
| 139 | - * @var long |
|
| 140 | - */ |
|
| 141 | - public $created_timestamp; |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Unique ID of the member. |
|
| 145 | - * @var integer |
|
| 146 | - */ |
|
| 147 | - public $member_id; |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * The list of dates that should be omitted from the schedules. |
|
| 151 | - * @var string[] |
|
| 152 | - */ |
|
| 153 | - public $schedule_blacklist; |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Number of the routes containing the contact. |
|
| 157 | - * @var integer |
|
| 158 | - */ |
|
| 159 | - public $in_route_count; |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * When the contact was last visited. |
|
| 163 | - * @var long |
|
| 164 | - */ |
|
| 165 | - public $last_visited_timestamp; |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * When the contact was last routed. |
|
| 169 | - * @var long |
|
| 170 | - */ |
|
| 171 | - public $last_routed_timestamp; |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Start of the contact's local time window. |
|
| 175 | - * @var long |
|
| 176 | - */ |
|
| 177 | - public $local_time_window_start; |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * End of the contact's local time window. |
|
| 181 | - * @var long |
|
| 182 | - */ |
|
| 183 | - public $local_time_window_end; |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Start of the contact's second local time window. |
|
| 187 | - * @var long |
|
| 188 | - */ |
|
| 189 | - public $local_time_window_start_2; |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * End of the contact's second local time window. |
|
| 193 | - * @var long |
|
| 194 | - */ |
|
| 195 | - public $local_time_window_end_2; |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * The service time at the contact's address. |
|
| 199 | - * @var integer |
|
| 200 | - */ |
|
| 201 | - public $service_time; |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * The contact's local timezone. |
|
| 205 | - * @var string |
|
| 206 | - */ |
|
| 207 | - public $local_timezone_string; |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * The contact's icon on the map. |
|
| 211 | - * @var string |
|
| 212 | - */ |
|
| 213 | - public $address_icon; |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * The contact's stop type. |
|
| 217 | - * @var string |
|
| 218 | - */ |
|
| 219 | - public $address_stop_type; |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * The cubic volume of the contact's cargo. |
|
| 223 | - * @var double |
|
| 224 | - */ |
|
| 225 | - public $address_cube; |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * The number of pieces/pallets that this destination/order/line-item consumes/contains on a vehicle. |
|
| 229 | - * @var integer |
|
| 230 | - */ |
|
| 231 | - public $address_pieces; |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * The reference number of the address. |
|
| 235 | - * @var string |
|
| 236 | - */ |
|
| 237 | - public $address_reference_no; |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * The revenue from the contact. |
|
| 241 | - * @var double |
|
| 242 | - */ |
|
| 243 | - public $address_revenue; |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * The weight of the contact's cargo. |
|
| 247 | - * @var double |
|
| 248 | - */ |
|
| 249 | - public $address_weight; |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * If present, the priority will sequence addresses in all the optimal routes so that |
|
| 253 | - * higher priority addresses are general at the beginning of the route sequence.<br> |
|
| 254 | - * 1 is the highest priority, 100000 is the lowest. |
|
| 255 | - * @var integer |
|
| 256 | - */ |
|
| 257 | - public $address_priority; |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * The customer purchase order of the contact. |
|
| 261 | - * @var string |
|
| 262 | - */ |
|
| 263 | - public $address_customer_po; |
|
| 264 | - |
|
| 265 | - public function __construct() |
|
| 266 | - { |
|
| 267 | - Route4Me::setBaseUrl(Endpoint::BASE_URL); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - public static function fromArray(array $params) |
|
| 271 | - { |
|
| 272 | - $addressbooklocation = new self(); |
|
| 273 | - |
|
| 274 | - foreach ($params as $key => $value) { |
|
| 275 | - if (property_exists($addressbooklocation, $key)) { |
|
| 276 | - $addressbooklocation->{$key} = $value; |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - return $addressbooklocation; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * @deprecated 1.2.8 |
|
| 285 | - * @see \Route4Me\V5\AddressBook\AddressBook::getAddressById() |
|
| 286 | - */ |
|
| 287 | - public static function getAddressBookLocation($addressId) |
|
| 288 | - { |
|
| 289 | - $ablocations = Route4Me::makeRequst([ |
|
| 290 | - 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 291 | - 'method' => 'GET', |
|
| 292 | - 'query' => [ |
|
| 293 | - 'query' => $addressId, |
|
| 294 | - 'limit' => 30, |
|
| 295 | - ], |
|
| 296 | - ]); |
|
| 297 | - |
|
| 298 | - return $ablocations; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * @deprecated 1.2.8 |
|
| 303 | - * @see \Route4Me\V5\AddressBook\AddressBook::getAddressesByBodyPayload() |
|
| 304 | - */ |
|
| 305 | - public static function searchAddressBookLocations($params) |
|
| 306 | - { |
|
| 307 | - $allQueryFields = ['display', 'query', 'fields', 'limit', 'offset']; |
|
| 308 | - |
|
| 309 | - $result = Route4Me::makeRequst([ |
|
| 310 | - 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 311 | - 'method' => 'GET', |
|
| 312 | - 'query' => Route4Me::generateRequestParameters($allQueryFields, $params), |
|
| 313 | - ]); |
|
| 314 | - |
|
| 315 | - return $result; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @deprecated 1.2.8 |
|
| 320 | - * @see \Route4Me\V5\AddressBook\AddressBook::getAddressesByIds() |
|
| 321 | - */ |
|
| 322 | - public static function getAddressBookLocations($params) |
|
| 323 | - { |
|
| 324 | - $allQueryFields = ['limit', 'offset', 'address_id']; |
|
| 325 | - |
|
| 326 | - $ablocations = Route4Me::makeRequst([ |
|
| 327 | - 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 328 | - 'method' => 'GET', |
|
| 329 | - 'query' => Route4Me::generateRequestParameters($allQueryFields, $params), |
|
| 330 | - ]); |
|
| 331 | - |
|
| 332 | - return $ablocations; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - public static function getRandomAddressBookLocation($params) |
|
| 336 | - { |
|
| 337 | - $ablocations = self::getAddressBookLocations($params); |
|
| 338 | - |
|
| 339 | - if (isset($ablocations['results'])) { |
|
| 340 | - $locationsSize = sizeof($ablocations['results']); |
|
| 341 | - |
|
| 342 | - if ($locationsSize > 0) { |
|
| 343 | - $randomLocationIndex = rand(0, $locationsSize - 1); |
|
| 344 | - |
|
| 345 | - return $ablocations['results'][$randomLocationIndex]; |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - return null; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * @deprecated 1.2.8 |
|
| 354 | - * @see \Route4Me\V5\AddressBook\AddressBook::addAddress() |
|
| 355 | - * |
|
| 356 | - * @param AddressBookLocation $params |
|
| 357 | - */ |
|
| 358 | - public static function addAdressBookLocation($params) |
|
| 359 | - { |
|
| 360 | - $allBodyFields = Route4Me::getObjectProperties(new self(), ['address_id', 'in_route_count']); |
|
| 361 | - |
|
| 362 | - $response = Route4Me::makeRequst([ |
|
| 363 | - 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 364 | - 'method' => 'POST', |
|
| 365 | - 'body' => Route4Me::generateRequestParameters($allBodyFields, $params), |
|
| 366 | - ]); |
|
| 367 | - |
|
| 368 | - return $response; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * @deprecated 1.2.8 |
|
| 373 | - * @see \Route4Me\V5\AddressBook\AddressBook::deleteAddressesByIds() |
|
| 374 | - */ |
|
| 375 | - public function deleteAdressBookLocation($address_ids) |
|
| 376 | - { |
|
| 377 | - $result = Route4Me::makeRequst([ |
|
| 378 | - 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 379 | - 'method' => 'DELETEARRAY', |
|
| 380 | - 'query' => [ |
|
| 381 | - 'address_ids' => $address_ids, |
|
| 382 | - ], |
|
| 383 | - ]); |
|
| 384 | - |
|
| 385 | - return $result; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * @deprecated 1.2.8 |
|
| 390 | - * @see \Route4Me\V5\AddressBook\AddressBook::updateAddressById() |
|
| 391 | - */ |
|
| 392 | - public function updateAddressBookLocation($params) |
|
| 393 | - { |
|
| 394 | - $allBodyFields = Route4Me::getObjectProperties(new self(), ['in_route_count']); |
|
| 395 | - |
|
| 396 | - $response = Route4Me::makeRequst([ |
|
| 397 | - 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 398 | - 'method' => 'PUT', |
|
| 399 | - 'body' => Route4Me::generateRequestParameters($allBodyFields, $params), |
|
| 400 | - ]); |
|
| 401 | - |
|
| 402 | - return $response; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - public static function validateScheduleMode($scheduleMode) |
|
| 406 | - { |
|
| 407 | - $schedModes = ['daily', 'weekly', 'monthly', 'annually']; |
|
| 408 | - |
|
| 409 | - if (in_array($scheduleMode, $schedModes)) { |
|
| 410 | - return true; |
|
| 411 | - } else { |
|
| 412 | - return false; |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - public static function validateScheduleEnable($scheduleEnabled) |
|
| 417 | - { |
|
| 418 | - if (is_string($scheduleEnabled)) { |
|
| 419 | - if (strtolower($scheduleEnabled)=="true") $scheduleEnabled = true; |
|
| 420 | - if (strtolower($scheduleEnabled)=="false") $scheduleEnabled = false; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - $schedEnables = [true, false,]; |
|
| 424 | - |
|
| 425 | - if (in_array($scheduleEnabled, $schedEnables,true)) { |
|
| 426 | - return true; |
|
| 427 | - } else { |
|
| 428 | - return false; |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - public static function validateScheduleEvery($scheduleEvery) |
|
| 433 | - { |
|
| 434 | - if (is_numeric($scheduleEvery)) { |
|
| 435 | - if ($scheduleEvery>0) { |
|
| 436 | - return true; |
|
| 437 | - } else { |
|
| 438 | - return false; |
|
| 439 | - } |
|
| 440 | - } else { |
|
| 441 | - return false; |
|
| 442 | - } |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - public static function validateScheduleWeekDays($scheduleWeekDays) |
|
| 446 | - { |
|
| 447 | - if (is_bool($scheduleWeekDays)) return false; |
|
| 448 | - |
|
| 449 | - $weekdays = explode(',', $scheduleWeekDays); |
|
| 450 | - $weekdaysSize = sizeof($weekdays); |
|
| 451 | - |
|
| 452 | - if ($weekdaysSize < 1) { |
|
| 453 | - return false; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - $isValid = true; |
|
| 457 | - |
|
| 458 | - for ($i = 0; $i < $weekdaysSize; ++$i) { |
|
| 459 | - if (is_bool($weekdays[$i])) { |
|
| 460 | - $isValid = false; |
|
| 461 | - } elseif (is_numeric($weekdays[$i])) { |
|
| 462 | - $wday = intval($weekdays[$i]); |
|
| 463 | - if ($wday < 1 || $wday > 7) { |
|
| 464 | - $isValid = false; |
|
| 465 | - } |
|
| 466 | - } else { |
|
| 467 | - $isValid = false; |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - return $isValid; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - public static function validateScheduleMonthlyMode($scheduleMonthlyMode) |
|
| 475 | - { |
|
| 476 | - $schedMonthlyMmodes = ['dates', 'nth']; |
|
| 477 | - |
|
| 478 | - if (in_array($scheduleMonthlyMode, $schedMonthlyMmodes,true)) { |
|
| 479 | - return true; |
|
| 480 | - } else { |
|
| 481 | - return false; |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - public static function validateScheduleMonthlyDates($scheduleMonthlyDates) |
|
| 486 | - { |
|
| 487 | - if (is_bool($scheduleMonthlyDates)) return false; |
|
| 488 | - |
|
| 489 | - $monthlyDates = explode(',', $scheduleMonthlyDates); |
|
| 490 | - $monthlyDatesSize = sizeof($monthlyDates); |
|
| 491 | - |
|
| 492 | - if ($monthlyDatesSize < 1) { |
|
| 493 | - return false; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - $isValid = true; |
|
| 497 | - |
|
| 498 | - for ($i = 0; $i < $monthlyDatesSize; ++$i) { |
|
| 499 | - if (is_numeric($monthlyDates[$i])) { |
|
| 500 | - $mday = intval($monthlyDates[$i]); |
|
| 501 | - if ($mday < 1 || $mday > 31) { |
|
| 502 | - $isValid = false; |
|
| 503 | - } |
|
| 504 | - } else { |
|
| 505 | - $isValid = false; |
|
| 506 | - } |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - return $isValid; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - public static function validateScheduleNthN($scheduleNthN) |
|
| 513 | - { |
|
| 514 | - if (!is_numeric($scheduleNthN)) { |
|
| 515 | - return false; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - $schedNthNs = [1, 2, 3, 4, 5, -1]; |
|
| 519 | - |
|
| 520 | - if (in_array($scheduleNthN, $schedNthNs)) { |
|
| 521 | - return true; |
|
| 522 | - } else { |
|
| 523 | - return false; |
|
| 524 | - } |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - public static function validateScheduleNthWhat($scheduleNthWhat) |
|
| 528 | - { |
|
| 529 | - if (!is_numeric($scheduleNthWhat)) { |
|
| 530 | - return false; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - $schedNthWhats = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
|
| 534 | - |
|
| 535 | - if (in_array($scheduleNthWhat, $schedNthWhats)) { |
|
| 536 | - return true; |
|
| 537 | - } else { |
|
| 538 | - return false; |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** Function adds the locations (with/without schedule) from the CSV file. |
|
| 543 | - * $csvFileHandle - a file handler. |
|
| 544 | - * Returns array $results which contains two arrays: fail and succes. |
|
| 545 | - */ |
|
| 546 | - public function addLocationsFromCsvFile($csvFileHandle, $locationsFieldsMapping) |
|
| 547 | - { |
|
| 548 | - $max_line_length = 512; |
|
| 549 | - $delemietr = ','; |
|
| 550 | - |
|
| 551 | - $results = []; |
|
| 552 | - $results['fail'] = []; |
|
| 553 | - $results['success'] = []; |
|
| 554 | - |
|
| 555 | - $columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr); |
|
| 556 | - |
|
| 557 | - $addressBookFields = Route4Me::getObjectProperties(new self(), ['address_id', 'in_route_count']); |
|
| 558 | - |
|
| 559 | - if (empty($columns)) { |
|
| 560 | - array_push($results['fail'], 'Empty CSV table'); |
|
| 561 | - |
|
| 562 | - return $results; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - $iRow = 1; |
|
| 566 | - |
|
| 567 | - while (false !== ($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr))) { |
|
| 568 | - if (!isset($rows[$locationsFieldsMapping['cached_lat']]) || !isset($rows[$locationsFieldsMapping['cached_lng']]) |
|
| 569 | - || !isset($rows[$locationsFieldsMapping['address_1']]) || [null] == $rows) { |
|
| 570 | - continue; |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - $curSchedule = ''; |
|
| 574 | - $mode = ''; |
|
| 575 | - |
|
| 576 | - $failCount = sizeof($results['fail']); |
|
| 577 | - |
|
| 578 | - if (isset($rows[$locationsFieldsMapping['schedule_mode']])) { |
|
| 579 | - if ($this->validateScheduleMode($rows[$locationsFieldsMapping['schedule_mode']])) { |
|
| 580 | - $curSchedule = '"mode":"'.$rows[$locationsFieldsMapping['schedule_mode']].'",'; |
|
| 581 | - $mode = $rows[$locationsFieldsMapping['schedule_mode']]; |
|
| 582 | - } else { |
|
| 583 | - array_push($results['fail'], "$iRow --> Wrong schedule mode parameter"); |
|
| 584 | - } |
|
| 585 | - } else { |
|
| 586 | - array_push($results['fail'], "$iRow --> The schedule mode parameter is not set"); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - if (isset($rows[$locationsFieldsMapping['schedule_enabled']])) { |
|
| 590 | - if ($this->validateScheduleEnable($rows[$locationsFieldsMapping['schedule_enabled']])) { |
|
| 591 | - $curSchedule .= '"enabled":'.$rows[$locationsFieldsMapping['schedule_enabled']].','; |
|
| 592 | - } else { |
|
| 593 | - array_push($results['fail'], "$iRow --> The schedule enabled parameter is not set "); |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - if (isset($rows[$locationsFieldsMapping['schedule_every']])) { |
|
| 598 | - if ($this->validateScheduleEvery($rows[$locationsFieldsMapping['schedule_every']])) { |
|
| 599 | - $curSchedule .= '"'.$mode.'":{'.'"every":'.$rows[$locationsFieldsMapping['schedule_every']].','; |
|
| 600 | - if ('daily' == $mode) { |
|
| 601 | - $curSchedule = trim($curSchedule, ','); |
|
| 602 | - $curSchedule .= '}'; |
|
| 603 | - } |
|
| 604 | - } else { |
|
| 605 | - array_push($results['fail'], "$iRow --> The parameter sched_every is not set"); |
|
| 606 | - } |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - if ('daily' != $mode) { |
|
| 610 | - switch ($mode) { |
|
| 611 | - case 'weekly': |
|
| 612 | - if (isset($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
|
| 613 | - if ($this->validateScheduleWeekDays($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
|
| 614 | - $curSchedule .= '"weekdays":['.$rows[$locationsFieldsMapping['schedule_weekdays']].']}'; |
|
| 615 | - } else { |
|
| 616 | - array_push($results['fail'], "$iRow --> Wrong weekdays"); |
|
| 617 | - } |
|
| 618 | - } else { |
|
| 619 | - array_push($results['fail'], "$iRow --> The parameters sched_weekdays is not set"); |
|
| 620 | - } |
|
| 621 | - break; |
|
| 622 | - case 'monthly': |
|
| 623 | - $monthlyMode = ''; |
|
| 624 | - if (isset($rows[$locationsFieldsMapping['monthly_mode']])) { |
|
| 625 | - if ($this->validateScheduleMonthlyMode($rows[$locationsFieldsMapping['monthly_mode']])) { |
|
| 626 | - $monthlyMode = $rows[$locationsFieldsMapping['monthly_mode']]; |
|
| 627 | - $curSchedule .= '"mode": "'.$rows[$locationsFieldsMapping['monthly_mode']].'",'; |
|
| 628 | - } else { |
|
| 629 | - array_push($results['fail'], "$iRow --> Wrong monthly mode"); |
|
| 630 | - } |
|
| 631 | - } else { |
|
| 632 | - array_push($results['fail'], "$iRow --> The parameter sched_monthly_mode is not set"); |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - if ('' != $monthlyMode) { |
|
| 636 | - switch ($monthlyMode) { |
|
| 637 | - case 'dates': |
|
| 638 | - if (isset($rows[$locationsFieldsMapping['monthly_dates']])) { |
|
| 639 | - if ($this->validateScheduleMonthlyDates($rows[$locationsFieldsMapping['monthly_dates']])) { |
|
| 640 | - $curSchedule .= '"dates":['.$rows[$locationsFieldsMapping['monthly_dates']].']}'; |
|
| 641 | - } else { |
|
| 642 | - array_push($results['fail'], "$iRow --> Wrong monthly dates"); |
|
| 643 | - } |
|
| 644 | - } |
|
| 645 | - break; |
|
| 646 | - case 'nth': |
|
| 647 | - if (isset($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
|
| 648 | - if ($this->validateScheduleNthN($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
|
| 649 | - $curSchedule .= '"nth":{"n":'.$rows[$locationsFieldsMapping['monthly_nth_n']].','; |
|
| 650 | - } else { |
|
| 651 | - array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_n"); |
|
| 652 | - } |
|
| 653 | - } else { |
|
| 654 | - array_push($results['fail'], "$iRow --> The parameter sched_nth_n is not set"); |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - if ('' != $curSchedule) { |
|
| 658 | - if (isset($rows[$locationsFieldsMapping['monthly_nth_what']])) { |
|
| 659 | - if ($this->validateScheduleNthWhat($rows[$locationsFieldsMapping['monthly_nth_what']])) { |
|
| 660 | - $curSchedule .= '"what":'.$rows[$locationsFieldsMapping['monthly_nth_what']].'}}'; |
|
| 661 | - } else { |
|
| 662 | - array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_what"); |
|
| 663 | - } |
|
| 664 | - } else { |
|
| 665 | - array_push($results['fail'], "$iRow --> The parameter sched_nth_what is not set"); |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - break; |
|
| 669 | - } |
|
| 670 | - } |
|
| 671 | - break; |
|
| 672 | - default: |
|
| 673 | - $curSchedule = ''; |
|
| 674 | - break; |
|
| 675 | - } |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - if (sizeof($results['fail']) > $failCount) { |
|
| 679 | - $curSchedule = ''; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - if (('daily' == $mode || 'weekly' == $mode || 'monthy' == $mode) && '' == $curSchedule) { |
|
| 683 | - ++$iRow; |
|
| 684 | - continue; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - $curSchedule = strtolower($curSchedule); |
|
| 688 | - |
|
| 689 | - $curSchedule = '[{'.$curSchedule.'}]'; |
|
| 690 | - |
|
| 691 | - $parametersArray = []; |
|
| 692 | - |
|
| 693 | - foreach ($addressBookFields as $addressBookField) { |
|
| 694 | - if (isset($locationsFieldsMapping[$addressBookField])) { |
|
| 695 | - $parametersArray[$addressBookField] = $rows[$locationsFieldsMapping[$addressBookField]]; |
|
| 696 | - } |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - $AdressBookLocationParameters = self::fromArray($parametersArray); |
|
| 700 | - |
|
| 701 | - $abContacts = new self(); |
|
| 702 | - |
|
| 703 | - $abcResults = $abContacts->addAdressBookLocation($AdressBookLocationParameters); //temporarry |
|
| 704 | - |
|
| 705 | - array_push( |
|
| 706 | - $results['success'], |
|
| 707 | - 'The schedule location with address_id = '.strval($abcResults['address_id']).' added successfuly.' |
|
| 708 | - ); |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - return $results; |
|
| 712 | - } |
|
| 17 | + /** |
|
| 18 | + * A territory shape name the contact belongs. |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + public $address_id; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * A group the contact belongs. |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + public $address_group; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The contact's alias. |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + public $address_alias; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The geographic address of the contact. |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + public $address_1; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Second geographic address of the contact. |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + public $address_2; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The first name of the contact person. |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + public $first_name; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The last name of the contact person. |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + public $last_name; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The contact's email. |
|
| 61 | + * @var string |
|
| 62 | + */ |
|
| 63 | + public $address_email; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * The contact's phone number. |
|
| 67 | + * @var string |
|
| 68 | + */ |
|
| 69 | + public $address_phone_number; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * A city the contact belongs. |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + public $address_city; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * The ID of the state the contact belongs. |
|
| 79 | + * @var string |
|
| 80 | + */ |
|
| 81 | + public $address_state_id; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * The ID of the country the contact belongs. |
|
| 85 | + * @var string |
|
| 86 | + */ |
|
| 87 | + public $address_country_id; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * The contact's ZIP code. |
|
| 91 | + * @var string |
|
| 92 | + */ |
|
| 93 | + public $address_zip; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * A latitude of the contact's cached position. |
|
| 97 | + * @var double |
|
| 98 | + */ |
|
| 99 | + public $cached_lat; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * A longitude of the contact's cached position. |
|
| 103 | + * @var double |
|
| 104 | + */ |
|
| 105 | + public $cached_lng; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * A latitude of the contact's curbside. |
|
| 109 | + * @var double |
|
| 110 | + */ |
|
| 111 | + public $curbside_lat; |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * A longitude of the contact's curbside. |
|
| 115 | + * @var double |
|
| 116 | + */ |
|
| 117 | + public $curbside_lng; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * The contact's color on the map. |
|
| 121 | + * @var string |
|
| 122 | + */ |
|
| 123 | + public $color; |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * An array of the contact's custom field-value pairs. |
|
| 127 | + * @var array |
|
| 128 | + */ |
|
| 129 | + public $address_custom_data; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * An array of the contact's schedules. |
|
| 133 | + * @var Schedule[] |
|
| 134 | + */ |
|
| 135 | + public $schedule; |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Time when the contact was created. |
|
| 139 | + * @var long |
|
| 140 | + */ |
|
| 141 | + public $created_timestamp; |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Unique ID of the member. |
|
| 145 | + * @var integer |
|
| 146 | + */ |
|
| 147 | + public $member_id; |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * The list of dates that should be omitted from the schedules. |
|
| 151 | + * @var string[] |
|
| 152 | + */ |
|
| 153 | + public $schedule_blacklist; |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Number of the routes containing the contact. |
|
| 157 | + * @var integer |
|
| 158 | + */ |
|
| 159 | + public $in_route_count; |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * When the contact was last visited. |
|
| 163 | + * @var long |
|
| 164 | + */ |
|
| 165 | + public $last_visited_timestamp; |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * When the contact was last routed. |
|
| 169 | + * @var long |
|
| 170 | + */ |
|
| 171 | + public $last_routed_timestamp; |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Start of the contact's local time window. |
|
| 175 | + * @var long |
|
| 176 | + */ |
|
| 177 | + public $local_time_window_start; |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * End of the contact's local time window. |
|
| 181 | + * @var long |
|
| 182 | + */ |
|
| 183 | + public $local_time_window_end; |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Start of the contact's second local time window. |
|
| 187 | + * @var long |
|
| 188 | + */ |
|
| 189 | + public $local_time_window_start_2; |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * End of the contact's second local time window. |
|
| 193 | + * @var long |
|
| 194 | + */ |
|
| 195 | + public $local_time_window_end_2; |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * The service time at the contact's address. |
|
| 199 | + * @var integer |
|
| 200 | + */ |
|
| 201 | + public $service_time; |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * The contact's local timezone. |
|
| 205 | + * @var string |
|
| 206 | + */ |
|
| 207 | + public $local_timezone_string; |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * The contact's icon on the map. |
|
| 211 | + * @var string |
|
| 212 | + */ |
|
| 213 | + public $address_icon; |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * The contact's stop type. |
|
| 217 | + * @var string |
|
| 218 | + */ |
|
| 219 | + public $address_stop_type; |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * The cubic volume of the contact's cargo. |
|
| 223 | + * @var double |
|
| 224 | + */ |
|
| 225 | + public $address_cube; |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * The number of pieces/pallets that this destination/order/line-item consumes/contains on a vehicle. |
|
| 229 | + * @var integer |
|
| 230 | + */ |
|
| 231 | + public $address_pieces; |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * The reference number of the address. |
|
| 235 | + * @var string |
|
| 236 | + */ |
|
| 237 | + public $address_reference_no; |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * The revenue from the contact. |
|
| 241 | + * @var double |
|
| 242 | + */ |
|
| 243 | + public $address_revenue; |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * The weight of the contact's cargo. |
|
| 247 | + * @var double |
|
| 248 | + */ |
|
| 249 | + public $address_weight; |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * If present, the priority will sequence addresses in all the optimal routes so that |
|
| 253 | + * higher priority addresses are general at the beginning of the route sequence.<br> |
|
| 254 | + * 1 is the highest priority, 100000 is the lowest. |
|
| 255 | + * @var integer |
|
| 256 | + */ |
|
| 257 | + public $address_priority; |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * The customer purchase order of the contact. |
|
| 261 | + * @var string |
|
| 262 | + */ |
|
| 263 | + public $address_customer_po; |
|
| 264 | + |
|
| 265 | + public function __construct() |
|
| 266 | + { |
|
| 267 | + Route4Me::setBaseUrl(Endpoint::BASE_URL); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + public static function fromArray(array $params) |
|
| 271 | + { |
|
| 272 | + $addressbooklocation = new self(); |
|
| 273 | + |
|
| 274 | + foreach ($params as $key => $value) { |
|
| 275 | + if (property_exists($addressbooklocation, $key)) { |
|
| 276 | + $addressbooklocation->{$key} = $value; |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + return $addressbooklocation; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * @deprecated 1.2.8 |
|
| 285 | + * @see \Route4Me\V5\AddressBook\AddressBook::getAddressById() |
|
| 286 | + */ |
|
| 287 | + public static function getAddressBookLocation($addressId) |
|
| 288 | + { |
|
| 289 | + $ablocations = Route4Me::makeRequst([ |
|
| 290 | + 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 291 | + 'method' => 'GET', |
|
| 292 | + 'query' => [ |
|
| 293 | + 'query' => $addressId, |
|
| 294 | + 'limit' => 30, |
|
| 295 | + ], |
|
| 296 | + ]); |
|
| 297 | + |
|
| 298 | + return $ablocations; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * @deprecated 1.2.8 |
|
| 303 | + * @see \Route4Me\V5\AddressBook\AddressBook::getAddressesByBodyPayload() |
|
| 304 | + */ |
|
| 305 | + public static function searchAddressBookLocations($params) |
|
| 306 | + { |
|
| 307 | + $allQueryFields = ['display', 'query', 'fields', 'limit', 'offset']; |
|
| 308 | + |
|
| 309 | + $result = Route4Me::makeRequst([ |
|
| 310 | + 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 311 | + 'method' => 'GET', |
|
| 312 | + 'query' => Route4Me::generateRequestParameters($allQueryFields, $params), |
|
| 313 | + ]); |
|
| 314 | + |
|
| 315 | + return $result; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @deprecated 1.2.8 |
|
| 320 | + * @see \Route4Me\V5\AddressBook\AddressBook::getAddressesByIds() |
|
| 321 | + */ |
|
| 322 | + public static function getAddressBookLocations($params) |
|
| 323 | + { |
|
| 324 | + $allQueryFields = ['limit', 'offset', 'address_id']; |
|
| 325 | + |
|
| 326 | + $ablocations = Route4Me::makeRequst([ |
|
| 327 | + 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 328 | + 'method' => 'GET', |
|
| 329 | + 'query' => Route4Me::generateRequestParameters($allQueryFields, $params), |
|
| 330 | + ]); |
|
| 331 | + |
|
| 332 | + return $ablocations; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + public static function getRandomAddressBookLocation($params) |
|
| 336 | + { |
|
| 337 | + $ablocations = self::getAddressBookLocations($params); |
|
| 338 | + |
|
| 339 | + if (isset($ablocations['results'])) { |
|
| 340 | + $locationsSize = sizeof($ablocations['results']); |
|
| 341 | + |
|
| 342 | + if ($locationsSize > 0) { |
|
| 343 | + $randomLocationIndex = rand(0, $locationsSize - 1); |
|
| 344 | + |
|
| 345 | + return $ablocations['results'][$randomLocationIndex]; |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + return null; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * @deprecated 1.2.8 |
|
| 354 | + * @see \Route4Me\V5\AddressBook\AddressBook::addAddress() |
|
| 355 | + * |
|
| 356 | + * @param AddressBookLocation $params |
|
| 357 | + */ |
|
| 358 | + public static function addAdressBookLocation($params) |
|
| 359 | + { |
|
| 360 | + $allBodyFields = Route4Me::getObjectProperties(new self(), ['address_id', 'in_route_count']); |
|
| 361 | + |
|
| 362 | + $response = Route4Me::makeRequst([ |
|
| 363 | + 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 364 | + 'method' => 'POST', |
|
| 365 | + 'body' => Route4Me::generateRequestParameters($allBodyFields, $params), |
|
| 366 | + ]); |
|
| 367 | + |
|
| 368 | + return $response; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * @deprecated 1.2.8 |
|
| 373 | + * @see \Route4Me\V5\AddressBook\AddressBook::deleteAddressesByIds() |
|
| 374 | + */ |
|
| 375 | + public function deleteAdressBookLocation($address_ids) |
|
| 376 | + { |
|
| 377 | + $result = Route4Me::makeRequst([ |
|
| 378 | + 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 379 | + 'method' => 'DELETEARRAY', |
|
| 380 | + 'query' => [ |
|
| 381 | + 'address_ids' => $address_ids, |
|
| 382 | + ], |
|
| 383 | + ]); |
|
| 384 | + |
|
| 385 | + return $result; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * @deprecated 1.2.8 |
|
| 390 | + * @see \Route4Me\V5\AddressBook\AddressBook::updateAddressById() |
|
| 391 | + */ |
|
| 392 | + public function updateAddressBookLocation($params) |
|
| 393 | + { |
|
| 394 | + $allBodyFields = Route4Me::getObjectProperties(new self(), ['in_route_count']); |
|
| 395 | + |
|
| 396 | + $response = Route4Me::makeRequst([ |
|
| 397 | + 'url' => Endpoint::ADDRESS_BOOK_V4, |
|
| 398 | + 'method' => 'PUT', |
|
| 399 | + 'body' => Route4Me::generateRequestParameters($allBodyFields, $params), |
|
| 400 | + ]); |
|
| 401 | + |
|
| 402 | + return $response; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + public static function validateScheduleMode($scheduleMode) |
|
| 406 | + { |
|
| 407 | + $schedModes = ['daily', 'weekly', 'monthly', 'annually']; |
|
| 408 | + |
|
| 409 | + if (in_array($scheduleMode, $schedModes)) { |
|
| 410 | + return true; |
|
| 411 | + } else { |
|
| 412 | + return false; |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + public static function validateScheduleEnable($scheduleEnabled) |
|
| 417 | + { |
|
| 418 | + if (is_string($scheduleEnabled)) { |
|
| 419 | + if (strtolower($scheduleEnabled)=="true") $scheduleEnabled = true; |
|
| 420 | + if (strtolower($scheduleEnabled)=="false") $scheduleEnabled = false; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + $schedEnables = [true, false,]; |
|
| 424 | + |
|
| 425 | + if (in_array($scheduleEnabled, $schedEnables,true)) { |
|
| 426 | + return true; |
|
| 427 | + } else { |
|
| 428 | + return false; |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + public static function validateScheduleEvery($scheduleEvery) |
|
| 433 | + { |
|
| 434 | + if (is_numeric($scheduleEvery)) { |
|
| 435 | + if ($scheduleEvery>0) { |
|
| 436 | + return true; |
|
| 437 | + } else { |
|
| 438 | + return false; |
|
| 439 | + } |
|
| 440 | + } else { |
|
| 441 | + return false; |
|
| 442 | + } |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + public static function validateScheduleWeekDays($scheduleWeekDays) |
|
| 446 | + { |
|
| 447 | + if (is_bool($scheduleWeekDays)) return false; |
|
| 448 | + |
|
| 449 | + $weekdays = explode(',', $scheduleWeekDays); |
|
| 450 | + $weekdaysSize = sizeof($weekdays); |
|
| 451 | + |
|
| 452 | + if ($weekdaysSize < 1) { |
|
| 453 | + return false; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + $isValid = true; |
|
| 457 | + |
|
| 458 | + for ($i = 0; $i < $weekdaysSize; ++$i) { |
|
| 459 | + if (is_bool($weekdays[$i])) { |
|
| 460 | + $isValid = false; |
|
| 461 | + } elseif (is_numeric($weekdays[$i])) { |
|
| 462 | + $wday = intval($weekdays[$i]); |
|
| 463 | + if ($wday < 1 || $wday > 7) { |
|
| 464 | + $isValid = false; |
|
| 465 | + } |
|
| 466 | + } else { |
|
| 467 | + $isValid = false; |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + return $isValid; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + public static function validateScheduleMonthlyMode($scheduleMonthlyMode) |
|
| 475 | + { |
|
| 476 | + $schedMonthlyMmodes = ['dates', 'nth']; |
|
| 477 | + |
|
| 478 | + if (in_array($scheduleMonthlyMode, $schedMonthlyMmodes,true)) { |
|
| 479 | + return true; |
|
| 480 | + } else { |
|
| 481 | + return false; |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + public static function validateScheduleMonthlyDates($scheduleMonthlyDates) |
|
| 486 | + { |
|
| 487 | + if (is_bool($scheduleMonthlyDates)) return false; |
|
| 488 | + |
|
| 489 | + $monthlyDates = explode(',', $scheduleMonthlyDates); |
|
| 490 | + $monthlyDatesSize = sizeof($monthlyDates); |
|
| 491 | + |
|
| 492 | + if ($monthlyDatesSize < 1) { |
|
| 493 | + return false; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + $isValid = true; |
|
| 497 | + |
|
| 498 | + for ($i = 0; $i < $monthlyDatesSize; ++$i) { |
|
| 499 | + if (is_numeric($monthlyDates[$i])) { |
|
| 500 | + $mday = intval($monthlyDates[$i]); |
|
| 501 | + if ($mday < 1 || $mday > 31) { |
|
| 502 | + $isValid = false; |
|
| 503 | + } |
|
| 504 | + } else { |
|
| 505 | + $isValid = false; |
|
| 506 | + } |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + return $isValid; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + public static function validateScheduleNthN($scheduleNthN) |
|
| 513 | + { |
|
| 514 | + if (!is_numeric($scheduleNthN)) { |
|
| 515 | + return false; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + $schedNthNs = [1, 2, 3, 4, 5, -1]; |
|
| 519 | + |
|
| 520 | + if (in_array($scheduleNthN, $schedNthNs)) { |
|
| 521 | + return true; |
|
| 522 | + } else { |
|
| 523 | + return false; |
|
| 524 | + } |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + public static function validateScheduleNthWhat($scheduleNthWhat) |
|
| 528 | + { |
|
| 529 | + if (!is_numeric($scheduleNthWhat)) { |
|
| 530 | + return false; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + $schedNthWhats = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
|
| 534 | + |
|
| 535 | + if (in_array($scheduleNthWhat, $schedNthWhats)) { |
|
| 536 | + return true; |
|
| 537 | + } else { |
|
| 538 | + return false; |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** Function adds the locations (with/without schedule) from the CSV file. |
|
| 543 | + * $csvFileHandle - a file handler. |
|
| 544 | + * Returns array $results which contains two arrays: fail and succes. |
|
| 545 | + */ |
|
| 546 | + public function addLocationsFromCsvFile($csvFileHandle, $locationsFieldsMapping) |
|
| 547 | + { |
|
| 548 | + $max_line_length = 512; |
|
| 549 | + $delemietr = ','; |
|
| 550 | + |
|
| 551 | + $results = []; |
|
| 552 | + $results['fail'] = []; |
|
| 553 | + $results['success'] = []; |
|
| 554 | + |
|
| 555 | + $columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr); |
|
| 556 | + |
|
| 557 | + $addressBookFields = Route4Me::getObjectProperties(new self(), ['address_id', 'in_route_count']); |
|
| 558 | + |
|
| 559 | + if (empty($columns)) { |
|
| 560 | + array_push($results['fail'], 'Empty CSV table'); |
|
| 561 | + |
|
| 562 | + return $results; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + $iRow = 1; |
|
| 566 | + |
|
| 567 | + while (false !== ($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr))) { |
|
| 568 | + if (!isset($rows[$locationsFieldsMapping['cached_lat']]) || !isset($rows[$locationsFieldsMapping['cached_lng']]) |
|
| 569 | + || !isset($rows[$locationsFieldsMapping['address_1']]) || [null] == $rows) { |
|
| 570 | + continue; |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + $curSchedule = ''; |
|
| 574 | + $mode = ''; |
|
| 575 | + |
|
| 576 | + $failCount = sizeof($results['fail']); |
|
| 577 | + |
|
| 578 | + if (isset($rows[$locationsFieldsMapping['schedule_mode']])) { |
|
| 579 | + if ($this->validateScheduleMode($rows[$locationsFieldsMapping['schedule_mode']])) { |
|
| 580 | + $curSchedule = '"mode":"'.$rows[$locationsFieldsMapping['schedule_mode']].'",'; |
|
| 581 | + $mode = $rows[$locationsFieldsMapping['schedule_mode']]; |
|
| 582 | + } else { |
|
| 583 | + array_push($results['fail'], "$iRow --> Wrong schedule mode parameter"); |
|
| 584 | + } |
|
| 585 | + } else { |
|
| 586 | + array_push($results['fail'], "$iRow --> The schedule mode parameter is not set"); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + if (isset($rows[$locationsFieldsMapping['schedule_enabled']])) { |
|
| 590 | + if ($this->validateScheduleEnable($rows[$locationsFieldsMapping['schedule_enabled']])) { |
|
| 591 | + $curSchedule .= '"enabled":'.$rows[$locationsFieldsMapping['schedule_enabled']].','; |
|
| 592 | + } else { |
|
| 593 | + array_push($results['fail'], "$iRow --> The schedule enabled parameter is not set "); |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + if (isset($rows[$locationsFieldsMapping['schedule_every']])) { |
|
| 598 | + if ($this->validateScheduleEvery($rows[$locationsFieldsMapping['schedule_every']])) { |
|
| 599 | + $curSchedule .= '"'.$mode.'":{'.'"every":'.$rows[$locationsFieldsMapping['schedule_every']].','; |
|
| 600 | + if ('daily' == $mode) { |
|
| 601 | + $curSchedule = trim($curSchedule, ','); |
|
| 602 | + $curSchedule .= '}'; |
|
| 603 | + } |
|
| 604 | + } else { |
|
| 605 | + array_push($results['fail'], "$iRow --> The parameter sched_every is not set"); |
|
| 606 | + } |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + if ('daily' != $mode) { |
|
| 610 | + switch ($mode) { |
|
| 611 | + case 'weekly': |
|
| 612 | + if (isset($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
|
| 613 | + if ($this->validateScheduleWeekDays($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
|
| 614 | + $curSchedule .= '"weekdays":['.$rows[$locationsFieldsMapping['schedule_weekdays']].']}'; |
|
| 615 | + } else { |
|
| 616 | + array_push($results['fail'], "$iRow --> Wrong weekdays"); |
|
| 617 | + } |
|
| 618 | + } else { |
|
| 619 | + array_push($results['fail'], "$iRow --> The parameters sched_weekdays is not set"); |
|
| 620 | + } |
|
| 621 | + break; |
|
| 622 | + case 'monthly': |
|
| 623 | + $monthlyMode = ''; |
|
| 624 | + if (isset($rows[$locationsFieldsMapping['monthly_mode']])) { |
|
| 625 | + if ($this->validateScheduleMonthlyMode($rows[$locationsFieldsMapping['monthly_mode']])) { |
|
| 626 | + $monthlyMode = $rows[$locationsFieldsMapping['monthly_mode']]; |
|
| 627 | + $curSchedule .= '"mode": "'.$rows[$locationsFieldsMapping['monthly_mode']].'",'; |
|
| 628 | + } else { |
|
| 629 | + array_push($results['fail'], "$iRow --> Wrong monthly mode"); |
|
| 630 | + } |
|
| 631 | + } else { |
|
| 632 | + array_push($results['fail'], "$iRow --> The parameter sched_monthly_mode is not set"); |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + if ('' != $monthlyMode) { |
|
| 636 | + switch ($monthlyMode) { |
|
| 637 | + case 'dates': |
|
| 638 | + if (isset($rows[$locationsFieldsMapping['monthly_dates']])) { |
|
| 639 | + if ($this->validateScheduleMonthlyDates($rows[$locationsFieldsMapping['monthly_dates']])) { |
|
| 640 | + $curSchedule .= '"dates":['.$rows[$locationsFieldsMapping['monthly_dates']].']}'; |
|
| 641 | + } else { |
|
| 642 | + array_push($results['fail'], "$iRow --> Wrong monthly dates"); |
|
| 643 | + } |
|
| 644 | + } |
|
| 645 | + break; |
|
| 646 | + case 'nth': |
|
| 647 | + if (isset($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
|
| 648 | + if ($this->validateScheduleNthN($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
|
| 649 | + $curSchedule .= '"nth":{"n":'.$rows[$locationsFieldsMapping['monthly_nth_n']].','; |
|
| 650 | + } else { |
|
| 651 | + array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_n"); |
|
| 652 | + } |
|
| 653 | + } else { |
|
| 654 | + array_push($results['fail'], "$iRow --> The parameter sched_nth_n is not set"); |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + if ('' != $curSchedule) { |
|
| 658 | + if (isset($rows[$locationsFieldsMapping['monthly_nth_what']])) { |
|
| 659 | + if ($this->validateScheduleNthWhat($rows[$locationsFieldsMapping['monthly_nth_what']])) { |
|
| 660 | + $curSchedule .= '"what":'.$rows[$locationsFieldsMapping['monthly_nth_what']].'}}'; |
|
| 661 | + } else { |
|
| 662 | + array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_what"); |
|
| 663 | + } |
|
| 664 | + } else { |
|
| 665 | + array_push($results['fail'], "$iRow --> The parameter sched_nth_what is not set"); |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + break; |
|
| 669 | + } |
|
| 670 | + } |
|
| 671 | + break; |
|
| 672 | + default: |
|
| 673 | + $curSchedule = ''; |
|
| 674 | + break; |
|
| 675 | + } |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + if (sizeof($results['fail']) > $failCount) { |
|
| 679 | + $curSchedule = ''; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + if (('daily' == $mode || 'weekly' == $mode || 'monthy' == $mode) && '' == $curSchedule) { |
|
| 683 | + ++$iRow; |
|
| 684 | + continue; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + $curSchedule = strtolower($curSchedule); |
|
| 688 | + |
|
| 689 | + $curSchedule = '[{'.$curSchedule.'}]'; |
|
| 690 | + |
|
| 691 | + $parametersArray = []; |
|
| 692 | + |
|
| 693 | + foreach ($addressBookFields as $addressBookField) { |
|
| 694 | + if (isset($locationsFieldsMapping[$addressBookField])) { |
|
| 695 | + $parametersArray[$addressBookField] = $rows[$locationsFieldsMapping[$addressBookField]]; |
|
| 696 | + } |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + $AdressBookLocationParameters = self::fromArray($parametersArray); |
|
| 700 | + |
|
| 701 | + $abContacts = new self(); |
|
| 702 | + |
|
| 703 | + $abcResults = $abContacts->addAdressBookLocation($AdressBookLocationParameters); //temporarry |
|
| 704 | + |
|
| 705 | + array_push( |
|
| 706 | + $results['success'], |
|
| 707 | + 'The schedule location with address_id = '.strval($abcResults['address_id']).' added successfuly.' |
|
| 708 | + ); |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + return $results; |
|
| 712 | + } |
|
| 713 | 713 | } |