diff options
author | Renato Botelho <renato@netgate.com> | 2015-08-26 15:12:02 -0300 |
---|---|---|
committer | Renato Botelho <renato@netgate.com> | 2015-08-26 15:12:02 -0300 |
commit | 03b19a93f4d8d870507ee96121cee4acd748dd2a (patch) | |
tree | 71a34e9e7e73d13de21cb4ad831799fb10c30df4 /src/usr/local/www/classes | |
parent | 7f410a121522c5d0e2660256ae50c1fde1df3645 (diff) | |
parent | 30ce58ac1ea27b758d5112cb5a3b190c9760f010 (diff) | |
download | pfsense-03b19a93f4d8d870507ee96121cee4acd748dd2a.zip pfsense-03b19a93f4d8d870507ee96121cee4acd748dd2a.tar.gz |
Merge branch 'master' into bootstrap
Diffstat (limited to 'src/usr/local/www/classes')
-rw-r--r-- | src/usr/local/www/classes/Form.class.php | 126 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/Button.class.php | 68 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/Checkbox.class.php | 65 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/Element.class.php | 94 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/Group.class.php | 152 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/Input.class.php | 200 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/IpAddress.class.php | 76 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/MultiCheckbox.class.php | 40 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/MultiCheckboxGroup.class.php | 64 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/Section.class.php | 82 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/Select.class.php | 71 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/StaticText.class.php | 45 | ||||
-rw-r--r-- | src/usr/local/www/classes/Form/Textarea.class.php | 54 | ||||
-rw-r--r-- | src/usr/local/www/classes/Modal.class.php | 67 |
14 files changed, 1204 insertions, 0 deletions
diff --git a/src/usr/local/www/classes/Form.class.php b/src/usr/local/www/classes/Form.class.php new file mode 100644 index 0000000..b489a72 --- /dev/null +++ b/src/usr/local/www/classes/Form.class.php @@ -0,0 +1,126 @@ +<?php +/* + Form.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ + +require_once('classes/Form/Element.class.php'); +require_once('classes/Form/Input.class.php'); +foreach (glob('classes/Form/*.class.php') as $file) + require_once($file); + +class Form extends Form_Element +{ + const LABEL_WIDTH = 2; + const MAX_INPUT_WIDTH = 10; + protected $_tagName = 'form'; + protected $_attributes = array( + 'class' => array('form-horizontal' => true), + 'method' => 'post', + // Empty is interpreted by all browsers to submit to the current URI + 'action' => '', + ); + protected $_sections = array(); + protected $_global = array(); + + public function __construct($submit = null) + { + if (!isset($submit)) + $submit = 'Save'; + + if (gettype($submit) == 'string') + $submit = new Form_Button( + 'save', + $submit + ); + + if (false !== $submit) + $this->addGlobal($submit); + } + + public function add(Form_Section $section) + { + array_push($this->_sections, $section); + $section->_setParent($this); + + return $section; + } + + public function setAction($url) + { + $this->_attributes['action'] = $url; + + return $this; + } + + public function addGlobal(Form_Input $input) + { + array_push($this->_global, $input); + + return $input; + } + + public function setMultipartEncoding() + { + $this->_attributes['enctype'] = 'multipart/form-data'; + + return $this; + } + + protected function _setParent() + { + throw new Exception('Form does not have a parent'); + } + + public function __toString() + { + $element = parent::__toString(); + $html = implode('', $this->_sections); + $buttons = ''; + + foreach ($this->_global as $global) + { + if ($global instanceof Form_Button) + $buttons .= $global; + else + $html .= $global; + } + + if (!empty($buttons)) + { + $group = new Form_Element; + $group->addClass('col-sm-'. Form::MAX_INPUT_WIDTH, 'col-sm-offset-'. Form::LABEL_WIDTH); + + $html .= $group . $buttons .'</div>'; + } + + return <<<EOT + {$element} + {$html} + </form> +EOT; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/Button.class.php b/src/usr/local/www/classes/Form/Button.class.php new file mode 100644 index 0000000..32876a7 --- /dev/null +++ b/src/usr/local/www/classes/Form/Button.class.php @@ -0,0 +1,68 @@ +<?php +/* + Button.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ +class Form_Button extends Form_Input +{ + protected $_tagSelfClosing = false; + protected $_attributes = array( + 'class' => array( + 'btn' => true, + ), + 'type' => 'submit', + ); + + public function __construct($name, $title, $link = null) + { + // If we have a link; we're actually an <a class='btn'> + if (isset($link)) + { + $this->_attributes['href'] = $link; + $this->_tagName = 'a'; + $this->addClass('btn-default'); + unset($this->_attributes['type']); + } + else + { + $this->_tagSelfClosing = true; + $this->_attributes['value'] = $title; + $this->addClass('btn-primary'); + } + + parent::__construct($name, $title, null); + } + + protected function _getInput() + { + $input = parent::_getInput(); + + if (!isset($this->_attributes['href'])) + return $input; + + return $input . htmlspecialchars($this->_title) .'</a>'; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/Checkbox.class.php b/src/usr/local/www/classes/Form/Checkbox.class.php new file mode 100644 index 0000000..e0734de --- /dev/null +++ b/src/usr/local/www/classes/Form/Checkbox.class.php @@ -0,0 +1,65 @@ +<?php +/* + Checkbox.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ + +class Form_Checkbox extends Form_Input +{ + protected $_attributes = array( + 'class' => array(), + ); + protected $_description; + + public function __construct($name, $title, $description, $checked, $value = 'yes') + { + parent::__construct($name, $title, 'checkbox', $value); + + $this->_description = $description; + + if ($checked) + $this->_attributes['checked'] = 'checked'; + + $this->column->addClass('checkbox'); + } + + public function displayAsRadio() + { + $this->_attributes['type'] = 'radio'; + + return $this; + } + + protected function _getInput() + { + $input = parent::_getInput(); + + if (!isset($this->_description)) + return $input; + + return '<label>'. $input .' '. htmlspecialchars(gettext($this->_description)) .'</label>'; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/Element.class.php b/src/usr/local/www/classes/Form/Element.class.php new file mode 100644 index 0000000..d3ad142 --- /dev/null +++ b/src/usr/local/www/classes/Form/Element.class.php @@ -0,0 +1,94 @@ +<?php +/* + Element.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ + +class Form_Element +{ + protected $_tagName; + protected $_tagSelfClosing; + protected $_attributes; + protected $_parent; + + public function __construct($tagName = 'div', $selfClose = false, $attributes = array('class' => array())) + { + $this->_tagName = $tagName; + $this->_tagSelfClosing = $selfClose; + $this->_attributes = $attributes; + } + + public function addClass() + { + foreach (func_get_args() as $class) + $this->_attributes['class'][$class] = true; + + return $this; + } + + public function removeClass($class) + { + unset($this->_attributes['class'][$class]); + + return $this; + } + + public function setAttribute($key, $value = null) + { + $this->_attributes[ $key ] = $value; + return $this; + } + + public function __toString() + { + $attributes = ''; + foreach ($this->_attributes as $key => $value) + { + if (is_array($value)) + { + // Used for classes. If it's empty, we don't want the attribute at all + if (!empty($value)) + $value = implode(' ', array_keys($value)); + else + $value = null; + } + + if ($value === null) + continue; + + $attributes .= ' '. $key; + if ($value !== true) + $attributes .= '="' . htmlspecialchars($value) . '"'; + } + + return '<'. $this->_tagName . $attributes . ($this->_tagSelfClosing ? '/' : '') .'>'; + } + + protected function _setParent(Form_Element $parent) + { + $this->_parent = $parent; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/Group.class.php b/src/usr/local/www/classes/Form/Group.class.php new file mode 100644 index 0000000..4ddf70d --- /dev/null +++ b/src/usr/local/www/classes/Form/Group.class.php @@ -0,0 +1,152 @@ +<?php +/* + Group.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ +class Form_Group extends Form_Element +{ + protected $_tagName = 'div'; + protected $_attributes = array( + 'class' => array('form-group' => true), + ); + protected $_title; + protected $_inputs = array(); + protected $_labelTarget; + protected $_help; + protected $_helpParams = array(); + + public function __construct($title) + { + $this->_title = $title; + } + + public function add(Form_Input $input) + { + array_push($this->_inputs, $input); + $input->_setParent($this); + + // Defaults to first input + if (!isset($this->_labelTarget)) + $this->_labelTarget = $input; + + return $input; + } + + public function setLabelTarget(Form_Input $input) + { + $this->_labelTarget = $input; + } + + public function setHelp($help, array $params = array()) + { + $this->_help = $help; + $this->_helpParams = $params; + + return $this; + } + + public function enableDuplication($max = null, $horiz = false) + { + if($horiz) + $this->addClass('user-duplication-horiz'); // added buttons are 2 cols wide with no offset + else + $this->addClass('user-duplication'); // added buttons 10 cols wide with 2 col offset + + if (isset($max)) + $this->_attributes('data-duplicate-max', $max); + + foreach ($this->_inputs as $input) + $input->setIsRepeated(); + + return $this; + } + + protected function _getHelp() + { + if (!isset($this->_help)) + return null; + + $group = new Form_Element; + $group->addClass('col-sm-'. Form::MAX_INPUT_WIDTH, 'col-sm-offset-'. Form::LABEL_WIDTH); + + $help = gettext($this->_help); + + if (!empty($this->_helpParams)) + $help = call_user_func_array('sprintf', array_merge([$help], $this->_helpParams)); + + return <<<EOT + {$group} + <span class="help-block"> + {$help} + </span> + </div> +EOT; + } + + public function __toString() + { + $element = parent::__toString(); + + // Automatically determine width for inputs without explicit set + $spaceLeft = Form::MAX_INPUT_WIDTH; + $missingWidth = array(); + + foreach ($this->_inputs as $input) + { + if (count($this->_inputs) > 1 && !$input->hasAttribute('placeholder')) + $input->setPlaceholder($input->getTitle()); + + $width = $input->getWidth(); + + if (isset($width)) + $spaceLeft -= $width; + else + array_push($missingWidth, $input); + } + + foreach ($missingWidth as $input) + $input->setWidth($spaceLeft / count($missingWidth)); + + $target = $this->_labelTarget->getId(); + $inputs = implode('', $this->_inputs); + $help = $this->_getHelp(); + + $label = new Form_Element('label', false, ['for' => $target]); + $label->addClass('col-sm-'.Form::LABEL_WIDTH, 'control-label'); + + $title = htmlspecialchars(gettext($this->_title)); + + return <<<EOT + {$element} + {$label} + {$title} + </label> + {$inputs} + {$help} + </div> +EOT; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/Input.class.php b/src/usr/local/www/classes/Form/Input.class.php new file mode 100644 index 0000000..ec1cdca --- /dev/null +++ b/src/usr/local/www/classes/Form/Input.class.php @@ -0,0 +1,200 @@ +<?php +/* + Input.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ +class Form_Input extends Form_Element +{ + public $column; + protected $_tagName = 'input'; + protected $_tagSelfClosing = true; + protected $_attributes = array( + 'class' => array('form-control' => true), + 'name' => null, + 'id' => null, + 'title' => null, + ); + protected $_title; + protected $_help; + protected $_helpParams = array(); + protected $_columnWidth; + + public function __construct($name, $title, $type = 'text', $value = null, array $attributes = array()) + { + $this->column = new Form_Element; + + $this->_attributes['name'] = $name; + $this->_attributes['id'] = $name; + $this->_title = $title; + + if (isset($type)) + $this->_attributes['type'] = $type; + + switch ($type) + { + case 'number': + $attributes += array('min' => 1, 'step' => 1); + break; + case 'file': + unset($this->_attributes['class']['form-control']); + break; + } + + if (isset($value)) + $this->_attributes['value'] = $value; + + foreach ($attributes as $name => $value) + $this->_attributes[$name] = $value; + } + + public function getTitle() + { + return $this->_title; + } + + public function getName() + { + return $this->_attributes['name']; + } + + public function getId() + { + return $this->_attributes['id']; + } + + public function setHelp($help, array $params = array()) + { + $this->_help = $help; + $this->_helpParams = $params; + + return $this; + } + + public function getWidth() + { + return $this->_columnWidth; + } + + public function setWidth($size) + { + if ($size < 1 || $size > Form::MAX_INPUT_WIDTH) + throw new Exception('Incorrect size, pass a number between 1 and '.Form::MAX_INPUT_WIDTH); + + $this->column->removeClass('col-sm-'. $this->_columnWidth); + + $this->_columnWidth = (int)$size; + + $this->column->addClass('col-sm-'. $this->_columnWidth); + + return $this; + } + + public function setReadonly() + { + $this->_attributes['readonly'] = 'readonly'; + + return $this; + } + + public function setDisabled() + { + $this->_attributes['disabled'] = 'disabled'; + + return $this; + } + + public function toggles($selector = null, $type = 'collapse') + { + if (isset($selector)) + $this->_attributes['data-target'] = $selector; + + $this->_attributes['data-toggle'] = $type; + + return $this; + } + + public function setPattern($regexp) + { + $this->_attributes['pattern'] = $regexp; + + return $this; + } + + public function setPlaceholder($text) + { + $this->_attributes['placeholder'] = $text; + + return $this; + } + + public function hasAttribute($name) + { + // not strict, null should return false as well + return isset($this->_attributes[$name]); + } + + public function setIsRepeated() + { + $this->_attributes['name'] .= '[]'; + // No I don't like this. Yes it works fine + $this->_attributes['id'] .= ':'.substr(uniqid(), 9); + + return $this; + } + + protected function _getInput() + { + return parent::__toString(); + } + + public function __toString() + { + $input = $this->_getInput(); + $column = (string)$this->column; + + // Don't add an empty <div>, skip it instead + if (!isset($this->_help) && '<div>' == $column) + return (string)$input; + + if (isset($this->_help)) + { + $help = gettext($this->_help); + + if (!empty($this->_helpParams)) + $help = call_user_func_array('sprintf', array_merge([$help], $this->_helpParams)); + + $help = '<span class="help-block">'. $help .'</span>'; + } + + return <<<EOT + {$column} + {$input} + + {$help} + </div> +EOT; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/IpAddress.class.php b/src/usr/local/www/classes/Form/IpAddress.class.php new file mode 100644 index 0000000..759b65d --- /dev/null +++ b/src/usr/local/www/classes/Form/IpAddress.class.php @@ -0,0 +1,76 @@ +<?php +/* + IpAddress.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ + +class Form_IpAddress extends Form_Input +{ + protected $_mask; + + public function __construct($name, $title, $value) + { + parent::__construct($name, $title, 'text', $value); + + $this->_attributes['pattern'] = '[a-f0-9:.]*'; + } + + public function addMask($name, $value, $max = 128) + { + $this->_mask = new Form_Select( + $name, + null, + $value, + array_combine(range($max, 1), range($max, 1)) + ); + + return $this; + } + + public function setIsRepeated() + { + if (isset($this->_mask)) + $this->_mask->setIsRepeated(); + + return parent::setIsRepeated(); + } + + protected function _getInput() + { + $input = parent::_getInput(); + + if (!isset($this->_mask)) + return $input; + + return <<<EOT + <div class="input-group"> + $input + <span class="input-group-addon input-group-inbetween pfIpMask">/</span> + {$this->_mask} + </div> +EOT; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/MultiCheckbox.class.php b/src/usr/local/www/classes/Form/MultiCheckbox.class.php new file mode 100644 index 0000000..9310977 --- /dev/null +++ b/src/usr/local/www/classes/Form/MultiCheckbox.class.php @@ -0,0 +1,40 @@ +<?php +/* + Form_MultiCheckbox.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ +class Form_MultiCheckbox extends Form_Checkbox +{ + public function setHelp($help, array $params = array()) + { + throw new Exception('MultiCheckboxes do not support help-texts, please use $group->setHelp instead'); + } + + public function __toString() + { + return (string)$this->_getInput(); + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/MultiCheckboxGroup.class.php b/src/usr/local/www/classes/Form/MultiCheckboxGroup.class.php new file mode 100644 index 0000000..c04bf07 --- /dev/null +++ b/src/usr/local/www/classes/Form/MultiCheckboxGroup.class.php @@ -0,0 +1,64 @@ +<?php +/* + Form_MultiCheckboxGroup.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ +class Form_MultiCheckboxGroup extends Form_Group +{ + public function add(Form_MultiCheckbox $input) + { + return parent::add($input); + } + + public function __toString() + { + $element = Form_Element::__toString(); + $column = new Form_Element; + $column->addClass('checkbox', 'multi', 'col-sm-10'); + + $inputs = implode('', $this->_inputs); + $help = $this->_getHelp(); + + $label = new Form_Element('label'); + $label->addClass('col-sm-'.Form::LABEL_WIDTH, 'control-label'); + + $title = htmlspecialchars(gettext($this->_title)); + + return <<<EOT + {$element} + {$label} + {$title} + </label> + + {$column} + {$inputs} + </div> + + {$help} + </div> +EOT; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/Section.class.php b/src/usr/local/www/classes/Form/Section.class.php new file mode 100644 index 0000000..b1dfb22 --- /dev/null +++ b/src/usr/local/www/classes/Form/Section.class.php @@ -0,0 +1,82 @@ +<?php +/* + Section.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ +class Form_Section extends Form_Element +{ + protected $_tagName = 'div'; + protected $_attributes = array( + 'class' => array( + 'panel' => true, + 'panel-default' => true, + ), + ); + protected $_title; + protected $_groups = array(); + + public function __construct($title) + { + $this->_title = $title; + } + + public function add(Form_Group $group) + { + array_push($this->_groups, $group); + $group->_setParent($this); + + return $group; + } + + // Shortcut, adds a group for the specified input + public function addInput(Form_Input $input) + { + $group = new Form_Group($input->getTitle()); + $group->add($input); + + $this->add($group); + + return $input; + } + + public function __toString() + { + $element = parent::__toString(); + $title = htmlspecialchars(gettext($this->_title)); + $body = implode('', $this->_groups); + + return <<<EOT + {$element} + <div class="panel-heading"> + <h2 class="panel-title">{$title}</h2> + </div> + <div class="panel-body"> + {$body} + </div> + </div> +EOT; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/Select.class.php b/src/usr/local/www/classes/Form/Select.class.php new file mode 100644 index 0000000..1c4594d --- /dev/null +++ b/src/usr/local/www/classes/Form/Select.class.php @@ -0,0 +1,71 @@ +<?php +/* + Select.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ + +class Form_Select extends Form_Input +{ + protected $_tagName = 'select'; + protected $_values; + protected $_value; + + public function __construct($name, $title, $value, array $values, $allowMultiple = false) + { + if ($allowMultiple) + $name .= '[]'; + + parent::__construct($name, $title, null); + + if ($allowMultiple) + $this->_attributes['multiple'] = 'multiple'; + + $this->_value = $value; + $this->_values = $values; + } + + protected function _getInput() + { + $element = parent::_getInput(); + + $options = ''; + foreach ($this->_values as $value => $name) + { + if (isset($this->_attributes['multiple'])) + $selected = in_array($value, (array)$this->_value); + else + $selected = ($this->_value == $value); + + $options .= '<option value="'. htmlspecialchars($value) .'"'.($selected ? ' selected' : '').'>'. htmlspecialchars(gettext($name)) .'</option>'; + } + + return <<<EOT + {$element} + {$options} + </select> +EOT; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/StaticText.class.php b/src/usr/local/www/classes/Form/StaticText.class.php new file mode 100644 index 0000000..b6f7983 --- /dev/null +++ b/src/usr/local/www/classes/Form/StaticText.class.php @@ -0,0 +1,45 @@ +<?php +/* + StaticText.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ + +class Form_StaticText extends Form_Input +{ + protected $_text; + + public function __construct($title, $text) + { + parent::__construct(null, $title); + + $this->_text = $text; + } + + protected function _getInput() + { + return $this->_text; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Form/Textarea.class.php b/src/usr/local/www/classes/Form/Textarea.class.php new file mode 100644 index 0000000..8c1c157 --- /dev/null +++ b/src/usr/local/www/classes/Form/Textarea.class.php @@ -0,0 +1,54 @@ +<?php +/* + Textarea.class.php + + Copyright (C) 2015 Sjon Hortensius + 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. +*/ +class Form_Textarea extends Form_Input +{ + protected $_tagName = 'textarea'; + protected $_value; + protected $_attributes = array( + 'rows' => 5, + 'class' => array('form-control' => true), + ); + + public function __construct($name, $title, $value) + { + parent::__construct($name, $title, null); + + $this->_value = $value; + } + + protected function _getInput() + { + $element = parent::_getInput(); + $value = htmlspecialchars($this->_value); + + return <<<EOT + {$element}{$value}</textarea> +EOT; + } +}
\ No newline at end of file diff --git a/src/usr/local/www/classes/Modal.class.php b/src/usr/local/www/classes/Modal.class.php new file mode 100644 index 0000000..e77c608 --- /dev/null +++ b/src/usr/local/www/classes/Modal.class.php @@ -0,0 +1,67 @@ +<?php + +require_once('classes/Form.class.php'); + +class Modal extends Form_Section +{ + protected $_attributes = array( + 'id' => null, + 'class' => array( + 'modal' => true, + 'fade' => true, + ), + 'role' => 'dialog', + 'aria-labelledby' => null, + 'aria-hidden' => 'true', + ); + protected $_global = array(); + protected $_isLarge; + + public function __construct($title, $id, $isLarge = false, $submit = null) + { + $this->_title = $title; + $this->_attributes['id'] = $this->_attributes['aria-labelledby'] = $id; + $this->_isLarge = $isLarge; + + if (gettype($submit) == 'string') + $submit = (new Form_Button( + 'save', + $submit + ))->setAttribute('data-dismiss', 'modal'); + + if (false !== $submit) + array_push($this->_global, $submit); + } + + public function __toString() + { + $element = Form_Element::__toString(); + $title = htmlspecialchars(gettext($this->_title)); + $body = implode('', $this->_groups); + $footer = implode('', $this->_global); + $modalClass = $this->_isLarge ? 'modal-lg' : 'modal-sm'; + + return <<<EOT + {$element} + <div class="modal-dialog {$modalClass}"> + <div class="modal-content"> + <div class="modal-header"> + <button type="button" class="close" data-dismiss="modal" aria-label="Close"> + <span aria-hidden="true">×</span> + </button> + <h3 class="modal-title">{$title}</h3> + </div> + <form class="form-horizontal" action="" method="post"> + <div class="modal-body"> + {$html} + </div> + <div class="modal-footer"> + {$footer} + </div> + </form> + </div> + </div> + </div> +EOT; + } +}
\ No newline at end of file |