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"
154  ." WHERE SearchId = ".$this->SearchId);
155  while ($Record = $this->DB->FetchRow())
156  {
157  # add parameter to search criteria
158  if ($Record["FieldId"] == -101)
159  {
160  $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] =
161  $Record["SearchText"];
162  }
163  else
164  {
165  $Field = $Schema->GetField($Record["FieldId"]);
166  $SearchGroups["MAIN"]["SearchStrings"][$Field->Name()] =
167  $Record["SearchText"];
168  }
169  }
170 
171  # for each value ID search parameter
172  $this->DB->Query("SELECT * FROM SavedSearchIdParameters"
173  ." WHERE SearchId = ".$this->SearchId);
174  while ($Record = $this->DB->FetchRow())
175  {
176  # translate value based on field type
177  $FieldId = $Record["FieldId"];
178  if (!isset($Fields[$FieldId]))
179  {
180  $Fields[$FieldId] = $Schema->GetField($FieldId);
181  }
182  $Values = SavedSearch::TranslateValues($Fields[$FieldId],
183  $Record["SearchValueId"], "Database to SearchGroup");
184 
185  # add parameter to search criteria
186  foreach ($Values as $Value)
187  {
188  $SearchGroups[$FieldId]["SearchStrings"]
189  [$Fields[$FieldId]->Name()][] = $Value;
190  }
191  }
192 
193  # set appropriate logic in search parameters
194  foreach ($SearchGroups as $GroupIndex => $Group)
195  {
196  $SearchGroups[$GroupIndex]["Logic"] =
197  ($GroupIndex == "MAIN") ? SearchEngine::LOGIC_AND
199  }
200 
201  # save search parameters locally
202  $this->SearchGroups = $SearchGroups;
203  }
204  }
205 
206  # return search parameters to caller
207  return $this->SearchGroups;
208  }
209 
215  function SearchName($NewValue = DB_NOVALUE)
216  { return $this->UpdateValue("SearchName", $NewValue); }
217 
222  function Id() { return $this->SearchId; }
223 
229  function UserId($NewValue = DB_NOVALUE)
230  { return $this->UpdateValue("UserId", $NewValue); }
231 
237  function Frequency($NewValue = DB_NOVALUE)
238  { return $this->UpdateValue("Frequency", $NewValue); }
239 
240  # set date search was last run to current date/time
241  function UpdateDateLastRun()
242  {
243  $this->DB->Query("UPDATE SavedSearches SET DateLastRun = NOW() WHERE SearchId = ".$this->SearchId);
244  }
245 
246  # get/set date search was last run
247  function DateLastRun($NewValue = DB_NOVALUE)
248  { return $this->UpdateValue("DateLastRun", $NewValue); }
249 
256  {
257  return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
258  }
259 
266  static function TranslateSearchGroupsToUrlParameters($SearchGroups)
267  {
268  # assume that no parameters will be found
269  $UrlPortion = "";
270 
271  # for each group in parameters
272  $Schema = new MetadataSchema();
273  foreach ($SearchGroups as $GroupIndex => $Group)
274  {
275  # if group holds single parameters
276  if ($GroupIndex == "MAIN")
277  {
278  # for each field within group
279  foreach ($Group["SearchStrings"] as $FieldName => $Value)
280  {
281  # add segment to URL for this field
282  if ($FieldName == "XXXKeywordXXX")
283  {
284  $FieldId = "K";
285  }
286  else
287  {
288  $Field = $Schema->GetFieldByName($FieldName);
289  $FieldId = $Field->Id();
290  }
291  if (is_array($Value))
292  {
293  $UrlPortion .= "&F".$FieldId."=";
294  $ValueString = "";
295  foreach ($Value as $SingleValue)
296  {
297  $ValueString .= $SingleValue." ";
298  }
299  $UrlPortion .= urlencode(trim($ValueString));
300  }
301  else
302  {
303  $UrlPortion .= "&F".$FieldId."=".urlencode($Value);
304  }
305  }
306  }
307  else
308  {
309  # convert value based on field type
310  $FieldId = $GroupIndex;
311  $Field = $Schema->GetField($FieldId);
312  $FieldName = $Field->Name();
313  $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
314 
315  # add values to URL
316  $FirstValue = TRUE;
317  foreach ($Values as $Value)
318  {
319  if ($FirstValue)
320  {
321  $FirstValue = FALSE;
322  $UrlPortion .= "&G".$FieldId."=".$Value;
323  }
324  else
325  {
326  $UrlPortion .= "-".$Value;
327  }
328  }
329  }
330  }
331 
332  # trim off any leading "&"
333  if (strlen($UrlPortion)) { $UrlPortion = substr($UrlPortion, 1); }
334 
335  # return URL portion to caller
336  return $UrlPortion;
337  }
338 
345  {
346  return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
347  }
348 
355  static function TranslateSearchGroupsToUrlParameterArray($SearchGroups)
356  {
357  # assume that no parameters will be found
358  $UrlPortion = array();
359 
360  # for each group in parameters
361  $Schema = new MetadataSchema();
362  foreach ($SearchGroups as $GroupIndex => $Group)
363  {
364  # if group holds single parameters
365  if ($GroupIndex == "MAIN")
366  {
367  # for each field within group
368  foreach ($Group["SearchStrings"] as $FieldName => $Value)
369  {
370  # add segment to URL for this field
371  if ($FieldName == "XXXKeywordXXX")
372  {
373  $FieldId = "K";
374  }
375  else
376  {
377  $Field = $Schema->GetFieldByName($FieldName);
378  $FieldId = $Field->Id();
379  }
380  if (is_array($Value))
381  {
382  $ValueString = "";
383  foreach ($Value as $SingleValue)
384  {
385  $ValueString .= $SingleValue." ";
386  }
387 
388  $UrlPortion["F".$FieldId] = urlencode(trim($ValueString));
389  }
390  else
391  {
392  $UrlPortion["F".$FieldId] = urlencode($Value);
393  }
394  }
395  }
396  else
397  {
398  # convert value based on field type
399  $FieldId = $GroupIndex;
400  $Field = $Schema->GetField($FieldId);
401  $FieldName = $Field->Name();
402  $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
403 
404  # add values to URL
405  $FirstValue = TRUE;
406  foreach ($Values as $Value)
407  {
408  if ($FirstValue)
409  {
410  $FirstValue = FALSE;
411  $UrlPortion["G".$FieldId] = $Value;
412  }
413  else
414  {
415  $UrlPortion["G".$FieldId] .= "-".$Value;
416  }
417  }
418  }
419  }
420 
421  # return URL portion to caller
422  return $UrlPortion;
423  }
424 
425  # set search groups from URL (GET method) parameters
426  # (returns search group array)
427  static function TranslateUrlParametersToSearchGroups($GetVars)
428  {
429  # if URL segment was passed in instead of GET var array
430  if (is_string($GetVars))
431  {
432  parse_str($GetVars, $GetVars);
433  }
434 
435  # start with empty list of parameters
436  $SearchGroups = array();
437 
438  $Schema = new MetadataSchema();
439  $AllFields = $Schema->GetFields(NULL, NULL, TRUE);
440 
441  foreach ($AllFields as $Field)
442  {
443  $FieldId = $Field->Id();
444  $FieldName = $Field->Name();
445 
446  # if URL included literal value for this field
447  if (isset($GetVars["F".$FieldId]))
448  {
449  # retrieve value and add to search parameters
450  $SearchGroups["MAIN"]["SearchStrings"][$FieldName] = $GetVars["F".$FieldId];
451  }
452 
453  # if URL included group value for this field
454  if (isset($GetVars["G".$FieldId]))
455  {
456  # retrieve and parse out values
457  $Values = explode("-", $GetVars["G".$FieldId]);
458 
459  # translate values
460  $Values = SavedSearch::TranslateValues($Field, $Values, "Database to SearchGroup");
461 
462  # add values to searchgroups
463  $SearchGroups[$FieldId]["SearchStrings"][$FieldName] = $Values;
464  }
465  }
466 
467  # if keyword pseudo-field was included in URL
468  if (isset($GetVars["FK"]))
469  {
470  # retrieve value and add to search parameters
471  $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] = $GetVars["FK"];
472  }
473 
474  # set search logic
475  foreach ($SearchGroups as $GroupIndex => $Group)
476  {
477  $SearchGroups[$GroupIndex]["Logic"] = ($GroupIndex == "MAIN")
479  }
480 
481  # return parameters to caller
482  return $SearchGroups;
483  }
484 
496  $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
497  {
498  return self::TranslateSearchGroupsToTextDescription($this->SearchGroups(),
499  $IncludeHtml, $StartWithBreak, $TruncateLongWordsTo);
500  }
501 
513  static function TranslateSearchGroupsToTextDescription($SearchGroups,
514  $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
515  {
516  $Schema = new MetadataSchema();
517 
518  # start with empty description
519  $Descrip = "";
520 
521  # set characters used to indicate literal strings
522  $LiteralStart = $IncludeHtml ? "<i>" : "\"";
523  $LiteralEnd = $IncludeHtml ? "</i>" : "\"";
524  $LiteralBreak = $IncludeHtml ? "<br>\n" : "\n";
525 
526  # if this is a simple keyword search
527  if (isset($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"])
528  && (count($SearchGroups) == 1)
529  && (count($SearchGroups["MAIN"]["SearchStrings"]) == 1))
530  {
531  # just use the search string
532  $Descrip .= $LiteralStart;
533  $Descrip .= defaulthtmlentities($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"]);
534  $Descrip .= $LiteralEnd . $LiteralBreak;
535  }
536  else
537  {
538  # start description on a new line (if requested)
539  if ($StartWithBreak)
540  {
541  $Descrip .= $LiteralBreak;
542  }
543 
544  # define list of phrases used to represent logical operators
545  $WordsForOperators = array(
546  "=" => "is",
547  ">" => "is greater than",
548  "<" => "is less than",
549  ">=" => "is at least",
550  "<=" => "is no more than",
551  "!" => "is not",
552  );
553 
554  # for each search group
555  foreach ($SearchGroups as $GroupIndex => $Group)
556  {
557  # if group is main
558  if ($GroupIndex == "MAIN")
559  {
560  # for each field in group
561  foreach ($Group["SearchStrings"] as $FieldName => $Value)
562  {
563  # convert keyword pseudo-field name if necessary
564  if ($FieldName == "XXXKeywordXXX") { $FieldName = "Keyword"; }
565 
566  # determine wording based on operator
567  preg_match("/^[=><!]+/", $Value, $Matches);
568  if (count($Matches) && isset($WordsForOperators[$Matches[0]]))
569  {
570  $Value = preg_replace("/^[=><!]+/", "", $Value);
571  $Wording = $WordsForOperators[$Matches[0]];
572  }
573  else
574  {
575  $Wording = "contains";
576  }
577 
578  $Field = $Schema->GetFieldByName($FieldName);
579 
580  # get display name for field
581  if ($Field && $Field->Status() == MetadataSchema::MDFSTAT_OK)
582  {
583  $FieldName = $Field->GetDisplayName();
584  }
585 
586  # add criteria for field
587  $Descrip .= $FieldName." ".$Wording." "
588  .$LiteralStart.htmlspecialchars($Value)
589  .$LiteralEnd.$LiteralBreak;
590  }
591  }
592  else
593  {
594  # for each field in group
595  foreach ($Group["SearchStrings"] as $FieldName => $Values)
596  {
597  # translate values
598  $Values = SavedSearch::TranslateValues($FieldName, $Values, "SearchGroup to Display");
599 
600  # for each value
601  $FirstValue = TRUE;
602  foreach ($Values as $Value)
603  {
604  # determine wording based on operator
605  preg_match("/^[=><!]+/", $Value, $Matches);
606  $Operator = $Matches[0];
607  $Wording = $WordsForOperators[$Operator];
608 
609  # strip off operator
610  $Value = preg_replace("/^[=><!]+/", "", $Value);
611 
612  # add text to description
613  if ($FirstValue)
614  {
615  $Descrip .= $FieldName." ".$Wording." ".$LiteralStart.htmlspecialchars($Value).$LiteralEnd.$LiteralBreak;
616  $FirstValue = FALSE;
617  }
618  else
619  {
620  $Descrip .= ($IncludeHtml ? "&nbsp;&nbsp;&nbsp;&nbsp;" : " ")
621  ."or ".$Wording." ".$LiteralStart
622  .htmlspecialchars($Value).$LiteralEnd
623  .$LiteralBreak;
624  }
625  }
626  }
627  }
628  }
629  }
630 
631  # if caller requested that long words be truncated
632  if ($TruncateLongWordsTo > 4)
633  {
634  # break description into words
635  $Words = explode(" ", $Descrip);
636 
637  # for each word
638  $NewDescrip = "";
639  foreach ($Words as $Word)
640  {
641  # if word is longer than specified length
642  if (strlen(strip_tags($Word)) > $TruncateLongWordsTo)
643  {
644  # truncate word and add ellipsis
645  $Word = NeatlyTruncateString($Word, $TruncateLongWordsTo - 3);
646  }
647 
648  # add word to new description
649  $NewDescrip .= " ".$Word;
650  }
651 
652  # set description to new description
653  $Descrip = $NewDescrip;
654  }
655 
656  # return description to caller
657  return $Descrip;
658  }
659 
665  {
666  return self::TranslateSearchGroupsToSearchFieldNames($this->SearchGroups());
667  }
668 
674  static function TranslateSearchGroupsToSearchFieldNames($SearchGroups)
675  {
676  # start out assuming no fields are being searched
677  $FieldNames = array();
678 
679  # for each search group defined
680  foreach ($SearchGroups as $GroupIndex => $Group)
681  {
682  # for each field in group
683  foreach ($Group["SearchStrings"] as $FieldName => $Values)
684  {
685  # add field name to list of fields being searched
686  $FieldNames[] = $FieldName;
687  }
688  }
689 
690  # return list of fields being searched to caller
691  return $FieldNames;
692  }
693 
699  static function GetSearchFrequencyList()
700  {
701  # define list with descriptions
702  $FreqDescr = array(
703  self::SEARCHFREQ_NEVER => "Never",
704  self::SEARCHFREQ_HOURLY => "Hourly",
705  self::SEARCHFREQ_DAILY => "Daily",
706  self::SEARCHFREQ_WEEKLY => "Weekly",
707  self::SEARCHFREQ_BIWEEKLY => "Bi-Weekly",
708  self::SEARCHFREQ_MONTHLY => "Monthly",
709  self::SEARCHFREQ_QUARTERLY => "Quarterly",
710  self::SEARCHFREQ_YEARLY => "Yearly",
711  );
712 
713  # for each argument passed in
714  $Args = func_get_args();
715  foreach ($Args as $Arg)
716  {
717  # remove value from list
718  $FreqDescr = array_diff_key($FreqDescr, array($Arg => ""));
719  }
720 
721  # return list to caller
722  return $FreqDescr;
723  }
724 
728  function Delete()
729  {
730  $this->DB->Query("DELETE FROM SavedSearches"
731  ." WHERE SearchId = ".intval($this->SearchId));
732  $this->DB->Query("DELETE FROM SavedSearchTextParameters"
733  ." WHERE SearchId = ".intval($this->SearchId));
734  $this->DB->Query("DELETE FROM SavedSearchIdParameters"
735  ." WHERE SearchId = ".intval($this->SearchId));
736  }
737 
738 
739  # ---- PRIVATE INTERFACE -------------------------------------------------
740 
741  private $SearchId;
742  private $Record;
743  private $SearchGroups;
744 
745  # utility function to convert between value representations
746  # (method accepts a value or array and always return an array)
747  # (this is needed because values are represented differently:
748  # FLAG USER OPTION
749  # in DB / in URL / in forms 0/1 123 456
750  # used in SearchGroups 0/1 jdoe cname
751  # displayed to user On/Off jdoe cname
752  # where "123" and "456" are option or controlled name IDs)
753  private static function TranslateValues($FieldOrFieldName, $Values, $TranslationType)
754  {
755  # start out assuming we won't find any values to translate
756  $ReturnValues = array();
757 
758  # convert field name to field object if necessary
759  if (is_object($FieldOrFieldName))
760  {
761  $Field = $FieldOrFieldName;
762  }
763  else
764  {
765  static $Schema;
766  if (!isset($Schema)) { $Schema = new MetadataSchema(); }
767  $Field = $Schema->GetFieldByName($FieldOrFieldName);
768  }
769 
770  # if incoming value is not an array
771  if (!is_array($Values))
772  {
773  # convert incoming value to an array
774  $Values = array($Values);
775  }
776 
777  # for each incoming value
778  foreach ($Values as $Value)
779  {
780  switch ($TranslationType)
781  {
782  case "SearchGroup to Display":
783  # if field is Flag field
784  if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
785  {
786  # translate value to true/false label and add leading operator
787  $ReturnValues[] = ($Value == "=1") ? "=".$Field->FlagOnLabel() : "=".$Field->FlagOffLabel();
788  }
789  elseif ($Field->Name() == "Cumulative Rating")
790  {
791  # translate numeric value to stars
792  $StarStrings = array(
793  "20" => "*",
794  "40" => "**",
795  "60" => "***",
796  "80" => "****",
797  "100" => "*****",
798  );
799  preg_match("/[0-9]+$/", $Value, $Matches);
800  $Number = $Matches[0];
801  preg_match("/^[=><!]+/", $Value, $Matches);
802  $Operator = $Matches[0];
803  $ReturnValues[] = $Operator.$StarStrings[$Number];
804  }
805  else
806  {
807  # use value as is
808  $ReturnValues[] = $Value;
809  }
810  break;
811 
812  case "SearchGroup to Database":
813  # strip off leading operator on value
814  $Value = preg_replace("/^[=><!]+/", "", $Value);
815 
816  # look up index for value
818  {
819  # (for flag or number fields the value index is already what is used in SearchGroups)
820  if ($Value >= 0)
821  {
822  $ReturnValues[] = $Value;
823  }
824  }
825  elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
826  {
827  # (for user fields the value index is the user ID)
828  $User = new SPTUser(strval($Value));
829  if ($User)
830  {
831  $ReturnValues[] = $User->Id();
832  }
833  }
834  elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
835  {
836  if (!isset($PossibleFieldValues))
837  {
838  $PossibleFieldValues = $Field->GetPossibleValues();
839  }
840  $NewValue = array_search($Value, $PossibleFieldValues);
841  if ($NewValue !== FALSE)
842  {
843  $ReturnValues[] = $NewValue;
844  }
845  }
846  else
847  {
848  $NewValue = $Field->GetIdForValue($Value);
849  if ($NewValue !== NULL)
850  {
851  $ReturnValues[] = $NewValue;
852  }
853  }
854  break;
855 
856  case "Database to SearchGroup":
857  # look up value for index
858  if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
859  {
860  # (for flag fields the value index (0 or 1) is already what is used in Database)
861  if ($Value >= 0)
862  {
863  $ReturnValues[] = "=".$Value;
864  }
865  }
866  elseif ($Field->Type() == MetadataSchema::MDFTYPE_NUMBER)
867  {
868  # (for flag fields the value index (0 or 1) is already what is used in Database)
869  if ($Value >= 0)
870  {
871  $ReturnValues[] = ">=".$Value;
872  }
873  }
874  elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
875  {
876  $User = new SPTUser(intval($Value));
877  if ($User)
878  {
879  $ReturnValues[] = "=".$User->Get("UserName");
880  }
881  }
882  elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
883  {
884  if (!isset($PossibleFieldValues))
885  {
886  $PossibleFieldValues = $Field->GetPossibleValues();
887  }
888 
889  if (isset($PossibleFieldValues[$Value]))
890  {
891  $ReturnValues[] = "=".$PossibleFieldValues[$Value];
892  }
893  }
894  else
895  {
896  $NewValue = $Field->GetValueForId($Value);
897  if ($NewValue !== NULL)
898  {
899  $ReturnValues[] = "=".$NewValue;
900  }
901  }
902  break;
903  }
904  }
905 
906  # return array of translated values to caller
907  return $ReturnValues;
908  }
909 
912  # utility function for updating values in database
913  private function UpdateValue($FieldName, $NewValue)
914  {
915  return $this->DB->UpdateValue("SavedSearches", $FieldName, $NewValue,
916  "SearchId = ".$this->SearchId, $this->Record);
917  }
918 
919  # legacy methods for backward compatibility
920  function GetSearchId() { return $this->Id(); }
921 
923 }