1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class joosHTML
17: {
18: 19: 20: 21: 22: 23: 24:
25: public static function js_file($file)
26: {
27: $file = ((strpos($file, '://') === false)) ? JPATH_SITE . $file : $file;
28:
29: return '<script type="text/javascript" src="' . $file . '"></script>';
30: }
31:
32: 33: 34: 35: 36: 37: 38:
39: public static function js_code($code)
40: {
41: return '<script type="text/javascript" charset="utf-8">;' . $code . ';</script>';
42: }
43:
44: 45: 46: 47: 48: 49: 50: 51:
52: public static function css_file($file, $media = 'all')
53: {
54: $file = ((strpos($file, '://') === false)) ? JPATH_SITE . $file : $file;
55:
56: return '<link rel="stylesheet" type="text/css" media="' . $media . '" href="' . $file . '" />';
57: }
58:
59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70:
71: public static function ico($name, $size = '16x16')
72: {
73: return sprintf('%s/media/images/icons/%s/candy/%s.png', JPATH_SITE, $size, $name);
74: }
75:
76: 77: 78: 79: 80: 81: 82: 83: 84:
85: public static function anchor($uri, $title = NULL, $attributes = NULL, $escape_title = true)
86: {
87: return '<a href="' . joosFilter::specialurlencode($uri, FALSE) . '"' . (is_array($attributes) ? joosHtml::attributes($attributes) : '') . '>' . ($escape_title ? joosFilter::htmlspecialchars((($title === NULL) ? $uri : $title), FALSE) : (($title === NULL) ? $uri : $title)) . '</a>';
88: }
89:
90: 91: 92: 93: 94: 95: 96: 97: 98:
99: public static function make_option($value, $text = '', $value_name = 'value', $text_name = 'text')
100: {
101: $obj = new stdClass;
102: $obj->$value_name = $value;
103: $obj->$text_name = trim($text) ? $text : $value;
104:
105: return $obj;
106: }
107:
108: public static function select_list(array $arr, $tag_name, $tag_attribs, $key, $text, $selected = null, $first_el_key = '*000', $first_el_text = '*000')
109: {
110: is_array($arr) ? reset($arr) : null;
111:
112: $html = "<select name=\"$tag_name\" $tag_attribs>";
113:
114: if ($first_el_key != '*000' && $first_el_text != '*000') {
115: $html .= "\n\t<option value=\"$first_el_key\">$first_el_text</option>";
116: }
117:
118: $count = count($arr);
119: for ($i = 0, $n = $count; $i < $n; $i++) {
120: $k = $arr[$i]->$key;
121: $t = $arr[$i]->$text;
122: $id = (isset($arr[$i]->id) ? $arr[$i]->id : null);
123:
124: $extra = '';
125: $extra .= $id ? " id=\"" . $arr[$i]->id . "\"" : '';
126: if (is_array($selected)) {
127: foreach ($selected as $obj) {
128: $k2 = $obj->$key;
129: if ($k == $k2) {
130: $extra .= " selected=\"selected\"";
131: break;
132: }
133: }
134: } else {
135: $extra .= ($k == $selected ? " selected=\"selected\"" : '');
136: }
137: $html .= "\n\t<option value=\"" . $k . "\"$extra>" . $t . "</option>";
138: }
139: $html .= "\n</select>\n";
140:
141: return $html;
142: }
143:
144: public static function select_day($tag_name, $tag_attribs, $selected)
145: {
146: $arr = array();
147:
148: for ($i = 1; $i <= 31; $i++) {
149: $pref = '';
150: if ($i <= 9) {
151: $pref = '0';
152: }
153: $arr[] = joosHtml::make_option($pref . $i, $pref . $i);
154: }
155:
156: return joosHtml::select_list($arr, $tag_name, $tag_attribs, 'value', 'text', $selected);
157: }
158:
159: public static function select_month($tag_name, $tag_attribs, $selected, $type = 0)
160: {
161:
162: $arr_1 = array(
163: joosHtml::make_option('01', 'Январь'),
164: joosHtml::make_option('02', 'Февраль'),
165: joosHtml::make_option('03', 'Март'),
166: joosHtml::make_option('04', 'Апрель'),
167: joosHtml::make_option('5', 'Май'),
168: joosHtml::make_option('06', 'Июнь'),
169: joosHtml::make_option('07', 'Июль'),
170: joosHtml::make_option('08', 'Август'),
171: joosHtml::make_option('09', 'Сентябрь'),
172: joosHtml::make_option('10', 'Октябрь'),
173: joosHtml::make_option('11', 'Ноябрь'),
174: joosHtml::make_option('12', 'Декабрь')
175: );
176:
177:
178: $arr_2 = array(
179: joosHtml::make_option('01', 'Января'),
180: joosHtml::make_option('02', 'Февраля'),
181: joosHtml::make_option('03', 'Марта'),
182: joosHtml::make_option('04', 'Апреля'),
183: joosHtml::make_option('05', 'Мая'),
184: joosHtml::make_option('06', 'Июня'),
185: joosHtml::make_option('07', 'Июля'),
186: joosHtml::make_option('08', 'Августа'),
187: joosHtml::make_option('09', 'Сентября'),
188: joosHtml::make_option('10', 'Октября'),
189: joosHtml::make_option('11', 'Ноября'),
190: joosHtml::make_option('12', 'Декабря')
191: );
192:
193: $arr = $type ? $arr_2 : $arr_1;
194:
195: return joosHtml::select_list($arr, $tag_name, $tag_attribs, 'value', 'text', $selected);
196: }
197:
198: public static function select_year($tag_name, $tag_attribs, $selected, $min = 1900, $max = null)
199: {
200: $max = ($max == null) ? date('Y', time()) : $max;
201:
202: $arr = array();
203: for ($i = $min; $i <= $max; $i++) {
204: $arr[] = joosHtml::make_option($i, $i);
205: }
206:
207: return joosHtml::select_list($arr, $tag_name, $tag_attribs, 'value', 'text', $selected);
208: }
209:
210: public static function gender_select_list($tag_name, $tag_attribs, $selected)
211: {
212: $arr = array(joosHtml::make_option('no_gender', 'Не указано'), joosHtml::make_option('male', 'М'), joosHtml::make_option('female', 'Ж'));
213:
214: return joosHtml::select_list($arr, $tag_name, $tag_attribs, 'value', 'text', $selected);
215: }
216:
217: public static function id_box($rowNum, $recId, $checkedOut = false, $name = 'cid')
218: {
219: return $checkedOut ? '' : '<input class="js-select" type="checkbox" id="cb' . $rowNum . '" name="' . $name . '[]" value="' . $recId . '" />';
220: }
221:
222: public static function dropdown($data, $options = NULL, $selected = NULL, $extra = '')
223: {
224: if (!is_array($data)) {
225: $data = array('name' => $data);
226: } else {
227: if (isset($data['options'])) {
228:
229: $options = $data['options'];
230: }
231:
232: if (isset($data['selected'])) {
233:
234: $selected = $data['selected'];
235: }
236: }
237:
238: if (is_array($selected)) {
239:
240: $data['multiple'] = 'multiple';
241: } else {
242:
243: $selected = array($selected);
244: }
245:
246: $input = '<select' . joosHTML::attributes($data, 'select') . ' ' . $extra . '>' . "\n";
247:
248: foreach ((array) $options as $key => $val) {
249:
250: $key = (string) $key;
251:
252: if (is_array($val)) {
253: $input .= '<optgroup label="' . $key . '">' . "\n";
254: foreach ($val as $inner_key => $inner_val) {
255:
256: $inner_key = (string) $inner_key;
257:
258: $sel = in_array($inner_key, $selected) ? ' selected="selected"' : '';
259: $input .= '<option value="' . $inner_key . '"' . $sel . '>' . $inner_val . '</option>' . "\n";
260: }
261: $input .= '</optgroup>' . "\n";
262: } else {
263: $sel = in_array($key, $selected) ? ' selected="selected"' : '';
264: $input .= '<option value="' . $key . '"' . $sel . '>' . $val . '</option>' . "\n";
265: }
266: }
267: $input .= '</select>';
268:
269: return $input;
270: }
271:
272: public static function textarea($data, $value = '', $extra = '', $double_encode = TRUE)
273: {
274: if (!is_array($data)) {
275: $data = array('name' => $data);
276: }
277:
278: $value = isset($data['value']) ? $data['value'] : $value;
279:
280: unset($data['value']);
281:
282: return '<textarea' . joosHtml::attributes($data, 'textarea') . ' ' . $extra . '>' . joosFilter::htmlspecialchars($value, $double_encode) . '</textarea>';
283: }
284:
285: public static function input($data, $value = '', $extra = '')
286: {
287: if (!is_array($data)) {
288: $data = array('name' => $data);
289: }
290:
291: $data += array('type' => 'text', 'value' => $value);
292:
293: return '<input' . joosHtml::attributes($data) . ' ' . $extra . ' />';
294: }
295:
296: public static function label($data = '', $text = NULL, $extra = '')
297: {
298: if (!is_array($data)) {
299: if (is_string($data)) {
300: $data = array('for' => $data);
301: } else {
302: $data = array();
303: }
304: }
305:
306: return '<label class="control-label"' . joosHtml::attributes($data) . ' ' . $extra . '>' . $text . '</label>';
307: }
308:
309: public static function hidden($data, $value = '')
310: {
311: if (!is_array($data)) {
312: $data = array($data => $value);
313: }
314:
315: $input = '';
316: foreach ($data as $name => $value) {
317: $attr = array('type' => 'hidden', 'name' => $name, 'value' => $value);
318:
319: $input .= joosHtml::input($attr) . "\n";
320: }
321:
322: return $input;
323: }
324:
325: public static function checkbox($data, $value = '', $checked = FALSE, $extra = '')
326: {
327: if (!is_array($data)) {
328: $data = array('name' => $data);
329: }
330:
331: $data['type'] = 'checkbox';
332:
333: if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE)) {
334: $data['checked'] = 'checked';
335: } else {
336: unset($data['checked']);
337: }
338:
339: return joosHtml::input($data, $value, $extra);
340: }
341:
342: 343: 344: 345: 346: 347:
348: private static function attributes($attrs)
349: {
350: if (empty($attrs)) {
351: return '';
352: }
353:
354: if (is_string($attrs)) {
355: return ' ' . $attrs;
356: }
357:
358: $compiled = '';
359: foreach ($attrs as $key => $val) {
360:
361:
362: if (is_array($val)) {
363: continue;
364: }
365:
366: $compiled .= ' ' . $key . '="' . joosFilter::htmlspecialchars($val) . '"';
367: }
368:
369: return $compiled;
370: }
371:
372: 373: 374: 375: 376: 377: 378:
379: public static function prepare_for_ajax_output($content)
380: {
381: $content = str_replace(array("\n", "\t", "\r"), ' ', $content);
382: $content = preg_replace("/\s\s+/iu", ' ', $content);
383: $content = preg_replace("/>\s+</iu", '><', $content);
384:
385: return $content;
386: }
387:
388: }
389: