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 Id()
00230 {
00231 return $this->SearchId;
00232 }
00233 function GetSearchId() { return $this->Id(); }
00234
00235 # get/set user ID
00236 function UserId($NewValue = DB_NOVALUE)
00237 {
00238 return intval($this->DB->UpdateValue("SavedSearches", "UserId", $NewValue,
00239 "SearchId = ".$this->SearchId, $this->Record));
00240 }
00241
00242 # get/set search frequency
00243 function Frequency($NewValue = DB_NOVALUE)
00244 {
00245 return $this->DB->UpdateValue("SavedSearches", "Frequency", $NewValue,
00246 "SearchId = ".$this->SearchId, $this->Record);
00247 }
00248
00249 # set date search was last run to current date/time
00250 function UpdateDateLastRun()
00251 {
00252 $this->DB->Query("UPDATE SavedSearches SET DateLastRun = NOW() WHERE SearchId = ".$this->SearchId);
00253 }
00254
00255 # get/set date search was last run
00256 function DateLastRun($NewValue = DB_NOVALUE)
00257 {
00258 return $this->DB->UpdateValue("SavedSearches", "DateLastRun", $NewValue,
00259 "SearchId = ".$this->SearchId, $this->Record);
00260 }
00261
00262 # get search parameters in the form of URL GET-encoded values
00263 # (e.g. returns something like F2=madison&F4=american+history&G22=17-41)
00264 # (may be called statically by supplying $SearchGroups value)
00265 function TranslateSearchGroupsToUrlParameters($SearchGroups = NULL)
00266 {
00267 # if search groups were not supplied
00268 if ($SearchGroups == NULL)
00269 {
00270 # if we are part of an instance
00271 if (isset($this) && ($this instanceof SavedSearch))
00272 {
00273 # use our search groups
00274 $SearchGroups = $this->SearchGroups();
00275 }
00276 else
00277 {
00278 # return empty string to caller
00279 return "";
00280 }
00281 }
00282
00283 # assume that no parameters will be found
00284 $UrlPortion = "";
00285
00286 # for each group in parameters
00287 $Schema = new MetadataSchema();
00288 foreach ($SearchGroups as $GroupIndex => $Group)
00289 {
00290 # if group holds single parameters
00291 if ($GroupIndex == "MAIN")
00292 {
00293 # for each field within group
00294 foreach ($Group["SearchStrings"] as $FieldName => $Value)
00295 {
00296 # add segment to URL for this field
00297 if ($FieldName == "XXXKeywordXXX")
00298 {
00299 $FieldId = "K";
00300 }
00301 else
00302 {
00303 $Field = $Schema->GetFieldByName($FieldName);
00304 $FieldId = $Field->Id();
00305 }
00306 if (is_array($Value))
00307 {
00308 $UrlPortion .= "&F".$FieldId."=";
00309 $ValueString = "";
00310 foreach ($Value as $SingleValue)
00311 {
00312 $ValueString .= $SingleValue." ";
00313 }
00314 $UrlPortion .= urlencode(trim($ValueString));
00315 }
00316 else
00317 {
00318 $UrlPortion .= "&F".$FieldId."=".urlencode($Value);
00319 }
00320 }
00321 }
00322 else
00323 {
00324 # convert value based on field type
00325 $FieldId = $GroupIndex;
00326 $Field = $Schema->GetField($FieldId);
00327 $FieldName = $Field->Name();
00328 $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
00329
00330 # add values to URL
00331 $FirstValue = TRUE;
00332 foreach ($Values as $Value)
00333 {
00334 if ($FirstValue)
00335 {
00336 $FirstValue = FALSE;
00337 $UrlPortion .= "&G".$FieldId."=".$Value;
00338 }
00339 else
00340 {
00341 $UrlPortion .= "-".$Value;
00342 }
00343 }
00344 }
00345 }
00346
00347 # trim off any leading "&"
00348 if (strlen($UrlPortion)) { $UrlPortion = substr($UrlPortion, 1); }
00349
00350 # return URL portion to caller
00351 return $UrlPortion;
00352 }
00353
00354 # get search parameters in the form of URL parameters
00355 # (may be called statically by supplying $SearchGroups value)
00356 function TranslateSearchGroupsToUrlParameterArray($SearchGroups = NULL)
00357 {
00358 # if search groups were not supplied
00359 if ($SearchGroups == NULL)
00360 {
00361 # if we are part of an instance
00362 if (isset($this) && ($this instanceof SavedSearch))
00363 {
00364 # use our search groups
00365 $SearchGroups = $this->SearchGroups();
00366 }
00367 else
00368 {
00369 # return empty array to caller
00370 return array();
00371 }
00372 }
00373
00374 # assume that no parameters will be found
00375 $UrlPortion = array();
00376
00377 # for each group in parameters
00378 $Schema = new MetadataSchema();
00379 foreach ($SearchGroups as $GroupIndex => $Group)
00380 {
00381 # if group holds single parameters
00382 if ($GroupIndex == "MAIN")
00383 {
00384 # for each field within group
00385 foreach ($Group["SearchStrings"] as $FieldName => $Value)
00386 {
00387 # add segment to URL for this field
00388 if ($FieldName == "XXXKeywordXXX")
00389 {
00390 $FieldId = "K";
00391 }
00392 else
00393 {
00394 $Field = $Schema->GetFieldByName($FieldName);
00395 $FieldId = $Field->Id();
00396 }
00397 if (is_array($Value))
00398 {
00399 $ValueString = "";
00400 foreach ($Value as $SingleValue)
00401 {
00402 $ValueString .= $SingleValue." ";
00403 }
00404
00405 $UrlPortion["F".$FieldId] = urlencode(trim($ValueString));
00406 }
00407 else
00408 {
00409 $UrlPortion["F".$FieldId] = urlencode($Value);
00410 }
00411 }
00412 }
00413 else
00414 {
00415 # convert value based on field type
00416 $FieldId = $GroupIndex;
00417 $Field = $Schema->GetField($FieldId);
00418 $FieldName = $Field->Name();
00419 $Values = SavedSearch::TranslateValues($Field, $Group["SearchStrings"][$FieldName], "SearchGroup to Database");
00420
00421 # add values to URL
00422 $FirstValue = TRUE;
00423 foreach ($Values as $Value)
00424 {
00425 if ($FirstValue)
00426 {
00427 $FirstValue = FALSE;
00428 $UrlPortion["G".$FieldId] = $Value;
00429 }
00430 else
00431 {
00432 $UrlPortion["G".$FieldId] .= "-".$Value;
00433 }
00434 }
00435 }
00436 }
00437
00438 # return URL portion to caller
00439 return $UrlPortion;
00440 }
00441
00442 # set search groups from URL (GET method) parameters
00443 # (returns search group array)
00444 function TranslateUrlParametersToSearchGroups($GetVars)
00445 {
00446 # if URL segment was passed in instead of GET var array
00447 if (is_string($GetVars))
00448 {
00449 # split URL segment into GET var array
00450 $VarAssignments = explode("&", $GetVars);
00451 $GetVars = array();
00452 foreach ($VarAssignments as $VarAss)
00453 {
00454 $VarAssBits = explode("=", $VarAss);
00455 if (isset($VarAssBits[1]))
00456 {
00457 $GetVars[$VarAssBits[0]] = urldecode($VarAssBits[1]);
00458 }
00459 }
00460 }
00461
00462 # start with empty list of parameters
00463 $SearchGroups = array();
00464
00465 # for each possible metadata field ID
00466 $Schema = new MetadataSchema();
00467 $HighestFieldId = $Schema->GetHighestFieldId();
00468 for ($FieldId = 0; $FieldId <= $HighestFieldId; $FieldId++)
00469 {
00470 # if field exists for this ID
00471 $Field = $Schema->GetField($FieldId);
00472 if ($Field)
00473 {
00474 # if URL included literal value for this field
00475 $FieldName = $Field->Name();
00476 if (isset($GetVars["F".$FieldId]))
00477 {
00478 # retrieve value and add to search parameters
00479 $SearchGroups["MAIN"]["SearchStrings"][$FieldName] = $GetVars["F".$FieldId];
00480 }
00481
00482 # if URL included group value for this field
00483 if (isset($GetVars["G".$FieldId]))
00484 {
00485 # retrieve and parse out values
00486 $Values = explode("-", $GetVars["G".$FieldId]);
00487
00488 # translate values
00489 $Values = SavedSearch::TranslateValues($Field, $Values, "Database to SearchGroup");
00490
00491 # add values to searchgroups
00492 $SearchGroups[$FieldId]["SearchStrings"][$FieldName] = $Values;
00493 }
00494 }
00495 }
00496
00497 # if keyword psuedo-field was included in URL
00498 if (isset($GetVars["FK"]))
00499 {
00500 # retrieve value and add to search parameters
00501 $SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"] = $GetVars["FK"];
00502 }
00503
00504 # set search logic
00505 foreach ($SearchGroups as $GroupIndex => $Group)
00506 {
00507 $SearchGroups[$GroupIndex]["Logic"] = ($GroupIndex == "MAIN")
00508 ? SearchEngine::SEARCHLOGIC_AND : SearchEngine::SEARCHLOGIC_OR;
00509 }
00510
00511 # save parameters (if we're an instance)
00512 if (isset($this) && ($this instanceof SavedSearch))
00513 {
00514 $this->SearchGroups($SearchGroups);
00515 }
00516
00517 # return parameters to caller
00518 return $SearchGroups;
00519 }
00520
00521 # return multi-line string describing search criteria
00522 # (may be called statically by supplying $SearchGroups value)
00523 function GetSearchGroupsAsTextDescription($SearchGroups = NULL,
00524 $IncludeHtml = TRUE, $StartWithBreak = TRUE, $TruncateLongWordsTo = 0)
00525 {
00526 # if search groups were not supplied
00527 if ($SearchGroups == NULL)
00528 {
00529 # if we are part of an instance
00530 if (isset($this) && ($this instanceof SavedSearch))
00531 {
00532 # use our search groups
00533 $SearchGroups = $this->SearchGroups();
00534 }
00535 else
00536 {
00537 # return empty string to caller
00538 return "";
00539 }
00540 }
00541
00542 # start with empty description
00543 $Descrip = "";
00544
00545 # set characters used to indicate literal strings
00546 $LiteralStart = $IncludeHtml ? "<i>" : "\"";
00547 $LiteralEnd = $IncludeHtml ? "</i>" : "\"";
00548 $LiteralBreak = $IncludeHtml ? "<br>\n" : "\n";
00549
00550 # if this is a simple keyword search
00551 if (isset($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"])
00552 && (count($SearchGroups) == 1)
00553 && (count($SearchGroups["MAIN"]["SearchStrings"]) == 1))
00554 {
00555 # just use the search string
00556 $Descrip .= $LiteralStart.htmlspecialchars($SearchGroups["MAIN"]["SearchStrings"]["XXXKeywordXXX"]).$LiteralEnd.$LiteralBreak;
00557 }
00558 else
00559 {
00560 # start description on a new line (if requested)
00561 if ($StartWithBreak)
00562 {
00563 $Descrip .= $LiteralBreak;
00564 }
00565
00566 # define list of phrases used to represent logical operators
00567 $WordsForOperators = array(
00568 "=" => "is",
00569 ">" => "is greater than",
00570 "<" => "is less than",
00571 ">=" => "is at least",
00572 "<=" => "is no more than",
00573 "!" => "is not",
00574 );
00575
00576 # for each search group
00577 foreach ($SearchGroups as $GroupIndex => $Group)
00578 {
00579 # if group is main
00580 if ($GroupIndex == "MAIN")
00581 {
00582 # for each field in group
00583 foreach ($Group["SearchStrings"] as $FieldName => $Value)
00584 {
00585 # convert keyword pseudo-field name if necessary
00586 if ($FieldName == "XXXKeywordXXX") { $FieldName = "Keyword"; }
00587
00588 # determine wording based on operator
00589 preg_match("/^[=><!]+/", $Value, $Matches);
00590 if (count($Matches) && isset($WordsForOperators[$Matches[0]]))
00591 {
00592 $Value = preg_replace("/^[=><!]+/", "", $Value);
00593 $Wording = $WordsForOperators[$Matches[0]];
00594 }
00595 else
00596 {
00597 $Wording = "contains";
00598 }
00599
00600 # add criteria for field
00601 $Descrip .= $FieldName." ".$Wording." "
00602 .$LiteralStart.htmlspecialchars($Value)
00603 .$LiteralEnd.$LiteralBreak;
00604 }
00605 }
00606 else
00607 {
00608 # for each field in group
00609 foreach ($Group["SearchStrings"] as $FieldName => $Values)
00610 {
00611 # translate values
00612 $Values = SavedSearch::TranslateValues($FieldName, $Values, "SearchGroup to Display");
00613
00614 # for each value
00615 $FirstValue = TRUE;
00616 foreach ($Values as $Value)
00617 {
00618 # determine wording based on operator
00619 preg_match("/^[=><!]+/", $Value, $Matches);
00620 $Operator = $Matches[0];
00621 $Wording = $WordsForOperators[$Operator];
00622
00623 # strip off operator
00624 $Value = preg_replace("/^[=><!]+/", "", $Value);
00625
00626 # add text to description
00627 if ($FirstValue)
00628 {
00629 $Descrip .= $FieldName." ".$Wording." ".$LiteralStart.htmlspecialchars($Value).$LiteralEnd.$LiteralBreak;
00630 $FirstValue = FALSE;
00631 }
00632 else
00633 {
00634 $Descrip .= ($IncludeHtml ? " " : " ")
00635 ."or ".$Wording." ".$LiteralStart
00636 .htmlspecialchars($Value).$LiteralEnd
00637 .$LiteralBreak;
00638 }
00639 }
00640 }
00641 }
00642 }
00643 }
00644
00645 # if caller requested that long words be truncated
00646 if ($TruncateLongWordsTo > 4)
00647 {
00648 # break description into words
00649 $Words = explode(" ", $Descrip);
00650
00651 # for each word
00652 $NewDescrip = "";
00653 foreach ($Words as $Word)
00654 {
00655 # if word is longer than specified length
00656 if (strlen(strip_tags($Word)) > $TruncateLongWordsTo)
00657 {
00658 # truncate word and add ellipsis
00659 $Word = substr($Word, 0, ($TruncateLongWordsTo - 3))."...";
00660 }
00661
00662 # add word to new description
00663 $NewDescrip .= " ".$Word;
00664 }
00665
00666 # set description to new description
00667 $Descrip = $NewDescrip;
00668 }
00669
00670 # return description to caller
00671 return $Descrip;
00672 }
00673
00674 # get list of fields to be searched (returns array of field names)
00675 # (may be called statically by supplying search groups)
00676 function GetSearchFieldNames($SuppliedSearchGroups = NULL)
00677 {
00678 # make sure search groups are loaded
00679 if ($SuppliedSearchGroups !== NULL)
00680 {
00681 $SearchGroups = $SuppliedSearchGroups;
00682 }
00683 else
00684 {
00685 if (!isset($this->SearchGroups)) { $this->SearchGroups(); }
00686 $SearchGroups = $this->SearchGroups;
00687 }
00688
00689 # start out assuming no fields are being searched
00690 $FieldNames = array();
00691
00692 # for each search group defined
00693 foreach ($SearchGroups as $GroupIndex => $Group)
00694 {
00695 # for each field in group
00696 foreach ($Group["SearchStrings"] as $FieldName => $Values)
00697 {
00698 # add field name to list of fields being searched
00699 $FieldNames[] = $FieldName;
00700 }
00701 }
00702
00703 # return list of fields being searched to caller
00704 return $FieldNames;
00705 }
00706
00707 # return array of possible search frequency descriptions with mnemonics as indices
00708 # (frequencies may be excluded from list by supplying them as arguments)
00709 static function GetSearchFrequencyList()
00710 {
00711 # define list with descriptions
00712 $FreqDescr = array(
00713 self::SEARCHFREQ_NEVER => "Never",
00714 self::SEARCHFREQ_HOURLY => "Hourly",
00715 self::SEARCHFREQ_DAILY => "Daily",
00716 self::SEARCHFREQ_WEEKLY => "Weekly",
00717 self::SEARCHFREQ_BIWEEKLY => "Bi-Weekly",
00718 self::SEARCHFREQ_MONTHLY => "Monthly",
00719 self::SEARCHFREQ_QUARTERLY => "Quarterly",
00720 self::SEARCHFREQ_YEARLY => "Yearly",
00721 );
00722
00723 # for each argument passed in
00724 $Args = func_get_args();
00725 foreach ($Args as $Arg)
00726 {
00727 # remove value from list
00728 $FreqDescr = array_diff_key($FreqDescr, array($Arg => ""));
00729 }
00730
00731 # return list to caller
00732 return $FreqDescr;
00733 }
00734
00735 # remove search from database
00736 # (NOTE: object is no longer usable after this call!)
00737 function Delete()
00738 {
00739 $this->DB->Query("DELETE FROM SavedSearches WHERE SearchId = '".addslashes($this->SearchId)."'");
00740 $this->DB->Query("DELETE FROM SavedSearchTextParameters WHERE SearchId = '".addslashes($this->SearchId)."'");
00741 $this->DB->Query("DELETE FROM SavedSearchIdParameters WHERE SearchId = '".addslashes($this->SearchId)."'");
00742 }
00743
00744
00745 # ---- PRIVATE INTERFACE -------------------------------------------------
00746
00747 var $SearchId;
00748 var $Record;
00749 var $SearchGroups;
00750
00751 # utility function to convert between value representations
00752 # (method accepts a value or array and always return an array)
00753 # (this is needed because values are represented differently:
00754 # FLAG USER OPTION
00755 # in DB / in URL / in forms 0/1 123 456
00756 # used in SearchGroups 0/1 jdoe cname
00757 # displayed to user On/Off jdoe cname
00758 # where "123" and "456" are option or controlled name IDs)
00759 static function TranslateValues($FieldOrFieldName, $Values, $TranslationType)
00760 {
00761 # start out assuming we won't find any values to translate
00762 $ReturnValues = array();
00763
00764 # convert field name to field object if necessary
00765 if (is_object($FieldOrFieldName))
00766 {
00767 $Field = $FieldOrFieldName;
00768 }
00769 else
00770 {
00771 static $Schema;
00772 if (!isset($Schema)) { $Schema = new MetadataSchema(); }
00773 $Field = $Schema->GetFieldByName($FieldOrFieldName);
00774 }
00775
00776 # if incoming value is not an array
00777 if (!is_array($Values))
00778 {
00779 # convert incoming value to an array
00780 $Values = array($Values);
00781 }
00782
00783 # for each incoming value
00784 foreach ($Values as $Value)
00785 {
00786 switch ($TranslationType)
00787 {
00788 case "SearchGroup to Display":
00789 # if field is Flag field
00790 if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
00791 {
00792 # translate value to true/false label and add leading operator
00793 $ReturnValues[] = ($Value == "=1") ? "=".$Field->FlagOnLabel() : "=".$Field->FlagOffLabel();
00794 }
00795 elseif ($Field->Name() == "Cumulative Rating")
00796 {
00797 # translate numeric value to stars
00798 $StarStrings = array(
00799 "20" => "*",
00800 "40" => "**",
00801 "60" => "***",
00802 "80" => "****",
00803 "100" => "*****",
00804 );
00805 preg_match("/[0-9]+$/", $Value, $Matches);
00806 $Number = $Matches[0];
00807 preg_match("/^[=><!]+/", $Value, $Matches);
00808 $Operator = $Matches[0];
00809 $ReturnValues[] = $Operator.$StarStrings[$Number];
00810 }
00811 else
00812 {
00813 # use value as is
00814 $ReturnValues[] = $Value;
00815 }
00816 break;
00817
00818 case "SearchGroup to Database":
00819 # strip off leading operator on value
00820 $Value = preg_replace("/^[=><!]+/", "", $Value);
00821
00822 # look up index for value
00823 if ($Field->Type() & (MetadataSchema::MDFTYPE_FLAG|MetadataSchema::MDFTYPE_NUMBER))
00824 {
00825 # (for flag or number fields the value index is already what is used in SearchGroups)
00826 if ($Value >= 0)
00827 {
00828 $ReturnValues[] = $Value;
00829 }
00830 }
00831 elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
00832 {
00833 # (for user fields the value index is the user ID)
00834 $User = new SPTUser(strval($Value));
00835 if ($User)
00836 {
00837 $ReturnValues[] = $User->Id();
00838 }
00839 }
00840 elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
00841 {
00842 if (!isset($PossibleFieldValues))
00843 {
00844 $PossibleFieldValues = $Field->GetPossibleValues();
00845 }
00846 $NewValue = array_search($Value, $PossibleFieldValues);
00847 if ($NewValue !== FALSE)
00848 {
00849 $ReturnValues[] = $NewValue;
00850 }
00851 }
00852 else
00853 {
00854 $NewValue = $Field->GetIdForValue($Value);
00855 if ($NewValue !== NULL)
00856 {
00857 $ReturnValues[] = $NewValue;
00858 }
00859 }
00860 break;
00861
00862 case "Database to SearchGroup":
00863 # look up value for index
00864 if ($Field->Type() == MetadataSchema::MDFTYPE_FLAG)
00865 {
00866 # (for flag fields the value index (0 or 1) is already what is used in Database)
00867 if ($Value >= 0)
00868 {
00869 $ReturnValues[] = "=".$Value;
00870 }
00871 }
00872 elseif ($Field->Type() == MetadataSchema::MDFTYPE_NUMBER)
00873 {
00874 # (for flag fields the value index (0 or 1) is already what is used in Database)
00875 if ($Value >= 0)
00876 {
00877 $ReturnValues[] = ">=".$Value;
00878 }
00879 }
00880 elseif ($Field->Type() == MetadataSchema::MDFTYPE_USER)
00881 {
00882 $User = new SPTUser(intval($Value));
00883 if ($User)
00884 {
00885 $ReturnValues[] = "=".$User->Get("UserName");
00886 }
00887 }
00888 elseif ($Field->Type() == MetadataSchema::MDFTYPE_OPTION)
00889 {
00890 if (!isset($PossibleFieldValues))
00891 {
00892 $PossibleFieldValues = $Field->GetPossibleValues();
00893 }
00894
00895 if (isset($PossibleFieldValues[$Value]))
00896 {
00897 $ReturnValues[] = "=".$PossibleFieldValues[$Value];
00898 }
00899 }
00900 else
00901 {
00902 $NewValue = $Field->GetValueForId($Value);
00903 if ($NewValue !== NULL)
00904 {
00905 $ReturnValues[] = "=".$NewValue;
00906 }
00907 }
00908 break;
00909 }
00910 }
00911
00912 # return array of translated values to caller
00913 return $ReturnValues;
00914 }
00915 }
00916
00917
00918 ?>