PageRenderTime 69ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/soap/SoapSugarUsers.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 2262 lines | 1560 code | 261 blank | 441 comment | 334 complexity | 139d0b860d8efdae004c89c28417e96c MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  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. require_once('soap/SoapHelperFunctions.php');
  38. require_once('soap/SoapTypes.php');
  39. /*************************************************************************************
  40. THIS IS FOR SUGARCRM USERS
  41. *************************************************************************************/
  42. $disable_date_format = true;
  43. $server->register(
  44. 'is_user_admin',
  45. array('session'=>'xsd:string'),
  46. array('return'=>'xsd:int'),
  47. $NAMESPACE);
  48. /**
  49. * Return if the user is an admin or not
  50. *
  51. * @param String $session -- Session ID returned by a previous call to login.
  52. * @return int 1 or 0 depending on if the user is an admin
  53. */
  54. function is_user_admin($session){
  55. if(validate_authenticated($session)){
  56. global $current_user;
  57. return is_admin($current_user);
  58. }else{
  59. return 0;
  60. }
  61. }
  62. $server->register(
  63. 'login',
  64. array('user_auth'=>'tns:user_auth', 'application_name'=>'xsd:string'),
  65. array('return'=>'tns:set_entry_result'),
  66. $NAMESPACE);
  67. /**
  68. * Log the user into the application
  69. *
  70. * @param UserAuth array $user_auth -- Set user_name and password (password needs to be
  71. * in the right encoding for the type of authentication the user is setup for. For Base
  72. * sugar validation, password is the MD5 sum of the plain text password.
  73. * @param String $application -- The name of the application you are logging in from. (Currently unused).
  74. * @return Array(session_id, error) -- session_id is the id of the session that was
  75. * created. Error is set if there was any error during creation.
  76. */
  77. function login($user_auth, $application){
  78. global $sugar_config, $system_config;
  79. $error = new SoapError();
  80. $user = new User();
  81. $success = false;
  82. //rrs
  83. $system_config = new Administration();
  84. $system_config->retrieveSettings('system');
  85. $authController = new AuthenticationController((!empty($sugar_config['authenticationClass'])? $sugar_config['authenticationClass'] : 'SugarAuthenticate'));
  86. //rrs
  87. $isLoginSuccess = $authController->login($user_auth['user_name'], $user_auth['password'], array('passwordEncrypted' => true));
  88. $usr_id=$user->retrieve_user_id($user_auth['user_name']);
  89. if($usr_id) {
  90. $user->retrieve($usr_id);
  91. }
  92. if ($isLoginSuccess) {
  93. if ($_SESSION['hasExpiredPassword'] =='1') {
  94. $error->set_error('password_expired');
  95. $GLOBALS['log']->fatal('password expired for user ' . $user_auth['user_name']);
  96. LogicHook::initialize();
  97. $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
  98. return array('id'=>-1, 'error'=>$error);
  99. } // if
  100. if(!empty($user) && !empty($user->id) && !$user->is_group) {
  101. $success = true;
  102. global $current_user;
  103. $current_user = $user;
  104. } // if
  105. } else if($usr_id && isset($user->user_name) && ($user->getPreference('lockout') == '1')) {
  106. $error->set_error('lockout_reached');
  107. $GLOBALS['log']->fatal('Lockout reached for user ' . $user_auth['user_name']);
  108. LogicHook::initialize();
  109. $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
  110. return array('id'=>-1, 'error'=>$error);
  111. } else if(function_exists('mcrypt_cbc')){
  112. $password = decrypt_string($user_auth['password']);
  113. $authController = new AuthenticationController((!empty($sugar_config['authenticationClass'])? $sugar_config['authenticationClass'] : 'SugarAuthenticate'));
  114. if($authController->login($user_auth['user_name'], $password) && isset($_SESSION['authenticated_user_id'])){
  115. $success = true;
  116. } // if
  117. } // else if
  118. if($success){
  119. session_start();
  120. global $current_user;
  121. //$current_user = $user;
  122. login_success();
  123. $current_user->loadPreferences();
  124. $_SESSION['is_valid_session']= true;
  125. $_SESSION['ip_address'] = query_client_ip();
  126. $_SESSION['user_id'] = $current_user->id;
  127. $_SESSION['type'] = 'user';
  128. $_SESSION['avail_modules']= get_user_module_list($current_user);
  129. $_SESSION['authenticated_user_id'] = $current_user->id;
  130. $_SESSION['unique_key'] = $sugar_config['unique_key'];
  131. $current_user->call_custom_logic('after_login');
  132. return array('id'=>session_id(), 'error'=>$error);
  133. }
  134. $error->set_error('invalid_login');
  135. $GLOBALS['log']->fatal('SECURITY: User authentication for '. $user_auth['user_name']. ' failed');
  136. LogicHook::initialize();
  137. $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
  138. return array('id'=>-1, 'error'=>$error);
  139. }
  140. //checks if the soap server and client are running on the same machine
  141. $server->register(
  142. 'is_loopback',
  143. array(),
  144. array('return'=>'xsd:int'),
  145. $NAMESPACE);
  146. /**
  147. * Check to see if the soap server and client are on the same machine.
  148. * We don't allow a server to sync to itself.
  149. *
  150. * @return true -- if the SOAP server and client are on the same machine
  151. * @return false -- if the SOAP server and client are not on the same machine.
  152. */
  153. function is_loopback(){
  154. if(query_client_ip() == $_SERVER['SERVER_ADDR'])
  155. return 1;
  156. return 0;
  157. }
  158. /**
  159. * Validate the provided session information is correct and current. Load the session.
  160. *
  161. * @param String $session_id -- The session ID that was returned by a call to login.
  162. * @return true -- If the session is valid and loaded.
  163. * @return false -- if the session is not valid.
  164. */
  165. function validate_authenticated($session_id){
  166. if(!empty($session_id)){
  167. session_id($session_id);
  168. session_start();
  169. if(!empty($_SESSION['is_valid_session']) && is_valid_ip_address('ip_address') && $_SESSION['type'] == 'user'){
  170. global $current_user;
  171. $current_user = new User();
  172. $current_user->retrieve($_SESSION['user_id']);
  173. login_success();
  174. return true;
  175. }
  176. session_destroy();
  177. }
  178. LogicHook::initialize();
  179. $GLOBALS['log']->fatal('SECURITY: The session ID is invalid');
  180. $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
  181. return false;
  182. }
  183. /**
  184. * Use the same logic as in SugarAuthenticate to validate the ip address
  185. *
  186. * @param string $session_var
  187. * @return bool - true if the ip address is valid, false otherwise.
  188. */
  189. function is_valid_ip_address($session_var){
  190. global $sugar_config;
  191. // grab client ip address
  192. $clientIP = query_client_ip();
  193. $classCheck = 0;
  194. // check to see if config entry is present, if not, verify client ip
  195. if (!isset ($sugar_config['verify_client_ip']) || $sugar_config['verify_client_ip'] == true) {
  196. // check to see if we've got a current ip address in $_SESSION
  197. // and check to see if the session has been hijacked by a foreign ip
  198. if (isset ($_SESSION[$session_var])) {
  199. $session_parts = explode(".", $_SESSION[$session_var]);
  200. $client_parts = explode(".", $clientIP);
  201. if(count($session_parts) < 4) {
  202. $classCheck = 0;
  203. }else {
  204. // match class C IP addresses
  205. for ($i = 0; $i < 3; $i ++) {
  206. if ($session_parts[$i] == $client_parts[$i]) {
  207. $classCheck = 1;
  208. continue;
  209. } else {
  210. $classCheck = 0;
  211. break;
  212. }
  213. }
  214. }
  215. // we have a different IP address
  216. if ($_SESSION[$session_var] != $clientIP && empty ($classCheck)) {
  217. $GLOBALS['log']->fatal("IP Address mismatch: SESSION IP: {$_SESSION[$session_var]} CLIENT IP: {$clientIP}");
  218. return false;
  219. }
  220. } else {
  221. return false;
  222. }
  223. }
  224. return true;
  225. }
  226. $server->register(
  227. 'seamless_login',
  228. array('session'=>'xsd:string'),
  229. array('return'=>'xsd:int'),
  230. $NAMESPACE);
  231. /**
  232. * Perform a seamless login. This is used internally during the sync process.
  233. *
  234. * @param String $session -- Session ID returned by a previous call to login.
  235. * @return true -- if the session was authenticated
  236. * @return false -- if the session could not be authenticated
  237. */
  238. function seamless_login($session){
  239. if(!validate_authenticated($session)){
  240. return 0;
  241. }
  242. $_SESSION['seamless_login'] = true;
  243. return 1;
  244. }
  245. $server->register(
  246. 'get_entry_list',
  247. array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'query'=>'xsd:string', 'order_by'=>'xsd:string','offset'=>'xsd:int', 'select_fields'=>'tns:select_fields', 'max_results'=>'xsd:int', 'deleted'=>'xsd:int'),
  248. array('return'=>'tns:get_entry_list_result'),
  249. $NAMESPACE);
  250. /**
  251. * Retrieve a list of beans. This is the primary method for getting list of SugarBeans from Sugar using the SOAP API.
  252. *
  253. * @param String $session -- Session ID returned by a previous call to login.
  254. * @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)..
  255. * @param String $query -- SQL where clause without the word 'where'
  256. * @param String $order_by -- SQL order by clause without the phrase 'order by'
  257. * @param String $offset -- The record offset to start from.
  258. * @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.
  259. * @param String $max_results -- The maximum number of records to return. The default is the sugar configuration value for 'list_max_entries_per_page'
  260. * @param Number $deleted -- false if deleted records should not be include, true if deleted records should be included.
  261. * @return Array 'result_count' -- The number of records returned
  262. * 'next_offset' -- 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.
  263. * 'field_list' -- The vardef information on the selected fields.
  264. * Array -- 'field'=> 'name' -- the name of the field
  265. * 'type' -- the data type of the field
  266. * 'label' -- the translation key for the label of the field
  267. * 'required' -- Is the field required?
  268. * 'options' -- Possible values for a drop down field
  269. * 'entry_list' -- The records that were retrieved
  270. * 'error' -- The SOAP error, if any
  271. */
  272. function get_entry_list($session, $module_name, $query, $order_by,$offset, $select_fields, $max_results, $deleted ){
  273. global $beanList, $beanFiles;
  274. $error = new SoapError();
  275. if(!validate_authenticated($session)){
  276. $error->set_error('invalid_login');
  277. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  278. }
  279. $using_cp = false;
  280. if($module_name == 'CampaignProspects'){
  281. $module_name = 'Prospects';
  282. $using_cp = true;
  283. }
  284. if(empty($beanList[$module_name])){
  285. $error->set_error('no_module');
  286. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  287. }
  288. global $current_user;
  289. if(!check_modules_access($current_user, $module_name, 'read')){
  290. $error->set_error('no_access');
  291. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  292. }
  293. // If the maximum number of entries per page was specified, override the configuration value.
  294. if($max_results > 0){
  295. global $sugar_config;
  296. $sugar_config['list_max_entries_per_page'] = $max_results;
  297. }
  298. $class_name = $beanList[$module_name];
  299. require_once($beanFiles[$class_name]);
  300. $seed = new $class_name();
  301. if(! ($seed->ACLAccess('Export') && $seed->ACLAccess('list')))
  302. {
  303. $error->set_error('no_access');
  304. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  305. }
  306. require_once 'include/SugarSQLValidate.php';
  307. $valid = new SugarSQLValidate();
  308. if(!$valid->validateQueryClauses($query, $order_by)) {
  309. $GLOBALS['log']->error("Bad query: $query $order_by");
  310. $error->set_error('no_access');
  311. return array(
  312. 'result_count' => -1,
  313. 'error' => $error->get_soap_array()
  314. );
  315. }
  316. if($query == ''){
  317. $where = '';
  318. }
  319. if($offset == '' || $offset == -1){
  320. $offset = 0;
  321. }
  322. if($using_cp){
  323. $response = $seed->retrieveTargetList($query, $select_fields, $offset,-1,-1,$deleted);
  324. }else{
  325. $response = $seed->get_list($order_by, $query, $offset,-1,-1,$deleted,true);
  326. }
  327. $list = $response['list'];
  328. $output_list = array();
  329. $isEmailModule = false;
  330. if($module_name == 'Emails'){
  331. $isEmailModule = true;
  332. }
  333. // retrieve the vardef information on the bean's fields.
  334. $field_list = array();
  335. foreach($list as $value)
  336. {
  337. if(isset($value->emailAddress)){
  338. $value->emailAddress->handleLegacyRetrieve($value);
  339. }
  340. if($isEmailModule){
  341. $value->retrieveEmailText();
  342. }
  343. $value->fill_in_additional_detail_fields();
  344. $output_list[] = get_return_value($value, $module_name);
  345. if(empty($field_list)){
  346. $field_list = get_field_list($value);
  347. }
  348. }
  349. // Filter the search results to only include the requested fields.
  350. $output_list = filter_return_list($output_list, $select_fields, $module_name);
  351. // Filter the list of fields to only include information on the requested fields.
  352. $field_list = filter_return_list($field_list,$select_fields, $module_name);
  353. // Calculate the offset for the start of the next page
  354. $next_offset = $offset + sizeof($output_list);
  355. return array('result_count'=>sizeof($output_list), 'next_offset'=>$next_offset,'field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
  356. }
  357. $server->register(
  358. 'get_entry',
  359. array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'id'=>'xsd:string', 'select_fields'=>'tns:select_fields'),
  360. array('return'=>'tns:get_entry_result'),
  361. $NAMESPACE);
  362. /**
  363. * Retrieve a single SugarBean based on ID.
  364. *
  365. * @param String $session -- Session ID returned by a previous call to login.
  366. * @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)..
  367. * @param String $id -- The SugarBean's ID value.
  368. * @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.
  369. * @return unknown
  370. */
  371. function get_entry($session, $module_name, $id,$select_fields ){
  372. return get_entries($session, $module_name, array($id), $select_fields);
  373. }
  374. $server->register(
  375. 'get_entries',
  376. array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'ids'=>'tns:select_fields', 'select_fields'=>'tns:select_fields'),
  377. array('return'=>'tns:get_entry_result'),
  378. $NAMESPACE);
  379. /**
  380. * Retrieve a list of SugarBean's based on provided IDs.
  381. *
  382. * @param String $session -- Session ID returned by a previous call to login.
  383. * @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)..
  384. * @param Array $ids -- An array of SugarBean IDs.
  385. * @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.
  386. * @return Array 'field_list' -- Var def information about the returned fields
  387. * 'entry_list' -- The records that were retrieved
  388. * 'error' -- The SOAP error, if any
  389. */
  390. function get_entries($session, $module_name, $ids,$select_fields ){
  391. global $beanList, $beanFiles;
  392. $error = new SoapError();
  393. $field_list = array();
  394. $output_list = array();
  395. if(!validate_authenticated($session)){
  396. $error->set_error('invalid_login');
  397. return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  398. }
  399. $using_cp = false;
  400. if($module_name == 'CampaignProspects'){
  401. $module_name = 'Prospects';
  402. $using_cp = true;
  403. }
  404. if(empty($beanList[$module_name])){
  405. $error->set_error('no_module');
  406. return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  407. }
  408. global $current_user;
  409. if(!check_modules_access($current_user, $module_name, 'read')){
  410. $error->set_error('no_access');
  411. return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  412. }
  413. $class_name = $beanList[$module_name];
  414. require_once($beanFiles[$class_name]);
  415. //todo can modify in there to call bean->get_list($order_by, $where, 0, -1, -1, $deleted);
  416. //that way we do not have to call retrieve for each bean
  417. //perhaps also add a select_fields to this, so we only get the fields we need
  418. //and not do a select *
  419. foreach($ids as $id){
  420. $seed = new $class_name();
  421. if($using_cp){
  422. $seed = $seed->retrieveTarget($id);
  423. }else{
  424. if ($seed->retrieve($id) == null)
  425. $seed->deleted = 1;
  426. }
  427. if ($seed->deleted == 1) {
  428. $list = array();
  429. $list[] = array('name'=>'warning', 'value'=>'Access to this object is denied since it has been deleted or does not exist');
  430. $list[] = array('name'=>'deleted', 'value'=>'1');
  431. $output_list[] = Array('id'=>$id,
  432. 'module_name'=> $module_name,
  433. 'name_value_list'=>$list,
  434. );
  435. continue;
  436. }
  437. if(! $seed->ACLAccess('DetailView')){
  438. $error->set_error('no_access');
  439. return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  440. }
  441. $output_list[] = get_return_value($seed, $module_name);
  442. if(empty($field_list)){
  443. $field_list = get_field_list($seed);
  444. }
  445. }
  446. $output_list = filter_return_list($output_list, $select_fields, $module_name);
  447. $field_list = filter_field_list($field_list,$select_fields, $module_name);
  448. return array( 'field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
  449. }
  450. $server->register(
  451. 'set_entry',
  452. array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'name_value_list'=>'tns:name_value_list'),
  453. array('return'=>'tns:set_entry_result'),
  454. $NAMESPACE);
  455. /**
  456. * Update or create a single SugarBean.
  457. *
  458. * @param String $session -- Session ID returned by a previous call to login.
  459. * @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)..
  460. * @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.
  461. * @return Array 'id' -- the ID of the bean that was written to (-1 on error)
  462. * 'error' -- The SOAP error if any.
  463. */
  464. function set_entry($session,$module_name, $name_value_list){
  465. global $beanList, $beanFiles;
  466. $error = new SoapError();
  467. if(!validate_authenticated($session)){
  468. $error->set_error('invalid_login');
  469. return array('id'=>-1, 'error'=>$error->get_soap_array());
  470. }
  471. if(empty($beanList[$module_name])){
  472. $error->set_error('no_module');
  473. return array('id'=>-1, 'error'=>$error->get_soap_array());
  474. }
  475. global $current_user;
  476. if(!check_modules_access($current_user, $module_name, 'write')){
  477. $error->set_error('no_access');
  478. return array('id'=>-1, 'error'=>$error->get_soap_array());
  479. }
  480. $class_name = $beanList[$module_name];
  481. require_once($beanFiles[$class_name]);
  482. $seed = new $class_name();
  483. foreach($name_value_list as $value){
  484. if($value['name'] == 'id' && isset($value['value']) && strlen($value['value']) > 0){
  485. $result = $seed->retrieve($value['value']);
  486. //bug: 44680 - check to ensure the user has access before proceeding.
  487. if(is_null($result))
  488. {
  489. $error->set_error('no_access');
  490. return array('id'=>-1, 'error'=>$error->get_soap_array());
  491. }
  492. else
  493. {
  494. break;
  495. }
  496. }
  497. }
  498. foreach($name_value_list as $value){
  499. $GLOBALS['log']->debug($value['name']." : ".$value['value']);
  500. $seed->$value['name'] = $value['value'];
  501. }
  502. if(! $seed->ACLAccess('Save') || ($seed->deleted == 1 && !$seed->ACLAccess('Delete')))
  503. {
  504. $error->set_error('no_access');
  505. return array('id'=>-1, 'error'=>$error->get_soap_array());
  506. }
  507. $seed->save();
  508. if($seed->deleted == 1){
  509. $seed->mark_deleted($seed->id);
  510. }
  511. return array('id'=>$seed->id, 'error'=>$error->get_soap_array());
  512. }
  513. $server->register(
  514. 'set_entries',
  515. array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'name_value_lists'=>'tns:name_value_lists'),
  516. array('return'=>'tns:set_entries_result'),
  517. $NAMESPACE);
  518. /**
  519. * Update or create a list of SugarBeans
  520. *
  521. * @param String $session -- Session ID returned by a previous call to login.
  522. * @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)..
  523. * @param Array $name_value_lists -- Array of Bean specific Arrays where the keys of the array are the SugarBean attributes, the values of the array are the values the attributes should have.
  524. * @return Array 'ids' -- Array of the IDs of the beans that was written to (-1 on error)
  525. * 'error' -- The SOAP error if any.
  526. */
  527. function set_entries($session,$module_name, $name_value_lists){
  528. $error = new SoapError();
  529. if(!validate_authenticated($session)){
  530. $error->set_error('invalid_login');
  531. return array(
  532. 'ids' => array(),
  533. 'error' => $error->get_soap_array()
  534. );
  535. }
  536. return handle_set_entries($module_name, $name_value_lists, FALSE);
  537. }
  538. /*
  539. NOTE SPECIFIC CODE
  540. */
  541. $server->register(
  542. 'set_note_attachment',
  543. array('session'=>'xsd:string','note'=>'tns:note_attachment'),
  544. array('return'=>'tns:set_entry_result'),
  545. $NAMESPACE);
  546. /**
  547. * Add or replace the attachment on a Note.
  548. *
  549. * @param String $session -- Session ID returned by a previous call to login.
  550. * @param Binary $note -- The flie contents of the attachment.
  551. * @return Array 'id' -- The ID of the new note or -1 on error
  552. * 'error' -- The SOAP error if any.
  553. */
  554. function set_note_attachment($session,$note)
  555. {
  556. $error = new SoapError();
  557. if(!validate_authenticated($session)){
  558. $error->set_error('invalid_login');
  559. return array('id'=>-1, 'error'=>$error->get_soap_array());
  560. }
  561. require_once('modules/Notes/NoteSoap.php');
  562. $ns = new NoteSoap();
  563. return array('id'=>$ns->saveFile($note), 'error'=>$error->get_soap_array());
  564. }
  565. $server->register(
  566. 'get_note_attachment',
  567. array('session'=>'xsd:string', 'id'=>'xsd:string'),
  568. array('return'=>'tns:return_note_attachment'),
  569. $NAMESPACE);
  570. /**
  571. * Retrieve an attachment from a note
  572. * @param String $session -- Session ID returned by a previous call to login.
  573. * @param Binary $note -- The flie contents of the attachment.
  574. * @return Array 'id' -- The ID of the new note or -1 on error
  575. * 'error' -- The SOAP error if any.
  576. *
  577. * @param String $session -- Session ID returned by a previous call to login.
  578. * @param String $id -- The ID of the appropriate Note.
  579. * @return Array 'note_attachment' -- Array String 'id' -- The ID of the Note containing the attachment
  580. * String 'filename' -- The file name of the attachment
  581. * Binary 'file' -- The binary contents of the file.
  582. * 'error' -- The SOAP error if any.
  583. */
  584. function get_note_attachment($session,$id)
  585. {
  586. $error = new SoapError();
  587. if(!validate_authenticated($session)){
  588. $error->set_error('invalid_login');
  589. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  590. }
  591. $note = new Note();
  592. $note->retrieve($id);
  593. if(!$note->ACLAccess('DetailView')){
  594. $error->set_error('no_access');
  595. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  596. }
  597. require_once('modules/Notes/NoteSoap.php');
  598. $ns = new NoteSoap();
  599. if(!isset($note->filename)){
  600. $note->filename = '';
  601. }
  602. $file= $ns->retrieveFile($id,$note->filename);
  603. if($file == -1){
  604. $error->set_error('no_file');
  605. $file = '';
  606. }
  607. return array('note_attachment'=>array('id'=>$id, 'filename'=>$note->filename, 'file'=>$file), 'error'=>$error->get_soap_array());
  608. }
  609. $server->register(
  610. 'relate_note_to_module',
  611. array('session'=>'xsd:string', 'note_id'=>'xsd:string', 'module_name'=>'xsd:string', 'module_id'=>'xsd:string'),
  612. array('return'=>'tns:error_value'),
  613. $NAMESPACE);
  614. /**
  615. * Attach a note to another bean. Once you have created a note to store an
  616. * attachment, the note needs to be related to the bean.
  617. *
  618. * @param String $session -- Session ID returned by a previous call to login.
  619. * @param String $note_id -- The ID of the note that you want to associate with a bean
  620. * @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)..
  621. * @param String $module_id -- The ID of the bean that you want to associate the note with
  622. * @return no error for success, error for failure
  623. */
  624. function relate_note_to_module($session,$note_id, $module_name, $module_id){
  625. global $beanList, $beanFiles;
  626. $error = new SoapError();
  627. if(!validate_authenticated($session)){
  628. $error->set_error('invalid_login');
  629. return $error->get_soap_array();
  630. }
  631. if(empty($beanList[$module_name])){
  632. $error->set_error('no_module');
  633. return $error->get_soap_array();
  634. }
  635. global $current_user;
  636. if(!check_modules_access($current_user, $module_name, 'read')){
  637. $error->set_error('no_access');
  638. return $error->get_soap_array();
  639. }
  640. $class_name = $beanList['Notes'];
  641. require_once($beanFiles[$class_name]);
  642. $seed = new $class_name();
  643. $seed->retrieve($note_id);
  644. if(!$seed->ACLAccess('ListView')){
  645. $error->set_error('no_access');
  646. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  647. }
  648. if($module_name != 'Contacts'){
  649. $seed->parent_type=$module_name;
  650. $seed->parent_id = $module_id;
  651. }else{
  652. $seed->contact_id=$module_id;
  653. }
  654. $seed->save();
  655. return $error->get_soap_array();
  656. }
  657. $server->register(
  658. 'get_related_notes',
  659. array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'module_id'=>'xsd:string', 'select_fields'=>'tns:select_fields'),
  660. array('return'=>'tns:get_entry_result'),
  661. $NAMESPACE);
  662. /**
  663. * Retrieve the collection of notes that are related to a bean.
  664. *
  665. * @param String $session -- Session ID returned by a previous call to login.
  666. * @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)..
  667. * @param String $module_id -- The ID of the bean that you want to associate the note with
  668. * @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.
  669. * @return Array 'result_count' -- The number of records returned (-1 on error)
  670. * 'next_offset' -- 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.
  671. * 'field_list' -- The vardef information on the selected fields.
  672. * 'entry_list' -- The records that were retrieved
  673. * 'error' -- The SOAP error, if any
  674. */
  675. function get_related_notes($session,$module_name, $module_id, $select_fields){
  676. global $beanList, $beanFiles;
  677. $error = new SoapError();
  678. if(!validate_authenticated($session)){
  679. $error->set_error('invalid_login');
  680. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  681. }
  682. if(empty($beanList[$module_name])){
  683. $error->set_error('no_module');
  684. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  685. }
  686. global $current_user;
  687. if(!check_modules_access($current_user, $module_name, 'read')){
  688. $error->set_error('no_access');
  689. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  690. }
  691. $class_name = $beanList[$module_name];
  692. require_once($beanFiles[$class_name]);
  693. $seed = new $class_name();
  694. $seed->retrieve($module_id);
  695. if(!$seed->ACLAccess('DetailView')){
  696. $error->set_error('no_access');
  697. return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
  698. }
  699. $list = $seed->get_linked_beans('notes','Note', array(), 0, -1, 0);
  700. $output_list = Array();
  701. $field_list = Array();
  702. foreach($list as $value)
  703. {
  704. $output_list[] = get_return_value($value, 'Notes');
  705. if(empty($field_list))
  706. {
  707. $field_list = get_field_list($value);
  708. }
  709. }
  710. $output_list = filter_return_list($output_list, $select_fields, $module_name);
  711. $field_list = filter_field_list($field_list,$select_fields, $module_name);
  712. return array('result_count'=>sizeof($output_list), 'next_offset'=>0,'field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
  713. }
  714. $server->register(
  715. 'logout',
  716. array('session'=>'xsd:string'),
  717. array('return'=>'tns:error_value'),
  718. $NAMESPACE);
  719. /**
  720. * Log out of the session. This will destroy the session and prevent other's from using it.
  721. *
  722. * @param String $session -- Session ID returned by a previous call to login.
  723. * @return Empty error on success, Error on failure
  724. */
  725. function logout($session){
  726. global $current_user;
  727. $error = new SoapError();
  728. LogicHook::initialize();
  729. if(validate_authenticated($session)){
  730. $current_user->call_custom_logic('before_logout');
  731. session_destroy();
  732. $GLOBALS['logic_hook']->call_custom_logic('Users', 'after_logout');
  733. return $error->get_soap_array();
  734. }
  735. $error->set_error('no_session');
  736. $GLOBALS['logic_hook']->call_custom_logic('Users', 'after_logout');
  737. return $error->get_soap_array();
  738. }
  739. $server->register(
  740. 'get_module_fields',
  741. array('session'=>'xsd:string', 'module_name'=>'xsd:string'),
  742. array('return'=>'tns:module_fields'),
  743. $NAMESPACE);
  744. /**
  745. * Retrieve vardef information on the fields of the specified bean.
  746. *
  747. * @param String $session -- Session ID returned by a previous call to login.
  748. * @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)..
  749. * @return Array 'module_fields' -- The vardef information on the selected fields.
  750. * 'error' -- The SOAP error, if any
  751. */
  752. function get_module_fields($session, $module_name){
  753. global $beanList, $beanFiles;
  754. $error = new SoapError();
  755. $module_fields = array();
  756. if(! validate_authenticated($session)){
  757. $error->set_error('invalid_session');
  758. return array('module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
  759. }
  760. if(empty($beanList[$module_name])){
  761. $error->set_error('no_module');
  762. return array('module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
  763. }
  764. global $current_user;
  765. if(!check_modules_access($current_user, $module_name, 'read')){
  766. $error->set_error('no_access');
  767. return array('module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
  768. }
  769. $class_name = $beanList[$module_name];
  770. if(empty($beanFiles[$class_name]))
  771. {
  772. $error->set_error('no_file');
  773. return array('module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
  774. }
  775. require_once($beanFiles[$class_name]);
  776. $seed = new $class_name();
  777. if($seed->ACLAccess('ListView', true) || $seed->ACLAccess('DetailView', true) || $seed->ACLAccess('EditView', true) )
  778. {
  779. return get_return_module_fields($seed, $module_name, $error);
  780. }
  781. else
  782. {
  783. $error->set_error('no_access');
  784. return array('module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
  785. }
  786. }
  787. $server->register(
  788. 'get_available_modules',
  789. array('session'=>'xsd:string'),
  790. array('return'=>'tns:module_list'),
  791. $NAMESPACE);
  792. /**
  793. * Retrieve the list of available modules on the system available to the currently logged in user.
  794. *
  795. * @param String $session -- Session ID returned by a previous call to login.
  796. * @return Array 'modules' -- An array of module names
  797. * 'error' -- The SOAP error, if any
  798. */
  799. function get_available_modules($session){
  800. $error = new SoapError();
  801. $modules = array();
  802. if(! validate_authenticated($session)){
  803. $error->set_error('invalid_session');
  804. return array('modules'=> $modules, 'error'=>$error->get_soap_array());
  805. }
  806. $modules = array_keys($_SESSION['avail_modules']);
  807. return array('modules'=> $modules, 'error'=>$error->get_soap_array());
  808. }
  809. $server->register(
  810. 'update_portal_user',
  811. array('session'=>'xsd:string', 'portal_name'=>'xsd:string', 'name_value_list'=>'tns:name_value_list'),
  812. array('return'=>'tns:error_value'),
  813. $NAMESPACE);
  814. /**
  815. * Update the properties of a contact that is portal user. Add the portal user name to the user's properties.
  816. *
  817. * @param String $session -- Session ID returned by a previous call to login.
  818. * @param String $portal_name -- The portal user_name of the contact
  819. * @param Array $name_value_list -- collection of 'name'=>'value' pairs for finding the contact
  820. * @return Empty error on success, Error on failure
  821. */
  822. function update_portal_user($session,$portal_name, $name_value_list){
  823. global $beanList, $beanFiles;
  824. $error = new SoapError();
  825. if(! validate_authenticated($session)){
  826. $error->set_error('invalid_session');
  827. return $error->get_soap_array();
  828. }
  829. $contact = new Contact();
  830. $searchBy = array('deleted'=>0);
  831. foreach($name_value_list as $name_value){
  832. $searchBy[$name_value['name']] = $name_value['value'];
  833. }
  834. if($contact->retrieve_by_string_fields($searchBy) != null){
  835. if(!$contact->duplicates_found){
  836. $contact->portal_name = $portal_name;
  837. $contact->portal_active = 1;
  838. if($contact->ACLAccess('Save')){
  839. $contact->save();
  840. }else{
  841. $error->set_error('no_access');
  842. }
  843. return $error->get_soap_array();
  844. }
  845. $error->set_error('duplicates');
  846. return $error->get_soap_array();
  847. }
  848. $error->set_error('no_records');
  849. return $error->get_soap_array();
  850. }
  851. $server->register(
  852. 'get_user_id',
  853. array('session'=>'xsd:string'),
  854. array('return'=>'xsd:string'),
  855. $NAMESPACE);
  856. /**
  857. * Return the user_id of the user that is logged into the current session.
  858. *
  859. * @param String $session -- Session ID returned by a previous call to login.
  860. * @return String -- the User ID of the current session
  861. * -1 on error.
  862. */
  863. function get_user_id($session){
  864. if(validate_authenticated($session)){
  865. global $current_user;
  866. return $current_user->id;
  867. }else{
  868. return '-1';
  869. }
  870. }
  871. $server->register(
  872. 'get_user_team_id',
  873. array('session'=>'xsd:string'),
  874. array('return'=>'xsd:string'),
  875. $NAMESPACE);
  876. /**
  877. * Return the ID of the default team for the user that is logged into the current session.
  878. *
  879. * @param String $session -- Session ID returned by a previous call to login.
  880. * @return String -- the Team ID of the current user's default team
  881. * 1 for Community Edition
  882. * -1 on error.
  883. */
  884. function get_user_team_id($session){
  885. if(validate_authenticated($session))
  886. {
  887. return 1;
  888. }else{
  889. return '-1';
  890. }
  891. }
  892. $server->register(
  893. 'get_server_time',
  894. array(),
  895. array('return'=>'xsd:string'),
  896. $NAMESPACE);
  897. /**
  898. * Return the current time on the server in the format 'Y-m-d H:i:s'. This time is in the server's default timezone.
  899. *
  900. * @return String -- The current date/time 'Y-m-d H:i:s'
  901. */
  902. function get_server_time(){
  903. return date('Y-m-d H:i:s');
  904. }
  905. $server->register(
  906. 'get_gmt_time',
  907. array(),
  908. array('return'=>'xsd:string'),
  909. $NAMESPACE);
  910. /**
  911. * Return the current time on the server in the format 'Y-m-d H:i:s'. This time is in GMT.
  912. *
  913. * @return String -- The current date/time 'Y-m-d H:i:s'
  914. */
  915. function get_gmt_time(){
  916. return TimeDate::getInstance()->nowDb();
  917. }
  918. $server->register(
  919. 'get_sugar_flavor',
  920. array(),
  921. array('return'=>'xsd:string'),
  922. $NAMESPACE);
  923. /**
  924. * Retrieve the specific flavor of sugar.
  925. *
  926. * @return String 'CE' -- For Community Edition
  927. * 'PRO' -- For Professional
  928. * 'ENT' -- For Enterprise
  929. */
  930. function get_sugar_flavor(){
  931. global $sugar_flavor;
  932. return $sugar_flavor;
  933. }
  934. $server->register(
  935. 'get_server_version',
  936. array(),
  937. array('return'=>'xsd:string'),
  938. $NAMESPACE);
  939. /**
  940. * Retrieve the version number of Sugar that the server is running.
  941. *
  942. * @return String -- The current sugar version number.
  943. * '1.0' on error.
  944. */
  945. function get_server_version(){
  946. $admin = new Administration();
  947. $admin->retrieveSettings('info');
  948. if(isset($admin->settings['info_sugar_version'])){
  949. return $admin->settings['info_sugar_version'];
  950. }else{
  951. return '1.0';
  952. }
  953. }
  954. $server->register(
  955. 'get_relationships',
  956. array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'module_id'=>'xsd:string', 'related_module'=>'xsd:string', 'related_module_query'=>'xsd:string', 'deleted'=>'xsd:int'),
  957. array('return'=>'tns:get_relationships_result'),
  958. $NAMESPACE);
  959. /**
  960. * Retrieve a collection of beans tha are related to the specified bean.
  961. * As of 4.5.1c, all combinations of related modules are supported
  962. *
  963. * @param String $session -- Session ID returned by a previous call to login.
  964. * @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)..
  965. * @param String $module_id -- The ID of the bean in the specified module
  966. * @param String $related_module -- The name of the related 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)..
  967. * @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.
  968. * @param Number $deleted -- false if deleted records should not be include, true if deleted records should be included.
  969. * @return unknown
  970. */
  971. function get_relationships($session, $module_name, $module_id, $related_module, $related_module_query, $deleted){
  972. $error = new SoapError();
  973. $ids = array();
  974. if(!validate_authenticated($session)){
  975. $error->set_error('invalid_login');
  976. return array('ids'=>$ids,'error'=> $error->get_soap_array());
  977. }
  978. global $beanList, $beanFiles;
  979. $error = new SoapError();
  980. if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
  981. $error->set_error('no_module');
  982. return array('ids'=>$ids, 'error'=>$error->get_soap_array());
  983. }
  984. $class_name = $beanList[$module_name];
  985. require_once($beanFiles[$class_name]);
  986. $mod = new $class_name();
  987. $mod->retrieve($module_id);
  988. if(!$mod->ACLAccess('DetailView')){
  989. $error->set_error('no_access');
  990. return array('ids'=>$ids, 'error'=>$error->get_soap_array());
  991. }
  992. require_once 'include/SugarSQLValidate.php';
  993. $valid = new SugarSQLValidate();
  994. if(!$valid->validateQueryClauses($related_module_query)) {
  995. $GLOBALS['log']->error("Bad query: $related_module_query");
  996. $error->set_error('no_access');
  997. return array(
  998. 'result_count' => -1,
  999. 'error' => $error->get_soap_array()
  1000. );
  1001. }
  1002. $id_list = get_linked_records($related_module, $module_name, $module_id);
  1003. if ($id_list === FALSE) {
  1004. $error->set_error('no_relationship_support');
  1005. return array('ids'=>$ids, 'error'=>$error->get_soap_array());
  1006. }
  1007. elseif (count($id_list) == 0) {
  1008. return array('ids'=>$ids, 'error'=>$error->get_soap_array());
  1009. }
  1010. $list = array();
  1011. $in = "'".implode("', '", $id_list)."'";
  1012. $related_class_name = $beanList[$related_module];
  1013. require_once($beanFiles[$related_class_name]);
  1014. $related_mod = new $related_class_name();
  1015. $sql = "SELECT {$related_mod->table_name}.id FROM {$related_mod->table_name} ";
  1016. $sql .= " WHERE {$related_mod->table_name}.id IN ({$in}) ";
  1017. if (!empty($related_module_query)) {
  1018. $sql .= " AND ( {$related_module_query} )";
  1019. }
  1020. $result = $related_mod->db->query($sql);
  1021. while ($row = $related_mod->db->fetchByAssoc($result)) {
  1022. $list[] = $row['id'];
  1023. }
  1024. $return_list = array();
  1025. foreach($list as $id) {
  1026. $related_class_name = $beanList[$related_module];
  1027. $related_mod = new $related_class_name();
  1028. $related_mod->retrieve($id);
  1029. $return_list[] = array(
  1030. 'id' => $id,
  1031. 'date_modified' => $related_mod->date_modified,
  1032. 'deleted' => $related_mod->deleted
  1033. );
  1034. }
  1035. return array('ids' => $return_list, 'error' => $error->get_soap_array());
  1036. }
  1037. $server->register(
  1038. 'set_relationship',
  1039. array('session'=>'xsd:string','set_relationship_value'=>'tns:set_relationship_value'),
  1040. array('return'=>'tns:error_value'),
  1041. $NAMESPACE);
  1042. /**
  1043. * Set a single relationship between two beans. The items are related by module name and id.
  1044. *
  1045. * @param String $session -- Session ID returned by a previous call to login.
  1046. * @param Array $set_relationship_value --
  1047. * 'module1' -- 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)..
  1048. * 'module1_id' -- The ID of the bean in the specified module
  1049. * 'module2' -- The name of the module that the related 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)..
  1050. * 'module2_id' -- The ID of the bean in the specified module
  1051. * @return Empty error on success, Error on failure
  1052. */
  1053. function set_relationship($session, $set_relationship_value){
  1054. $error = new SoapError();
  1055. if(!validate_authenticated($session)){
  1056. $error->set_error('invalid_login');
  1057. return $error->get_soap_array();
  1058. }
  1059. return handle_set_relationship($set_relationship_value, $session);
  1060. }
  1061. $server->register(
  1062. 'set_relationships',
  1063. array('session'=>'xsd:string','set_relationship_list'=>'tns:set_relationship_list'),
  1064. array('return'=>'tns:set_relationship_list_result'),
  1065. $NAMESPACE);
  1066. /**
  1067. * Setup several relationships between pairs of beans. The items are related by module name and id.
  1068. *
  1069. * @param String $session -- Session ID returned by a previous call to login.
  1070. * @param Array $set_relationship_list -- One for each relationship to setup. Each entry is itself an array.
  1071. * 'module1' -- 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)..
  1072. * 'module1_id' -- The ID of the bean in the specified module
  1073. * 'module2' -- The name of the module that the related 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)..
  1074. * 'module2_id' -- The ID of the bean in the specified module
  1075. * @return Empty error on success, Error on failure
  1076. */
  1077. function set_relationships($session, $set_relationship_list){
  1078. $error = new SoapError();
  1079. if(!validate_authenticated($session)){
  1080. $error->set_error('invalid_login');
  1081. return -1;
  1082. }
  1083. $count = 0;
  1084. $failed = 0;
  1085. foreach($set_relationship_list as $set_relationship_value){
  1086. $reter = handle_set_relationship($set_relationship_value, $session);
  1087. if($reter['number'] == 0){
  1088. $count++;
  1089. }else{
  1090. $failed++;
  1091. }
  1092. }
  1093. return array('created'=>$count , 'failed'=>$failed, 'error'=>$error);
  1094. }
  1095. //INTERNAL FUNCTION NOT EXPOSED THROUGH SOAP
  1096. /**
  1097. * (Internal) Create a relationship between two beans.
  1098. *
  1099. * @param Array $set_relationship_value --
  1100. * 'module1' -- 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)..
  1101. * 'module1_id' -- The ID of the bean in the specified module
  1102. * 'module2' -- The name of the module that the related 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)..
  1103. * 'module2_id' -- The ID of the bean in the specified module
  1104. * @return Empty error on success, Error on failure
  1105. */
  1106. function handle_set_relationship($set_relationship_value, $session='')
  1107. {
  1108. global $beanList, $beanFiles;
  1109. $error = new SoapError();
  1110. $module1 = $set_relationship_value['module1'];
  1111. $module1_id = $set_relationship_value['module1_id'];
  1112. $module2 = $set_relationship_value['module2'];
  1113. $module2_id = $set_relationship_value['module2_id'];
  1114. if(empty($beanList[$module1]) || empty($beanList[$module2]) )
  1115. {
  1116. $error->set_error('no_module');
  1117. return $error->get_soap_array();
  1118. }
  1119. $class_name = $beanList[$module1];
  1120. require_once($beanFiles[$class_name]);
  1121. $mod = new $class_name();
  1122. $mod->retrieve($module1_id);
  1123. if(!$mod->ACLAccess('DetailView')){
  1124. $error->set_error('no_access');
  1125. return $error->get_soap_array();
  1126. }
  1127. if($module1 == "Contacts" && $module2 == "Users"){
  1128. $key = 'contacts_users_id';
  1129. }
  1130. else{
  1131. $key = array_search(strtolower($module2),$mod->relationship_fields);
  1132. if(!$key) {
  1133. $key = Relationship::retrieve_by_modules($module1, $module2, $GLOBALS['db']);
  1134. // BEGIN SnapLogic fix for bug 32064
  1135. if ($module1 == "Quotes" && $module2 == "ProductBundles") {
  1136. // Alternative solution is perhaps to
  1137. // do whatever Sugar does when the same
  1138. // request is received from the web:
  1139. $pb_cls = $beanList[$module2];
  1140. $pb = new $pb_cls();
  1141. $pb->retrieve($module2_id);
  1142. // Check if this relationship already exists
  1143. $query = "SELECT count(*) AS count FROM product_bundle_quote WHERE quote_id = '{$module1_id}' AND bundle_id = '{$module2_id}' AND deleted = '0'";
  1144. $result = $GLOBALS['db']->query($query, true, "Error checking for previously existing relationship between quote and product_bundle");
  1145. $row = $GLOBALS['db']->fetchByAssoc($result);
  1146. if(isset($row['count']) && $row['count'] > 0){
  1147. return $error->get_soap_array();
  1148. }
  1149. $query = "SELECT MAX(bundle_index)+1 AS idx FROM product_bundle_quote WHERE quote_id = '{$module1_id}' AND deleted='0'";
  1150. $result = $GLOBALS['db']->query($query, true, "Error getting bundle_index");
  1151. $GLOBALS['log']->debug("*********** Getting max bundle_index");
  1152. $GLOBALS['log']->debug($query);
  1153. $row = $GLOBALS['db']->fetchByAssoc($result);
  1154. $idx = 0;
  1155. if ($row) {
  1156. $idx = $row['idx'];
  1157. }
  1158. $pb->set_productbundle_quote_relationship($module1_id,$module2_id,$idx);
  1159. $pb->save();
  1160. return $error->get_soap_array();
  1161. } else if ($module1 == "ProductBundles" && $module2 == "Products") {
  1162. // And, well, similar things apply in this case
  1163. $pb_cls = $beanList[$module1];
  1164. $pb = new $pb_cls();
  1165. $pb->retrieve($module1_id);
  1166. // Check if this relationship already exists

Large files files are truncated, but you can click here to view the full file