]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/core/SugarWebServiceImpl.php
Release 6.5.0
[Github/sugarcrm.git] / service / core / SugarWebServiceImpl.php
1 <?php
2 if(!defined('sugarEntry'))define('sugarEntry', true);
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 /**
40  * This class is an implemenatation class for all the web services
41  */
42 require_once('service/core/SoapHelperWebService.php');
43 SugarWebServiceImpl::$helperObject = new SoapHelperWebServices();
44
45 class SugarWebServiceImpl{
46
47         public static $helperObject = null;
48
49 /**
50  * Retrieve a single SugarBean based on ID.
51  *
52  * @param String $session -- Session ID returned by a previous call to login.
53  * @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)..
54  * @param String $id -- The SugarBean's ID value.
55  * @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.
56 * @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')))
57 * @return Array
58 *        'entry_list' -- Array - The records name value pair for the simple data types excluding link field data.
59 *            '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 ) ) ) ) )
60 * @exception 'SoapFault' -- The SOAP error, if any
61 */
62 function get_entry($session, $module_name, $id,$select_fields, $link_name_to_fields_array){
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);
65         $GLOBALS['log']->info('end: SugarWebServiceImpl->get_entry');
66 }
67
68 /**
69  * Retrieve a list of SugarBean's based on provided IDs. This API will not wotk with report module
70  *
71  * @param String $session -- Session ID returned by a previous call to login.
72  * @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)..
73  * @param Array $ids -- An array of SugarBean IDs.
74  * @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.
75 * @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')))
76 * @return Array
77 *        'entry_list' -- Array - The records name value pair for the simple data types excluding link field data.
78 *            '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 ) ) ) ) )
79 * @exception 'SoapFault' -- The SOAP error, if any
80 */
81 function get_entries($session, $module_name, $ids, $select_fields, $link_name_to_fields_array){
82         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_entries');
83         global  $beanList, $beanFiles;
84         $error = new SoapError();
85
86         $linkoutput_list = array();
87         $output_list = array();
88     $using_cp = false;
89     if($module_name == 'CampaignProspects'){
90         $module_name = 'Prospects';
91         $using_cp = true;
92     }
93         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
94                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entries');
95                 return;
96         } // if
97
98         if($module_name == 'Reports'){
99                 $error->set_error('invalid_call_error');
100                 self::$helperObject->setFaultObject($error);
101                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entries');
102                 return;
103         }
104
105         $class_name = $beanList[$module_name];
106         require_once($beanFiles[$class_name]);
107
108         $temp = new $class_name();
109         foreach($ids as $id) {
110                 $seed = @clone($temp);
111             if($using_cp){
112                 $seed = $seed->retrieveTarget($id);
113             }else{
114                         if ($seed->retrieve($id) == null)
115                                 $seed->deleted = 1;
116                 }
117
118                 if ($seed->deleted == 1) {
119                         $list = array();
120                         $list[] = array('name'=>'warning', 'value'=>'Access to this object is denied since it has been deleted or does not exist');
121                         $list[] = array('name'=>'deleted', 'value'=>'1');
122                         $output_list[] = Array('id'=>$id,
123                                                                         'module_name'=> $module_name,
124                                                                         'name_value_list'=>$list,
125                                                                         );
126                         continue;
127     }
128             if (!self::$helperObject->checkACLAccess($seed, 'DetailView', $error, 'no_access')) {
129                 return;
130             }
131                 $output_list[] = self::$helperObject->get_return_value_for_fields($seed, $module_name, $select_fields);
132                 if (!empty($link_name_to_fields_array)) {
133                         $linkoutput_list[] = self::$helperObject->get_return_value_for_link_fields($seed, $module_name, $link_name_to_fields_array);
134                 }
135         }
136         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entries');
137         return array('entry_list'=>$output_list, 'relationship_list' => $linkoutput_list);
138 }
139
140
141 /**
142  * Retrieve a list of beans.  This is the primary method for getting list of SugarBeans from Sugar using the SOAP API.
143  *
144  * @param String $session -- Session ID returned by a previous call to login.
145  * @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)..
146  * @param String $query -- SQL where clause without the word 'where'
147  * @param String $order_by -- SQL order by clause without the phrase 'order by'
148  * @param integer $offset -- The record offset to start from.
149  * @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.
150  * @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')))
151 * @param integer $max_results -- The maximum number of records to return.  The default is the sugar configuration value for 'list_max_entries_per_page'
152  * @param integer $deleted -- false if deleted records should not be include, true if deleted records should be included.
153  * @return Array 'result_count' -- integer - The number of records returned
154  *               '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.
155  *               'entry_list' -- Array - The records that were retrieved
156  *                       '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 ) ) ) ) )
157 * @exception 'SoapFault' -- The SOAP error, if any
158 */
159 function get_entry_list($session, $module_name, $query, $order_by,$offset, $select_fields, $link_name_to_fields_array, $max_results, $deleted ){
160         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_entry_list');
161         global  $beanList, $beanFiles;
162         $error = new SoapError();
163     $using_cp = false;
164     if($module_name == 'CampaignProspects'){
165         $module_name = 'Prospects';
166         $using_cp = true;
167     }
168         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
169                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
170                 return;
171         } // if
172
173         // If the maximum number of entries per page was specified, override the configuration value.
174         if($max_results > 0){
175                 global $sugar_config;
176                 $sugar_config['list_max_entries_per_page'] = $max_results;
177         } // if
178
179         $class_name = $beanList[$module_name];
180         require_once($beanFiles[$class_name]);
181         $seed = new $class_name();
182
183     if (!self::$helperObject->checkQuery($error, $query, $order_by)) {
184                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
185         return;
186     } // if
187
188     if (!self::$helperObject->checkACLAccess($seed, 'Export', $error, 'no_access')) {
189                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
190         return;
191     } // if
192
193     if (!self::$helperObject->checkACLAccess($seed, 'list', $error, 'no_access')) {
194                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
195         return;
196     } // if
197
198         if($query == ''){
199                 $where = '';
200         } // if
201         if($offset == '' || $offset == -1){
202                 $offset = 0;
203         } // if
204     if($using_cp){
205         $response = $seed->retrieveTargetList($query, $select_fields, $offset,-1,-1,$deleted);
206     }else{
207         /* @var $seed SugarBean */
208            $response = $seed->get_list($order_by, $query, $offset,-1,-1,$deleted, false, $select_fields);
209     } // else
210         $list = $response['list'];
211
212         $output_list = array();
213         $linkoutput_list = array();
214
215         foreach($list as $value) {
216                 if(isset($value->emailAddress)){
217                         $value->emailAddress->handleLegacyRetrieve($value);
218                 } // if
219                 $value->fill_in_additional_detail_fields();
220
221                 $output_list[] = self::$helperObject->get_return_value_for_fields($value, $module_name, $select_fields);
222                 if(!empty($link_name_to_fields_array)){
223                         $linkoutput_list[] = self::$helperObject->get_return_value_for_link_fields($value, $module_name, $link_name_to_fields_array);
224                 }
225         } // foreach
226
227         // Calculate the offset for the start of the next page
228         $next_offset = $offset + sizeof($output_list);
229
230         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entry_list');
231         return array('result_count'=>sizeof($output_list), 'next_offset'=>$next_offset, 'entry_list'=>$output_list, 'relationship_list' => $linkoutput_list);
232 } // fn
233
234
235 /**
236  * Set a single relationship between two beans.  The items are related by module name and id.
237  *
238  * @param String $session -- Session ID returned by a previous call to login.
239  * @param String $module_name -- 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)..
240  * @param String $module_id - The ID of the bean in the specified module_name
241  * @param String link_field_name -- name of the link field which relates to the other module for which the relationship needs to be generated.
242  * @param array related_ids -- array of related record ids for which relationships needs to be generated
243  * @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.
244  * @param integer $delete -- Optional, if the value 0 or nothing is passed then it will add the relationship for related_ids and if 1 is passed, it will delete this relationship for related_ids
245  * @return Array - created - integer - How many relationships has been created
246  *               - failed - integer - How many relationsip creation failed
247  *                               - deleted - integer - How many relationships were deleted
248  * @exception 'SoapFault' -- The SOAP error, if any
249  */
250 function set_relationship($session, $module_name, $module_id, $link_field_name, $related_ids, $name_value_list, $delete){
251         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->set_relationship');
252         $error = new SoapError();
253         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
254                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_relationship');
255                 return;
256         } // if
257
258         $count = 0;
259         $deletedCount = 0;
260         $failed = 0;
261         $deleted = 0;
262         $name_value_array = array();
263         if (is_array($name_value_list)) {
264                 $name_value_array = $name_value_list;
265         }
266
267         if (isset($delete)) {
268                 $deleted = $delete;
269         }
270         if (self::$helperObject->new_handle_set_relationship($module_name, $module_id, $link_field_name, $related_ids,$name_value_array, $deleted)) {
271                 if ($deleted) {
272                         $deletedCount++;
273                 } else {
274                         $count++;
275                 }
276         } else {
277                 $failed++;
278         } // else
279         $GLOBALS['log']->info('End: SugarWebServiceImpl->set_relationship');
280         return array('created'=>$count , 'failed'=>$failed, 'deleted' => $deletedCount);
281 }
282
283 /**
284  * Set a single relationship between two beans.  The items are related by module name and id.
285  *
286  * @param String $session -- Session ID returned by a previous call to login.
287  * @param array $module_names -- Array of 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)..
288  * @param array $module_ids - The array of ID of the bean in the specified module_name
289  * @param array $link_field_names -- Array of the name of the link field which relates to the other module for which the relationships needs to be generated.
290  * @param array $related_ids -- array of an array of related record ids for which relationships needs to be generated
291  * @param array $name_value_lists -- Array of Array. The keys of the inner array are the SugarBean attributes, the values of the inner array are the values the attributes should have.
292  * @param array int $delete_array -- Optional, array of 0 or 1. If the value 0 or nothing is passed then it will add the relationship for related_ids and if 1 is passed, it will delete this relationship for related_ids
293  * @return Array - created - integer - How many relationships has been created
294  *               - failed - integer - How many relationsip creation failed
295  *                               - deleted - integer - How many relationships were deleted
296 *
297  * @exception 'SoapFault' -- The SOAP error, if any
298 */
299 function set_relationships($session, $module_names, $module_ids, $link_field_names, $related_ids, $name_value_lists, $delete_array) {
300         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->set_relationships');
301         $error = new SoapError();
302         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
303                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_relationships');
304                 return;
305         } // if
306
307         if ((empty($module_names) || empty($module_ids) || empty($link_field_names) || empty($related_ids)) ||
308                 (sizeof($module_names) != (sizeof($module_ids) || sizeof($link_field_names) || sizeof($related_ids)))) {
309                 $error->set_error('invalid_data_format');
310                 self::$helperObject->setFaultObject($error);
311                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_relationships');
312                 return;
313         } // if
314
315         $count = 0;
316         $deletedCount = 0;
317         $failed = 0;
318         $counter = 0;
319         $deleted = 0;
320         foreach($module_names as $module_name) {
321                 $name_value_list = array();
322                 if (is_array($name_value_lists) && isset($name_value_lists[$counter])) {
323                         $name_value_list = $name_value_lists[$counter];
324                 }
325                 if (is_array($delete_array) && isset($delete_array[$counter])) {
326                         $deleted = $delete_array[$counter];
327                 }
328                 if (self::$helperObject->new_handle_set_relationship($module_name, $module_ids[$counter], $link_field_names[$counter], $related_ids[$counter], $name_value_list, $deleted)) {
329                         if ($deleted) {
330                                 $deletedCount++;
331                         } else {
332                                 $count++;
333                         }
334                 } else {
335                         $failed++;
336                 } // else
337                 $counter++;
338         } // foreach
339         $GLOBALS['log']->info('End: SugarWebServiceImpl->set_relationships');
340         return array('created'=>$count , 'failed'=>$failed, 'deleted' => $deletedCount);
341 } // fn
342
343 /**
344  * Retrieve a collection of beans that are related to the specified bean and optionally return relationship data for those related beans.
345  * So in this API you can get contacts info for an account and also return all those contact's email address or an opportunity info also.
346  *
347  * @param String $session -- Session ID returned by a previous call to login.
348  * @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)..
349  * @param String $module_id -- The ID of the bean in the specified module
350  * @param String $link_field_name -- The name of the lnk field to return records from.  This name should be the name the relationship.
351  * @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. (IGNORED)
352  * @param Array $related_fields - Array of related bean fields to be returned.
353  * @param Array $related_module_link_name_to_fields_array - For every related bean returrned, specify link fields name to fields info for that bean to be returned. For ex.'link_name_to_fields_array' => array(array('name' =>  'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address'))).
354  * @param Number $deleted -- false if deleted records should not be include, true if deleted records should be included.
355  * @return Array 'entry_list' -- Array - The records that were retrieved
356  *                       'relationship_list' -- Array - The records link field data. The example is if asked about accounts contacts email address then return data would look like Array ( [0] => Array ( [name] => email_addresses [records] => Array ( [0] => Array ( [0] => Array ( [name] => id [value] => 3fb16797-8d90-0a94-ac12-490b63a6be67 ) [1] => Array ( [name] => email_address [value] => hr.kid.qa@example.com ) [2] => Array ( [name] => opt_out [value] => 0 ) [3] => Array ( [name] => primary_address [value] => 1 ) ) [1] => Array ( [0] => Array ( [name] => id [value] => 403f8da1-214b-6a88-9cef-490b63d43566 ) [1] => Array ( [name] => email_address [value] => kid.hr@example.name ) [2] => Array ( [name] => opt_out [value] => 0 ) [3] => Array ( [name] => primary_address [value] => 0 ) ) ) ) )
357 * @exception 'SoapFault' -- The SOAP error, if any
358 */
359 function get_relationships($session, $module_name, $module_id, $link_field_name, $related_module_query, $related_fields, $related_module_link_name_to_fields_array, $deleted){
360
361         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_relationships');
362         global  $beanList, $beanFiles;
363         $error = new SoapError();
364         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
365                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
366                 return;
367         } // if
368
369         $class_name = $beanList[$module_name];
370         require_once($beanFiles[$class_name]);
371         $mod = new $class_name();
372         $mod->retrieve($module_id);
373
374     if (!self::$helperObject->checkQuery($error, $related_module_query)) {
375                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
376         return;
377     } // if
378
379         if (!self::$helperObject->checkACLAccess($mod, 'DetailView', $error, 'no_access')) {
380                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
381         return;
382     } // if
383
384     $output_list = array();
385         $linkoutput_list = array();
386
387         // get all the related mmodules data.
388     $result = self::$helperObject->getRelationshipResults($mod, $link_field_name, $related_fields, $related_module_query);
389     if (self::$helperObject->isLogLevelDebug()) {
390                 $GLOBALS['log']->debug('SoapHelperWebServices->get_relationships - return data for getRelationshipResults is ' . var_export($result, true));
391     } // if
392         if ($result) {
393                 $list = $result['rows'];
394                 $filterFields = $result['fields_set_on_rows'];
395
396                 if (sizeof($list) > 0) {
397                         // get the related module name and instantiate a bean for that.
398                         $submodulename = $mod->$link_field_name->getRelatedModuleName();
399                         $submoduleclass = $beanList[$submodulename];
400                         require_once($beanFiles[$submoduleclass]);
401
402                         $submoduletemp = new $submoduleclass();
403                         foreach($list as $row) {
404                                 $submoduleobject = @clone($submoduletemp);
405                                 // set all the database data to this object
406                                 foreach ($filterFields as $field) {
407                                         $submoduleobject->$field = $row[$field];
408                                 } // foreach
409                                 if (isset($row['id'])) {
410                                         $submoduleobject->id = $row['id'];
411                                 }
412                                 $output_list[] = self::$helperObject->get_return_value_for_fields($submoduleobject, $submodulename, $filterFields);
413                                 if (!empty($related_module_link_name_to_fields_array)) {
414                                         $linkoutput_list[] = self::$helperObject->get_return_value_for_link_fields($submoduleobject, $submodulename, $related_module_link_name_to_fields_array);
415                                 } // if
416
417                         } // foreach
418                 }
419
420         } // if
421
422         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
423         return array('entry_list'=>$output_list, 'relationship_list' => $linkoutput_list);
424
425 } // fn
426
427 /**
428  * Update or create a single SugarBean.
429  *
430  * @param String $session -- Session ID returned by a previous call to login.
431  * @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)..
432  * @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.
433  * @return Array    'id' -- the ID of the bean that was written to (-1 on error)
434  * @exception 'SoapFault' -- The SOAP error, if any
435 */
436 function set_entry($session,$module_name, $name_value_list){
437         global  $beanList, $beanFiles, $current_user;
438
439         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->set_entry');
440     if (self::$helperObject->isLogLevelDebug()) {
441                 $GLOBALS['log']->debug('SoapHelperWebServices->set_entry - input data is ' . var_export($name_value_list, true));
442     } // if
443         $error = new SoapError();
444         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'write', 'no_access', $error)) {
445                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_entry');
446                 return;
447         } // if
448         $class_name = $beanList[$module_name];
449         require_once($beanFiles[$class_name]);
450         $seed = new $class_name();
451         foreach($name_value_list as $name=>$value){
452                 if(is_array($value) &&  $value['name'] == 'id'){
453                         $seed->retrieve($value['value']);
454                         break;
455                 }else if($name === 'id' ){
456
457                         $seed->retrieve($value);
458                 }
459         }
460
461         foreach($name_value_list as $name=>$value){
462                 if($module_name == 'Users' && !empty($seed->id) && ($seed->id != $current_user->id) && $name == 'user_hash'){
463                         continue;
464                 }
465                 if(!is_array($value)){
466                         $seed->$name = $value;
467                 }else{
468                         $seed->$value['name'] = $value['value'];
469                 }
470         }
471     if (!self::$helperObject->checkACLAccess($seed, 'Save', $error, 'no_access') || ($seed->deleted == 1  && !self::$helperObject->checkACLAccess($seed, 'Delete', $error, 'no_access'))) {
472                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_entry');
473         return;
474     } // if
475
476         $seed->save(self::$helperObject->checkSaveOnNotify());
477         if($seed->deleted == 1){
478                 $seed->mark_deleted($seed->id);
479         }
480         $GLOBALS['log']->info('End: SugarWebServiceImpl->set_entry');
481         return array('id'=>$seed->id);
482 } // fn
483
484 /**
485  * Update or create a list of SugarBeans
486  *
487  * @param String $session -- Session ID returned by a previous call to login.
488  * @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)..
489  * @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.
490  * @return Array    'ids' -- Array of the IDs of the beans that was written to (-1 on error)
491  * @exception 'SoapFault' -- The SOAP error, if any
492  */
493 function set_entries($session,$module_name, $name_value_lists){
494         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->set_entries');
495     if (self::$helperObject->isLogLevelDebug()) {
496                 $GLOBALS['log']->debug('SoapHelperWebServices->set_entries - input data is ' . var_export($name_value_lists, true));
497     } // if
498         $error = new SoapError();
499         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'write', 'no_access', $error)) {
500                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_entries');
501                 return;
502         } // if
503
504         $GLOBALS['log']->info('End: SugarWebServiceImpl->set_entries');
505         return self::$helperObject->new_handle_set_entries($module_name, $name_value_lists, FALSE);
506 }
507
508 /**
509  * Log the user into the application
510  *
511  * @param UserAuth array $user_auth -- Set user_name and password (password needs to be
512  *      in the right encoding for the type of authentication the user is setup for.  For Base
513  *      sugar validation, password is the MD5 sum of the plain text password.
514  * @param String $application -- The name of the application you are logging in from.  (Currently unused).
515  * @param array $name_value_list -- Array of name value pair of extra parameters. As of today only 'language' and 'notifyonsave' is supported
516  * @return Array - id - String id is the session_id of the session that was created.
517  *                               - module_name - String - module name of user
518  *                               - name_value_list - Array - The name value pair of user_id, user_name, user_language, user_currency_id, user_currency_name
519  * @exception 'SoapFault' -- The SOAP error, if any
520  */
521 public function login($user_auth, $application, $name_value_list){
522         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->login');
523         global $sugar_config, $system_config;
524         $error = new SoapError();
525         $user = new User();
526         $success = false;
527         if(!empty($user_auth['encryption']) && $user_auth['encryption'] === 'PLAIN'){
528                 $user_auth['password'] = md5($user_auth['password']);
529         }
530         //rrs
531                 $system_config = new Administration();
532         $system_config->retrieveSettings('system');
533         $authController = new AuthenticationController((!empty($sugar_config['authenticationClass'])? $sugar_config['authenticationClass'] : 'SugarAuthenticate'));
534         //rrs
535         $isLoginSuccess = $authController->login($user_auth['user_name'], $user_auth['password'], array('passwordEncrypted' => true));
536         $usr_id=$user->retrieve_user_id($user_auth['user_name']);
537         if($usr_id) {
538                 $user->retrieve($usr_id);
539         }
540         if ($isLoginSuccess) {
541                 if ($_SESSION['hasExpiredPassword'] =='1') {
542                         $error->set_error('password_expired');
543                         $GLOBALS['log']->fatal('password expired for user ' . $user_auth['user_name']);
544                         LogicHook::initialize();
545                         $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
546                         self::$helperObject->setFaultObject($error);
547                         return;
548                 } // if
549                 if(!empty($user) && !empty($user->id) && !$user->is_group) {
550                         $success = true;
551                         global $current_user;
552                         $current_user = $user;
553                 } // if
554         } else if($usr_id && isset($user->user_name) && ($user->getPreference('lockout') == '1')) {
555                         $error->set_error('lockout_reached');
556                         $GLOBALS['log']->fatal('Lockout reached for user ' . $user_auth['user_name']);
557                         LogicHook::initialize();
558                         $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
559                         self::$helperObject->setFaultObject($error);
560                         return;
561         } else if(function_exists('mcrypt_cbc')){
562                 $password = self::$helperObject->decrypt_string($user_auth['password']);
563                 if($authController->login($user_auth['user_name'], $password) && isset($_SESSION['authenticated_user_id'])){
564                         $success = true;
565                 } // if
566         } // else if
567
568         if($success){
569                 session_start();
570                 global $current_user;
571                 //$current_user = $user;
572                 self::$helperObject->login_success($name_value_list);
573                 $current_user->loadPreferences();
574                 $_SESSION['is_valid_session']= true;
575                 $_SESSION['ip_address'] = query_client_ip();
576                 $_SESSION['user_id'] = $current_user->id;
577                 $_SESSION['type'] = 'user';
578                 $_SESSION['avail_modules']= self::$helperObject->get_user_module_list($current_user);
579                 $_SESSION['authenticated_user_id'] = $current_user->id;
580                 $_SESSION['unique_key'] = $sugar_config['unique_key'];
581                 $current_user->call_custom_logic('after_login');
582                 $GLOBALS['log']->info('End: SugarWebServiceImpl->login - succesful login');
583                 $nameValueArray = array();
584                 global $current_language;
585                 $nameValueArray['user_id'] = self::$helperObject->get_name_value('user_id', $current_user->id);
586                 $nameValueArray['user_name'] = self::$helperObject->get_name_value('user_name', $current_user->user_name);
587                 $nameValueArray['user_language'] = self::$helperObject->get_name_value('user_language', $current_language);
588                 $cur_id = $current_user->getPreference('currency');
589                 $nameValueArray['user_currency_id'] = self::$helperObject->get_name_value('user_currency_id', $cur_id);
590                 $currencyObject = new Currency();
591                 $currencyObject->retrieve($cur_id);
592                 $nameValueArray['user_currency_name'] = self::$helperObject->get_name_value('user_currency_name', $currencyObject->name);
593                 $_SESSION['user_language'] = $current_language;
594                 return array('id'=>session_id(), 'module_name'=>'Users', 'name_value_list'=>$nameValueArray);
595 } // if
596         LogicHook::initialize();
597         $GLOBALS['logic_hook']->call_custom_logic('Users', 'login_failed');
598         $error->set_error('invalid_login');
599         self::$helperObject->setFaultObject($error);
600         $GLOBALS['log']->info('End: SugarWebServiceImpl->login - failed login');
601 }
602
603 /**
604  * Log out of the session.  This will destroy the session and prevent other's from using it.
605  *
606  * @param String $session -- Session ID returned by a previous call to login.
607  * @return Empty
608  * @exception 'SoapFault' -- The SOAP error, if any
609  */
610 function logout($session){
611         global $current_user;
612
613         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->logout');
614         $error = new SoapError();
615         LogicHook::initialize();
616         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
617                 $GLOBALS['logic_hook']->call_custom_logic('Users', 'after_logout');
618                 $GLOBALS['log']->info('End: SugarWebServiceImpl->logout');
619                 return;
620         } // if
621
622         $current_user->call_custom_logic('before_logout');
623         session_destroy();
624         $GLOBALS['logic_hook']->call_custom_logic('Users', 'after_logout');
625         $GLOBALS['log']->info('End: SugarWebServiceImpl->logout');
626 } // fn
627
628 /**
629  * Gets server info. This will return information like version, flavor and gmt_time.
630  * @return Array - flavor - String - Retrieve the specific flavor of sugar.
631  *                               - version - String - Retrieve the version number of Sugar that the server is running.
632  *                               - gmt_time - String - Return the current time on the server in the format 'Y-m-d H:i:s'. This time is in GMT.
633  * @exception 'SoapFault' -- The SOAP error, if any
634  */
635 function get_server_info(){
636         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_server_info');
637         global $sugar_flavor;
638         require_once('sugar_version.php');
639         require_once('modules/Administration/Administration.php');
640
641         $admin  = new Administration();
642         $admin->retrieveSettings('info');
643         $sugar_version = '';
644         if(isset($admin->settings['info_sugar_version'])){
645                 $sugar_version = $admin->settings['info_sugar_version'];
646         }else{
647                 $sugar_version = '1.0';
648         }
649
650         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_server_info');
651         return array('flavor' => $sugar_flavor, 'version' => $sugar_version, 'gmt_time' => TimeDate::getInstance()->nowDb());
652 } // fn
653
654 /**
655  * Return the user_id of the user that is logged into the current session.
656  *
657  * @param String $session -- Session ID returned by a previous call to login.
658  * @return String -- the User ID of the current session
659  * @exception 'SoapFault' -- The SOAP error, if any
660  */
661 function get_user_id($session){
662         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_user_id');
663         $error = new SoapError();
664         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
665                 return;
666         } // if
667         global $current_user;
668         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_user_id');
669         return $current_user->id;
670 } // fn
671
672 /**
673  * Retrieve vardef information on the fields of the specified bean.
674  *
675  * @param String $session -- Session ID returned by a previous call to login.
676  * @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)..
677  * @param Array $fields -- Optional, if passed then retrieve vardef information on these fields only.
678  * @return Array    'module_fields' -- Array - The vardef information on the selected fields.
679  *                  'link_fields' -- Array - The vardef information on the link fields
680  * @exception 'SoapFault' -- The SOAP error, if any
681  */
682 function get_module_fields($session, $module_name, $fields = array()){
683         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_module_fields for ' . $module_name);
684         global  $beanList, $beanFiles;
685         $error = new SoapError();
686         $module_fields = array();
687
688         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
689                 $GLOBALS['log']->error('End: SugarWebServiceImpl->get_module_fields FAILED on checkSessionAndModuleAccess for ' . $module_name);
690                 return;
691         } // if
692
693         $class_name = $beanList[$module_name];
694         require_once($beanFiles[$class_name]);
695         $seed = new $class_name();
696         if($seed->ACLAccess('ListView', true) || $seed->ACLAccess('DetailView', true) ||        $seed->ACLAccess('EditView', true) ) {
697         $return = self::$helperObject->get_return_module_fields($seed, $module_name, $fields);
698         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_module_fields SUCCESS for ' . $module_name);
699         return $return;
700     }
701     $error->set_error('no_access');
702         self::$helperObject->setFaultObject($error);
703     $GLOBALS['log']->error('End: SugarWebServiceImpl->get_module_fields FAILED NO ACCESS to ListView, DetailView or EditView for ' . $module_name);
704 }
705
706 /**
707  * Perform a seamless login. This is used internally during the sync process.
708  *
709  * @param String $session -- Session ID returned by a previous call to login.
710  * @return 1 -- integer - if the session was authenticated
711  * @return 0 -- integer - if the session could not be authenticated
712  */
713 function seamless_login($session){
714         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->seamless_login');
715         if(!self::$helperObject->validate_authenticated($session)){
716                 return 0;
717         }
718         $_SESSION['seamless_login'] = true;
719         $GLOBALS['log']->info('End: SugarWebServiceImpl->seamless_login');
720         return 1;
721 }
722
723 /**
724  * Add or replace the attachment on a Note.
725  * Optionally you can set the relationship of this note to Accounts/Contacts and so on by setting related_module_id, related_module_name
726  *
727  * @param String $session -- Session ID returned by a previous call to login.
728  * @param Array 'note' -- Array String 'id' -- The ID of the Note containing the attachment
729  *                              String 'filename' -- The file name of the attachment
730  *                              Binary 'file' -- The binary contents of the file.
731  *                                                              String 'related_module_id' -- module id to which this note to related to
732  *                                                              String 'related_module_name' - module name to which this note to related to
733  *
734  * @return Array 'id' -- String - The ID of the Note
735  * @exception 'SoapFault' -- The SOAP error, if any
736  */
737 function set_note_attachment($session, $note) {
738         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->set_note_attachment');
739         $error = new SoapError();
740         $module_name = '';
741         $module_access = '';
742         $module_id = '';
743         if (!empty($note['related_module_id']) && !empty($note['related_module_name'])) {
744                 $module_name = $note['related_module_name'];
745                 $module_id = $note['related_module_id'];
746                 $module_access = 'read';
747         }
748         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, $module_access, 'no_access', $error)) {
749                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_note_attachment');
750                 return;
751         } // if
752
753         require_once('modules/Notes/NoteSoap.php');
754         $ns = new NoteSoap();
755         $GLOBALS['log']->info('End: SugarWebServiceImpl->set_note_attachment');
756         return array('id'=>$ns->newSaveFile($note));
757 } // fn
758
759 /**
760  * Retrieve an attachment from a note
761  * @param String $session -- Session ID returned by a previous call to login.
762  * @param String $id -- The ID of the appropriate Note.
763  * @return Array 'note_attachment' -- Array String 'id' -- The ID of the Note containing the attachment
764  *                                          String 'filename' -- The file name of the attachment
765  *                                          Binary 'file' -- The binary contents of the file.
766  *                                                                                      String 'related_module_id' -- module id to which this note is related
767  *                                                                                      String 'related_module_name' - module name to which this note is related
768  * @exception 'SoapFault' -- The SOAP error, if any
769  */
770 function get_note_attachment($session,$id) {
771         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_note_attachment');
772         $error = new SoapError();
773         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
774                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_note_attachment');
775                 return;
776         } // if
777         require_once('modules/Notes/Note.php');
778         $note = new Note();
779
780         $note->retrieve($id);
781     if (!self::$helperObject->checkACLAccess($note, 'DetailView', $error, 'no_access')) {
782                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_note_attachment');
783         return;
784     } // if
785
786         require_once('modules/Notes/NoteSoap.php');
787         $ns = new NoteSoap();
788         if(!isset($note->filename)){
789                 $note->filename = '';
790         }
791         $file= $ns->retrieveFile($id,$note->filename);
792         if($file == -1){
793                 $file = '';
794         }
795
796         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_note_attachment');
797         return array('note_attachment'=>array('id'=>$id, 'filename'=>$note->filename, 'file'=>$file, 'related_module_id' => $note->parent_id, 'related_module_name' => $note->parent_type));
798
799 } // fn
800
801 /**
802  * sets a new revision for this document
803  *
804  * @param String $session -- Session ID returned by a previous call to login.
805  * @param Array $document_revision -- Array String 'id' --      The ID of the document object
806  *                                                                                      String 'document_name' - The name of the document
807  *                                                                                      String 'revision' - The revision value for this revision
808  *                                              String 'filename' -- The file name of the attachment
809  *                                          String 'file' -- The binary contents of the file.
810  * @return Array - 'id' - String - document revision id
811  * @exception 'SoapFault' -- The SOAP error, if any
812  */
813 function set_document_revision($session, $document_revision) {
814         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->set_document_revision');
815         $error = new SoapError();
816         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
817                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_document_revision');
818                 return;
819         } // if
820
821         require_once('modules/Documents/DocumentSoap.php');
822         $dr = new DocumentSoap();
823         $GLOBALS['log']->info('End: SugarWebServiceImpl->set_document_revision');
824         return array('id'=>$dr->saveFile($document_revision));
825 }
826
827 /**
828  * This method is used as a result of the .htaccess lock down on the cache directory. It will allow a
829  * properly authenticated user to download a document that they have proper rights to download.
830  *
831  * @param String $session -- Session ID returned by a previous call to login.
832  * @param String $id      -- ID of the document revision to obtain
833  * @return new_return_document_revision - Array String 'id' -- The ID of the document revision containing the attachment
834  *                                                                                              String document_name - The name of the document
835  *                                                                                              String revision - The revision value for this revision
836  *                                                      String 'filename' -- The file name of the attachment
837  *                                              Binary 'file' -- The binary contents of the file.
838  * @exception 'SoapFault' -- The SOAP error, if any
839  */
840 function get_document_revision($session, $id) {
841         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_document_revision');
842     global $sugar_config;
843
844     $error = new SoapError();
845         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
846                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_document_revision');
847                 return;
848         } // if
849
850     require_once('modules/DocumentRevisions/DocumentRevision.php');
851     $dr = new DocumentRevision();
852     $dr->retrieve($id);
853     if(!empty($dr->filename)){
854         $filename = "upload://{$dr->id}";
855         if (filesize($filename) > 0) {
856                 $contents = sugar_file_get_contents($filename);
857         } else {
858             $contents = '';
859         }
860         $contents = base64_encode($contents);
861         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_document_revision');
862         return array('document_revision'=>array('id' => $dr->id, 'document_name' => $dr->document_name, 'revision' => $dr->revision, 'filename' => $dr->filename, 'file' => $contents));
863     }else{
864         $error->set_error('no_records');
865         self::$helperObject->setFaultObject($error);
866                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_document_revision');
867     }
868
869 }
870
871 /**
872  * Given a list of modules to search and a search string, return the id, module_name, along with the fields
873  * We will support Accounts, Bug Tracker, Cases, Contacts, Leads, Opportunities, Project, ProjectTask, Quotes
874  *
875  * @param string $session                       - Session ID returned by a previous call to login.
876  * @param string $search_string         - string to search
877  * @param string[] $modules                     - array of modules to query
878  * @param int $offset                           - a specified offset in the query
879  * @param int $max_results                      - max number of records to return
880  * @return Array 'entry_list' -- Array('Accounts' => array(array('name' => 'first_name', 'value' => 'John', 'name' => 'last_name', 'value' => 'Do')))
881  * @exception 'SoapFault' -- The SOAP error, if any
882  */
883 function search_by_module($session, $search_string, $modules, $offset, $max_results){
884         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->search_by_module');
885         global  $beanList, $beanFiles;
886         global $sugar_config,$current_language;
887
888         $error = new SoapError();
889         $output_list = array();
890         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
891                 $error->set_error('invalid_login');
892                 $GLOBALS['log']->info('End: SugarWebServiceImpl->search_by_module');
893                 return;
894         }
895         global $current_user;
896         if($max_results > 0){
897                 $sugar_config['list_max_entries_per_page'] = $max_results;
898         }
899
900         require_once('modules/Home/UnifiedSearchAdvanced.php');
901         require_once 'include/utils.php';
902         $usa = new UnifiedSearchAdvanced();
903     if(!file_exists($cachedfile = sugar_cached('modules/unified_search_modules.php'))) {
904         $usa->buildCache();
905     }
906
907         include($cachedfile);
908         $modules_to_search = array();
909         $unified_search_modules['Users'] =   array('fields' => array());
910
911         $unified_search_modules['ProjectTask'] =   array('fields' => array());
912
913     foreach($unified_search_modules as $module=>$data) {
914         if (in_array($module, $modules)) {
915                 $modules_to_search[$module] = $beanList[$module];
916         } // if
917     } // foreach
918
919     $GLOBALS['log']->info('SugarWebServiceImpl->search_by_module - search string = ' . $search_string);
920
921         if(!empty($search_string) && isset($search_string)) {
922                 $search_string = trim($GLOBALS['db']->quote(securexss(from_html(clean_string($search_string, 'UNIFIED_SEARCH')))));
923         foreach($modules_to_search as $name => $beanName) {
924                 $where_clauses_array = array();
925                         $unifiedSearchFields = array () ;
926                         foreach ($unified_search_modules[$name]['fields'] as $field=>$def ) {
927                                 $unifiedSearchFields[$name] [ $field ] = $def ;
928                                 $unifiedSearchFields[$name] [ $field ]['value'] = $search_string;
929                         }
930
931                         require_once $beanFiles[$beanName] ;
932                         $seed = new $beanName();
933                         require_once 'include/SearchForm/SearchForm2.php' ;
934                         if ($beanName == "User"
935                             || $beanName == "ProjectTask"
936                             ) {
937                                 if(!self::$helperObject->check_modules_access($current_user, $seed->module_dir, 'read')){
938                                         continue;
939                                 } // if
940                                 if(!$seed->ACLAccess('ListView')) {
941                                         continue;
942                                 } // if
943                         }
944
945                         if ($beanName != "User"
946                             && $beanName != "ProjectTask"
947                             ) {
948                                 $searchForm = new SearchForm ($seed, $name ) ;
949
950                                 $searchForm->setup(array ($name => array()) ,$unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
951                                 $where_clauses = $searchForm->generateSearchWhere() ;
952                                 require_once 'include/SearchForm/SearchForm2.php' ;
953                                 $searchForm = new SearchForm ($seed, $name ) ;
954
955                                 $searchForm->setup(array ($name => array()) ,$unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
956                                 $where_clauses = $searchForm->generateSearchWhere() ;
957                                 $emailQuery = false;
958
959                                 $where = '';
960                                 if (count($where_clauses) > 0 ) {
961                                         $where = '('. implode(' ) OR ( ', $where_clauses) . ')';
962                                 }
963
964                                 $mod_strings = return_module_language($current_language, $seed->module_dir);
965                                 if(file_exists('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php')){
966                                         require_once('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
967                                 }else{
968                                         require_once('modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
969                                 }
970                     $filterFields = array();
971                                 foreach($listViewDefs[$seed->module_dir] as $colName => $param) {
972                         if(!empty($param['default']) && $param['default'] == true) {
973                             $filterFields[] = strtolower($colName);
974                         } // if
975                     } // foreach
976
977                     if (!in_array('id', $filterFields)) {
978                         $filterFields[] = 'id';
979                     } // if
980                                 $ret_array = $seed->create_new_list_query('', $where, $filterFields, array(), 0, '', true, $seed, true);
981                         if(empty($params) or !is_array($params)) $params = array();
982                         if(!isset($params['custom_select'])) $params['custom_select'] = '';
983                         if(!isset($params['custom_from'])) $params['custom_from'] = '';
984                         if(!isset($params['custom_where'])) $params['custom_where'] = '';
985                         if(!isset($params['custom_order_by'])) $params['custom_order_by'] = '';
986                                 $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'];
987                         } else {
988                                 if ($beanName == "User") {
989                     // $search_string gets cleaned above, so we can use it here
990                                         $filterFields = array('id', 'user_name', 'first_name', 'last_name', 'email_address');
991                                         $main_query = "select users.id, ea.email_address, users.user_name, first_name, last_name from users ";
992                                         $main_query = $main_query . " LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_module = '{$seed->module_dir}'
993 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) ";
994                                         $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'";
995                                 } // if
996                                 if ($beanName == "ProjectTask") {
997                     // $search_string gets cleaned above, so we can use it here
998                                         $filterFields = array('id', 'name', 'project_id', 'project_name');
999                                         $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} ";
1000                                         $seed->add_team_security_where_clause($main_query);
1001                                         $main_query .= "LEFT JOIN teams ON $seed->table_name.team_id=teams.id AND (teams.deleted=0) ";
1002                             $main_query .= "LEFT JOIN project ON $seed->table_name.project_id = project.id ";
1003                             $main_query .= "where {$seed->table_name}.name like '{$search_string}%'";
1004                                 } // if
1005                         } // else
1006
1007                         $GLOBALS['log']->info('SugarWebServiceImpl->search_by_module - query = ' . $main_query);
1008                         if($max_results < -1) {
1009                                 $result = $seed->db->query($main_query);
1010                         }
1011                         else {
1012                                 if($max_results == -1) {
1013                                         $limit = $sugar_config['list_max_entries_per_page'];
1014                     } else {
1015                         $limit = $max_results;
1016                     }
1017                     $result = $seed->db->limitQuery($main_query, $offset, $limit + 1);
1018                         }
1019
1020                         $rowArray = array();
1021                         while($row = $seed->db->fetchByAssoc($result)) {
1022                                 $nameValueArray = array();
1023                                 foreach ($filterFields as $field) {
1024                                         $nameValue = array();
1025                                         if (isset($row[$field])) {
1026                                                 $nameValueArray[$field] = self::$helperObject->get_name_value($field, $row[$field]);
1027                                         } // if
1028                                 } // foreach
1029                                 $rowArray[] = $nameValueArray;
1030                         } // while
1031                         $output_list[] = array('name' => $name, 'records' => $rowArray);
1032         } // foreach
1033
1034         $GLOBALS['log']->info('End: SugarWebServiceImpl->search_by_module');
1035         return array('entry_list'=>$output_list);
1036         } // if
1037         return array('entry_list'=>$output_list);
1038 } // fn
1039
1040
1041 /**
1042  * Retrieve the list of available modules on the system available to the currently logged in user.
1043  *
1044  * @param String $session -- Session ID returned by a previous call to login.
1045  * @return Array    'modules' -- Array - An array of module names
1046  * @exception 'SoapFault' -- The SOAP error, if any
1047  */
1048 function get_available_modules($session){
1049         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_available_modules');
1050
1051         $error = new SoapError();
1052         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
1053                 $error->set_error('invalid_login');
1054                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
1055                 return;
1056         } // if
1057
1058         $modules = array();
1059         $modules = array_keys($_SESSION['avail_modules']);
1060
1061         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
1062         return array('modules'=> $modules);
1063 } // fn
1064
1065
1066 /**
1067 *   Once we have successfuly done a mail merge on a campaign, we need to notify Sugar of the targets
1068 *   and the campaign_id for tracking purposes
1069 *
1070 * @param String session  -- Session ID returned by a previous call to login.
1071 * @param Array targets   -- a string array of ids identifying the targets used in the merge
1072 * @param String campaign_id  --  the campaign_id used for the merge
1073 * @return - No output
1074 *
1075 * @exception 'SoapFault' -- The SOAP error, if any
1076 */
1077 function set_campaign_merge($session,$targets, $campaign_id){
1078         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->set_campaign_merge');
1079
1080         $error = new SoapError();
1081         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
1082                 $error->set_error('invalid_login');
1083                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_campaign_merge');
1084                 return;
1085         } // if
1086     if (empty($campaign_id) or !is_array($targets) or count($targets) == 0) {
1087                 $error->set_error('invalid_set_campaign_merge_data');
1088                 self::$helperObject->setFaultObject($error);
1089         $GLOBALS['log']->debug('set_campaign_merge: Merge action status will not be updated, because, campaign_id is null or no targets were selected.');
1090                 $GLOBALS['log']->info('End: SugarWebServiceImpl->set_campaign_merge');
1091                 return;
1092     } else {
1093         require_once('modules/Campaigns/utils.php');
1094         campaign_log_mail_merge($campaign_id,$targets);
1095     } // else
1096 } // fn
1097 /**
1098 *   Retrieve number of records in a given module
1099 *
1100 * @param String session      -- Session ID returned by a previous call to login.
1101 * @param String module_name  -- module to retrieve number of records from
1102 * @param String query        -- allows webservice user to provide a WHERE clause
1103 * @param int deleted         -- specify whether or not to include deleted records
1104 *
1105 * @return Array  result_count - integer - Total number of records for a given module and query
1106 * @exception 'SoapFault' -- The SOAP error, if any
1107 */
1108 function get_entries_count($session, $module_name, $query, $deleted) {
1109         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_entries_count');
1110
1111         $error = new SoapError();
1112         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'list', 'no_access', $error)) {
1113                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entries_count');
1114                 return;
1115         } // if
1116
1117         global $beanList, $beanFiles, $current_user;
1118
1119         $class_name = $beanList[$module_name];
1120         require_once($beanFiles[$class_name]);
1121         $seed = new $class_name();
1122
1123     if (!self::$helperObject->checkQuery($error, $query)) {
1124                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entries_count');
1125         return;
1126     } // if
1127
1128     if (!self::$helperObject->checkACLAccess($seed, 'ListView', $error, 'no_access')) {
1129         return;
1130     }
1131
1132         $sql = 'SELECT COUNT(*) result_count FROM ' . $seed->table_name . ' ';
1133
1134
1135         // build WHERE clauses, if any
1136         $where_clauses = array();
1137         if (!empty($query)) {
1138             $where_clauses[] = $query;
1139         }
1140         if ($deleted == 0) {
1141                 $where_clauses[] = $seed->table_name . '.deleted = 0';
1142         }
1143
1144         // if WHERE clauses exist, add them to query
1145         if (!empty($where_clauses)) {
1146                 $sql .= ' WHERE ' . implode(' AND ', $where_clauses);
1147         }
1148
1149         $res = $GLOBALS['db']->query($sql);
1150         $row = $GLOBALS['db']->fetchByAssoc($res);
1151
1152         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_entries_count');
1153         return array(
1154                 'result_count' => $row['result_count'],
1155         );
1156 }
1157
1158
1159
1160 } // clazz
1161