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