]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/v4_1/SugarWebServiceImplv4_1.php
Release 6.5.0
[Github/sugarcrm.git] / service / v4_1 / SugarWebServiceImplv4_1.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  * SugarWebServiceImplv4_1.php
41  *
42  * This class is an implementation class for all the web services.  Version 4_1 adds limit/off support to the
43  * get_relationships function.  We also added the sync_get_modified_relationships function call from version
44  * one to facilitate querying for related meetings/calls contacts/users records.
45  *
46  */
47 require_once('service/v4/SugarWebServiceImplv4.php');
48 require_once('service/v4_1/SugarWebServiceUtilv4_1.php');
49
50 class SugarWebServiceImplv4_1 extends SugarWebServiceImplv4
51 {
52
53     /**
54      * Class Constructor Object
55      *
56      */
57     public function __construct()
58     {
59         self::$helperObject = new SugarWebServiceUtilv4_1();
60     }
61
62     /**
63      * Retrieve a collection of beans that are related to the specified bean and optionally return relationship data for those related beans.
64      * 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.
65      *
66      * @param String $session -- Session ID returned by a previous call to login.
67      * @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)..
68      * @param String $module_id -- The ID of the bean in the specified module
69      * @param String $link_field_name -- The name of the lnk field to return records from.  This name should be the name the relationship.
70      * @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.
71      * @param Array $related_fields - Array of related bean fields to be returned.
72      * @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'))).
73      * @param Number $deleted -- false if deleted records should not be include, true if deleted records should be included.
74      * @param String $order_by -- field to order the result sets by
75      * @param Number $offset -- where to start in the return
76      * @param Number $limit -- number of results to return (defaults to all)
77      * @return Array 'entry_list' -- Array - The records that were retrieved
78      *               '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 ) ) ) ) )
79      * @exception 'SoapFault' -- The SOAP error, if any
80      */
81     function get_relationships($session, $module_name, $module_id, $link_field_name, $related_module_query, $related_fields, $related_module_link_name_to_fields_array, $deleted, $order_by = '', $offset = 0, $limit = false)
82     {
83         $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_relationships');
84         self::$helperObject = new SugarWebServiceUtilv4_1();
85         global  $beanList, $beanFiles;
86         $error = new SoapError();
87
88         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
89                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
90                 return;
91         } // if
92
93         $mod = BeanFactory::getBean($module_name, $module_id);
94
95         if (!self::$helperObject->checkQuery($error, $related_module_query, $order_by)) {
96                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
97                 return;
98         } // if
99
100         if (!self::$helperObject->checkACLAccess($mod, 'DetailView', $error, 'no_access')) {
101                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
102                 return;
103         } // if
104
105         $output_list = array();
106         $linkoutput_list = array();
107
108         // get all the related modules data.
109         $result = self::$helperObject->getRelationshipResults($mod, $link_field_name, $related_fields, $related_module_query, $order_by, $offset, $limit);
110
111         if (self::$helperObject->isLogLevelDebug()) {
112                 $GLOBALS['log']->debug('SoapHelperWebServices->get_relationships - return data for getRelationshipResults is ' . var_export($result, true));
113         } // if
114         if ($result) {
115
116                 $list = $result['rows'];
117                 $filterFields = $result['fields_set_on_rows'];
118
119                 if (sizeof($list) > 0) {
120                         // get the related module name and instantiate a bean for that
121                         $submodulename = $mod->$link_field_name->getRelatedModuleName();
122                 $submoduletemp = BeanFactory::getBean($submodulename);
123
124                         foreach($list as $row) {
125                                 $submoduleobject = @clone($submoduletemp);
126                                 // set all the database data to this object
127                                 foreach ($filterFields as $field) {
128                                         $submoduleobject->$field = $row[$field];
129                                 } // foreach
130                                 if (isset($row['id'])) {
131                                         $submoduleobject->id = $row['id'];
132                                 }
133                                 $output_list[] = self::$helperObject->get_return_value_for_fields($submoduleobject, $submodulename, $filterFields);
134                                 if (!empty($related_module_link_name_to_fields_array)) {
135                                         $linkoutput_list[] = self::$helperObject->get_return_value_for_link_fields($submoduleobject, $submodulename, $related_module_link_name_to_fields_array);
136                                 } // if
137
138                         } // foreach
139                 }
140
141         } // if
142
143         $GLOBALS['log']->info('End: SugarWebServiceImpl->get_relationships');
144         return array('entry_list'=>$output_list, 'relationship_list' => $linkoutput_list);
145     }
146
147
148     /**
149      * get_modified_relationships
150      *
151      * Get a list of the relationship records that have a date_modified value set within a specified date range.  This is used to
152      * help facilitate sync operations.  The module_name should be "Users" and the related_module one of "Meetings", "Calls" and
153      * "Contacts".
154      *
155      * @param xsd:string $session String of the session id
156      * @param xsd:string $module_name String value of the primary module to retrieve relationship against
157      * @param xsd:string $related_module String value of the related module to retrieve records off of
158      * @param xsd:string $from_date String value in YYYY-MM-DD HH:MM:SS format of date_start range (required)
159      * @param xsd:string $to_date String value in YYYY-MM-DD HH:MM:SS format of ending date_start range (required)
160      * @param xsd:int $offset Integer value of the offset to begin returning records from
161      * @param xsd:int $max_results Integer value of the max_results to return; -99 for unlimited
162      * @param xsd:int $deleted Integer value indicating deleted column value search (defaults to 0).  Set to 1 to find deleted records
163      * @param xsd:string $module_user_id String value of the user id (optional, but defaults to SOAP session user id anyway)  The module_user_id value
164      * here ought to be the user id of the user initiating the SOAP session
165      * @param tns:select_fields $select_fields Array value of fields to select and return as name/value pairs
166      * @param xsd:string $relationship_name String value of the relationship name to search on
167      * @param xsd:string $deletion_date String value in YYYY-MM-DD HH:MM:SS format for filtering on deleted records whose date_modified falls within range
168      * this allows deleted records to be returned as well
169      *
170      * @return Array records that match search criteria
171      */
172     function get_modified_relationships($session, $module_name, $related_module, $from_date, $to_date, $offset, $max_results, $deleted=0, $module_user_id = '', $select_fields = array(), $relationship_name = '', $deletion_date = ''){
173         global  $beanList, $beanFiles;
174         $error = new SoapError();
175         $output_list = array();
176
177         if(empty($from_date))
178         {
179             $error->set_error('invalid_call_error, missing from_date');
180             return array('result_count'=>0, 'next_offset'=>0, 'field_list'=>$select_fields, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
181         }
182
183         if(empty($to_date))
184         {
185             $error->set_error('invalid_call_error, missing to_date');
186             return array('result_count'=>0, 'next_offset'=>0, 'field_list'=>$select_fields, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
187         }
188
189         self::$helperObject = new SugarWebServiceUtilv4_1();
190         if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error))
191         {
192                 $GLOBALS['log']->info('End: SugarWebServiceImpl->get_modified_relationships');
193                 return;
194         } // if
195
196         if(empty($beanList[$module_name]) || empty($beanList[$related_module]))
197         {
198             $error->set_error('no_module');
199             return array('result_count'=>0, 'next_offset'=>0, 'field_list'=>$select_fields, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
200         }
201
202         global $current_user;
203         if(!self::$helperObject->check_modules_access($current_user, $module_name, 'read') || !self::$helperObject->check_modules_access($current_user, $related_module, 'read')){
204             $error->set_error('no_access');
205             return array('result_count'=>0, 'next_offset'=>0, 'field_list'=>$select_fields, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
206         }
207
208         if($max_results > 0 || $max_results == '-99'){
209             global $sugar_config;
210             $sugar_config['list_max_entries_per_page'] = $max_results;
211         }
212
213         // Cast to integer
214         $deleted = (int)$deleted;
215         $query = "(m1.date_modified > " . db_convert("'".$GLOBALS['db']->quote($from_date)."'", 'datetime'). " AND m1.date_modified <= ". db_convert("'".$GLOBALS['db']->quote($to_date)."'", 'datetime')." AND {0}.deleted = $deleted)";
216         if(isset($deletion_date) && !empty($deletion_date)){
217             $query .= " OR ({0}.date_modified > " . db_convert("'".$GLOBALS['db']->quote($deletion_date)."'", 'datetime'). " AND {0}.date_modified <= ". db_convert("'".$GLOBALS['db']->quote($to_date)."'", 'datetime')." AND {0}.deleted = 1)";
218         }
219
220         if(!empty($module_user_id))
221         {
222             $query .= " AND m2.id = '".$GLOBALS['db']->quote($module_user_id)."'";
223         }
224
225         //if($related_module == 'Meetings' || $related_module == 'Calls' || $related_module = 'Contacts'){
226         $query = string_format($query, array('m1'));
227         //}
228
229         require_once('soap/SoapRelationshipHelper.php');
230         $results = retrieve_modified_relationships($module_name, $related_module, $query, $deleted, $offset, $max_results, $select_fields, $relationship_name);
231
232         $list = $results['result'];
233
234         foreach($list as $value)
235         {
236              $output_list[] = self::$helperObject->array_get_return_value($value, $results['table_name']);
237         }
238
239         $next_offset = $offset + count($output_list);
240
241         return array(
242             'result_count'=> count($output_list),
243             'next_offset' => $next_offset,
244             'entry_list' => $output_list,
245             'error' => $error->get_soap_array()
246         );
247     }
248
249 }