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
64528499
Commit
64528499
authored
Apr 09, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add OptionField type
parent
78b31ba5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
2 deletions
+141
-2
BaseModel.php
src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php
+19
-2
OptionField.php
...e/mvc/app/models/OPNsense/Base/FieldTypes/OptionField.php
+115
-0
Sample.xml
src/opnsense/mvc/app/models/OPNsense/Sample/Sample.xml
+7
-0
No files found.
src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php
View file @
64528499
...
...
@@ -65,6 +65,24 @@ abstract class BaseModel
return
;
}
/**
* parse option data for model setter.
* @param $xmlNode
* @return array|string
*/
private
function
parseOptionData
(
$xmlNode
)
{
if
(
$xmlNode
->
count
()
==
0
)
{
$result
=
$xmlNode
->
__toString
();
}
else
{
$result
=
array
();
foreach
(
$xmlNode
->
children
()
as
$childNode
)
{
$result
[
$childNode
->
getName
()]
=
$this
->
parseOptionData
(
$childNode
);
}
}
return
$result
;
}
/**
* parse model and config xml to object model using types in FieldTypes
* @param SimpleXMLElement $xml model xml data (from items section)
...
...
@@ -106,10 +124,9 @@ abstract class BaseModel
if
(
$xmlNode
->
count
()
>
0
)
{
// if fieldtype contains properties, try to call the setters
foreach
(
$xmlNode
->
children
()
as
$fieldMethod
)
{
$param_value
=
$fieldMethod
->
__toString
()
;
$method_name
=
"set"
.
$fieldMethod
->
getName
();
if
(
$field_rfcls
->
hasMethod
(
$method_name
))
{
$fieldObject
->
$method_name
(
$
param_value
);
$fieldObject
->
$method_name
(
$
this
->
parseOptionData
(
$fieldMethod
)
);
}
}
}
...
...
src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/OptionField.php
0 → 100644
View file @
64528499
<?php
/**
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace
OPNsense\Base\FieldTypes
;
use
Phalcon\Validation\Validator\Regex
;
/**
* Class OptionField
* @package OPNsense\Base\FieldTypes
*/
class
OptionField
extends
BaseField
{
/**
* @var bool marks if this is a data node or a container
*/
protected
$internalIsContainer
=
false
;
/**
* @var array
*/
private
$internalOptionList
=
array
();
/**
* setter for option values
* @param $data
*/
public
function
setOptionValues
(
$data
)
{
if
(
is_array
(
$data
))
{
$this
->
internalOptionList
=
array
();
// copy options to internal structure, make sure we don't copy in array structures
foreach
(
$data
as
$key
=>
$value
)
{
if
(
!
is_array
(
$value
))
{
$this
->
internalOptionList
[
$key
]
=
$value
;
}
}
}
}
/**
* get valid options, descriptions and selected value
* @return array
*/
public
function
getNodeData
()
{
$result
=
array
();
foreach
(
$this
->
internalOptionList
as
$optKey
=>
$optValue
)
{
if
(
$optKey
==
$this
->
internalValue
)
{
$selected
=
0
;
}
else
{
$selected
=
1
;
}
$result
[
$optKey
]
=
array
(
"value"
=>
$optKey
,
"description"
=>
$optValue
,
"selected"
=>
$selected
);
}
return
$result
;
}
/**
* @return array returns Text/regex validator
*/
public
function
getValidators
()
{
// build validation mask
$validationMask
=
'('
;
$countid
=
0
;
foreach
(
$this
->
internalOptionList
as
$key
=>
$value
)
{
if
(
$countid
>
0
)
{
$validationMask
.=
'|'
;
}
$validationMask
.=
$key
;
$countid
++
;
}
$validationMask
.=
')'
;
if
(
$this
->
internalValidationMessage
==
null
)
{
$msg
=
"option not in list"
;
}
else
{
$msg
=
$this
->
internalValidationMessage
;
}
if
((
$this
->
internalIsRequired
==
true
||
$this
->
internalValue
!=
null
)
&&
$validationMask
!=
null
)
{
return
array
(
new
Regex
(
array
(
'message'
=>
$msg
,
'pattern'
=>
trim
(
$validationMask
))));
}
else
{
// empty field and not required, skip this validation.
return
array
();
}
}
}
\ No newline at end of file
src/opnsense/mvc/app/models/OPNsense/Sample/Sample.xml
View file @
64528499
...
...
@@ -11,6 +11,13 @@
<Mask>
/^([A-Z,1-9]){0,10}$/
</Mask>
<ValidationMessage>
test
</ValidationMessage>
</tag1>
<optionTag
type=
"OptionField"
>
<default>
1
</default>
<OptionValues>
<A>
description 1
</A>
<B>
description 2
</B>
</OptionValues>
</optionTag>
<tagX>
<tag1
type=
"EmailField"
>
<Default>
test1234
</Default>
...
...
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