CWIS Developer Documentation
ItemFactory.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: ItemFactory.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2007-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
17 abstract class ItemFactory {
18 
19  # ---- PUBLIC INTERFACE --------------------------------------------------
20 
36  function ItemFactory($ItemClassName, $ItemTableName, $ItemIdFieldName,
37  $ItemNameFieldName = NULL, $OrderOpsAllowed = FALSE, $SqlCondition = NULL)
38  {
39  # save item access names
40  $this->ItemClassName = $ItemClassName;
41  $this->ItemTableName = $ItemTableName;
42  $this->ItemIdFieldName = $ItemIdFieldName;
43  $this->ItemNameFieldName = $ItemNameFieldName;
44 
45  # save flag indicating whether item type allows ordering operations
46  $this->OrderOpsAllowed = $OrderOpsAllowed;
47  if ($OrderOpsAllowed)
48  {
49  $this->OrderList = new PersistentDoublyLinkedList(
50  $ItemTableName, $ItemIdFieldName);
51  $this->SetOrderOpsCondition(NULL);
52  }
53 
54  # save database operation conditional
55  $this->SqlCondition = $SqlCondition;
56 
57  # grab our own database handle
58  $this->DB = new Database();
59  }
60 
65  function GetItemClassName()
66  {
67  return $this->ItemClassName;
68  }
69 
75  {
76  # if ID available in session variable
77  $SessionIndex = $this->ItemClassName."EditedIds";
78  if (isset($_SESSION[$SessionIndex]))
79  {
80  # look up value in session variable
81  $ItemId = $_SESSION[$SessionIndex][0];
82  }
83  else
84  {
85  # attempt to look up last temp item ID
86  $ItemId = $this->GetLastTempItemId();
87 
88  # store it in session variable
89  $_SESSION[$SessionIndex] = array($ItemId);
90  }
91 
92  # return ID (if any) to caller
93  return $ItemId;
94  }
95 
100  function SetCurrentEditedItemId($NewId)
101  {
102  # if edited ID array already stored for session
103  $SessionIndex = $this->ItemClassName."EditedIds";
104  if (isset($_SESSION[$SessionIndex]))
105  {
106  # prepend new value to array
107  array_unshift($_SESSION[$SessionIndex], $NewId);
108  }
109  else
110  {
111  # start with fresh array
112  $_SESSION[$SessionIndex] = array($NewId);
113  }
114  }
115 
120  {
121  # if edited item IDs available in a session variable
122  $SessionIndex = $this->ItemClassName."EditedIds";
123  if (isset($_SESSION[$SessionIndex]))
124  {
125  # remove current item from edited item ID array
126  array_shift($_SESSION[$SessionIndex]);
127 
128  # if no further edited items
129  if (count($_SESSION[$SessionIndex]) < 1)
130  {
131  # destroy session variable
132  unset($_SESSION[$SessionIndex]);
133  }
134  }
135  }
136 
144  {
145  # if current edited item is temp item
146  $CurrentEditedItemId = $this->GetCurrentEditedItemId();
147  if ($CurrentEditedItemId < 0)
148  {
149  # delete temp item from DB
150  if (method_exists($this->ItemClassName, "Delete"))
151  {
152  $Item = new $this->ItemClassName($CurrentEditedItemId);
153  $Item->Delete();
154  }
155  else
156  {
157  $this->DB->Query("DELETE FROM ".$this->ItemTableName
158  ." WHERE ".$this->ItemIdFieldName." = ".$CurrentEditedItemId);
159  }
160  }
161 
162  # clear current edited item ID
163  $this->ClearCurrentEditedItemId();
164  }
165 
173  function CleanOutStaleTempItems($MinutesUntilStale = 10080)
174  {
175  # load array of stale items
176  $MinutesUntilStale = max($MinutesUntilStale, 1);
177  $this->DB->Query("SELECT ".$this->ItemIdFieldName." FROM ".$this->ItemTableName
178  ." WHERE ".$this->ItemIdFieldName." < 0"
179  ." AND DateLastModified < DATE_SUB(NOW(), "
180  ." INTERVAL ".intval($MinutesUntilStale)." MINUTE)"
181  .($this->SqlCondition ? " AND ".$this->SqlCondition : ""));
182  $ItemIds = $this->DB->FetchColumn($this->ItemIdFieldName);
183 
184  # delete stale items
185  foreach ($ItemIds as $ItemId)
186  {
187  $Item = new $this->ItemClassName($ItemId);
188  $Item->Delete();
189  }
190 
191  # report number of items deleted to caller
192  return count($ItemIds);
193  }
194 
200  function GetLastTempItemId()
201  {
202  # retrieve ID of most recently modified temp item for this user
203  $ItemId = $this->DB->Query("SELECT ".$this->ItemIdFieldName
204  ." FROM ".$this->ItemTableName
205  ." WHERE LastModifiedById = '"
206  .$GLOBALS["User"]->Get("UserId")."'"
207  ." AND ".$this->ItemIdFieldName." < 0"
208  .($this->SqlCondition ? " AND ".$this->SqlCondition : "")
209  ." ORDER BY ".$this->ItemIdFieldName." ASC"
210  ." LIMIT 1",
211  $this->ItemIdFieldName);
212 
213  # return item to caller (or NULL if none found)
214  return $ItemId;
215  }
216 
222  function GetNextItemId()
223  {
224  # if no highest item ID found
225  $HighestItemId = $this->GetHighestItemId(TRUE);
226  if ($HighestItemId <= 0)
227  {
228  # start with item ID 1
229  $ItemId = 1;
230  }
231  else
232  {
233  # else use next ID available after highest
234  $ItemId = $HighestItemId + 1;
235  }
236 
237  # return next ID to caller
238  return $ItemId;
239  }
240 
248  function GetHighestItemId($IgnoreSqlCondition = FALSE)
249  {
250  # use class-wide condition if set
251  $ConditionString = ($this->SqlCondition && !$IgnoreSqlCondition)
252  ? " WHERE ".$this->SqlCondition : "";
253 
254  # return highest item ID to caller
255  return $this->DB->Query("SELECT ".$this->ItemIdFieldName
256  ." FROM ".$this->ItemTableName
257  .$ConditionString
258  ." ORDER BY ".$this->ItemIdFieldName
259  ." DESC LIMIT 1",
260  $this->ItemIdFieldName);
261  }
262 
267  function GetNextTempItemId()
268  {
269  $LowestItemId = $this->DB->Query("SELECT ".$this->ItemIdFieldName
270  ." FROM ".$this->ItemTableName
271  ." ORDER BY ".$this->ItemIdFieldName
272  ." ASC LIMIT 1",
273  $this->ItemIdFieldName);
274  if ($LowestItemId > 0)
275  {
276  $ItemId = -1;
277  }
278  else
279  {
280  $ItemId = $LowestItemId - 1;
281  }
282  return $ItemId;
283  }
284 
293  function GetItemCount($Condition = NULL, $IncludeTempItems = FALSE)
294  {
295  # use condition if supplied
296  $ConditionString = ($Condition != NULL) ? " WHERE ".$Condition : "";
297 
298  # if temp items are to be excluded
299  if (!$IncludeTempItems)
300  {
301  # if a condition was previously set
302  if (strlen($ConditionString))
303  {
304  # add in condition to exclude temp items
305  $ConditionString .= " AND (".$this->ItemIdFieldName." >= 0)";
306  }
307  else
308  {
309  # use condition to exclude temp items
310  $ConditionString = " WHERE ".$this->ItemIdFieldName." >= 0";
311  }
312  }
313 
314  # add class-wide condition if set
315  if ($this->SqlCondition)
316  {
317  if (strlen($ConditionString))
318  {
319  $ConditionString .= " AND ".$this->SqlCondition;
320  }
321  else
322  {
323  $ConditionString = " WHERE ".$this->SqlCondition;
324  }
325  }
326 
327  # retrieve item count
328  $Count = $this->DB->Query("SELECT COUNT(*) AS RecordCount"
329  ." FROM ".$this->ItemTableName
330  .$ConditionString,
331  "RecordCount");
332 
333  # return count to caller
334  return $Count;
335  }
336 
350  function GetItemIds($Condition = NULL, $IncludeTempItems = FALSE,
351  $SortField = NULL, $SortAscending = TRUE)
352  {
353  # if temp items are supposed to be included
354  if ($IncludeTempItems)
355  {
356  # condition is only as supplied
357  $ConditionString = ($Condition == NULL) ? "" : " WHERE ".$Condition;
358  }
359  else
360  {
361  # condition is non-negative IDs plus supplied condition
362  $ConditionString = " WHERE ".$this->ItemIdFieldName." >= 0"
363  .(($Condition == NULL) ? "" : " AND ".$Condition);
364  }
365 
366  # add class-wide condition if set
367  if ($this->SqlCondition)
368  {
369  if (strlen($ConditionString))
370  {
371  $ConditionString .= " AND ".$this->SqlCondition;
372  }
373  else
374  {
375  $ConditionString = " WHERE ".$this->SqlCondition;
376  }
377  }
378 
379  # add sorting if specified
380  if ($SortField !== NULL)
381  {
382  $ConditionString .= " ORDER BY `".addslashes($SortField)."` "
383  .($SortAscending ? "ASC" : "DESC");
384  }
385 
386  # get item IDs
387  $this->DB->Query("SELECT ".$this->ItemIdFieldName
388  ." FROM ".$this->ItemTableName
389  .$ConditionString);
390  $ItemIds = $this->DB->FetchColumn($this->ItemIdFieldName);
391 
392  # return IDs to caller
393  return $ItemIds;
394  }
395 
403  function GetLatestModificationDate($Condition = NULL)
404  {
405  # set up SQL condition if supplied
406  $ConditionString = ($Condition == NULL) ? "" : " WHERE ".$Condition;
407 
408  # add class-wide condition if set
409  if ($this->SqlCondition)
410  {
411  if (strlen($ConditionString))
412  {
413  $ConditionString .= " AND ".$this->SqlCondition;
414  }
415  else
416  {
417  $ConditionString = " WHERE ".$this->SqlCondition;
418  }
419  }
420 
421  # return modification date for item most recently changed
422  return $this->DB->Query("SELECT MAX(DateLastModified) AS LastChangeDate"
423  ." FROM ".$this->ItemTableName.$ConditionString,
424  "LastChangeDate");
425  }
426 
433  function GetItem($ItemId)
434  {
435  return new $this->ItemClassName($ItemId);
436  }
437 
445  function ItemExists($ItemId, $IgnoreSqlCondition = FALSE)
446  {
447  $Condition = $IgnoreSqlCondition ? ""
448  : ($this->SqlCondition ? " AND ".$this->SqlCondition : "");
449  $ItemCount = $this->DB->Query("SELECT COUNT(*) AS ItemCount"
450  ." FROM ".$this->ItemTableName
451  ." WHERE ".$this->ItemIdFieldName." = ".intval($ItemId)
452  .$Condition, "ItemCount");
453  return ($ItemCount > 0) ? TRUE : FALSE;
454  }
455 
463  function GetItemByName($Name, $IgnoreCase = FALSE)
464  {
465  # get item ID
466  $ItemId = $this->GetItemIdByName($Name, $IgnoreCase);
467 
468  # if item not found
469  if ($ItemId === NULL)
470  {
471  # report error to caller
472  return NULL;
473  }
474  else
475  {
476  # load object and return to caller
477  return $this->GetItem($ItemId);
478  }
479  }
480 
488  function GetItemIdByName($Name, $IgnoreCase = FALSE)
489  {
490  # error out if this is an illegal operation for this item type
491  if ($this->ItemNameFieldName == NULL)
492  {
493  throw new Exception("Attempt to get item ID by name on item type"
494  ."(".$this->ItemClassName.") that has no name field specified.");
495  }
496 
497  # query database for item ID
498  $Comparison = $IgnoreCase
499  ? "LOWER(".$this->ItemNameFieldName.") = '"
500  .addslashes(strtolower($Name))."'"
501  : $this->ItemNameFieldName." = '" .addslashes($Name)."'";
502  $ItemId = $this->DB->Query("SELECT ".$this->ItemIdFieldName
503  ." FROM ".$this->ItemTableName
504  ." WHERE ".$Comparison
505  .($this->SqlCondition
506  ? " AND ".$this->SqlCondition
507  : ""),
508  $this->ItemIdFieldName);
509 
510  # return ID or error indicator to caller
511  return ($ItemId === FALSE) ? NULL : $ItemId;
512  }
513 
520  function GetItemNames($SqlCondition = NULL)
521  {
522  # error out if this is an illegal operation for this item type
523  if ($this->ItemNameFieldName == NULL)
524  {
525  throw new Exception("Attempt to get array of item names"
526  ." on item type (".$this->ItemClassName.") that has no"
527  ." name field specified.");
528  }
529 
530  # query database for item names
531  $Condition = "";
532  if ($SqlCondition)
533  {
534  $Condition = "WHERE ".$SqlCondition;
535  }
536  if ($this->SqlCondition)
537  {
538  if (strlen($Condition))
539  {
540  $Condition .= " AND ".$this->SqlCondition;
541  }
542  else
543  {
544  $Condition = " WHERE ".$this->SqlCondition;
545  }
546  }
547  $this->DB->Query("SELECT ".$this->ItemIdFieldName
548  .", ".$this->ItemNameFieldName
549  ." FROM ".$this->ItemTableName." "
550  .$Condition
551  ." ORDER BY ".$this->ItemNameFieldName);
552  $Names = $this->DB->FetchColumn(
553  $this->ItemNameFieldName, $this->ItemIdFieldName);
554 
555  # return item names to caller
556  return $Names;
557  }
558 
565  function GetItems($SqlCondition = NULL)
566  {
567  $Items = array();
568  $Ids = $this->GetItemIds($SqlCondition);
569  foreach ($Ids as $Id)
570  {
571  $Items[$Id] = $this->GetItem($Id);
572  }
573  return $Items;
574  }
575 
591  function GetItemsAsOptionList($OptionListName, $SelectedItemId = NULL,
592  $SqlCondition = NULL, $DisplaySize = 1, $SubmitOnChange = FALSE)
593  {
594  # retrieve requested fields
595  $ItemNames = $this->GetItemNames($SqlCondition);
596 
597  # if multiple selections are allowed
598  if ($DisplaySize > 1)
599  {
600  # begin multi-selection HTML option list
601  $Html = "<select name=\"".htmlspecialchars($OptionListName)."[]\""
602  .($SubmitOnChange ? " onChange=\"submit()\"" : "")
603  ." multiple=\"multiple\" size=\"".$DisplaySize."\">\n";
604  }
605  else
606  {
607  # begin single-selection HTML option list
608  $Html = "<select name=\"".htmlspecialchars($OptionListName)."\""
609  .($SubmitOnChange ? " onChange=\"submit()\"" : "")
610  ." size=\"1\">\n";
611  $Html .= "<option value=\"-1\">--</option>\n";
612  }
613 
614  # for each metadata field
615  foreach ($ItemNames as $Id => $Name)
616  {
617  # add entry for field to option list
618  $Html .= "<option value=\"".$Id."\"";
619  if (($Id == $SelectedItemId)
620  || (is_array($SelectedItemId) && in_array($Id, $SelectedItemId)))
621  {
622  $Html .= " selected";
623  }
624  $Html .= ">".htmlspecialchars($Name)."</option>\n";
625  }
626 
627  # end HTML option list
628  $Html .= "</select>\n";
629 
630  # return constructed HTML to caller
631  return $Html;
632  }
633 
640  function NameIsInUse($Name, $IgnoreCase = FALSE)
641  {
642  $Condition = $IgnoreCase
643  ? "LOWER(".$this->ItemNameFieldName.")"
644  ." = '".addslashes(strtolower($Name))."'"
645  : $this->ItemNameFieldName." = '".addslashes($Name)."'";
646  if ($this->SqlCondition)
647  {
648  $Condition .= " AND ".$this->SqlCondition;
649  }
650  $NameCount = $this->DB->Query("SELECT COUNT(*) AS RecordCount FROM "
651  .$this->ItemTableName." WHERE ".$Condition, "RecordCount");
652  return ($NameCount > 0) ? TRUE : FALSE;
653  }
654 
671  function SearchForItemNames($SearchString, $NumberOfResults = 100,
672  $IncludeVariants = FALSE, $UseBooleanMode = TRUE, $Offset=0,
673  $IdExclusions = array(), $ValueExclusions=array())
674  {
675  # error out if this is an illegal operation for this item type
676  if ($this->ItemNameFieldName == NULL)
677  {
678  throw new Exception("Attempt to search for item names on item type"
679  ."(".$this->ItemClassName.") that has no name field specified.");
680  }
681 
682  # return no results if empty search string passed in
683  if (!strlen(trim($SearchString))) { return array(); }
684 
685  # construct SQL query
686  $DB = new Database();
687  $QueryString = "SELECT ".$this->ItemIdFieldName.",".$this->ItemNameFieldName
688  ." FROM ".$this->ItemTableName." WHERE "
689  .$this->ConstructSqlConditionsForSearch(
690  $SearchString, $IncludeVariants, $UseBooleanMode, $IdExclusions,
691  $ValueExclusions) ;
692 
693  # limit response set
694  $QueryString .= " LIMIT ".intval($NumberOfResults)." OFFSET "
695  .intval($Offset);
696 
697  # perform query and retrieve names and IDs of items found by query
698  $DB->Query($QueryString);
699  $Names = $DB->FetchColumn($this->ItemNameFieldName, $this->ItemIdFieldName);
700 
701  $Words = preg_split("/[\s]+/", trim($SearchString));
702  foreach ($Words as $Word)
703  {
704  $TgtWord = preg_replace("/[^a-zA-Z]/", "", $Word);
705  if ($Word{0} == "-" && strlen($TgtWord) < $MinWordLen)
706  {
707  $NewNames = array();
708  foreach ($Names as $Id => $Name)
709  {
710  if (! preg_match('/\b'.$TgtWord.'/i', $Name))
711  {
712  $NewNames[$Id] = $Name;
713  }
714  }
715  $Names = $NewNames;
716  }
717  }
718 
719  # return names to caller
720  return $Names;
721  }
722 
735  function GetCountForItemNames($SearchString, $IncludeVariants = FALSE,
736  $UseBooleanMode = TRUE, $IdExclusions = array(), $ValueExclusions=array())
737  {
738  # return no results if empty search string passed in
739  if (!strlen(trim($SearchString))) { return 0; }
740 
741  # construct SQL query
742  $DB = new Database();
743  $QueryString = "SELECT COUNT(*) as ItemCount FROM "
744  .$this->ItemTableName." WHERE "
745  .$this->ConstructSqlConditionsForSearch(
746  $SearchString, $IncludeVariants, $UseBooleanMode, $IdExclusions,
747  $ValueExclusions);
748 
749  # perform query and retrieve names and IDs of items found by query
750  $DB->Query($QueryString);
751  return intval($DB->FetchField("ItemCount"));
752  }
753 
762  function AddItem($ItemName, $AdditionalValues = NULL)
763  {
764  # build initial database query for adding item
765  $Query = "INSERT INTO ".$this->ItemTableName." SET `"
766  .$this->ItemNameFieldName."` = '".addslashes($ItemName)."'";
767 
768  # add any additional values to query
769  if ($AdditionalValues)
770  {
771  foreach ($AdditionalValues as $FieldName => $Value)
772  {
773  $Query .= ", `".$FieldName."` = '".addslashes($Value)."'";
774  }
775  }
776 
777  # add item to database
778  $this->DB->Query($Query);
779 
780  # retrieve ID of new item
781  $Id = $this->DB->LastInsertId();
782 
783  # return ID to caller
784  return $Id;
785  }
786 
791  function DeleteItem($ItemId)
792  {
793  # delete item from database
794  $this->DB->Query("DELETE FROM ".$this->ItemTableName
795  ." WHERE ".$this->ItemIdFieldName." = '".addslashes($ItemId)."'");
796  }
797 
798 
799  # ---- order operations --------------------------------------------------
800 
807  function SetOrderOpsCondition($Condition)
808  {
809  # condition is non-negative IDs (non-temp items) plus supplied condition
810  $NewCondition = $this->ItemIdFieldName." >= 0"
811  .($Condition ? " AND ".$Condition : "")
812  .($this->SqlCondition ? " AND ".$this->SqlCondition : "");
813  $this->OrderList->SqlCondition($NewCondition);
814  }
815 
823  function InsertBefore($TargetItem, $NewItem)
824  {
825  # error out if ordering operations are not allowed for this item type
826  if (!$this->OrderOpsAllowed)
827  {
828  throw new Exception("Attempt to perform order operation on item"
829  ." type (".$this->ItemClassName.") that does not support"
830  ." ordering.");
831  }
832 
833  # insert/move item
834  $this->OrderList->InsertBefore($TargetItem, $NewItem);
835  }
836 
844  function InsertAfter($TargetItem, $NewItem)
845  {
846  # error out if ordering operations are not allowed for this item type
847  if (!$this->OrderOpsAllowed)
848  {
849  throw new Exception("Attempt to perform order operation on item"
850  ." type (".$this->ItemClassName.") that does not support"
851  ." ordering.");
852  }
853 
854  # insert/move item
855  $this->OrderList->InsertAfter($TargetItem, $NewItem);
856  }
857 
863  function Prepend($Item)
864  {
865  # error out if ordering operations are not allowed for this item type
866  if (!$this->OrderOpsAllowed)
867  {
868  throw new Exception("Attempt to perform order operation on item"
869  ." type (".$this->ItemClassName.") that does not support"
870  ." ordering.");
871  }
872 
873  # prepend item
874  $this->OrderList->Prepend($Item);
875  }
876 
882  function Append($Item)
883  {
884  # error out if ordering operations are not allowed for this item type
885  if (!$this->OrderOpsAllowed)
886  {
887  throw new Exception("Attempt to perform order operation on item"
888  ." type (".$this->ItemClassName.") that does not support"
889  ." ordering.");
890  }
891 
892  # add/move item
893  $this->OrderList->Append($Item);
894  }
895 
900  function GetItemIdsInOrder()
901  {
902  # error out if ordering operations are not allowed for this item type
903  if (!$this->OrderOpsAllowed)
904  {
905  throw new Exception("Attempt to perform order operation on item"
906  ." type (".$this->ItemClassName.") that does not support"
907  ." ordering.");
908  }
909 
910  # retrieve list of IDs
911  return $this->OrderList->GetIds();
912  }
913 
920  function RemoveItemFromOrder($ItemId)
921  {
922  # error out if ordering operations are not allowed for this item type
923  if (!$this->OrderOpsAllowed)
924  {
925  throw new Exception("Attempt to perform order operation on item"
926  ." type (".$this->ItemClassName.") that does not support"
927  ." ordering.");
928  }
929 
930  # remove item
931  $this->OrderList->Remove($ItemId);
932  }
933 
934 
935  # ---- PRIVATE INTERFACE -------------------------------------------------
936 
951  private function ConstructSqlConditionsForSearch(
952  $SearchString, $IncludeVariants = FALSE,
953  $UseBooleanMode = TRUE, $IdExclusions = array(), $ValueExclusions=array() )
954  {
955  $QueryString = "";
956 
957  # If the search string is valid but shorter than the minimum word length
958  # indexed by the FTS, just do a normal equality test instead of using
959  # the index. Otherwise, FTS away.
960  $DB = new Database();
961  $MinWordLen = $DB->Query(
962  "SHOW VARIABLES WHERE variable_name='ft_min_word_len'", "Value");
963  if (strlen($SearchString) < $MinWordLen)
964  {
965  $QueryString .= " ".$this->ItemNameFieldName."='".addslashes($SearchString)."'";
966  }
967  else if ($UseBooleanMode)
968  {
969  # When we're in boolean mode, construct a search string to use in our
970  # query. Include quoted strings verbatim. Make sure that each
971  # non-quoted word is prefixed with either + or -, so that it is
972  # either explicitly included or explicitily excluded.
973  # Keep track of stopwords in the search query (these will not
974  # match in the boolean search because FTS indexes ignores them).
975  # Append 'REGEXP' queries to match, so that our search results
976  # pay *some* attention to stopwords.
977  $SearchString = preg_replace("/[)\(><]+/", "", $SearchString);
978  $Words = preg_split("/[\s]+/", trim($SearchString));
979  $NewSearchString = "";
980  $SearchedStopwords = array();
981  $InQuotedString = FALSE;
982  $SqlVarObj = new MysqlSystemVariables($DB);
983  $StopWordList = $SqlVarObj->GetStopWords();
984  $MinWordLen = $SqlVarObj->Get("ft_min_word_len");
985  foreach ($Words as $Word)
986  {
987  # remove any query-specific terms, punctuation, etc.
988  $JustTheWord = preg_replace("/[^a-zA-Z-]/", "", $Word);
989 
990  # require (boolean AND) certain words
991  if ($InQuotedString == FALSE
992  && !in_array($JustTheWord, $StopWordList)
993  && strlen($JustTheWord) >= $MinWordLen
994  && $Word{0} != "+"
995  && $Word{0} != "-")
996  {
997  $NewSearchString .= "+";
998  }
999 
1000  if (preg_match("/^\"/", $Word)) { $InQuotedString = TRUE; }
1001  if (preg_match("/\"$/", $Word)) { $InQuotedString = FALSE; }
1002  $NewSearchString .= $Word." ";
1003 
1004  if (in_array($JustTheWord, $StopWordList))
1005  $SearchedStopwords []= $JustTheWord;
1006  }
1007 
1008  # Build onto our query string by appending the boolean search
1009  # conditions
1010  $QueryString .= " MATCH (".$this->ItemNameFieldName.")"
1011  ." AGAINST ('".addslashes(trim($NewSearchString))."'"
1012  ." IN BOOLEAN MODE)";
1013 
1014  # If there were any stopwords included in the search string,
1015  # append REGEXP conditions to match those.
1016  foreach ($SearchedStopwords as $Stopword)
1017  {
1018  $QueryString .= " AND ".$this->ItemNameFieldName
1019  ." REGEXP '".addslashes(preg_quote($Stopword))."'";
1020  }
1021  }
1022  else
1023  {
1024  # If we weren't in boolean mode, just include the search
1025  # string verbatim as a match condition:
1026  $QueryString .= " MATCH (".$this->ItemNameFieldName.")"
1027  ." AGAINST ('".addslashes(trim($SearchString))."')";
1028  }
1029 
1030  # add each ID exclusion
1031  foreach ($IdExclusions as $IdExclusion)
1032  {
1033  $QueryString .= " AND ".$this->ItemIdFieldName." != '"
1034  .addslashes($IdExclusion)."' ";
1035  }
1036 
1037  # add each value exclusion
1038  foreach ($ValueExclusions as $ValueExclusion)
1039  {
1040  $QueryString .= " AND ".$this->ItemNameFieldName." != '"
1041  .addslashes($ValueExclusion)."' ";
1042  }
1043 
1044  # add class-wide condition if set
1045  if ($this->SqlCondition)
1046  {
1047  $QueryString .= " AND ".$this->SqlCondition;
1048  }
1049 
1050  return $QueryString;
1051  }
1052 
1053  protected $DB;
1054 
1055  private $ItemClassName;
1056  private $ItemTableName;
1057  private $ItemIdFieldName;
1058  private $ItemNameFieldName;
1059  private $OrderOpsAllowed;
1060  private $OrderList;
1061  private $SqlCondition;
1062 }
GetHighestItemId($IgnoreSqlCondition=FALSE)
Retrieve highest item ID in use.
GetLastTempItemId()
Retrieve most recent temp item ID for currently-logged-in user.
GetItemIdsInOrder()
Retrieve list of item IDs in order.
RemoveItemFromOrder($ItemId)
Remove item from existing order.
Prepend($Item)
Add item to beginning of order.
GetItemClassName()
Get class name of items manipulated by factory.
Definition: ItemFactory.php:65
ClearCurrentEditedItemId()
Clear currently edited item ID.
GetItemIdByName($Name, $IgnoreCase=FALSE)
Retrieve item ID by name.
SetCurrentEditedItemId($NewId)
Set ID of currently edited item.
SQL database abstraction object with smart query caching.
DeleteItem($ItemId)
Delete item.
GetItemsAsOptionList($OptionListName, $SelectedItemId=NULL, $SqlCondition=NULL, $DisplaySize=1, $SubmitOnChange=FALSE)
Retrieve items of specified type as HTML option list with item names as labels and item IDs as value ...
GetItemNames($SqlCondition=NULL)
Retrieve item names.
GetItemCount($Condition=NULL, $IncludeTempItems=FALSE)
Get count of items.
AddItem($ItemName, $AdditionalValues=NULL)
Add new item.
SearchForItemNames($SearchString, $NumberOfResults=100, $IncludeVariants=FALSE, $UseBooleanMode=TRUE, $Offset=0, $IdExclusions=array(), $ValueExclusions=array())
Retrieve items with names matching search string.
GetCurrentEditedItemId()
Get ID of currently edited item.
Definition: ItemFactory.php:74
GetItem($ItemId)
Retrieve item by item ID.
GetItemByName($Name, $IgnoreCase=FALSE)
Retrieve item by name.
GetLatestModificationDate($Condition=NULL)
Get newest modification date (based on values in "DateLastModified" column in database table)...
GetCountForItemNames($SearchString, $IncludeVariants=FALSE, $UseBooleanMode=TRUE, $IdExclusions=array(), $ValueExclusions=array())
Retrieve count of items with names matching search string.
CleanOutStaleTempItems($MinutesUntilStale=10080)
Clear out (call the Delete() method) for any temp items more than specified number of minutes old...
ItemExists($ItemId, $IgnoreSqlCondition=FALSE)
Check that item exists with specified ID.
Class that allows permits easier access to MySQL system variables.
InsertBefore($TargetItem, $NewItem)
Insert item into order before specified item.
InsertAfter($TargetItem, $NewItem)
Insert item into order after specified item.
Persistent doubly-linked-list data structure, with its data stored in a specified database table...
GetItems($SqlCondition=NULL)
Retrieve items.
GetNextTempItemId()
Return next available temporary item ID.
Append($Item)
Add item to end of order.
SetOrderOpsCondition($Condition)
Set SQL condition (added to WHERE clause) used to select items for ordering operations.
Common factory class for item manipulation.
Definition: ItemFactory.php:17
GetItemIds($Condition=NULL, $IncludeTempItems=FALSE, $SortField=NULL, $SortAscending=TRUE)
Return array of item IDs.
NameIsInUse($Name, $IgnoreCase=FALSE)
Check whether item name is currently in use.
ClearCurrentEditedItem()
Delete currently edited item and clear currently edited item ID.
ItemFactory($ItemClassName, $ItemTableName, $ItemIdFieldName, $ItemNameFieldName=NULL, $OrderOpsAllowed=FALSE, $SqlCondition=NULL)
Class constructor.
Definition: ItemFactory.php:36
GetNextItemId()
Retrieve next available (non-temp) item ID.