Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
d04db9be
Commit
d04db9be
authored
May 12, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(mvc) add uuid on recurring items
parent
e9d3cd9f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
1 deletion
+79
-1
BaseModel.php
src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php
+8
-1
ArrayField.php
...se/mvc/app/models/OPNsense/Base/FieldTypes/ArrayField.php
+28
-0
BaseField.php
...nse/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php
+43
-0
No files found.
src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php
View file @
d04db9be
...
...
@@ -92,6 +92,14 @@ abstract class BaseModel
*/
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
)
{
$tagName
=
$xmlNode
->
getName
();
// 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
// set field content from config (if available)
$fieldObject
->
setValue
(
$config_data
->
$tagName
->
__toString
());
}
}
else
{
// add new child node container, always try to pass config data
if
(
$config_data
!=
null
&&
isset
(
$config_data
->
$tagName
))
{
...
...
src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ArrayField.php
View file @
d04db9be
...
...
@@ -72,6 +72,13 @@ class ArrayField extends BaseField
$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
foreach
(
$new_record
as
$key
=>
$node
)
{
$node
->
setInternalReference
(
$container_node
->
__reference
.
"."
.
$key
);
$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
...
...
@@ -116,4 +125,23 @@ class ArrayField extends BaseField
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
;
}
}
src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php
View file @
d04db9be
...
...
@@ -80,6 +80,24 @@ abstract class BaseField
*/
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.
* Overwrite this method for custom loading hooks.
...
...
@@ -89,6 +107,7 @@ abstract class BaseField
return
;
}
/**
* trigger post loading event. (executed by BaseModel)
*/
...
...
@@ -218,6 +237,24 @@ abstract class BaseField
$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
*/
...
...
@@ -364,6 +401,12 @@ abstract class BaseField
$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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment