]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - soap/SoapPortalHelper.php
Release 6.1.5
[Github/sugarcrm.git] / soap / SoapPortalHelper.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 $portal_modules = array('Contacts', 'Accounts', 'Notes');
39 $portal_modules[] = 'Cases';
40 $portal_modules[] = 'Bugs';
41
42
43 /*
44 BUGS
45 */
46
47
48
49
50
51 function get_bugs_in_contacts($in, $orderBy = '', $where='')
52     {
53         //bail if the in is empty
54         if(empty($in)  || $in =='()' || $in =="('')")return;
55         // First, get the list of IDs.
56         
57         $query = "SELECT bug_id as id from contacts_bugs where contact_id IN $in AND deleted=0";
58         if(!empty($orderBy)){
59             $query .= ' ORDER BY ' . $orderBy;
60         }
61
62         $sugar = new Contact();
63         set_module_in($sugar->build_related_in($query), 'Bugs');
64     }
65
66 function get_bugs_in_accounts($in, $orderBy = '', $where='')
67     {
68         //bail if the in is empty
69         if(empty($in)  || $in =='()' || $in =="('')")return;
70         // First, get the list of IDs.
71         
72         $query = "SELECT bug_id as id from accounts_bugs where account_id IN $in AND deleted=0";
73         if(!empty($orderBy)){
74             $query .= ' ORDER BY ' . $orderBy;
75         }
76
77         $sugar = new Account();
78
79         set_module_in($sugar->build_related_in($query), 'Bugs');
80     }
81
82 /*
83 Cases
84 */
85
86 function get_cases_in_contacts($in, $orderBy = '')
87     {
88         //bail if the in is empty
89         if(empty($in)  || $in =='()' || $in =="('')")return;
90         // First, get the list of IDs.
91
92         $query = "SELECT case_id as id from contacts_cases where contact_id IN $in AND deleted=0";
93         if(!empty($orderBy)){
94             $query .= ' ORDER BY ' . $orderBy;
95         }
96
97         $sugar = new Contact();
98         set_module_in($sugar->build_related_in($query), 'Cases');
99     }
100
101 function get_cases_in_accounts($in, $orderBy = '')
102     {
103         if(empty($_SESSION['viewable']['Accounts'])){
104             return;
105         }
106         //bail if the in is empty
107         if(empty($in)  || $in =='()' || $in =="('')")return;
108         // First, get the list of IDs.
109         $query = "SELECT id  from cases where account_id IN $in AND deleted=0";
110         if(!empty($orderBy)){
111             $query .= ' ORDER BY ' . $orderBy;
112         }       
113         
114         $sugar = new Account();
115         set_module_in($sugar->build_related_in($query), 'Cases');
116     }
117
118
119
120 /*
121 NOTES
122 */
123
124
125 function get_notes_in_contacts($in, $orderBy = '')
126     {
127         //bail if the in is empty
128         if(empty($in)  || $in =='()' || $in =="('')")return;
129         // First, get the list of IDs.
130         $query = "SELECT id from notes where contact_id IN $in AND deleted=0 AND portal_flag=1";
131         if(!empty($orderBy)){
132             $query .= ' ORDER BY ' . $orderBy;
133         }
134             
135         $contact = new Contact();
136         $note = new Note();
137         return $contact->build_related_list($query, $note);
138     }
139
140 function get_notes_in_module($in, $module, $orderBy = '')
141     {
142         //bail if the in is empty
143         if(empty($in)  || $in =='()' || $in =="('')")return;
144         // First, get the list of IDs.
145         $query = "SELECT id from notes where parent_id IN $in AND parent_type='$module' AND deleted=0 AND portal_flag = 1";
146         if(!empty($orderBy)){
147             $query .= ' ORDER BY ' . $orderBy;
148         }
149         global $beanList, $beanFiles;
150
151         if(!empty($beanList[$module])){
152             $class_name = $beanList[$module];
153             require_once($beanFiles[$class_name]);
154             $sugar = new $class_name();
155         }else{
156             return array();
157         }
158
159         $note = new Note();
160         return $sugar->build_related_list($query, $note);
161     }
162     
163     function get_related_in_module($in, $module, $rel_module, $orderBy = '', $row_offset = 0, $limit= -1)
164     {
165         global $beanList, $beanFiles;
166          if(!empty($beanList[$rel_module])){
167             $class_name = $beanList[$rel_module];
168             require_once($beanFiles[$class_name]);
169             $rel = new $class_name();
170         }else{
171             return array();
172         }
173         
174         //bail if the in is empty
175         if(empty($in)  || $in =='()' || $in =="('')")return;
176
177         // First, get the list of IDs.
178         if ($module == 'KBDocuments' || $module == 'DocumentRevisions') {
179             $query = "SELECT dr.* from document_revisions dr
180                       inner join kbdocument_revisions kr on kr.document_revision_id = dr.id AND kr.kbdocument_id IN ($in)
181                       AND dr.file_mime_type is not null";
182         } else {
183             $query = "SELECT id from $rel->table_name where parent_id IN $in AND parent_type='$module' AND deleted=0 AND portal_flag = 1";
184         }
185
186         if(!empty($orderBy)){
187             $query .= ' ORDER BY ' . $orderBy;
188         }
189
190         if(!empty($beanList[$module])){
191             $class_name = $beanList[$module];
192             require_once($beanFiles[$class_name]);
193             $sugar = new $class_name();
194         }else{
195             return array();
196         }
197         
198         
199         $count_query = $sugar->create_list_count_query($query);
200         if(!empty($count_query))
201         {
202             // We have a count query.  Run it and get the results.
203             $result = $sugar->db->query($count_query, true, "Error running count query for $sugar->object_name List: ");
204             $assoc = $sugar->db->fetchByAssoc($result);
205             if(!empty($assoc['c']))
206             {
207                 $rows_found = $assoc['c'];
208             }
209         }
210         $list = $sugar->build_related_list($query, $rel, $row_offset, $limit);
211         $list['result_count'] = $rows_found;
212         return $list;
213     }
214
215 function get_accounts_from_contact($contact_id, $orderBy = '')
216     {
217                 // First, get the list of IDs.
218         $query = "SELECT account_id as id from accounts_contacts where contact_id='$contact_id' AND deleted=0";
219         if(!empty($orderBy)){
220             $query .= ' ORDER BY ' . $orderBy;
221         }
222         $sugar = new Contact();
223         set_module_in($sugar->build_related_in($query), 'Accounts');
224     }
225
226 function get_contacts_from_account($account_id, $orderBy = '')
227     {
228         // First, get the list of IDs.
229         $query = "SELECT contact_id as id from accounts_contacts where account_id='$account_id' AND deleted=0";
230         if(!empty($orderBy)){
231             $query .= ' ORDER BY ' . $orderBy;
232         }
233         $sugar = new Account();
234         set_module_in($sugar->build_related_in($query), 'Contacts');
235     }
236
237 function get_related_list($in, $template, $where, $order_by, $row_offset = 0, $limit = ""){
238
239         $q = '';
240         //if $in is empty then pass in a query to get the list of related list
241         if(empty($in)  || $in =='()' || $in =="('')"){
242             $in = '';
243             //build the query to pass into the template list function
244              $q = 'select id from '.$template->table_name.' where deleted = 0 ';
245                 //add where statement if it is not empty
246                         if(!empty($where)){
247                                 $q .= ' and '.$where;
248                         }
249         }
250         
251         return $template->build_related_list_where($q, $template, $where, $in, $order_by, $limit, $row_offset);
252
253     }
254
255 function build_relationship_tree($contact){
256     global $sugar_config;
257     $contact->retrieve($contact->id);
258
259     get_accounts_from_contact($contact->id);
260
261     set_module_in(array('list'=>array($contact->id), 'in'=> "('$contact->id')"), 'Contacts');
262
263     $accounts = $_SESSION['viewable']['Accounts'];
264     foreach($accounts as $id){
265         if(!isset($sugar_config['portal_view']) || $sugar_config['portal_view'] != 'single_user'){
266             get_contacts_from_account($id);
267         }
268     }
269 }
270
271 function get_contacts_in(){
272     return $_SESSION['viewable']['contacts_in'];
273 }
274
275 function get_accounts_in(){
276     return $_SESSION['viewable']['accounts_in'];
277 }
278
279 function get_module_in($module_name){
280     if(!isset($_SESSION['viewable'][$module_name])){
281         return '()';
282     }
283
284     $mod_in = "('" . join("','", array_keys($_SESSION['viewable'][$module_name])) . "')";
285     $_SESSION['viewable'][strtolower($module_name).'_in'] = $mod_in;
286     
287     return $mod_in;
288 }
289
290 function set_module_in($arrayList, $module_name){
291
292         if(!isset($_SESSION['viewable'][$module_name])){
293             $_SESSION['viewable'][$module_name] = array();
294         }
295         foreach($arrayList['list'] as $id){
296             $_SESSION['viewable'][$module_name][$id] = $id;
297         }
298         if($module_name == 'Accounts' && isset($id)){
299             $_SESSION['account_id'] = $id;
300         }
301
302         if(!empty($_SESSION['viewable'][strtolower($module_name).'_in'])){
303             if($arrayList['in'] != '()') {
304                 $_SESSION['viewable'][strtolower($module_name).'_in'] = "('" . implode("', '", $_SESSION['viewable'][strtolower($module_name).'_in']);
305                 $_SESSION['viewable'][strtolower($module_name).'_in'] .= implode("', '", $arrayList['list']) . "')";
306             }
307         }else{
308             $_SESSION['viewable'][strtolower($module_name).'_in'] = $arrayList['in'];
309         }
310 }
311
312 /*
313  * Given the user auth, attempt to log the user in.
314  * used by SoapPortalUsers.php
315  */
316 function login_user($portal_auth){
317      $error = new SoapError();
318      $user = new User();
319      $user = $user->retrieve_by_string_fields(array('user_name'=>$portal_auth['user_name'],'user_hash'=>$portal_auth['password'], 'deleted'=>0, 'status'=>'Active', 'portal_only'=>1) );    
320         
321         if($user != null){
322             global $current_user;
323             $current_user = $user;
324             return 'success';
325         }else{
326             $GLOBALS['log']->fatal('SECURITY: User authentication for '. $portal_auth['user_name']. ' failed');
327             return 'fail';
328         }
329 }
330
331
332 function portal_get_entry_list_limited($session, $module_name,$where, $order_by, $select_fields, $row_offset, $limit){
333     global  $beanList, $beanFiles, $portal_modules;
334     $error = new SoapError();
335     if(! portal_validate_authenticated($session)){
336         $error->set_error('invalid_session');
337         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
338     }
339     if($_SESSION['type'] == 'lead' ){
340         $error->set_error('no_access');
341         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
342     }
343     if(empty($beanList[$module_name])){
344         $error->set_error('no_module');
345         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
346     }
347     if($module_name == 'Cases'){
348
349         //if the related cases have not yet been loaded into the session object,
350         //then call the methods that will load the cases related to the contact/accounts for this user
351         if(!isset($_SESSION['viewable'][$module_name])){
352             //retrieve the contact/account id's for this user
353             $c =get_contacts_in();
354             $a = get_accounts_in();
355            if(!empty($c)) {get_cases_in_contacts($c);}
356            if(!empty($a)) { get_cases_in_accounts($a);}
357         }
358          
359         $sugar = new aCase();
360
361         $list = array();
362         //if no Cases have been loaded into the session as viewable, then do not issue query, just return empty list
363         //issuing a query with no cases loaded in session will return ALL the Cases, which is not a good thing
364         if(!empty($_SESSION['viewable'][$module_name])){
365             $list =  get_related_list(get_module_in($module_name), new aCase(), $where,$order_by, $row_offset, $limit);
366         }
367
368     }else if($module_name == 'Contacts'){
369             $sugar = new Contact();
370             $list =  get_related_list(get_module_in($module_name), new Contact(), $where,$order_by);
371     }else if($module_name == 'Accounts'){
372             $sugar = new Account();
373             $list =  get_related_list(get_module_in($module_name), new Account(), $where,$order_by);
374     }else if($module_name == 'Bugs'){
375
376         //if the related bugs have not yet been loaded into the session object,
377         //then call the methods that will load the bugs related to the contact/accounts for this user
378             if(!isset($_SESSION['viewable'][$module_name])){
379                 //retrieve the contact/account id's for this user
380                 $c =get_contacts_in();
381                 $a = get_accounts_in();
382                 if(!empty($c)) {get_bugs_in_contacts($c);}
383                 if(!empty($a)) {get_bugs_in_accounts($a);}
384             }
385
386         $list = array();
387         //if no Bugs have been loaded into the session as viewable, then do not issue query, just return empty list
388         //issuing a query with no bugs loaded in session will return ALL the Bugs, which is not a good thing
389         if(!empty($_SESSION['viewable'][$module_name])){
390             $list = get_related_list(get_module_in($module_name), new Bug(), $where, $order_by, $row_offset, $limit);
391         }
392     } else if ($module_name == 'KBDocuments') {
393     } else if ($module_name == 'FAQ') {
394     } else{
395         $error->set_error('no_module_support');
396         return array('result_count'=>-1, 'entry_list'=>array(), 'error'=>$error->get_soap_array());
397
398     }
399
400     $output_list = Array();
401     $field_list = array();
402     foreach($list as $value)
403     {
404
405         //$loga->fatal("Adding another account to the list");
406         $output_list[] = get_return_value($value, $module_name);
407         $_SESSION['viewable'][$module_name][$value->id] = $value->id;
408         if(empty($field_list)){
409             $field_list = get_field_list($value);
410         }
411     }
412     $output_list = filter_return_list($output_list, $select_fields, $module_name);
413     $field_list = filter_field_list($field_list,$select_fields, $module_name);
414
415     return array('result_count'=>sizeof($output_list), 'next_offset'=>0,'field_list'=>$field_list, 'entry_list'=>$output_list, 'error'=>$error->get_soap_array());
416 }
417
418 $invalid_contact_fields = array('portal_name'=>1, 'portal_password'=>1, 'portal_active'=>1);
419 $valid_modules_for_contact = array('Contacts'=>1, 'Cases'=>1, 'Notes'=>1, 'Bugs'=>1, 'Accounts'=>1, 'Leads'=>1, 'KBDocuments'=>1);
420
421
422
423
424 ?>