1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
| <?php
/**
* ResourceHelper class file.
*
* General Helper to support random functionality that need not be in it's own helper
* Has BlueprintCSS support, jQuert ASM HABTM, File displaying, jQuery Error displaying
* and Google Maps support
*
* @author Jose Diaz-Gonzalez
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://josediazgonzalez/code/resourcehelper/
* @package app
* @subpackage app.views.helpers
* @version .2
*/
class ResourceHelper extends AppHelper {
/**
* Array of helpers in use by the ResourceHelper
*
* @var array
**/
var $helpers = array('Form', 'Html');
/**
* Types of images supported by the ResourceHelper::image() function
*
* @var array
*/
var $_imageTypes = array('.jpg', '.jpeg', '.gif', '.bmp', '.png');
/**
* Boolean containing whether ResourceHelper::habtm() has been invoked on
* the form or not
*
* @var boolean
*/
var $_habtm = false;
/**
* The value of the class attribute assigned to the wrapper div for every
* label in the format elementType => class
*
* @var array
*/
var $_labelClass = array();
/**
* The value of the class attribute assigned to the wrapper div for most
* elements
*
* @var string
*/
var $_inputClass = array();
/**
* The value of the class assigned outside the input element if necessary
*
* @var string
**/
var $_specialClass = array();
/**
* Sets up Blueprint for inclusion in the header
*
* @param array $options Additional options to be set if the plugin settings need to be configured
* @return string
* @access public
* @author Jose Diaz-Gonzalez
**/
function setup($options = array()) {
$options = array_merge(array('screen' => 'blueprint/screen','print' => 'blueprint/print'), $options);
$head = $this->Html->css($options['screen'], 'stylesheet', array('media' => 'screen, projection'));
$head .= $this->Html->css($options['print'], 'stylesheet', array('media' => 'print'));
return $head;
}
/**
* Sets up the IE stylesheet for the header
*
* @param array $options Additional options to be set if the plugin settings need to be configured
* @return string
* @access public
* @author Jose Diaz-Gonzalez
**/
function ie($options = array()) {
$options = array_merge(array('ie' => 'blueprint/ie'), $options);
$head = "<!--[if IE]>";
$head .= $this->Html->css($options['ie'], 'stylesheet', array('media' => 'screen'));
$head .= "<![endif]-->";
return $head;
}
/**
* Wrapper for Html Helper that includes a plugin(s) in the header
*
* @param string $path This is the path within the blueprint plugins folder to the plugin(s)
* containing the plugin. Do not include "screen.css" or the trailing slash
* @param array $options Additional options to be set if the plugin settings need to be configured
* @return string
* @access public
* @author Jose Diaz-Gonzalez
**/
function plugins($path = NULL, $options = array()) {
$styles = '';
if (!empty($path)) {
$options = array_merge(array('blueprint' => 'blueprint/plugins','file' => 'screen', 'media' => 'screen, projection'), $options);
if (is_array($path)) {
foreach ($path as $plugin) {
$styles .= $this->Html->css($options['blueprint'] . "//$plugin//" . $options['file'], 'stylesheet', array('media' => $options['media']));
}
} else {
$styles = $this->Html->css($options['blueprint'] . "//$path//" . $options['file'], 'stylesheet', array('media' => $options['media']));
}
}
return $styles;
}
/**
* Caches the blueprintCSS markup needed for labels and form elements
*
* @param string $label The value assigned to the class attribute of the div wrapper
* @param string $input
* @access public
* @author Dave Mahon
*/
function configure($elements, $label = '', $input = '', $special = '') {
if (is_array($elements)) {
foreach ($elements as $element){
$this->_labelClass[$element] = $label;
$this->_inputClass[$element] = $input;
if (!empty($special)) {
$this->_specialClass[$element] = $special;
}
}
} else {
$this->_labelClass[$elements] = $label;
$this->_inputClass[$elements] = $input;
if (!empty($special)) {
$this->_specialClass[$elements] = $special;
}
}
}
/**
* Wrapper for FormHelper->input that adds blueprintCSS markup
*
* @param string $fieldName This should be "Modelname.fieldname", "Modelname/fieldname" is deprecated
* @param array $options
* @param boolean $tagging if true
* @return string
* @access public
* @author Dave Mahon
*/
function input($fieldName, $options = array(), $tagging = false) {
if (!isset($options['type'])) {
$options['type'] = 'text';
}
if (!empty($this->_specialClass[$options['type']])) {
$options = array_merge(array(
'before' => '<div class="' . $this->_labelClass[$options['type']] . '">',
'between' => '</div><div class="' . $this->_specialClass[$options['type']] . '"><div class="' . $this->_inputClass[$options['type']] . '">',
'after' => '</div></div>'
), $options);
} else {
$options = array_merge(array(
'before' => '<div class="' . $this->_labelClass[$options['type']] . '">',
'between' => '</div><div class="' . $this->_inputClass[$options['type']] . '">',
'after' => '</div>'
), $options);
}
if ($tagging) {
App::import('Helper', 'Tagging.Tagging');
return $this->Tagging->input($fieldName, $options);
}
return $this->Form->input($fieldName, $options);
}
/**
* Wrapper for FormHelper->end that adds blueprintCSS markup
*
* If $options is set a form submit button will be created.
*
* @param mixed $options as a string will use $options as the value of button,
* array usage:
* array('label' => 'save'); value="save"
* array('label' => 'save', 'name' => 'Whatever'); value="save" name="Whatever"
* array('name' => 'Whatever'); value="Submit" name="Whatever"
* array('label' => 'save', 'name' => 'Whatever', 'div' => 'good') <div class="good"> value="save" name="Whatever"
* array('label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')); <div class="good"> value="save" name="Whatever"
*
* @return string a closing FORM tag optional submit button.
* @access public
* @author Dave Mahon
*/
function end($options = null) {
if ($options !== null) {
if (is_string($options)) {
$options = array('label'=>$options);
}
if (isset($options['div'])) {
$options['div'] = $this->addClass($options['div'], 'clear');
} else {
$options['div'] = array('class' => 'clear');
}
}
return $this->Form->end($options);
}
/**
* Wrapper for HtmlHelper->div that adds blueprintCSS markup for class clear
*
* @param string $fieldName This should be "Modelname.fieldname", "Modelname/fieldname" is deprecated
* @param string $class CSS class name of the div element.
* @param string $text String content that will appear inside the div element.
* If null, only a start tag will be printed
* @param array $attributes Additional HTML attributes of the DIV tag
* @param boolean $escape If true, $text will be HTML-escaped
* @return string The formatted DIV element
* @access public
* @author Dave Mahon
*/
function clear($class = null, $text = null, $attributes = array(), $escape = false) {
if (strlen($class) > 0) {
$class .= ' clear';
} else {
$class = 'clear';
}
return $this->Html->div($class, $text, $attributes, $escape);
}
/**
* Automatically wraps text(s) with classes/ids
*
* @param boolean $wrapAll whether or not to wrap each text with the same class. true by default
* @param string/array $texts string or array of text that should be wrapped
* @param string/array $classes string or array of classes to wrap the texts
* @param string/array $ids string or array of ids to wrap the texts
* @return void
* @author Jose Diaz-Gonzalez
**/
function span($wrapAll = true, $texts = NULL, $classes = NULL, $ids = null) {
$finalText = '';
if (is_array($texts)) {
if ($wrapAll) {
foreach ($texts as $text) {
$finalText .= "<div";
if (isset($ids)) {
$finalText .= " id=\"$ids\"";
}
if (isset($classes)) {
$finalText .= " class=\"$classes\"";
}
$finalText .= ">$text</div>";
}
} else {
$i = 0;
foreach($texts as $text) {
$finalText .= "<div";
if (isset($ids)) {
$finalText .= " id=\"" . $ids[$i] . "\"";
}
if (isset($classes)) {
$finalText .= " class=\"" . $classes[$i] . "\"";
}
$finalText .= ">$text</div>";
}
}
} else {
$finalText .= "<div";
if (isset($ids)) {
$finalText .= " id=\"" . $ids[$i] . "\"";
}
if (isset($classes)) {
$finalText .= " class=\"" . $classes[$i] . "\"";
}
$finalText .= ">$text</div>";
}
return $finalText;
}
function submit($title = 'Submit', $options = array()) {
$defaults = array('type' => 'submit');
$options = array_merge($defaults, $options);
if (isset($options['class'])) {
$options['class'] .= ' positive';
} else {
$options['class'] = ' positive';
}
$keys = array_keys($options);
$values = array_values($options);
$buttonOptions = '';
for ($i = 0; $i < count($keys); $i++) {
$buttonOptions .= " " . $keys[$i] . "=\"" . $values[$i] . "\"";
}
return "<div class=\"button\"><button{$buttonOptions}>{$title}</button></div>";
}
function habtm($field, $options = array()) {
$defaults = array('jsPath' => 'jquery/asm');
$options = array_merge($defaults, $options);
if ($this->_habtm) {
return $this->Form->input($field);
} else {
$this->_habtm = true;
return $this->Form->input($field) . $this->Html->script($options['jsPath'], false);
}
}
function file($value, $options = array()) {
if ($this->_isImage($value)) {
if ($this->_isUrl($value)) {
$options['url'] = $value;
return $this->Html->image("{$value}", $options);
} else {
return $this->Html->image("/{$value}", $options);
}
}
$link = Router::url("/{$value}", true);
return $this->Html->link("{$link}", "/{$value}", $options);
}
function image($value, $options = array(), $foptions = array()) {
$defaults = array();
$options = array_merge($defaults, $options);
$defoptions = array($path = null, $size = 'medium', 'url' => null);
$foptions = array_merge($defoptions, $foptions);
return $this->Html->image("../{$foptions['path']}/thumb/{$foptions['size']}/{$value}", $options);
}
function imageLink($value, $options = array()){
if ($this->_isImage($value)) {
if ($this->_isUrl($value)) {
$options['url'] = $value;
return $this->Html->image("{$value}", $options);
} else {
return $this->Html->image("/{$value}", $options);
}
}
return $value;
}
function value($value, $options = array()) {
$defaults = array('div' => true);
$options = array_merge($defaults, $options);
if (isset($value) and !empty($value) and ($value !== '')) {
if ($options['div']){
return "<div>{$value}</div>";
}
return "{$value}<br />";
}
return "{$value}";
}
function map($api, $options = array()) {
return
'<div id="map_container">
<div id="big_spinner" style="display:none"></div>
<div id="map"></div>
<div id="map_tooltip">
drag marker to fix location
</div>
</div>' . $this->_mapSetup($api, $options);
}
function mapInput($value, $options = array()) {
$defaults = array('onchange' => 'widget.initMap()');
$options = array_merge($defaults, $options);
return $this->Form->input($value, $options);
}
/**
* Prints out a jQuery enhanced list of validation errors
*
* @param array $data Array of all possible errors for all models being validated
* @param integer $type Integer indicating the css id of the error
* @param boolean $flash Boolean indicating whether this is a flash message
* @access public
*/
function validationErrors($data, $id, $flash = false){
$errors = $this->_getArray($data);
if ($errors != array()){
echo $this->_jqueryList(array_values($errors), $id, $flash);
}
}
/**
* Prints out a jQuery enhanced list of validation errors
*
* @param array $data Array of all possible errors
* @param integer $type Integer indicating the css id of the error
* @param boolean $flash Boolean indicating whether this is a flash message
* @return string String of Javascript/HTML containing list of errors
* @access public
*/
function _jqueryList($data, $type, $flash) {
$temp = $this->_getList($data, $flash);
$id = $this->_getType($type);
if ($flash){
$output = "<div id=\"". $id ."\" class=\"flash\"style=\"display: none\">Error Message:<br />{$temp}</div>
<script type=\"text/javascript\">
jQuery(document).ready(function() {
$ (\".flash\").fadeIn(\"slow\");
});
</script>";
} else {
$output = "<div id=\"". $id ."\" class=\"flash\"style=\"display: none\">Reasons for error:<br />{$temp}</div>
<script type=\"text/javascript\">
jQuery(document).ready(function() {
$ (\".flash\").fadeIn(\"slow\");
});
</script>";
}
return $output;
}
/**
* Used in conjunction with jQueryList
* Returns the id of the notice
*
* @param integer $data Integer referencing an error type
* @return string String assigned to an error
* @access public
*/
function _getType($data){
$output = "";
switch ($data) {
case 0:
$output = "error";
break;
case 1:
$output = "success";
break;
case 2:
$output = "notice";
break;
case 3:
$output = "reasons";
break;
default:
$output = "notice";
break;
}
return $output;
}
/**
* Returns a list of items, each wrapped in an <li></li>
*
* @param array $data Array of all possible items
* @param boolean $flash Boolean indicating whether this is a flash message
* @return string String containing a <li></li> wrapped list of items
* @access public
*/
function _getList($data, $flash){
$output = '';
if (is_array($data)){
if($flash){
$output .= "<li>{$data['0']}</li>";
} else {
foreach($data as $item){
$output .= "<li>{$item}</li>";
}
}
}
else {
$output .= "<li>{$data}</li>";
}
return "<ul>".$output."</ul>";
}
/**
* Returns an Array of items that are nested within some other array
*
* @param array $data Array of items which may be arrays themselves
* @return array Array of items which remove one layer of nesting
* @access public
*/
function _getArray($data){
$arrayValues = array_values($data);
$output = array();
foreach($arrayValues as $value){
$output += $value;
}
return $output;
}
function _endsWith($input = null, $query = null) {
// Get the length of the end string
$queryLength = strlen($query);
// Look at the end of input for the substring the size of query
$inputEnd = substr($input, strlen($input) - $queryLength);
// If it matches, it does end with query
return $inputEnd == $query;
}
function _isImage($input) {
foreach ($this->_imageTypes as $imageType) {
if ($this->_endsWith($input, $imageType)) {
return true;
}
}
return false;
}
function _mapSetup($api = null, $options = array()) {
$defaults = array('proxy' => 'map/proxy', 'addresschooser' => 'map/addresschooser', 'display' => 'map/display');
$options = array_merge($defaults, $options);
$return = $this->Html->script('http://www.google.com/jsapi?key=' . $api, array('safe' => false));
$return .= $this->Html->script($options['proxy'], array('safe' => false));
$return .= $this->Html->script($options['addresschooser'], array('safe' => false));
$return .= $this->Html->script($options['display'], array('safe' => false));
return $return;
}
/**
* Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt
*
* The regex checks for the following component parts:
* a valid, optional, scheme
* a valid ip address OR
* a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt
* with an optional port number
* an optional valid path
* an optional query string (get parameters)
* an optional fragment (anchor tag)
*
* @param string $check Value to check
* @param boolean $strict Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)
* @return boolean Success
* @access public
*/
function _isUrl($check, $strict = false) {
$_this =& Validation::getInstance();
$_this->check = $check;
$validChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=') . '\/0-9a-z]|(%[0-9a-f]{2}))';
$_this->regex = '/^(?:(?:https?|ftps?|file|news|gopher):\/\/)' . ife($strict, '', '?') .
'(?:' . $_this->__pattern['ip'] . '|' . $_this->__pattern['hostname'] . ')(?::[1-9][0-9]{0,3})?' .
'(?:\/?|\/' . $validChars . '*)?' .
'(?:\?' . $validChars . '*)?' .
'(?:#' . $validChars . '*)?$/i';
return $_this->_check();
}
}
?>
|
Discussion