Skip to content
This repository
Browse code

ENHANCEMENT: Made the tree search form more extensible.

- Use DataQuery for generating the search query.
- For custom field matching, use searchcontext rather than a LIKE query.
- Added an extension hook for updating the search form.
  • Loading branch information...
commit 21bcc01878c94605e2ac251e01a8dd6726290640 1 parent 97d678b
Andrew Short authored wilr committed
1  code/controllers/CMSMain.php
@@ -303,6 +303,7 @@ function SearchForm() {
303 303
 			->unsetValidator();
304 304
 		$form->loadDataFrom($this->request->getVars());
305 305
 
  306
+		$this->extend('updateSearchForm', $form);
306 307
 		return $form;
307 308
 	}
308 309
 	
51  code/controllers/CMSSiteTreeFilter.php
@@ -173,46 +173,53 @@ static function title() {
173 173
 	 * 
174 174
 	 * @return Array
175 175
 	 */
176  
-	function pagesIncluded() {
  176
+	public function pagesIncluded() {
  177
+		$sng = singleton('SiteTree');
177 178
 		$ids = array();
178  
-		$q = new SQLQuery();
179  
-		$q->setSelect(array('"ID"','"ParentID"'))
180  
-			->setFrom('"SiteTree"');
181  
-		$where = array();
182  
-		
183  
-		$SQL_params = Convert::raw2sql($this->params);
184  
-		foreach($SQL_params as $name => $val) {
  179
+
  180
+		$query = new DataQuery('SiteTree');
  181
+		$query->setQueriedColumns(array('ID', 'ParentID'));
  182
+
  183
+		foreach($this->params as $name => $val) {
  184
+			$SQL_val = Convert::raw2sql($val);
  185
+
185 186
 			switch($name) {
186  
-				// Match against URLSegment, Title, MenuTitle & Content
187 187
 				case 'Term':
188  
-					if($val) $where[] = "\"URLSegment\" LIKE '%$val%' OR \"Title\" LIKE '%$val%' OR \"MenuTitle\" LIKE '%$val%' OR \"Content\" LIKE '%$val%'";
  188
+					$query->whereAny(array(
  189
+						"\"URLSegment\" LIKE '%$SQL_val%'",
  190
+						"\"Title\" LIKE '%$SQL_val%'",
  191
+						"\"MenuTitle\" LIKE '%$SQL_val%'",
  192
+						"\"Content\" LIKE '%$SQL_val%'"
  193
+					));
189 194
 					break;
190  
-				// Match against date
  195
+
191 196
 				case 'LastEditedFrom':
192  
-					if($val) $where[] = "\"LastEdited\" >= '$val'";
  197
+					$query->where("\"LastEdited\" >= '$SQL_val'");
193 198
 					break;
  199
+
194 200
 				case 'LastEditedTo':
195  
-					if($val) $where[] = "\"LastEdited\" <= '$val'";
  201
+					$query->where("\"LastEdited\" <= '$SQL_val'");
196 202
 					break;
197  
-				// Match against exact ClassName
  203
+
198 204
 				case 'ClassName':
199 205
 					if($val && $val != 'All') {
200  
-						$where[] = "\"ClassName\" = '$val'";
  206
+						$query->where("\"ClassName\" = '$SQL_val'");
201 207
 					}
202 208
 					break;
  209
+
203 210
 				default:
204  
-					// Partial string match against a variety of fields 
205  
-					if(!empty($val) && singleton("SiteTree")->hasDatabaseField($name)) {
206  
-						$where[] = "\"$name\" LIKE '%$val%'";
  211
+					if(!empty($val) && $sng->hasDatabaseField($name)) {
  212
+						$filter = $sng->dbObject($name)->defaultSearchFilter();
  213
+						$filter->setValue($val);
  214
+						$filter->apply($query);
207 215
 					}
208 216
 			}
209 217
 		}
210  
-		$q->setWhere(empty($where) ? '' : '(' . implode(') AND (',$where) . ')');
211 218
 
212  
-		foreach($q->execute() as $row) {
213  
-			$ids[] = array('ID'=>$row['ID'],'ParentID'=>$row['ParentID']);
  219
+		foreach($query->execute() as $row) {
  220
+			$ids[] = array('ID' => $row['ID'], 'ParentID' => $row['ParentID']);
214 221
 		}
215  
-		
  222
+
216 223
 		return $ids;
217 224
 	}
218 225
 }

0 notes on commit 21bcc01

Please sign in to comment.
Something went wrong with that request. Please try again.