Skip to content

Instantly share code, notes, and snippets.

@j6s
Created November 21, 2014 15:21
Show Gist options
  • Select an option

  • Save j6s/bae25d4ab6935b2c91f1 to your computer and use it in GitHub Desktop.

Select an option

Save j6s/bae25d4ab6935b2c91f1 to your computer and use it in GitHub Desktop.
<?php
/**
* @param array $filter
* The inner arrays will be treated as AND and the outer as OR
* [
* [
* {
* type: string (min|max|equals|contains),
* field: string,
* val: string | float,
* negate: boolean
* },
* ...
* ], [
* {
* ...
* }
* ]
* ]
* A simple query can be writte without the array mess:
* => (time > time())
* array(
* "field" => "time",
* "type" => "min",
* "val" => time()
* )
*
* A simple AND connection can also be written with less arrays:
* => (time > time() AND title LIKE %awesome%)
* array(
* array(
* "field" => "time",
* "type" => "min",
* "val" => time()
* ),
* array(
* "field" => "title",
* "type" => "contains",
* "val" => "awesome"
* )
* )
*
*
*
*
* @param array $settings
* {
* offset: integer,
* limit: integer,
* orderBy: {
* "fieldname" => "ASC/DESC"
* },
* showHidden: boolean
* }
*
*
*
* @example the following filterarray will create something like this:
* (field1 == "test" && field2 < 5) || (field3 > 2 && field4 LIKE "%bla%")
*
* array(
* array(
* array(
* "field" => "field1",
* "type" => "equals",
* "val" => "test"
* ),
* array(
* "field" => "field2",
* "type" => "max",
* "val" => 5
* )
* ),
* array(
* array(
* "field" => "field3",
* "type" => "min"
* "val" => 2
* ),
* array(
* "field" => "field4",
* "type" => "contains",
* "val" => "bla"
* )
* )
* )
*
*@return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function filter($filter,$settings = NULL){
$f = $this->getQuery($filter,$settings);
return $f->execute();
}
/**
* @param array $filter
* @return int
*/
public function countFilter($filter){
return $this->getQuery($filter)->count();
}
/**
* Creates a query with the filter options
*
* @param array|null $filter
* @param array|null $settings
*
* @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
*/
public function getQuery($filter = NULL,$settings = NULL){
$filter = is_array($filter[0]) ? $filter : array($filter);
$filter = is_array($filter[0][0]) ? $filter : array($filter);
NIMGeneral::log(print_r($filter,true),NIMGeneral::LOG_INFO);
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(false);
if($filter != NULL && !empty($filter)){
$qor = array();
foreach($filter as $or){
$qand = array();
foreach($or as $and){
$__tmp_qand = "";
switch($and["type"]){
case "min":
$__tmp_qand = $query->greaterThanOrEqual($and["field"], (float)$and["val"]);
break;
case "max":
$__tmp_qand = $query->lessThanOrEqual($and["field"], (float)$and["val"]);
break;
case "equals":
$__tmp_qand = $query->equals($and["field"], $and["val"]);
break;
case "contains":
// if it is not a string, then we are looking for a relation contains
if(!is_string($and["val"])){
$__tmp_qand = $query->contains($and["field"], $and["val"]);
break;
}
// If it is, we just want a LIKE %val%
$and["val"] = "%{$and["val"]}%";
case "like":
$__tmp_qand = $query->like($and["field"],$and["val"]);
break;
default:
break;
}
if($and["negate"] === true){
$qand[] = $query->logicalNot($__tmp_qand);
} else {
$qand[] = $__tmp_qand;
}
unset($__tmp_qand);
NIMGeneral::log($and["field"]. " " . $and["type"] . " " . $and["val"],NIMGeneral::LOG_TEXT);
}
NIMGeneral::log("AND",NIMGeneral::LOG_TEXT);
$qor[] = $query->logicalAnd($qand);
}
NIMGeneral::log("OR",NIMGeneral::LOG_TEXT);
$query->matching($query->logicalOr($qor));
}
if($settings != NULL) {
if(!empty($settings["limit"])){
$query->setLimit($settings["limit"]);
}
if(!empty($settings["offset"])){
$query->setOffset((int)$settings["offset"]);
if(empty($settings["limit"])){
throw new InvalidPropertyException("Repository: if offset is set, limit also must be set");
}
}
if(!empty($settings["orderBy"])){
$field = array_keys($settings["orderBy"]);
$field = $field[0];
// ASCENDING als default, DESCENDING; nur wenn wirklich gewollt
$ordering = ($settings["orderBy"][$field] == "DESC") ? QueryInterface::ORDER_DESCENDING : QueryInterface::ORDER_ASCENDING;
$query->setOrderings(array($field => $ordering));
}
if(!empty($settings["showHidden"]) && $settings["showHidden"] === true){
$query->getQuerySettings()->setIgnoreEnableFields(true);
}
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment