|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Projax |
|
5
|
|
|
* |
|
6
|
|
|
* An open source set of php helper classes for prototype and script.aculo.us. |
|
7
|
|
|
* |
|
8
|
|
|
* @package Projax |
|
9
|
|
|
* @author Vikas Patial |
|
10
|
|
|
* @copyright Copyright (c) 2006, ngcoders. |
|
11
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html |
|
12
|
|
|
* @link http://www.ngcoders.com |
|
13
|
|
|
* @since Version 0.2 |
|
14
|
|
|
* @filesource |
|
15
|
|
|
*/ |
|
16
|
|
View Code Duplication |
class JavaScript |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param $name |
|
20
|
|
|
* @param null $function |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
public function button_to_function($name, $function = null) |
|
24
|
|
|
{ |
|
25
|
|
|
return '<input type="button" value="' . $name . '" onclick="' . $function . '">'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $javascript |
|
30
|
|
|
* @return mixed|string |
|
31
|
|
|
*/ |
|
32
|
|
|
public function escape($javascript) |
|
33
|
|
|
{ |
|
34
|
|
|
$javascript = addslashes($javascript); |
|
35
|
|
|
$javascript = str_replace(["\r\n", "\n", "\r"], ["\n"], $javascript); |
|
36
|
|
|
|
|
37
|
|
|
return $javascript; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $content |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function tag($content) |
|
45
|
|
|
{ |
|
46
|
|
|
return '<script type="text/javascript">' . $content . '</script>'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param $name |
|
51
|
|
|
* @param $function |
|
52
|
|
|
* @param null $html_options |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function link_to_function($name, $function, $html_options = null) |
|
56
|
|
|
{ |
|
57
|
|
|
return '<a href="' . (isset($html_options['href']) ? $html_options['href'] : '#') . '" onclick="' . (isset($html_options['onclick']) ? $html_options['onclick'] . ';' : '') . $function . '; return false;">' . $name . '</a>'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
///////////////////////////////////////////////////////////////////////////////////// |
|
61
|
|
|
// Private functions |
|
62
|
|
|
///////////////////////////////////////////////////////////////////////////////////// |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $option |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function _array_or_string_for_javascript($option) |
|
69
|
|
|
{ |
|
70
|
|
|
$return_val = ''; |
|
71
|
|
|
if (is_array($option)) { |
|
72
|
|
|
foreach ($option as $value) { |
|
73
|
|
|
if (!empty($return_val)) { |
|
74
|
|
|
$ret_val .= ', '; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
$return_val .= $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return '[' . $return_val . ']'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return "'$option'"; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $options |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function _options_for_javascript($options) |
|
90
|
|
|
{ |
|
91
|
|
|
$return_val = ''; |
|
92
|
|
|
|
|
93
|
|
|
if (is_array($options)) { |
|
94
|
|
|
foreach ($options as $var => $val) { |
|
95
|
|
|
if (!empty($return_val)) { |
|
96
|
|
|
$return_val .= ', '; |
|
97
|
|
|
} |
|
98
|
|
|
$return_val .= "$var:$val"; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return '{' . $return_val . '}'; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.