]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/ytree/Tree.php
Release 6.5.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-2012 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       );
55                                          
56   var $_debug_window=false;
57   var $_debug_div_name='debug_tree';
58   var $_name;
59   var $_nodes=array();
60   var $json;
61   //collection of parmeter properties;
62   var $_params=array();
63                                    
64   function Tree($name) {
65                 $this->_name=$name;
66                 $this->json=new JSON(JSON_LOOSE_TYPE);  
67   }
68   
69   //optionally add json.js, required for making AJAX Calls. 
70   function include_json_reference($reference=null) {
71     // if (empty($reference)) {
72     //  $this->_header_files[]='include/JSON.js';
73     // } else {
74     //  $this->_header_files[]=$reference;
75     // }
76   }
77            
78   function add_node($node) {
79                 $this->_nodes[$node->uid]=$node;
80   }
81   
82 // returns html for including necessary javascript files.
83   function generate_header() {
84     $ret="<link rel='stylesheet' href='{$this->tree_style}'>\n";
85         foreach ($this->_header_files as $filename) {
86                 $ret.="<script language='JavaScript' src='" . getJSPath($filename) . "'></script>\n";   
87         }
88         return $ret;
89   }
90   
91 //properties set here will be accessible from
92 //the tree's name space..
93   function set_param($name, $value) {
94         if (!empty($name) && !empty($value)) {
95                 $this->_params[$name]=$value;
96         }
97   }
98           
99   function generate_nodes_array($scriptTags = true) {
100         global $sugar_config;
101         $node=null;
102         $ret=array();
103         foreach ($this->_nodes as $node ) {
104                 $ret['nodes'][]=$node->get_definition();
105         }       
106         
107         //todo removed site_url setting from here.
108         //todo make these variables unique.     
109         $tree_data="var TREE_DATA= " . $this->json->encode($ret) . ";\n";
110         $tree_data.="var param= " . $this->json->encode($this->_params) . ";\n";        
111
112         $tree_data.="var mytree;\n";
113         $tree_data.="treeinit(mytree,TREE_DATA,'{$this->_name}',param);\n";
114         if($scriptTags) return '<script>'.$tree_data.'</script>';
115     else return $tree_data;
116   }
117         
118         
119         /**
120          * Generates the javascript node arrays without calling treeinit().  Also generates a callback function that can be
121          * easily called to instatiate the treeview object onload().
122          * 
123          * IE6/7 will throw an "Operation Aborted" error when calling certain types of scripts before the page is fully
124          * loaded.  The workaround is to move the init() call to the onload handler.  See: http://www.viavirtualearth.
125          * com/wiki/DeferScript.ashx
126          * 
127          * @param bool insertScriptTags Flag to add <script> tags
128          * @param string customInitFunction Defaults to "onloadTreeInit"
129          * @return string
130          */
131         function generateNodesNoInit($insertScriptTags=true, $customInitFunction="") {
132                 $node = null;
133                 $ret = array();
134                 
135                 $initFunction = (empty($customInitFunction)) ? 'treeinit' : $customInitFunction;
136                 
137                 foreach($this->_nodes as $node) {
138                         $ret['nodes'][] = $node->get_definition();
139                 }
140                 
141                 $treeData  = "var TREE_DATA = ".$this->json->encode($ret).";\n";
142                 $treeData .= "var param = ".$this->json->encode($this->_params).";\n";
143                 $treeData .= "var mytree;\n";
144                 $treeData .= "function onloadTreeinit() { {$initFunction}(mytree,TREE_DATA,'{$this->_name}',param); }\n";
145                 
146                 if($insertScriptTags) {
147                         $treeData = "<script type='text/javascript' language='javascript'>{$treeData}</script>";
148                 }
149                 
150                 return $treeData;
151         }
152         
153         function generateNodesRaw() {
154                 $node = null;
155                 $ret = array();
156                 $return = array();
157                 
158                 foreach($this->_nodes as $node) {
159                         $ret['nodes'][] = $node->get_definition();
160                 }
161                 
162                 $return['tree_data'] = $ret;
163                 $return['param'] = $this->_params;
164                 
165                 return $return;
166         }
167 }
168 ?>