]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - soap/SoapData.php
Release 6.4.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     // Cast to integer
88     $deleted = (int)$deleted;
89         if($max_results > 0 || $max_results == '-99'){
90                 global $sugar_config;
91                 $sugar_config['list_max_entries_per_page'] = $max_results;      
92         }
93
94         $date_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)";
95         if(isset($deletion_date) && !empty($deletion_date)){
96                 $date_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)";
97         }
98
99         $in = '';
100         if(isset($ids) && !empty($ids)){
101                 foreach($ids as $value){
102                         if(empty($in))
103                         {
104                                 $in .= "('" . $GLOBALS['db']->quote($value) . "'";      
105                         }
106                         else
107                         {
108                                 $in .= ",'" . $GLOBALS['db']->quote($value) . "'";      
109                         }
110                 }
111                 $in .=')';
112         }
113         $query = '';
114         if(isset($in) && !empty($in)){
115                 $query .= "( $date_query AND m1.id IN $in) OR (m1.id NOT IN $in AND {0}.deleted = 0)";
116         }
117         else{
118                 $query .= "( {0}.deleted = 0)";
119         }
120         if(isset($module_id) && !empty($module_id)){
121                 //if(isset($in) && !empty($in)){
122                         $query .= " AND";
123                 //}
124         $query .= " m2.id = '".$GLOBALS['db']->quote($module_id)."'";
125         }
126         if($related_module == 'Meetings' || $related_module == 'Calls'){
127                 $query = string_format($query, array('m1'));    
128         }       
129         $results = retrieve_modified_relationships($module_name,  $related_module, $query, $deleted, $offset, $max_results, $select_fields, $relationship_name);
130
131         $list = $results['result'];
132         
133         $xml = '<?xml version="1.0" encoding="utf-8"?><items>';
134         foreach($list as $value)
135         {
136                 $val = array_get_return_value($value, $results['table_name']);
137                 if($php_serialize == 0){
138                         $xml .= get_name_value_xml($val, $module_name);
139                 }
140                 $output_list[] = $val;
141         }
142         $xml .= '</items>';
143         $next_offset = $offset + sizeof($output_list);
144         
145         if($php_serialize == 0){
146                 $myoutput = base64_encode($xml);        
147         }
148         else{
149                 $myoutput = get_encoded($output_list);
150         }
151         
152         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());
153 }
154
155
156 $server->register(
157     'get_modified_entries',
158     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'ids'=>'tns:select_fields', 'select_fields'=>'tns:select_fields'),
159     array('return'=>'tns:get_sync_result_encoded'),
160     $NAMESPACE);
161
162 function get_modified_entries($session, $module_name, $ids, $select_fields ){
163         global  $beanList, $beanFiles;
164         $error = new SoapError();
165         $field_list = array();
166         $output_list = array();
167         if(!validate_authenticated($session)){
168                 $error->set_error('invalid_login');     
169                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
170         }
171         if(empty($beanList[$module_name])){
172                 $error->set_error('no_module'); 
173                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
174         }
175         global $current_user;
176         if(!check_modules_access($current_user, $module_name, 'read')){
177                 $error->set_error('no_access'); 
178                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
179         }
180         
181         $class_name = $beanList[$module_name];
182         require_once($beanFiles[$class_name]);
183         $seed = new $class_name();
184         //rsmith
185         $in = '';
186         $field_select ='';
187         
188         $table_name = $seed->table_name;
189         if(isset($ids)){
190                 foreach($ids as $value){
191                         if(empty($in))
192                         {
193                                 $in .= "('" . $GLOBALS['db']->quote($value) . "'";      
194                         }
195                         else
196                         {
197                                 $in .= ",'" . $GLOBALS['db']->quote($value) . "'";      
198                         }
199                 }//end foreach
200         }
201         $index = 0;
202         foreach($select_fields as $field){
203             if ( !isset($seed->field_defs[$field]) ) {
204                 continue;
205             }
206                         $field_select .= $table_name.".".$field;
207                 
208                         if($index < (count($select_fields) - 1))
209                         {
210                                 $field_select .= ",";
211                                 $index++;
212                         }
213                 }//end foreach
214         
215                 $ids = array();
216         
217         //end rsmith
218         if(!empty($in)){
219                         $in .=')';      
220         }
221
222         $ret_array = $seed->create_new_list_query('', "$table_name.id IN $in", $select_fields, array(), -2, '', true, $seed, true);
223     if(!is_array($params)) $params = array();
224     if(!isset($params['custom_select'])) $params['custom_select'] = '';
225     if(!isset($params['custom_from'])) $params['custom_from'] = '';
226     if(!isset($params['custom_where'])) $params['custom_where'] = '';
227     if(!isset($params['custom_order_by'])) $params['custom_order_by'] = '';
228         $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'];
229         $result = $seed->db->query($main_query);
230
231         $xml = '<?xml version="1.0" encoding="utf-8"?><items>';
232         while($row = $seed->db->fetchByAssoc($result))
233         {
234                 if (version_compare(phpversion(), '5.0') < 0) {
235                 $temp = $seed;
236         } else {
237                 $temp = @clone($seed);
238         }
239         $temp->setupCustomFields($temp->module_dir);
240                 $temp->loadFromRow($row);
241                 $temp->fill_in_additional_detail_fields();
242                 if(isset($temp->emailAddress)){
243                         $temp->emailAddress->handleLegacyRetrieve($temp);
244                 }
245                 $val = get_return_value($temp, $table_name);
246                 $xml .= get_name_value_xml($val, $module_name); 
247         }
248         $xml .= "</items>";
249
250         $xml = base64_encode($xml);
251         
252         return array('result'=>$xml, 'error'=>$error->get_soap_array());
253 }
254
255 $server->register(
256     'get_attendee_list',
257     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'id'=>'xsd:string'),
258     array('return'=>'tns:get_sync_result_encoded'),
259     $NAMESPACE);
260
261 function get_attendee_list($session, $module_name, $id){
262         global  $beanList, $beanFiles;
263         $error = new SoapError();
264         $field_list = array();
265         $output_list = array();
266         if(!validate_authenticated($session)){
267                 $error->set_error('invalid_login');     
268                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
269         }
270         if(empty($beanList[$module_name])){
271                 $error->set_error('no_module'); 
272                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
273         }
274         global $current_user;
275         if(!check_modules_access($current_user, $module_name, 'read')){
276                 $error->set_error('no_access'); 
277                 return array('field_list'=>$field_list, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
278         }
279         
280
281         $class_name = $beanList[$module_name];
282         require_once($beanFiles[$class_name]);
283         $seed = new $class_name();
284         
285         
286                         //rsmith
287                         $xml = '<?xml version="1.0" encoding="utf-8"?>';
288                         if($module_name == 'Meetings' || $module_name == 'Calls'){
289                                 //if we find a meeting or call we want to send back the attendees
290                                 $l_module_name = strtolower($module_name);
291                                 $table_name = $l_module_name."_users";
292                                 if($module_name == 'Meetings')
293                                         $join_field = "meeting";
294                                 else
295                                         $join_field = "call";
296                                 $xml .= '<attendees>';
297                                 $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 = '".$GLOBALS['db']->quote($id)."' AND $table_name.deleted = 0"); 
298                                 $user = new User();
299                                 while($row = $seed->db->fetchByAssoc($result))
300                                 {
301                                         $user->id = $row['id'];
302                                         $email = $user->emailAddress->getPrimaryAddress($user);
303                                         $xml .= '<attendee>';
304                                         $xml .= '<id>'.$user->id.'</id>';
305                                         $xml .= '<first_name>'.$row['first_name'].'</first_name>';
306                                         $xml .= '<last_name>'.$row['last_name'].'</last_name>';
307                                         $xml .= '<email1>'.$email.'</email1>';
308                                         $xml .= '</attendee>';
309                                 }       
310                                 //now get contacts
311                                 $table_name = $l_module_name."_contacts";
312                                 $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 = '".$GLOBALS['db']->quote($id)."' AND ".$table_name.".deleted = 0 AND (contacts.id != ".$seed->table_name.".parent_id OR ".$seed->table_name.".parent_id IS NULL)"); 
313                                 $contact = new Contact();
314                                 while($row = $seed->db->fetchByAssoc($result))
315                                 {
316                                         $contact->id = $row['id'];
317                                         $email = $contact->emailAddress->getPrimaryAddress($contact);
318                                         $xml .= '<attendee>';
319                                         $xml .= '<id>'.$contact->id.'</id>';
320                                         $xml .= '<first_name>'.$row['first_name'].'</first_name>';
321                                         $xml .= '<last_name>'.$row['last_name'].'</last_name>';
322                                         $xml .= '<email1>'.$email.'</email1>';
323                                         $xml .= '</attendee>';
324                                 }                                       
325                                 $xml .= '</attendees>';
326                         }
327         $xml = base64_encode($xml);
328         return array('result'=>$xml, 'error'=>$error->get_soap_array());
329 }
330 ?>