2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
37 require_once('service/core/SoapHelperWebService.php');
38 class SugarWebServiceUtilv3 extends SoapHelperWebServices {
40 function get_name_value($field,$value)
42 if($value instanceof Link2 && !method_exists($value, '__toString'))
44 return array('name'=>$field, 'value'=>$value);
47 function filter_fields($value, $fields)
49 $GLOBALS['log']->info('Begin: SoapHelperWebServices->filter_fields');
50 global $invalid_contact_fields;
51 $filterFields = array();
52 foreach($fields as $field)
54 if (is_array($invalid_contact_fields))
56 if (in_array($field, $invalid_contact_fields))
61 if (isset($value->field_defs[$field]))
63 $var = $value->field_defs[$field];
64 if($var['type'] == 'link') continue;
65 if( isset($var['source'])
66 && ($var['source'] != 'db' && $var['source'] != 'custom_fields' && $var['source'] != 'non-db')
67 && $var['name'] != 'email1' && $var['name'] != 'email2'
68 && (!isset($var['type'])|| $var['type'] != 'relate')) {
70 if( $value->module_dir == 'Emails'
71 && (($var['name'] == 'description') || ($var['name'] == 'description_html') || ($var['name'] == 'from_addr_name')
72 || ($var['name'] == 'reply_to_addr') || ($var['name'] == 'to_addrs_names') || ($var['name'] == 'cc_addrs_names')
73 || ($var['name'] == 'bcc_addrs_names') || ($var['name'] == 'raw_source')))
83 $filterFields[] = $field;
85 $GLOBALS['log']->info('End: SoapHelperWebServices->filter_fields');
89 function getRelationshipResults($bean, $link_field_name, $link_module_fields, $optional_where = '', $order_by = '') {
90 $GLOBALS['log']->info('Begin: SoapHelperWebServices->getRelationshipResults');
91 require_once('include/TimeDate.php');
92 global $beanList, $beanFiles, $current_user;
93 global $disable_date_format, $timedate;
95 $bean->load_relationship($link_field_name);
96 if (isset($bean->$link_field_name)) {
97 //First get all the related beans
99 if (!empty($optional_where))
101 $params['where'] = $optional_where;
103 $related_beans = $bean->$link_field_name->getBeans($params);
104 //Create a list of field/value rows based on $link_module_fields
106 $filterFields = array();
107 if (!empty($order_by) && !empty($related_beans))
109 $related_beans = order_beans($related_beans, $order_by);
111 foreach($related_beans as $id => $bean)
113 if (empty($filterFields) && !empty($link_module_fields))
115 $filterFields = $this->filter_fields($bean, $link_module_fields);
118 foreach ($filterFields as $field) {
119 if (isset($bean->$field))
121 if (isset($bean->field_defs[$field]['type']) && $bean->field_defs[$field]['type'] == 'date') {
122 $row[$field] = $timedate->to_display_date_time($bean->$field);
124 $row[$field] = $bean->$field;
131 //Users can't see other user's hashes
132 if(is_a($bean, 'User') && $current_user->id != $bean->id && isset($row['user_hash'])) {
133 $row['user_hash'] = "";
137 $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults');
138 return array('rows' => $list, 'fields_set_on_rows' => $filterFields);
140 $GLOBALS['log']->info('End: SoapHelperWebServices->getRelationshipResults - ' . $link_field_name . ' relationship does not exists');
146 function get_field_list($value, $fields, $translate=true) {
148 $GLOBALS['log']->info('Begin: SoapHelperWebServices->get_field_list');
149 $module_fields = array();
150 $link_fields = array();
151 if(!empty($value->field_defs)){
153 foreach($value->field_defs as $var){
154 if(!empty($fields) && !in_array( $var['name'], $fields))continue;
155 if(isset($var['source']) && ($var['source'] != 'db' && $var['source'] != 'non-db' &&$var['source'] != 'custom_fields') && $var['name'] != 'email1' && $var['name'] != 'email2' && (!isset($var['type'])|| $var['type'] != 'relate'))continue;
156 if ((isset($var['source']) && $var['source'] == 'non_db') || (isset($var['type']) && $var['type'] == 'link')) {
160 $options_dom = array();
161 $options_ret = array();
162 // Apparently the only purpose of this check is to make sure we only return fields
163 // when we've read a record. Otherwise this function is identical to get_module_field_list
164 if( isset($var['required']) && ($var['required'] || $var['required'] == 'true' ) ){
168 if(isset($var['options'])){
169 $options_dom = translate($var['options'], $value->module_dir);
170 if(!is_array($options_dom)) $options_dom = array();
171 foreach($options_dom as $key=>$oneOption)
172 $options_ret[$key] = $this->get_name_value($key,$oneOption);
175 if(!empty($var['dbType']) && $var['type'] == 'bool') {
176 $options_ret['type'] = $this->get_name_value('type', $var['dbType']);
180 $entry['name'] = $var['name'];
181 $entry['type'] = $var['type'];
182 $entry['group'] = isset($var['group']) ? $var['group'] : '';
183 $entry['id_name'] = isset($var['id_name']) ? $var['id_name'] : '';
185 if ($var['type'] == 'link') {
186 $entry['relationship'] = (isset($var['relationship']) ? $var['relationship'] : '');
187 $entry['module'] = (isset($var['module']) ? $var['module'] : '');
188 $entry['bean_name'] = (isset($var['bean_name']) ? $var['bean_name'] : '');
189 $link_fields[$var['name']] = $entry;
192 $entry['label'] = isset($var['vname']) ? translate($var['vname'], $value->module_dir) : $var['name'];
194 $entry['label'] = isset($var['vname']) ? $var['vname'] : $var['name'];
196 $entry['required'] = $required;
197 $entry['options'] = $options_ret;
198 $entry['related_module'] = (isset($var['id_name']) && isset($var['module'])) ? $var['module'] : '';
199 if(isset($var['default'])) {
200 $entry['default_value'] = $var['default'];
202 $module_fields[$var['name']] = $entry;
207 if($value->module_dir == 'Bugs'){
208 require_once('modules/Releases/Release.php');
209 $seedRelease = new Release();
210 $options = $seedRelease->get_releases(TRUE, "Active");
211 $options_ret = array();
212 foreach($options as $name=>$value){
213 $options_ret[] = array('name'=> $name , 'value'=>$value);
215 if(isset($module_fields['fixed_in_release'])){
216 $module_fields['fixed_in_release']['type'] = 'enum';
217 $module_fields['fixed_in_release']['options'] = $options_ret;
219 if(isset($module_fields['release'])){
220 $module_fields['release']['type'] = 'enum';
221 $module_fields['release']['options'] = $options_ret;
223 if(isset($module_fields['release_name'])){
224 $module_fields['release_name']['type'] = 'enum';
225 $module_fields['release_name']['options'] = $options_ret;
229 if(isset($value->assigned_user_name) && isset($module_fields['assigned_user_id'])) {
230 $module_fields['assigned_user_name'] = $module_fields['assigned_user_id'];
231 $module_fields['assigned_user_name']['name'] = 'assigned_user_name';
233 if(isset($value->assigned_name) && isset($module_fields['team_id'])) {
234 $module_fields['team_name'] = $module_fields['team_id'];
235 $module_fields['team_name']['name'] = 'team_name';
237 if(isset($module_fields['modified_user_id'])) {
238 $module_fields['modified_by_name'] = $module_fields['modified_user_id'];
239 $module_fields['modified_by_name']['name'] = 'modified_by_name';
241 if(isset($module_fields['created_by'])) {
242 $module_fields['created_by_name'] = $module_fields['created_by'];
243 $module_fields['created_by_name']['name'] = 'created_by_name';
246 $GLOBALS['log']->info('End: SoapHelperWebServices->get_field_list');
247 return array('module_fields' => $module_fields, 'link_fields' => $link_fields);
250 function get_subpanel_defs($module, $type)
252 global $beanList, $beanFiles;
257 if (file_exists('custom/modules/'.$module.'/metadata/wireless.subpaneldefs.php'))
258 require_once('custom/modules/'.$module.'/metadata/wireless.subpaneldefs.php');
259 else if (file_exists('modules/'.$module.'/metadata/wireless.subpaneldefs.php'))
260 require_once('modules/'.$module.'/metadata/wireless.subpaneldefs.php');
265 if (file_exists ('modules/'.$module.'/metadata/subpaneldefs.php' ))
266 require ('modules/'.$module.'/metadata/subpaneldefs.php');
267 if ( file_exists('custom/modules/'.$module.'/Ext/Layoutdefs/layoutdefs.ext.php' ))
268 require ('custom/modules/'.$module.'/Ext/Layoutdefs/layoutdefs.ext.php');
271 //Filter results for permissions
272 foreach ($layout_defs[$module]['subpanel_setup'] as $subpanel => $subpaneldefs)
274 $moduleToCheck = $subpaneldefs['module'];
275 if(!isset($beanList[$moduleToCheck]))
277 $class_name = $beanList[$moduleToCheck];
278 $bean = new $class_name();
279 if($bean->ACLAccess('list'))
280 $results[$subpanel] = $subpaneldefs;
287 function get_module_view_defs($module_name, $type, $view){
288 require_once('include/MVC/View/SugarView.php');
289 $metadataFile = null;
291 $view = strtolower($view);
292 switch (strtolower($type)){
294 if( $view == 'list'){
295 require_once('include/SugarWireless/SugarWirelessListView.php');
296 $GLOBALS['module'] = $module_name; //WirelessView keys off global variable not instance variable...
297 $v = new SugarWirelessListView();
298 $results = $v->getMetaDataFile();
300 elseif ($view == 'subpanel')
301 $results = $this->get_subpanel_defs($module_name, $type);
303 require_once('include/SugarWireless/SugarWirelessView.php');
304 $v = new SugarWirelessView();
305 $v->module = $module_name;
306 $fullView = ucfirst($view) . 'View';
307 $meta = $v->getMetaDataFile('Wireless' . $fullView);
308 $metadataFile = $meta['filename'];
309 require_once($metadataFile);
310 //Wireless detail metadata may actually be just edit metadata.
311 $results = isset($viewdefs[$meta['module_name']][$fullView] ) ? $viewdefs[$meta['module_name']][$fullView] : $viewdefs[$meta['module_name']]['EditView'];
317 if ($view == 'subpanel')
318 $results = $this->get_subpanel_defs($module_name, $type);
321 $v = new SugarView(null,array());
322 $v->module = $module_name;
324 $fullView = ucfirst($view) . 'View';
325 $metadataFile = $v->getMetaDataFile();
326 require_once($metadataFile);
328 $results = $listViewDefs[$module_name];
330 $results = $viewdefs[$module_name][$fullView];
338 * Examine the wireless_module_registry to determine which modules have been enabled for the mobile view.
340 * @param array $availModules An array of all the modules the user already has access to.
341 * @return array Modules enalbed for mobile view.
343 function get_visible_mobile_modules($availModules){
344 $enabled_modules = array();
345 $availModulesKey = array_flip($availModules);
346 foreach ( array ( '','custom/') as $prefix)
348 if(file_exists($prefix.'include/MVC/Controller/wireless_module_registry.php'))
349 require $prefix.'include/MVC/Controller/wireless_module_registry.php' ;
352 foreach ( $wireless_module_registry as $e => $def )
354 if( isset($availModulesKey[$e]) )
355 $enabled_modules[] = $e;
358 return $enabled_modules;
362 * Examine the application to determine which modules have been enabled..
364 * @param array $availModules An array of all the modules the user already has access to.
365 * @return array Modules enabled within the application.
367 function get_visible_modules($availModules) {
368 require_once("modules/MySettings/TabController.php");
369 $controller = new TabController();
370 $tabs = $controller->get_tabs_system();
371 $enabled_modules= array();
372 $availModulesKey = array_flip($availModules);
373 foreach ($tabs[0] as $key=>$value)
375 if( isset($availModulesKey[$key]) )
376 $enabled_modules[] = $key;
379 return $enabled_modules;
383 * Retrieve all of the upcoming activities for a particular user.
387 function get_upcoming_activities()
392 $activityModules = array('Meetings' => array('date_field' => 'date_start','status' => 'Planned','status_field' => 'status', 'status_opp' => '='),
393 'Calls' => array('date_field' => 'date_start','status' => 'Planned','status_field' => 'status', 'status_opp' => '='),
394 'Tasks' => array('date_field' =>'date_due','status' => 'Not Started','status_field' => 'status','status_opp' => '='),
395 'Opportunities' => array('date_field' => 'date_closed','status' => array('Closed Won','Closed Lost'), 'status_field' => 'sales_stage', 'status_opp' => '!=') );
397 foreach ($activityModules as $module => $meta)
399 if(!self::check_modules_access($GLOBALS['current_user'], $module, 'read'))
401 $GLOBALS['log']->debug("SugarWebServiceImpl->get_last_viewed: NO ACCESS to $module");
405 $class_name = $beanList[$module];
406 $seed = new $class_name();
407 $query = $this->generateUpcomingActivitiesWhereClause($seed, $meta);
409 $response = $seed->get_list(/* Order by date field */"{$meta['date_field']} ASC", /*Where clause */$query, /* No Offset */ 0,
410 /* No limit */-1, /* Max 10 items */10, /*No Deleted */ 0 );
414 if( isset($response['list']) )
415 $result = $this->format_upcoming_activities_entries($response['list'],$meta['date_field']);
417 $results = array_merge($results,$result);
420 //Sort the result list by the date due flag in ascending order
421 usort( $results, array( $this , "cmp_datedue" ) ) ;
423 //Only return a subset of the results.
424 $results = array_slice($results, 0, $maxCount);
429 function generateUpcomingActivitiesWhereClause($seed,$meta)
432 $query_date = TimeDate::getInstance()->nowDb();
433 $query[] = " {$seed->table_name}.{$meta['date_field']} > '$query_date'"; //Add date filter
434 $query[] = "{$seed->table_name}.assigned_user_id = '{$GLOBALS['current_user']->id}' "; //Add assigned user filter
435 if(is_array($meta['status_field']))
437 foreach ($meta['status'] as $field)
438 $query[] = "{$seed->table_name}.{$meta['status_field']} {$meta['status_opp']} '".$GLOBALS['db']->quote($field)."' ";
441 $query[] = "{$seed->table_name}.{$meta['status_field']} {$meta['status_opp']} '".$GLOBALS['db']->quote($meta['status'])."' ";
443 return implode(" AND ",$query);
446 * Given a list of bean entries, format the expected response.
448 * @param array $list An array containing a bean list.
449 * @param string $date_field Name of the field storing the date field we are examining
450 * @return array The results.
452 function format_upcoming_activities_entries($list,$date_field)
455 foreach ($list as $bean)
457 $results[] = array('id' => $bean->id, 'module' => $bean->module_dir,'date_due' => $bean->$date_field,
458 'summary' => $bean->get_summary_text() );
465 * Sort the array for upcoming activities based on the date due flag ascending.
469 * @return int Indicates equality for date due flag
471 static function cmp_datedue( $a, $b )
473 $a_date = strtotime( $a['date_due'] ) ;
474 $b_date = strtotime( $b['date_due'] ) ;
476 if( $a_date == $b_date ) return 0 ;
477 return ($a_date > $b_date ) ? 1 : -1;