~oh-dev/openhealth/phit-tools

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
<?php
/**
* © Copyright 2009 IntraHealth International, Inc.
* 
* This File is part of I2CE 
* 
* I2CE is free software; you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published by 
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License 
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* @package i2ce
* @subpackage core
* @author Carl Leitner <litlfred@ibiblio.org>
* @version v3.2.0
* @since v3.2.0
* @filesource 
*/ 
/** 
* Class I2CE_Module_TreeSelect
* 
* @access public
*/


class I2CE_Module_TreeSelect extends I2CE_Module{

    
    public static function getMethods() {
        return array(
            'I2CE_Page->addAutoCompleteInputTreeById'=>'addAutoCompleteInputTreeById',
            'I2CE_Template->addAutoCompleteInputTreeById'=>'addAutoCompleteInputTreeById',
            'I2CE_Page->addAutoCompleteInputTree'=>'addAutoCompleteInputTree',
            'I2CE_Template->addAutoCompleteInputTree'=>'addAutoCompleteInputTree'
            );
    }

    public static function getHooks() {
        return array(
            'post_page_prepare_display_I2CE_Template'=> 'writeOutJS'
            );
    }

    protected $trees;

    public function  __construct() {
        $this->trees = array();
    }

    /**
     * Adds a auto complete input tree
     * @param string $tree_id The id of the tree
     * @param array $tree_options.  Defaults to empty array.  Passed to the javascript formwork constructor.
     * @param array $autocomplete_options.  Defaults to empty array.  Passed to the javascript formwork constructor.
     */
    public function addAutoCompleteInputTreeById($obj,$tree_id,$tree_options=array(),$autocomplete_options = array()) {
        if (!array_key_exists('auto_input',$this->trees) || !is_array($this->trees['auto_input'])) {
            $this->trees['auto_input'] = array();
        }
        if (is_array($tree_options) ) {
            $tree_options = json_encode($tree_options);
        }
        if (!is_string($tree_options)) {
            $tree_options = '{}';
        }
        if (is_array($autocomplete_options)) {
            $autocomplete_options = json_encode($autocomplete_options);
        }
        if (!is_string($autocomplete_options)) {
            $autocomplete_options = '{}';
        }
        $this->trees['auto_input'][] = "new I2CE_TreeSelectAutoCompleter(new I2CE_TreeInputSelect('{$tree_id}',{$tree_options}),{$autocomplete_options});\n";
    }



    
    public function addAutoCompleteInputTree($template,$node,$hidden_name,$hidden_id,$selected=null,$data, 
                                             $tree_options=array(),$autocomplete_options=array()) {
        if ($template instanceof I2CE_Page) {
            $template = $template->getTemplate();
        }
        if (!$template instanceof I2CE_Template) {
            return;
        }
        if (!is_array($data)) {
            return;
        }
        if (!$node instanceof DOMElement) {
            return;
        }
        $display_id = $hidden_id . '_inputtree_display';
        $tree_id = $hidden_id . '_inputtree_tree';
        $toggle_class = $tree_id . '_toggle';
        $hiddenNode = $template->createElement(
            'input', 
            array(
                'id'=>$hidden_id,
                'name'=>$hidden_name,
                'type'=>'hidden'
                ));
        if (is_array($selected) && array_key_exists('value', $selected)) {
            $hiddenNode->setAttribute('value',$selected['value']);
        }

        $node->appendChild($hiddenNode);
        $displayNode = $template->createElement(
            'input', 
            array(
                'id'=>$display_id,
                'type'=>'text'
                ));
        if (is_array($selected) && array_key_exists('display', $selected)) {
            $displayNode->setAttribute('value',$selected['display']);
        }
        $node->appendChild($displayNode);
        $toggleNode = $template->createElement(
            'span', 
            array(
                'class'=>"tree_main_toggle $toggle_class"
                ),
            'Select Value');        
        $node->appendChild($toggleNode);        
        $treeNode = $template->createElement(
            'span', 
            array(
                'id'=>$tree_id,
                'class'=>'tree_closed treeselect'
                ));
        $template->setClassValues(
            $treeNode,
            array(
                'inputSelectHidden'=>$hidden_id,
                'inputSelectVisible'=>$display_id
                ));
        $node->appendChild($treeNode);        
        $this->createTreeData($template,$treeNode, $data);
        $this->addAutoCompleteInputTreeById($template,$tree_id, $tree_options,$autocomplete_options);
    }


    protected function createTreeData($template,$treeNode,$data) {
        $has_selectable = false;
        foreach ($data as $d) {
            if (!is_array($d)) {
                continue;
            }
            if (!array_key_exists('display',$d)) {
                continue;
            }            
            if (array_key_exists('children',$d) && is_array($d['children'])) {
                $childNode = $template->createElement('span',array('class'=>'tree_children tree_closed'));
                $has_selectable_children = $this->createTreeData($template,$childNode,$d['children']);
                //we have children, so this is an expander
                $expanderNode = $template->createElement('span',array('class'=>'tree_expandable'));
                $treeNode->appendChild($expanderNode);
                if (array_key_exists('value',$d)) {
                    //it is selectable
                    $has_selectable = true;
                    $expanderNode->appendChild( //the toggler
                        $template->createElement('span',array('class'=>'tree_toggle'),' '));
                    $selectorNode = 
                        $template->createElement(
                            'span',
                            array('class'=>'treeselect_selectable'),
                            $d['display']);
                    if (array_key_exists('title',$d)) {
                        $selectorNode->setAttribute('title',$d['title']);
                    }
                    $template->setClassValue($selectorNode, 'treeselect_value',$d['value']);
                    $expanderNode->appendChild( $selectorNode);
                } else {
                    //it is not selectable
                    $class = 'tree_toggle';
                    if ($has_selectable_children){
                        $has_selectable = true;
                    } else {
                        $class .= ' treeselect_notselectable';
                        continue;
                    }
                    $togglerNode = $template->createElement('span',array('class'=>$class),$d['display']);
                    if (array_key_exists('title',$d)) {
                        $togglerNode->setAttribute('title',$d['title']);
                    }
                    $expanderNode->appendChild( $togglerNode);
                }
                $expanderNode->appendChild($childNode);
            } else {
                //we have no children, so this is not an expander
                $childNode = $template->createElement('span', array(),$d['display']);                
                if (array_key_exists('value',$d)) {
                    $has_selectable = true;
                    $childNode->setAttribute('class','treeselect_selectable');
                    $template->setClassValue($childNode,'treeselect_value', $d['value']);
                } else {
                    $childNode->setAttribute('class','treeselect_notselectable');
                    continue;
                }
                if (array_key_exists('title',$d)) {
                    $childNode->setAttribute('title',$d['title']);
                }
                $treeNode->appendChild($childNode);
            }
        }
        return $has_selectable;
    }





    public function writeOutJS($page) {
        if (count($this->trees) == 0) {
            return;
        }
        if (!$page instanceof I2CE_Page) {
            return;
        }
        $template= $page->getTemplate();
        if (!$template instanceof I2CE_Template) {
            return;
        }
        $template->addHeaderLink("mootools-core.js");
        $template->addHeaderLink("mootools-more.js");
        $template->addHeaderLink("getElementsByClassName-1.0.1.js");
        $template->addHeaderLink("I2CE_ClassValues.js");       
        $template->addHeaderLink("I2CE_Window.js");       
        $template->addHeaderLink("I2CE_ToggableWindow.js");       
        $template->addHeaderLink("I2CE_TreeSelect.js");       
        $template->addHeaderLink("Tree.css");       
        if (array_key_exists('auto_input',$this->trees) && is_array($this->trees['auto_input']) && count($this->trees['auto_input'])> 0) {
            $template->addHeaderLink('Observer.js');
            $template->addHeaderLink('Autocompleter.js');
            $template->addHeaderLink('Autocompleter.css');
            $template->addHeaderLink('I2CE_TreeSelectAutoCompleter.js');
        }
        $js = '';
        foreach ($this->trees as $type=>$trees) {
            foreach ($trees as $tree_js) {
                $js .= $tree_js;
            }            
        }        
        $js = "window.addEvent('domready',function() {\n" . $js . "\n});\n";        
        $template->addHeaderText($js,'script','treeselect');
    }



}
# Local Variables:
# mode: php
# c-default-style: "bsd"
# indent-tabs-mode: nil
# c-basic-offset: 4
# End: