1: <?php
 2: /*
 3:  You may not change or alter any portion of this comment or credits
 4:  of supporting developers from this source code or any supporting source code
 5:  which is considered copyrighted (c) material of the original comment or credit authors.
 6: 
 7:  This program is distributed in the hope that it will be useful,
 8:  but WITHOUT ANY WARRANTY; without even the implied warranty of
 9:  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11: 
12: namespace Xoops\Form;
13: 
14: /**
15:  * ButtonTray - button tray form element
16:  *
17:  * @category  Xoops\Form\ButtonTray
18:  * @package   Xoops\Form
19:  * @author    John Neill <catzwolf@xoops.org>
20:  * @copyright 2012-2015 XOOPS Project (http://xoops.org)
21:  * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22:  * @link      http://xoops.org
23:  */
24: class ButtonTray extends Element
25: {
26:     /**
27:      * Create a tray of buttons - delete (optional,) cancel, reset, and a primary button, i.e. submit
28:      *
29:      * @param string|array $name       name or array of all attributes
30:      *                                  Control attributes:
31:      *                                      :showdelete true to show delete button
32:      * @param string       $value      value
33:      * @param string       $type       Type for primary button. This could be either "button", "submit", or "reset"
34:      * @param string       $onclick    setExtra() javascript code for primary button. If using the array
35:      *                                  of attributes invocation, set event handler(s) using attributes
36:      * @param boolean      $showDelete show delete button, includes javascript confirmation dialog
37:      */
38:     public function __construct($name, $value = '', $type = '', $onclick = '', $showDelete = false)
39:     {
40:         if (is_array($name)) {
41:             parent::__construct($name);
42:         } else {
43:             parent::__construct([]);
44:             $this->setWithDefaults('name', $name, 'name_error');
45:             $this->set('value', $value);
46:             $this->setWithDefaults('type', $type, 'submit', ['button', 'submit', 'reset']);
47:             $this->setWithDefaults(':showdelete', $showDelete, false, [true, false]);
48:             if (!empty($onclick)) {
49:                 $this->setExtra($onclick);
50:             }
51:         }
52:     }
53: 
54:     /**
55:      * getType
56:      *
57:      * @return string type
58:      */
59:     public function getType()
60:     {
61:         return (string) $this->get('type', '');
62:     }
63: 
64:     /**
65:      * render
66:      *
67:      * @return string rendered button tray
68:      */
69:     public function render()
70:     {
71:         $ret = '';
72:         $this->add('class', 'btn');
73:         $class = 'class="' . $this->getClass() . '"';
74: 
75:         $attributes = $this->renderAttributeString();
76: 
77:         if ((bool) $this->get(':showdelete', false)) {
78:             $ret .= '<input type="submit"' . $class . ' name="delete" id="delete" value="'
79:                 . \XoopsLocale::A_DELETE . '" onclick="this.form.elements.op.value=\'delete\'">';
80:         }
81:         $ret .= ' <input type="button" ' . $class . ' value="' . \XoopsLocale::A_CANCEL
82:             . '" onclick="history.go(-1);return true;" />'
83:             . ' <input type="reset"' . $class . ' name="reset"  id="reset" value="' . \XoopsLocale::A_RESET . '" />'
84:             . ' <input ' . $attributes . $this->getExtra() . ' />';
85:         return $ret;
86:     }
87: }
88: