]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - soap/SoapPortalUsers.php
Release 6.4.0
[Github/sugarcrm.git] / soap / SoapPortalUsers.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
39 require_once('soap/SoapHelperFunctions.php');
40 require_once('soap/SoapTypes.php');
41
42
43 require_once('soap/SoapPortalHelper.php');
44
45
46
47
48 /*************************************************************************************
49
50 THIS IS FOR PORTAL USERS
51
52
53 *************************************************************************************/
54 /*
55 this authenticates a user as a portal user and returns the session id or it returns false otherwise;
56 */
57 $server->register(
58         'portal_login',
59         array('portal_auth'=>'tns:user_auth','user_name'=>'xsd:string', 'application_name'=>'xsd:string'),
60         array('return'=>'tns:set_entry_result'),
61         $NAMESPACE);
62
63 function portal_login($portal_auth, $user_name, $application_name){
64     $error = new SoapError();
65     $contact = new Contact();
66     $result = login_user($portal_auth);
67
68     if($result == 'fail' || $result == 'sessions_exceeded'){
69         if($result == 'sessions_exceeded') {
70             $error->set_error('sessions_exceeded');
71         }
72         else {
73             $error->set_error('no_portal');
74         }
75         return array('id'=>-1, 'error'=>$error->get_soap_array());
76     }
77     global $current_user;
78
79     if($user_name == 'lead'){
80         session_start();
81         $_SESSION['is_valid_session']= true;
82         $_SESSION['ip_address'] = query_client_ip();
83         $_SESSION['portal_id'] = $current_user->id;
84         $_SESSION['type'] = 'lead';
85         login_success();
86         return array('id'=>session_id(), 'error'=>$error->get_soap_array());
87     }else if($user_name == 'portal'){
88         session_start();
89         $_SESSION['is_valid_session']= true;
90         $_SESSION['ip_address'] = query_client_ip();
91         $_SESSION['portal_id'] = $current_user->id;
92         $_SESSION['type'] = 'portal';
93         $GLOBALS['log']->debug("Saving new session");
94         login_success();
95         return array('id'=>session_id(), 'error'=>$error->get_soap_array());
96     }else{
97     $contact = $contact->retrieve_by_string_fields(array('portal_name'=>$user_name, 'portal_active'=>'1', 'deleted'=>0) );
98     if($contact != null){
99         session_start();
100         $_SESSION['is_valid_session']= true;
101         $_SESSION['ip_address'] = query_client_ip();
102         $_SESSION['user_id'] = $contact->id;
103         $_SESSION['portal_id'] = $current_user->id;
104
105         $_SESSION['type'] = 'contact';
106         $_SESSION['assigned_user_id'] = $contact->assigned_user_id;
107         login_success();
108         build_relationship_tree($contact);
109         return array('id'=>session_id(), 'error'=>$error->get_soap_array());
110     }
111     }
112     $error->set_error('invalid_login');
113     return array('id'=>-1, 'error'=>$error->get_soap_array());
114 }
115
116 /*
117 this validates the session and starts the session;
118 */
119 function portal_validate_authenticated($session_id){
120     $old_error_reporting = error_reporting(0);
121     session_id($session_id);
122
123     // This little construct checks to see if the session validated
124     if(session_start()) {
125         $valid_session = true;
126
127                 if(!empty($_SESSION['is_valid_session']) && $_SESSION['ip_address'] == query_client_ip() && $valid_session != null && ($_SESSION['type'] == 'contact' || $_SESSION['type'] == 'lead' || $_SESSION['type'] == 'portal')){
128                         global $current_user;
129             $current_user = new User();
130             $current_user->retrieve($_SESSION['portal_id']);
131             login_success();
132             error_reporting($old_error_reporting);
133             return true;
134         }
135     }
136     session_destroy();
137     $GLOBALS['log']->fatal('SECURITY: The session ID is invalid');
138     error_reporting($old_error_reporting);
139     return false;
140 }
141
142
143 $server->register(
144         'portal_logout',
145         array('session'=>'xsd:string'),
146         array('return'=>'tns:error_value'),
147         $NAMESPACE);
148 function portal_logout($session){
149     $error = new SoapError();
150     if(portal_validate_authenticated($session)){
151         session_destroy();
152         return $error->get_soap_array();
153     }
154     $error->set_error('invalid_session');
155     return $error->get_soap_array();
156 }
157
158 $server->register(
159         'portal_get_sugar_id',
160         array('session'=>'xsd:string'),
161         array('return'=>'tns:set_entry_result'),
162         $NAMESPACE);
163 function portal_get_sugar_id($session){
164     $error = new SoapError();
165     if(portal_validate_authenticated($session)){
166         return array('id'=>$_SESSION['portal_id'], 'error'=>$error->get_soap_array());
167     }
168     $error->set_error('invalid_session');
169     return array('id'=>-1, 'error'=>$error->get_soap_array());
170
171 }
172
173 $server->register(
174         'portal_get_sugar_contact_id',
175         array('session'=>'xsd:string'),
176         array('return'=>'tns:set_entry_result'),
177         $NAMESPACE);
178 function portal_get_sugar_contact_id($session){
179     $error = new SoapError();
180     if(portal_validate_authenticated($session)){
181         return array('id'=>$_SESSION['user_id'], 'error'=>$error->get_soap_array());
182     }
183     $error->set_error('invalid_session');
184     return array('id'=>-1, 'error'=>$error->get_soap_array());
185
186 }
187
188
189 $server->register(
190     'portal_get_entry_list',
191     array('session'=>'xsd:string', 'module_name'=>'xsd:string','where'=>'xsd:string', 'order_by'=>'xsd:string', 'select_fields'=>'tns:select_fields'),
192     array('return'=>'tns:get_entry_list_result'),
193     $NAMESPACE);
194
195 function portal_get_entry_list($session, $module_name,$where, $order_by, $select_fields){
196     return portal_get_entry_list_limited($session, $module_name, $where, $order_by, $select_fields, 0, "");
197 }
198
199 /*
200  * Acts like a normal get_entry_list except it will build the where clause based on the name_value pairs passed
201  * Here we assume 'AND'
202  */
203 $server->register(
204     'portal_get_entry_list_filter',
205     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'order_by'=>'xsd:string', 'select_fields'=>'tns:select_fields', 'row_offset' => 'xsd:int', 'limit'=>'xsd:int', 'filter' =>'tns:name_value_operator_list'),
206     array('return'=>'tns:get_entry_list_result'),
207     $NAMESPACE);
208
209
210 function portal_get_entry_list_filter($session, $module_name, $order_by, $select_fields, $row_offset, $limit, $filter){
211     global  $beanList, $beanFiles, $portal_modules;
212     $error = new SoapError();
213     if(! portal_validate_authenticated($session)){
214         $error->set_error('invalid_session');
215         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
216     }
217     if($_SESSION['type'] == 'lead'){
218         $error->set_error('no_access');
219         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
220     }
221     if(empty($beanList[$module_name])){
222         $error->set_error('no_module');
223         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
224     }
225
226     //build the where clause
227
228     $sugar = null;
229     if($module_name == 'Cases'){
230         $sugar = new aCase();
231     }else if($module_name == 'Contacts'){
232         $sugar = new Contact();
233     }else if($module_name == 'Accounts'){
234         $sugar = new Account(); 
235     } else if($module_name == 'Bugs'){
236         $sugar = new Bug();
237     } else if($module_name == 'KBDocuments' || $module_name == 'FAQ') {
238         $sugar = new KBDocument();
239     } else {
240         $error->set_error('no_module_support');
241         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
242     }
243
244     if($sugar != null){
245         if(isset($filter) && is_array($filter)){
246             $where = "";
247             foreach($filter as $nvOp){
248                 $name = $nvOp['name'];
249                 $value = $nvOp['value'];
250                 $value_array = $nvOp['value_array'];
251                 $operator = $nvOp['operator'];
252                 //do nothing if all three values are not set
253                 if(isset($name) && (isset($value) || isset($value_array)) && isset($operator)){
254                     if(!empty($where)){
255                         $where .= ' AND ';
256                     }
257                     if(isset($sugar->field_defs[$name])){
258                         // MFH - Added Support For Custom Fields in Searches
259                         $cstm = isset($sugar->field_defs[$name]['source']) && $sugar->field_defs[$name]['source'] == 'custom_fields' ? '_cstm' : '';
260
261                         $where .=  "$sugar->table_name$cstm.$name $operator ";
262                         if($sugar->field_defs['name']['type'] == 'datetime'){
263                             $where .= db_convert("'".$GLOBALS['db']->quote($value)."'", 'datetime');
264                         }else{
265                             if(empty($value)) {
266                                 $tmp = array();
267                                 foreach($value_array as $v) {
268                                     $tmp[] = $GLOBALS['db']->quote($v);
269                                 }
270                                 $where .= "('" . implode("', '", $tmp) . "')";
271                             } else {
272                                 $where .= "'".$GLOBALS['db']->quote($value)."'";
273                             }
274                         }
275                     }
276                 }
277             }
278         }
279         return portal_get_entry_list_limited($session, $module_name, $where, $order_by, $select_fields, $row_offset, $limit);
280     }else{
281         $error->set_error('no_module_support');
282         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
283     }
284 }
285
286
287 $server->register(
288     'portal_get_entry',
289     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'id'=>'xsd:string', 'select_fields'=>'tns:select_fields'),
290     array('return'=>'tns:get_entry_result'),
291     $NAMESPACE);
292
293 function portal_get_entry($session, $module_name, $id,$select_fields ){
294     global  $beanList, $beanFiles;
295     $error = new SoapError();
296     
297     if(!portal_validate_authenticated($session)){
298         $error->set_error('invalid_session');
299         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
300     }
301     
302     //set the working module
303     set_module_in(array('list'=>array($id=>$id), 'in'=>'('.$id.')'), $module_name);
304     
305     if($_SESSION['type'] == 'lead'){
306         $error->set_error('no_access');
307         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
308     }
309     if(empty($beanList[$module_name])){
310         $error->set_error('no_module');
311         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
312     }
313
314     if(empty($_SESSION['viewable'][$module_name][$id])){
315         $error->set_error('no_access');
316         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
317     }
318
319     $class_name = $beanList[$module_name];
320     require_once($beanFiles[$class_name]);
321     $seed = new $class_name();
322     $seed->retrieve($id);
323     if($module_name == 'KBDocuments') {
324        $body = $seed->get_kbdoc_body($id);
325        $seed->description = $body;
326     }
327
328     $output_list = Array();
329     $output_list[] = get_return_value($seed, $module_name);
330
331     //$output_list[0]['name_value_list']['description'] = array('name'=>'description', 'value'=>$seed->description);
332     //$output_list = filter_return_list($output_list, $select_fields, $module_name);
333     $field_list = array();
334     if(empty($field_list)){
335        $field_list = get_field_list($seed, true);
336     }
337     $output_list = filter_return_list($output_list, $select_fields, $module_name);
338     $field_list = filter_field_list($field_list,$select_fields, $module_name);
339
340     return array('field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
341 }
342
343
344 $server->register(
345     'portal_set_entry',
346     array('session'=>'xsd:string', 'module_name'=>'xsd:string',  'name_value_list'=>'tns:name_value_list'),
347     array('return'=>'tns:set_entry_result'),
348     $NAMESPACE);
349
350 function portal_set_entry($session,$module_name, $name_value_list){
351     global  $beanList, $beanFiles, $valid_modules_for_contact;
352
353     $error = new SoapError();
354     if(!portal_validate_authenticated($session)){
355         $error->set_error('invalid_session');
356         return array('id'=>-1,  'error'=>$error->get_soap_array());
357     }
358     if(empty($beanList[$module_name])){
359         $error->set_error('no_module');
360         return array('id'=>-1, 'error'=>$error->get_soap_array());
361     }
362     if($_SESSION['type'] == 'lead' && $module_name != 'Leads'){
363         $error->set_error('no_access');
364         return array('id'=>-1, 'error'=>$error->get_soap_array());
365     }
366
367     if($_SESSION['type'] == 'contact' && !key_exists($module_name, $valid_modules_for_contact) ){
368         $error->set_error('no_access');
369         return array('id'=>-1, 'error'=>$error->get_soap_array());
370     }
371
372
373     $class_name = $beanList[$module_name];
374     require_once($beanFiles[$class_name]);
375     $seed = new $class_name();
376     $is_update = false;
377     $values_set = array();
378
379     foreach($name_value_list as $value){
380         if($value['name'] == 'id' && !empty($value['value'])) {
381             $seed->disable_row_level_security = true;
382             $seed->retrieve($value['value']);
383             $is_update = true;
384             break;
385         }
386         $values_set[$value['name']] = $value['value'];
387         $seed->$value['name'] = $value['value'];
388     }
389
390     // If it was an update, we have to set the values again
391     if($is_update) {
392         foreach($name_value_list as $value){
393             $seed->$value['name'] = $value['value'];
394         }
395     }
396
397     if(!isset($_SESSION['viewable'][$module_name])){
398         $_SESSION['viewable'][$module_name] = array();
399     }
400
401     if(!$is_update){
402         if(isset($_SESSION['assigned_user_id']) && (!key_exists('assigned_user_id', $values_set) || empty($values_set['assigned_user_id']))){
403             $seed->assigned_user_id = $_SESSION['assigned_user_id'];
404         }
405         if(isset($_SESSION['account_id']) && (!key_exists('account_id', $values_set) || empty($values_set['account_id']))){
406             $seed->account_id = $_SESSION['account_id'];
407         }
408         $seed->portal_flag = 1;
409         $seed->portal_viewable = true;
410     }
411     $id = $seed->save();
412     set_module_in(array('in'=>"('".$GLOBALS['db']->quote($id)."')", 'list'=>array($id)), $module_name);
413     if($_SESSION['type'] == 'contact' && $module_name != 'Contacts' && !$is_update){
414         if($module_name == 'Notes'){
415             $seed->contact_id = $_SESSION['user_id'];
416             if(isset( $_SESSION['account_id'])){
417                 $seed->parent_type = 'Accounts';
418                 $seed->parent_id = $_SESSION['account_id'];
419
420             }
421             $id = $seed->save();
422         }else{
423             $contact = new Contact();
424             $contact->disable_row_level_security = TRUE;
425             $contact->retrieve($_SESSION['user_id']);
426             $seed->contact_id = $contact;
427
428             if(isset( $_SESSION['account_id'])){
429                 $seed->account_id = $_SESSION['account_id'];
430
431             }
432             $seed->save_relationship_changes(false);
433         }
434     }
435     return array('id'=>$id, 'error'=>$error->get_soap_array());
436
437 }
438
439
440
441 /*
442
443 NOTE SPECIFIC CODE
444 */
445 $server->register(
446         'portal_set_note_attachment',
447         array('session'=>'xsd:string','note'=>'tns:note_attachment'),
448         array('return'=>'tns:set_entry_result'),
449         $NAMESPACE);
450
451 function portal_set_note_attachment($session,$note)
452 {
453     $error = new SoapError();
454     if(!portal_validate_authenticated($session)){
455         $error->set_error('invalid_session');
456         return array('id'=>'-1', 'error'=>$error->get_soap_array());
457     }
458     if($_SESSION['type'] == 'lead' || !isset($_SESSION['viewable']['Notes'][$note['id']])){
459         $error->set_error('no_access');
460         return array('id'=>-1, 'error'=>$error->get_soap_array());
461     }
462     require_once('modules/Notes/NoteSoap.php');
463     $ns = new NoteSoap();
464     $id = $ns->saveFile($note, true);
465     return array('id'=>$id, 'error'=>$error->get_soap_array());
466
467 }
468
469 $server->register(
470     'portal_remove_note_attachment',
471     array('session'=>'xsd:string', 'id'=>'xsd:string'),
472     array('return'=>'tns:error_value'),
473     $NAMESPACE);
474
475 function portal_remove_note_attachment($session, $id)
476 {
477     $error = new SoapError();
478     if(! portal_validate_authenticated($session)){
479         $error->set_error('invalid_session');
480         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
481     }
482     if($_SESSION['type'] == 'lead' || !isset($_SESSION['viewable']['Notes'][$id])){
483         $error->set_error('no_access');
484         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
485     }
486     
487     $focus = new Note();
488     $focus->retrieve($id);
489     $result = $focus->deleteAttachment();
490
491     return $error->get_soap_array();
492 }
493
494 $server->register(
495     'portal_get_note_attachment',
496     array('session'=>'xsd:string', 'id'=>'xsd:string'),
497     array('return'=>'tns:return_note_attachment'),
498     $NAMESPACE);
499
500 function portal_get_note_attachment($session,$id)
501 {
502
503     $error = new SoapError();
504     if(! portal_validate_authenticated($session)){
505         $error->set_error('invalid_session');
506         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
507     }
508     if($_SESSION['type'] == 'lead' || !isset($_SESSION['viewable']['Notes'][$id])){
509         $error->set_error('no_access');
510         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
511     }
512     $current_user = $seed_user;
513     
514     $note = new Note();
515     $note->retrieve($id);
516     require_once('modules/Notes/NoteSoap.php');
517     $ns = new NoteSoap();
518     if(!isset($note->filename)){
519         $note->filename = '';
520     }
521     $file= $ns->retrieveFile($id,$note->filename);
522     if($file == -1){
523         $error->set_error('no_file');
524         $file = '';
525     }
526
527     return array('note_attachment'=>array('id'=>$id, 'filename'=>$note->filename, 'file'=>$file), 'error'=>$error->get_soap_array());
528
529 }
530 $server->register(
531     'portal_relate_note_to_module',
532     array('session'=>'xsd:string', 'note_id'=>'xsd:string', 'module_name'=>'xsd:string', 'module_id'=>'xsd:string'),
533     array('return'=>'tns:error_value'),
534     $NAMESPACE);
535
536 function portal_relate_note_to_module($session,$note_id, $module_name, $module_id){
537     global  $beanList, $beanFiles, $current_user;
538     $error = new SoapError();
539     if(! portal_validate_authenticated($session)){
540         $error->set_error('invalid_session');
541         return $error->get_soap_array();
542         }
543     if($_SESSION['type'] == 'lead' || !isset($_SESSION['viewable']['Notes'][$note_id]) || !isset($_SESSION['viewable'][$module_name][$module_id])){
544         $error->set_error('no_access');
545         return $error->get_soap_array();
546         }
547     if(empty($beanList[$module_name])){
548         $error->set_error('no_module');
549         return $error->get_soap_array();
550     }
551
552     $class_name = $beanList[$module_name];
553     require_once($beanFiles[$class_name]);
554
555     $seed = new $class_name();
556     $seed->retrieve($module_id);
557     if($module_name == 'Cases' || $module_name == 'Bugs') {
558         $seed->note_id =  $note_id;
559         $seed->save(false);
560     } else {
561         $error->set_error('no_module_support');
562         $error->description .= ': '. $module_name;
563     }
564     return $error->get_soap_array();
565
566 }
567 $server->register(
568     'portal_get_related_notes',
569     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'module_id'=>'xsd:string',  'select_fields'=>'tns:select_fields', 'order_by'=>'xsd:string'),
570     array('return'=>'tns:get_entry_result'),
571     $NAMESPACE);
572
573 function portal_get_related_notes($session,$module_name, $module_id, $select_fields, $order_by){
574     global  $beanList, $beanFiles;
575     $error = new SoapError();
576     if(! portal_validate_authenticated($session)){
577         $error->set_error('invalid_session');
578         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
579     }
580     if($_SESSION['type'] == 'lead' ){
581         $error->set_error('no_access');
582         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
583     }
584     if(empty($beanList[$module_name])){
585         $error->set_error('no_module');
586         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
587     }
588     if(empty($_SESSION['viewable'][$module_name][$module_id])){
589         $error->set_error('no_access');
590         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
591     }
592
593     if($module_name =='Contacts'){
594         if($_SESSION['user_id'] != $module_id){
595             $error->set_error('no_access');
596             return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
597         }
598         $list = get_notes_in_contacts("('".$GLOBALS['db']->quote($module_id)."')", $order_by);
599     }else{
600         $list = get_notes_in_module("('".$GLOBALS['db']->quote($module_id)."')", $module_name, $order_by);
601     }
602
603
604
605     $output_list = Array();
606     $field_list = Array();
607     foreach($list as $value)
608     {
609         $output_list[] = get_return_value($value, 'Notes');
610         $_SESSION['viewable']['Notes'][$value->id] = $value->id;
611     if(empty($field_list)){
612             $field_list = get_field_list($value, true);
613         }
614     }
615     $output_list = filter_return_list($output_list, $select_fields, $module_name);
616     $field_list = filter_field_list($field_list,$select_fields, $module_name);
617
618
619     return array('result_count'=>sizeof($output_list), 'next_offset'=>0,'field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
620 }
621
622 $server->register(
623     'portal_get_related_list',
624     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'rel_module'=>'xsd:string', 'module_id'=>'xsd:string',  'select_fields'=>'tns:select_fields', 'order_by'=>'xsd:string', 'offset' => 'xsd:int', 'limit' => 'xsd:int'),
625     array('return'=>'tns:get_entry_result'),
626     $NAMESPACE);
627
628 function portal_get_related_list($session, $module_name, $rel_module, $module_id, $select_fields, $order_by, $offset, $limit){
629     global  $beanList, $beanFiles;
630     $error = new SoapError();
631     if(! portal_validate_authenticated($session)){
632         $error->set_error('invalid_session');
633         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
634     }
635     if($_SESSION['type'] == 'lead' ){
636         $error->set_error('no_access');
637         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
638     }
639     if(empty($beanList[$module_name])){
640         $error->set_error('no_module');
641         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
642     }
643     if(empty($_SESSION['viewable'][$module_name][$module_id])){
644         $error->set_error('no_access');
645         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
646     }
647
648     $list = get_related_in_module("('".$GLOBALS['db']->quote($module_id)."')", $module_name, $rel_module, $order_by, $offset, $limit);
649
650     $output_list = Array();
651     $field_list = Array();
652     foreach($list as $value)
653     {
654         $output_list[] = get_return_value($value, $rel_module);
655         $_SESSION['viewable'][$rel_module][$value->id] = $value->id;
656     if(empty($field_list)){
657             $field_list = get_field_list($value, true);
658         }
659     }
660     $output_list = filter_return_list($output_list, $select_fields, $module_name);
661     $field_list = filter_field_list($field_list,$select_fields, $module_name);
662
663
664     return array('result_count'=>$list['result_count'], 'next_offset'=>0,'field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
665 }
666
667 $server->register(
668     'portal_get_module_fields',
669     array('session'=>'xsd:string', 'module_name'=>'xsd:string'),
670     array('return'=>'tns:module_fields'),
671     $NAMESPACE);
672
673 function portal_get_module_fields($session, $module_name){
674     global  $beanList, $beanFiles, $portal_modules, $valid_modules_for_contact;
675     $error = new SoapError();
676     $module_fields = array();
677     if(! portal_validate_authenticated($session)){
678         $error->set_error('invalid_session');
679         $error->description .=$session;
680         return array('module_name'=>$module_name, 'module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
681     }
682     if($_SESSION['type'] == 'lead' && $module_name != 'Leads'){
683         $error->set_error('no_access');
684         return array('module_name'=>$module_name, 'module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
685     }
686
687     if(empty($beanList[$module_name])){
688         $error->set_error('no_module');
689         return array('module_name'=>$module_name, 'module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
690     }
691
692     if(($_SESSION['type'] == 'portal'||$_SESSION['type'] == 'contact') &&  !key_exists($module_name, $valid_modules_for_contact)){
693         $error->set_error('no_module');
694         return array('module_name'=>$module_name, 'module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
695     }
696
697     $class_name = $beanList[$module_name];
698     require_once($beanFiles[$class_name]);
699     $seed = new $class_name();
700     $seed->fill_in_additional_detail_fields();
701     $returnFields = get_return_module_fields($seed, $module_name, $error->get_soap_array(), true);
702     if(is_subclass_of($seed, 'Person')) {
703        $returnFields['module_fields']['email1'] = array('name'=>'email1', 'type'=>'email', 'required'=>0, 'label'=>translate('LBL_EMAIL_ADDRESS', $seed->module_dir));
704        $returnFields['module_fields']['email_opt_out'] = array('name'=>'email_opt_out', 'type'=>'bool', 'required'=>0, 'label'=>translate('LBL_EMAIL_OPT_OUT', $seed->module_dir), 'options'=>array());
705     } //if
706
707     return $returnFields;
708 }
709 $server->register(
710     'portal_get_subscription_lists',
711     array('session'=>'xsd:string'),
712     array('return'=>'tns:get_subscription_lists_result'),
713     $NAMESPACE);
714
715 function portal_get_subscription_lists($session){
716     global  $beanList, $beanFiles;
717
718     $error = new SoapError();
719     if(! portal_validate_authenticated($session)){
720         $error->set_error('invalid_session');
721         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
722     }
723
724     require_once('modules/Campaigns/utils.php');
725
726     $contact = new Contact();
727     $contact->retrieve($_SESSION['user_id']);
728
729     if(!empty($contact->id)) {
730         $result = get_subscription_lists_keyed($contact, true);
731     }
732
733
734     $return_results = array('unsubscribed' => array(), 'subscribed' => array());
735
736     foreach($result['unsubscribed'] as $newsletter_name => $data) {
737         $return_results['unsubscribed'][] = array('name' => $newsletter_name, 'prospect_list_id' => $data['prospect_list_id'],
738                                                   'campaign_id' => $data['campaign_id'], 'description' => $data['description'],
739                                                   'frequency' => $data['frequency']);
740     }
741     foreach($result['subscribed'] as $newsletter_name => $data) {
742         $return_results['subscribed'][] = array('name' => $newsletter_name, 'prospect_list_id' => $data['prospect_list_id'],
743                                                 'campaign_id' => $data['campaign_id'], 'description' => $data['description'],
744                                                 'frequency' => $data['frequency']);
745     }
746
747     return array('unsubscribed'=>$return_results['unsubscribed'], 'subscribed' => $return_results['subscribed'], 'error'=>$error->get_soap_array());
748 }
749
750 $server->register(
751     'portal_set_newsletters',
752     array('session'=>'xsd:string', 'subscribe_ids' => 'tns:select_fields', 'unsubscribe_ids' => 'tns:select_fields'),
753     array('return'=>'tns:error_value'),
754     $NAMESPACE);
755
756 function portal_set_newsletters($session, $subscribe_ids, $unsubscribe_ids){
757     global  $beanList, $beanFiles;
758
759     $error = new SoapError();
760     if(! portal_validate_authenticated($session)){
761         $error->set_error('invalid_session');
762         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
763     }
764
765     require_once('modules/Campaigns/utils.php');
766
767     $contact = new Contact();
768     $contact->retrieve($_SESSION['user_id']);
769
770     if(!empty($contact->id)) {
771         foreach($subscribe_ids as $campaign_id) {
772             subscribe($campaign_id, null, $contact, true);
773         }
774         foreach($unsubscribe_ids as $campaign_id) {
775             unsubscribe($campaign_id, $contact);
776         }
777     }
778
779     return $error->get_soap_array();
780 }
781
782 ?>