Framework  3.9
SearchParameters Class Reference

The SearchParameters class interprets the set of input parameters for a search and generates the corresponding SQL constraints to perform the search against the database. More...

Public Member Functions

 __sleep ()
 
 copy ($src)
 
 SearchParameters ($target, $form=null)
 
 get ($field, $mode=null)
 
 setHandler ($field, $handler)
 Override default handling by setting a custom callback to generate the constraint for a field. More...
 
 fromArray ($array)
 
 setParam ($fieldname, $mode, $value)
 
 clearParam ($field)
 Clear any parameters set for the specified field. More...
 
 set ($field, $mode, $value)
 
 fromGET ()
 
 fromPOST ()
 
 remapField ($old, $new)
 
 copyField ($old, $new)
 
 secondaryFields ($src)
 Specifies additional data fields to search when querying for the given source field. More...
 
 toQueryString ()
 
 generateConstraint ($first=true, $firstText="WHERE")
 generateConstraint More...
 
 getClause ($handler, $modifier, $field, $value)
 
 _getClause ($handler, $modifier, $field, $value)
 

Static Public Member Functions

static joinClauses (&$first, $clause, $joinWith="AND", $firstText)
 joinClauses More...
 
static closeClause (&$constraint, $firstText)
 

Public Attributes

 $form
 
 $target
 
 $params
 
 $handlers
 
 $joinWith = "AND"
 
 $paramHandlers = array()
 
 $secondaryFields = array()
 
 $additional = array()
 
 $empty = true
 true if no search parameters were specified, false if any parameter was found More...
 

Detailed Description

The SearchParameters class interprets the set of input parameters for a search and generates the corresponding SQL constraints to perform the search against the database.

Author
andy

Definition at line 556 of file search_form.inc.

Member Function Documentation

◆ __sleep()

SearchParameters::__sleep ( )

Definition at line 569 of file search_form.inc.

570  {
571  return SearchParameters::$serializedFields;
572  }

◆ _getClause()

SearchParameters::_getClause (   $handler,
  $modifier,
  $field,
  $value 
)

Definition at line 894 of file search_form.inc.

895  {
896  $clause = "";
897 
898  switch($modifier)
899  {
900  case "equal":
901 
902  $clause = $handler->equal($field, $value);
903  break;
904 
905  case "like":
906 
907  $clause = $handler->like($field, $value);
908  break;
909 
910  case "startsWith":
911 
912  $clause = $handler->startsWith($field, $value);
913  break;
914 
915  case "from":
916 
917  if (array_key_exists("{$field}:to", $this->params))
918  {
919  trace("date range", 3);
920  }
921  else
922  {
923  $clause = $handler->from($field, $value);
924  }
925  break;
926 
927  case "to":
928 
929  $clause = $handler->to($field, $value);
930  break;
931 
932  case "range":
933 
934  $param = $this->params[$field];
935  $clause = $handler->range($field, $param->from, $param->to);
936  break;
937 
938  case "member":
939 
940  $clause = $handler->member($field, $value);
941  break;
942 
943  case "any":
944 
945  $clause = $handler->any($field, $value);
946  break;
947 
948  case "all":
949 
950  $clause = $handler->all($field, $value);
951  break;
952 
953  case "fullName":
954 
955  $clause = $handler->fullName($field, $value);
956  break;
957 
958  case "checked":
959 
960  $clause = $handler->checked($field, $value);
961  break;
962 
963  default:
964  break;
965 
966  } // end case statement
967 
968  return $clause;
969  }
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ clearParam()

SearchParameters::clearParam (   $field)

Clear any parameters set for the specified field.

Parameters
string$fieldthe name of the field

Definition at line 733 of file search_form.inc.

734  {
735  foreach($this->params as $name => $value)
736  {
737  list($f, $mode) = explode(":", $name);
738  if ($f == $field) unset($this->params["$f:$mode"]);
739  }
740  }

◆ closeClause()

static SearchParameters::closeClause ( $constraint,
  $firstText 
)
static

Definition at line 1001 of file search_form.inc.

1002  {
1003  /*
1004  * If no clauses were created, remove the
1005  * $firstText if "AND (" or "OR (".
1006  * Close out an opened group of clauses, by
1007  * adding the ")";
1008  */
1009  if($constraint == "") return;
1010 
1011  if(strpos($firstText, "(") !== false)
1012  {
1013  $constraint .= ") ";
1014  }
1015  }

◆ copy()

SearchParameters::copy (   $src)

Definition at line 574 of file search_form.inc.

575  {
576  $this->params = $src->params;
577  foreach(SearchParameters::$serializedFields as $field)
578  {
579  $this->$field = $src->$field;
580  }
581  }

◆ copyField()

SearchParameters::copyField (   $old,
  $new 
)

Definition at line 770 of file search_form.inc.

771  {
772  $this->params[$new] = $this->params[$old];
773  }

◆ fromArray()

SearchParameters::fromArray (   $array)

Definition at line 649 of file search_form.inc.

650  {
651  $this->empty = true;
652  $this->isDefaultState = true;
653 
654  $fields = array_keys($this->target->getFields());
655  foreach($array as $field => $value)
656  {
657  list($fieldname, $mode) = explode(":", $field);
658 
659  $skip = false;
660 
661  if ($this->target->filter && $this->target->filter->isExcluded($fieldname))
662  {
663  trace("## Skipping $fieldname", 4);
664  continue;
665  }
666 
667  if (!$this->target->hasField($fieldname) &&
668  (!$this->form || !$this->form->hasAdditional($fieldname)))
669  {
670  trace("### Skipping $fieldname", 4);
671  continue;
672  }
673 
674  $this->isDefaultState = false;
675  //if (!$mode) continue;
676  if ($value != "")
677  {
678  $this->empty = false;
679 
680  $this->setParam($fieldname, $mode, $value);
681  }
682  }
683  }
setParam($fieldname, $mode, $value)

◆ fromGET()

SearchParameters::fromGET ( )

Definition at line 747 of file search_form.inc.

748  {
749  $this->fromArray($_GET);
750  }

◆ fromPOST()

SearchParameters::fromPOST ( )

Definition at line 752 of file search_form.inc.

753  {
754  $this->fromArray($_POST);
755  }

◆ generateConstraint()

SearchParameters::generateConstraint (   $first = true,
  $firstText = "WHERE" 
)

generateConstraint

@first: whether this set of clauses is the first in a group

@firstText: the text is this is the first clause: either WHERE or AND (

Definition at line 820 of file search_form.inc.

821  {
822  $constraint = "";
823 
824  trace(print_r($this->params, true), 3);
825 
826  foreach($this->params as $field => $param)
827  {
828  $joinWith = (!$first) ? $this->joinWith : "";
829 
830  $modifier = $param->mode;
831  $value = $param->value;
832 
833 // if (strpos($param, ':') > 0)
834 // {
835 // list($field, $modifier) = explode(":", $param);
836 // }
837 // else
838 // {
839 // $field = $param;
840 // $modifier = "equal";
841 // }
842 
843  // JDG 10/25/2011 - allow callback function for clauses
844  if(!array_key_exists($field, $this->paramHandlers))
845  {
846  $fields = $this->target->getFields();
847  $handler = $this->handlers[$fields[$field]];
848 
849  //AJG: Default to String if field type is not known
850  if (!$handler) $handler = new StringSearchParameterHandler();
851  $clause = $this->getClause($handler, $modifier, $field, $value);
852  }
853  else
854  {
855  $handler = $this->paramHandlers[$field];
856 
857  if(is_callable($handler))
858  $clause = call_user_func($handler, $field, $value);
859  elseif(method_exists($handler, $modifier))
860  $clause = $handler->$modifier($field, $value);
861  }
862 
863  $constraint .= $this->joinClauses($first, $clause, $joinWith, $firstText);
864  }
865 
866  $this->closeClause($constraint, $firstText);
867 
868  return $constraint;
869  }
static closeClause(&$constraint, $firstText)
static joinClauses(&$first, $clause, $joinWith="AND", $firstText)
joinClauses
getClause($handler, $modifier, $field, $value)
StringSearchParameterHandler is a SearchParameterHandler for text database types.

◆ get()

SearchParameters::get (   $field,
  $mode = null 
)

Definition at line 601 of file search_form.inc.

602  {
603  if (!isset($this->params[$field])) return null;
604 
605  $val = $this->params[$field];
606  if ($val->mode == "range")
607  {
608  if ($mode == "from")
609  {
610  $value = $val->from;
611  }
612  else if ($mode == "to")
613  {
614  $value = $val->to;
615  }
616  else return null;
617  }
618  else
619  {
620  if ($mode !== null && $val->mode != $mode) return null;
621  }
622 
623  $value = $val->value;
624  trace("SearchParameters::get({$field}:$mode): $value", 3);
625  return $value;
626  }

◆ getClause()

SearchParameters::getClause (   $handler,
  $modifier,
  $field,
  $value 
)

Definition at line 871 of file search_form.inc.

872  {
873  $clause = array();
874 
875  $clause[] = $this->_getClause($handler, $modifier, $field, $value);
876 
877  if (isset($this->secondaryFields[$field]))
878  {
879  $secondaries = $this->secondaryFields[$field];
880 
881  foreach($secondaries as $secondary)
882  {
883  $clause[] = $this->_getClause($handler, $modifier, $secondary, $value);
884  }
885 
886  return "(".implode(" OR ", $clause).") ";
887  }
888  else
889  {
890  return $clause[0];
891  }
892  }
_getClause($handler, $modifier, $field, $value)
secondaryFields($src)
Specifies additional data fields to search when querying for the given source field.

◆ joinClauses()

static SearchParameters::joinClauses ( $first,
  $clause,
  $joinWith = "AND",
  $firstText 
)
static

joinClauses

@first: whether this is the first clause; if so start with "WHERE"

@clause: the statement, e.g., l_name like 'smith'

@joinWith: specify is statement is to be joined with OR or AND

@firstText: the text to output if first: WHERE or "" - clause may be first in a group "(" rather than first of all clauses.

Definition at line 983 of file search_form.inc.

984  {
985  if ($clause != "")
986  {
987  if ($first)
988  {
989  $constraint = "{$firstText} $clause";
990  $first = false;
991  }
992  else
993  {
994  $constraint .= " $joinWith $clause";
995  }
996  }
997 
998  return $constraint;
999  }

◆ remapField()

SearchParameters::remapField (   $old,
  $new 
)

Definition at line 757 of file search_form.inc.

758  {
759  foreach($this->params as $key => $value)
760  {
761  list($field, $mode) = explode(":", $key);
762  if ($field == $old)
763  {
764  $this->params["$new:$mode"] = $value;
765  unset($this->params["$old:$mode"]);
766  }
767  }
768  }

◆ SearchParameters()

SearchParameters::SearchParameters (   $target,
  $form = null 
)

Definition at line 583 of file search_form.inc.

584  {
585  $this->target = $target;
586  $this->form = $form;
587  $this->params = array();
588 
589  $this->handlers = array();
590 
591  $this->handlers[String] = new StringSearchParameterHandler();
592  $this->handlers[HTML] = new StringSearchParameterHandler();
593  $this->handlers[Date] = new DateSearchParameterHandler();
594  $this->handlers[DateTime] = new DateSearchParameterHandler();
595  $this->handlers[Timestamp] = new DateSearchParameterHandler();
596  $this->handlers[Number] = new NumberSearchParameterHandler();
597  $this->handlers[Currency] = new NumberSearchParameterHandler();
598  $this->handlers[Boolean] = new BooleanSearchParameterHandler();
599  }
DateSearchParameterHandler is a SearchParameterHandler for date database types.
NumberSearchParameterHandler is a SearchParameterHandler for numeric database types.

◆ secondaryFields()

SearchParameters::secondaryFields (   $src)

Specifies additional data fields to search when querying for the given source field.

Parameters
string$srcname of the primary field
[string]the list of additional fields to search

Definition at line 781 of file search_form.inc.

782  {
783  if (!isset($this->secondaryFields[$src]))
784  {
785  $this->secondaryFields[$src] = array();
786  }
787 
788  for($i = 1; $i < func_num_args(); ++$i)
789  {
790  $this->secondaryFields[$src][] = func_get_arg($i);
791  }
792  }

◆ set()

SearchParameters::set (   $field,
  $mode,
  $value 
)

Definition at line 742 of file search_form.inc.

743  {
744  $this->setParam($field, $mode, $value);
745  }

◆ setHandler()

SearchParameters::setHandler (   $field,
  $handler 
)

Override default handling by setting a custom callback to generate the constraint for a field.

Parameters
$field- the field name in the obj to be queried

@handler - callback function such as array(MyClass, MyFunction)

When the function is called, it is given the parameters $name and $value

Definition at line 640 of file search_form.inc.

641  {
642  $this->paramHandlers[$field] = $handler;
643  if (!$this->target->hasField($field))
644  {
645  $this->additional[] = $field;
646  }
647  }

◆ setParam()

SearchParameters::setParam (   $fieldname,
  $mode,
  $value 
)

Definition at line 689 of file search_form.inc.

690  {
691  if (is_array($value)) $value = implode(",", array_values($value));
692 
693  if (isset($this->params[$fieldname]))
694  {
695  $p = $this->params[$fieldname];
696  if ($mode)
697  {
698  if ($mode == "from" && $p->mode == "to")
699  {
700  $p->mode = "range";
701  $p->to = $p->value;
702  $p->from = $value;
703  $p->value = null;
704  }
705  else if ($mode == "to" && $p->mode == "from")
706  {
707  $p->mode = "range";
708  $p->to = $value;
709  $p->from = $p->value;
710  $p->value = null;
711  }
712  else
713  {
714  $p->mode = $mode;
715  $p->value = $value;
716  }
717  }
718  else
719  {
720  $p->value = $value;
721  }
722  }
723  else
724  {
725  $this->params[$fieldname] = new SearchParameterValue($mode, $value);
726  }
727  }

◆ toQueryString()

SearchParameters::toQueryString ( )

Definition at line 794 of file search_form.inc.

795  {
796  $qs = "";
797 
798  foreach($this->params as $name => $value)
799  {
800  if ($value == "") continue;
801 
802  $qs .= ($qs == "") ? "?" : "&";
803 
804  $qs .= urlencode($name);
805  $qs .= "=";
806  $qs .= urlencode($value);
807  }
808 
809  return $qs;
810  }

Member Data Documentation

◆ $additional

SearchParameters::$additional = array()

Definition at line 565 of file search_form.inc.

◆ $empty

SearchParameters::$empty = true

true if no search parameters were specified, false if any parameter was found

Definition at line 566 of file search_form.inc.

◆ $form

SearchParameters::$form

Definition at line 558 of file search_form.inc.

◆ $handlers

SearchParameters::$handlers

Definition at line 561 of file search_form.inc.

◆ $joinWith

SearchParameters::$joinWith = "AND"

Definition at line 562 of file search_form.inc.

◆ $paramHandlers

SearchParameters::$paramHandlers = array()

Definition at line 563 of file search_form.inc.

◆ $params

SearchParameters::$params

Definition at line 560 of file search_form.inc.

◆ $secondaryFields

SearchParameters::$secondaryFields = array()

Definition at line 564 of file search_form.inc.

◆ $target

SearchParameters::$target

Definition at line 559 of file search_form.inc.


The documentation for this class was generated from the following file: