CWIS Developer Documentation
SavedSearch.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: SavedSearch.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2011-2015 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
11 {
12 
13  # ---- PUBLIC INTERFACE --------------------------------------------------
14 
15  # search frequency mnemonics
16  const SEARCHFREQ_NEVER = 0;
17  const SEARCHFREQ_HOURLY = 1;
18  const SEARCHFREQ_DAILY = 2;
19  const SEARCHFREQ_WEEKLY = 3;
21  const SEARCHFREQ_MONTHLY = 5;
23  const SEARCHFREQ_YEARLY = 7;
24 
25  # object constructor
26 
34  public function __construct($SearchId, $SearchName = NULL, $UserId = NULL,
35  $Frequency = NULL, $SearchParameters = NULL)
36  {
37  # get our own database handle
38  $this->DB = new Database();
39 
40  # if search ID was provided
41  if ($SearchId !== NULL)
42  {
43  # save search ID
44  $this->SearchId = intval($SearchId);
45 
46  # initialize our local copies of data
47  $this->DB->Query(
48  "SELECT * FROM SavedSearches "
49  ."WHERE SearchId = ".$this->SearchId);
50 
51  if ($this->DB->NumRowsSelected() == 0)
52  {
53  throw new Exception ("Specified SearchId does not exist");
54  }
55 
56  $this->Record = $this->DB->FetchRow();
57 
58  # get our Search Parameters
59  if (isset($this->Record["SearchData"]))
60  {
62  $this->Record["SearchData"]);
63 
64  # remove now redundant 'Data' from our record
65  unset ($this->Record["SearchData"]);
66  }
67  else
68  {
69  $this->SearchParameters = new SearchParameterSet();
70  }
71 
72  # update search details where provided
73  if ($SearchName)
74  {
75  $this->SearchName($SearchName);
76  }
77 
78  if ($UserId)
79  {
80  $this->UserId($UserId);
81  }
82 
83  if ($Frequency)
84  {
85  $this->Frequency($Frequency);
86  }
87  }
88  else
89  {
90  # add new saved search to database
91  $this->DB->Query("INSERT INTO SavedSearches"
92  ." (SearchName, UserId, Frequency) VALUES ("
93  ."'".addslashes($SearchName)."', "
94  .intval($UserId).", "
95  .intval($Frequency).")");
96 
97  # retrieve and save ID of new search locally
98  $this->SearchId = $this->DB->LastInsertId();
99 
100  # populate our local data for this record
101  $this->DB->Query(
102  "SELECT * FROM SavedSearches "
103  ."WHERE SearchId = ".$this->SearchId);
104  $this->Record = $this->DB->FetchRow();
105 
106  $this->InitializeSearchParameters($SearchParameters);
107  $this->InitializeLastMatches();
108  }
109  }
110 
119  public function SearchGroups($NewSearchGroups = NULL)
120  {
121  if (!is_null($NewSearchGroups))
122  {
123  $Params = new SearchParameterSet();
124  $Params->SetFromLegacyArray($NewSearchGroups);
125  $this->SearchParameters($Params);
126  }
127 
128  # return search parameters to caller
129  return $this->SearchParameters()->GetAsLegacyArray();
130  }
131 
137  public function SearchParameters($NewParams = NULL)
138  {
139  if (!is_null($NewParams))
140  {
141  if ($NewParams instanceof SearchParameterSet)
142  {
143 
144  $Data = $NewParams->Data();
145 
146  $this->DB->Query(
147  "UPDATE SavedSearches SET SearchData = '". addslashes($Data)."'"
148  ." WHERE SearchId = ".$this->SearchId);
149 
150  $this->SearchParameters = new SearchParameterSet($Data);
151  }
152  else
153  {
154  throw new Exception("NewParams must be a SearchParameterSet");
155  }
156  }
157 
158  return clone $this->SearchParameters;
159  }
160 
166  public function SearchName($NewValue = DB_NOVALUE)
167  {
168  return $this->UpdateValue("SearchName", $NewValue);
169  }
170 
175  public function Id()
176  {
177  return $this->SearchId;
178  }
179 
185  public function UserId($NewValue = DB_NOVALUE)
186  {
187  return $this->UpdateValue("UserId", $NewValue);
188  }
189 
195  public function Frequency($NewValue = DB_NOVALUE)
196  {
197  return $this->UpdateValue("Frequency", $NewValue);
198  }
199 
200 
204  public function UpdateDateLastRun()
205  {
206  $this->DB->Query(
207  "UPDATE SavedSearches SET DateLastRun = NOW() "
208  ."WHERE SearchId = ".$this->SearchId);
209  }
210 
216  public function DateLastRun($NewValue = DB_NOVALUE)
217  {
218  return $this->UpdateValue("DateLastRun", $NewValue);
219  }
220 
225  public function SaveLastMatches($ArrayofMatchingIds)
226  {
227  $NewValue = implode(",", $ArrayofMatchingIds);
228  $this->UpdateValue("LastMatchingIds", $NewValue);
229  }
230 
235  public function LastMatches()
236  {
237  return explode(",", $this->DB->Query(
238  "SELECT LastMatchingIds FROM SavedSearches "
239  ."WHERE SearchId = ".$this->SearchId, "LastMatchingIds"));
240  }
247  {
248  return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
249  }
250 
288  public static function TranslateSearchGroupsToUrlParameters($SearchGroups)
289  {
290  # assume that no parameters will be found
291  $UrlPortion = "";
292 
293  # for each group in parameters
294  $Schema = new MetadataSchema();
295  foreach ($SearchGroups as $GroupIndex => $Group)
296  {
297  # if group holds single parameters
298  if ($GroupIndex == "MAIN")
299  {
300  # for each field within group
301  foreach ($Group["SearchStrings"] as $FieldName => $Value)
302  {
303  # add segment to URL for this field
304  if ($FieldName == "XXXKeywordXXX")
305  {
306  $FieldId = "K";
307  }
308  else
309  {
310  $Field = $Schema->GetFieldByName($FieldName);
311  $FieldId = $Field->Id();
312  }
313  if (is_array($Value))
314  {
315  $UrlPortion .= "&F".$FieldId."=";
316  $ValueString = "";
317  foreach ($Value as $SingleValue)
318  {
319  $ValueString .= $SingleValue." ";
320  }
321  $UrlPortion .= urlencode(trim($ValueString));
322  }
323  else
324  {
325  $UrlPortion .= "&F".$FieldId."=".urlencode($Value);
326  }
327  }
328  }
329  else
330  {
331  # convert value based on field type
332  $FieldId = ($GroupIndex[0] == "X")
333  ? substr($GroupIndex, 1)
334  : $GroupIndex;
335  $Field = $Schema->GetField($FieldId);
336  $FieldName = $Field->Name();
337  $Values = self::TranslateValues($Field,
338  $Group["SearchStrings"][$FieldName],
339  "SearchGroup to Database");
340 
341  # add values to URL
342  $FirstValue = TRUE;
343  foreach ($Values as $Value)
344  {
345  if ($FirstValue)
346  {
347  $FirstValue = FALSE;
348  $UrlPortion .= "&G".$FieldId."=".$Value;
349  }
350  else
351  {
352  $UrlPortion .= "-".$Value;
353  }
354  }
355  }
356  }
357 
358  # trim off any leading "&"
359  if (strlen($UrlPortion)) { $UrlPortion = substr($UrlPortion, 1); }
360 
361  # return URL portion to caller
362  return $UrlPortion;
363  }
364 
371  {
372  return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
373  }
374 
381  public static function TranslateSearchGroupsToUrlParameterArray($SearchGroups)
382  {
383  # assume that no parameters will be found
384  $UrlPortion = array();
385 
386  # for each group in parameters
387  $Schema = new MetadataSchema();
388  foreach ($SearchGroups as $GroupIndex => $Group)
389  {
390  # if group holds single parameters
391  if ($GroupIndex == "MAIN")
392  {
393  # for each field within group
394  foreach ($Group["SearchStrings"] as $FieldName => $Value)
395  {
396  # add segment to URL for this field
397  if ($FieldName == "XXXKeywordXXX")
398  {
399  $FieldId = "K";
400  }
401  else
402  {
403  $Field = $Schema->GetFieldByName($FieldName);
404  $FieldId = $Field->Id();
405  }
406  if (is_array($Value))
407  {
408  $ValueString = "";
409  foreach ($Value as $SingleValue)
410  {
411  $ValueString .= $SingleValue." ";
412  }
413 
414  $UrlPortion["F".$FieldId] = urlencode(trim($ValueString));
415  }
416  else
417  {
418  $UrlPortion["F".$FieldId] = urlencode($Value);
419  }
420  }
421  }
422  else
423  {
424  # convert value based on field type
425  $FieldId = ($GroupIndex[0] == "X")
426  ? substr($GroupIndex, 1)
427  : $GroupIndex;
428  $Field = $Schema->GetField($FieldId);
429  $FieldName = $Field->Name();
430  $Values = self::TranslateValues($Field,
431  $Group["SearchStrings"][$FieldName],
432  "SearchGroup to Database");
433  $LeadChar = ($Group["Logic"] == SearchEngine::LOGIC_AND)
434  ? "H" : "G";
435 
436  # add values to URL
437  $FirstValue = TRUE;
438  foreach ($Values as $Value)
439  {
440  if ($FirstValue)
441  {
442  $FirstValue = FALSE;
443  $UrlPortion[$LeadChar.$FieldId] = $Value;
444  }
445  else
446  {
447  $UrlPortion[$LeadChar.$FieldId] .= "-".$Value;
448  }
449  }
450  }
451  }
452 
453  # return URL portion to caller
454  return $UrlPortion;
455  }
456 
462  public static function TranslateUrlParametersToSearchGroups($GetVars)
463  {
464  # if URL segment was passed in instead of GET var array
465  if (is_string($GetVars))
466  {
467  $GetVars = ParseQueryString($GetVars);
468  }
469 
470  # start with empty list of parameters
471  $SearchGroups = array();
472 
473  $Schema = new MetadataSchema();
474  $AllFields = $Schema->GetFields(NULL, NULL, TRUE);
475 
476  foreach ($AllFields as $Field)
477  {
478  $FieldId = $Field->Id();
479  $FieldName = $Field->Name();
480 
481  # if URL included literal value for this field
482  if (isset($GetVars["F".$FieldId]))
483  {
484  # retrieve value and add to search parameters
485  $SearchGroups["MAIN"]["SearchStrings"][$FieldName] =
486  $GetVars["F".$FieldId];
487  }
488 
489  # if URL included group value for this field
490  if (isset($GetVars["G".$FieldId]))
491  {
492  # retrieve and parse out values
493  $Values = explode("-", $GetVars["G".$FieldId]);
494 
495  # translate values
496  $Values = self::TranslateValues($Field, $Values,
497  "Database to SearchGroup");
498 
499  # add values to searchgroups
500  $SearchGroups[$FieldId]["SearchStrings"][$FieldName] = $Values;
501  }
502 
503  # if URL included group value for this field
504  if (isset($GetVars["H".$FieldId]))
505  {
506  # retrieve and parse out values
507  $Values = explode("-", $GetVars["H".$FieldId]);
508 
509  # translate values
510  $Values = self::TranslateValues($Field, $Values,
511  "Database to SearchGroup");
512 
513  # add values to searchgroups
514  $SearchGroups["X".$FieldId]["SearchStrings"][$FieldName] = $Values;
515  }
516  }
517 
518  # if keyword pseudo-field was included in URL
519  if (isset($GetVars["FK"]))
520  {
521  # retrieve value and add to search parameters
522  $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] = $GetVars["FK"];
523  }
524 
525  # set search logic
526  foreach ($SearchGroups as $GroupIndex => $Group)
527  {
528  $SearchGroups[$GroupIndex]["Logic"] = ($GroupIndex == "MAIN")
530  : (($GroupIndex[0] == "X")
532  }
533 
534  # return parameters to caller
535  return $SearchGroups;
536  }
537 
549  $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
550  {
551  return $this->SearchParameters->TextDescription(
552  $IncludeHtml, $StartWithBreak, $TruncateLongWordsTo);
553  }
554 
566  public static function TranslateSearchGroupsToTextDescription($SearchGroups,
567  $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
568  {
569  $Schema = new MetadataSchema();
570 
571  # start with empty description
572  $Descrip = "";
573 
574  # set characters used to indicate literal strings
575  $LiteralStart = $IncludeHtml ? "<i>" : "\"";
576  $LiteralEnd = $IncludeHtml ? "</i>" : "\"";
577  $LiteralBreak = $IncludeHtml ? "<br>\n" : "\n";
578 
579  # if this is a simple keyword search
580  if (isset($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"])
581  && (count($SearchGroups) == 1)
582  && (count($SearchGroups["MAIN"]["SearchStrings"]) == 1))
583  {
584  # just use the search string
585  $Descrip .= $LiteralStart;
586  $Descrip .= defaulthtmlentities(
587  $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"]);
588  $Descrip .= $LiteralEnd . $LiteralBreak;
589  }
590  else
591  {
592  # start description on a new line (if requested)
593  if ($StartWithBreak)
594  {
595  $Descrip .= $LiteralBreak;
596  }
597 
598  # define list of phrases used to represent logical operators
599  $WordsForOperators = array(
600  "=" => "is",
601  ">" => "is greater than",
602  "<" => "is less than",
603  ">=" => "is at least",
604  "<=" => "is no more than",
605  "!" => "is not",
606  );
607 
608  # for each search group
609  foreach ($SearchGroups as $GroupIndex => $Group)
610  {
611  # if group is main
612  if ($GroupIndex == "MAIN")
613  {
614  # for each field in group
615  foreach ($Group["SearchStrings"] as $FieldName => $Value)
616  {
617  # determine wording based on operator
618  preg_match("/^[=><!]+/", $Value, $Matches);
619  if (count($Matches) && isset($WordsForOperators[$Matches[0]]))
620  {
621  $Value = preg_replace("/^[=><!]+/", "", $Value);
622  $Wording = $WordsForOperators[$Matches[0]];
623  }
624  else
625  {
626  $Wording = "contains";
627  }
628 
629  # if field is psuedo-field
630  if ($FieldName == "XXXKeywordXXX")
631  {
632  # add criteria for psuedo-field
633  $Descrip .= "Keyword ".$Wording." "
634  .$LiteralStart.htmlspecialchars($Value)
635  .$LiteralEnd.$LiteralBreak;
636  }
637  else
638  {
639  # if field is valid
640  $Field = $Schema->GetFieldByName($FieldName);
641  if ($Field !== NULL)
642  {
643  # add criteria for field
644  $Descrip .= $Field->GetDisplayName()." ".$Wording." "
645  .$LiteralStart.htmlspecialchars($Value)
646  .$LiteralEnd.$LiteralBreak;
647  }
648  }
649  }
650  }
651  else
652  {
653  # for each field in group
654  $LogicTerm = ($Group["Logic"] == SearchEngine::LOGIC_AND)
655  ? "and " : "or ";
656  foreach ($Group["SearchStrings"] as $FieldName => $Values)
657  {
658  # translate values
659  $Values = self::TranslateValues(
660  $FieldName, $Values, "SearchGroup to Display");
661 
662  # for each value
663  $FirstValue = TRUE;
664  foreach ($Values as $Value)
665  {
666  # determine wording based on operator
667  preg_match("/^[=><!]+/", $Value, $Matches);
668  $Operator = $Matches[0];
669  $Wording = $WordsForOperators[$Operator];
670 
671  # strip off operator
672  $Value = preg_replace("/^[=><!]+/", "", $Value);
673 
674  # add text to description
675  if ($FirstValue)
676  {
677  $Descrip .= $FieldName." ".$Wording." "
678  .$LiteralStart.htmlspecialchars($Value)
679  .$LiteralEnd.$LiteralBreak;
680  $FirstValue = FALSE;
681  }
682  else
683  {
684  $Descrip .= ($IncludeHtml ?
685  "&nbsp;&nbsp;&nbsp;&nbsp;" : " ")
686  .$LogicTerm.$Wording." ".$LiteralStart
687  .htmlspecialchars($Value).$LiteralEnd
688  .$LiteralBreak;
689  }
690  }
691  }
692  }
693  }
694  }
695 
696  # if caller requested that long words be truncated
697  if ($TruncateLongWordsTo > 4)
698  {
699  # break description into words
700  $Words = explode(" ", $Descrip);
701 
702  # for each word
703  $NewDescrip = "";
704  foreach ($Words as $Word)
705  {
706  # if word is longer than specified length
707  if (strlen(strip_tags($Word)) > $TruncateLongWordsTo)
708  {
709  # truncate word and add ellipsis
710  $Word = StdLib::NeatlyTruncateString($Word, $TruncateLongWordsTo - 3);
711  }
712 
713  # add word to new description
714  $NewDescrip .= " ".$Word;
715  }
716 
717  # set description to new description
718  $Descrip = $NewDescrip;
719  }
720 
721  # return description to caller
722  return $Descrip;
723  }
724 
729  public function GetSearchFieldNames()
730  {
731  return $this->SearchParameters->GetFields();
732  }
733 
739  public static function TranslateSearchGroupsToSearchFieldNames($SearchGroups)
740  {
741  # start out assuming no fields are being searched
742  $FieldNames = array();
743 
744  # for each search group defined
745  foreach ($SearchGroups as $GroupIndex => $Group)
746  {
747  # for each field in group
748  foreach ($Group["SearchStrings"] as $FieldName => $Values)
749  {
750  # add field name to list of fields being searched
751  $FieldNames[] = $FieldName;
752  }
753  }
754 
755  # return list of fields being searched to caller
756  return $FieldNames;
757  }
758 
764  public static function GetSearchFrequencyList()
765  {
766  # define list with descriptions
767  $FreqDescr = array(
768  self::SEARCHFREQ_NEVER => "Never",
769  self::SEARCHFREQ_HOURLY => "Hourly",
770  self::SEARCHFREQ_DAILY => "Daily",
771  self::SEARCHFREQ_WEEKLY => "Weekly",
772  self::SEARCHFREQ_BIWEEKLY => "Biweekly",
773  self::SEARCHFREQ_MONTHLY => "Monthly",
774  self::SEARCHFREQ_QUARTERLY => "Quarterly",
775  self::SEARCHFREQ_YEARLY => "Yearly",
776  );
777 
778  # for each argument passed in
779  $Args = func_get_args();
780  foreach ($Args as $Arg)
781  {
782  # remove value from list
783  $FreqDescr = array_diff_key($FreqDescr, array($Arg => ""));
784  }
785 
786  # return list to caller
787  return $FreqDescr;
788  }
789 
793  public function Delete()
794  {
795  $this->DB->Query("DELETE FROM SavedSearches"
796  ." WHERE SearchId = ".intval($this->SearchId));
797  }
798 
799 
800  # ---- PRIVATE INTERFACE -------------------------------------------------
801 
802  private $SearchId;
803  private $Record;
804  private $SearchGroups;
805  private $DB;
806  private $SearchParameters;
807 
822  private static function TranslateValues($FieldOrFieldName, $Values, $TranslationType)
823  {
824  # start out assuming we won't find any values to translate
825  $ReturnValues = array();
826 
827  # convert field name to field object if necessary
828  if (is_object($FieldOrFieldName))
829  {
830  $Field = $FieldOrFieldName;
831  }
832  else
833  {
834  static $Schema;
835  if (!isset($Schema)) { $Schema = new MetadataSchema(); }
836  $Field = $Schema->GetFieldByName($FieldOrFieldName);
837  }
838 
839  # if incoming value is not an array
840  if (!is_array($Values))
841  {
842  # convert incoming value to an array
843  $Values = array($Values);
844  }
845 
846  # for each incoming value
847  foreach ($Values as $Value)
848  {
849  switch ($TranslationType)
850  {
851  case "SearchGroup to Display":
852  # if field is Flag field
853  if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
854  {
855  # translate value to true/false label and add leading operator
856  $ReturnValues[] = ($Value == "=1") ?
857  "=".$Field->FlagOnLabel() : "=".$Field->FlagOffLabel();
858  }
859  elseif ($Field->Name() == "Cumulative Rating")
860  {
861  # translate numeric value to stars
862  $StarStrings = array(
863  "20" => "*",
864  "40" => "**",
865  "60" => "***",
866  "80" => "****",
867  "100" => "*****",
868  );
869  preg_match("/[0-9]+$/", $Value, $Matches);
870  $Number = $Matches[0];
871  preg_match("/^[=><!]+/", $Value, $Matches);
872  $Operator = $Matches[0];
873  $ReturnValues[] = $Operator.$StarStrings[$Number];
874  }
875  else
876  {
877  # use value as is
878  $ReturnValues[] = $Value;
879  }
880  break;
881 
882  case "SearchGroup to Database":
883  # strip off leading operator on value
884  $Value = preg_replace("/^[=><!]+/", "", $Value);
885 
886  # look up index for value
887  if ($Field->Type() & (MetadataSchema::MDFTYPE_FLAG |
889  {
890  # (for flag or number fields the value index is already
891  # what is used in SearchGroups)
892  if ($Value >= 0)
893  {
894  $ReturnValues[] = $Value;
895  }
896  }
897  elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
898  {
899  # (for user fields the value index is the user ID)
900  $User = new CWUser(strval($Value));
901  if ($User)
902  {
903  $ReturnValues[] = $User->Id();
904  }
905  }
906  elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
907  {
908  if (!isset($PossibleFieldValues))
909  {
910  $PossibleFieldValues = $Field->GetPossibleValues();
911  }
912  $NewValue = array_search($Value, $PossibleFieldValues);
913  if ($NewValue !== FALSE)
914  {
915  $ReturnValues[] = $NewValue;
916  }
917  }
918  else
919  {
920  $NewValue = $Field->GetIdForValue($Value);
921  if ($NewValue !== NULL)
922  {
923  $ReturnValues[] = $NewValue;
924  }
925  }
926  break;
927 
928  case "Database to SearchGroup":
929  # look up value for index
930  if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
931  {
932  # (for flag fields the value index (0 or 1) is already
933  # what is used in Database)
934  if ($Value >= 0)
935  {
936  $ReturnValues[] = "=".$Value;
937  }
938  }
939  elseif ($Field->Type() == MetadataSchema::MDFTYPE_NUMBER)
940  {
941  # (for flag fields the value index (0 or 1) is already
942  # what is used in Database)
943 
944  if ($Value >= 0)
945  {
946  $ReturnValues[] = ">=".$Value;
947  }
948  }
949  elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
950  {
951  $User = new CWUser(intval($Value));
952  if ($User)
953  {
954  $ReturnValues[] = "=".$User->Get("UserName");
955  }
956  }
957  elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
958  {
959  if (!isset($PossibleFieldValues))
960  {
961  $PossibleFieldValues = $Field->GetPossibleValues();
962  }
963 
964  if (isset($PossibleFieldValues[$Value]))
965  {
966  $ReturnValues[] = "=".$PossibleFieldValues[$Value];
967  }
968  }
969  else
970  {
971  $NewValue = $Field->GetValueForId($Value);
972  if ($NewValue !== NULL)
973  {
974  $ReturnValues[] = "=".$NewValue;
975  }
976  }
977  break;
978  }
979  }
980 
981  # return array of translated values to caller
982  return $ReturnValues;
983  }
984 
987  # utility function for updating values in database
988 
994  private function UpdateValue($FieldName, $NewValue)
995  {
996  return $this->DB->UpdateValue("SavedSearches", $FieldName, $NewValue,
997  "SearchId = ".$this->SearchId, $this->Record);
998  }
999 
1005  public function GetSearchId()
1006  {
1007  return $this->Id();
1008  }
1009 
1016  private function InitializeSearchParameters($SearchParameters)
1017  {
1018  # if given legacy data, modernize it
1019  if (!is_null($SearchParameters) && is_array($SearchParameters))
1020  {
1021  $Params = new SearchParameterSet();
1022  $Params->SetFromLegacyArray($SearchParameters);
1023  $SearchParameters = $Params;
1024  }
1025 
1026  $EndUser = new CWUser($this->UserId());
1027 
1028  # signal event to allow modification of search parameters
1029  $SignalResult = $GLOBALS["AF"]->SignalEvent(
1030  "EVENT_FIELDED_SEARCH", array(
1031  "SearchParameters" => $SearchParameters,
1032  "User" => $EndUser,
1033  "SavedSearch" => $this));
1034 
1035  # save possibly modified values
1036  $this->SearchParameters($SignalResult["SearchParameters"]);
1037  }
1038 
1042  private function InitializeLastMatches()
1043  {
1044  # perform search
1045  $SearchEngine = new SPTSearchEngine();
1046  $SearchResults = $SearchEngine->SearchAll($this->SearchParameters);
1047 
1048  # build the list of results the user can see
1049  $EndUser = new CWUser($this->UserId());
1050  $NewItemIds = [];
1051  foreach ($SearchResults as $SchemaId => $SchemaResults)
1052  {
1053  $RFactory = new ResourceFactory($SchemaId);
1054  $SchemaItemIds = $RFactory->FilterNonViewableResources(
1055  array_keys($SchemaResults), $EndUser);
1056  $NewItemIds = array_merge(
1057  $NewItemIds, $SchemaItemIds);
1058  }
1059 
1060  # if visible search results were found, save them
1061  if (count($NewItemIds))
1062  {
1063  $this->SaveLastMatches($NewItemIds);
1064  }
1065  }
1066 }
const SEARCHFREQ_WEEKLY
Definition: SavedSearch.php:19
const SEARCHFREQ_NEVER
Definition: SavedSearch.php:16
static TranslateUrlParametersToSearchGroups($GetVars)
Translate URL parameters to legacy search group array.
Metadata schema (in effect a Factory class for MetadataField).
const SEARCHFREQ_DAILY
Definition: SavedSearch.php:18
Set of parameters used to perform a search.
UpdateDateLastRun()
Update date this search was last run.
SQL database abstraction object with smart query caching.
Definition: Database.php:22
GetSearchId()
Get search id.
Id()
Get ID of search.
static TranslateSearchGroupsToUrlParameterArray($SearchGroups)
Translate a search group array to an URL parameter array.
const SEARCHFREQ_QUARTERLY
Definition: SavedSearch.php:22
UserId($NewValue=DB_NOVALUE)
Get/set user ID.
Frequency($NewValue=DB_NOVALUE)
Get/set search frequency.
const SEARCHFREQ_YEARLY
Definition: SavedSearch.php:23
const SEARCHFREQ_HOURLY
Definition: SavedSearch.php:17
const SEARCHFREQ_BIWEEKLY
Definition: SavedSearch.php:20
static TranslateSearchGroupsToTextDescription($SearchGroups, $IncludeHtml=TRUE, $StartWithBreak=TRUE, $TruncateLongWordsTo=0)
Translate search group array into multi-line string describing search criteria.
const DB_NOVALUE
Definition: Database.php:1784
DateLastRun($NewValue=DB_NOVALUE)
Get/set the date this search was last run.
const SEARCHFREQ_MONTHLY
Definition: SavedSearch.php:21
static TranslateSearchGroupsToUrlParameters($SearchGroups)
Translate search group array into URL parameters (e.g.
GetSearchGroupsAsTextDescription($IncludeHtml=TRUE, $StartWithBreak=TRUE, $TruncateLongWordsTo=0)
Get multi-line string describing search criteria.
static NeatlyTruncateString($String, $MaxLength, $BreakAnywhere=FALSE)
Attempt to truncate a string as neatly as possible with respect to word breaks, punctuation, and HTML tags.
Definition: StdLib.php:239
SearchGroups($NewSearchGroups=NULL)
Get/set search parameters from legacy array.
GetSearchFieldNames()
Get list of fields to be searched.
__construct($SearchId, $SearchName=NULL, $UserId=NULL, $Frequency=NULL, $SearchParameters=NULL)
Object constructor.
Definition: SavedSearch.php:34
Delete()
Delete saved search.
SearchName($NewValue=DB_NOVALUE)
Get/set name of search.
GetSearchGroupsAsUrlParameterArray()
Get search groups as an URL parameter array.
GetSearchGroupsAsUrlParameters()
Get search groups as URL parameters (e.g.
LastMatches()
Return array of most recently matched ResourceIds for a search.
Factory for Resource objects.
CWIS-specific user class.
Definition: CWUser.php:13
SearchParameters($NewParams=NULL)
Get/set search parameters.
static GetSearchFrequencyList()
Get array of possible search frequency descriptions.
static TranslateSearchGroupsToSearchFieldNames($SearchGroups)
Extract list of fields to be searched from search group array.
SaveLastMatches($ArrayofMatchingIds)
Save array of last matches.