CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ControlledNameFactory.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: ControlledNameFactory.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2011-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9
13 class ControlledNameFactory extends ItemFactory
14 {
15
16 # ---- PUBLIC INTERFACE --------------------------------------------------
17
22 public function __construct($FieldId = NULL)
23 {
24 # save field ID for our later use
25 $this->FieldId = $FieldId;
26
27 # set up item factory base class
28 parent::__construct("ControlledName", "ControlledNames",
29 "ControlledNameId", "ControlledName", FALSE,
30 ($FieldId ? "FieldId = ".intval($FieldId) : NULL));
31 }
32
38 public function GetUsageCount()
39 {
40 return $this->DB->Query(
41 "SELECT COUNT(DISTINCT RNI.ResourceId) AS ResourceCount"
42 ." FROM ResourceNameInts RNI, ControlledNames CN"
43 ." WHERE CN.FieldId = ".intval($this->FieldId)
44 ." AND RNI.ControlledNameId = CN.ControlledNameId"
45 ." AND RNI.ResourceId >= 0",
46 "ResourceCount");
47 }
48
58 public function FindMatchingRecentlyUsedValues($SearchString, $NumberOfResults=5,
59 $IdExclusions=array(), $ValueExclusions=array() )
60 {
61 # return no results if empty search string passed in
62 if (!strlen(trim($SearchString))) { return array(); }
63
64 $IdExclusionSql = (count($IdExclusions)>0)
65 ? "AND ControlledNameId NOT IN ("
66 .implode(',', array_map('intval', $IdExclusions)).")"
67 : "";
68
69 $ValueExclusionSql = (count($ValueExclusions)>0)
70 ? "AND ControlledName NOT IN ("
71 .implode(',', array_map(
72 function($v){ return "'".addslashes($v)."'"; },
73 $ValueExclusions) ).")"
74 : "";
75
76 # mark all search elements as required
77 $SearchString = preg_replace("%\S+%", "+\$0", $SearchString);
78
79 $QueryString =
80 "SELECT ControlledNameId, ControlledName FROM ControlledNames "
81 ."WHERE FieldId=".$this->FieldId
82 ." AND LastAssigned IS NOT NULL"
83 ." AND MATCH(ControlledName) AGAINST ('"
84 .addslashes(trim($SearchString))."' IN BOOLEAN MODE)"
85 ." ".$IdExclusionSql
86 ." ".$ValueExclusionSql
87 ." ORDER BY LastAssigned DESC LIMIT ".$NumberOfResults;
88
89 $this->DB->Query($QueryString);
90
91 $Names = $this->DB->FetchColumn("ControlledName", "ControlledNameId");
92
93 return $Names;
94 }
95
103 public function ControlledNameSearch($SearchString)
104 {
105 # escape special chars in the regex
106 $CNRegex = preg_quote($SearchString);
107
108 # replace * and space with wild cards
109 $CNRegex = str_replace("\\*", ".*.", $CNRegex);
110 $CNRegex = str_replace(" ", ".*.", $CNRegex);
111
112 # add escaping for sql
113 $CNRegex = addslashes($CNRegex);
114
115 # construct and execute our SQL query
116 $Query = "SELECT C.ControlledNameId AS ControlledNameId ".
117 "FROM ControlledNames AS C LEFT JOIN VariantNames AS V ON ".
118 "C.ControlledNameId = V.ControlledNameId WHERE (".
119 "ControlledName REGEXP \"".$CNRegex."\" OR ".
120 "VariantName REGEXP \"".$CNRegex."\") ".
121 "AND C.FieldId = ".$this->FieldId." ".
122 "ORDER BY ControlledName";
123 $this->DB->Query($Query);
124
125 # return the matching CNIDs
126 return $this->DB->FetchColumn("ControlledNameId");
127 }
128
129 # ---- PRIVATE INTERFACE -------------------------------------------------
130
131 private $FieldId;
132 }
ControlledNameFactory\__construct
__construct($FieldId=NULL)
Constructor for ControlledNameFactory class.
Definition: ControlledNameFactory.php:22
PHP
PHP
Definition: RSSClient--Test.php:12
ControlledNameFactory
Factory for manipulating ControlledName objects.
Definition: ControlledNameFactory.php:13
ControlledNameFactory\GetUsageCount
GetUsageCount()
Determine how many resources have controlled names (associated with this metadata field) assigned to ...
Definition: ControlledNameFactory.php:38
ControlledNameFactory\ControlledNameSearch
ControlledNameSearch($SearchString)
Search for ControlledNames or variants that match a search string.
Definition: ControlledNameFactory.php:103
ItemFactory
Common factory class for item manipulation.
Definition: ItemFactory.php:17
ControlledNameFactory\FindMatchingRecentlyUsedValues
FindMatchingRecentlyUsedValues($SearchString, $NumberOfResults=5, $IdExclusions=array(), $ValueExclusions=array())
Retrieve recently used items matching a search string.
Definition: ControlledNameFactory.php:58