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