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:
16: namespace Cakestrap\View\Helper;
17:
18: /**
19: * Flexible plugin that utilizes a handful of
20: * classes for easy toggle behavior.
21: *
22: * <code>
23: * <?php
24: * echo $this->Cakestrap->Collapse()
25: * ->title("Menu 1")
26: * ->body("Menu 1 Content")
27: * ->prepare()
28: *
29: * ->title("Menu 2")
30: * ->body("Menu 2 Content")
31: * ->prepare()
32: *
33: * ->set()
34: * ?>
35: * </code>
36: *
37: * @method title(string $title)
38: * @method body(string $body)
39: *
40: * @package Cakestrap\View\Helper
41: * @link http://getbootstrap.com/javascript/#collapse
42: */
43: class CollapseHelper extends Basic
44: {
45: /**
46: * Additional options for this helper
47: *
48: * @var array
49: */
50: protected $_options = [];
51:
52: /**
53: * Mark the content as new set
54: *
55: * @var array
56: */
57: protected $_prepare = [];
58:
59: /**
60: * Contain active string value
61: *
62: * @var string
63: */
64: protected $_active = null;
65: protected $_in = null;
66:
67: /**
68: * Set all defined values.
69: * This will return the final html template.
70: *
71: * @return string
72: */
73: public function set()
74: {
75: if(empty($this->_prepare)) {
76: $this->_setTitle();
77: $this->_setBody();
78: }
79:
80: $this->_addToContainer();
81: return $this->_container;
82: }
83:
84: /**
85: * Prepare the template content
86: *
87: * @param boolean $isActive Set item active or inactive
88: * @return object $this
89: */
90: public function prepare($isActive = false)
91: {
92: $this->_active = ($isActive ? 'active' : null);
93: $this->_in = ($isActive ? 'in' : null);
94:
95: $this->_setTitle();
96: $this->_setBody();
97:
98: $this->_prepare[] = $this->_stringTemplate->format('prepare', ['prepare' => $this->_contents()]);
99: $this->_reset();
100:
101: return $this;
102: }
103:
104: /**
105: * Add collapse items to parent container
106: *
107: * @return void
108: */
109: protected function _addToContainer()
110: {
111: if(empty($this->_prepare)) {
112: $contents = $this->_contents();
113: $prepare = $this->_stringTemplate->format('prepare', ['prepare' => $contents]);
114: } else {
115: $prepare = implode("", $this->_prepare);
116: }
117:
118: $this->_container = $this->_stringTemplate->format('container', ['container'=>$prepare]);
119: }
120:
121: /**
122: * Create string id based on collapse title
123: *
124: * @return string $id
125: */
126: protected function _createId()
127: {
128: return $id = strtolower(str_replace(" ", "_", trim($this->title)));
129: }
130:
131: /**
132: * Set the Bootstrap collapse title
133: *
134: * @return void
135: */
136: protected function _setTitle()
137: {
138:
139: $this->_contents[] = $this->_stringTemplate->format('title', [
140: 'title' => $this->title,
141: 'id' => $this->_createId(),
142: 'href' => $this->_href(),
143: 'active' => $this->_active]);
144: }
145:
146: /**
147: * Set the Bootstrap collapse body
148: *
149: * @return void
150: */
151: protected function _setBody()
152: {
153: $this->_contents[] = $this->_stringTemplate->format('body', [
154: 'body' => $this->body,
155: 'id' => $this->_createId(),
156: 'href' => $this->_href(),
157: 'in' => $this->_in
158: ]);
159: }
160:
161: /**
162: * Set the Bootstrap collapse contents
163: *
164: * @return array $contents
165: */
166: protected function _contents()
167: {
168: $contents = implode("", $this->_contents);
169: return $contents;
170: }
171:
172: /**
173: * Set the href attribute of an element
174: *
175: * @return array $href
176: */
177: protected function _href()
178: {
179: if(is_array($this->href)) {
180: $link = [
181: 'plugin' => null,
182: 'controller' => null,
183: 'action' => null,
184: ];
185: $merged = array_merge($link, $this->href);
186: $this->href = sprintf("/%s", implode("/", $merged));
187: } else {
188: $this->href = sprintf("#collapse_%s", $this->_createId());
189: }
190:
191: return $this->href;
192: }
193: }