1: <?php
2:
3: App::uses('AppHelper', 'View/Helper');
4:
5: class ContextHelper extends AppHelper {
6:
7: public $helpers = array(
8: 'ExtendedScaffold.ExtendedForm',
9: 'Widgets.ActionList' => array(
10: 'beforeAll' => '<div class="ContextMenu"><ul>',
11: 'afterAll' => '</ul></div>',
12: 'beforeEach' => '<li>',
13: 'afterEach' => '</li>',
14: ),
15: );
16:
17: public function ($modules) {
18: if (array_key_exists($this->params['controller'], $modules)) {
19: return $this->ActionList->actionList($this->_actions($modules));
20: } else {
21: return '';
22: }
23: }
24:
25: public function selector() {
26: if (array_key_exists('contextObjectsList', $this->_View->viewVars)) {
27: if (count($this->_View->viewVars['contextObjectsList']) > 1) {
28: $id = $this->ExtendedForm->createNewDomId();
29: return $this->_selectorInput($id)
30: . $this->_selectorOnChangeEvent($id);
31: }
32: }
33:
34: return '';
35: }
36:
37: private function _selectorInput($inputId) {
38: return $this->ExtendedForm->select(
39: 'my_store'
40: , $this->_View->viewVars['contextObjectsList']
41: , array(
42: 'class' => 'ContextSelector'
43: , 'empty' => false
44: , 'id' => $inputId
45: , 'value' => $this->_View->viewVars['contextCurrentId']
46: )
47: );
48: }
49:
50: private function _selectorOnChangeEvent($inputId) {
51: $b = <<<EOT
52: <script type='text/javascript' >
53:
54: $('#$inputId').change(function(){
55: var urls = {};
56:
57: EOT;
58: foreach (array_keys($this->_View->viewVars['contextObjectsList']) as $contextId) {
59: $b .= "urls[$contextId] = '{$this->_selectorContextUrl($contextId)}';\n";
60: }
61: $b .= <<<EOT
62: window.location = urls[$(this).val()];
63:
64: });
65:
66: </script>
67: EOT;
68: return $b;
69: }
70:
71: private function _selectorContextUrl($contextId) {
72: $url = Router::parse($this->request->url);
73: $url['named']['changeContextId'] = $contextId;
74: $url = array_merge($url['named'], $url['pass'], $url);
75: unset($url['named']);
76: unset($url['pass']);
77: return Router::url($url);
78: }
79:
80: private function _actions($modules) {
81: $actions = array();
82: foreach ($modules as $controller => $actionData) {
83: $actions[] = array(
84: 'url' => $this->_moduleActionUrl($controller, $actionData),
85: 'caption' => $this->_moduleActionCaption($actionData),
86: 'linkOptions' => $this->_moduleActionLinkOptions($controller, $actionData),
87: );
88: }
89: return $actions;
90: }
91:
92: private function _moduleActionUrl($controller, $actionData) {
93: if (empty($this->_View->viewVars['contextCurrentId'])) {
94: throw new Exception('Variável $contextCurrentId não foi setada');
95: }
96: $url = empty($actionData['url']) ? "/{$controller}/index" : $actionData['url'];
97: return $this->_getContext()->buildUrl(
98: $url, $this->_View->viewVars['contextCurrentId']);
99: }
100:
101: 102: 103: 104:
105: private function _getContext() {
106: return Contexts::getContext($this->_View->viewVars['contextId']);
107: }
108:
109: private function _moduleActionCaption($actionData) {
110: return empty($actionData['url']) ? $actionData : $actionData['caption'];
111: }
112:
113: private function _moduleActionLinkOptions($controller, $actionData) {
114: $linkOptions = array();
115: if ($controller == $this->params['controller']) {
116: $linkOptions['class'] = 'current';
117: }
118: return $linkOptions;
119: }
120:
121: }
122: