Skip to content

Commit

Permalink
Completed more criteria filters for live prev. #21
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamminf committed Jun 4, 2016
1 parent 121282b commit 76e75dd
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions neo/models/Neo_CriteriaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class Neo_CriteriaModel extends ElementCriteriaModel
private $_allElements;
private $_currentFilters = [];

private $_descendant = null;

public static function convert(ElementCriteriaModel $ecm)
{
$attributes = array_filter($ecm->getAttributes(), function($value)
Expand Down Expand Up @@ -67,16 +65,16 @@ private function _runFilters()
{
if(!empty($this->_allElements))
{
$elements = array_filter($this->_allElements, function($element)
$elements = array_filter($this->_allElements, function($element, $index)
{
return $this->_elementFilters($element);
return $this->_elementFilters($element, $index);
});

$this->setMatchedElements($elements);
}
}

private function _elementFilters($element)
private function _elementFilters($element, $index)
{
foreach($this->filterOrder as $filter)
{
Expand All @@ -85,7 +83,7 @@ private function _elementFilters($element)
$value = $this->_currentFilters[$filter];
$method = '__' . $filter;

if(!$this->$method($element, $value))
if(!$this->$method($element, $value, $index))
{
return false;
}
Expand All @@ -99,6 +97,9 @@ private function _elementFilters($element)
* Filtering methods
*/

private $_ancestor = null;
private $_descendant = null;

// (*) Unsure what these filters are or how they work
protected $filterOrder = [
'id',
Expand Down Expand Up @@ -160,7 +161,7 @@ protected function __ancestorOf($element, $value)

protected function __archived($element, $value)
{
return true; // TODO
return true; // Not applicable to Neo blocks
}

protected function __childField($element, $value)
Expand All @@ -175,7 +176,12 @@ protected function __childOf($element, $value)

protected function __collapsed($element, $value)
{
return true; // TODO
if($value == null)
{
return true;
}

return $element->collapsed == $value;
}

protected function __dateCreated($element, $value)
Expand Down Expand Up @@ -242,7 +248,12 @@ protected function __descendantDist($element, $value)

protected function __fieldId($element, $value)
{
return true; // TODO
if(!$value)
{
return true;
}

return $element->fieldId == $value;
}

protected function __fixedOrder($element, $value)
Expand All @@ -252,7 +263,12 @@ protected function __fixedOrder($element, $value)

protected function __id($element, $value)
{
return true; // TODO
if(!$value)
{
return true;
}

return $element->id == $value;
}

protected function __indexBy($element, $value)
Expand All @@ -267,12 +283,17 @@ protected function __level($element, $value)
return true;
}

return $element->level == $value;
return $element->level == $value; // TODO Support comparison operators `>=4` etc
}

protected function __limit($element, $value)
protected function __limit($element, $value, $index)
{
return true; // TODO
if(!$value)
{
return true;
}

return $index >= $value;
}

protected function __locale($element, $value)
Expand All @@ -282,7 +303,7 @@ protected function __locale($element, $value)

protected function __localeEnabled($element, $value)
{
return true; // TODO
return true; // Just return true because the blocks will already be locale filtered
}

protected function __nextSiblingOf($element, $value)
Expand Down Expand Up @@ -357,27 +378,45 @@ protected function __siblingOf($element, $value)

protected function __slug($element, $value)
{
return true; // TODO
return true; // Not applicable to Neo blocks
}

protected function __status($element, $value)
{
return true; // TODO
return true; // Not applicable to Neo blocks
}

protected function __title($element, $value)
{
return true; // TODO
return true; // Not applicable to Neo blocks
}

protected function __type($element, $value)
{
return true; // TODO
if(!$value)
{
return true;
}

$types = craft()->neo->getBlockTypesByFieldId($element->fieldId, 'handle');
$type = isset($types[$value]) ? $types[$value] : false;

return $type && $element->typeId == $type->id;
}

protected function __typeId($element, $value)
{
if(!$value)
{
return true;
}

return $element->typeId == $value;
}

protected function __uri($element, $value)
{
return true; // TODO
return true; // Not applicable to Neo blocks
}

protected function __with($element, $value)
Expand Down

0 comments on commit 76e75dd

Please sign in to comment.