]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - soap/SoapData.php
Release 6.2.0
[Github/sugarcrm.git] / soap / SoapData.php
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-2011 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 require_once('soap/SoapRelationshipHelper.php');
39 set_time_limit(360);
40  
41 $server->register(
42     'sync_get_modified_relationships',
43     array('session'=>'xsd:string', 'module_name'=>'xsd:string','related_module'=>'xsd:string', 'from_date'=>'xsd:string', 'to_date'=>'xsd:string','offset'=>'xsd:int', 'max_results'=>'xsd:int','deleted'=>'xsd:int', 'module_id'=>'xsd:string', 'select_fields'=>'tns:select_fields', 'ids'=>'tns:select_fields', 'relationship_name'=>'xsd:string', 'deletion_date'=>'xsd:string', 'php_serialize'=>'xsd:int'),
44     array('return'=>'tns:get_entry_list_result_encoded'),
45     $NAMESPACE);
46
47
48
49 /**
50  * Get a list of the relationship records that have been modified within a 
51  * specified date range.  This is used to perform a sync with a mobile client.
52  * The results are paged.
53  *
54  * @param xsd:string $session
55  * @param xsd:string $module_name
56  * @param xsd:string $related_module
57  * @param xsd:string $from_date
58  * @param xsd:string $to_date
59  * @param xsd:int $offset
60  * @param xsd:int $max_results
61  * @param xsd:int $deleted
62  * @param xsd:int $module_id
63  * @param tns:select_fields $select_fields
64  * @param tns:select_fields $ids
65  * @param xsd:string $relationship_name
66  * @param xsd:string $deletion_date
67  * @param xsd:int $php_serialize
68  * @return 
69  */
70 function sync_get_modified_relationships($session, $module_name, $related_module,$from_date,$to_date,$offset, $max_results, $deleted, $module_id = '', $select_fields = array(), $ids = array(), $relationship_name = '', $deletion_date = '', $php_serialize = 1){
71         global  $beanList, $beanFiles;
72         $error = new SoapError();
73         $output_list = array();
74         if(!validate_authenticated($session)){
75                 $error->set_error('invalid_login');     
76                 return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
77         }
78         if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
79                 $error->set_error('no_module'); 
80                 return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
81         }
82         global $current_user;
83         if(!check_modules_access($current_user, $module_name, 'read') || !check_modules_access($current_user, $related_module, 'read')){
84                 $error->set_error('no_access'); 
85                 return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
86         }
87         if($max_results > 0 || $max_results == '-99'){
88                 global $sugar_config;
89                 $sugar_config['list_max_entries_per_page'] = $max_results;      
90         }
91
92         $date_query = "(m1.date_modified > " . db_convert("'$from_date'", 'datetime'). " AND m1.date_modified <= ". db_convert("'$to_date'", 'datetime')." AND {0}.deleted = $deleted)";
93         if(isset($deletion_date) && !empty($deletion_date)){
94                 $date_query .= " OR ({0}.date_modified > " . db_convert("'$deletion_date'", 'datetime'). " AND {0}.date_modified <= ". db_convert("'$to_date'", 'datetime')." AND {0}.deleted = 1)";
95         }
96
97         $in = '';
98         if(isset($ids) && !empty($ids)){
99                 foreach($ids as $value){
100                         if(empty($in))
101                         {
102                                 $in .= "('" . $value . "'";     
103                         }
104                         else
105                         {
106                                 $in .= ",'" . $value . "'";     
107                         }
108                 }
109                 $in .=')';
110         }
111         $query = '';
112         if(isset($in) && !empty($in)){
113                 $query .= "( $date_query AND m1.id IN $in) OR (m1.id NOT IN $in AND {0}.deleted = 0)";
114         }
115         else{
116                 $query .= "( {0}.deleted = 0)";
117         }
118         if(isset($module_id) && !empty($module_id)){
119                 //if(isset($in) && !empty($in)){
120                         $query .= " AND";
121                 //}
122                 $query .= " m2.id = '$module_id'";
123         }
124         if($related_module == 'Meetings' || $related_module == 'Calls'){
125                 $query = string_format($query, array('m1'));    
126         }       
127         $results = retrieve_modified_relationships($module_name,  $related_module, $query, $deleted, $offset, $max_results, $select_fields, $relationship_name);
128
129         $list = $results['result'];
130         
131         $xml = '<?xml version="1.0" encoding="utf-8"?><items>';
132         foreach($list as $value)
133         {
134                 $val = array_get_return_value($value, $results['table_name']);
135                 if($php_serialize == 0){
136                         $xml .= get_name_value_xml($val, $module_name);
137                 }
138                 $output_list[] = $val;
139         }
140         $xml .= '</items>';
141         $next_offset = $offset + sizeof($output_list);
142         
143         if($php_serialize == 0){
144                 $myoutput = base64_encode($xml);        
145         }
146         else{
147                 $myoutput = get_encoded($output_list);
148         }
149         
150         return array('result_count'=>sizeof($output_list),'next_offset'=>0, 'total_count'=>sizeof($output_list), 'field_list'=>array(), 'entry_list'=>$myoutput , 'error'=>$error->get_soap_array());
151 }
152
153
154 $server->register(
155     'get_modified_entries',
156     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'ids'=>'tns:select_fields', 'select_fields'=>'tns:select_fields'),
157     array('return'=>'tns:get_sync_result_encoded'),
158     $NAMESPACE);
159
160 function get_modified_entries($session, $module_name, $ids, $select_fields ){
161         global  $beanList, $beanFiles;
162         $error = new SoapError();
163         $field_list = array();
164         $output_list = array();
165         if(!validate_authenticated($session)){
166                 $error->set_error('invalid_login');     
167                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
168         }
169         if(empty($beanList[$module_name])){
170                 $error->set_error('no_module'); 
171                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
172         }
173         global $current_user;
174         if(!check_modules_access($current_user, $module_name, 'read')){
175                 $error->set_error('no_access'); 
176                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
177         }
178         
179         $class_name = $beanList[$module_name];
180         require_once($beanFiles[$class_name]);
181         $seed = new $class_name();
182         //rsmith
183         $in = '';
184         $field_select ='';
185         
186         $table_name = $seed->table_name;
187         if(isset($ids)){
188                 foreach($ids as $value){
189                         if(empty($in))
190                         {
191                                 $in .= "('" . $value . "'";     
192                         }
193                         else
194                         {
195                                 $in .= ",'" . $value . "'";     
196                         }
197                 }//end foreach
198         }
199         $index = 0;
200         foreach($select_fields as $field){
201                         $field_select .= $table_name.".".$field;
202                 
203                         if($index < (count($select_fields) - 1))
204                         {
205                                 $field_select .= ",";
206                                 $index++;
207                         }
208                 }//end foreach
209         
210                 $ids = array();
211         
212         //end rsmith
213         if(!empty($in)){
214                         $in .=')';      
215         }
216
217         $ret_array = $seed->create_new_list_query('', "$table_name.id IN $in", $select_fields, array(), -2, '', true, $seed, true);
218     if(!is_array($params)) $params = array();
219     if(!isset($params['custom_select'])) $params['custom_select'] = '';
220     if(!isset($params['custom_from'])) $params['custom_from'] = '';
221     if(!isset($params['custom_where'])) $params['custom_where'] = '';
222     if(!isset($params['custom_order_by'])) $params['custom_order_by'] = '';
223         $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'];
224         $result = $seed->db->query($main_query);
225
226         $xml = '<?xml version="1.0" encoding="utf-8"?><items>';
227         while($row = $seed->db->fetchByAssoc($result))
228         {
229                 if (version_compare(phpversion(), '5.0') < 0) {
230                 $temp = $seed;
231         } else {
232                 $temp = @clone($seed);
233         }
234         $temp->setupCustomFields($temp->module_dir);
235                 $temp->loadFromRow($row);
236                 $temp->fill_in_additional_detail_fields();
237                 if(isset($temp->emailAddress)){
238                         $temp->emailAddress->handleLegacyRetrieve($temp);
239                 }
240                 $val = get_return_value($temp, $table_name);
241                 $xml .= get_name_value_xml($val, $module_name); 
242         }
243         $xml .= "</items>";
244
245         $xml = base64_encode($xml);
246         
247         return array('result'=>$xml, 'error'=>$error->get_soap_array());
248 }
249
250 $server->register(
251     'get_attendee_list',
252     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'id'=>'xsd:string'),
253     array('return'=>'tns:get_sync_result_encoded'),
254     $NAMESPACE);
255
256 function get_attendee_list($session, $module_name, $id){
257         global  $beanList, $beanFiles;
258         $error = new SoapError();
259         $field_list = array();
260         $output_list = array();
261         if(!validate_authenticated($session)){
262                 $error->set_error('invalid_login');     
263                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
264         }
265         if(empty($beanList[$module_name])){
266                 $error->set_error('no_module'); 
267                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
268         }
269         global $current_user;
270         if(!check_modules_access($current_user, $module_name, 'read')){
271                 $error->set_error('no_access'); 
272                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
273         }
274         
275
276         $class_name = $beanList[$module_name];
277         require_once($beanFiles[$class_name]);
278         $seed = new $class_name();
279         
280         
281                         //rsmith
282                         $xml = '<?xml version="1.0" encoding="utf-8"?>';
283                         if($module_name == 'Meetings' || $module_name == 'Calls'){
284                                 //if we find a meeting or call we want to send back the attendees
285                                 $l_module_name = strtolower($module_name);
286                                 $table_name = $l_module_name."_users";
287                                 if($module_name == 'Meetings')
288                                         $join_field = "meeting";
289                                 else
290                                         $join_field = "call";
291                                 $xml .= '<attendees>';
292                                 $result = $seed->db->query("SELECT users.id, $table_name.date_modified, first_name, last_name FROM users INNER JOIN $table_name ON $table_name.user_id = users.id WHERE ".$table_name.".".$join_field."_id = '".$id."' AND $table_name.deleted = 0"); 
293                                 $user = new User();
294                                 while($row = $seed->db->fetchByAssoc($result))
295                                 {
296                                         $user->id = $row['id'];
297                                         $email = $user->emailAddress->getPrimaryAddress($user);
298                                         $xml .= '<attendee>';
299                                         $xml .= '<id>'.$user->id.'</id>';
300                                         $xml .= '<first_name>'.$row['first_name'].'</first_name>';
301                                         $xml .= '<last_name>'.$row['last_name'].'</last_name>';
302                                         $xml .= '<email1>'.$email.'</email1>';
303                                         $xml .= '</attendee>';
304                                 }       
305                                 //now get contacts
306                                 $table_name = $l_module_name."_contacts";
307                                 $result = $seed->db->query("SELECT contacts.id, $table_name.date_modified, first_name, last_name FROM contacts INNER JOIN $table_name ON $table_name.contact_id = contacts.id INNER JOIN $seed->table_name ON ".$seed->table_name.".id = ".$table_name.".".$join_field."_id WHERE ".$table_name.".".$join_field."_id = '".$id."' AND ".$table_name.".deleted = 0 AND (contacts.id != ".$seed->table_name.".parent_id OR ".$seed->table_name.".parent_id IS NULL)"); 
308                                 $contact = new Contact();
309                                 while($row = $seed->db->fetchByAssoc($result))
310                                 {
311                                         $contact->id = $row['id'];
312                                         $email = $contact->emailAddress->getPrimaryAddress($contact);
313                                         $xml .= '<attendee>';
314                                         $xml .= '<id>'.$contact->id.'</id>';
315                                         $xml .= '<first_name>'.$row['first_name'].'</first_name>';
316                                         $xml .= '<last_name>'.$row['last_name'].'</last_name>';
317                                         $xml .= '<email1>'.$email.'</email1>';
318                                         $xml .= '</attendee>';
319                                 }                                       
320                                 $xml .= '</attendees>';
321                         }
322         $xml = base64_encode($xml);
323         return array('result'=>$xml, 'error'=>$error->get_soap_array());
324 }
325 ?>