Commit d04db9be authored by Ad Schellevis's avatar Ad Schellevis

(mvc) add uuid on recurring items

parent e9d3cd9f
...@@ -92,6 +92,14 @@ abstract class BaseModel ...@@ -92,6 +92,14 @@ abstract class BaseModel
*/ */
private function parseXml($xml, &$config_data, &$internal_data) private function parseXml($xml, &$config_data, &$internal_data)
{ {
// copy xml tag attributes to Field
if ($config_data != null) {
foreach($config_data->attributes() as $AttrKey => $AttrValue) {
$internal_data->setAttributeValue($AttrKey, $AttrValue);
}
}
// iterate model children
foreach ($xml->children() as $xmlNode) { foreach ($xml->children() as $xmlNode) {
$tagName = $xmlNode->getName(); $tagName = $xmlNode->getName();
// every item results in a Field type object, the first step is to determine which object to create // every item results in a Field type object, the first step is to determine which object to create
...@@ -134,7 +142,6 @@ abstract class BaseModel ...@@ -134,7 +142,6 @@ abstract class BaseModel
// set field content from config (if available) // set field content from config (if available)
$fieldObject->setValue($config_data->$tagName->__toString()); $fieldObject->setValue($config_data->$tagName->__toString());
} }
} else { } else {
// add new child node container, always try to pass config data // add new child node container, always try to pass config data
if ($config_data != null && isset($config_data->$tagName)) { if ($config_data != null && isset($config_data->$tagName)) {
......
...@@ -72,6 +72,13 @@ class ArrayField extends BaseField ...@@ -72,6 +72,13 @@ class ArrayField extends BaseField
$this->internalArrayCounter--; $this->internalArrayCounter--;
} }
} }
// check if all children have a uuid, generate one if missing
foreach ($this->internalChildnodes as $nodeKey => $node) {
if (!array_key_exists('uuid', $node->getAttributes())) {
$node->setAttributeValue("uuid", $this->generateUUID());
}
}
} }
/** /**
...@@ -98,6 +105,8 @@ class ArrayField extends BaseField ...@@ -98,6 +105,8 @@ class ArrayField extends BaseField
foreach ($new_record as $key => $node) { foreach ($new_record as $key => $node) {
$node->setInternalReference($container_node->__reference.".".$key); $node->setInternalReference($container_node->__reference.".".$key);
$container_node->addChildNode($key, $node); $container_node->addChildNode($key, $node);
// make sure we have a UUID on repeating child items
$container_node->setAttributeValue("uuid", $this->generateUUID());
} }
// add node to this object // add node to this object
...@@ -116,4 +125,23 @@ class ArrayField extends BaseField ...@@ -116,4 +125,23 @@ class ArrayField extends BaseField
unset($this->internalChildnodes[$index]); unset($this->internalChildnodes[$index]);
} }
} }
/**
* search child item by UUID
* @param $uuid item uuid
* @return BaseField|null
*/
public function findByUUID($uuid)
{
foreach ($this->internalChildnodes as $nodeKey => $node) {
$nodeAttr = $node->getAttributes();
if (array_key_exists('uuid', $nodeAttr) && $nodeAttr['uuid'] == $uuid) {
return $node;
}
}
return null;
}
} }
...@@ -80,6 +80,24 @@ abstract class BaseField ...@@ -80,6 +80,24 @@ abstract class BaseField
*/ */
protected $internalIsVirtual = false ; protected $internalIsVirtual = false ;
/**
* @var array key value store for attributes (will be saved as xml attributes)
*/
protected $internalAttributes = array();
/**
* @return string uuid v4 number
*/
protected function generateUUID() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
/** /**
* Template action for post loading actions, triggered by eventPostLoadingEvent. * Template action for post loading actions, triggered by eventPostLoadingEvent.
* Overwrite this method for custom loading hooks. * Overwrite this method for custom loading hooks.
...@@ -89,6 +107,7 @@ abstract class BaseField ...@@ -89,6 +107,7 @@ abstract class BaseField
return; return;
} }
/** /**
* trigger post loading event. (executed by BaseModel) * trigger post loading event. (executed by BaseModel)
*/ */
...@@ -218,6 +237,24 @@ abstract class BaseField ...@@ -218,6 +237,24 @@ abstract class BaseField
$this->internalValue = $value; $this->internalValue = $value;
} }
/**
* Set attribute on Field object
* @param $key attribute key
* @param $value attribute value
*/
public function setAttributeValue($key, $value)
{
$this->internalAttributes[$key] = $value;
}
/**
* @return array Field attributes
*/
public function getAttributes()
{
return $this->internalAttributes;
}
/** /**
* @return array child items * @return array child items
*/ */
...@@ -364,6 +401,12 @@ abstract class BaseField ...@@ -364,6 +401,12 @@ abstract class BaseField
$subnode = $node->addChild($this->getInternalXMLTagName()); $subnode = $node->addChild($this->getInternalXMLTagName());
} }
// copy attributes into xml node
foreach ($this->getAttributes() as $AttrKey => $AttrValue) {
$subnode->addAttribute($AttrKey, $AttrValue);
}
} }
foreach ($this->__items as $key => $FieldNode) { foreach ($this->__items as $key => $FieldNode) {
......
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