PageRenderTime 87ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/service/v3_1/SugarWebServiceImplv3_1.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 870 lines | 595 code | 100 blank | 175 comment | 142 complexity | 089d90f7c588afbcfd47582769d13d00 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/v3/SugarWebServiceImplv3.php');
  41. require_once('SugarWebServiceUtilv3_1.php');
  42. class SugarWebServiceImplv3_1 extends SugarWebServiceImplv3 {
  43. public function __construct()
  44. {
  45. self::$helperObject = new SugarWebServiceUtilv3_1();
  46. }
  47. /**
  48. * Retrieve a single SugarBean based on ID.
  49. *
  50. * @param String $session -- Session ID returned by a previous call to login.
  51. * @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)..
  52. * @param String $id -- The SugarBean's ID value.
  53. * @param Array $select_fields -- A list of the fields to be included in the results. This optional parameter allows for only needed fields to be retrieved.
  54. * @param Array $link_name_to_fields_array -- A list of link_names and for each link_name, what fields value to be returned. For ex.'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
  55. * @param bool $trackView -- Should we track the record accessed.
  56. * @return Array
  57. * 'entry_list' -- Array - The records name value pair for the simple data types excluding link field data.
  58. * 'relationship_list' -- Array - The records link field data. The example is if asked about accounts 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 ) ) ) ) )
  59. * @exception 'SoapFault' -- The SOAP error, if any
  60. */
  61. function get_entry($session, $module_name, $id,$select_fields, $link_name_to_fields_array,$track_view = FALSE)
  62. {
  63. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_entry');
  64. return self::get_entries($session, $module_name, array($id), $select_fields, $link_name_to_fields_array, $track_view);
  65. $GLOBALS['log']->info('end: SugarWebServiceImpl->get_entry');
  66. }
  67. /**
  68. * Retrieve the md5 hash of the vardef entries for a particular module.
  69. *
  70. * @param String $session -- Session ID returned by a previous call to login.
  71. * @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)..
  72. * @return String The md5 hash of the vardef definition.
  73. * @exception 'SoapFault' -- The SOAP error, if any
  74. */
  75. function get_module_fields_md5($session, $module_name){
  76. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_module_fields_md5(v3_1) for module: '. print_r($module_name, true));
  77. $results = array();
  78. if( is_array($module_name) )
  79. {
  80. foreach ($module_name as $module)
  81. $results[$module] = md5(serialize(self::get_module_fields($session, $module)));
  82. }
  83. else
  84. $results[$module_name] = md5(serialize(self::get_module_fields($session, $module_name)));
  85. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_fields_md5 (v3_1) for module: ' . print_r($module_name, true));
  86. return $results;
  87. }
  88. /**
  89. * Retrieve the md5 hash of a layout metadata for a given module given a specific type and view.
  90. *
  91. * @param String $session -- Session ID returned by a previous call to login.
  92. * @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)..
  93. * @return array $type(s) The type of view requested. Current supported types are 'default' (for application) and 'wireless'
  94. * @return array $view(s) The view requested. Current supported types are edit, detail, and list.
  95. * @exception 'SoapFault' -- The SOAP error, if any
  96. */
  97. function get_module_layout_md5($session, $module_name, $type, $view, $acl_check = TRUE){
  98. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_module_layout_md5');
  99. $results = self::get_module_layout($session, $module_name, $type, $view, $acl_check, TRUE);
  100. return array('md5'=> $results);
  101. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout_md5');
  102. }
  103. /**
  104. * Retrieve a list of SugarBean's based on provided IDs. This API will not wotk with report module
  105. *
  106. * @param String $session -- Session ID returned by a previous call to login.
  107. * @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)..
  108. * @param Array $ids -- An array of SugarBean IDs.
  109. * @param Array $select_fields -- A list of the fields to be included in the results. This optional parameter allows for only needed fields to be retrieved.
  110. * @param Array $link_name_to_fields_array -- A list of link_names and for each link_name, what fields value to be returned. For ex.'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
  111. * @param bool $trackView -- Should we track the record accessed.
  112. * @return Array
  113. * 'entry_list' -- Array - The records name value pair for the simple data types excluding link field data.
  114. * 'relationship_list' -- Array - The records link field data. The example is if asked about accounts 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 ) ) ) ) )
  115. * @exception 'SoapFault' -- The SOAP error, if any
  116. */
  117. function get_entries($session, $module_name, $ids, $select_fields, $link_name_to_fields_array, $track_view = FALSE)
  118. {
  119. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_entries');
  120. global $beanList, $beanFiles;
  121. $error = new SoapError();
  122. $linkoutput_list = array();
  123. $output_list = array();
  124. $using_cp = false;
  125. if($module_name == 'CampaignProspects')
  126. {
  127. $module_name = 'Prospects';
  128. $using_cp = true;
  129. }
  130. if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error))
  131. {
  132. $GLOBALS['log']->info('No Access: SugarWebServiceImpl->get_entries');
  133. return;
  134. } // if
  135. $class_name = $beanList[$module_name];
  136. require_once($beanFiles[$class_name]);
  137. $temp = new $class_name();
  138. foreach($ids as $id)
  139. {
  140. $seed = @clone($temp);
  141. if($using_cp)
  142. $seed = $seed->retrieveTarget($id);
  143. else
  144. {
  145. if ($seed->retrieve($id) == null)
  146. $seed->deleted = 1;
  147. }
  148. if ($seed->deleted == 1)
  149. {
  150. $list = array();
  151. $list[] = array('name'=>'warning', 'value'=>'Access to this object is denied since it has been deleted or does not exist');
  152. $list[] = array('name'=>'deleted', 'value'=>'1');
  153. $output_list[] = Array('id'=>$id,'module_name'=> $module_name,'name_value_list'=>$list,);
  154. continue;
  155. }
  156. if (!self::$helperObject->checkACLAccess($seed, 'DetailView', $error, 'no_access'))
  157. {
  158. return;
  159. }
  160. $output_list[] = self::$helperObject->get_return_value_for_fields($seed, $module_name, $select_fields);
  161. if (!empty($link_name_to_fields_array))
  162. {
  163. $linkoutput_list[] = self::$helperObject->get_return_value_for_link_fields($seed, $module_name, $link_name_to_fields_array);
  164. }
  165. $GLOBALS['log']->info('Should we track view: ' . $track_view);
  166. if($track_view)
  167. {
  168. self::$helperObject->trackView($seed, 'detailview');
  169. }
  170. }
  171. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entries');
  172. return array('entry_list'=>$output_list, 'relationship_list' => $linkoutput_list);
  173. }
  174. /**
  175. * Update or create a single SugarBean.
  176. *
  177. * @param String $session -- Session ID returned by a previous call to login.
  178. * @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)..
  179. * @param Array $name_value_list -- The keys of the array are the SugarBean attributes, the values of the array are the values the attributes should have.
  180. * @param Bool $track_view -- Should the tracker be notified that the action was performed on the bean.
  181. * @return Array 'id' -- the ID of the bean that was written to (-1 on error)
  182. * @exception 'SoapFault' -- The SOAP error, if any
  183. */
  184. function set_entry($session,$module_name, $name_value_list, $track_view = FALSE){
  185. global $beanList, $beanFiles, $current_user;
  186. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->set_entry');
  187. if (self::$helperObject->isLogLevelDebug()) {
  188. $GLOBALS['log']->debug('SoapHelperWebServices->set_entry - input data is ' . var_export($name_value_list, true));
  189. } // if
  190. $error = new SoapError();
  191. if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'write', 'no_access', $error)) {
  192. $GLOBALS['log']->info('End: SugarWebServiceImpl->set_entry');
  193. return;
  194. } // if
  195. $class_name = $beanList[$module_name];
  196. require_once($beanFiles[$class_name]);
  197. $seed = new $class_name();
  198. foreach($name_value_list as $name=>$value){
  199. if(is_array($value) && $value['name'] == 'id'){
  200. $seed->retrieve($value['value']);
  201. break;
  202. }else if($name === 'id' ){
  203. $seed->retrieve($value);
  204. }
  205. }
  206. $return_fields = array();
  207. foreach($name_value_list as $name=>$value){
  208. if($module_name == 'Users' && !empty($seed->id) && ($seed->id != $current_user->id) && $name == 'user_hash'){
  209. continue;
  210. }
  211. if(!is_array($value)){
  212. $seed->$name = $value;
  213. $return_fields[] = $name;
  214. }else{
  215. $seed->$value['name'] = $value['value'];
  216. $return_fields[] = $value['name'];
  217. }
  218. }
  219. if (!self::$helperObject->checkACLAccess($seed, 'Save', $error, 'no_access') || ($seed->deleted == 1 && !self::$helperObject->checkACLAccess($seed, 'Delete', $error, 'no_access'))) {
  220. $GLOBALS['log']->info('End: SugarWebServiceImpl->set_entry');
  221. return;
  222. } // if
  223. $seed->save(self::$helperObject->checkSaveOnNotify());
  224. $return_entry_list = self::$helperObject->get_name_value_list_for_fields($seed, $return_fields );
  225. if($seed->deleted == 1){
  226. $seed->mark_deleted($seed->id);
  227. }
  228. if($track_view){
  229. self::$helperObject->trackView($seed, 'editview');
  230. }
  231. $GLOBALS['log']->info('End: SugarWebServiceImpl->set_entry');
  232. return array('id'=>$seed->id, 'entry_list' => $return_entry_list);
  233. } // fn
  234. /**
  235. * Log the user into the application
  236. *
  237. * @param UserAuth array $user_auth -- Set user_name and password (password needs to be
  238. * in the right encoding for the type of authentication the user is setup for. For Base
  239. * sugar validation, password is the MD5 sum of the plain text password.
  240. * @param String $application -- The name of the application you are logging in from. (Currently unused).
  241. * @param array $name_value_list -- Array of name value pair of extra parameters. As of today only 'language' and 'notifyonsave' is supported
  242. * @return Array - id - String id is the session_id of the session that was created.
  243. * - module_name - String - module name of user
  244. * - name_value_list - Array - The name value pair of user_id, user_name, user_language, user_currency_id, user_currency_name,
  245. * - user_default_team_id, user_is_admin, user_default_dateformat, user_default_timeformat
  246. * @exception 'SoapFault' -- The SOAP error, if any
  247. */
  248. public function login($user_auth, $application, $name_value_list = array()){
  249. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->login');
  250. global $sugar_config, $system_config;
  251. $error = new SoapError();
  252. $user = new User();
  253. $success = false;
  254. //rrs
  255. $system_config = new Administration();
  256. $system_config->retrieveSettings('system');
  257. $authController = new AuthenticationController((!empty($sugar_config['authenticationClass'])? $sugar_config['authenticationClass'] : 'SugarAuthenticate'));
  258. //rrs
  259. if(!empty($user_auth['encryption']) && $user_auth['encryption'] === 'PLAIN' && $authController->authController->userAuthenticateClass != "LDAPAuthenticateUser")
  260. {
  261. $user_auth['password'] = md5($user_auth['password']);
  262. }
  263. $isLoginSuccess = $authController->login($user_auth['user_name'], $user_auth['password'], array('passwordEncrypted' => true));
  264. $usr_id=$user->retrieve_user_id($user_auth['user_name']);
  265. if($usr_id)
  266. $user->retrieve($usr_id);
  267. if ($isLoginSuccess)
  268. {
  269. if ($_SESSION['hasExpiredPassword'] =='1')
  270. {
  271. $error->set_error('password_expired');
  272. $GLOBALS['log']->fatal('password expired for user ' . $user_auth['user_name']);
  273. LogicHook::initialize();
  274. $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
  275. self::$helperObject->setFaultObject($error);
  276. return;
  277. }
  278. if(!empty($user) && !empty($user->id) && !$user->is_group)
  279. {
  280. $success = true;
  281. global $current_user;
  282. $current_user = $user;
  283. }
  284. }
  285. else if($usr_id && isset($user->user_name) && ($user->getPreference('lockout') == '1'))
  286. {
  287. $error->set_error('lockout_reached');
  288. $GLOBALS['log']->fatal('Lockout reached for user ' . $user_auth['user_name']);
  289. LogicHook::initialize();
  290. $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
  291. self::$helperObject->setFaultObject($error);
  292. return;
  293. }
  294. else if( $authController->authController->userAuthenticateClass == "LDAPAuthenticateUser"
  295. && (empty($user_auth['encryption']) || $user_auth['encryption'] !== 'PLAIN' ) )
  296. {
  297. $error->set_error('ldap_error');
  298. LogicHook::initialize();
  299. $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
  300. self::$helperObject->setFaultObject($error);
  301. return;
  302. }
  303. else if(function_exists('mcrypt_cbc'))
  304. {
  305. $password = self::$helperObject->decrypt_string($user_auth['password']);
  306. if($authController->login($user_auth['user_name'], $password) && isset($_SESSION['authenticated_user_id']))
  307. $success = true;
  308. }
  309. if($success)
  310. {
  311. session_start();
  312. global $current_user;
  313. //$current_user = $user;
  314. self::$helperObject->login_success($name_value_list);
  315. $current_user->loadPreferences();
  316. $_SESSION['is_valid_session']= true;
  317. $_SESSION['ip_address'] = query_client_ip();
  318. $_SESSION['user_id'] = $current_user->id;
  319. $_SESSION['type'] = 'user';
  320. $_SESSION['avail_modules']= self::$helperObject->get_user_module_list($current_user);
  321. $_SESSION['authenticated_user_id'] = $current_user->id;
  322. $_SESSION['unique_key'] = $sugar_config['unique_key'];
  323. $current_user->call_custom_logic('after_login');
  324. $GLOBALS['log']->info('End: SugarWebServiceImpl->login - succesful login');
  325. $nameValueArray = array();
  326. global $current_language;
  327. $nameValueArray['user_id'] = self::$helperObject->get_name_value('user_id', $current_user->id);
  328. $nameValueArray['user_name'] = self::$helperObject->get_name_value('user_name', $current_user->user_name);
  329. $nameValueArray['user_language'] = self::$helperObject->get_name_value('user_language', $current_language);
  330. $cur_id = $current_user->getPreference('currency');
  331. $nameValueArray['user_currency_id'] = self::$helperObject->get_name_value('user_currency_id', $cur_id);
  332. $nameValueArray['user_is_admin'] = self::$helperObject->get_name_value('user_is_admin', is_admin($current_user));
  333. $nameValueArray['user_default_team_id'] = self::$helperObject->get_name_value('user_default_team_id', $current_user->default_team );
  334. $nameValueArray['user_default_dateformat'] = self::$helperObject->get_name_value('user_default_dateformat', $current_user->getPreference('datef') );
  335. $nameValueArray['user_default_timeformat'] = self::$helperObject->get_name_value('user_default_timeformat', $current_user->getPreference('timef') );
  336. $num_grp_sep = $current_user->getPreference('num_grp_sep');
  337. $dec_sep = $current_user->getPreference('dec_sep');
  338. $nameValueArray['user_number_seperator'] = self::$helperObject->get_name_value('user_number_seperator', empty($num_grp_sep) ? $sugar_config['default_number_grouping_seperator'] : $num_grp_sep);
  339. $nameValueArray['user_decimal_seperator'] = self::$helperObject->get_name_value('user_decimal_seperator', empty($dec_sep) ? $sugar_config['default_decimal_seperator'] : $dec_sep);
  340. $nameValueArray['mobile_max_list_entries'] = self::$helperObject->get_name_value('mobile_max_list_entries', $sugar_config['wl_list_max_entries_per_page'] );
  341. $nameValueArray['mobile_max_subpanel_entries'] = self::$helperObject->get_name_value('mobile_max_subpanel_entries', $sugar_config['wl_list_max_entries_per_subpanel'] );
  342. if($application == 'mobile')
  343. {
  344. $modules = $availModuleNames = array();
  345. $availModules = array_keys($_SESSION['avail_modules']); //ACL check already performed.
  346. $modules = self::$helperObject->get_visible_mobile_modules($availModules);
  347. $nameValueArray['available_modules'] = $modules;
  348. //Get the vardefs md5
  349. foreach($modules as $mod_def)
  350. $availModuleNames[] = $mod_def['module_key'];
  351. $nameValueArray['vardefs_md5'] = self::get_module_fields_md5(session_id(), $availModuleNames);
  352. }
  353. $currencyObject = new Currency();
  354. $currencyObject->retrieve($cur_id);
  355. $nameValueArray['user_currency_name'] = self::$helperObject->get_name_value('user_currency_name', $currencyObject->name);
  356. $_SESSION['user_language'] = $current_language;
  357. return array('id'=>session_id(), 'module_name'=>'Users', 'name_value_list'=>$nameValueArray);
  358. }
  359. LogicHook::initialize();
  360. $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
  361. $error->set_error('invalid_login');
  362. self::$helperObject->setFaultObject($error);
  363. $GLOBALS['log']->info('End: SugarWebServiceImpl->login - failed login');
  364. }
  365. /**
  366. * Retrieve the list of available modules on the system available to the currently logged in user.
  367. *
  368. * @param String $session -- Session ID returned by a previous call to login.
  369. * @param String $filter -- Valid values are: all - Return all modules,
  370. * default - Return all visible modules for the application
  371. * mobile - Return all visible modules for the mobile view
  372. * @return Array 'modules' -- Array - An array of module names
  373. * @exception 'SoapFault' -- The SOAP error, if any
  374. */
  375. function get_available_modules($session,$filter='all'){
  376. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_available_modules');
  377. $error = new SoapError();
  378. if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
  379. $error->set_error('invalid_login');
  380. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
  381. return;
  382. } // if
  383. $modules = array();
  384. $availModules = array_keys($_SESSION['avail_modules']); //ACL check already performed.
  385. switch ($filter){
  386. case 'default':
  387. $modules = self::$helperObject->get_visible_modules($availModules);
  388. break;
  389. case 'mobile':
  390. $modules = self::$helperObject->get_visible_mobile_modules($availModules);
  391. break;
  392. case 'all':
  393. default:
  394. $modules = self::$helperObject->getModulesFromList(array_flip($availModules), $availModules);
  395. }
  396. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
  397. return array('modules'=> $modules);
  398. } // fn
  399. /**
  400. * Enter description here...
  401. *
  402. * @param string $session - Session ID returned by a previous call to login.
  403. * @param array $modules Array of modules to return
  404. * @param bool $MD5 Should the results be md5d
  405. */
  406. function get_language_definition($session, $modules, $MD5 = FALSE)
  407. {
  408. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_language_file');
  409. global $beanList, $beanFiles;
  410. global $sugar_config,$current_language;
  411. $error = new SoapError();
  412. $output_list = array();
  413. if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error))
  414. {
  415. $error->set_error('invalid_login');
  416. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_report_pdf');
  417. return;
  418. }
  419. if( is_string($modules) )
  420. $modules = array($modules);
  421. $results = array();
  422. foreach ($modules as $mod)
  423. {
  424. if( strtolower($mod) == 'app_strings' )
  425. {
  426. $values = return_application_language($current_language);
  427. $key = 'app_strings';
  428. }
  429. else if ( strtolower($mod) == 'app_list_strings' )
  430. {
  431. $values = return_app_list_strings_language($current_language);
  432. $key = 'app_list_strings';
  433. }
  434. else
  435. {
  436. $values = return_module_language($current_language, $mod);
  437. $key = $mod;
  438. }
  439. if( $MD5 )
  440. $values = md5(serialize($values));
  441. $results[$key] = $values;
  442. }
  443. return $results;
  444. }
  445. /**
  446. * Retrieve the layout metadata for a given module given a specific type and view.
  447. *
  448. * @param String $session -- Session ID returned by a previous call to login.
  449. * @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)..
  450. * @return array $type The type(s) of views requested. Current supported types are 'default' (for application) and 'wireless'
  451. * @return array $view The view(s) requested. Current supported types are edit, detail, list, and subpanel.
  452. * @exception 'SoapFault' -- The SOAP error, if any
  453. */
  454. function get_module_layout($session, $a_module_names, $a_type, $a_view,$acl_check = TRUE, $md5 = FALSE){
  455. $GLOBALS['log']->fatal('Begin: SugarWebServiceImpl->get_module_layout');
  456. global $beanList, $beanFiles;
  457. $error = new SoapError();
  458. $results = array();
  459. foreach ($a_module_names as $module_name)
  460. {
  461. if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error))
  462. {
  463. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout');
  464. continue;
  465. }
  466. $class_name = $beanList[$module_name];
  467. require_once($beanFiles[$class_name]);
  468. $seed = new $class_name();
  469. foreach ($a_view as $view)
  470. {
  471. $aclViewCheck = (strtolower($view) == 'subpanel') ? 'DetailView' : ucfirst(strtolower($view)) . 'View';
  472. if(!$acl_check || $seed->ACLAccess($aclViewCheck, true) )
  473. {
  474. foreach ($a_type as $type)
  475. {
  476. $a_vardefs = self::$helperObject->get_module_view_defs($module_name, $type, $view);
  477. if($md5)
  478. $results[$module_name][$type][$view] = md5(serialize($a_vardefs));
  479. else
  480. $results[$module_name][$type][$view] = $a_vardefs;
  481. }
  482. }
  483. }
  484. }
  485. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_layout');
  486. return $results;
  487. }
  488. /**
  489. * Retrieve a list of beans. This is the primary method for getting list of SugarBeans from Sugar using the SOAP API.
  490. *
  491. * @param String $session -- Session ID returned by a previous call to login.
  492. * @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)..
  493. * @param String $query -- SQL where clause without the word 'where'
  494. * @param String $order_by -- SQL order by clause without the phrase 'order by'
  495. * @param integer $offset -- The record offset to start from.
  496. * @param Array $select_fields -- A list of the fields to be included in the results. This optional parameter allows for only needed fields to be retrieved.
  497. * @param Array $link_name_to_fields_array -- A list of link_names and for each link_name, what fields value to be returned. For ex.'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
  498. * @param integer $max_results -- The maximum number of records to return. The default is the sugar configuration value for 'list_max_entries_per_page'
  499. * @param integer $deleted -- false if deleted records should not be include, true if deleted records should be included.
  500. * @return Array 'result_count' -- integer - The number of records returned
  501. * 'next_offset' -- integer - The start of the next page (This will always be the previous offset plus the number of rows returned. It does not indicate if there is additional data unless you calculate that the next_offset happens to be closer than it should be.
  502. * 'entry_list' -- Array - The records that were retrieved
  503. * 'relationship_list' -- Array - The records link field data. The example is if asked about accounts 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 ) ) ) ) )
  504. * @exception 'SoapFault' -- The SOAP error, if any
  505. */
  506. function get_entry_list($session, $module_name, $query, $order_by,$offset, $select_fields, $link_name_to_fields_array, $max_results, $deleted, $favorites = false ){
  507. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_entry_list');
  508. global $beanList, $beanFiles;
  509. $error = new SoapError();
  510. $using_cp = false;
  511. if($module_name == 'CampaignProspects'){
  512. $module_name = 'Prospects';
  513. $using_cp = true;
  514. }
  515. if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
  516. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
  517. return;
  518. } // if
  519. if (!self::$helperObject->checkQuery($error, $query, $order_by)) {
  520. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
  521. return;
  522. } // if
  523. // If the maximum number of entries per page was specified, override the configuration value.
  524. if($max_results > 0){
  525. global $sugar_config;
  526. $sugar_config['list_max_entries_per_page'] = $max_results;
  527. } // if
  528. $class_name = $beanList[$module_name];
  529. require_once($beanFiles[$class_name]);
  530. $seed = new $class_name();
  531. if (!self::$helperObject->checkACLAccess($seed, 'Export', $error, 'no_access')) {
  532. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
  533. return;
  534. } // if
  535. if (!self::$helperObject->checkACLAccess($seed, 'list', $error, 'no_access')) {
  536. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
  537. return;
  538. } // if
  539. if($query == ''){
  540. $where = '';
  541. } // if
  542. if($offset == '' || $offset == -1){
  543. $offset = 0;
  544. } // if
  545. if($using_cp){
  546. $response = $seed->retrieveTargetList($query, $select_fields, $offset,-1,-1,$deleted);
  547. }else
  548. {
  549. $response = self::$helperObject->get_data_list($seed,$order_by, $query, $offset,-1,-1,$deleted,$favorites);
  550. } // else
  551. $list = $response['list'];
  552. $output_list = array();
  553. $linkoutput_list = array();
  554. foreach($list as $value) {
  555. if(isset($value->emailAddress)){
  556. $value->emailAddress->handleLegacyRetrieve($value);
  557. } // if
  558. $value->fill_in_additional_detail_fields();
  559. $output_list[] = self::$helperObject->get_return_value_for_fields($value, $module_name, $select_fields);
  560. if(!empty($link_name_to_fields_array)){
  561. $linkoutput_list[] = self::$helperObject->get_return_value_for_link_fields($value, $module_name, $link_name_to_fields_array);
  562. }
  563. } // foreach
  564. // Calculate the offset for the start of the next page
  565. $next_offset = $offset + sizeof($output_list);
  566. $returnRelationshipList = array();
  567. foreach($linkoutput_list as $rel){
  568. $link_output = array();
  569. foreach($rel as $row){
  570. $rowArray = array();
  571. foreach($row['records'] as $record){
  572. $rowArray[]['link_value'] = $record;
  573. }
  574. $link_output[] = array('name' => $row['name'], 'records' => $rowArray);
  575. }
  576. $returnRelationshipList[]['link_list'] = $link_output;
  577. }
  578. $totalRecordCount = $response['row_count'];
  579. if( !empty($sugar_config['disable_count_query']) )
  580. $totalRecordCount = -1;
  581. $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
  582. return array('result_count'=>sizeof($output_list), 'total_count' => $totalRecordCount, 'next_offset'=>$next_offset, 'entry_list'=>$output_list, 'relationship_list' => $returnRelationshipList);
  583. } // fn
  584. /**
  585. * Given a list of modules to search and a search string, return the id, module_name, along with the fields
  586. * We will support Accounts, Bug Tracker, Cases, Contacts, Leads, Opportunities, Project, ProjectTask, Quotes
  587. *
  588. * @param string $session - Session ID returned by a previous call to login.
  589. * @param string $search_string - string to search
  590. * @param string[] $modules - array of modules to query
  591. * @param int $offset - a specified offset in the query
  592. * @param int $max_results - max number of records to return
  593. * @param string $assigned_user_id - a user id to filter all records by, leave empty to exclude the filter
  594. * @param string[] $select_fields - An array of fields to return. If empty the default return fields will be from the active list view defs.
  595. * @param bool $unified_search_only - A boolean indicating if we should only search against those modules participating in the unified search.
  596. * @return Array return_search_result - Array('Accounts' => array(array('name' => 'first_name', 'value' => 'John', 'name' => 'last_name', 'value' => 'Do')))
  597. * @exception 'SoapFault' -- The SOAP error, if any
  598. */
  599. function search_by_module($session, $search_string, $modules, $offset, $max_results,$assigned_user_id = '', $select_fields = array(), $unified_search_only = TRUE){
  600. $GLOBALS['log']->info('Begin: SugarWebServiceImpl->search_by_module');
  601. global $beanList, $beanFiles;
  602. global $sugar_config,$current_language;
  603. $error = new SoapError();
  604. $output_list = array();
  605. if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
  606. $error->set_error('invalid_login');
  607. $GLOBALS['log']->info('End: SugarWebServiceImpl->search_by_module');
  608. return;
  609. }
  610. global $current_user;
  611. if($max_results > 0){
  612. $sugar_config['list_max_entries_per_page'] = $max_results;
  613. }
  614. require_once('modules/Home/UnifiedSearchAdvanced.php');
  615. require_once 'include/utils.php';
  616. $usa = new UnifiedSearchAdvanced();
  617. if(!file_exists($cachedfile = sugar_cached('modules/unified_search_modules.php'))) {
  618. $usa->buildCache();
  619. }
  620. include($cachedfile);
  621. $modules_to_search = array();
  622. $unified_search_modules['Users'] = array('fields' => array());
  623. $unified_search_modules['ProjectTask'] = array('fields' => array());
  624. //If we are ignoring the unified search flag within the vardef we need to re-create the search fields. This allows us to search
  625. //against a specific module even though it is not enabled for the unified search within the application.
  626. if( !$unified_search_only )
  627. {
  628. foreach ($modules as $singleModule)
  629. {
  630. if( !isset($unified_search_modules[$singleModule]) )
  631. {
  632. $newSearchFields = array('fields' => self::$helperObject->generateUnifiedSearchFields($singleModule) );
  633. $unified_search_modules[$singleModule] = $newSearchFields;
  634. }
  635. }
  636. }
  637. foreach($unified_search_modules as $module=>$data) {
  638. if (in_array($module, $modules)) {
  639. $modules_to_search[$module] = $beanList[$module];
  640. } // if
  641. } // foreach
  642. $GLOBALS['log']->info('SugarWebServiceImpl->search_by_module - search string = ' . $search_string);
  643. if(!empty($search_string) && isset($search_string)) {
  644. $search_string = trim($GLOBALS['db']->quote(securexss(from_html(clean_string($search_string, 'UNIFIED_SEARCH')))));
  645. foreach($modules_to_search as $name => $beanName) {
  646. $where_clauses_array = array();
  647. $unifiedSearchFields = array () ;
  648. foreach ($unified_search_modules[$name]['fields'] as $field=>$def ) {
  649. $unifiedSearchFields[$name] [ $field ] = $def ;
  650. $unifiedSearchFields[$name] [ $field ]['value'] = $search_string;
  651. }
  652. require_once $beanFiles[$beanName] ;
  653. $seed = new $beanName();
  654. require_once 'include/SearchForm/SearchForm2.php' ;
  655. if ($beanName == "User"
  656. || $beanName == "ProjectTask"
  657. ) {
  658. if(!self::$helperObject->check_modules_access($current_user, $seed->module_dir, 'read')){
  659. continue;
  660. } // if
  661. if(!$seed->ACLAccess('ListView')) {
  662. continue;
  663. } // if
  664. }
  665. if ($beanName != "User"
  666. && $beanName != "ProjectTask"
  667. ) {
  668. $searchForm = new SearchForm ($seed, $name ) ;
  669. $searchForm->setup(array ($name => array()) ,$unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
  670. $where_clauses = $searchForm->generateSearchWhere() ;
  671. require_once 'include/SearchForm/SearchForm2.php' ;
  672. $searchForm = new SearchForm ($seed, $name ) ;
  673. $searchForm->setup(array ($name => array()) ,$unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
  674. $where_clauses = $searchForm->generateSearchWhere() ;
  675. $emailQuery = false;
  676. $where = '';
  677. if (count($where_clauses) > 0 ) {
  678. $where = '('. implode(' ) OR ( ', $where_clauses) . ')';
  679. }
  680. $mod_strings = return_module_language($current_language, $seed->module_dir);
  681. if(count($select_fields) > 0)
  682. $filterFields = $select_fields;
  683. else {
  684. if(file_exists('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php'))
  685. require_once('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
  686. else
  687. require_once('modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
  688. $filterFields = array();
  689. foreach($listViewDefs[$seed->module_dir] as $colName => $param) {
  690. if(!empty($param['default']) && $param['default'] == true)
  691. $filterFields[] = strtolower($colName);
  692. }
  693. if (!in_array('id', $filterFields))
  694. $filterFields[] = 'id';
  695. }
  696. //Pull in any db fields used for the unified search query so the correct joins will be added
  697. $selectOnlyQueryFields = array();
  698. foreach ($unifiedSearchFields[$name] as $field => $def){
  699. if( isset($def['db_field']) && !in_array($field,$filterFields) ){
  700. $filterFields[] = $field;
  701. $selectOnlyQueryFields[] = $field;
  702. }
  703. }
  704. //Add the assigned user filter if applicable
  705. if (!empty($assigned_user_id) && isset( $seed->field_defs['assigned_user_id']) ) {
  706. $ownerWhere = $seed->getOwnerWhere($assigned_user_id);
  707. $where = "($where) AND $ownerWhere";
  708. }
  709. if( $beanName == "Employee" )
  710. {
  711. $where = "($where) AND users.deleted = 0 AND users.is_group = 0 AND users.employee_status = 'Active'";
  712. }
  713. $ret_array = $seed->create_new_list_query('', $where, $filterFields, array(), 0, '', true, $seed, true);
  714. if(empty($params) or !is_array($params)) $params = array();
  715. if(!isset($params['custom_select'])) $params['custom_select'] = '';
  716. if(!isset($params['custom_from'])) $params['custom_from'] = '';
  717. if(!isset($params['custom_where'])) $params['custom_where'] = '';
  718. if(!isset($params['custom_order_by'])) $params['custom_order_by'] = '';
  719. $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'];
  720. } else {
  721. if ($beanName == "User") {
  722. $filterFields = array('id', 'user_name', 'first_name', 'last_name', 'email_address');
  723. $main_query = "select users.id, ea.email_address, users.user_name, first_name, last_name from users ";
  724. $main_query = $main_query . " LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_module = '{$seed->module_dir}'
  725. LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) ";
  726. $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'";
  727. } // if
  728. if ($beanName == "ProjectTask") {
  729. $filterFields = array('id', 'name', 'project_id', 'project_name');
  730. $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} ";
  731. $seed->add_team_security_where_clause($main_query);
  732. $main_query .= "LEFT JOIN teams ON $seed->table_name.team_id=teams.id AND (teams.deleted=0) ";
  733. $main_query .= "LEFT JOIN project ON $seed->table_name.project_id = project.id ";
  734. $main_query .= "where {$seed->table_name}.name like '{$search_string}%'";
  735. } // if
  736. } // else
  737. $GLOBALS['log']->info('SugarWebServiceImpl->search_by_module - query = ' . $main_query);
  738. if($max_results < -1) {
  739. $result = $seed->db->query($main_query);
  740. }
  741. else {
  742. if($max_results == -1) {
  743. $limit = $sugar_config['list_max_entries_per_page'];
  744. } else {
  745. $limit = $max_results;
  746. }
  747. $result = $seed->db->limitQuery($main_query, $offset, $limit + 1);
  748. }
  749. $rowArray = array();
  750. while($row = $seed->db->fetchByAssoc($result)) {
  751. $nameValueArray = array();
  752. foreach ($filterFields as $field) {
  753. if(in_array($field, $selectOnlyQueryFields))
  754. continue;
  755. $nameValue = array();
  756. if (isset($row[$field])) {
  757. $nameValueArray[$field] = self::$helperObject->get_name_value($field, $row[$field]);
  758. } // if
  759. } // foreach
  760. $rowArray[] = $nameValueArray;
  761. } // while
  762. $output_list[] = array('name' => $name, 'records' => $rowArray);
  763. } // foreach
  764. $GLOBALS['log']->info('End: SugarWebServiceImpl->search_by_module');
  765. return array('entry_list'=>$output_list);
  766. } // if
  767. return array('entry_list'=>$output_list);
  768. } // fn
  769. }
  770. SugarWebServiceImplv3_1::$helperObject = new SugarWebServiceUtilv3_1();