2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once('soap/SoapRelationshipHelper.php');
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'),
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.
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
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());
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());
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());
88 $deleted = (int)$deleted;
89 if($max_results > 0 || $max_results == '-99'){
91 $sugar_config['list_max_entries_per_page'] = $max_results;
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)";
100 if(isset($ids) && !empty($ids)){
101 foreach($ids as $value){
104 $in .= "('" . $GLOBALS['db']->quote($value) . "'";
108 $in .= ",'" . $GLOBALS['db']->quote($value) . "'";
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)";
118 $query .= "( {0}.deleted = 0)";
120 if(isset($module_id) && !empty($module_id)){
121 //if(isset($in) && !empty($in)){
124 $query .= " m2.id = '".$GLOBALS['db']->quote($module_id)."'";
126 if($related_module == 'Meetings' || $related_module == 'Calls'){
127 $query = string_format($query, array('m1'));
129 $results = retrieve_modified_relationships($module_name, $related_module, $query, $deleted, $offset, $max_results, $select_fields, $relationship_name);
131 $list = $results['result'];
133 $xml = '<?xml version="1.0" encoding="utf-8"?><items>';
134 foreach($list as $value)
136 $val = array_get_return_value($value, $results['table_name']);
137 if($php_serialize == 0){
138 $xml .= get_name_value_xml($val, $module_name);
140 $output_list[] = $val;
143 $next_offset = $offset + sizeof($output_list);
145 if($php_serialize == 0){
146 $myoutput = base64_encode($xml);
149 $myoutput = get_encoded($output_list);
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());
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'),
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());
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());
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());
181 $class_name = $beanList[$module_name];
182 require_once($beanFiles[$class_name]);
183 $seed = new $class_name();
188 $table_name = $seed->table_name;
190 foreach($ids as $value){
193 $in .= "('" . $GLOBALS['db']->quote($value) . "'";
197 $in .= ",'" . $GLOBALS['db']->quote($value) . "'";
202 foreach($select_fields as $field){
203 if ( !isset($seed->field_defs[$field]) ) {
206 $field_select .= $table_name.".".$field;
208 if($index < (count($select_fields) - 1))
210 $field_select .= ",";
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);
231 $xml = '<?xml version="1.0" encoding="utf-8"?><items>';
232 while($row = $seed->db->fetchByAssoc($result))
234 if (version_compare(phpversion(), '5.0') < 0) {
237 $temp = @clone($seed);
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);
245 $val = get_return_value($temp, $table_name);
246 $xml .= get_name_value_xml($val, $module_name);
250 $xml = base64_encode($xml);
252 return array('result'=>$xml, 'error'=>$error->get_soap_array());
257 array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'id'=>'xsd:string'),
258 array('return'=>'tns:get_sync_result_encoded'),
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());
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());
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());
281 $class_name = $beanList[$module_name];
282 require_once($beanFiles[$class_name]);
283 $seed = new $class_name();
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";
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");
299 while($row = $seed->db->fetchByAssoc($result))
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>';
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))
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>';
325 $xml .= '</attendees>';
327 $xml = base64_encode($xml);
328 return array('result'=>$xml, 'error'=>$error->get_soap_array());