-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDateSearchPlugin.php
614 lines (496 loc) · 19 KB
/
DateSearchPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
<?php
/**
* DateSearch plugin.
*
* @package Omeka\Plugins\DateSearch
*/
class DateSearchPlugin extends Omeka_Plugin_AbstractPlugin {
/**
* @var array This plugin's hooks.
*/
protected $_hooks = array(
'initialize', # tap into i18n
'install', # create additional table and batch-preprocess existing items for dates / timespans
'uninstall', # delete table
'config_form', # display choice whether or not to use [G] / [J] prefixes
'config', # store config settings in the database
'after_save_item', # preprocess saved item for dates / timespans
'after_delete_item', # delete deleted item's preprocessed dates / timespans
'admin_items_search', # add a time search field to the advanced search panel in admin
'public_items_search', # add a time search field to the advanced search panel in public
'admin_items_show_sidebar', # Debug output of stored dates/timespans in item's sidebar (if activated)
'items_browse_sql', # filter for a date after search page submission.
'admin_head', # add calendar sheet / date picker / Greg/Jul conversion functionality
);
protected $_options = array(
'date_search_use_gregjul_prefixes' => 0,
'date_search_search_all_fields' => 1,
'date_search_limit_fields' => "[]",
'date_search_search_rel_comments' => 1,
'date_search_debug_output' => 0,
);
/**
* Add the translations.
*/
public function hookInitialize() {
add_translation_source(dirname(__FILE__) . '/languages');
}
/**
* Install the plugin.
*/
public function hookInstall() {
# Create table
$db = get_db();
# Dates are always "YYYY-MM-DD", i.e. 10 characters long
$sql = "
CREATE TABLE IF NOT EXISTS `$db->DateSearchDates` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(10) unsigned NOT NULL REFERENCES `$db->Item`,
`fromdate` varchar(10) NOT NULL,
`todate` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db->query($sql);
SELF::_installOptions();
# SELF::_batchProcessExistingItems(); # Don't ... Do it only if configured.
}
/**
* Uninstall the plugin.
*/
public function hookUninstall() {
$db = get_db();
# Drop the table
$sql = "DROP TABLE IF EXISTS `$db->DateSearchDates`";
$db->query($sql);
SELF::_uninstallOptions();
}
/**
* Display the plugin configuration form.
*/
public static function hookConfigForm() {
$useGregJulPrefixes = intval(get_option('date_search_use_gregjul_prefixes'));
$searchAllFields = (int)(boolean) get_option('date_search_search_all_fields');
$db = get_db();
$sql = "select id, name from `$db->Elements` order by name asc";
$elements = $db->fetchAll($sql);
$searchElements = array();
foreach($elements as $element) { $searchElements[$element["id"]] = $element["name"]; }
$LimitFields = get_option('date_search_limit_fields');
$LimitFields = ( $LimitFields ? json_decode($LimitFields) : array() );
$withRelComments=SELF::_withRelComments();
$searchRelComments = (int)(boolean) get_option('date_search_search_rel_comments');
$debugOutput = (int)(boolean) get_option('date_search_debug_output'); # comment line to remove debug output panel
require dirname(__FILE__) . '/config_form.php';
}
/**
* Handle the plugin configuration form.
*/
public static function hookConfig() {
// Gregorian / Julian Prefix switch
$useGregJulPrefixes = intval($_POST['date_search_use_gregjul_prefixes']);
set_option('date_search_use_gregjul_prefixes', $useGregJulPrefixes);
// Search All Fields switch
$searchAllFields = (int)(boolean) $_POST['date_search_search_all_fields'];
set_option('date_search_search_all_fields', $searchAllFields);
// Limit Fields list (in case "Search All Fields" is false
$limitFields = array();
$postIds=false;
if (isset($_POST["date_search_limit_fields"])) { $postIds = $_POST["date_search_limit_fields"]; }
if (is_array($postIds)) {
foreach($postIds as $postId) {
$postId = intval($postId);
if ($postId) { $limitFields[] = $postId; }
}
}
sort($limitFields);
$limitFields = json_encode($limitFields);
set_option('date_search_limit_fields', $limitFields);
// Search Relationship Comments switch
$searchRelComments = (int)(boolean) $_POST['date_search_search_rel_comments'];
set_option('date_search_search_rel_comments', $searchRelComments);
// Debug Output switch -- if present
$debugOutput = 0; // Sanity
if (isset($_POST['date_search_debug_output'])) {
$debugOutput = (int)(boolean) $_POST['date_search_debug_output'];
}
set_option('date_search_debug_output', $debugOutput);
$reprocess = (int)(boolean) $_POST['date_search_trigger_reindex'];
if ($reprocess) { SELF::_batchProcessExistingItems(); }
# echo "<pre>"; print_r($_POST); echo "</pre>"; die();
}
/**
* Preprocess ALL existing items which could be rather EVIL in huge installations
*/
private function _batchProcessExistingItems() {
$db = get_db();
$sql= "select id from `$db->Items`";
$items = $db->fetchAll($sql);
foreach($items as $item) { SELF::_preProcessItem($item["id"]); }
}
/**
* Preprocess yyyy / yyyy-mm / yyyy-mm-dd dates after saving an item add/edit form.
*
* @param array $args
*/
public function hookAfterSaveItem($args) {
$item_id = intval(@$args["record"]["id"]);
if ($item_id) { SELF::_preProcessItem($item_id); }
} # hookAfterSaveItem()
/**
* Delete pre-processed dates after an item has been deleted
*
* @param array $args
*/
public function hookAfterDeleteItem($args) {
$db = get_db();
$item_id = intval($args["record"]["id"]);
if ($item_id) {
$sql = "delete from `$db->DateSearchDates` where item_id=$item_id";
$db->query($sql);
}
# echo "<pre>After Delete Item - ID: $item_id\nSQL: $sql\n"; print_r($args); die("</pre>");
} # hookAfterDeleteItem()
/**
* Determine if Item Relations is installed, and if it's patched to feature relationship comments
*/
private function _withRelComments() {
$db = get_db();
$withRelComments=false;
$sql = "show columns from `$db->ItemRelationsRelations` where field='relation_comment'";
try { $withRelComments = ($db->fetchOne($sql) !== false); }
catch (Exception $e) { $withRelComments=false; }
return $withRelComments;
}
/**
* Get an item's relationship comment text
*/
private function _relationshipCommentText($item_id) {
$db = get_db();
$text = "";
# Check if we could add relation comments in case Item Relations is installed and has been patched
# to feature relation comments.
$withRelComments=SELF::_withRelComments();
if ($withRelComments) {
$sql = "select relation_comment from `$db->ItemRelationsRelations` where subject_item_id=$item_id";
$comments = $db->fetchAll($sql);
if ($comments) {
foreach($comments as $comment) { $text .= " ".$comment["relation_comment"]; }
}
}
return $text;
}
/**
* Pre-process one item's textual data and store timespans in DateSearchDates table
*/
private function _preProcessItem($item_id) {
$db = get_db();
if ($item_id) {
$sql = "delete from `$db->DateSearchDates` where item_id=$item_id";
$db->query($sql);
$text = false;
$searchAllFields = (int)(boolean) get_option('date_search_search_all_fields');
if ($searchAllFields) {
$text = $db->fetchOne("select text from `$db->SearchTexts` where record_type='Item' and record_id=$item_id");
$text = ( $text ? $text : "" );
$text .= SELF::_relationshipCommentText($item_id);
$text = ( $text ? $text : false );
} # if ($searchAllFields)
else { # !$searchAllFields
$limitFields = get_option('date_search_limit_fields');
$limitFields = ( $limitFields ? json_decode($limitFields) : array() );
$elementIds=array();
if (is_array($limitFields)) {
foreach($limitFields as $limitField) {
$limitField = intval($limitField);
if ($limitField) { $elementIds[] = $limitField; }
}
sort($elementIds);
}
if ($elementIds) {
$elementIds = "(" . implode(",", $elementIds) . ")";
$elementTexts = $db -> fetchAll("select text from `$db->ElementTexts`".
" where record_id=$item_id".
" and element_id in $elementIds");
if ($elementTexts) {
$text = "";
foreach($elementTexts as $elementText) { $text .= " " . $elementText["text"]; }
} # if ($elementTexts)
} # if ($elementIds)
$searchRelComments = (int)(boolean) get_option('date_search_search_rel_comments');
if ($searchRelComments) {
$text = ( $text ? $text : "" );
$text .= SELF::_relationshipCommentText($item_id);
$text = ( $text ? $text : false );
}
} # !$searchAllFields
if ($text !== false) {
$cookedDates = SELF::_processDateText($text);
// echo "<pre>" . print_r($cookedDates,true) . "</pre>"; die();
if ($cookedDates) {
$values = array();
foreach($cookedDates as $cookedDate) {
SELF::_swapIfNecessary($cookedDate[0], $cookedDate[1]);
$values[]='('.$item_id.',"'.$cookedDate[0].'","'.$cookedDate[1].'")';
}
$values = implode(", ", $values);
$sql = "insert into `$db->DateSearchDates` (item_id, fromdate, todate) values $values";
$db->query($sql);
# die($sql);
} # if ($cookedDates)
} # if ($text)
} # if ($item_id)
} # function _preProcessItem()
/**
* Display the time search form on the admin advanced search page
*/
protected function _ItemsSearch() {
echo common('date-search-advanced-search', null);
}
/**
* Display the time search form on the admin advanced search page in admin
*/
public function hookAdminItemsSearch() { SELF::_itemsSearch(); }
/**
* Display the time search form on the admin advanced search page in public
*/
public function hookPublicItemsSearch() { SELF::_itemsSearch(); }
/**
* Debug output of stored dates/timespans in item's sidebar (if activated)
*
* @param Item $item
*/
public function hookAdminItemsShowSidebar($args) {
$debugOutput = (int)(boolean) get_option('date_search_debug_output');
if ($debugOutput) {
$itemID = $args['item']['id'];
if ($itemID) {
echo "<div class='panel'><h4>".__("Date Search Debug Output")."</h4>\n";
$db = get_db();
$sql = "select * from `$db->DateSearchDates` where item_id=$itemID";
$timespans = $db->fetchAll($sql);
if ($timespans) {
echo "<ul>\n";
foreach($timespans as $timespan) {
$fromDate = $timespan["fromdate"];
$toDate = $timespan["todate"];
echo "<li>". $fromDate . " … ". $toDate . "</li>\n";
}
echo "</ul>\n";
}
echo "</div>\n";
}
}
}
/**
* Filter for an date after search page submission.
*
* @param array $args
*/
public function hookItemsBrowseSql($args) {
$select = $args['select'];
$params = $args['params'];
$regEx = SELF::_constructRegEx();
$date = $regEx["date"];
$dateTimespan = $regEx["dateTimespan"];
if ( (isset($params['date_search_term'])) and
(preg_match( "($dateTimespan)", $params['date_search_term'])) ) {
$singleCount = preg_match_all ( "($date)", $params['date_search_term'], $singleSplit );
$timespan = array();
$timespan[] = $singleSplit[0][0];
$timespan[] = $singleSplit[0][ ($singleCount==2 ? 1 : 0 ) ];
$timespan = SELF::_expandTimespan($timespan);
$searchFromDate = $timespan[0];
$searchToDate = $timespan[1];
$db = get_db();
$select
->join(
array('date_search_dates' => $db->DateSearchDates),
"date_search_dates.item_id = items.id",
array()
)
->where("'$searchFromDate'<=date_search_dates.todate and '$searchToDate'>=date_search_dates.fromdate");
# die("<pre>$searchFromDate / $searchToDate --- $select</pre>");
}
}
# ------------------------------------------------------------------------------------------------------
/**
* Sdd calendar sheet / date picker / Greg/Jul conversion functionality
*/
public function hookAdminHead() {
$request = Zend_Controller_Front::getInstance()->getRequest();
$module = $request->getModuleName();
if (is_null($module)) { $module = 'default'; }
$controller = $request->getControllerName();
$action = $request->getActionName();
if ($module === 'default'
&& $controller === 'items'
&& in_array($action, array('add', 'edit'))) {
queue_js_file('datesearch');
queue_js_file('rangyinputs-jquery');
queue_css_file('datesearch');
queue_js_file('jquery.plugin.min');
queue_js_file('jquery.mousewheel.min');
queue_js_file('jquery.calendars.all.min');
queue_js_file('jquery.calendars.julian.min');
queue_js_file('jquery.calendars.picker.min');
$locale = get_html_lang();
$underscore=strpos($locale, "-");
$locale = ($underscore ? substr($locale,0,$underscore) : $locale);
if ($locale!="en") {
queue_js_file('jquery.calendars-'.$locale);
queue_js_file('jquery.calendars.picker-'.$locale);
}
queue_css_file('jquery.calendars.picker');
$timespan = __("Time Span");
$gregorian = __("Gregorian");
$julian = __("Julian");
$date = __("Date (without prefix)");
$convert = __("Convert");
$selectFirst = __("Please select a target text area first.");
$cancel = __("Cancel");
$selectDate = __("Please select a date or time span to convert.");
$cantEdit = __("Can not edit selected date as %.");
$cantConvert = __("Can not convert selected date to %, as it is already %.");
$dateEntry = __("Date Entry");
queue_js_string("
var dateSearchLocale='$locale';
var dateSearchTimeSpan='$timespan';
var dateSearchConvert='$convert';
var dateSearchGregorian='$gregorian';
var dateSearchJulian='$julian';
var dateSearchDate='$date';
var dateSearchSelectFirst='$selectFirst';
var dateSearchCancel='$cancel';
var dateSearchSelectDate='$selectDate';
var dateSearchCantEdit='$cantEdit';
var dateSearchCantConvert='$cantConvert';
var dateSearchDateEntry='$dateEntry';
");
}
}
# ------------------------------------------------------------------------------------------------------
/**
* Cross swap in case the first element is "bigger" (i.e. sorts behind) the second
*/
private function _swapIfNecessary(&$x,&$y) {
# as in http://stackoverflow.com/a/26549027
if ($x > $y) {
$tmp=$x;
$x=$y;
$y=$tmp;
}
}
# ------------------------------------------------------------------------------------------------------
/**
* Main regex processing to extract dates and timespans, to be able to expand them later
*/
private function _processDateText($text) {
$regEx = SELF::_constructRegEx();
$julGregPrefix = $regEx["julGregPrefix"];
$date = $regEx["date"];
$useGregJulPrefixes = intval(get_option('date_search_use_gregjul_prefixes'));
$mainRegEx = $regEx["dateTimespan"]; // Default: Ignore prefixes
switch ($useGregJulPrefixes) {
case 1 : $mainRegEx = $regEx["julGregDateTimeSpan"]; break; // 1 == old "true": Require [J]/[G]
// 2 == optional prefix: honor if present, but also parse dates without prefix
case 2 : $mainRegEx = $regEx["optionalJulGregTimeSpan"]; break;
}
$mainRegEx = "\b$mainRegEx\b";
$allCount = preg_match_all( "($mainRegEx)i", $text, $allMatches);
// echo "<p>$text</p>";
// echo "<pre>Count: $allCount\n" . print_r($allMatches,true) . "</pre>"; // die();
$cookedDates = array();
foreach(array_unique($allMatches[0]) as $singleMatch) {
$singleCount = preg_match_all ( "($date)", $singleMatch, $singleSplit );
$timespan = array();
$timespan[] = $singleSplit[0][0];
$timespan[] = $singleSplit[0][ ($singleCount==2 ? 1 : 0 ) ];
$timespan = SELF::_expandTimespan($timespan);
$storeDate = true;
if ($useGregJulPrefixes>0) { // Gregorian / Julian date prefixes
$julGreg = preg_match( "($julGregPrefix)i", $singleMatch, $julGregMatch );
$julGregJG = ($julGreg == 1 ? strtoupper($julGregMatch[1]) : null ); // "G" or "J" or null
# echo "<pre>$julGreg / $julGregJG: " . print_r($julGregMatch,true) . "</pre>";
switch ($julGregJG) {
case "J" : $storeDate = ($timespan[0] <= "1582-10-04"); break;
case "G" : $storeDate = ($timespan[1] >= "1582-10-15"); break;
}
# echo "<pre>StoreDate: $storeDate\n" . print_r($timespan,true) . "</pre>";
}
if ($storeDate) { $cookedDates[] = $timespan; }
}
// echo "<pre>" . print_r($cookedDates,true) . "</pre>"; die();
return $cookedDates;
}
# ------------------------------------------------------------------------------------------------------
/**
* Create the necessary regEx expressions to deal with yyyy / yyyy-mm / yyyy-mm-dd / yyyy-mm-dd - yyyy-mm-dd
*/
private function _constructRegEx() {
# Construct RegEx
$year = "\d{4}";
$month = $day = "\d{1,2}";
$monthDay = "$month(?:-$day)?";
$date = "$year(?:-$monthDay)?\b";
$separator = "\s*-\s*";
$dateTimespan = "$date(?:$separator$date)?";
$julGregPrefix = "\[([J,G])\] ";
$julGregDateTimeSpan = $julGregPrefix.$dateTimespan;
$optionalJulGregTimeSpan = "(?:$julGregDateTimeSpan|$dateTimespan)";
$result=array(
"year" => $year,
"month" => $month,
"day" => $day,
"monthDay" => $monthDay,
"date" => $date,
"separator" => $separator,
"dateTimespan" => $dateTimespan,
"julGregPrefix" => $julGregPrefix,
"julGregDateTimeSpan" => $julGregDateTimeSpan,
"optionalJulGregTimeSpan" => $optionalJulGregTimeSpan,
);
return $result;
}
# ------------------------------------------------------------------------------------------------------
/**
* Transform a (valid) date yyyy-mm-dd into a timespan -- down to yyyy-01-01 to yyyy-12-31
*
* @param string $timespan as in single date or timespan
* @result array [0] => left edge, [1] => right edge
*/
private function _expandTimespan($timespan) {
$result = $timespan;
if (!is_array($result)) { $result = array($result, $result); }
$result[0] = SELF::_updateDate($result[0], -1); # -1 == left edge, xxxx-01-01
$result[1] = SELF::_updateDate($result[1], +1); # +1 == right edge, xxxx-12-31
return $result;
}
# ------------------------------------------------------------------------------------------------------
/**
* Take a valid yyyy / yyyy-m / yyyy-mm / yyyy-m-d / yyyy-mm-d / yyyy-mm-dd
* and transform it towards a left edge of possibly yyyy-01-01 or yyyy-12-31
* or at least add leading zeros, as in yyyy-0m-0d
*
* @param string $date to be updated
* @param int edge -- -1 -> left edge (-01-01) / +1 -> right edge (-12-31)
* @result string $date -- transformed towards edge and with leading zeros
*/
protected function _updateDate($date, $edge) {
$result=$date;
$regEx = SELF::_constructRegEx();
$year = $regEx["year"];
$month =$regEx["month"];
$day = $regEx["day"];
$yearOnly = "^$year$";
$yearMonth = "^$year-$month$";
$yearMonthDay = "^$year-$month-$day$";
if ( preg_match( "($yearOnly)", $result ) ) { $result = $result."-".( $edge<0 ? "1" : "12" ); }
if ( preg_match( "($yearMonth)", $result ) ) { $result = $result."-".( $edge<0 ? "1" : "31" ); }
if ( preg_match( "($yearMonthDay)", $result ) ) {
$oneDigit = "\b(\d)\b";
$result = preg_replace("($oneDigit)", '0${0}', $result);
}
return $result;
}
# ------------------------------------------------------------------------------------------------------
} # class