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,24 +58,45 @@ class ModelRelationField extends BaseField ...@@ -53,24 +58,45 @@ 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;
foreach ($mdlStructure as $modelData) { }
if (array_key_exists("source", $modelData)) {
$className = str_replace(".", "\\", $modelData['source']); // set internal key for this node based on sources and filter criteria
$modelObj = new $className; $this->internalCacheKey = md5(serialize($mdlStructure));
if (array_key_exists("items", $modelData)) {
foreach ($modelObj->getNodeByReference($modelData['items'])->__items as $node) { // only collect options once per source/filter combination, we use a static to save our unique option
$displayKey = $modelData['display']; // combinations over the running application.
if (array_key_exists("uuid", $node->getAttributes()) && $node->$displayKey != null) { if (!array_key_exists($this->internalCacheKey, self::$internalOptionList)) {
$uuid = $node->getAttributes()['uuid']; self::$internalOptionList[$this->internalCacheKey] = array();
self::$internalOptionList[$uuid] = $node->$displayKey->__toString(); foreach ($mdlStructure as $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']);
$modelObj = new $className;
foreach ($modelObj->getNodeByReference($modelData['items'])->__items as $node) {
$displayKey = $modelData['display'];
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'];
self::$internalOptionList[$this->internalCacheKey][$uuid] =
$node->$displayKey->__toString();
}
} }
unset($modelObj);
} }
unset($modelObj);
} }
} }
} }
...@@ -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