| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TScaffoldSearch class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id$
|
|
|
10 |
* @package System.Data.ActiveRecord.Scaffold
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Import the scaffold base.
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* TScaffoldSearch provide a simple textbox and a button that is used
|
|
|
20 |
* to perform search on a TScaffoldListView with ID given by {@link setListViewID ListViewID}.
|
|
|
21 |
*
|
|
|
22 |
* The {@link getSearchText SearchText} property is a TTextBox and the
|
|
|
23 |
* {@link getSearchButton SearchButton} property is a TButton with label value "Search".
|
|
|
24 |
*
|
|
|
25 |
* Searchable fields of the Active Record can be restricted by specifying
|
|
|
26 |
* a comma delimited string of allowable fields in the
|
|
|
27 |
* {@link setSearchableFields SearchableFields} property. The default is null,
|
|
|
28 |
* meaning that most text type fields are searched (the default searchable fields
|
|
|
29 |
* are database dependent).
|
|
|
30 |
*
|
|
|
31 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
32 |
* @version $Id$
|
|
|
33 |
* @package System.Data.ActiveRecord.Scaffold
|
|
|
34 |
* @since 3.1
|
|
|
35 |
*/
|
|
|
36 |
class TScaffoldSearch extends TScaffoldBase
|
|
|
37 |
{
|
|
|
38 |
/**
|
|
|
39 |
* @var TScaffoldListView the scaffold list view.
|
|
|
40 |
*/
|
|
|
41 |
private $_list;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* @return TScaffoldListView the scaffold list view this search box belongs to.
|
|
|
45 |
*/
|
|
|
46 |
protected function getListView()
|
|
|
47 |
{
|
|
|
48 |
if($this->_list===null && ($id = $this->getListViewID()) !== null)
|
|
|
49 |
{
|
|
|
50 |
$this->_list = $this->getParent()->findControl($id);
|
|
|
51 |
if($this->_list ===null)
|
|
|
52 |
throw new TConfigurationException('scaffold_unable_to_find_list_view', $id);
|
|
|
53 |
}
|
|
|
54 |
return $this->_list;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* @param string ID of the TScaffoldListView this search control belongs to.
|
|
|
59 |
*/
|
|
|
60 |
public function setListViewID($value)
|
|
|
61 |
{
|
|
|
62 |
$this->setViewState('ListViewID', $value);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* @return string ID of the TScaffoldListView this search control belongs to.
|
|
|
67 |
*/
|
|
|
68 |
public function getListViewID()
|
|
|
69 |
{
|
|
|
70 |
return $this->getViewState('ListViewID');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Sets the SearchCondition of the TScaffoldListView as the search terms
|
|
|
75 |
* given by the text of the search text box.
|
|
|
76 |
*/
|
|
|
77 |
public function bubbleEvent($sender, $param)
|
|
|
78 |
{
|
|
|
79 |
if(strtolower($param->getCommandName())==='search')
|
|
|
80 |
{
|
|
|
81 |
if(($list = $this->getListView()) !== null)
|
|
|
82 |
{
|
|
|
83 |
$list->setSearchCondition($this->createSearchCondition());
|
|
|
84 |
return false;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
$this->raiseBubbleEvent($this, $param);
|
|
|
88 |
return true;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* @return string the search criteria for the search terms in the search text box.
|
|
|
93 |
*/
|
|
|
94 |
protected function createSearchCondition()
|
|
|
95 |
{
|
|
|
96 |
$table = $this->getTableInfo();
|
|
|
97 |
if(strlen($str=$this->getSearchText()->getText()) > 0)
|
|
|
98 |
{
|
|
|
99 |
$builder = $table->createCommandBuilder($this->getRecordFinder()->getDbConnection());
|
|
|
100 |
return $builder->getSearchExpression($this->getFields(), $str);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* @return array list of fields to be searched.
|
|
|
106 |
*/
|
|
|
107 |
protected function getFields()
|
|
|
108 |
{
|
|
|
109 |
if(strlen(trim($str=$this->getSearchableFields()))>0)
|
|
|
110 |
$fields = preg_split('/\s*,\s*/', $str);
|
|
|
111 |
else
|
|
|
112 |
$fields = $this->getTableInfo()->getColumns()->getKeys();
|
|
|
113 |
return $fields;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* @return string comma delimited list of fields that may be searched.
|
|
|
118 |
*/
|
|
|
119 |
public function getSearchableFields()
|
|
|
120 |
{
|
|
|
121 |
return $this->getViewState('SearchableFields','');
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* @param string comma delimited list of fields that may be searched.
|
|
|
126 |
*/
|
|
|
127 |
public function setSearchableFields($value)
|
|
|
128 |
{
|
|
|
129 |
$this->setViewState('SearchableFields', $value, '');
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* @return TButton button with default label "Search".
|
|
|
134 |
*/
|
|
|
135 |
public function getSearchButton()
|
|
|
136 |
{
|
|
|
137 |
$this->ensureChildControls();
|
|
|
138 |
return $this->getRegisteredObject('_search');
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* @return TTextBox search text box.
|
|
|
143 |
*/
|
|
|
144 |
public function getSearchText()
|
|
|
145 |
{
|
|
|
146 |
$this->ensureChildControls();
|
|
|
147 |
return $this->getRegisteredObject('_textbox');
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|