CWIS Developer Documentation
SavedSearch.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: SavedSearch.php
4 #
5 # NOTES:
6 # - the "$SearchGroups" values used herein contain a multi-dimentional
7 # array in the form of:
8 # $Criteria["MAIN"]["SearchStrings"][<field names>] = <value>
9 # for fields with a single value, and:
10 # $Criteria[<field ID>]["SearchStrings"][<field name>][] = <value>
11 # for fields with multiple values
12 #
13 #
14 # Part of the Collection Workflow Integration System (CWIS)
15 # Copyright 2011-2013 Edward Almasy and Internet Scout Research Group
16 # http://scout.wisc.edu/cwis/
17 #
18 
19 class SavedSearch {
20 
21  # ---- PUBLIC INTERFACE --------------------------------------------------
22 
23  # search frequency mnemonics
24  const SEARCHFREQ_NEVER = 0;
25  const SEARCHFREQ_HOURLY = 1;
26  const SEARCHFREQ_DAILY = 2;
27  const SEARCHFREQ_WEEKLY = 3;
29  const SEARCHFREQ_MONTHLY = 5;
31  const SEARCHFREQ_YEARLY = 7;
32 
33  # object constructor
34  function SavedSearch($SearchId, $SearchName = NULL, $UserId = NULL,
35  $Frequency = NULL, $SearchGroups = 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("SELECT * FROM SavedSearches"
48  ." WHERE SearchId = '".$this->SearchId."'");
49  $this->Record = $this->DB->FetchRow();
50 
51  # update search details where provided
52  if ($SearchName) { $this->SearchName($SearchName); }
53  if ($UserId) { $this->UserId($UserId); }
54  if ($Frequency) { $this->Frequency($Frequency); }
55  }
56  else
57  {
58  # add new saved search to database
59  $this->DB->Query("INSERT INTO SavedSearches"
60  ." (SearchName, UserId, Frequency) VALUES ("
61  ."'".addslashes($SearchName)."', "
62  .intval($UserId).", "
63  .intval($Frequency).")");
64 
65  # retrieve and save ID of new search locally
66  $this->SearchId = $this->DB->LastInsertId();
67 
68  # save frequency and user ID locally
69  $this->Record["SearchName"] = $SearchName;
70  $this->Record["UserId"] = $UserId;
71  $this->Record["Frequency"] = $Frequency;
72  }
73 
74  # save search parameters if provided
75  if ($SearchGroups) { $this->SearchGroups($SearchGroups); }
76  }
77 
78  # get/set search parameters
79  function SearchGroups($NewSearchGroups = NULL)
80  {
81  $Schema = new MetadataSchema();
82 
83  # if new search parameters were supplied
84  if ($NewSearchGroups)
85  {
86  # remove existing entries for this search from the database
87  $this->DB->Query("DELETE FROM SavedSearchTextParameters WHERE SearchId = ".$this->SearchId);
88  $this->DB->Query("DELETE FROM SavedSearchIdParameters WHERE SearchId = ".$this->SearchId);
89 
90  # for each search group
91  foreach ($NewSearchGroups as $GroupIndex => $Group)
92  {
93  # if group holds single parameters
94  if ($GroupIndex == "MAIN")
95  {
96  # for each field within group
97  foreach ($Group["SearchStrings"] as $FieldName => $Value)
98  {
99  # convert value array to single value (if necessary)
100  if (is_array($Value))
101  {
102  $ConvertedValue = "";
103  foreach ($Value as $SingleValue)
104  {
105  $ConvertedValue .= $SingleValue." ";
106  }
107  $Value = trim($ConvertedValue);
108  }
109 
110  # add new text search parameter entry to database
111  if ($FieldName == "XXXKeywordXXX")
112  {
113  $FieldId = -101;
114  }
115  else
116  {
117  $Field = $Schema->GetFieldByName($FieldName);
118  $FieldId = $Field->Id();
119  }
120  $this->DB->Query("INSERT INTO SavedSearchTextParameters"
121  ." (SearchId, FieldId, SearchText) VALUES"
122  ." (".$this->SearchId.", ".$FieldId.", '".addslashes($Value)."')");
123  }
124  }
125  else
126  {
127  # convert value(s) as appropriate for field type
128  $FieldId = ($GroupIndex[0] == "X")
129  ? substr($GroupIndex, 1)
130  : $GroupIndex;
131  $Field = $Schema->GetField($FieldId);
132  $FieldName = $Field->Name();
133  $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
134 
135  # for each converted value
136  foreach ($Values as $Value)
137  {
138  # add new ID search parameter entry to database
139  $this->DB->Query("INSERT INTO SavedSearchIdParameters"
140  ." (SearchId, FieldId, SearchValueId) VALUES"
141  ." (".$this->SearchId.", ".$FieldId.", ".$Value.")");
142  }
143  }
144  }
145 
146  # save search parameters locally
147  $this->SearchGroups = $NewSearchGroups;
148  }
149  else
150  {
151  # if search groups not already read in
152  if (!isset($this->SearchGroups))
153  {
154  # for each text search parameter
155  $SearchGroups = array();
156  $this->DB->Query("SELECT * FROM SavedSearchTextParameters"
157  ." WHERE SearchId = ".$this->SearchId);
158  while ($Record = $this->DB->FetchRow())
159  {
160  # add parameter to search criteria
161  if ($Record["FieldId"] == -101)
162  {
163  $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] =
164  $Record["SearchText"];
165  }
166  else
167  {
168  $Field = $Schema->GetField($Record["FieldId"]);
169  $SearchGroups["MAIN"]["SearchStrings"][$Field->Name()] =
170  $Record["SearchText"];
171  }
172  }
173 
174  # for each value ID search parameter
175  $this->DB->Query("SELECT * FROM SavedSearchIdParameters"
176  ." WHERE SearchId = ".$this->SearchId);
177  while ($Record = $this->DB->FetchRow())
178  {
179  # translate value based on field type
180  $FieldId = $Record["FieldId"];
181  if (!isset($Fields[$FieldId]))
182  {
183  $Fields[$FieldId] = $Schema->GetField($FieldId);
184  }
185  $Values = SavedSearch::TranslateValues($Fields[$FieldId],
186  $Record["SearchValueId"], "Database to SearchGroup");
187 
188  # add parameter to search criteria
189  foreach ($Values as $Value)
190  {
191  $SearchGroups[$FieldId]["SearchStrings"]
192  [$Fields[$FieldId]->Name()][] = $Value;
193  }
194  }
195 
196  # set appropriate logic in search parameters
197  foreach ($SearchGroups as $GroupIndex => $Group)
198  {
199  $SearchGroups[$GroupIndex]["Logic"] =
200  ($GroupIndex == "MAIN") ? SearchEngine::LOGIC_AND
202  }
203 
204  # save search parameters locally
205  $this->SearchGroups = $SearchGroups;
206  }
207  }
208 
209  # return search parameters to caller
210  return $this->SearchGroups;
211  }
212 
218  function SearchName($NewValue = DB_NOVALUE)
219  { return $this->UpdateValue("SearchName", $NewValue); }
220 
225  function Id() { return $this->SearchId; }
226 
232  function UserId($NewValue = DB_NOVALUE)
233  { return $this->UpdateValue("UserId", $NewValue); }
234 
240  function Frequency($NewValue = DB_NOVALUE)
241  { return $this->UpdateValue("Frequency", $NewValue); }
242 
243  # set date search was last run to current date/time
244  function UpdateDateLastRun()
245  {
246  $this->DB->Query("UPDATE SavedSearches SET DateLastRun = NOW() WHERE SearchId = ".$this->SearchId);
247  }
248 
249  # get/set date search was last run
250  function DateLastRun($NewValue = DB_NOVALUE)
251  { return $this->UpdateValue("DateLastRun", $NewValue); }
252 
259  {
260  return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
261  }
262 
300  static function TranslateSearchGroupsToUrlParameters($SearchGroups)
301  {
302  # assume that no parameters will be found
303  $UrlPortion = "";
304 
305  # for each group in parameters
306  $Schema = new MetadataSchema();
307  foreach ($SearchGroups as $GroupIndex => $Group)
308  {
309  # if group holds single parameters
310  if ($GroupIndex == "MAIN")
311  {
312  # for each field within group
313  foreach ($Group["SearchStrings"] as $FieldName => $Value)
314  {
315  # add segment to URL for this field
316  if ($FieldName == "XXXKeywordXXX")
317  {
318  $FieldId = "K";
319  }
320  else
321  {
322  $Field = $Schema->GetFieldByName($FieldName);
323  $FieldId = $Field->Id();
324  }
325  if (is_array($Value))
326  {
327  $UrlPortion .= "&F".$FieldId."=";
328  $ValueString = "";
329  foreach ($Value as $SingleValue)
330  {
331  $ValueString .= $SingleValue." ";
332  }
333  $UrlPortion .= urlencode(trim($ValueString));
334  }
335  else
336  {
337  $UrlPortion .= "&F".$FieldId."=".urlencode($Value);
338  }
339  }
340  }
341  else
342  {
343  # convert value based on field type
344  $FieldId = ($GroupIndex[0] == "X")
345  ? substr($GroupIndex, 1)
346  : $GroupIndex;
347  $Field = $Schema->GetField($FieldId);
348  $FieldName = $Field->Name();
349  $Values = SavedSearch::TranslateValues($Field,
350  $Group["SearchStrings"][$FieldName],
351  "SearchGroup to Database");
352 
353  # add values to URL
354  $FirstValue = TRUE;
355  foreach ($Values as $Value)
356  {
357  if ($FirstValue)
358  {
359  $FirstValue = FALSE;
360  $UrlPortion .= "&G".$FieldId."=".$Value;
361  }
362  else
363  {
364  $UrlPortion .= "-".$Value;
365  }
366  }
367  }
368  }
369 
370  # trim off any leading "&"
371  if (strlen($UrlPortion)) { $UrlPortion = substr($UrlPortion, 1); }
372 
373  # return URL portion to caller
374  return $UrlPortion;
375  }
376 
383  {
384  return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
385  }
386 
393  static function TranslateSearchGroupsToUrlParameterArray($SearchGroups)
394  {
395  # assume that no parameters will be found
396  $UrlPortion = array();
397 
398  # for each group in parameters
399  $Schema = new MetadataSchema();
400  foreach ($SearchGroups as $GroupIndex => $Group)
401  {
402  # if group holds single parameters
403  if ($GroupIndex == "MAIN")
404  {
405  # for each field within group
406  foreach ($Group["SearchStrings"] as $FieldName => $Value)
407  {
408  # add segment to URL for this field
409  if ($FieldName == "XXXKeywordXXX")
410  {
411  $FieldId = "K";
412  }
413  else
414  {
415  $Field = $Schema->GetFieldByName($FieldName);
416  $FieldId = $Field->Id();
417  }
418  if (is_array($Value))
419  {
420  $ValueString = "";
421  foreach ($Value as $SingleValue)
422  {
423  $ValueString .= $SingleValue." ";
424  }
425 
426  $UrlPortion["F".$FieldId] = urlencode(trim($ValueString));
427  }
428  else
429  {
430  $UrlPortion["F".$FieldId] = urlencode($Value);
431  }
432  }
433  }
434  else
435  {
436  # convert value based on field type
437  $FieldId = ($GroupIndex[0] == "X")
438  ? substr($GroupIndex, 1)
439  : $GroupIndex;
440  $Field = $Schema->GetField($FieldId);
441  $FieldName = $Field->Name();
442  $Values = SavedSearch::TranslateValues($Field,
443  $Group["SearchStrings"][$FieldName],
444  "SearchGroup to Database");
445  $LeadChar = ($Group["Logic"] == SearchEngine::LOGIC_AND)
446  ? "H" : "G";
447 
448  # add values to URL
449  $FirstValue = TRUE;
450  foreach ($Values as $Value)
451  {
452  if ($FirstValue)
453  {
454  $FirstValue = FALSE;
455  $UrlPortion[$LeadChar.$FieldId] = $Value;
456  }
457  else
458  {
459  $UrlPortion[$LeadChar.$FieldId] .= "-".$Value;
460  }
461  }
462  }
463  }
464 
465  # return URL portion to caller
466  return $UrlPortion;
467  }
468 
469  # set search groups from URL (GET method) parameters
470  # (returns search group array)
471  static function TranslateUrlParametersToSearchGroups($GetVars)
472  {
473  # if URL segment was passed in instead of GET var array
474  if (is_string($GetVars))
475  {
476  $GetVars = ParseQueryString($GetVars);
477  }
478 
479  # start with empty list of parameters
480  $SearchGroups = array();
481 
482  $Schema = new MetadataSchema();
483  $AllFields = $Schema->GetFields(NULL, NULL, TRUE);
484 
485  foreach ($AllFields as $Field)
486  {
487  $FieldId = $Field->Id();
488  $FieldName = $Field->Name();
489 
490  # if URL included literal value for this field
491  if (isset($GetVars["F".$FieldId]))
492  {
493  # retrieve value and add to search parameters
494  $SearchGroups["MAIN"]["SearchStrings"][$FieldName] =
495  $GetVars["F".$FieldId];
496  }
497 
498  # if URL included group value for this field
499  if (isset($GetVars["G".$FieldId]))
500  {
501  # retrieve and parse out values
502  $Values = explode("-", $GetVars["G".$FieldId]);
503 
504  # translate values
505  $Values = SavedSearch::TranslateValues($Field, $Values,
506  "Database to SearchGroup");
507 
508  # add values to searchgroups
509  $SearchGroups[$FieldId]["SearchStrings"][$FieldName] = $Values;
510  }
511 
512  # if URL included group value for this field
513  if (isset($GetVars["H".$FieldId]))
514  {
515  # retrieve and parse out values
516  $Values = explode("-", $GetVars["H".$FieldId]);
517 
518  # translate values
519  $Values = SavedSearch::TranslateValues($Field, $Values,
520  "Database to SearchGroup");
521 
522  # add values to searchgroups
523  $SearchGroups["X".$FieldId]["SearchStrings"][$FieldName] = $Values;
524  }
525  }
526 
527  # if keyword pseudo-field was included in URL
528  if (isset($GetVars["FK"]))
529  {
530  # retrieve value and add to search parameters
531  $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] = $GetVars["FK"];
532  }
533 
534  # set search logic
535  foreach ($SearchGroups as $GroupIndex => $Group)
536  {
537  $SearchGroups[$GroupIndex]["Logic"] = ($GroupIndex == "MAIN")
539  : (($GroupIndex[0] == "X")
541  }
542 
543  # return parameters to caller
544  return $SearchGroups;
545  }
546 
558  $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
559  {
560  return self::TranslateSearchGroupsToTextDescription($this->SearchGroups(),
561  $IncludeHtml, $StartWithBreak, $TruncateLongWordsTo);
562  }
563 
575  static function TranslateSearchGroupsToTextDescription($SearchGroups,
576  $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
577  {
578  $Schema = new MetadataSchema();
579 
580  # start with empty description
581  $Descrip = "";
582 
583  # set characters used to indicate literal strings
584  $LiteralStart = $IncludeHtml ? "<i>" : "\"";
585  $LiteralEnd = $IncludeHtml ? "</i>" : "\"";
586  $LiteralBreak = $IncludeHtml ? "<br>\n" : "\n";
587 
588  # if this is a simple keyword search
589  if (isset($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"])
590  && (count($SearchGroups) == 1)
591  && (count($SearchGroups["MAIN"]["SearchStrings"]) == 1))
592  {
593  # just use the search string
594  $Descrip .= $LiteralStart;
595  $Descrip .= defaulthtmlentities($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"]);
596  $Descrip .= $LiteralEnd . $LiteralBreak;
597  }
598  else
599  {
600  # start description on a new line (if requested)
601  if ($StartWithBreak)
602  {
603  $Descrip .= $LiteralBreak;
604  }
605 
606  # define list of phrases used to represent logical operators
607  $WordsForOperators = array(
608  "=" => "is",
609  ">" => "is greater than",
610  "<" => "is less than",
611  ">=" => "is at least",
612  "<=" => "is no more than",
613  "!" => "is not",
614  );
615 
616  # for each search group
617  foreach ($SearchGroups as $GroupIndex => $Group)
618  {
619  # if group is main
620  if ($GroupIndex == "MAIN")
621  {
622  # for each field in group
623  foreach ($Group["SearchStrings"] as $FieldName => $Value)
624  {
625  # determine wording based on operator
626  preg_match("/^[=><!]+/", $Value, $Matches);
627  if (count($Matches) && isset($WordsForOperators[$Matches[0]]))
628  {
629  $Value = preg_replace("/^[=><!]+/", "", $Value);
630  $Wording = $WordsForOperators[$Matches[0]];
631  }
632  else
633  {
634  $Wording = "contains";
635  }
636 
637  # if field is psuedo-field
638  if ($FieldName == "XXXKeywordXXX")
639  {
640  # add criteria for psuedo-field
641  $Descrip .= "Keyword ".$Wording." "
642  .$LiteralStart.htmlspecialchars($Value)
643  .$LiteralEnd.$LiteralBreak;
644  }
645  else
646  {
647  # if field is valid
648  $Field = $Schema->GetFieldByName($FieldName);
649  if ($Field !== NULL)
650  {
651  # add criteria for field
652  $Descrip .= $Field->GetDisplayName()." ".$Wording." "
653  .$LiteralStart.htmlspecialchars($Value)
654  .$LiteralEnd.$LiteralBreak;
655  }
656  }
657  }
658  }
659  else
660  {
661  # for each field in group
662  $LogicTerm = ($Group["Logic"] == SearchEngine::LOGIC_AND)
663  ? "and " : "or ";
664  foreach ($Group["SearchStrings"] as $FieldName => $Values)
665  {
666  # translate values
667  $Values = SavedSearch::TranslateValues(
668  $FieldName, $Values, "SearchGroup to Display");
669 
670  # for each value
671  $FirstValue = TRUE;
672  foreach ($Values as $Value)
673  {
674  # determine wording based on operator
675  preg_match("/^[=><!]+/", $Value, $Matches);
676  $Operator = $Matches[0];
677  $Wording = $WordsForOperators[$Operator];
678 
679  # strip off operator
680  $Value = preg_replace("/^[=><!]+/", "", $Value);
681 
682  # add text to description
683  if ($FirstValue)
684  {
685  $Descrip .= $FieldName." ".$Wording." "
686  .$LiteralStart.htmlspecialchars($Value)
687  .$LiteralEnd.$LiteralBreak;
688  $FirstValue = FALSE;
689  }
690  else
691  {
692  $Descrip .= ($IncludeHtml ? "&nbsp;&nbsp;&nbsp;&nbsp;" : " ")
693  .$LogicTerm.$Wording." ".$LiteralStart
694  .htmlspecialchars($Value).$LiteralEnd
695  .$LiteralBreak;
696  }
697  }
698  }
699  }
700  }
701  }
702 
703  # if caller requested that long words be truncated
704  if ($TruncateLongWordsTo > 4)
705  {
706  # break description into words
707  $Words = explode(" ", $Descrip);
708 
709  # for each word
710  $NewDescrip = "";
711  foreach ($Words as $Word)
712  {
713  # if word is longer than specified length
714  if (strlen(strip_tags($Word)) > $TruncateLongWordsTo)
715  {
716  # truncate word and add ellipsis
717  $Word = NeatlyTruncateString($Word, $TruncateLongWordsTo - 3);
718  }
719 
720  # add word to new description
721  $NewDescrip .= " ".$Word;
722  }
723 
724  # set description to new description
725  $Descrip = $NewDescrip;
726  }
727 
728  # return description to caller
729  return $Descrip;
730  }
731 
737  {
738  return self::TranslateSearchGroupsToSearchFieldNames($this->SearchGroups());
739  }
740 
746  static function TranslateSearchGroupsToSearchFieldNames($SearchGroups)
747  {
748  # start out assuming no fields are being searched
749  $FieldNames = array();
750 
751  # for each search group defined
752  foreach ($SearchGroups as $GroupIndex => $Group)
753  {
754  # for each field in group
755  foreach ($Group["SearchStrings"] as $FieldName => $Values)
756  {
757  # add field name to list of fields being searched
758  $FieldNames[] = $FieldName;
759  }
760  }
761 
762  # return list of fields being searched to caller
763  return $FieldNames;
764  }
765 
771  static function GetSearchFrequencyList()
772  {
773  # define list with descriptions
774  $FreqDescr = array(
775  self::SEARCHFREQ_NEVER => "Never",
776  self::SEARCHFREQ_HOURLY => "Hourly",
777  self::SEARCHFREQ_DAILY => "Daily",
778  self::SEARCHFREQ_WEEKLY => "Weekly",
779  self::SEARCHFREQ_BIWEEKLY => "Bi-Weekly",
780  self::SEARCHFREQ_MONTHLY => "Monthly",
781  self::SEARCHFREQ_QUARTERLY => "Quarterly",
782  self::SEARCHFREQ_YEARLY => "Yearly",
783  );
784 
785  # for each argument passed in
786  $Args = func_get_args();
787  foreach ($Args as $Arg)
788  {
789  # remove value from list
790  $FreqDescr = array_diff_key($FreqDescr, array($Arg => ""));
791  }
792 
793  # return list to caller
794  return $FreqDescr;
795  }
796 
800  function Delete()
801  {
802  $this->DB->Query("DELETE FROM SavedSearches"
803  ." WHERE SearchId = ".intval($this->SearchId));
804  $this->DB->Query("DELETE FROM SavedSearchTextParameters"
805  ." WHERE SearchId = ".intval($this->SearchId));
806  $this->DB->Query("DELETE FROM SavedSearchIdParameters"
807  ." WHERE SearchId = ".intval($this->SearchId));
808  }
809 
810 
811  # ---- PRIVATE INTERFACE -------------------------------------------------
812 
813  private $SearchId;
814  private $Record;
815  private $SearchGroups;
816 
817  # utility function to convert between value representations
818  # (method accepts a value or array and always return an array)
819  # (this is needed because values are represented differently:
820  # FLAG USER OPTION
821  # in DB / in URL / in forms 0/1 123 456
822  # used in SearchGroups 0/1 jdoe cname
823  # displayed to user On/Off jdoe cname
824  # where "123" and "456" are option or controlled name IDs)
825  private static function TranslateValues($FieldOrFieldName, $Values, $TranslationType)
826  {
827  # start out assuming we won't find any values to translate
828  $ReturnValues = array();
829 
830  # convert field name to field object if necessary
831  if (is_object($FieldOrFieldName))
832  {
833  $Field = $FieldOrFieldName;
834  }
835  else
836  {
837  static $Schema;
838  if (!isset($Schema)) { $Schema = new MetadataSchema(); }
839  $Field = $Schema->GetFieldByName($FieldOrFieldName);
840  }
841 
842  # if incoming value is not an array
843  if (!is_array($Values))
844  {
845  # convert incoming value to an array
846  $Values = array($Values);
847  }
848 
849  # for each incoming value
850  foreach ($Values as $Value)
851  {
852  switch ($TranslationType)
853  {
854  case "SearchGroup to Display":
855  # if field is Flag field
856  if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
857  {
858  # translate value to true/false label and add leading operator
859  $ReturnValues[] = ($Value == "=1") ? "=".$Field->FlagOnLabel() : "=".$Field->FlagOffLabel();
860  }
861  elseif ($Field->Name() == "Cumulative Rating")
862  {
863  # translate numeric value to stars
864  $StarStrings = array(
865  "20" => "*",
866  "40" => "**",
867  "60" => "***",
868  "80" => "****",
869  "100" => "*****",
870  );
871  preg_match("/[0-9]+$/", $Value, $Matches);
872  $Number = $Matches[0];
873  preg_match("/^[=><!]+/", $Value, $Matches);
874  $Operator = $Matches[0];
875  $ReturnValues[] = $Operator.$StarStrings[$Number];
876  }
877  else
878  {
879  # use value as is
880  $ReturnValues[] = $Value;
881  }
882  break;
883 
884  case "SearchGroup to Database":
885  # strip off leading operator on value
886  $Value = preg_replace("/^[=><!]+/", "", $Value);
887 
888  # look up index for value
890  {
891  # (for flag or number fields the value index is already 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 what is used in Database)
933  if ($Value >= 0)
934  {
935  $ReturnValues[] = "=".$Value;
936  }
937  }
938  elseif ($Field->Type() == MetadataSchema::MDFTYPE_NUMBER)
939  {
940  # (for flag fields the value index (0 or 1) is already what is used in Database)
941  if ($Value >= 0)
942  {
943  $ReturnValues[] = ">=".$Value;
944  }
945  }
946  elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
947  {
948  $User = new CWUser(intval($Value));
949  if ($User)
950  {
951  $ReturnValues[] = "=".$User->Get("UserName");
952  }
953  }
954  elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
955  {
956  if (!isset($PossibleFieldValues))
957  {
958  $PossibleFieldValues = $Field->GetPossibleValues();
959  }
960 
961  if (isset($PossibleFieldValues[$Value]))
962  {
963  $ReturnValues[] = "=".$PossibleFieldValues[$Value];
964  }
965  }
966  else
967  {
968  $NewValue = $Field->GetValueForId($Value);
969  if ($NewValue !== NULL)
970  {
971  $ReturnValues[] = "=".$NewValue;
972  }
973  }
974  break;
975  }
976  }
977 
978  # return array of translated values to caller
979  return $ReturnValues;
980  }
981 
984  # utility function for updating values in database
985  private function UpdateValue($FieldName, $NewValue)
986  {
987  return $this->DB->UpdateValue("SavedSearches", $FieldName, $NewValue,
988  "SearchId = ".$this->SearchId, $this->Record);
989  }
990 
991  # legacy methods for backward compatibility
992  function GetSearchId() { return $this->Id(); }
993 
995 }
const SEARCHFREQ_WEEKLY
Definition: SavedSearch.php:27
const SEARCHFREQ_NEVER
Definition: SavedSearch.php:24
static TranslateUrlParametersToSearchGroups($GetVars)
Metadata schema (in effect a Factory class for MetadataField).
const SEARCHFREQ_DAILY
Definition: SavedSearch.php:26
SQL database abstraction object with smart query caching.
Id()
Get ID of search.
const DB_NOVALUE
static TranslateSearchGroupsToUrlParameterArray($SearchGroups)
Translate a search group array to an URL parameter array.
const SEARCHFREQ_QUARTERLY
Definition: SavedSearch.php:30
UserId($NewValue=DB_NOVALUE)
Get/set user ID.
Frequency($NewValue=DB_NOVALUE)
Get/set search frequency.
const SEARCHFREQ_YEARLY
Definition: SavedSearch.php:31
SavedSearch($SearchId, $SearchName=NULL, $UserId=NULL, $Frequency=NULL, $SearchGroups=NULL)
Definition: SavedSearch.php:34
PHP
Definition: OAIClient.php:39
const SEARCHFREQ_HOURLY
Definition: SavedSearch.php:25
const SEARCHFREQ_BIWEEKLY
Definition: SavedSearch.php:28
static TranslateSearchGroupsToTextDescription($SearchGroups, $IncludeHtml=TRUE, $StartWithBreak=TRUE, $TruncateLongWordsTo=0)
Translate search group array into multi-line string describing search criteria.
DateLastRun($NewValue=DB_NOVALUE)
const SEARCHFREQ_MONTHLY
Definition: SavedSearch.php:29
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.
SearchGroups($NewSearchGroups=NULL)
Definition: SavedSearch.php:79
GetSearchFieldNames()
Get list of fields to be searched.
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.
CWIS-specific user class.
Definition: CWUser.php:13
static GetSearchFrequencyList()
Get array of possible search frequency descriptions.
static TranslateSearchGroupsToSearchFieldNames($SearchGroups)
Extract list of fields to be searched from search group array.