-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAngularJsHelper.php
124 lines (107 loc) · 4.09 KB
/
AngularJsHelper.php
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
<?php
/**
* AngularJS <-> CakePHP adapter
*
* This Helper will do basically a few things:
*
* - Load AngularJS (from Google CDN by default) and all required libs (resource, controller, bootstrap, etc)
* - Generate a DIV tag which AngularJS scope is set to the controller you specify
* in $options['controller']. The tag / scope is closed by using the "end" method
* (so beware not to leave it open).
* - Inject CakePHP formatted data ($options['data']) into AngularJS controller's scope.
* The data is stored into $scope._data so you can easily make:
*
* <li ng-repeat="model in _data">{{model.id}}</li>
*
* ... and use all the magic from AngularJs over that data.
*
* @author Nicolas Iglesias <[email protected]>
*/
class AngularJsHelper extends HtmlHelper
{
private $_options = null;
private $_bootstrap = null;
private $_controller = null;
private $_id = null;
public function begin($options = array())
{
if (empty($options))
return null;
extract($options);
$this->_id = sha1(microtime());
$this->_options = $options;
echo $this->script('http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js',
array('inline' => isset($inline) ? $inline : false));
echo $this->script('http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular-resource.min.js',
array('inline' => isset($inline) ? $inline : false));
if (isset($extras)) {
foreach ($extras as $e) {
if (stristr($e, '.js')) {
echo $this->script($e,
array('inline' => isset($inline) ? $inline : false));
} else if (stristr($e, '.css')) {
echo $this->css($e);
}
}
}
if (isset($bootstrap)) {
$this->_bootstrap = trim(strtolower(preg_replace('/^.*\/(.+)$/si',
'$1', $bootstrap)));
echo $this->script($bootstrap,
array('inline' => isset($inline) ? $inline : false));
}
if (isset($controller)) {
$this->_controller = trim(strtolower(preg_replace('/^.*\/(.+)$/si',
'$1', $controller)));
echo $this->script($controller . '_controller',
array('inline' => isset($inline) ? $inline : false));
}
if (!empty($this->_bootstrap))
echo '<div ng-app="' . $this->_bootstrap . '">';
if (!empty($this->_controller)) {
$data = isset($data) ? json_encode($data) : 'null';
$js = <<<JS
window.document.onload = function(e){
angular.element('#{$this->_id}').scope()._data = {$data};
angular.element('#{$this->_id}').scope().\$apply();
};
JS;
echo $this->scriptBlock($js);
echo '<div ng-cloak ng-controller="' . ucwords($this->_controller) . 'Controller" id="' . $this->_id . '">';
}
}
public function end()
{
if (empty($this->_controller))
return null;
echo '</div>';
if (!empty($this->_bootstrap))
echo '</div>';
}
/**
* Formats a PHP array into a compatible AngularJs list to be used in "select" element.
* The keys on the array will be stored in the "value" property while the values will be placed in the "name" property.
*
* Example:
* <form ng-init="statuses=<?php echo $this->AngularJs->toAngularList($statuses);?>">
* <select ng-model="model.status" ng-options="s.value as s.name for s in statuses"></select>
* </form>
*
* @param type $arr
* @return type
*/
public function toAngularList($arr = null)
{
$ret = null;
if (!empty($arr)) {
$tmp = array();
foreach ($arr as $k => $v) {
$tmp[] = array('name' => $v, 'value' => $k);
}
$ret = str_replace('"', "'",
json_encode($tmp, JSON_NUMERIC_CHECK | JSON_OBJECT_AS_ARRAY));
}
return $ret;
}
}
?>