]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/v3/SugarWebServiceImplv3.php
Release 6.1.4
[Github/sugarcrm.git] / service / v3 / SugarWebServiceImplv3.php
1 <?php
2 if(!defined('sugarEntry'))define('sugarEntry', true);
3 /*********************************************************************************
4  * SugarCRM 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
39 /**
40  * This class is an implemenatation class for all the rest services
41  */
42 require_once('service/core/SugarWebServiceImpl.php');
43 require_once('SugarWebServiceUtilv3.php');
44
45
46 class SugarWebServiceImplv3 extends SugarWebServiceImpl {
47
48     public function __construct()
49     {
50         self::$helperObject = new SugarWebServiceUtilv3();
51     }
52     
53     
54     /**
55      * Log the user into the application
56      *
57      * @param UserAuth array $user_auth -- Set user_name and password (password needs to be
58      *      in the right encoding for the type of authentication the user is setup for.  For Base
59      *      sugar validation, password is the MD5 sum of the plain text password.
60      * @param String $application -- The name of the application you are logging in from.  (Currently unused).
61      * @param array $name_value_list -- Array of name value pair of extra parameters. As of today only 'language' and 'notifyonsave' is supported
62      * @return Array - id - String id is the session_id of the session that was created.
63      *                           - module_name - String - module name of user
64      *                           - name_value_list - Array - The name value pair of user_id, user_name, user_language, user_currency_id, user_currency_name, 
65      *                                         - user_default_team_id, user_is_admin, user_default_dateformat, user_default_timeformat
66      * @exception 'SoapFault' -- The SOAP error, if any
67      */
68     public function login($user_auth, $application, $name_value_list){
69         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->login');
70         global $sugar_config, $system_config;
71         $error = new SoapError();
72         $user = new User();
73         $success = false;
74         if(!empty($user_auth['encryption']) && $user_auth['encryption'] === 'PLAIN')
75         {
76             $user_auth['password'] = md5($user_auth['password']);
77         }
78         //rrs
79         $system_config = new Administration();
80         $system_config->retrieveSettings('system');
81         $authController = new AuthenticationController((!empty($sugar_config['authenticationClass'])? $sugar_config['authenticationClass'] : 'SugarAuthenticate'));
82         //rrs
83         $isLoginSuccess = $authController->login($user_auth['user_name'], $user_auth['password'], array('passwordEncrypted' => true));
84         $usr_id=$user->retrieve_user_id($user_auth['user_name']);
85         if($usr_id) 
86             $user->retrieve($usr_id);
87         
88         if ($isLoginSuccess) 
89         {
90             if ($_SESSION['hasExpiredPassword'] =='1') 
91             {
92                 $error->set_error('password_expired');
93                 $GLOBALS['log']->fatal('password expired for user ' . $user_auth['user_name']);
94                 LogicHook::initialize();
95                 $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
96                 self::$helperObject->setFaultObject($error);
97                 return;
98             } 
99             if(!empty($user) && !empty($user->id) && !$user->is_group) 
100             {
101                 $success = true;
102                 global $current_user;
103                 $current_user = $user;
104             } 
105         } 
106         else if($usr_id && isset($user->user_name) && ($user->getPreference('lockout') == '1')) 
107         {
108             $error->set_error('lockout_reached');
109             $GLOBALS['log']->fatal('Lockout reached for user ' . $user_auth['user_name']);
110             LogicHook::initialize();
111             $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
112             self::$helperObject->setFaultObject($error);
113             return;
114         } 
115         else if(function_exists('mcrypt_cbc'))
116         {
117             $password = self::$helperObject->decrypt_string($user_auth['password']);
118             if($authController->login($user_auth['user_name'], $password) && isset($_SESSION['authenticated_user_id']))
119                 $success = true;
120         }
121
122         if($success)
123         {
124             session_start();
125             global $current_user;
126             //$current_user = $user;
127             self::$helperObject->login_success($name_value_list);
128             $current_user->loadPreferences();
129             $_SESSION['is_valid_session']= true;
130             $_SESSION['ip_address'] = query_client_ip();
131             $_SESSION['user_id'] = $current_user->id;
132             $_SESSION['type'] = 'user';
133             $_SESSION['avail_modules']= self::$helperObject->get_user_module_list($current_user);
134             $_SESSION['authenticated_user_id'] = $current_user->id;
135             $_SESSION['unique_key'] = $sugar_config['unique_key'];
136             $current_user->call_custom_logic('after_login');
137             $GLOBALS['log']->info('End: SugarWebServiceImpl->login - succesful login');
138             $nameValueArray = array();
139             global $current_language;
140             $nameValueArray['user_id'] = self::$helperObject->get_name_value('user_id', $current_user->id);
141             $nameValueArray['user_name'] = self::$helperObject->get_name_value('user_name', $current_user->user_name);
142             $nameValueArray['user_language'] = self::$helperObject->get_name_value('user_language', $current_language);
143             $cur_id = $current_user->getPreference('currency');
144             $nameValueArray['user_currency_id'] = self::$helperObject->get_name_value('user_currency_id', $cur_id);
145             $nameValueArray['user_is_admin'] = self::$helperObject->get_name_value('user_is_admin', is_admin($current_user));
146             $nameValueArray['user_default_team_id'] = self::$helperObject->get_name_value('user_default_team_id', $current_user->default_team );
147             $nameValueArray['user_default_dateformat'] = self::$helperObject->get_name_value('user_default_dateformat', $current_user->getPreference('datef') );
148             $nameValueArray['user_default_timeformat'] = self::$helperObject->get_name_value('user_default_timeformat', $current_user->getPreference('timef') );
149             $currencyObject = new Currency();
150             $currencyObject->retrieve($cur_id);
151             $nameValueArray['user_currency_name'] = self::$helperObject->get_name_value('user_currency_name', $currencyObject->name);
152             $_SESSION['user_language'] = $current_language;
153             return array('id'=>session_id(), 'module_name'=>'Users', 'name_value_list'=>$nameValueArray);
154         }
155         LogicHook::initialize();
156         $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
157         $error->set_error('invalid_login');
158         self::$helperObject->setFaultObject($error);
159         $GLOBALS['log']->info('End: SugarWebServiceImpl->login - failed login');
160     }
161     
162     /**
163      * Retrieve the md5 hash of the vardef entries for a particular module.
164      *
165      * @param String $session -- Session ID returned by a previous call to login.
166      * @param String $module_name -- The name of the module to return records from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
167      * @return String The md5 hash of the vardef definition.
168      * @exception 'SoapFault' -- The SOAP error, if any
169      */
170     function get_module_fields_md5($session, $module_name){
171         
172         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_module_fields_md5');
173         
174         $results = array();
175         if( is_array($module_name) )
176         {
177             foreach ($module_name as $module)
178                 $results[$module] = md5(serialize(self::get_module_fields($session, $module)));
179         }
180         else 
181             $results[$module_name] = md5(serialize(self::get_module_fields($session, $module_name)));
182         
183         return $results;
184         
185         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_fields_md5');
186     }
187     
188     /**
189      * Gets server info. This will return information like version, flavor and gmt_time.
190      * @return Array - flavor - String - Retrieve the specific flavor of sugar.
191      *                           - version - String - Retrieve the version number of Sugar that the server is running.
192      *                           - gmt_time - String - Return the current time on the server in the format 'Y-m-d H:i:s'. This time is in GMT.
193      * @exception 'SoapFault' -- The SOAP error, if any
194      */
195     function get_server_info(){
196         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_server_info');
197         require_once('sugar_version.php');
198         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_server_info');
199         
200         return array('flavor' => $GLOBALS['sugar_flavor'], 'version' => $GLOBALS['sugar_version'], 'gmt_time' => gmdate('Y-m-d H:i:s'));
201     } // fn
202
203     /**
204      * Retrieve the layout metadata for a given module given a specific type and view.
205      *
206      * @param String $session -- Session ID returned by a previous call to login.
207      * @param array $module_name(s) -- The name of the module(s) to return records from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
208      * @return array $type The type(s) of views requested.  Current supported types are 'default' (for application) and 'wireless'
209      * @return array $view The view(s) requested.  Current supported types are edit, detail, list, and subpanel.
210      * @exception 'SoapFault' -- The SOAP error, if any
211      */
212     function get_module_layout($session, $a_module_names, $a_type, $a_view,$md5 = FALSE){
213         $GLOBALS['log']->fatal('Begin: SugarWebServiceImpl->get_module_layout');
214     
215         global  $beanList, $beanFiles;
216         $error = new SoapError();
217         $results = array();
218         foreach ($a_module_names as $module_name)
219         {
220             if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error))
221             {
222                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout');
223                 continue;
224             }
225
226             $class_name = $beanList[$module_name];
227             require_once($beanFiles[$class_name]);
228             $seed = new $class_name();
229
230             foreach ($a_view as $view)
231             {
232                 $aclViewCheck = (strtolower($view) == 'subpanel') ? 'DetailView' : ucfirst(strtolower($view)) . 'View';
233                 if($seed->ACLAccess($aclViewCheck, true) )
234                 {
235                     foreach ($a_type as $type)
236                     {
237                         $a_vardefs = self::$helperObject->get_module_view_defs($module_name, $type, $view);
238                         if($md5)
239                             $results[$module_name][$type][$view] = md5(serialize($a_vardefs));
240                         else
241                             $results[$module_name][$type][$view] = $a_vardefs;
242                     }
243                 }
244             }
245         }
246          
247         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout');
248         
249         return $results;
250     }
251     
252     /**
253      * Retrieve the md5 hash of a layout metadata for a given module given a specific type and view.
254      *
255      * @param String $session -- Session ID returned by a previous call to login.
256      * @param array $module_name(s) -- The name of the module to return records from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
257      * @return array $type(s) The type of view requested.  Current supported types are 'default' (for application) and 'wireless'
258      * @return array $view(s) The view requested.  Current supported types are edit, detail, and list.
259      * @exception 'SoapFault' -- The SOAP error, if any
260      */
261     function get_module_layout_md5($session, $module_name, $type, $view){
262         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_module_layout_md5');
263         $results = self::get_module_layout($session, $module_name, $type, $view, TRUE);
264             return array('md5'=> $results);
265         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout_md5');
266     }
267     
268     /**
269      * Retrieve the list of available modules on the system available to the currently logged in user.
270      *
271      * @param String $session -- Session ID returned by a previous call to login.
272      * @param String $filter --  Valid values are: all     - Return all modules,
273      *                                             default - Return all visible modules for the application 
274      *                                             mobile  - Return all visible modules for the mobile view
275      * @return Array    'modules' -- Array - An array of module names
276      * @exception 'SoapFault' -- The SOAP error, if any
277      */
278     function get_available_modules($session,$filter='all'){
279         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_available_modules');
280     
281         $error = new SoapError();
282         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
283                 $error->set_error('invalid_login');
284                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
285                 return;
286         } // if
287     
288         $modules = array();
289         $availModules = array_keys($_SESSION['avail_modules']); //ACL check already performed.
290         switch ($filter){
291             case 'default':
292                 $modules = self::$helperObject->get_visible_modules($availModules);
293                break;
294             case 'mobile':
295                 $modules = self::$helperObject->get_visible_mobile_modules($availModules);
296                 break;
297             case 'all':
298             default:
299                 $modules = $availModules;
300         }
301         
302         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
303         return array('modules'=> $modules);
304     } // fn
305     
306     /**
307      * Retrieve a list of recently viewed records by module.  
308      *
309      * @param String $session -- Session ID returned by a previous call to login.
310      * @param String $modules -- An array of modules or 'Home' to indicate all.
311      * @return Array The recently viewed records
312      * @exception 'SoapFault' -- The SOAP error, if any
313      */
314     function get_last_viewed($session, $module_names)
315     {
316         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_last_viewed');
317         $error = new SoapError();
318         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) 
319         {
320                 $error->set_error('invalid_login');
321                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_last_viewed');
322                 return;
323         } // if
324         
325         $results = array();
326         foreach ($module_names as $module )
327         {
328             if(!self::$helperObject->check_modules_access($GLOBALS['current_user'], $module, 'read'))
329             {
330                 $GLOBALS['log']->debug("SugarWebServiceImpl->get_last_viewed: NO ACCESS to $module");
331                 continue;
332             } 
333              
334             if($module == 'Home') $module = '';   
335             $tracker = new Tracker();
336             $entryList = $tracker->get_recently_viewed($GLOBALS['current_user']->id, $module);
337             foreach ($entryList as $entry)
338                 $results[] = $entry;
339         }
340
341         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_last_viewed');
342         return $results;
343     }
344     
345     /**
346      * Retrieve a list of upcoming activities including Calls, Meetings,Tasks and Opportunities
347      *
348      * @param String $session -- Session ID returned by a previous call to login.
349      * @return Array List of upcoming activities
350      * @exception 'SoapFault' -- The SOAP error, if any
351      */
352     function get_upcoming_activities($session)
353     {
354         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_upcoming_activities');
355         $error = new SoapError();
356         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) 
357         {
358                 $error->set_error('invalid_login');
359                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_upcoming_activities');
360                 return;
361         } // if
362         
363         $results = self::$helperObject->get_upcoming_activities();
364
365         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_upcoming_activities');
366     
367         return $results;
368     }
369     
370     /**
371      * Given a list of modules to search and a search string, return the id, module_name, along with the fields
372      * We will support Accounts, Bug Tracker, Cases, Contacts, Leads, Opportunities, Project, ProjectTask, Quotes
373      *
374      * @param string $session                   - Session ID returned by a previous call to login.
375      * @param string $search_string     - string to search
376      * @param string[] $modules                 - array of modules to query
377      * @param int $offset                               - a specified offset in the query
378      * @param int $max_results                  - max number of records to return
379      * @param string $assigned_user_id  - a user id to filter all records by, leave empty to exclude the filter
380      * @param string[] $select_fields   - An array of fields to return.  If empty the default return fields will be from the active list view defs.
381      * @return Array return_search_result       - Array('Accounts' => array(array('name' => 'first_name', 'value' => 'John', 'name' => 'last_name', 'value' => 'Do')))
382      * @exception 'SoapFault' -- The SOAP error, if any
383      */
384     function search_by_module($session, $search_string, $modules, $offset, $max_results,$assigned_user_id = '', $select_fields = array()){
385         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->search_by_module');
386         global  $beanList, $beanFiles;
387         global $sugar_config,$current_language;
388     
389         $error = new SoapError();
390         $output_list = array();
391         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
392                 $error->set_error('invalid_login');
393                 $GLOBALS['log']->info('End: SugarWebServiceImpl->search_by_module');
394                 return;
395         }
396         global $current_user;
397         if($max_results > 0){
398                 $sugar_config['list_max_entries_per_page'] = $max_results;
399         }
400     
401         require_once('modules/Home/UnifiedSearchAdvanced.php');
402         require_once 'include/utils.php';
403         $usa = new UnifiedSearchAdvanced();
404         if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php')) {
405             $usa->buildCache();
406         }
407     
408         include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
409         $modules_to_search = array();
410         $unified_search_modules['Users'] =   array('fields' => array());
411         
412         $unified_search_modules['ProjectTask'] =   array('fields' => array());
413         
414         foreach($unified_search_modules as $module=>$data) {
415                 if (in_array($module, $modules)) {
416                 $modules_to_search[$module] = $beanList[$module];
417                 } // if
418         } // foreach
419     
420         $GLOBALS['log']->info('SugarWebServiceImpl->search_by_module - search string = ' . $search_string);
421     
422         if(!empty($search_string) && isset($search_string)) {
423                 $search_string = trim($GLOBALS['db']->quote(securexss(from_html(clean_string($search_string, 'UNIFIED_SEARCH')))));
424                 foreach($modules_to_search as $name => $beanName) {
425                         $where_clauses_array = array();
426                         $unifiedSearchFields = array () ;
427                         foreach ($unified_search_modules[$name]['fields'] as $field=>$def ) {
428                                 $unifiedSearchFields[$name] [ $field ] = $def ;
429                                 $unifiedSearchFields[$name] [ $field ]['value'] = $search_string;
430                         }
431     
432                         require_once $beanFiles[$beanName] ;
433                         $seed = new $beanName();
434                         require_once 'include/SearchForm/SearchForm2.php' ;
435                         if ($beanName == "User" 
436                             || $beanName == "ProjectTask"
437                             ) {
438                                 if(!self::$helperObject->check_modules_access($current_user, $seed->module_dir, 'read')){
439                                         continue;
440                                 } // if
441                                 if(!$seed->ACLAccess('ListView')) {
442                                         continue;
443                                 } // if
444                         }
445     
446                         if ($beanName != "User" 
447                             && $beanName != "ProjectTask"
448                             ) {
449                                 $searchForm = new SearchForm ($seed, $name ) ;
450     
451                                 $searchForm->setup(array ($name => array()) ,$unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
452                                 $where_clauses = $searchForm->generateSearchWhere() ;
453                                 require_once 'include/SearchForm/SearchForm2.php' ;
454                                 $searchForm = new SearchForm ($seed, $name ) ;
455     
456                                 $searchForm->setup(array ($name => array()) ,$unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
457                                 $where_clauses = $searchForm->generateSearchWhere() ;
458                                 $emailQuery = false;
459     
460                                 $where = '';
461                                 if (count($where_clauses) > 0 ) {
462                                         $where = '('. implode(' ) OR ( ', $where_clauses) . ')';
463                                 }
464                     
465                                 $mod_strings = return_module_language($current_language, $seed->module_dir);
466                                 
467                                 if(count($select_fields) > 0) 
468                                     $filterFields = $select_fields;
469                                 else {
470                                     if(file_exists('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php'))
471                                            require_once('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
472                                         else
473                                                 require_once('modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
474                                         
475                                         $filterFields = array();
476                                         foreach($listViewDefs[$seed->module_dir] as $colName => $param) {
477                                 if(!empty($param['default']) && $param['default'] == true) 
478                                     $filterFields[] = strtolower($colName);
479                             } 
480                             if (!in_array('id', $filterFields))
481                                 $filterFields[] = 'id';
482                                 }
483                                 
484                                 //Pull in any db fields used for the unified search query so the correct joins will be added
485                                 $selectOnlyQueryFields = array();
486                                 foreach ($unifiedSearchFields[$name] as $field => $def){
487                                     if( isset($def['db_field']) && !in_array($field,$filterFields) ){
488                                         $filterFields[] = $field;
489                                         $selectOnlyQueryFields[] = $field;
490                                     }
491                                 }
492
493                     //Add the assigned user filter if applicable
494                     if (!empty($assigned_user_id) && isset( $seed->field_defs['assigned_user_id']) ) {
495                        $ownerWhere = $seed->getOwnerWhere($assigned_user_id);
496                        $where = "($where) AND $ownerWhere";
497                     }
498
499                                 $ret_array = $seed->create_new_list_query('', $where, $filterFields, array(), 0, '', true, $seed, true);
500                         if(empty($params) or !is_array($params)) $params = array();
501                         if(!isset($params['custom_select'])) $params['custom_select'] = '';
502                         if(!isset($params['custom_from'])) $params['custom_from'] = '';
503                         if(!isset($params['custom_where'])) $params['custom_where'] = '';
504                         if(!isset($params['custom_order_by'])) $params['custom_order_by'] = '';
505                                 $main_query = $ret_array['select'] . $params['custom_select'] . $ret_array['from'] . $params['custom_from'] . $ret_array['where'] . $params['custom_where'] . $ret_array['order_by'] . $params['custom_order_by'];
506                         } else {
507                                 if ($beanName == "User") {
508                                         $filterFields = array('id', 'user_name', 'first_name', 'last_name', 'email_address');
509                                         $main_query = "select users.id, ea.email_address, users.user_name, first_name, last_name from users ";
510                                         $main_query = $main_query . " LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_module = '{$seed->module_dir}'
511     LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) ";
512                                         $main_query = $main_query . "where ((users.first_name like '{$search_string}') or (users.last_name like '{$search_string}') or (users.user_name like '{$search_string}') or (ea.email_address like '{$search_string}')) and users.deleted = 0 and users.is_group = 0 and users.employee_status = 'Active'";
513                                 } // if
514                                 if ($beanName == "ProjectTask") {
515                                         $filterFields = array('id', 'name', 'project_id', 'project_name');
516                                         $main_query = "select {$seed->table_name}.project_task_id id,{$seed->table_name}.project_id, {$seed->table_name}.name, project.name project_name from {$seed->table_name} ";
517                                         $seed->add_team_security_where_clause($main_query);
518                                         $main_query .= "LEFT JOIN teams ON $seed->table_name.team_id=teams.id AND (teams.deleted=0) ";
519                             $main_query .= "LEFT JOIN project ON $seed->table_name.project_id = project.id ";
520                             $main_query .= "where {$seed->table_name}.name like '{$search_string}%'";
521                                 } // if
522                         } // else
523
524                         $GLOBALS['log']->info('SugarWebServiceImpl->search_by_module - query = ' . $main_query);
525                         if($max_results < -1) {
526                                 $result = $seed->db->query($main_query);
527                         }
528                         else {
529                                 if($max_results == -1) {
530                                         $limit = $sugar_config['list_max_entries_per_page'];
531                     } else {
532                         $limit = $max_results;
533                     }
534                     $result = $seed->db->limitQuery($main_query, $offset, $limit + 1);
535                         }
536     
537                         $rowArray = array();
538                         while($row = $seed->db->fetchByAssoc($result)) {
539                                 $nameValueArray = array();
540                                 foreach ($filterFields as $field) {
541                                     if(in_array($field, $selectOnlyQueryFields))
542                                         continue;
543                                         $nameValue = array();
544                                         if (isset($row[$field])) {
545                                                 $nameValueArray[$field] = self::$helperObject->get_name_value($field, $row[$field]);
546                                         } // if
547                                 } // foreach
548                                 $rowArray[] = $nameValueArray;
549                         } // while
550                         $output_list[] = array('name' => $name, 'records' => $rowArray);
551                 } // foreach
552     
553         $GLOBALS['log']->info('End: SugarWebServiceImpl->search_by_module');
554         return array('entry_list'=>$output_list);
555         } // if
556         return array('entry_list'=>$output_list);
557     } // fn
558     
559     /**
560      * Retrieve a collection of beans that are related to the specified bean and optionally return relationship data for those related beans.
561      * So in this API you can get contacts info for an account and also return all those contact's email address or an opportunity info also.
562      *
563      * @param String $session -- Session ID returned by a previous call to login.
564      * @param String $module_name -- The name of the module that the primary record is from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
565      * @param String $module_id -- The ID of the bean in the specified module
566      * @param String $link_field_name -- The name of the lnk field to return records from.  This name should be the name the relationship.
567      * @param String $related_module_query -- A portion of the where clause of the SQL statement to find the related items.  The SQL query will already be filtered to only include the beans that are related to the specified bean.
568      * @param Array $related_fields - Array of related bean fields to be returned.
569      * @param Array $related_module_link_name_to_fields_array - For every related bean returrned, specify link fields name to fields info for that bean to be returned. For ex.'link_name_to_fields_array' => array(array('name' =>  'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address'))).
570      * @param Number $deleted -- false if deleted records should not be include, true if deleted records should be included.
571      * @param String $order_by -- field to order the result sets by
572      * @return Array 'entry_list' -- Array - The records that were retrieved
573      *                   'relationship_list' -- Array - The records link field data. The example is if asked about accounts contacts email address then return data would look like Array ( [0] => Array ( [name] => email_addresses [records] => Array ( [0] => Array ( [0] => Array ( [name] => id [value] => 3fb16797-8d90-0a94-ac12-490b63a6be67 ) [1] => Array ( [name] => email_address [value] => hr.kid.qa@example.com ) [2] => Array ( [name] => opt_out [value] => 0 ) [3] => Array ( [name] => primary_address [value] => 1 ) ) [1] => Array ( [0] => Array ( [name] => id [value] => 403f8da1-214b-6a88-9cef-490b63d43566 ) [1] => Array ( [name] => email_address [value] => kid.hr@example.name ) [2] => Array ( [name] => opt_out [value] => 0 ) [3] => Array ( [name] => primary_address [value] => 0 ) ) ) ) )
574     * @exception 'SoapFault' -- The SOAP error, if any
575     */
576     function get_relationships($session, $module_name, $module_id, $link_field_name, $related_module_query, $related_fields, $related_module_link_name_to_fields_array, $deleted, $order_by = ''){
577     
578         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_relationships');
579         global  $beanList, $beanFiles;
580         $error = new SoapError();
581         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
582                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
583                 return;
584         } // if
585     
586         $class_name = $beanList[$module_name];
587         require_once($beanFiles[$class_name]);
588         $mod = new $class_name();
589         $mod->retrieve($module_id);
590     
591         if (!self::$helperObject->checkACLAccess($mod, 'DetailView', $error, 'no_access')) {
592                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
593                 return;
594         } // if
595     
596         $output_list = array();
597         $linkoutput_list = array();
598     
599         // get all the related mmodules data.
600         $result = self::$helperObject->getRelationshipResults($mod, $link_field_name, $related_fields, $related_module_query,$order_by);
601         if (self::$helperObject->isLogLevelDebug()) {
602                 $GLOBALS['log']->debug('SoapHelperWebServices->get_relationships - return data for getRelationshipResults is ' . var_export($result, true));
603         } // if
604         if ($result) {
605                 $list = $result['rows'];
606                 $filterFields = $result['fields_set_on_rows'];
607     
608                 if (sizeof($list) > 0) {
609                         // get the related module name and instantiate a bean for that.
610                         $submodulename = $mod->$link_field_name->getRelatedModuleName();
611                         $submoduleclass = $beanList[$submodulename];
612                         require_once($beanFiles[$submoduleclass]);
613     
614                         $submoduletemp = new $submoduleclass();
615                         foreach($list as $row) {
616                                 $submoduleobject = @clone($submoduletemp);
617                                 // set all the database data to this object
618                                 foreach ($filterFields as $field) {
619                                         $submoduleobject->$field = $row[$field];
620                                 } // foreach
621                                 if (isset($row['id'])) {
622                                         $submoduleobject->id = $row['id'];
623                                 }
624                                 $output_list[] = self::$helperObject->get_return_value_for_fields($submoduleobject, $submodulename, $filterFields);
625                                 if (!empty($related_module_link_name_to_fields_array)) {
626                                         $linkoutput_list[] = self::$helperObject->get_return_value_for_link_fields($submoduleobject, $submodulename, $related_module_link_name_to_fields_array);
627                                 } // if
628     
629                         } // foreach
630                 }
631     
632         } // if
633     
634         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
635         return array('entry_list'=>$output_list, 'relationship_list' => $linkoutput_list);
636     
637     } // fn
638 }
639
640 SugarWebServiceImplv3::$helperObject = new SugarWebServiceUtilv3();