PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/service/v3/SugarWebServiceImplv3.php

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