]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/json_config.php
Release 6.5.0
[Github/sugarcrm.git] / include / json_config.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 /*********************************************************************************
39
40  * Description:  This class is used to include the json server config inline. Previous method
41  * of using <script src=json_server.php></script> causes multiple server hits per page load
42  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43  * All Rights Reserved.
44  * Contributor(s): ______________________________________..
45  ********************************************************************************/
46
47 global $app_strings, $json;
48 $json = getJSONobj();
49
50 class json_config {
51         var $global_registry_var_name = 'GLOBAL_REGISTRY';
52
53         function get_static_json_server($configOnly = true, $getStrings = false, $module = null, $record = null, $scheduler = false) {
54                 global $current_user;
55                 $str = '';
56                 $str .= $this->getAppMetaJSON($scheduler);
57                 if(!$configOnly) {
58                         $str .= $this->getFocusData($module, $record);
59                         if($getStrings) $str .= $this->getStringsJSON($module);
60                 }
61                 $str .= $this->getUserConfigJSON();
62
63                 return $str;
64         }
65
66         function getAppMetaJSON($scheduler = false) {
67
68                 global $json, $sugar_config;
69
70                 $str = "\nvar ". $this->global_registry_var_name." = new Object();\n";
71                 $str .= "\n".$this->global_registry_var_name.".config = {\"site_url\":\"".getJavascriptSiteURL()."\"};\n";
72
73                 $str .= $this->global_registry_var_name.".meta = new Object();\n";
74                 $str .= $this->global_registry_var_name.".meta.modules = new Object();\n";
75
76                 /*
77                 $modules_arr = array('Meetings','Calls');
78                 $meta_modules = array();
79
80                 global $beanFiles,$beanList;
81                 //header('Content-type: text/xml');
82                 foreach($modules_arr as $module) {
83                         require_once($beanFiles[$beanList[$module]]);
84                         $focus = new $beanList[$module];
85                         $meta_modules[$module] = array();
86                         $meta_modules[$module]['field_defs'] = $focus->field_defs;
87                 }
88
89                 $str .= $this->global_registry_var_name.".meta.modules.Meetings = ". $json->encode($meta_modules['Meetings'])."\n";
90                 $str .= $this->global_registry_var_name.".meta.modules.Calls = ". $json->encode($meta_modules['Calls'])."\n";
91                 */
92                 return $str;
93         }
94
95         function getUserConfigJSON() {
96                 global $timedate;
97                 global $current_user, $sugar_config;
98                 $json = getJSONobj();
99                 if(isset($_SESSION['authenticated_user_theme']) && $_SESSION['authenticated_user_theme'] != '') {
100                         $theme = $_SESSION['authenticated_user_theme'];
101                 }
102                 else {
103                         $theme = $sugar_config['default_theme'];
104                 }
105                 $user_arr = array();
106                 $user_arr['theme'] = $theme;
107                 $user_arr['fields'] = array();
108                 $user_arr['module'] = 'User';
109                 $user_arr['fields']['id'] = $current_user->id;
110                 $user_arr['fields']['user_name'] = $current_user->user_name;
111                 $user_arr['fields']['first_name'] = $current_user->first_name;
112                 $user_arr['fields']['last_name'] = $current_user->last_name;
113                 $user_arr['fields']['full_name'] = $current_user->full_name;
114                 $user_arr['fields']['email'] = $current_user->email1;
115                 $user_arr['fields']['gmt_offset'] = $timedate->getUserUTCOffset();
116                 $user_arr['fields']['date_time_format'] = $current_user->getUserDateTimePreferences();
117                 $str = "\n".$this->global_registry_var_name.".current_user = ".$json->encode($user_arr).";\n";
118                 return $str;
119         }
120
121         function getFocusData($module, $record) {
122                 global $json;
123                 if (empty($module)) {
124                         return '';
125                 }
126                 else if(empty($record)) {
127                         return "\n".$this->global_registry_var_name.'["focus"] = {"module":"'.$module.'",users_arr:[],fields:{"id":"-1"}}'."\n";
128                 }
129
130                 $module_arr = $this->meeting_retrieve($module, $record);
131                 return "\n".$this->global_registry_var_name."['focus'] = ". $json->encode($module_arr).";\n";
132         }
133
134         function meeting_retrieve($module, $record) {
135                 global $json, $response;
136                 global $beanFiles, $beanList;
137                 require_once($beanFiles[$beanList[$module]]);
138                 $focus = new $beanList[$module];
139
140                 if(empty($module) || empty($record)) {
141                         return '';
142                 }
143
144                 $focus->retrieve($record);
145                 $module_arr = $this->populateBean($focus);
146
147                 if($module == 'Meetings') {
148                         $users = $focus->get_meeting_users();
149                 }
150                 else if ( $module == 'Calls') {
151                         $users = $focus->get_call_users();
152                 }
153
154                 $module_arr['users_arr'] = array();
155
156                 foreach($users as $user) {
157                         array_push($module_arr['users_arr'],  $this->populateBean($user));
158                 }
159
160                 $module_arr['orig_users_arr_hash'] = array();
161
162                 foreach($users as $user) {
163                         $module_arr['orig_users_arr_hash'][$user->id] = '1';
164                 }
165
166                 $module_arr['contacts_arr'] = array();
167
168                 $focus->load_relationships('contacts');
169                 $contacts=$focus->get_linked_beans('contacts','Contact');
170                 foreach($contacts as $contact) {
171                         array_push($module_arr['users_arr'], $this->populateBean($contact));
172                 }
173                 $module_arr['leads_arr'] = array();
174         $focus->load_relationships('leads');
175                 $leads=$focus->get_linked_beans('leads','Lead');
176                 foreach($leads as $lead) {
177                         array_push($module_arr['users_arr'], $this->populateBean($lead));
178                 }
179
180                 return $module_arr;
181         }
182
183         function getStringsJSON($module) {
184           global $current_language;
185           $currentModule = 'Calendar';
186           $mod_list_strings = return_mod_list_strings_language($current_language,$currentModule);
187
188           global $json;
189           $str = "\n".$this->global_registry_var_name."['calendar_strings'] =  {\"dom_cal_month_long\":". $json->encode($mod_list_strings['dom_cal_month_long']).",\"dom_cal_weekdays_long\":". $json->encode($mod_list_strings['dom_cal_weekdays_long'])."}\n";
190           if(empty($module)) {
191                 $module = 'Home';
192           }
193           $currentModule = $module;
194           $mod_strings = return_module_language($current_language,$currentModule);
195           return  $str . "\n".$this->global_registry_var_name."['meeting_strings'] =  ". $json->encode($mod_strings)."\n";
196         }
197
198         // HAS MEETING SPECIFIC CODE:
199         function populateBean(&$focus) {
200                 require_once('include/utils/db_utils.php');
201                 $all_fields = $focus->column_fields;
202                 // MEETING SPECIFIC
203                 $all_fields = array_merge($all_fields,array('required','accept_status','name')); // need name field for contacts and users
204                 //$all_fields = array_merge($focus->column_fields,$focus->additional_column_fields);
205
206                 $module_arr = array();
207
208                 $module_arr['module'] = $focus->object_name;
209
210                 $module_arr['fields'] = array();
211
212                 foreach($all_fields as $field) {
213                         if(isset($focus->$field) && !is_object($focus->$field)) {
214                                 $focus->$field =  from_html($focus->$field);
215                                 $focus->$field =  preg_replace("/\r\n/","<BR>",$focus->$field);
216                                 $focus->$field =  preg_replace("/\n/","<BR>",$focus->$field);
217                                 $module_arr['fields'][$field] = $focus->$field;
218                         }
219                 }
220                         $GLOBALS['log']->debug("JSON_SERVER:populate bean:");
221                         return $module_arr;
222                 }
223         }
224 ?>