30 require_once
'HTML/QuickForm.php';
31 require_once
'Dataface/Table.php';
32 require_once
'Dataface/Vocabulary.php';
33 require_once
'Dataface/QueryBuilder.php';
34 require_once
'Dataface/ResultController.php';
35 require_once
'Dataface/ResultList.php';
36 require_once
'Dataface/QueryTool.php';
40 $GLOBALS[
'HTML_QUICKFORM_ELEMENT_TYPES'][
'htmlarea'] = array(
'HTML/QuickForm/htmlarea.php',
'HTML_QuickForm_htmlarea');
73 $widgetTypes = array();
76 $this->_query = is_array($query) ? $query : array();
78 if ( !isset( $this->_query[
'-cursor'] ) ){
79 $this->_query[
'-cursor'] = 0;
90 $this->tablename = preg_replace(
'/ /',
'', $this->tablename);
93 $this->_fields = array();
95 $fields = array_keys($this->_table->fields(
false,
true));
97 foreach ($this->_table->relationships() as $relationship){
98 if ( @$relationship->_schema[
'visibility'] and @$relationship->_schema[
'visibility'][
'find'] ==
'hidden' ){
101 $rfields = $relationship->fields(
true);
102 $fkeys = $relationship->getForeignKeyValues();
103 $removedKeys = array();
104 foreach($fkeys as $fkeyTable => $fkey){
105 foreach (array_keys($fkey) as $fkeyKey){
106 $removedKeys[] = $fkeyTable.
'.'.$fkeyKey;
110 $rfields = array_diff($rfields, $removedKeys);
112 foreach ($rfields as $rfield){
113 list($rtable,$rfield) = explode(
'.',$rfield);
114 $fields[] = $relationship->getName().
'.'.$rfield;
117 unset($relationship);
122 $this->_fields = array();
124 $this->_fields[
$fieldname] =& $this->_table->getField($fieldname);
146 if ( $this->_isBuilt ){
149 $this->_isBuilt =
true;
151 $renderer =& $this->defaultRenderer();
152 foreach ($_REQUEST as $qkey=>$qval){
153 if ( strlen($qkey)>1 and $qkey{0} ==
'-' and strpos($qkey,
'-findq:') !== 0){
154 $this->addElement(
'hidden', $qkey);
155 $this->setDefaults( array($qkey=>$qval));
159 $this->addElement(
'hidden',
'--find-submit');
160 $this->setConstants( array(
'--find-submit'=>1));
162 $relatedSections=array();
164 foreach ( $this->_fields as $name =>
$field ){
166 if ( $this->_table->isPassword($name) )
continue;
167 if ( @
$field[
'visibility'][
'find'] ==
'hidden')
continue;
169 $widget =
$field[
'widget'];
170 if ( isset($widget[
'find']) ){
171 $widget = $widget[
'find'];
173 $vocabulary =
$field[
'vocabulary'];
175 if ( $widget[
'type'] ==
'meta' )
continue;
177 $inputName =
$field[
'name'];
179 if ( strpos($name,
'.') !== false ){
183 list($relationshipName,$name) = explode(
'.', $name);
184 $inputName = $relationshipName.
'/'.$name;
186 if ( !isset($relatedSections[$relationshipName]) ){
187 $relationship = $this->_table->getRelationship($relationshipName);
189 die($relationship->toString());
191 $this->addElement(
'submit',
'--submit', df_translate(
'scripts.GLOBAL.LABEL_SUBMIT',
'Submit'));
192 $this->addElement(
'header',$relationshipName,$relationship->getLabel());
193 $relatedSections[$relationshipName] =
true;
197 if ( isset( $vocabulary) && $vocabulary ){
201 if ( is_array($options) ){
202 $opts = array(
''=>df_translate(
'scripts.GLOBAL.FORMS.OPTION_PLEASE_SELECT',
"Please Select..."));
203 foreach ($options as $key=>$value){
204 $opts[$key] = $value;
211 if ( isset(
$field[
'vocabulary']) and
$field[
'vocabulary'] ){
212 $options =
$table->getValuelist($field[
'vocabulary']);
215 $el =& $this->addElement(
'select',
'-findq:'.$inputName, $widget[
'label'], $options, array(
'size'=>
'5',
'multiple'=>1));
216 $widgetTypes[$inputName] =
'select';
217 $el->setFieldDef($field);
218 if ( isset($field[
'repeat']) and $field[
'repeat']){
220 $this->addElement(
'radio',
'-find-op:'.$inputName,
'',df_translate(
'scripts.Dataface_SearchForm.LABEL_MATCH_ALL',
'Match all selected'),
'AND');
223 $this->addElement(
'radio',
'-find-op:'.$inputName,
'',df_translate(
'scripts.Dataface_SearchForm.LABEL_MATCH_ANY',
'Match any selected'),
'OR');
225 $this->addElement(
'radio',
'-find-op:'.$inputName,
'',df_translate(
'scripts.Dataface_SearchForm.LABEL_MATCH_NONE',
'Do not match selected'),
'None');
230 $el =& $this->addElement(
'text',
'-findq:'.$inputName, $widget[
'label'], array(
'class'=>$widget[
'class'],
'id'=>$inputName) );
231 $widgetTypes[$inputName] =
'text';
232 $el->setFieldDef($field);
238 $this->addElement(
'submit',
'--submit',df_translate(
'scripts.GLOBAL.LABEL_FIND',
'Find'));
239 $this->addElement(
'hidden',
'-action');
240 $this->addElement(
'hidden',
'-edit');
241 $this->addElement(
'hidden',
'-table');
244 foreach ($this->_query as $key=>$value){
245 if ( $key{0} !=
'-' ){
246 if ( @$widgetTypes[$key] ==
'select'){
247 $parts = explode(
' OR ', $value);
249 foreach ($parts as $part ){
250 while ( $part and in_array($part{0}, array(
'=',
'<',
'>',
'!') ) ) {
251 $part = substr($part,1);
258 $defaults[
'-findq:'.$key] = $value;
260 $defaults[$key] = $value;
264 $this->setDefaults( $defaults);
265 $this->setConstants(array(
'-action'=>
'find',
'-edit'=>1,
'-table'=>$this->tablename));
275 $tableLabel = htmlspecialchars($this->_table->getLabel());
278 'tableLabel' => $tableLabel
279 ),
'Dataface_Search_Instructions.html'
283 import(
'Dataface/FormTool.php');
285 $ft->display($this,
'Dataface_FindForm.html');
295 $query =
$app->getQuery();
297 if ( isset( $values[
'-find:result_action']) ){
298 $qstr =
'-action='.$values[
'-find:result_action'];
300 $qstr =
'-action=list';
302 if ( isset($values[
'-skip']) ) $values[
'-skip'] = 0;
303 if ( isset($values[
'-cursor']) ) $values[
'-cursor'] = 0;
307 foreach ($values as $key=>$value){
308 if ( strpos($key,
'-find-op:') === 0 ){
309 $key = substr($key, 9);
310 if ( !isset($values[
'-findq:'.$key]) or !is_array($values[
'-findq:'.$key]) ){
311 $values[
'-findq:'.$key] = array(
'');
315 foreach ($values as $key=>$value){
316 if ( strpos($key,
'-findq:') === 0 ){
317 $key = substr($key, 7);
318 $field = $this->_table->getField(str_replace(
'/',
'.',$key));
320 echo
"Failed to get field $key: ".$field->getMessage();
322 if ( is_array($value) and count($value) > 0){
323 $op = ( (isset( $values[
'-find-op:'.$key] ) ) ? $values[
'-find-op:'.$key] :
'AND');
324 if (!isset(
$field[
'repeat']) or !
$field[
'repeat']) $op =
'OR';
325 if ( isset($values[
'-find-op:'.$key]) and $values[
'-find-op:'.$key] ==
'None' ){
326 $qstr .=
'&'.urlencode($key).
'='.urlencode(
'=');
328 $qstr .=
'&'.urlencode($key).
'='.urlencode(
'='.implode(
' '.$op.
' =', $value));
330 }
else if ( !empty($value) ){
332 $qstr .=
'&'.urlencode($key).
'='.urlencode($value);
335 }
else if ( $key{0} ==
'-' and $key{1} !=
'-' and $key !=
'-action' and $key !=
'-search' and strpos($key,
'-find') !== 0 ){
336 $qstr .=
'&'.urlencode($key).
'='.urlencode($value);
341 $url =
$_SERVER[
'HOST_URI'].DATAFACE_SITE_HREF.
'?'.$qstr;
342 $app->redirect($url);
345 function process($callback=null, $mergFiles=
true){
346 if ( isset( $this->_query[
'--find-submit']) ){
355 foreach ($this->_fields as $key=>$value){
356 if ( strtolower($value[
'Key']) == strtolower(
'PRI') ){
357 $keys[$key] =& $this->_fields[$key];
364 return Dataface_Table::_deserialize(
$field);
371 return Dataface_Table::_serialize(
$field);