-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoccurrence_comment_quick_reply_page.php
More file actions
1274 lines (1184 loc) · 39.7 KB
/
occurrence_comment_quick_reply_page.php
File metadata and controls
1274 lines (1184 loc) · 39.7 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Post a comment</title>
<link href="vendor-other/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="vendor-other/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css" />
<link href="modules/rest_api/media/css/rest_api.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<?php
if ((empty($_GET['user_id']) && empty($_GET['email_address'])) || empty($_GET['occurrence_id']) || empty($_GET['auth'])) {
echo '<p>Invalid link</p>';
}
else {
require_once 'client_helpers/data_entry_helper.php';
// Request the saved authorisation number
// from the database for this particular occurrence.
$auth = occcurrenceCommentQuickReplyPage::getAuth('Indicia');
$tokenDbData = data_entry_helper::get_population_data(array(
'table' => 'comment_quick_reply_page_auth',
'caching' => FALSE,
'extraParams' => $auth['read'] + [
'occurrence_id' => $_GET['occurrence_id'],
'token' => $_GET['auth'],
],
'nocache' => TRUE,
));
if (count($tokenDbData) !== 1) {
echo '<p>Invalid link</p>';
}
else {
$tokenDetails = $tokenDbData[0];
$configuration = occcurrenceCommentQuickReplyPage::getPageConfiguration();
// If there is a POST, then the user has saved, so process this.
if (!empty($_POST)) {
$response = occcurrenceCommentQuickReplyPage::buildSubmission($configuration, $tokenDetails['id']);
$decodedResponse = json_decode($response);
if (isset($decodedResponse->error)) {
echo 'Error occurred';
?><h2>A problem seems to have occurred, the response from the server is as follows:</h2><?php
echo print_r($response, TRUE);
?><br><form><input type=button value="Return To Record Comments Screen" onClick="window.location = document.URL;"></form><?php
}
else {
?>
<div class="container">
<h2>Thank You</h2>
<p>Your comment has been saved successfully.</p>
<p>You posted the following comment:</p>
<blockquote><?php
$commentText = $_POST['comment-text'];
echo $commentText;
?><blockquote>
</div>
<br>
<?php
}
}
else {
// The authorisation number must exist and
// also match the one in the database if we are going to continue.
if (empty($tokenDetails['token']) || $_GET['auth'] !== $tokenDetails['token']) {
var_export($tokenDetails);
echo '<p>Authorisation failed. It may be that the link has already been used.</p>';
}
else {
// Get the record details if we are going to display the page and
// then pass this to the functions.
$auth = occcurrenceCommentQuickReplyPage::getAuth($configuration['privateKey']);
$occurrenceDetails = data_entry_helper::get_population_data(array(
'table' => 'occurrence',
'caching' => FALSE,
'extraParams' => $auth['read'] + array('view' => 'cache', 'id' => $_GET['occurrence_id']),
));
if (count($occurrenceDetails) === 0) {
echo '<em style="color:red">The record associated with this link cannot be found.</em><br>';
}
else {
$thisRecord = $occurrenceDetails[0];
echo '<h1>Record details and comments</h1>';
if ($thisRecord['confidential'] === 't') {
echo '<em style="color:red">This record is marked as confidential so the details are unavailable. You can still comment using the form below.</em><br>';
}
else {
if ($thisRecord['query'] !== 'Q') {
echo '<em style="color:red">This record no longer has a queried status and therefore doesn\'t require you to make a comment at this present time.</em><br>';
}
echo occcurrenceCommentQuickReplyPage::displayOccurrenceDetails($configuration, $thisRecord);
echo occcurrenceCommentQuickReplyPage::displayExistingOccurrenceComments($configuration);
}
echo occcurrenceCommentQuickReplyPage::commentForm($thisRecord['query']);
}
}
}
}
}
/**
* Allow a reply to an occurrence comment after user receives an email.
*/
class OcccurrenceCommentQuickReplyPage {
/**
* Store various information.
*
* (such as paths to things like CSS files in re-usable variables).
*/
public static function getPageConfiguration() {
$privateKey = 'Indicia';
$auth = self::getAuth($privateKey);
if (!empty($_GET['user_id'])) {
$userDetails = data_entry_helper::get_population_data(array(
'table' => 'user',
'extraParams' => $auth['read'] + array('id' => $_GET['user_id']),
));
$configuration['username'] = $userDetails[0]['username'];
}
$configuration['privateKey'] = $privateKey;
$configuration['dataEntryHelperPath'] = 'client_helpers/data_entry_helper.php';
if (!empty($_GET['person_name'])) {
$configuration['person_name'] = $_GET['person_name'];
}
if (!empty($_GET['email_address'])) {
$configuration['email_address'] = $_GET['email_address'];
}
return $configuration;
}
/**
* Display the details of the occurrence.
*/
public static function displayOccurrenceDetails($configuration, $thisRecord) {
require_once $configuration['dataEntryHelperPath'];
?>
<fieldset class="fieldset-auto-width"><legend>Details</legend>
<?php
echo "<p>Species: " . $thisRecord['taxon'] . "</p>";
$vagueDate = self::vagueDateToString(array(
$thisRecord['date_start'],
$thisRecord['date_end'],
$thisRecord['date_type'],
));
echo '<p>Date: ' . $vagueDate . "</p>";
// Needs blurred output as don't know user's rights.
if (!empty($thisRecord['public_entered_sref'])) {
$srefData = $thisRecord['public_entered_sref'] . ' (' . $thisRecord['entered_sref_system'] . ')';
}
else {
// Note: Get population data not returning output_sref_system at moment,
// hence added check for this in case this changes.
if (!empty($thisRecord['output_sref_system'])) {
$srefData = "$thisRecord[output_sref] ($thisRecord[output_sref_system])";
}
else {
$srefData = $thisRecord['output_sref'];
}
}
echo "<p>Spatial reference: " . $srefData . "</p>";
echo "</fieldset>\n";
}
/**
* Display details of the occurrence from the database.
*/
public static function displayExistingOccurrenceComments($configuration) {
$configuration = self::getPageConfiguration();
$auth = self::getAuth($configuration['privateKey']);
$r = '<div>';
$comments = data_entry_helper::get_population_data(array(
'table' => 'occurrence_comment',
'caching' => FALSE,
'extraParams' => $auth['read'] + array(
'occurrence_id' => $_GET['occurrence_id'],
'sortdir' => 'DESC',
'orderby' => 'updated_on',
),
));
$r .= '<div id="comment-list">';
if (count($comments) === 0) {
$r .= '<p id="no-comments">No comments have been made.</p>';
}
else {
foreach ($comments as $comment) {
$r .= '<div class="comment">';
$r .= '<div class="header">';
$r .= "<strong>$comment[person_name]</strong> ";
$commentTime = strtotime($comment['updated_on']);
// Output the comment time.
// Skip if in future (i.e. server/client date settings don't match).
if ($commentTime < time()) {
$r .= self::ago($commentTime);
}
$r .= '</div>';
$c = str_replace("\n", '<br/>', $comment['comment']);
$r .= "<div>$c</div>";
$r .= '</div><br>';
}
}
$r .= '</div>';
echo '<div class="detail-panel" id="detail-panel-comments"><h3>Comments</h3>' . $r . '</div>';
}
/**
* Returns the HTML for the comment form,
*
* @param string $recordQueriedFlag
* Query status flag for the record.
*
* @return string
* HTML for the form.
*/
public static function commentForm($recordQueriedFlag) {
// Only allow commenting for queried records.
if ($recordQueriedFlag === 'Q') {
return <<<HTML
<form id="comment-form" method="POST" >
<fieldset>
<div class="form-group">
<label for="comment-text">Add new comment</label>
<textarea id="comment-text" name="comment-text" class="form-control"></textarea>
</div>
<div class="alert alert-info">Comments will be added to the record on iRecord, and are publicly visible - please do
not include personal information such as addresses or phone numbers.</div>
<input type="button" class="btn btn-primary" value="Save" onclick="
if (document.getElementById('comment-text').value) {
var r = confirm('Are you sure you want to save the comment?');
if (r == true) {
document.getElementById('comment-form').submit();
}
} else {
alert('Please enter a comment before saving');
}">
</fieldset>
</form>
HTML;
}
return '';
}
/**
* Get an authentication.
*/
public static function getAuth($password) {
if (!empty($_GET['user_id'])) {
$userId = $_GET['user_id'];
}
else {
$userId = 1;
}
$website_id = 0 - $userId;
$postargs = "website_id=$website_id";
$response = self::httpPost(self::getWarehouseUrl() . 'index.php/services/security/get_read_write_nonces', $postargs);
$nonces = json_decode($response, TRUE);
return array(
'read' => array(
'auth_token' => sha1("$nonces[read]:" . $password),
'nonce' => $nonces['read'],
),
'write' => array(
'auth_token' => sha1("$nonces[write]:" . $password),
'nonce' => $nonces['write'],
),
);
}
/**
* Need the warehouse url for various functions.
*/
private static function getWarehouseUrl() {
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$host = rtrim($_SERVER['HTTP_HOST'], '/');
return $protocol . $host . '/' . trim(str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])), '/') . '/';
}
/**
* Allow us to POST a submission.
*/
private static function httpPost($url, $postargs = NULL) {
$session = curl_init();
// Set the POST options.
curl_setopt($session, CURLOPT_URL, $url);
if ($postargs !== NULL) {
curl_setopt($session, CURLOPT_POST, TRUE);
curl_setopt($session, CURLOPT_POSTFIELDS, $postargs);
}
curl_setopt($session, CURLOPT_HEADER, FALSE);
curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
// Do the POST.
$response = curl_exec($session);
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
// Check for an error, or check if the http response was not OK.
if (curl_errno($session) || $httpCode !== 200) {
if (curl_errno($session)) {
throw new exception(curl_errno($session) . ' - ' . curl_error($session));
}
else {
throw new exception($httpCode . ' - ' . $response);
}
}
curl_close($session);
return $response;
}
/**
* Convert a timestamp into readable format for use on a comment list.
*
* @param int $timestamp
* The date time to convert.
*
* @return string
* The output string.
*/
public static function ago($timestamp) {
$difference = time() - $timestamp;
$periods = [
lang::get('{1} second ago'),
lang::get('{1} minute ago'),
lang::get('{1} hour ago'),
lang::get('Yesterday'),
lang::get('{1} week ago'),
lang::get('{1} month ago'),
lang::get('{1} year ago'),
lang::get('{1} decade ago'),
];
$periodsPlural = [
lang::get('{1} seconds ago'),
lang::get('{1} minutes ago'),
lang::get('{1} hours ago'),
lang::get('{1} days ago'),
lang::get('{1} weeks ago'),
lang::get('{1} months ago'),
lang::get('{1} years ago'),
lang::get('{1} decades ago'),
];
$lengths = ['60', '60', '24', '7', '4.35', '12', '10'];
for ($j = 0; $difference >= $lengths[$j]; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if ($difference == 1) {
$text = str_replace('{1}', $difference, $periods[$j]);
}
else {
$text = str_replace('{1}', $difference, $periodsPlural[$j]);
}
return $text;
}
/**
* Create data structure to submit when user saves.
*/
public static function buildSubmission($configuration, $authIdToDelete) {
$submission = array();
$submission['id'] = 'occurrence_comment';
if (!empty($_GET['user_id'])) {
$submission['fields']['created_by_id']['value'] = $_GET['user_id'];
$submission['fields']['updated_by_id']['value'] = $_GET['user_id'];
}
else {
// If we don't know who the updater is, just use the admin account.
$submission['fields']['created_by_id']['value'] = 1;
$submission['fields']['updated_by_id']['value'] = 1;
}
$submission['fields']['occurrence_id']['value'] = $_GET['occurrence_id'];
$submission['fields']['comment']['value'] = $_POST['comment-text'];
if (!empty($configuration['username'])) {
$submission['fields']['person_name']['value'] = $configuration['username'];
}
// If a specific person_name is supplied,
// then always use this (e.g. for anonymous users).
if (!empty($configuration['person_name'])) {
$submission['fields']['person_name']['value'] = $configuration['person_name'];
}
if (!empty($configuration['email_address'])) {
$submission['fields']['email_address']['value'] = $configuration['email_address'];
}
// Add main submission to the list.
$submission['submission_list']['entries'][0] = $submission;
// Delete the authorisation from the database as well during submission.
$submission['submission_list']['entries'][1]['id'] = 'comment_quick_reply_page_auth';
$submission['submission_list']['entries'][1]['fields']['id']['value'] = $authIdToDelete;
$submission['submission_list']['entries'][1]['fields']['deleted']['value'] = 't';
$response = self::doSubmission('save', $submission);
return $response;
}
/**
* Take the submission structure and give it to data services.
*/
private static function doSubmission($entity, $submission = NULL, $writeTokens = NULL) {
$configuration = self::getPageConfiguration();
$auth = self::getAuth($configuration['privateKey']);
$writeTokens = $auth['write'];
$request = self::getWarehouseUrl() . "index.php/services/data/$entity";
$postargs = 'submission=' . urlencode(json_encode($submission));
// Passthrough the authentication tokens as POST data.
// Use parameter writeTokens.
foreach ($writeTokens as $token => $value) {
$postargs .= '&' . $token . '=' . ($value === TRUE ? 'true' : ($value === FALSE ? 'false' : $value));
}
if (!empty($_GET['user_id'])) {
$postargs .= '&user_id=' . $_GET['user_id'];
}
$response = self::httpPost($request, $postargs);
return $response;
}
/**
* List of regex strings used to try to capture date ranges.
*/
private static function dateRangeStrings() {
return array(
array(
// Date to date or date - date.
'regex' => '/(?P<sep> to | - )/i',
'start' => -1,
'end' => 1,
),
array(
// dd/mm/yy(yy)-dd/mm/yy(yy) or dd.mm.yy(yy)-dd.mm.yy(yy)
'regex' => '/^\d{2}[\/\.]\d{2}[\/\.]\d{2}(\d{2})?(?P<sep>-)\d{2}[\/\.]\d{2}[\/\.]\d{2}(\d{2})?$/',
'start' => -1,
'end' => 1,
),
array(
// mm/yy(yy)-mm/yy(yy) or mm.yy(yy)-mm.yy(yy)
'regex' => '/^\d{2}[\/\.]\d{2}(\d{2})?(?P<sep>-)\d{2}[\/\.]\d{2}(\d{2})?$/',
'start' => -1,
'end' => 1,
),
array(
// yyyy-yyyy.
'regex' => '/^\d{4}(?P<sep>-)\d{4}$/',
'start' => -1,
'end' => 1,
),
array(
// Century to century.
'regex' => '/^\d{2}c-\d{2}c?$/',
'start' => -1,
'end' => 1,
),
array(
'regex' => '/^(?P<sep>to|pre|before[\.]?)/i',
'start' => 0,
'end' => 1,
),
array(
'regex' => '/(?P<sep>from|after)/i',
'start' => 1,
'end' => 0,
),
array(
'regex' => '/(?P<sep>-)$/',
'start' => -1,
'end' => 0,
),
array(
'regex' => '/^(?P<sep>-)/',
'start' => 0,
'end' => 1,
),
);
}
/**
* Array of formats used to parse a string looking for a single day.
*
* Uses the strptime() function.
*
* See http://uk2.php.net/manual/en/function.strptime.php.
*/
private static function singleDayFormats() {
return array(
'%Y-%m-%d',
'%d/%m/%Y',
'%d/%m/%y',
'%d.%m.%Y',
'%d.%m.%y',
'%A %e %B %Y',
'%a %e %B %Y',
'%A %e %b %Y',
'%a %e %b %Y',
'%A %e %B %y',
'%a %e %B %y',
'%A %e %b %y',
'%a %e %b %y',
'%A %e %B',
'%a %e %B',
'%A %e %b',
'%a %e %b',
'%e %B %Y',
'%e %b %Y',
'%e %B %y',
'%e %b %y',
'%m/%d/%y',
);
}
/**
* Used to parse a string looking for a single month in a year.
*
* Uses the strptime() function
* See http://uk2.php.net/manual/en/function.strptime.php.
*/
private static function singleMonthInYearFormats() {
return array(
'%Y-%m',
'%m/%Y',
'%m/%y',
'%B %Y',
'%b %Y',
'%B %y',
'%b %y',
);
}
/**
* Format single month.
*/
private static function singleMonthFormats() {
return array(
'%B',
'%b',
);
}
/**
* Format single year.
*/
private static function singleYearFormats() {
return array(
'%Y',
'%y',
);
}
/**
* Format year with season.
*/
private static function seasonInYearFormats() {
return array(
'%K %Y',
'%K %y',
);
}
/**
* Format season.
*/
private static function seasonFormats() {
return array(
'%K',
);
}
/**
* Format century.
*/
private static function centuryFormats() {
return array(
'%C',
);
}
/**
* Convert a vague date in the form of array(start, end, type) to a string.
*
* @param array $date
* Vague date in the form array(start_date, end_date, date_type),
* where start_date and end_date are DateTime objects or strings.
*
* @return string
* Vague date expressed as a string.
*/
public static function vagueDateToString(array $date) {
$start = empty($date[0]) ? NULL : $date[0];
$end = empty($date[1]) ? NULL : $date[1];
$type = $date[2];
if (is_string($start)) {
$start = DateTime::createFromFormat('d/m/Y', $date[0]);
if (!$start) {
// If not in warehouse default date format,
// allow PHP standard processing.
$start = new DateTime($date[0]);
}
}
if (is_string($end)) {
$end = DateTime::createFromFormat('d/m/Y', $date[1]);
if (!$end) {
// If not in warehouse default date format,
// allow PHP standard processing.
$end = new DateTime($date[1]);
}
}
self::validate($start, $end, $type);
switch ($type) {
case 'D':
return self::vagueDateToDay($start, $end);
case 'DD':
return self::vagueDateToDays($start, $end);
case 'O':
return self::vagueDateToMonthInYear($start, $end);
case 'OO':
return self::vagueDateToMonthsInYear($start, $end);
case 'P':
return self::vagueDateToSeasonInYear($start, $end);
case 'Y':
return self::vagueDateToYear($start, $end);
case 'YY':
return self::vagueDateToYears($start, $end);
case 'Y-':
return self::vagueDateToYearFrom($start, $end);
case '-Y':
return self::vagueDateToYearTo($start, $end);
case 'M':
return self::vagueDateToMonth($start, $end);
case 'S':
return self::vagueDateToSeason($start, $end);
case 'U':
return self::vagueDateToUnknown($start, $end);
case 'C':
return self::vagueDateToCentury($start, $end);
case 'CC':
return self::vagueDateToCenturies($start, $end);
case 'C-':
return self::vagueDateToCenturyFrom($start, $end);
case '-C':
return self::vagueDateToCenturyTo($start, $end);
}
throw new exception("Invalid date type $type");
}
/**
* Convert a string into a vague date.
*
* Returns an array with 3 entries, the start date, end date and date type.
*/
public static function stringToVagueDate($string) {
$parseFormats = array_merge(
self::singleDayFormats(),
self::singleMonthInYearFormats(),
self::singleMonthFormats(),
self::seasonInYearFormats(),
self::seasonFormats(),
self::centuryFormats(),
self::singleYearFormats()
);
/* Our approach shall be to gradually pare down
from the most complex possible dates to the simplest, and match as fast as
possible to try to grab the most information.
First we consider the potential ways that a range may be represented. */
$range = FALSE;
$startDate = FALSE;
$endDate = FALSE;
$matched = FALSE;
foreach (self::dateRangeStrings() as $a) {
if (preg_match($a['regex'], $string, $regs) != FALSE) {
switch ($a['start']) {
case -1:
$start = trim(substr($string, 0, strpos($string, $regs['sep'])));
break;
case 1:
$start = trim(substr($string, strpos($string, $regs['sep']) + strlen($regs['sep'])));
break;
default:
$start = FALSE;
}
switch ($a['end']) {
case -1:
$end = trim(substr($string, 0, strpos($string, $regs['sep'])));
break;
case 1:
$end = trim(substr($string, strpos($string, $regs['sep']) + strlen($regs['sep'])));
break;
default:
$end = FALSE;
}
$range = TRUE;
break;
}
}
if (!$range) {
$a = self::parseSingleDate($string, $parseFormats);
if ($a) {
$startDate = $endDate = $a;
$matched = TRUE;
}
}
else {
if ($start) {
$a = self::parseSingleDate($start, $parseFormats);
if ($a !== NULL) {
$startDate = $a;
$matched = TRUE;
}
}
if ($end) {
$a = self::parseSingleDate($end, $parseFormats);
if ($a !== NULL) {
$endDate = $a;
$matched = TRUE;
}
}
if ($matched) {
if ($start && !$end) {
$endDate = $startDate;
}
elseif ($end && !$start) {
$startDate = $endDate;
}
}
}
if (!$matched) {
if (trim($string) == 'U' || trim($string) == Kohana::lang('dates.unknown')) {
return array(NULL, NULL, 'U');
}
else {
return FALSE;
}
}
try {
if ($endDate->tm_season !== NULL) {
// We're a season. That means we could be P (if we have a year) or
// S (if we don't).
if ($endDate->tm_year !== NULL) {
// We're a P.
$vagueDate = array(
$endDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'P',
);
return $vagueDate;
}
else {
// No year, so we're an S.
$vagueDate = array(
$endDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'S',
);
return $vagueDate;
}
}
// Do we have day precision?
if ($endDate->tm_mday !== NULL) {
if (!$range) {
// We're a D.
$vagueDate = array(
$endDate->getIsoDate(),
$endDate->getIsoDate(),
'D',
);
return $vagueDate;
}
else {
// Type is DD. We copy across any data not set in the
// start date.
if ($startDate->getPrecision() == $endDate->getPrecision()) {
$vagueDate = array(
$startDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'DD',
);
}
else {
// Less precision in the start date -
// try and massage them together.
return FALSE;
}
return $vagueDate;
}
}
/* Right, scratch the possibility of days. Months are next - there are
* various possibilities with months,
* because months don't necessarily have years. Months can be:
* Type 'O' - month, year, !range
* Type 'OO' - month, year, range
* Type 'M' - month, !range
*
*/
if ($endDate->tm_mon !== NULL) {
if (!$range) {
// Either a month in a year or just a month.
if ($endDate->tm_year !== NULL) {
// Then we have a month in a year- type O.
$vagueDate = array(
$endDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'O',
);
return $vagueDate;
}
else {
// Month without a year - type M.
$vagueDate = array(
$endDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'M',
);
return $vagueDate;
}
}
else {
// We do have a range, OO.
if ($endDate->tm_year !== NULL) {
// We have a year - so this is OO.
$vagueDate = array(
$startDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'OO',
);
return $vagueDate;
}
else {
// MM is not an allowed type
// TODO think about this.
return FALSE;
}
}
}
/*
* No day, no month. We're some kind of year representation - Y,YY,Y- or
* -Y, C, CC, C- or -C.
*/
// Are we a century?
if ($endDate->tm_century !== NULL) {
// CC, C, C- or -C.
if (!$range) {
// Type C.
$vagueDate = array(
$endDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'C',
);
return $vagueDate;
}
else {
if ($start && $end) {
// We're CC.
$vagueDate = array(
$startDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'CC',
);
return $vagueDate;
}
elseif ($start && !$end) {
// We're C-.
$vagueDate = array(
$endDate->getImpreciseDateStart(),
NULL,
'C-',
);
return $vagueDate;
}
elseif ($end && !$start) {
// We're -C.
$vagueDate = array(
NULL,
$endDate->getImpreciseDateEnd(),
'-C',
);
return $vagueDate;
}
}
}
// Okay, we're one of the year representations.
if ($endDate->tm_year !== NULL) {
if (!$range) {
// We're Y.
$vagueDate = array(
$endDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'Y',
);
return $vagueDate;
}
else {
if ($start && $end) {
// We're YY.
$vagueDate = array(
$startDate->getImpreciseDateStart(),
$endDate->getImpreciseDateEnd(),
'YY',
);
return $vagueDate;
}
elseif ($start && !$end) {
// We're Y-.
$vagueDate = array(
$startDate->getImpreciseDateStart(),
NULL,
'Y-',
);
return $vagueDate;
}
elseif ($end && !$start) {
// We're -Y.
$vagueDate = array(
NULL,
$endDate->getImpreciseDateEnd(),
'-Y',
);
return $vagueDate;
}
}
}
else {
return FALSE;
}
}
catch (Exception $e) {
return FALSE;
}
}
/**
* Parses a single date from a string.
*/
protected static function parseSingleDate($string, $parseFormats) {
$parsedDate = NULL;
foreach ($parseFormats as $a) {
$dp = new DateParser($a);
if ($dp->strptime($string)) {
$parsedDate = $dp;
break;
}
}
return $parsedDate;
}
/**
* Convert a vague date to a string representing a fixed date.
*/
protected static function vagueDateToDay($start, $end) {
self::check(self::areDatesEqual($start, $end), 'Day vague dates should have the same date for the start and end of the date range');
return $start->format('d/m/Y');
}
/**
* Convert a vague date to a string representing a range of days.
*/
protected static function vagueDateToDays($start, $end) {
self::check(self::isFirstDateFirstOrEqual($start, $end), 'Day ranges should be presented in vague dates in the correct sequence. Start was %s, end was %s.', $start, $end);
return $start->format('d/m/Y') .
Kohana::lang('dates.range_separator') .
$end->format('d/m/Y');
}
/**
* Convert a vague date to a string representing a fixed month.
*/
protected static function vagueDateToMonthInYear($start, $end) {
self::check(self::isMonthStart($start) && self::isMonthEnd($end) && self::isSameMonth($start, $end),
'Month dates should be represented by the first day and last day of the same month. Start was %s, end was %s.', $start, $end);
return $start->format(Kohana::lang('dates.format_m_y'));
}
/**
* Convert a vague date to a string representing a range of months.
*/
protected static function vagueDateToMonthsInYear($start, $end) {
self::check(self::isMonthStart($start) && self::isMonthEnd($end) && self::isFirstDateFirst($start, $end),
'Month ranges should be represented by the first day of the first month and last day of the last month. Start was %s, end was %s.', $start, $end);
return $start->format(Kohana::lang('dates.format_m_y')) .