]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - soap/SoapPortalUsers.php
Release 6.2.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("'$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'=>"('$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             $seed->contact_id = $_SESSION['user_id'];
424
425             if(isset( $_SESSION['account_id'])){
426                 $seed->account_id = $_SESSION['account_id'];
427
428             }
429             $seed->save_relationship_changes(false);
430         }
431     }
432     return array('id'=>$id, 'error'=>$error->get_soap_array());
433
434 }
435
436
437
438 /*
439
440 NOTE SPECIFIC CODE
441 */
442 $server->register(
443         'portal_set_note_attachment',
444         array('session'=>'xsd:string','note'=>'tns:note_attachment'),
445         array('return'=>'tns:set_entry_result'),
446         $NAMESPACE);
447
448 function portal_set_note_attachment($session,$note)
449 {
450     $error = new SoapError();
451     if(!portal_validate_authenticated($session)){
452         $error->set_error('invalid_session');
453         return array('id'=>'-1', 'error'=>$error->get_soap_array());
454     }
455     if($_SESSION['type'] == 'lead' || !isset($_SESSION['viewable']['Notes'][$note['id']])){
456         $error->set_error('no_access');
457         return array('id'=>-1, 'error'=>$error->get_soap_array());
458     }
459     require_once('modules/Notes/NoteSoap.php');
460     $ns = new NoteSoap();
461     $id = $ns->saveFile($note, true);
462     return array('id'=>$id, 'error'=>$error->get_soap_array());
463
464 }
465
466 $server->register(
467     'portal_remove_note_attachment',
468     array('session'=>'xsd:string', 'id'=>'xsd:string'),
469     array('return'=>'tns:error_value'),
470     $NAMESPACE);
471
472 function portal_remove_note_attachment($session, $id)
473 {
474     $error = new SoapError();
475     if(! portal_validate_authenticated($session)){
476         $error->set_error('invalid_session');
477         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
478     }
479     if($_SESSION['type'] == 'lead' || !isset($_SESSION['viewable']['Notes'][$id])){
480         $error->set_error('no_access');
481         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
482     }
483     
484     $focus = new Note();
485     $focus->retrieve($id);
486     $result = $focus->deleteAttachment();
487
488     return $error->get_soap_array();
489 }
490
491 $server->register(
492     'portal_get_note_attachment',
493     array('session'=>'xsd:string', 'id'=>'xsd:string'),
494     array('return'=>'tns:return_note_attachment'),
495     $NAMESPACE);
496
497 function portal_get_note_attachment($session,$id)
498 {
499
500     $error = new SoapError();
501     if(! portal_validate_authenticated($session)){
502         $error->set_error('invalid_session');
503         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
504     }
505     if($_SESSION['type'] == 'lead' || !isset($_SESSION['viewable']['Notes'][$id])){
506         $error->set_error('no_access');
507         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
508     }
509     $current_user = $seed_user;
510     
511     $note = new Note();
512     $note->retrieve($id);
513     require_once('modules/Notes/NoteSoap.php');
514     $ns = new NoteSoap();
515     if(!isset($note->filename)){
516         $note->filename = '';
517     }
518     $file= $ns->retrieveFile($id,$note->filename);
519     if($file == -1){
520         $error->set_error('no_file');
521         $file = '';
522     }
523
524     return array('note_attachment'=>array('id'=>$id, 'filename'=>$note->filename, 'file'=>$file), 'error'=>$error->get_soap_array());
525
526 }
527 $server->register(
528     'portal_relate_note_to_module',
529     array('session'=>'xsd:string', 'note_id'=>'xsd:string', 'module_name'=>'xsd:string', 'module_id'=>'xsd:string'),
530     array('return'=>'tns:error_value'),
531     $NAMESPACE);
532
533 function portal_relate_note_to_module($session,$note_id, $module_name, $module_id){
534     global  $beanList, $beanFiles, $current_user;
535     $error = new SoapError();
536     if(! portal_validate_authenticated($session)){
537         $error->set_error('invalid_session');
538         return $error->get_soap_array();
539         }
540     if($_SESSION['type'] == 'lead' || !isset($_SESSION['viewable']['Notes'][$note_id]) || !isset($_SESSION['viewable'][$module_name][$module_id])){
541         $error->set_error('no_access');
542         return $error->get_soap_array();
543         }
544     if(empty($beanList[$module_name])){
545         $error->set_error('no_module');
546         return $error->get_soap_array();
547     }
548
549     $class_name = $beanList[$module_name];
550     require_once($beanFiles[$class_name]);
551
552     $seed = new $class_name();
553     $seed->retrieve($module_id);
554     if($module_name == 'Cases' || $module_name == 'Bugs') {
555         $seed->note_id =  $note_id;
556         $seed->save(false);
557     } else {
558         $error->set_error('no_module_support');
559         $error->description .= ': '. $module_name;
560     }
561     return $error->get_soap_array();
562
563 }
564 $server->register(
565     'portal_get_related_notes',
566     array('session'=>'xsd:string', 'module_name'=>'xsd:string', 'module_id'=>'xsd:string',  'select_fields'=>'tns:select_fields', 'order_by'=>'xsd:string'),
567     array('return'=>'tns:get_entry_result'),
568     $NAMESPACE);
569
570 function portal_get_related_notes($session,$module_name, $module_id, $select_fields, $order_by){
571     global  $beanList, $beanFiles;
572     $error = new SoapError();
573     if(! portal_validate_authenticated($session)){
574         $error->set_error('invalid_session');
575         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
576     }
577     if($_SESSION['type'] == 'lead' ){
578         $error->set_error('no_access');
579         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
580     }
581     if(empty($beanList[$module_name])){
582         $error->set_error('no_module');
583         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
584     }
585     if(empty($_SESSION['viewable'][$module_name][$module_id])){
586         $error->set_error('no_access');
587         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
588     }
589
590     if($module_name =='Contacts'){
591         if($_SESSION['user_id'] != $module_id){
592             $error->set_error('no_access');
593             return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
594         }
595         $list = get_notes_in_contacts("('$module_id')", $order_by);
596     }else{
597         $list = get_notes_in_module("('$module_id')", $module_name, $order_by);
598     }
599
600
601
602     $output_list = Array();
603     $field_list = Array();
604     foreach($list as $value)
605     {
606         $output_list[] = get_return_value($value, 'Notes');
607         $_SESSION['viewable']['Notes'][$value->id] = $value->id;
608     if(empty($field_list)){
609             $field_list = get_field_list($value, true);
610         }
611     }
612     $output_list = filter_return_list($output_list, $select_fields, $module_name);
613     $field_list = filter_field_list($field_list,$select_fields, $module_name);
614
615
616     return array('result_count'=>sizeof($output_list), 'next_offset'=>0,'field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
617 }
618
619 $server->register(
620     'portal_get_related_list',
621     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'),
622     array('return'=>'tns:get_entry_result'),
623     $NAMESPACE);
624
625 function portal_get_related_list($session, $module_name, $rel_module, $module_id, $select_fields, $order_by, $offset, $limit){
626     global  $beanList, $beanFiles;
627     $error = new SoapError();
628     if(! portal_validate_authenticated($session)){
629         $error->set_error('invalid_session');
630         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
631     }
632     if($_SESSION['type'] == 'lead' ){
633         $error->set_error('no_access');
634         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
635     }
636     if(empty($beanList[$module_name])){
637         $error->set_error('no_module');
638         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
639     }
640     if(empty($_SESSION['viewable'][$module_name][$module_id])){
641         $error->set_error('no_access');
642         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
643     }
644
645     $list = get_related_in_module("('$module_id')", $module_name, $rel_module, $order_by, $offset, $limit);
646
647     $output_list = Array();
648     $field_list = Array();
649     foreach($list as $value)
650     {
651         $output_list[] = get_return_value($value, $rel_module);
652         $_SESSION['viewable'][$rel_module][$value->id] = $value->id;
653     if(empty($field_list)){
654             $field_list = get_field_list($value, true);
655         }
656     }
657     $output_list = filter_return_list($output_list, $select_fields, $module_name);
658     $field_list = filter_field_list($field_list,$select_fields, $module_name);
659
660
661     return array('result_count'=>$list['result_count'], 'next_offset'=>0,'field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
662 }
663
664 $server->register(
665     'portal_get_module_fields',
666     array('session'=>'xsd:string', 'module_name'=>'xsd:string'),
667     array('return'=>'tns:module_fields'),
668     $NAMESPACE);
669
670 function portal_get_module_fields($session, $module_name){
671     global  $beanList, $beanFiles, $portal_modules, $valid_modules_for_contact;
672     $error = new SoapError();
673     $module_fields = array();
674     if(! portal_validate_authenticated($session)){
675         $error->set_error('invalid_session');
676         $error->description .=$session;
677         return array('module_name'=>$module_name, 'module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
678     }
679     if($_SESSION['type'] == 'lead' && $module_name != 'Leads'){
680         $error->set_error('no_access');
681         return array('module_name'=>$module_name, 'module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
682     }
683
684     if(empty($beanList[$module_name])){
685         $error->set_error('no_module');
686         return array('module_name'=>$module_name, 'module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
687     }
688
689     if(($_SESSION['type'] == 'portal'||$_SESSION['type'] == 'contact') &&  !key_exists($module_name, $valid_modules_for_contact)){
690         $error->set_error('no_module');
691         return array('module_name'=>$module_name, 'module_fields'=>$module_fields, 'error'=>$error->get_soap_array());
692     }
693
694     $class_name = $beanList[$module_name];
695     require_once($beanFiles[$class_name]);
696     $seed = new $class_name();
697     $seed->fill_in_additional_detail_fields();
698     $returnFields = get_return_module_fields($seed, $module_name, $error->get_soap_array(), true);
699     if(is_subclass_of($seed, 'Person')) {
700        $returnFields['module_fields']['email1'] = array('name'=>'email1', 'type'=>'email', 'required'=>0, 'label'=>translate('LBL_EMAIL_ADDRESS', $seed->module_dir));
701        $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());
702     } //if
703
704     return $returnFields;
705 }
706 $server->register(
707     'portal_get_subscription_lists',
708     array('session'=>'xsd:string'),
709     array('return'=>'tns:get_subscription_lists_result'),
710     $NAMESPACE);
711
712 function portal_get_subscription_lists($session){
713     global  $beanList, $beanFiles;
714
715     $error = new SoapError();
716     if(! portal_validate_authenticated($session)){
717         $error->set_error('invalid_session');
718         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
719     }
720
721     require_once('modules/Campaigns/utils.php');
722
723     $contact = new Contact();
724     $contact->retrieve($_SESSION['user_id']);
725
726     if(!empty($contact->id)) {
727         $result = get_subscription_lists_keyed($contact, true);
728     }
729
730
731     $return_results = array('unsubscribed' => array(), 'subscribed' => array());
732
733     foreach($result['unsubscribed'] as $newsletter_name => $data) {
734         $return_results['unsubscribed'][] = array('name' => $newsletter_name, 'prospect_list_id' => $data['prospect_list_id'],
735                                                   'campaign_id' => $data['campaign_id'], 'description' => $data['description'],
736                                                   'frequency' => $data['frequency']);
737     }
738     foreach($result['subscribed'] as $newsletter_name => $data) {
739         $return_results['subscribed'][] = array('name' => $newsletter_name, 'prospect_list_id' => $data['prospect_list_id'],
740                                                 'campaign_id' => $data['campaign_id'], 'description' => $data['description'],
741                                                 'frequency' => $data['frequency']);
742     }
743
744     return array('unsubscribed'=>$return_results['unsubscribed'], 'subscribed' => $return_results['subscribed'], 'error'=>$error->get_soap_array());
745 }
746
747 $server->register(
748     'portal_set_newsletters',
749     array('session'=>'xsd:string', 'subscribe_ids' => 'tns:select_fields', 'unsubscribe_ids' => 'tns:select_fields'),
750     array('return'=>'tns:error_value'),
751     $NAMESPACE);
752
753 function portal_set_newsletters($session, $subscribe_ids, $unsubscribe_ids){
754     global  $beanList, $beanFiles;
755
756     $error = new SoapError();
757     if(! portal_validate_authenticated($session)){
758         $error->set_error('invalid_session');
759         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
760     }
761
762     require_once('modules/Campaigns/utils.php');
763
764     $contact = new Contact();
765     $contact->retrieve($_SESSION['user_id']);
766
767     if(!empty($contact->id)) {
768         foreach($subscribe_ids as $campaign_id) {
769             subscribe($campaign_id, null, $contact, true);
770         }
771         foreach($unsubscribe_ids as $campaign_id) {
772             unsubscribe($campaign_id, $contact);
773         }
774     }
775
776     return $error->get_soap_array();
777 }
778
779 ?>