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 # Part of the Collection Workflow Integration System (CWIS)
14 # Copyright 2011 Edward Almasy and Internet Scout Project
15 # http://scout.wisc.edu/
16 #
17 
18 class SavedSearch {
19 
20  # ---- PUBLIC INTERFACE --------------------------------------------------
21 
22  # search frequency mnemonics
23  const SEARCHFREQ_NEVER = 0;
24  const SEARCHFREQ_HOURLY = 1;
25  const SEARCHFREQ_DAILY = 2;
26  const SEARCHFREQ_WEEKLY = 3;
28  const SEARCHFREQ_MONTHLY = 5;
30  const SEARCHFREQ_YEARLY = 7;
31 
32  # object constructor
33  function SavedSearch($SearchId, $SearchName = NULL, $UserId = NULL,
34  $Frequency = NULL, $SearchGroups = NULL)
35  {
36  # get our own database handle
37  $this->DB = new Database();
38 
39  # if search ID was provided
40  if ($SearchId !== NULL)
41  {
42  # save search ID
43  $this->SearchId = intval($SearchId);
44 
45  # initialize our local copies of data
46  $this->DB->Query("SELECT * FROM SavedSearches"
47  ." WHERE SearchId = '".$this->SearchId."'");
48  $this->Record = $this->DB->FetchRow();
49 
50  # update search details where provided
51  if ($SearchName) { $this->SearchName($SearchName); }
52  if ($UserId) { $this->UserId($UserId); }
53  if ($Frequency) { $this->Frequency($Frequency); }
54  }
55  else
56  {
57  # add new saved search to database
58  $this->DB->Query("INSERT INTO SavedSearches"
59  ." (SearchName, UserId, Frequency) VALUES ("
60  ."'".addslashes($SearchName)."', "
61  .intval($UserId).", "
62  .intval($Frequency).")");
63 
64  # retrieve and save ID of new search locally
65  $this->SearchId = $this->DB->LastInsertId("SavedSearches");
66 
67  # save frequency and user ID locally
68  $this->Record["SearchName"] = $SearchName;
69  $this->Record["UserId"] = $UserId;
70  $this->Record["Frequency"] = $Frequency;
71  }
72 
73  # save search parameters if provided
74  if ($SearchGroups) { $this->SearchGroups($SearchGroups); }
75  }
76 
77  # get/set search parameters
78  function SearchGroups($NewSearchGroups = NULL)
79  {
80  $Schema = new MetadataSchema();
81 
82  # if new search parameters were supplied
83  if ($NewSearchGroups)
84  {
85  # remove existing entries for this search from the database
86  $this->DB->Query("DELETE FROM SavedSearchTextParameters WHERE SearchId = ".$this->SearchId);
87  $this->DB->Query("DELETE FROM SavedSearchIdParameters WHERE SearchId = ".$this->SearchId);
88 
89  # for each search group
90  foreach ($NewSearchGroups as $GroupIndex => $Group)
91  {
92  # if group holds single parameters
93  if ($GroupIndex == "MAIN")
94  {
95  # for each field within group
96  foreach ($Group["SearchStrings"] as $FieldName => $Value)
97  {
98  # convert value array to single value (if necessary)
99  if (is_array($Value))
100  {
101  $ConvertedValue = "";
102  foreach ($Value as $SingleValue)
103  {
104  $ConvertedValue .= $SingleValue." ";
105  }
106  $Value = trim($ConvertedValue);
107  }
108 
109  # add new text search parameter entry to database
110  if ($FieldName == "XXXKeywordXXX")
111  {
112  $FieldId = -101;
113  }
114  else
115  {
116  $Field = $Schema->GetFieldByName($FieldName);
117  $FieldId = $Field->Id();
118  }
119  $this->DB->Query("INSERT INTO SavedSearchTextParameters"
120  ." (SearchId, FieldId, SearchText) VALUES"
121  ." (".$this->SearchId.", ".$FieldId.", '".addslashes($Value)."')");
122  }
123  }
124  else
125  {
126  # convert value(s) as appropriate for field type
127  $FieldId = $GroupIndex;
128  $Field = $Schema->GetField($FieldId);
129  $FieldName = $Field->Name();
130  $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
131 
132  # for each converted value
133  foreach ($Values as $Value)
134  {
135  # add new ID search parameter entry to database
136  $this->DB->Query("INSERT INTO SavedSearchIdParameters"
137  ." (SearchId, FieldId, SearchValueId) VALUES"
138  ." (".$this->SearchId.", ".$FieldId.", ".$Value.")");
139  }
140  }
141  }
142 
143  # save search parameters locally
144  $this->SearchGroups = $NewSearchGroups;
145  }
146  else
147  {
148  # if search groups not already read in
149  if (!isset($this->SearchGroups))
150  {
151  # for each text search parameter
152  $SearchGroups = array();
153  $this->DB->Query("SELECT * FROM SavedSearchTextParameters WHERE SearchId = ".$this->SearchId);
154  while ($Record = $this->DB->FetchRow())
155  {
156  # add parameter to search criteria
157  if ($Record["FieldId"] == -101)
158  {
159  $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] =
160  $Record["SearchText"];
161  }
162  else
163  {
164  $Field = $Schema->GetField($Record["FieldId"]);
165  $SearchGroups["MAIN"]["SearchStrings"][$Field->Name()] =
166  $Record["SearchText"];
167  }
168  }
169 
170  # for each value ID search parameter
171  $this->DB->Query("SELECT * FROM SavedSearchIdParameters WHERE SearchId = ".$this->SearchId);
172  while ($Record = $this->DB->FetchRow())
173  {
174  # translate value based on field type
175  $FieldId = $Record["FieldId"];
176  if (!isset($Fields[$FieldId])) { $Fields[$FieldId] = new MetadataField($FieldId); }
177  $Values = SavedSearch::TranslateValues($Fields[$FieldId],
178  $Record["SearchValueId"], "Database to SearchGroup");
179 
180  # add parameter to search criteria
181  foreach ($Values as $Value)
182  {
183  $SearchGroups[$FieldId]["SearchStrings"][$Fields[$FieldId]->Name()][] = $Value;
184  }
185  }
186 
187  # set appropriate logic in search parameters
188  foreach ($SearchGroups as $GroupIndex => $Group)
189  {
190  $SearchGroups[$GroupIndex]["Logic"] =
191  ($GroupIndex == "MAIN") ? SearchEngine::LOGIC_AND
193  }
194 
195  # save search parameters locally
196  $this->SearchGroups = $SearchGroups;
197  }
198  }
199 
200  # return search parameters to caller
201  return $this->SearchGroups;
202  }
203 
209  function SearchName($NewValue = DB_NOVALUE)
210  { return $this->UpdateValue("SearchName", $NewValue); }
211 
216  function Id() { return $this->SearchId; }
217 
223  function UserId($NewValue = DB_NOVALUE)
224  { return $this->UpdateValue("UserId", $NewValue); }
225 
231  function Frequency($NewValue = DB_NOVALUE)
232  { return $this->UpdateValue("Frequency", $NewValue); }
233 
234  # set date search was last run to current date/time
235  function UpdateDateLastRun()
236  {
237  $this->DB->Query("UPDATE SavedSearches SET DateLastRun = NOW() WHERE SearchId = ".$this->SearchId);
238  }
239 
240  # get/set date search was last run
241  function DateLastRun($NewValue = DB_NOVALUE)
242  { return $this->UpdateValue("DateLastRun", $NewValue); }
243 
250  {
251  return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
252  }
253 
260  static function TranslateSearchGroupsToUrlParameters($SearchGroups)
261  {
262  # assume that no parameters will be found
263  $UrlPortion = "";
264 
265  # for each group in parameters
266  $Schema = new MetadataSchema();
267  foreach ($SearchGroups as $GroupIndex => $Group)
268  {
269  # if group holds single parameters
270  if ($GroupIndex == "MAIN")
271  {
272  # for each field within group
273  foreach ($Group["SearchStrings"] as $FieldName => $Value)
274  {
275  # add segment to URL for this field
276  if ($FieldName == "XXXKeywordXXX")
277  {
278  $FieldId = "K";
279  }
280  else
281  {
282  $Field = $Schema->GetFieldByName($FieldName);
283  $FieldId = $Field->Id();
284  }
285  if (is_array($Value))
286  {
287  $UrlPortion .= "&F".$FieldId."=";
288  $ValueString = "";
289  foreach ($Value as $SingleValue)
290  {
291  $ValueString .= $SingleValue." ";
292  }
293  $UrlPortion .= urlencode(trim($ValueString));
294  }
295  else
296  {
297  $UrlPortion .= "&F".$FieldId."=".urlencode($Value);
298  }
299  }
300  }
301  else
302  {
303  # convert value based on field type
304  $FieldId = $GroupIndex;
305  $Field = $Schema->GetField($FieldId);
306  $FieldName = $Field->Name();
307  $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
308 
309  # add values to URL
310  $FirstValue = TRUE;
311  foreach ($Values as $Value)
312  {
313  if ($FirstValue)
314  {
315  $FirstValue = FALSE;
316  $UrlPortion .= "&G".$FieldId."=".$Value;
317  }
318  else
319  {
320  $UrlPortion .= "-".$Value;
321  }
322  }
323  }
324  }
325 
326  # trim off any leading "&"
327  if (strlen($UrlPortion)) { $UrlPortion = substr($UrlPortion, 1); }
328 
329  # return URL portion to caller
330  return $UrlPortion;
331  }
332 
339  {
340  return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
341  }
342 
349  static function TranslateSearchGroupsToUrlParameterArray($SearchGroups)
350  {
351  # assume that no parameters will be found
352  $UrlPortion = array();
353 
354  # for each group in parameters
355  $Schema = new MetadataSchema();
356  foreach ($SearchGroups as $GroupIndex => $Group)
357  {
358  # if group holds single parameters
359  if ($GroupIndex == "MAIN")
360  {
361  # for each field within group
362  foreach ($Group["SearchStrings"] as $FieldName => $Value)
363  {
364  # add segment to URL for this field
365  if ($FieldName == "XXXKeywordXXX")
366  {
367  $FieldId = "K";
368  }
369  else
370  {
371  $Field = $Schema->GetFieldByName($FieldName);
372  $FieldId = $Field->Id();
373  }
374  if (is_array($Value))
375  {
376  $ValueString = "";
377  foreach ($Value as $SingleValue)
378  {
379  $ValueString .= $SingleValue." ";
380  }
381 
382  $UrlPortion["F".$FieldId] = urlencode(trim($ValueString));
383  }
384  else
385  {
386  $UrlPortion["F".$FieldId] = urlencode($Value);
387  }
388  }
389  }
390  else
391  {
392  # convert value based on field type
393  $FieldId = $GroupIndex;
394  $Field = $Schema->GetField($FieldId);
395  $FieldName = $Field->Name();
396  $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
397 
398  # add values to URL
399  $FirstValue = TRUE;
400  foreach ($Values as $Value)
401  {
402  if ($FirstValue)
403  {
404  $FirstValue = FALSE;
405  $UrlPortion["G".$FieldId] = $Value;
406  }
407  else
408  {
409  $UrlPortion["G".$FieldId] .= "-".$Value;
410  }
411  }
412  }
413  }
414 
415  # return URL portion to caller
416  return $UrlPortion;
417  }
418 
419  # set search groups from URL (GET method) parameters
420  # (returns search group array)
421  static function TranslateUrlParametersToSearchGroups($GetVars)
422  {
423  # if URL segment was passed in instead of GET var array
424  if (is_string($GetVars))
425  {
426  parse_str($GetVars, $GetVars);
427  }
428 
429  # start with empty list of parameters
430  $SearchGroups = array();
431 
432  $Schema = new MetadataSchema();
433  $AllFields = $Schema->GetFields(NULL, NULL, TRUE);
434 
435  foreach ($AllFields as $Field)
436  {
437  $FieldId = $Field->Id();
438  $FieldName = $Field->Name();
439 
440  # if URL included literal value for this field
441  if (isset($GetVars["F".$FieldId]))
442  {
443  # retrieve value and add to search parameters
444  $SearchGroups["MAIN"]["SearchStrings"][$FieldName] = $GetVars["F".$FieldId];
445  }
446 
447  # if URL included group value for this field
448  if (isset($GetVars["G".$FieldId]))
449  {
450  # retrieve and parse out values
451  $Values = explode("-", $GetVars["G".$FieldId]);
452 
453  # translate values
454  $Values = SavedSearch::TranslateValues($Field, $Values, "Database to SearchGroup");
455 
456  # add values to searchgroups
457  $SearchGroups[$FieldId]["SearchStrings"][$FieldName] = $Values;
458  }
459  }
460 
461  # if keyword pseudo-field was included in URL
462  if (isset($GetVars["FK"]))
463  {
464  # retrieve value and add to search parameters
465  $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] = $GetVars["FK"];
466  }
467 
468  # set search logic
469  foreach ($SearchGroups as $GroupIndex => $Group)
470  {
471  $SearchGroups[$GroupIndex]["Logic"] = ($GroupIndex == "MAIN")
473  }
474 
475  # return parameters to caller
476  return $SearchGroups;
477  }
478 
490  $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
491  {
492  return self::TranslateSearchGroupsToTextDescription($this->SearchGroups(),
493  $IncludeHtml, $StartWithBreak, $TruncateLongWordsTo);
494  }
495 
507  static function TranslateSearchGroupsToTextDescription($SearchGroups,
508  $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
509  {
510  $Schema = new MetadataSchema();
511 
512  # start with empty description
513  $Descrip = "";
514 
515  # set characters used to indicate literal strings
516  $LiteralStart = $IncludeHtml ? "<i>" : "\"";
517  $LiteralEnd = $IncludeHtml ? "</i>" : "\"";
518  $LiteralBreak = $IncludeHtml ? "<br>\n" : "\n";
519 
520  # if this is a simple keyword search
521  if (isset($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"])
522  && (count($SearchGroups) == 1)
523  && (count($SearchGroups["MAIN"]["SearchStrings"]) == 1))
524  {
525  # just use the search string
526  $Descrip .= $LiteralStart;
527  $Descrip .= defaulthtmlentities($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"]);
528  $Descrip .= $LiteralEnd . $LiteralBreak;
529  }
530  else
531  {
532  # start description on a new line (if requested)
533  if ($StartWithBreak)
534  {
535  $Descrip .= $LiteralBreak;
536  }
537 
538  # define list of phrases used to represent logical operators
539  $WordsForOperators = array(
540  "=" => "is",
541  ">" => "is greater than",
542  "<" => "is less than",
543  ">=" => "is at least",
544  "<=" => "is no more than",
545  "!" => "is not",
546  );
547 
548  # for each search group
549  foreach ($SearchGroups as $GroupIndex => $Group)
550  {
551  # if group is main
552  if ($GroupIndex == "MAIN")
553  {
554  # for each field in group
555  foreach ($Group["SearchStrings"] as $FieldName => $Value)
556  {
557  # convert keyword pseudo-field name if necessary
558  if ($FieldName == "XXXKeywordXXX") { $FieldName = "Keyword"; }
559 
560  # determine wording based on operator
561  preg_match("/^[=><!]+/", $Value, $Matches);
562  if (count($Matches) && isset($WordsForOperators[$Matches[0]]))
563  {
564  $Value = preg_replace("/^[=><!]+/", "", $Value);
565  $Wording = $WordsForOperators[$Matches[0]];
566  }
567  else
568  {
569  $Wording = "contains";
570  }
571 
572  $Field = $Schema->GetFieldByName($FieldName);
573 
574  # get display name for field
575  if ($Field && $Field->Status() == MetadataSchema::MDFSTAT_OK)
576  {
577  $FieldName = $Field->GetDisplayName();
578  }
579 
580  # add criteria for field
581  $Descrip .= $FieldName." ".$Wording." "
582  .$LiteralStart.htmlspecialchars($Value)
583  .$LiteralEnd.$LiteralBreak;
584  }
585  }
586  else
587  {
588  # for each field in group
589  foreach ($Group["SearchStrings"] as $FieldName => $Values)
590  {
591  # translate values
592  $Values = SavedSearch::TranslateValues($FieldName, $Values, "SearchGroup to Display");
593 
594  # for each value
595  $FirstValue = TRUE;
596  foreach ($Values as $Value)
597  {
598  # determine wording based on operator
599  preg_match("/^[=><!]+/", $Value, $Matches);
600  $Operator = $Matches[0];
601  $Wording = $WordsForOperators[$Operator];
602 
603  # strip off operator
604  $Value = preg_replace("/^[=><!]+/", "", $Value);
605 
606  # add text to description
607  if ($FirstValue)
608  {
609  $Descrip .= $FieldName." ".$Wording." ".$LiteralStart.htmlspecialchars($Value).$LiteralEnd.$LiteralBreak;
610  $FirstValue = FALSE;
611  }
612  else
613  {
614  $Descrip .= ($IncludeHtml ? "&nbsp;&nbsp;&nbsp;&nbsp;" : " ")
615  ."or ".$Wording." ".$LiteralStart
616  .htmlspecialchars($Value).$LiteralEnd
617  .$LiteralBreak;
618  }
619  }
620  }
621  }
622  }
623  }
624 
625  # if caller requested that long words be truncated
626  if ($TruncateLongWordsTo > 4)
627  {
628  # break description into words
629  $Words = explode(" ", $Descrip);
630 
631  # for each word
632  $NewDescrip = "";
633  foreach ($Words as $Word)
634  {
635  # if word is longer than specified length
636  if (strlen(strip_tags($Word)) > $TruncateLongWordsTo)
637  {
638  # truncate word and add ellipsis
639  $Word = NeatlyTruncateString($Word, $TruncateLongWordsTo - 3);
640  }
641 
642  # add word to new description
643  $NewDescrip .= " ".$Word;
644  }
645 
646  # set description to new description
647  $Descrip = $NewDescrip;
648  }
649 
650  # return description to caller
651  return $Descrip;
652  }
653 
659  {
660  return self::TranslateSearchGroupsToSearchFieldNames($this->SearchGroups());
661  }
662 
668  static function TranslateSearchGroupsToSearchFieldNames($SearchGroups)
669  {
670  # start out assuming no fields are being searched
671  $FieldNames = array();
672 
673  # for each search group defined
674  foreach ($SearchGroups as $GroupIndex => $Group)
675  {
676  # for each field in group
677  foreach ($Group["SearchStrings"] as $FieldName => $Values)
678  {
679  # add field name to list of fields being searched
680  $FieldNames[] = $FieldName;
681  }
682  }
683 
684  # return list of fields being searched to caller
685  return $FieldNames;
686  }
687 
693  static function GetSearchFrequencyList()
694  {
695  # define list with descriptions
696  $FreqDescr = array(
697  self::SEARCHFREQ_NEVER => "Never",
698  self::SEARCHFREQ_HOURLY => "Hourly",
699  self::SEARCHFREQ_DAILY => "Daily",
700  self::SEARCHFREQ_WEEKLY => "Weekly",
701  self::SEARCHFREQ_BIWEEKLY => "Bi-Weekly",
702  self::SEARCHFREQ_MONTHLY => "Monthly",
703  self::SEARCHFREQ_QUARTERLY => "Quarterly",
704  self::SEARCHFREQ_YEARLY => "Yearly",
705  );
706 
707  # for each argument passed in
708  $Args = func_get_args();
709  foreach ($Args as $Arg)
710  {
711  # remove value from list
712  $FreqDescr = array_diff_key($FreqDescr, array($Arg => ""));
713  }
714 
715  # return list to caller
716  return $FreqDescr;
717  }
718 
722  function Delete()
723  {
724  $this->DB->Query("DELETE FROM SavedSearches"
725  ." WHERE SearchId = ".intval($this->SearchId));
726  $this->DB->Query("DELETE FROM SavedSearchTextParameters"
727  ." WHERE SearchId = ".intval($this->SearchId));
728  $this->DB->Query("DELETE FROM SavedSearchIdParameters"
729  ." WHERE SearchId = ".intval($this->SearchId));
730  }
731 
732 
733  # ---- PRIVATE INTERFACE -------------------------------------------------
734 
735  private $SearchId;
736  private $Record;
737  private $SearchGroups;
738 
739  # utility function to convert between value representations
740  # (method accepts a value or array and always return an array)
741  # (this is needed because values are represented differently:
742  # FLAG USER OPTION
743  # in DB / in URL / in forms 0/1 123 456
744  # used in SearchGroups 0/1 jdoe cname
745  # displayed to user On/Off jdoe cname
746  # where "123" and "456" are option or controlled name IDs)
747  private static function TranslateValues($FieldOrFieldName, $Values, $TranslationType)
748  {
749  # start out assuming we won't find any values to translate
750  $ReturnValues = array();
751 
752  # convert field name to field object if necessary
753  if (is_object($FieldOrFieldName))
754  {
755  $Field = $FieldOrFieldName;
756  }
757  else
758  {
759  static $Schema;
760  if (!isset($Schema)) { $Schema = new MetadataSchema(); }
761  $Field = $Schema->GetFieldByName($FieldOrFieldName);
762  }
763 
764  # if incoming value is not an array
765  if (!is_array($Values))
766  {
767  # convert incoming value to an array
768  $Values = array($Values);
769  }
770 
771  # for each incoming value
772  foreach ($Values as $Value)
773  {
774  switch ($TranslationType)
775  {
776  case "SearchGroup to Display":
777  # if field is Flag field
778  if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
779  {
780  # translate value to true/false label and add leading operator
781  $ReturnValues[] = ($Value == "=1") ? "=".$Field->FlagOnLabel() : "=".$Field->FlagOffLabel();
782  }
783  elseif ($Field->Name() == "Cumulative Rating")
784  {
785  # translate numeric value to stars
786  $StarStrings = array(
787  "20" => "*",
788  "40" => "**",
789  "60" => "***",
790  "80" => "****",
791  "100" => "*****",
792  );
793  preg_match("/[0-9]+$/", $Value, $Matches);
794  $Number = $Matches[0];
795  preg_match("/^[=><!]+/", $Value, $Matches);
796  $Operator = $Matches[0];
797  $ReturnValues[] = $Operator.$StarStrings[$Number];
798  }
799  else
800  {
801  # use value as is
802  $ReturnValues[] = $Value;
803  }
804  break;
805 
806  case "SearchGroup to Database":
807  # strip off leading operator on value
808  $Value = preg_replace("/^[=><!]+/", "", $Value);
809 
810  # look up index for value
812  {
813  # (for flag or number fields the value index is already what is used in SearchGroups)
814  if ($Value >= 0)
815  {
816  $ReturnValues[] = $Value;
817  }
818  }
819  elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
820  {
821  # (for user fields the value index is the user ID)
822  $User = new SPTUser(strval($Value));
823  if ($User)
824  {
825  $ReturnValues[] = $User->Id();
826  }
827  }
828  elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
829  {
830  if (!isset($PossibleFieldValues))
831  {
832  $PossibleFieldValues = $Field->GetPossibleValues();
833  }
834  $NewValue = array_search($Value, $PossibleFieldValues);
835  if ($NewValue !== FALSE)
836  {
837  $ReturnValues[] = $NewValue;
838  }
839  }
840  else
841  {
842  $NewValue = $Field->GetIdForValue($Value);
843  if ($NewValue !== NULL)
844  {
845  $ReturnValues[] = $NewValue;
846  }
847  }
848  break;
849 
850  case "Database to SearchGroup":
851  # look up value for index
852  if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
853  {
854  # (for flag fields the value index (0 or 1) is already what is used in Database)
855  if ($Value >= 0)
856  {
857  $ReturnValues[] = "=".$Value;
858  }
859  }
860  elseif ($Field->Type() == MetadataSchema::MDFTYPE_NUMBER)
861  {
862  # (for flag fields the value index (0 or 1) is already what is used in Database)
863  if ($Value >= 0)
864  {
865  $ReturnValues[] = ">=".$Value;
866  }
867  }
868  elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
869  {
870  $User = new SPTUser(intval($Value));
871  if ($User)
872  {
873  $ReturnValues[] = "=".$User->Get("UserName");
874  }
875  }
876  elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
877  {
878  if (!isset($PossibleFieldValues))
879  {
880  $PossibleFieldValues = $Field->GetPossibleValues();
881  }
882 
883  if (isset($PossibleFieldValues[$Value]))
884  {
885  $ReturnValues[] = "=".$PossibleFieldValues[$Value];
886  }
887  }
888  else
889  {
890  $NewValue = $Field->GetValueForId($Value);
891  if ($NewValue !== NULL)
892  {
893  $ReturnValues[] = "=".$NewValue;
894  }
895  }
896  break;
897  }
898  }
899 
900  # return array of translated values to caller
901  return $ReturnValues;
902  }
903 
906  # utility function for updating values in database
907  private function UpdateValue($FieldName, $NewValue)
908  {
909  return $this->DB->UpdateValue("SavedSearches", $FieldName, $NewValue,
910  "SearchId = ".$this->SearchId, $this->Record);
911  }
912 
913  # legacy methods for backward compatibility
914  function GetSearchId() { return $this->Id(); }
915 
917 }