]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/v3/SugarWebServiceImplv3.php
Release 6.5.9
[Github/sugarcrm.git] / service / v3 / SugarWebServiceImplv3.php
1 <?php
2 if(!defined('sugarEntry'))define('sugarEntry', true);
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  * 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|array $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(v3) for module: '. print_r($module_name, true));
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
186     /**
187      * Gets server info. This will return information like version, flavor and gmt_time.
188      * @return Array - flavor - String - Retrieve the specific flavor of sugar.
189      *                           - version - String - Retrieve the version number of Sugar that the server is running.
190      *                           - gmt_time - String - Return the current time on the server in the format 'Y-m-d H:i:s'. This time is in GMT.
191      * @exception 'SoapFault' -- The SOAP error, if any
192      */
193     function get_server_info(){
194         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_server_info');
195         global $sugar_flavor, $sugar_version;
196         if (empty($sugar_version))
197         {
198             require_once('sugar_version.php');
199         }
200         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_server_info');
201
202         return array('flavor' => $sugar_flavor, 'version' => $sugar_version, 'gmt_time' => TimeDate::getInstance()->nowDb());
203     } // fn
204
205     /**
206      * Retrieve the layout metadata for a given module given a specific type and view.
207      *
208      * @param String $session -- Session ID returned by a previous call to login.
209      * @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)..
210      * @return array $type The type(s) of views requested.  Current supported types are 'default' (for application) and 'wireless'
211      * @return array $view The view(s) requested.  Current supported types are edit, detail, list, and subpanel.
212      * @exception 'SoapFault' -- The SOAP error, if any
213      */
214     function get_module_layout($session, $a_module_names, $a_type, $a_view,$md5 = FALSE){
215         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_module_layout');
216
217         global  $beanList, $beanFiles;
218         $error = new SoapError();
219         $results = array();
220         foreach ($a_module_names as $module_name)
221         {
222             if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error))
223             {
224                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout');
225                 continue;
226             }
227
228             $class_name = $beanList[$module_name];
229             require_once($beanFiles[$class_name]);
230             $seed = new $class_name();
231
232             foreach ($a_view as $view)
233             {
234                 $aclViewCheck = (strtolower($view) == 'subpanel') ? 'DetailView' : ucfirst(strtolower($view)) . 'View';
235                 if($seed->ACLAccess($aclViewCheck, true) )
236                 {
237                     foreach ($a_type as $type)
238                     {
239                         $a_vardefs = self::$helperObject->get_module_view_defs($module_name, $type, $view);
240                         if($md5)
241                             $results[$module_name][$type][$view] = md5(serialize($a_vardefs));
242                         else
243                             $results[$module_name][$type][$view] = $a_vardefs;
244                     }
245                 }
246             }
247         }
248
249         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout');
250
251         return $results;
252     }
253
254     /**
255      * Retrieve the md5 hash of a layout metadata for a given module given a specific type and view.
256      *
257      * @param String $session -- Session ID returned by a previous call to login.
258      * @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)..
259      * @return array $type(s) The type of view requested.  Current supported types are 'default' (for application) and 'wireless'
260      * @return array $view(s) The view requested.  Current supported types are edit, detail, and list.
261      * @exception 'SoapFault' -- The SOAP error, if any
262      */
263     function get_module_layout_md5($session, $module_name, $type, $view){
264         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_module_layout_md5');
265         $results = self::get_module_layout($session, $module_name, $type, $view, TRUE);
266             return array('md5'=> $results);
267         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout_md5');
268     }
269
270     /**
271      * Retrieve the list of available modules on the system available to the currently logged in user.
272      *
273      * @param String $session -- Session ID returned by a previous call to login.
274      * @param String $filter --  Valid values are: all     - Return all modules,
275      *                                             default - Return all visible modules for the application
276      *                                             mobile  - Return all visible modules for the mobile view
277      * @return Array    'modules' -- Array - An array of module names
278      * @exception 'SoapFault' -- The SOAP error, if any
279      */
280     function get_available_modules($session,$filter='all'){
281         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_available_modules');
282
283         $error = new SoapError();
284         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
285                 $error->set_error('invalid_login');
286                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
287                 return;
288         } // if
289
290         $modules = array();
291         $availModules = array_keys($_SESSION['avail_modules']); //ACL check already performed.
292         switch ($filter){
293             case 'default':
294                 $modules = self::$helperObject->get_visible_modules($availModules);
295                break;
296             case 'all':
297             default:
298                 $modules = $availModules;
299         }
300
301         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
302         return array('modules'=> $modules);
303     } // fn
304
305     /**
306      * Retrieve a list of recently viewed records by module.
307      *
308      * @param String $session -- Session ID returned by a previous call to login.
309      * @param String $modules -- An array of modules or 'Home' to indicate all.
310      * @return Array The recently viewed records
311      * @exception 'SoapFault' -- The SOAP error, if any
312      */
313     function get_last_viewed($session, $module_names)
314     {
315         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_last_viewed');
316         $error = new SoapError();
317         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error))
318         {
319                 $error->set_error('invalid_login');
320                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_last_viewed');
321                 return;
322         } // if
323
324         $results = array();
325         foreach ($module_names as $module )
326         {
327             if(!self::$helperObject->check_modules_access($GLOBALS['current_user'], $module, 'read'))
328             {
329                 $GLOBALS['log']->debug("SugarWebServiceImpl->get_last_viewed: NO ACCESS to $module");
330                 continue;
331             }
332
333             if($module == 'Home') $module = '';
334             $tracker = new Tracker();
335             $entryList = $tracker->get_recently_viewed($GLOBALS['current_user']->id, $module);
336             foreach ($entryList as $entry)
337                 $results[] = $entry;
338         }
339
340         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_last_viewed');
341         return $results;
342     }
343
344     /**
345      * Retrieve a list of upcoming activities including Calls, Meetings,Tasks and Opportunities
346      *
347      * @param String $session -- Session ID returned by a previous call to login.
348      * @return Array List of upcoming activities
349      * @exception 'SoapFault' -- The SOAP error, if any
350      */
351     function get_upcoming_activities($session)
352     {
353         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_upcoming_activities');
354         $error = new SoapError();
355         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error))
356         {
357                 $error->set_error('invalid_login');
358                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_upcoming_activities');
359                 return;
360         } // if
361
362         $results = self::$helperObject->get_upcoming_activities();
363
364         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_upcoming_activities');
365
366         return $results;
367     }
368
369     /**
370      * Given a list of modules to search and a search string, return the id, module_name, along with the fields
371      * We will support Accounts, Bug Tracker, Cases, Contacts, Leads, Opportunities, Project, ProjectTask, Quotes
372      *
373      * @param string $session                   - Session ID returned by a previous call to login.
374      * @param string $search_string     - string to search
375      * @param string[] $modules                 - array of modules to query
376      * @param int $offset                               - a specified offset in the query
377      * @param int $max_results                  - max number of records to return
378      * @param string $assigned_user_id  - a user id to filter all records by, leave empty to exclude the filter
379      * @param string[] $select_fields   - An array of fields to return.  If empty the default return fields will be from the active list view defs.
380      * @return Array return_search_result       - Array('Accounts' => array(array('name' => 'first_name', 'value' => 'John', 'name' => 'last_name', 'value' => 'Do')))
381      * @exception 'SoapFault' -- The SOAP error, if any
382      */
383     function search_by_module($session, $search_string, $modules, $offset, $max_results,$assigned_user_id = '', $select_fields = array()){
384         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->search_by_module');
385         global  $beanList, $beanFiles;
386         global $sugar_config,$current_language;
387
388         $error = new SoapError();
389         $output_list = array();
390         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
391                 $error->set_error('invalid_login');
392                 $GLOBALS['log']->info('End: SugarWebServiceImpl->search_by_module');
393                 return;
394         }
395         global $current_user;
396         if($max_results > 0){
397                 $sugar_config['list_max_entries_per_page'] = $max_results;
398         }
399
400         require_once('modules/Home/UnifiedSearchAdvanced.php');
401         require_once 'include/utils.php';
402         $usa = new UnifiedSearchAdvanced();
403         if(!file_exists($cachedfile = sugar_cached('modules/unified_search_modules.php'))) {
404             $usa->buildCache();
405         }
406
407         include($cachedfile);
408         $modules_to_search = array();
409         $unified_search_modules['Users'] =   array('fields' => array());
410
411         $unified_search_modules['ProjectTask'] =   array('fields' => array());
412
413         foreach($unified_search_modules as $module=>$data) {
414                 if (in_array($module, $modules)) {
415                 $modules_to_search[$module] = $beanList[$module];
416                 } // if
417         } // foreach
418
419         $GLOBALS['log']->info('SugarWebServiceImpl->search_by_module - search string = ' . $search_string);
420
421         if(!empty($search_string) && isset($search_string)) {
422                 $search_string = trim($GLOBALS['db']->quote(securexss(from_html(clean_string($search_string, 'UNIFIED_SEARCH')))));
423                 foreach($modules_to_search as $name => $beanName) {
424                         $where_clauses_array = array();
425                         $unifiedSearchFields = array () ;
426                         foreach ($unified_search_modules[$name]['fields'] as $field=>$def ) {
427                                 $unifiedSearchFields[$name] [ $field ] = $def ;
428                                 $unifiedSearchFields[$name] [ $field ]['value'] = $search_string;
429                         }
430
431                         require_once $beanFiles[$beanName] ;
432                         $seed = new $beanName();
433                         require_once 'include/SearchForm/SearchForm2.php' ;
434                         if ($beanName == "User"
435                             || $beanName == "ProjectTask"
436                             ) {
437                                 if(!self::$helperObject->check_modules_access($current_user, $seed->module_dir, 'read')){
438                                         continue;
439                                 } // if
440                                 if(!$seed->ACLAccess('ListView')) {
441                                         continue;
442                                 } // if
443                         }
444
445                         if ($beanName != "User"
446                             && $beanName != "ProjectTask"
447                             ) {
448                                 $searchForm = new SearchForm ($seed, $name ) ;
449
450                                 $searchForm->setup(array ($name => array()) ,$unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
451                                 $where_clauses = $searchForm->generateSearchWhere() ;
452                                 require_once 'include/SearchForm/SearchForm2.php' ;
453                                 $searchForm = new SearchForm ($seed, $name ) ;
454
455                                 $searchForm->setup(array ($name => array()) ,$unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
456                                 $where_clauses = $searchForm->generateSearchWhere() ;
457                                 $emailQuery = false;
458
459                                 $where = '';
460                                 if (count($where_clauses) > 0 ) {
461                                         $where = '('. implode(' ) OR ( ', $where_clauses) . ')';
462                                 }
463
464                                 $mod_strings = return_module_language($current_language, $seed->module_dir);
465
466                                 if(count($select_fields) > 0)
467                                     $filterFields = $select_fields;
468                                 else {
469                                     if(file_exists('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php'))
470                                            require_once('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
471                                         else
472                                                 require_once('modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
473
474                                         $filterFields = array();
475                                         foreach($listViewDefs[$seed->module_dir] as $colName => $param) {
476                                 if(!empty($param['default']) && $param['default'] == true)
477                                     $filterFields[] = strtolower($colName);
478                             }
479                             if (!in_array('id', $filterFields))
480                                 $filterFields[] = 'id';
481                                 }
482
483                                 //Pull in any db fields used for the unified search query so the correct joins will be added
484                                 $selectOnlyQueryFields = array();
485                                 foreach ($unifiedSearchFields[$name] as $field => $def){
486                                     if( isset($def['db_field']) && !in_array($field,$filterFields) ){
487                                         $filterFields[] = $field;
488                                         $selectOnlyQueryFields[] = $field;
489                                     }
490                                 }
491
492                     //Add the assigned user filter if applicable
493                     if (!empty($assigned_user_id) && isset( $seed->field_defs['assigned_user_id']) ) {
494                        $ownerWhere = $seed->getOwnerWhere($assigned_user_id);
495                        $where = "($where) AND $ownerWhere";
496                     }
497
498                                 $ret_array = $seed->create_new_list_query('', $where, $filterFields, array(), 0, '', true, $seed, true);
499                         if(empty($params) or !is_array($params)) $params = array();
500                         if(!isset($params['custom_select'])) $params['custom_select'] = '';
501                         if(!isset($params['custom_from'])) $params['custom_from'] = '';
502                         if(!isset($params['custom_where'])) $params['custom_where'] = '';
503                         if(!isset($params['custom_order_by'])) $params['custom_order_by'] = '';
504                                 $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'];
505                         } else {
506                                 if ($beanName == "User") {
507                                         $filterFields = array('id', 'user_name', 'first_name', 'last_name', 'email_address');
508                                         $main_query = "select users.id, ea.email_address, users.user_name, first_name, last_name from users ";
509                                         $main_query = $main_query . " LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_module = '{$seed->module_dir}'
510     LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) ";
511                                         $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'";
512                                 } // if
513                                 if ($beanName == "ProjectTask") {
514                                         $filterFields = array('id', 'name', 'project_id', 'project_name');
515                                         $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} ";
516                                         $seed->add_team_security_where_clause($main_query);
517                                         $main_query .= "LEFT JOIN teams ON $seed->table_name.team_id=teams.id AND (teams.deleted=0) ";
518                             $main_query .= "LEFT JOIN project ON $seed->table_name.project_id = project.id ";
519                             $main_query .= "where {$seed->table_name}.name like '{$search_string}%'";
520                                 } // if
521                         } // else
522
523                         $GLOBALS['log']->info('SugarWebServiceImpl->search_by_module - query = ' . $main_query);
524                         if($max_results < -1) {
525                                 $result = $seed->db->query($main_query);
526                         }
527                         else {
528                                 if($max_results == -1) {
529                                         $limit = $sugar_config['list_max_entries_per_page'];
530                     } else {
531                         $limit = $max_results;
532                     }
533                     $result = $seed->db->limitQuery($main_query, $offset, $limit + 1);
534                         }
535
536                         $rowArray = array();
537                         while($row = $seed->db->fetchByAssoc($result)) {
538                                 $nameValueArray = array();
539                                 foreach ($filterFields as $field) {
540                                     if(in_array($field, $selectOnlyQueryFields))
541                                         continue;
542                                         $nameValue = array();
543                                         if (isset($row[$field])) {
544                                                 $nameValueArray[$field] = self::$helperObject->get_name_value($field, $row[$field]);
545                                         } // if
546                                 } // foreach
547                                 $rowArray[] = $nameValueArray;
548                         } // while
549                         $output_list[] = array('name' => $name, 'records' => $rowArray);
550                 } // foreach
551
552         $GLOBALS['log']->info('End: SugarWebServiceImpl->search_by_module');
553         return array('entry_list'=>$output_list);
554         } // if
555         return array('entry_list'=>$output_list);
556     } // fn
557
558     /**
559      * Retrieve a collection of beans that are related to the specified bean and optionally return relationship data for those related beans.
560      * 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.
561      *
562      * @param String $session -- Session ID returned by a previous call to login.
563      * @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)..
564      * @param String $module_id -- The ID of the bean in the specified module
565      * @param String $link_field_name -- The name of the lnk field to return records from.  This name should be the name the relationship.
566      * @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.
567      * @param Array $related_fields - Array of related bean fields to be returned.
568      * @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'))).
569      * @param Number $deleted -- false if deleted records should not be include, true if deleted records should be included.
570      * @param String $order_by -- field to order the result sets by
571      * @return Array 'entry_list' -- Array - The records that were retrieved
572      *                   '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 ) ) ) ) )
573     * @exception 'SoapFault' -- The SOAP error, if any
574     */
575     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 = ''){
576         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_relationships');
577         global  $beanList, $beanFiles;
578         $error = new SoapError();
579         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
580                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
581                 return;
582         } // if
583
584         $class_name = $beanList[$module_name];
585         require_once($beanFiles[$class_name]);
586         $mod = new $class_name();
587         $mod->retrieve($module_id);
588
589         if (!self::$helperObject->checkQuery($error, $related_module_query, $order_by)) {
590                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
591                 return;
592         } // if
593
594         if (!self::$helperObject->checkACLAccess($mod, 'DetailView', $error, 'no_access')) {
595                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
596                 return;
597         } // if
598
599         $output_list = array();
600         $linkoutput_list = array();
601
602         // get all the related mmodules data.
603         $result = self::$helperObject->getRelationshipResults($mod, $link_field_name, $related_fields, $related_module_query,$order_by);
604         if (self::$helperObject->isLogLevelDebug()) {
605                 $GLOBALS['log']->debug('SoapHelperWebServices->get_relationships - return data for getRelationshipResults is ' . var_export($result, true));
606         } // if
607         if ($result) {
608                 $list = $result['rows'];
609                 $filterFields = $result['fields_set_on_rows'];
610
611                 if (sizeof($list) > 0) {
612                         // get the related module name and instantiate a bean for that.
613                         $submodulename = $mod->$link_field_name->getRelatedModuleName();
614                         $submoduleclass = $beanList[$submodulename];
615                         require_once($beanFiles[$submoduleclass]);
616
617                         $submoduletemp = new $submoduleclass();
618                         foreach($list as $row) {
619                                 $submoduleobject = @clone($submoduletemp);
620                                 // set all the database data to this object
621                                 foreach ($filterFields as $field) {
622                                         $submoduleobject->$field = $row[$field];
623                                 } // foreach
624                                 if (isset($row['id'])) {
625                                         $submoduleobject->id = $row['id'];
626                                 }
627                                 $output_list[] = self::$helperObject->get_return_value_for_fields($submoduleobject, $submodulename, $filterFields);
628                                 if (!empty($related_module_link_name_to_fields_array)) {
629                                         $linkoutput_list[] = self::$helperObject->get_return_value_for_link_fields($submoduleobject, $submodulename, $related_module_link_name_to_fields_array);
630                                 } // if
631
632                         } // foreach
633                 }
634
635         } // if
636
637         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
638         return array('entry_list'=>$output_list, 'relationship_list' => $linkoutput_list);
639
640     } // fn
641 }
642
643 SugarWebServiceImplv3::$helperObject = new SugarWebServiceUtilv3();