1 <?php
2 /**
3 * @copyright Copyright © 2amigOS! Consulting Group 2013-
4 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
5 * @package foundation.components
6 * @version 1.0.0
7 */
8
9 namespace foundation\helpers;
10
11 /**
12 * Html holds those modified methods from CHtml required for Foundation helpers and/or widgets.
13 *
14 * @author Antonio Ramirez <amigo.cobos@gmail.com>
15 * @package foundation\helpers
16 */
17 class Html
18 {
19 /**
20 * @var integer the counter for generating automatic input field names.
21 */
22 public static $count = 0;
23 /**
24 * @var int $_counter private widget counter
25 */
26 private static $_counter = 0;
27 /**
28 * Sets the default style for attaching jQuery event handlers.
29 *
30 * If set to true (default), event handlers are delegated.
31 * Event handlers are attached to the document body and can process events
32 * from descendant elements that are added to the document at a later time.
33 *
34 * If set to false, event handlers are directly bound.
35 * Event handlers are attached directly to the DOM element, that must already exist
36 * on the page. Elements injected into the page at a later time will not be processed.
37 *
38 * You can override this setting for a particular element by setting the htmlOptions delegate attribute
39 * (see {@link clientChange}).
40 *
41 * For more information about attaching jQuery event handler see {@link http://api.jquery.com/on/}
42 * @see http://www.yiiframework.com/doc/api/1.1/CHtml/#clientChange-detail
43 * @see clientChange
44 */
45 public static $liveEvents = true;
46
47
48 /**
49 * Generates the JavaScript with the specified client changes.
50 * @param string $event event name (without 'on')
51 * @param array $htmlOptions HTML attributes which may contain the following special attributes
52 * specifying the client change behaviors:
53 * <ul>
54 * <li>submit: string, specifies the URL to submit to. If the current element has a parent form, that form will be
55 * submitted, and if 'submit' is non-empty its value will replace the form's URL. If there is no parent form the
56 * data listed in 'params' will be submitted instead (via POST method), to the URL in 'submit' or the currently
57 * requested URL if 'submit' is empty. Please note that if the 'csrf' setting is true, the CSRF token will be
58 * included in the params too.</li>
59 * <li>params: array, name-value pairs that should be submitted together with the form. This is only used when 'submit' option is specified.</li>
60 * <li>csrf: boolean, whether a CSRF token should be automatically included in 'params' when {@link CHttpRequest::enableCsrfValidation} is true. Defaults to false.
61 * You may want to set this to be true if there is no enclosing form around this element.
62 * This option is meaningful only when 'submit' option is set.</li>
63 * <li>return: boolean, the return value of the javascript. Defaults to false, meaning that the execution of
64 * javascript would not cause the default behavior of the event.</li>
65 * <li>confirm: string, specifies the message that should show in a pop-up confirmation dialog.</li>
66 * <li>ajax: array, specifies the AJAX options (see {@link ajax}).</li>
67 * <li>live: boolean, whether the event handler should be delegated or directly bound.
68 * If not set, {@link liveEvents} will be used. This option has been available since version 1.1.11.</li>
69 * </ul>
70 * @see http://www.yiiframework.com/doc/api/1.1/CHtml/#clientChange-detail
71 */
72 public static function clientChange($event, &$htmlOptions)
73 {
74 if (!isset($htmlOptions['submit']) && !isset($htmlOptions['confirm']) && !isset($htmlOptions['ajax'])) {
75 return;
76 }
77
78 $live = ArrayHelper::getValue($htmlOptions, 'live', static::$liveEvents);
79
80 $return = (isset($htmlOptions['return']) && $htmlOptions['return'])
81 ? 'return true'
82 : 'return false';
83
84 if (isset($htmlOptions['on' . $event])) {
85 $handler = trim($htmlOptions['on' . $event], ';') . ';';
86 unset($htmlOptions['on' . $event]);
87 } else {
88 $handler = '';
89 }
90
91 if (isset($htmlOptions['id'])) {
92 $id = $htmlOptions['id'];
93 } else {
94 $id = $htmlOptions['id'] = isset($htmlOptions['name']) ? $htmlOptions['name'] : CHtml::ID_PREFIX . CHtml::$count++;
95 }
96
97 $cs = \Yii::app()->getClientScript();
98 $cs->registerCoreScript('jquery');
99
100 if (isset($htmlOptions['submit'])) {
101 $cs->registerCoreScript('yii');
102 $request = \Yii::app()->getRequest();
103 if ($request->enableCsrfValidation && isset($htmlOptions['csrf']) && $htmlOptions['csrf']) {
104 $htmlOptions['params'][$request->csrfTokenName] = $request->getCsrfToken();
105 }
106 if (isset($htmlOptions['params'])) {
107 $params = \CJavaScript::encode($htmlOptions['params']);
108 } else {
109 $params = '{}';
110 }
111 if ($htmlOptions['submit'] !== '') {
112 $url = \CJavaScript::quote(\CHtml::normalizeUrl($htmlOptions['submit']));
113 } else {
114 $url = '';
115 }
116 $handler .= ";jQuery.yii.submitForm(this,'$url',$params);{$return};";
117 }
118
119 if (isset($htmlOptions['ajax'])) {
120 $handler .= \CHtml::ajax($htmlOptions['ajax']) . "{$return};";
121 }
122
123 if (isset($htmlOptions['confirm'])) {
124 $confirm = 'confirm(\'' . \CJavaScript::quote($htmlOptions['confirm']) . '\')';
125 if ($handler !== '') {
126 $handler = "if($confirm) {" . $handler . "} else return false;";
127 } else {
128 $handler = "return $confirm;";
129 }
130 }
131
132 if ($live) {
133 $cs->registerScript(
134 'Foundation.Html.#' . $id,
135 "jQuery('body').on('$event','#$id',function(){{$handler}});"
136 );
137 } else {
138 $cs->registerScript('Foundation.Html.#' . $id, "jQuery('#$id').on('$event', function(){{$handler}});");
139 }
140
141 $htmlOptions = ArrayHelper::removeKeys(
142 $htmlOptions,
143 array(
144 'params',
145 'submit',
146 'ajax',
147 'confirm',
148 'return',
149 'csrf'
150 )
151 );
152 }
153
154 /**
155 * Renders a Foundation thumbnail
156 * @param $src
157 * @param string $url
158 * @param array $htmlOptions
159 * @return string
160 */
161 public static function thumb($src, $url = '#', $htmlOptions = array())
162 {
163 static::addCssClass($htmlOptions, 'th');
164 return \CHtml::link(\CHtml::image($src), $url, $htmlOptions);
165 }
166
167 /**
168 * Adds a CSS class to the specified options.
169 * If the CSS class is already in the options, it will not be added again.
170 * @param array $options the options to be modified.
171 * @param string $class the CSS class to be added
172 */
173 public static function addCssClass(&$options, $class)
174 {
175 if (isset($options['class'])) {
176 $classes = ' ' . $options['class'] . ' ';
177 if (($pos = strpos($classes, ' ' . $class . ' ')) === false) {
178 $options['class'] .= ' ' . $class;
179 }
180 } else {
181 $options['class'] = $class;
182 }
183 }
184 }