]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/ytree/Tree.php
Release 6.2.0
[Github/sugarcrm.git] / include / ytree / Tree.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*usage: initialize the tree, add nodes, generate header for required files inclusion.
39  *        generate function that has tree data and script to convert data into tree init.
40  *            generate call to tree init function.
41  *                subsequent tree data calls will be served by the node class.  
42  *                tree view by default make ajax based calls for all requests.
43  */
44 //require_once('include/entryPoint.php');
45
46 require_once ('include/ytree/Tree.php');
47 require_once ('include/JSON.php');
48
49 class Tree {
50   var $tree_style='include/ytree/TreeView/css/folders/tree.css';        
51   var $_header_files=array(     
52                 'include/javascript/yui/build/treeview/treeview.js',
53         'include/ytree/treeutil.js',                            
54         'include/JSON.js',                                                                                                                                                                      
55       );
56                                          
57   var $_debug_window=false;
58   var $_debug_div_name='debug_tree';
59   var $_name;
60   var $_nodes=array();
61   var $json;
62   //collection of parmeter properties;
63   var $_params=array();
64                                    
65   function Tree($name) {
66                 $this->_name=$name;
67                 $this->json=new JSON(JSON_LOOSE_TYPE);  
68   }
69   
70   //optionally add json.js, required for making AJAX Calls. 
71   function include_json_reference($reference=null) {
72         if (empty($reference)) {
73                 $this->_header_files[]='include/JSON.js';
74         } else { 
75                 $this->_header_files[]=$reference;
76         }                                                                                                
77   }
78            
79   function add_node($node) {
80                 $this->_nodes[$node->uid]=$node;
81   }
82   
83 // returns html for including necessary javascript files.
84   function generate_header() {
85     $ret="<link rel='stylesheet' href='{$this->tree_style}'>\n";
86         foreach ($this->_header_files as $filename) {
87                 $ret.="<script language='JavaScript' src='" . getJSPath($filename) . "'></script>\n";   
88         }
89         return $ret;
90   }
91   
92 //properties set here will be accessible from
93 //the tree's name space..
94   function set_param($name, $value) {
95         if (!empty($name) && !empty($value)) {
96                 $this->_params[$name]=$value;
97         }
98   }
99           
100   function generate_nodes_array($scriptTags = true) {
101         global $sugar_config;
102         $node=null;
103         $ret=array();
104         foreach ($this->_nodes as $node ) {
105                 $ret['nodes'][]=$node->get_definition();
106         }       
107         
108         //todo removed site_url setting from here.
109         //todo make these variables unique.     
110         $tree_data="var TREE_DATA= " . $this->json->encode($ret) . ";\n";
111         $tree_data.="var param= " . $this->json->encode($this->_params) . ";\n";        
112
113         $tree_data.="var mytree;\n";
114         $tree_data.="treeinit(mytree,TREE_DATA,'{$this->_name}',param);\n";
115         if($scriptTags) return '<script>'.$tree_data.'</script>';
116     else return $tree_data;
117   }
118         
119         
120         /**
121          * Generates the javascript node arrays without calling treeinit().  Also generates a callback function that can be
122          * easily called to instatiate the treeview object onload().
123          * 
124          * IE6/7 will throw an "Operation Aborted" error when calling certain types of scripts before the page is fully
125          * loaded.  The workaround is to move the init() call to the onload handler.  See: http://www.viavirtualearth.
126          * com/wiki/DeferScript.ashx
127          * 
128          * @param bool insertScriptTags Flag to add <script> tags
129          * @param string customInitFunction Defaults to "onloadTreeInit"
130          * @return string
131          */
132         function generateNodesNoInit($insertScriptTags=true, $customInitFunction="") {
133                 $node = null;
134                 $ret = array();
135                 
136                 $initFunction = (empty($customInitFunction)) ? 'treeinit' : $customInitFunction;
137                 
138                 foreach($this->_nodes as $node) {
139                         $ret['nodes'][] = $node->get_definition();
140                 }
141                 
142                 $treeData  = "var TREE_DATA = ".$this->json->encode($ret).";\n";
143                 $treeData .= "var param = ".$this->json->encode($this->_params).";\n";
144                 $treeData .= "var mytree;\n";
145                 $treeData .= "function onloadTreeinit() { {$initFunction}(mytree,TREE_DATA,'{$this->_name}',param); }\n";
146                 
147                 if($insertScriptTags) {
148                         $treeData = "<script type='text/javascript' language='javascript'>{$treeData}</script>";
149                 }
150                 
151                 return $treeData;
152         }
153         
154         function generateNodesRaw() {
155                 $node = null;
156                 $ret = array();
157                 $return = array();
158                 
159                 foreach($this->_nodes as $node) {
160                         $ret['nodes'][] = $node->get_definition();
161                 }
162                 
163                 $return['tree_data'] = $ret;
164                 $return['param'] = $this->_params;
165                 
166                 return $return;
167         }
168 }
169 ?>