1: <?php
2: /**
3: * Copyright (c) CMNWorks
4: *
5: * Licensed under The MIT License
6: * For full copyright and license information, please see the LICENSE.txt
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * @copyright Copyright (c) CMNWorks Christopher M. Natan
10: * @author Christopher M. Natan
11: * @link http://cmnworks.com
12: * @since 1.8.8
13: * @license http://www.opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cakestrap\View\Helper;
16:
17: /**
18: * Modals are streamlined, but flexible, dialog prompts
19: * with the minimum required functionality and smart defaults.
20: *
21: * <code>
22: * <?php
23: * echo $this->Cakestrap->Modal()->header('Alert')
24: * ->options(['show' => true])
25: * ->body("This modal needs your attention, but it's not super important.")
26: * ->footer('I am footer.')
27: * ->set();
28: * ?>
29: * </code>
30: *
31: * @method header(string $header)
32: * @method body(arrayOrstring $body)
33: * @method footer(string $footer)
34: *
35: * @package Cakestrap\View\Helper
36: * @link http://getbootstrap.com/javascript/#modals
37: */
38: class ModalHelper extends Basic
39: {
40: /**
41: * Contains parent html tag
42: *
43: * @var string
44: */
45: protected $_container = [];
46:
47: /**
48: * An array of valid content for this helper.
49: *
50: * @var array
51: */
52: protected $_contents = [];
53:
54: /**
55: * Default modal options.
56: * These options are merged with the user-provided options.
57: *
58: * -width : You can use either modal-md, modal-lg, modal-sm
59: *
60: * -show : Show the modal on page load.
61: *
62: * -closable : Enable/disable the close(x) options
63: *
64: * @var array
65: */
66: protected $_options = [
67: 'show' => false,
68: 'width' => 'modal-md',
69: 'id' => '',
70: 'closable'=> false,
71: 'class' => ''
72: ];
73:
74: /**
75: * A variable that cloned the default $_options.
76: *
77: * @var array
78: */
79: protected $_clone;
80:
81: /**
82: * This method will merge the default modal helper
83: * options and user-provided options.
84: *
85: * @param array $options
86: * @return object $this
87: */
88: public function options($options = [])
89: {
90: parent::options($options);
91:
92: $this->_options['visibility'] = 'fade out';
93: $this->_options['id'] = 'id_' . mt_rand(1,12345);
94:
95: if((int)$this->_options['show'] == 1) {
96: $this->_options['visibility'] = 'fade in';
97: }
98:
99: return $this;
100: }
101:
102: /**
103: * Set all the defined values.
104: * This will return the final html template.
105: *
106: * @return string
107: */
108: public function set()
109: {
110: $this->_setHeader();
111: $this->_setBody();
112: $this->_setFooter();
113:
114: $this->_addToContainer();
115: $this->_reset();
116:
117: return $this->_container;
118: }
119:
120: /**
121: * Add the modal html template to parent container.
122: *
123: * @return void
124: */
125: protected function _addToContainer()
126: {
127: $container = array_merge(['container' => $this->_contents()], $this->_options);
128: $this->_container = $this->_stringTemplate->format('container', $container);
129: }
130:
131: /**
132: * Set the modal header
133: *
134: * @return void
135: */
136: protected function _setHeader()
137: {
138: if(!$this->header) return null;
139:
140: $closable = '';
141: if($this->_options['closable'] == true) {
142: $closable = $this->_template['closable'];
143: }
144:
145: $this->_contents[] = $this->_stringTemplate->format('header', [
146: 'header' => $this->header,
147: 'closable' => $closable
148: ]);
149: }
150:
151: /**
152: * Set the modal body
153: *
154: * @return void
155: */
156: protected function _setBody()
157: {
158: if(!$this->body) return null;
159: $this->_contents[] = $this->_stringTemplate->format('body', ['body'=>$this->body]);
160: }
161:
162: /**
163: * Set the modal footer
164: *
165: * @return void
166: */
167: protected function _setFooter()
168: {
169: if(!$this->footer) return null;
170: $this->_contents[] = $this->_stringTemplate->format('footer', ['footer'=>$this->footer]);
171: }
172:
173: /**
174: * And finally set all the html contents
175: *
176: * @return string $content
177: */
178: protected function _contents()
179: {
180: $contents = implode("", $this->_contents);
181: return $contents;
182: }
183: }