Commit afef1d9c authored by Ad Schellevis's avatar Ad Schellevis

(mvc) add filters to model relations

parent d5b9c78b
...@@ -45,7 +45,12 @@ class ModelRelationField extends BaseField ...@@ -45,7 +45,12 @@ class ModelRelationField extends BaseField
/** /**
* @var array collected options * @var array collected options
*/ */
private static $internalOptionList = null; private static $internalOptionList = array();
/**
* @var string cache relations
*/
private $internalCacheKey = "";
/** /**
* Set model as reference list, use uuid's as key * Set model as reference list, use uuid's as key
...@@ -53,19 +58,41 @@ class ModelRelationField extends BaseField ...@@ -53,19 +58,41 @@ class ModelRelationField extends BaseField
*/ */
public function setModel($mdlStructure) public function setModel($mdlStructure)
{ {
if (!is_array(self::$internalOptionList)) { // only handle array type input
self::$internalOptionList = array(); if (!is_array($mdlStructure)) {
if (is_array($mdlStructure)) { return;
}
// set internal key for this node based on sources and filter criteria
$this->internalCacheKey = md5(serialize($mdlStructure));
// only collect options once per source/filter combination, we use a static to save our unique option
// combinations over the running application.
if (!array_key_exists($this->internalCacheKey, self::$internalOptionList)) {
self::$internalOptionList[$this->internalCacheKey] = array();
foreach ($mdlStructure as $modelData) { foreach ($mdlStructure as $modelData) {
if (array_key_exists("source", $modelData)) { // only handle valid model sources
if (array_key_exists('source', $modelData) && array_key_exists('items', $modelData) &&
array_key_exists('display', $modelData)) {
$className = str_replace(".", "\\", $modelData['source']); $className = str_replace(".", "\\", $modelData['source']);
$modelObj = new $className; $modelObj = new $className;
if (array_key_exists("items", $modelData)) {
foreach ($modelObj->getNodeByReference($modelData['items'])->__items as $node) { foreach ($modelObj->getNodeByReference($modelData['items'])->__items as $node) {
$displayKey = $modelData['display']; $displayKey = $modelData['display'];
if (array_key_exists("uuid", $node->getAttributes()) && $node->$displayKey != null) { if (array_key_exists("uuid", $node->getAttributes()) && $node->$displayKey != null) {
// check for filters and apply if found
$isMatched = true;
if (array_key_exists("filters", $modelData)) {
foreach ($modelData['filters'] as $filterKey => $filterValue) {
$fieldData = $node->$filterKey;
if (!preg_match($filterValue, $fieldData) && $fieldData != null) {
$isMatched = false;
}
}
}
if ($isMatched) {
$uuid = $node->getAttributes()['uuid']; $uuid = $node->getAttributes()['uuid'];
self::$internalOptionList[$uuid] = $node->$displayKey->__toString(); self::$internalOptionList[$this->internalCacheKey][$uuid] =
$node->$displayKey->__toString();
} }
} }
} }
...@@ -74,7 +101,6 @@ class ModelRelationField extends BaseField ...@@ -74,7 +101,6 @@ class ModelRelationField extends BaseField
} }
} }
} }
}
/** /**
* get valid options, descriptions and selected value * get valid options, descriptions and selected value
...@@ -83,8 +109,9 @@ class ModelRelationField extends BaseField ...@@ -83,8 +109,9 @@ class ModelRelationField extends BaseField
public function getNodeData() public function getNodeData()
{ {
$result = array (); $result = array ();
if (is_array(self::$internalOptionList)) { if (array_key_exists($this->internalCacheKey, self::$internalOptionList) &&
foreach (self::$internalOptionList as $optKey => $optValue) { is_array(self::$internalOptionList[$this->internalCacheKey])) {
foreach (self::$internalOptionList[$this->internalCacheKey] as $optKey => $optValue) {
if ($optKey == $this->internalValue && $this->internalValue != null) { if ($optKey == $this->internalValue && $this->internalValue != null) {
$selected = 1; $selected = 1;
} else { } else {
...@@ -108,10 +135,16 @@ class ModelRelationField extends BaseField ...@@ -108,10 +135,16 @@ class ModelRelationField extends BaseField
$msg = $this->internalValidationMessage; $msg = $this->internalValidationMessage;
} }
if (($this->internalIsRequired == true || $this->internalValue != null) && if (($this->internalIsRequired == true || $this->internalValue != null)
count(self::$internalOptionList) > 0
) { ) {
return array(new InclusionIn(array('message' => $msg, 'domain' => array_keys(self::$internalOptionList)))); if (array_key_exists($this->internalCacheKey, self::$internalOptionList) &&
count(self::$internalOptionList[$this->internalCacheKey]) > 0) {
return array(new InclusionIn(array('message' => $msg,
'domain' => array_keys(self::$internalOptionList[$this->internalCacheKey]))));
} else {
return array(new InclusionIn(array('message' => $msg,
'domain' => array())));
}
} else { } else {
// empty field and not required, skip this validation. // empty field and not required, skip this validation.
return array(); return array();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment