00001 <?PHP
00002 #
00003 # FILE: SavedSearch.php
00004 #
00005 # NOTES:
00006 # - the "$SearchGroups" values used herein contain a multi-dimentional
00007 # array in the form of:
00008 # $Criteria["MAIN"]["SearchStrings"][<field names>] = <value>
00009 # for fields with a single value, and:
00010 # $Criteria[<field ID>]["SearchStrings"][<field name>][] = <value>
00011 # for fields with multiple values
00012 #
00013 # Part of the Collection Workflow Integration System (CWIS)
00014 # Copyright 2011 Edward Almasy and Internet Scout Project
00015 # http://scout.wisc.edu/
00016 #
00017
00018 class SavedSearch {
00019
00020 # ---- PUBLIC INTERFACE --------------------------------------------------
00021
00022 # search frequency mnemonics
00023 const SEARCHFREQ_NEVER = 0;
00024 const SEARCHFREQ_HOURLY = 1;
00025 const SEARCHFREQ_DAILY = 2;
00026 const SEARCHFREQ_WEEKLY = 3;
00027 const SEARCHFREQ_BIWEEKLY = 4;
00028 const SEARCHFREQ_MONTHLY = 5;
00029 const SEARCHFREQ_QUARTERLY = 6;
00030 const SEARCHFREQ_YEARLY = 7;
00031
00032 # object constructor
00033 function SavedSearch($SearchId, $SearchName = NULL, $UserId = NULL,
00034 $Frequency = NULL, $SearchGroups = NULL)
00035 {
00036 # get our own database handle
00037 $this->DB = new Database();
00038
00039 # if search ID was provided
00040 if ($SearchId !== NULL)
00041 {
00042 # save search ID
00043 $this->SearchId = intval($SearchId);
00044
00045 # initialize our local copies of data
00046 $this->DB->Query("SELECT * FROM SavedSearches"
00047 ." WHERE SearchId = '".$this->SearchId."'");
00048 $this->Record = $this->DB->FetchRow();
00049
00050 # update search details where provided
00051 if ($SearchName) { $this->SearchName($SearchName); }
00052 if ($UserId) { $this->UserId($UserId); }
00053 if ($Frequency) { $this->Frequency($Frequency); }
00054 }
00055 else
00056 {
00057 # add new saved search to database
00058 $this->DB->Query("INSERT INTO SavedSearches"
00059 ." (SearchName, UserId, Frequency) VALUES ("
00060 ."'".addslashes($SearchName)."', "
00061 .intval($UserId).", "
00062 .intval($Frequency).")");
00063
00064 # retrieve and save ID of new search locally
00065 $this->SearchId = $this->DB->LastInsertId("SavedSearches");
00066
00067 # save frequency and user ID locally
00068 $this->Record["SearchName"] = $SearchName;
00069 $this->Record["UserId"] = $UserId;
00070 $this->Record["Frequency"] = $Frequency;
00071 }
00072
00073 # save search parameters if provided
00074 if ($SearchGroups) { $this->SearchGroups($SearchGroups); }
00075 }
00076
00077 # get/set search parameters
00078 function SearchGroups($NewSearchGroups = NULL)
00079 {
00080 $Schema = new MetadataSchema();
00081
00082 # if new search parameters were supplied
00083 if ($NewSearchGroups)
00084 {
00085 # remove existing entries for this search from the database
00086 $this->DB->Query("DELETE FROM SavedSearchTextParameters WHERE SearchId = ".$this->SearchId);
00087 $this->DB->Query("DELETE FROM SavedSearchIdParameters WHERE SearchId = ".$this->SearchId);
00088
00089 # for each search group
00090 foreach ($NewSearchGroups as $GroupIndex => $Group)
00091 {
00092 # if group holds single parameters
00093 if ($GroupIndex == "MAIN")
00094 {
00095 # for each field within group
00096 foreach ($Group["SearchStrings"] as $FieldName => $Value)
00097 {
00098 # convert value array to single value (if necessary)
00099 if (is_array($Value))
00100 {
00101 $ConvertedValue = "";
00102 foreach ($Value as $SingleValue)
00103 {
00104 $ConvertedValue .= $SingleValue." ";
00105 }
00106 $Value = trim($ConvertedValue);
00107 }
00108
00109 # add new text search parameter entry to database
00110 if ($FieldName == "XXXKeywordXXX")
00111 {
00112 $FieldId = -101;
00113 }
00114 else
00115 {
00116 $Field = $Schema->GetFieldByName($FieldName);
00117 $FieldId = $Field->Id();
00118 }
00119 $this->DB->Query("INSERT INTO SavedSearchTextParameters"
00120 ." (SearchId, FieldId, SearchText) VALUES"
00121 ." (".$this->SearchId.", ".$FieldId.", '".addslashes($Value)."')");
00122 }
00123 }
00124 else
00125 {
00126 # convert value(s) as appropriate for field type
00127 $FieldId = $GroupIndex;
00128 $Field = $Schema->GetField($FieldId);
00129 $FieldName = $Field->Name();
00130 $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
00131
00132 # for each converted value
00133 foreach ($Values as $Value)
00134 {
00135 # add new ID search parameter entry to database
00136 $this->DB->Query("INSERT INTO SavedSearchIdParameters"
00137 ." (SearchId, FieldId, SearchValueId) VALUES"
00138 ." (".$this->SearchId.", ".$FieldId.", ".$Value.")");
00139 }
00140 }
00141 }
00142
00143 # save search parameters locally
00144 $this->SearchGroups = $NewSearchGroups;
00145 }
00146 else
00147 {
00148 # if search groups not already read in
00149 if (!isset($this->SearchGroups))
00150 {
00151 # for each text search parameter
00152 $SearchGroups = array();
00153 $this->DB->Query("SELECT * FROM SavedSearchTextParameters WHERE SearchId = ".$this->SearchId);
00154 while ($Record = $this->DB->FetchRow())
00155 {
00156 # add parameter to search criteria
00157 if ($Record["FieldId"] == -101)
00158 {
00159 $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] =
00160 $Record["SearchText"];
00161 }
00162 else
00163 {
00164 $Field = $Schema->GetField($Record["FieldId"]);
00165 $SearchGroups["MAIN"]["SearchStrings"][$Field->Name()] =
00166 $Record["SearchText"];
00167 }
00168 }
00169
00170 # for each value ID search parameter
00171 $this->DB->Query("SELECT * FROM SavedSearchIdParameters WHERE SearchId = ".$this->SearchId);
00172 while ($Record = $this->DB->FetchRow())
00173 {
00174 # translate value based on field type
00175 $FieldId = $Record["FieldId"];
00176 if (!isset($Fields[$FieldId])) { $Fields[$FieldId] = new MetadataField($FieldId); }
00177 $Values = SavedSearch::TranslateValues($Fields[$FieldId],
00178 $Record["SearchValueId"], "Database to SearchGroup");
00179
00180 # add parameter to search criteria
00181 foreach ($Values as $Value)
00182 {
00183 $SearchGroups[$FieldId]["SearchStrings"][$Fields[$FieldId]->Name()][] = $Value;
00184 }
00185 }
00186
00187 # set appropriate logic in search parameters
00188 foreach ($SearchGroups as $GroupIndex => $Group)
00189 {
00190 $SearchGroups[$GroupIndex]["Logic"] =
00191 ($GroupIndex == "MAIN") ? SearchEngine::LOGIC_AND
00192 : SearchEngine::LOGIC_OR;
00193 }
00194
00195 # save search parameters locally
00196 $this->SearchGroups = $SearchGroups;
00197 }
00198 }
00199
00200 # return search parameters to caller
00201 return $this->SearchGroups;
00202 }
00203
00209 function SearchName($NewValue = DB_NOVALUE)
00210 { return $this->UpdateValue("SearchName", $NewValue); }
00211
00216 function Id() { return $this->SearchId; }
00217
00223 function UserId($NewValue = DB_NOVALUE)
00224 { return $this->UpdateValue("UserId", $NewValue); }
00225
00231 function Frequency($NewValue = DB_NOVALUE)
00232 { return $this->UpdateValue("Frequency", $NewValue); }
00233
00234 # set date search was last run to current date/time
00235 function UpdateDateLastRun()
00236 {
00237 $this->DB->Query("UPDATE SavedSearches SET DateLastRun = NOW() WHERE SearchId = ".$this->SearchId);
00238 }
00239
00240 # get/set date search was last run
00241 function DateLastRun($NewValue = DB_NOVALUE)
00242 { return $this->UpdateValue("DateLastRun", $NewValue); }
00243
00249 function GetSearchGroupsAsUrlParameters()
00250 {
00251 return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
00252 }
00253
00260 static function TranslateSearchGroupsToUrlParameters($SearchGroups)
00261 {
00262 # assume that no parameters will be found
00263 $UrlPortion = "";
00264
00265 # for each group in parameters
00266 $Schema = new MetadataSchema();
00267 foreach ($SearchGroups as $GroupIndex => $Group)
00268 {
00269 # if group holds single parameters
00270 if ($GroupIndex == "MAIN")
00271 {
00272 # for each field within group
00273 foreach ($Group["SearchStrings"] as $FieldName => $Value)
00274 {
00275 # add segment to URL for this field
00276 if ($FieldName == "XXXKeywordXXX")
00277 {
00278 $FieldId = "K";
00279 }
00280 else
00281 {
00282 $Field = $Schema->GetFieldByName($FieldName);
00283 $FieldId = $Field->Id();
00284 }
00285 if (is_array($Value))
00286 {
00287 $UrlPortion .= "&F".$FieldId."=";
00288 $ValueString = "";
00289 foreach ($Value as $SingleValue)
00290 {
00291 $ValueString .= $SingleValue." ";
00292 }
00293 $UrlPortion .= urlencode(trim($ValueString));
00294 }
00295 else
00296 {
00297 $UrlPortion .= "&F".$FieldId."=".urlencode($Value);
00298 }
00299 }
00300 }
00301 else
00302 {
00303 # convert value based on field type
00304 $FieldId = $GroupIndex;
00305 $Field = $Schema->GetField($FieldId);
00306 $FieldName = $Field->Name();
00307 $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
00308
00309 # add values to URL
00310 $FirstValue = TRUE;
00311 foreach ($Values as $Value)
00312 {
00313 if ($FirstValue)
00314 {
00315 $FirstValue = FALSE;
00316 $UrlPortion .= "&G".$FieldId."=".$Value;
00317 }
00318 else
00319 {
00320 $UrlPortion .= "-".$Value;
00321 }
00322 }
00323 }
00324 }
00325
00326 # trim off any leading "&"
00327 if (strlen($UrlPortion)) { $UrlPortion = substr($UrlPortion, 1); }
00328
00329 # return URL portion to caller
00330 return $UrlPortion;
00331 }
00332
00338 function GetSearchGroupsAsUrlParameterArray()
00339 {
00340 return self::TranslateSearchGroupsToUrlParameters($this->SearchGroups());
00341 }
00342
00349 static function TranslateSearchGroupsToUrlParameterArray($SearchGroups)
00350 {
00351 # assume that no parameters will be found
00352 $UrlPortion = array();
00353
00354 # for each group in parameters
00355 $Schema = new MetadataSchema();
00356 foreach ($SearchGroups as $GroupIndex => $Group)
00357 {
00358 # if group holds single parameters
00359 if ($GroupIndex == "MAIN")
00360 {
00361 # for each field within group
00362 foreach ($Group["SearchStrings"] as $FieldName => $Value)
00363 {
00364 # add segment to URL for this field
00365 if ($FieldName == "XXXKeywordXXX")
00366 {
00367 $FieldId = "K";
00368 }
00369 else
00370 {
00371 $Field = $Schema->GetFieldByName($FieldName);
00372 $FieldId = $Field->Id();
00373 }
00374 if (is_array($Value))
00375 {
00376 $ValueString = "";
00377 foreach ($Value as $SingleValue)
00378 {
00379 $ValueString .= $SingleValue." ";
00380 }
00381
00382 $UrlPortion["F".$FieldId] = urlencode(trim($ValueString));
00383 }
00384 else
00385 {
00386 $UrlPortion["F".$FieldId] = urlencode($Value);
00387 }
00388 }
00389 }
00390 else
00391 {
00392 # convert value based on field type
00393 $FieldId = $GroupIndex;
00394 $Field = $Schema->GetField($FieldId);
00395 $FieldName = $Field->Name();
00396 $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
00397
00398 # add values to URL
00399 $FirstValue = TRUE;
00400 foreach ($Values as $Value)
00401 {
00402 if ($FirstValue)
00403 {
00404 $FirstValue = FALSE;
00405 $UrlPortion["G".$FieldId] = $Value;
00406 }
00407 else
00408 {
00409 $UrlPortion["G".$FieldId] .= "-".$Value;
00410 }
00411 }
00412 }
00413 }
00414
00415 # return URL portion to caller
00416 return $UrlPortion;
00417 }
00418
00419 # set search groups from URL (GET method) parameters
00420 # (returns search group array)
00421 static function TranslateUrlParametersToSearchGroups($GetVars)
00422 {
00423 # if URL segment was passed in instead of GET var array
00424 if (is_string($GetVars))
00425 {
00426 parse_str($GetVars, $GetVars);
00427 }
00428
00429 # start with empty list of parameters
00430 $SearchGroups = array();
00431
00432 $Schema = new MetadataSchema();
00433 $AllFields = $Schema->GetFields(NULL, NULL, TRUE);
00434
00435 foreach ($AllFields as $Field)
00436 {
00437 $FieldId = $Field->Id();
00438 $FieldName = $Field->Name();
00439
00440 # if URL included literal value for this field
00441 if (isset($GetVars["F".$FieldId]))
00442 {
00443 # retrieve value and add to search parameters
00444 $SearchGroups["MAIN"]["SearchStrings"][$FieldName] = $GetVars["F".$FieldId];
00445 }
00446
00447 # if URL included group value for this field
00448 if (isset($GetVars["G".$FieldId]))
00449 {
00450 # retrieve and parse out values
00451 $Values = explode("-", $GetVars["G".$FieldId]);
00452
00453 # translate values
00454 $Values = SavedSearch::TranslateValues($Field, $Values, "Database to SearchGroup");
00455
00456 # add values to searchgroups
00457 $SearchGroups[$FieldId]["SearchStrings"][$FieldName] = $Values;
00458 }
00459 }
00460
00461 # if keyword pseudo-field was included in URL
00462 if (isset($GetVars["FK"]))
00463 {
00464 # retrieve value and add to search parameters
00465 $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] = $GetVars["FK"];
00466 }
00467
00468 # set search logic
00469 foreach ($SearchGroups as $GroupIndex => $Group)
00470 {
00471 $SearchGroups[$GroupIndex]["Logic"] = ($GroupIndex == "MAIN")
00472 ? SearchEngine::LOGIC_AND : SearchEngine::LOGIC_OR;
00473 }
00474
00475 # return parameters to caller
00476 return $SearchGroups;
00477 }
00478
00489 function GetSearchGroupsAsTextDescription(
00490 $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
00491 {
00492 return self::TranslateSearchGroupsToTextDescription($this->SearchGroups(),
00493 $IncludeHtml, $StartWithBreak, $TruncateLongWordsTo);
00494 }
00495
00507 static function TranslateSearchGroupsToTextDescription($SearchGroups,
00508 $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
00509 {
00510 $Schema = new MetadataSchema();
00511
00512 # start with empty description
00513 $Descrip = "";
00514
00515 # set characters used to indicate literal strings
00516 $LiteralStart = $IncludeHtml ? "<i>" : "\"";
00517 $LiteralEnd = $IncludeHtml ? "</i>" : "\"";
00518 $LiteralBreak = $IncludeHtml ? "<br>\n" : "\n";
00519
00520 # if this is a simple keyword search
00521 if (isset($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"])
00522 && (count($SearchGroups) == 1)
00523 && (count($SearchGroups["MAIN"]["SearchStrings"]) == 1))
00524 {
00525 # just use the search string
00526 $Descrip .= $LiteralStart;
00527 $Descrip .= defaulthtmlentities($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"]);
00528 $Descrip .= $LiteralEnd . $LiteralBreak;
00529 }
00530 else
00531 {
00532 # start description on a new line (if requested)
00533 if ($StartWithBreak)
00534 {
00535 $Descrip .= $LiteralBreak;
00536 }
00537
00538 # define list of phrases used to represent logical operators
00539 $WordsForOperators = array(
00540 "=" => "is",
00541 ">" => "is greater than",
00542 "<" => "is less than",
00543 ">=" => "is at least",
00544 "<=" => "is no more than",
00545 "!" => "is not",
00546 );
00547
00548 # for each search group
00549 foreach ($SearchGroups as $GroupIndex => $Group)
00550 {
00551 # if group is main
00552 if ($GroupIndex == "MAIN")
00553 {
00554 # for each field in group
00555 foreach ($Group["SearchStrings"] as $FieldName => $Value)
00556 {
00557 # convert keyword pseudo-field name if necessary
00558 if ($FieldName == "XXXKeywordXXX") { $FieldName = "Keyword"; }
00559
00560 # determine wording based on operator
00561 preg_match("/^[=><!]+/", $Value, $Matches);
00562 if (count($Matches) && isset($WordsForOperators[$Matches[0]]))
00563 {
00564 $Value = preg_replace("/^[=><!]+/", "", $Value);
00565 $Wording = $WordsForOperators[$Matches[0]];
00566 }
00567 else
00568 {
00569 $Wording = "contains";
00570 }
00571
00572 $Field = $Schema->GetFieldByName($FieldName);
00573
00574 # get display name for field
00575 if ($Field && $Field->Status() == MetadataSchema::MDFSTAT_OK)
00576 {
00577 $FieldName = $Field->GetDisplayName();
00578 }
00579
00580 # add criteria for field
00581 $Descrip .= $FieldName." ".$Wording." "
00582 .$LiteralStart.htmlspecialchars($Value)
00583 .$LiteralEnd.$LiteralBreak;
00584 }
00585 }
00586 else
00587 {
00588 # for each field in group
00589 foreach ($Group["SearchStrings"] as $FieldName => $Values)
00590 {
00591 # translate values
00592 $Values = SavedSearch::TranslateValues($FieldName, $Values, "SearchGroup to Display");
00593
00594 # for each value
00595 $FirstValue = TRUE;
00596 foreach ($Values as $Value)
00597 {
00598 # determine wording based on operator
00599 preg_match("/^[=><!]+/", $Value, $Matches);
00600 $Operator = $Matches[0];
00601 $Wording = $WordsForOperators[$Operator];
00602
00603 # strip off operator
00604 $Value = preg_replace("/^[=><!]+/", "", $Value);
00605
00606 # add text to description
00607 if ($FirstValue)
00608 {
00609 $Descrip .= $FieldName." ".$Wording." ".$LiteralStart.htmlspecialchars($Value).$LiteralEnd.$LiteralBreak;
00610 $FirstValue = FALSE;
00611 }
00612 else
00613 {
00614 $Descrip .= ($IncludeHtml ? " " : " ")
00615 ."or ".$Wording." ".$LiteralStart
00616 .htmlspecialchars($Value).$LiteralEnd
00617 .$LiteralBreak;
00618 }
00619 }
00620 }
00621 }
00622 }
00623 }
00624
00625 # if caller requested that long words be truncated
00626 if ($TruncateLongWordsTo > 4)
00627 {
00628 # break description into words
00629 $Words = explode(" ", $Descrip);
00630
00631 # for each word
00632 $NewDescrip = "";
00633 foreach ($Words as $Word)
00634 {
00635 # if word is longer than specified length
00636 if (strlen(strip_tags($Word)) > $TruncateLongWordsTo)
00637 {
00638 # truncate word and add ellipsis
00639 $Word = NeatlyTruncateString($Word, $TruncateLongWordsTo - 3);
00640 }
00641
00642 # add word to new description
00643 $NewDescrip .= " ".$Word;
00644 }
00645
00646 # set description to new description
00647 $Descrip = $NewDescrip;
00648 }
00649
00650 # return description to caller
00651 return $Descrip;
00652 }
00653
00658 function GetSearchFieldNames()
00659 {
00660 return self::TranslateSearchGroupsToSearchFieldNames($this->SearchGroups());
00661 }
00662
00668 static function TranslateSearchGroupsToSearchFieldNames($SearchGroups)
00669 {
00670 # start out assuming no fields are being searched
00671 $FieldNames = array();
00672
00673 # for each search group defined
00674 foreach ($SearchGroups as $GroupIndex => $Group)
00675 {
00676 # for each field in group
00677 foreach ($Group["SearchStrings"] as $FieldName => $Values)
00678 {
00679 # add field name to list of fields being searched
00680 $FieldNames[] = $FieldName;
00681 }
00682 }
00683
00684 # return list of fields being searched to caller
00685 return $FieldNames;
00686 }
00687
00693 static function GetSearchFrequencyList()
00694 {
00695 # define list with descriptions
00696 $FreqDescr = array(
00697 self::SEARCHFREQ_NEVER => "Never",
00698 self::SEARCHFREQ_HOURLY => "Hourly",
00699 self::SEARCHFREQ_DAILY => "Daily",
00700 self::SEARCHFREQ_WEEKLY => "Weekly",
00701 self::SEARCHFREQ_BIWEEKLY => "Bi-Weekly",
00702 self::SEARCHFREQ_MONTHLY => "Monthly",
00703 self::SEARCHFREQ_QUARTERLY => "Quarterly",
00704 self::SEARCHFREQ_YEARLY => "Yearly",
00705 );
00706
00707 # for each argument passed in
00708 $Args = func_get_args();
00709 foreach ($Args as $Arg)
00710 {
00711 # remove value from list
00712 $FreqDescr = array_diff_key($FreqDescr, array($Arg => ""));
00713 }
00714
00715 # return list to caller
00716 return $FreqDescr;
00717 }
00718
00722 function Delete()
00723 {
00724 $this->DB->Query("DELETE FROM SavedSearches"
00725 ." WHERE SearchId = ".intval($this->SearchId));
00726 $this->DB->Query("DELETE FROM SavedSearchTextParameters"
00727 ." WHERE SearchId = ".intval($this->SearchId));
00728 $this->DB->Query("DELETE FROM SavedSearchIdParameters"
00729 ." WHERE SearchId = ".intval($this->SearchId));
00730 }
00731
00732
00733 # ---- PRIVATE INTERFACE -------------------------------------------------
00734
00735 private $SearchId;
00736 private $Record;
00737 private $SearchGroups;
00738
00739 # utility function to convert between value representations
00740 # (method accepts a value or array and always return an array)
00741 # (this is needed because values are represented differently:
00742 # FLAG USER OPTION
00743 # in DB / in URL / in forms 0/1 123 456
00744 # used in SearchGroups 0/1 jdoe cname
00745 # displayed to user On/Off jdoe cname
00746 # where "123" and "456" are option or controlled name IDs)
00747 private static function TranslateValues($FieldOrFieldName, $Values, $TranslationType)
00748 {
00749 # start out assuming we won't find any values to translate
00750 $ReturnValues = array();
00751
00752 # convert field name to field object if necessary
00753 if (is_object($FieldOrFieldName))
00754 {
00755 $Field = $FieldOrFieldName;
00756 }
00757 else
00758 {
00759 static $Schema;
00760 if (!isset($Schema)) { $Schema = new MetadataSchema(); }
00761 $Field = $Schema->GetFieldByName($FieldOrFieldName);
00762 }
00763
00764 # if incoming value is not an array
00765 if (!is_array($Values))
00766 {
00767 # convert incoming value to an array
00768 $Values = array($Values);
00769 }
00770
00771 # for each incoming value
00772 foreach ($Values as $Value)
00773 {
00774 switch ($TranslationType)
00775 {
00776 case "SearchGroup to Display":
00777 # if field is Flag field
00778 if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
00779 {
00780 # translate value to true/false label and add leading operator
00781 $ReturnValues[] = ($Value == "=1") ? "=".$Field->FlagOnLabel() : "=".$Field->FlagOffLabel();
00782 }
00783 elseif ($Field->Name() == "Cumulative Rating")
00784 {
00785 # translate numeric value to stars
00786 $StarStrings = array(
00787 "20" => "*",
00788 "40" => "**",
00789 "60" => "***",
00790 "80" => "****",
00791 "100" => "*****",
00792 );
00793 preg_match("/[0-9]+$/", $Value, $Matches);
00794 $Number = $Matches[0];
00795 preg_match("/^[=><!]+/", $Value, $Matches);
00796 $Operator = $Matches[0];
00797 $ReturnValues[] = $Operator.$StarStrings[$Number];
00798 }
00799 else
00800 {
00801 # use value as is
00802 $ReturnValues[] = $Value;
00803 }
00804 break;
00805
00806 case "SearchGroup to Database":
00807 # strip off leading operator on value
00808 $Value = preg_replace("/^[=><!]+/", "", $Value);
00809
00810 # look up index for value
00811 if ($Field->Type() & (MetadataSchema::MDFTYPE_FLAG|MetadataSchema::MDFTYPE_NUMBER))
00812 {
00813 # (for flag or number fields the value index is already what is used in SearchGroups)
00814 if ($Value >= 0)
00815 {
00816 $ReturnValues[] = $Value;
00817 }
00818 }
00819 elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
00820 {
00821 # (for user fields the value index is the user ID)
00822 $User = new SPTUser(strval($Value));
00823 if ($User)
00824 {
00825 $ReturnValues[] = $User->Id();
00826 }
00827 }
00828 elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
00829 {
00830 if (!isset($PossibleFieldValues))
00831 {
00832 $PossibleFieldValues = $Field->GetPossibleValues();
00833 }
00834 $NewValue = array_search($Value, $PossibleFieldValues);
00835 if ($NewValue !== FALSE)
00836 {
00837 $ReturnValues[] = $NewValue;
00838 }
00839 }
00840 else
00841 {
00842 $NewValue = $Field->GetIdForValue($Value);
00843 if ($NewValue !== NULL)
00844 {
00845 $ReturnValues[] = $NewValue;
00846 }
00847 }
00848 break;
00849
00850 case "Database to SearchGroup":
00851 # look up value for index
00852 if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
00853 {
00854 # (for flag fields the value index (0 or 1) is already what is used in Database)
00855 if ($Value >= 0)
00856 {
00857 $ReturnValues[] = "=".$Value;
00858 }
00859 }
00860 elseif ($Field->Type() == MetadataSchema::MDFTYPE_NUMBER)
00861 {
00862 # (for flag fields the value index (0 or 1) is already what is used in Database)
00863 if ($Value >= 0)
00864 {
00865 $ReturnValues[] = ">=".$Value;
00866 }
00867 }
00868 elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
00869 {
00870 $User = new SPTUser(intval($Value));
00871 if ($User)
00872 {
00873 $ReturnValues[] = "=".$User->Get("UserName");
00874 }
00875 }
00876 elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
00877 {
00878 if (!isset($PossibleFieldValues))
00879 {
00880 $PossibleFieldValues = $Field->GetPossibleValues();
00881 }
00882
00883 if (isset($PossibleFieldValues[$Value]))
00884 {
00885 $ReturnValues[] = "=".$PossibleFieldValues[$Value];
00886 }
00887 }
00888 else
00889 {
00890 $NewValue = $Field->GetValueForId($Value);
00891 if ($NewValue !== NULL)
00892 {
00893 $ReturnValues[] = "=".$NewValue;
00894 }
00895 }
00896 break;
00897 }
00898 }
00899
00900 # return array of translated values to caller
00901 return $ReturnValues;
00902 }
00903
00906 # utility function for updating values in database
00907 private function UpdateValue($FieldName, $NewValue)
00908 {
00909 return $this->DB->UpdateValue("SavedSearches", $FieldName, $NewValue,
00910 "SearchId = ".$this->SearchId, $this->Record);
00911 }
00912
00913 # legacy methods for backward compatibility
00914 function GetSearchId() { return $this->Id(); }
00915
00917 }