]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/EAPM/controller.php
Release 6.5.0
[Github/sugarcrm.git] / modules / EAPM / controller.php
1 <?PHP
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 class EAPMController extends SugarController
39 {
40     /**
41      * API implementation
42      * @var ExternalAPIPlugin
43      */
44     protected $api;
45
46     var $action_remap = array('detailview'=>'editview', 'DetailView'=>'EditView');
47
48     var $admin_actions = array('listview', 'index');
49
50         public function process() {
51                 if(!is_admin($GLOBALS['current_user']) && in_array(strtolower($this->action), $this->admin_actions)) {
52                         $this->hasAccess = false;
53                 }
54                 parent::process();
55         }
56
57     protected function failed($error)
58     {
59         SugarApplication::appendErrorMessage($error);
60         $GLOBALS['log']->error("Login error: $error");
61         $url = 'index.php?module=EAPM&action=EditView&record='.$this->bean->id;
62
63         if($this->return_module == 'Import'){
64             $url .= "&application={$this->bean->application}&return_module={$this->return_module}&return_action={$this->return_action}";
65         }
66         return $this->set_redirect($url);
67     }
68
69     public function pre_save()
70     {
71         parent::pre_save();
72         $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
73         if(empty($this->api)) {
74             return $this->failed(translate('LBL_AUTH_UNSUPPORTED', $this->bean->module_dir));
75         }
76         if(empty($this->bean->id)){
77             $eapmBean = EAPM::getLoginInfo($this->bean->application,true);
78             if($eapmBean){
79                 SugarApplication::appendErrorMessage(translate('LBL_APPLICATION_FOUND_NOTICE', $this->bean->module_dir));
80                 $this->bean->id = $eapmBean->id;
81             }
82         }
83         $this->bean->validated = false;
84         $this->bean->save_cleanup();
85         $this->api->loadEAPM($this->bean);
86     }
87
88     protected function post_save()
89     {
90         if(!$this->bean->deleted) {
91             // do not load bean here since password is already encoded
92             if ( $this->api->authMethod != 'oauth' ) {
93                 // OAuth beans have to be handled specially.
94                 
95                 $reply = $this->api->checkLogin();
96                 if ( !$reply['success'] ) {
97                     return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
98                 } else {
99                     $this->bean->validated();
100                 }
101             }
102         }
103         if($this->return_module == 'Users'){
104             $this->return_action = 'EditView';
105         }
106         parent::post_save();
107
108         if($this->return_module == 'Import'){
109             $this->set_redirect("index.php?module=Import&action=Step1&import_module=". $this->return_action . "&application=" . $this->bean->application);
110         }
111         // Override the redirect location to add the hash
112         $this->redirect_url = $this->redirect_url.'#tab5';
113         if ( $this->api->authMethod == 'oauth' && !$this->bean->deleted ) {
114             // It's OAuth, we have to handle this specially.
115             // We need to create a new window to handle the OAuth, and redirect this window back to the edit view
116             // So we will handle that in javascript.
117             $popup_warning_msg = string_format($GLOBALS['mod_strings']['LBL_ERR_POPUPS_DISABLED'], array($_SERVER['HTTP_HOST']) );
118             echo('<script src="modules/EAPM/EAPMEdit.js" type="text/javascript"></script><script type="text/javascript">EAPMPopupAndRedirect("index.php?module=EAPM&action=oauth&record='.$this->bean->id.'", "'.$this->redirect_url.'", \''.$popup_warning_msg.'\'); </script>');
119
120             // To prevent the normal handler from issuing a header call and destroying our neat little javascript we'll
121             // end right here.
122             sugar_die('');
123         } else {
124             return;
125         }
126     }
127
128     protected function action_oauth()
129     {
130         if(empty($this->bean->id)) {
131             return $this->set_redirect('index.php');
132         }
133                 if(!$this->bean->ACLAccess('save')){
134                         ACLController::displayNoAccess(true);
135                         sugar_cleanup(true);
136                         return true;
137                 }
138         if(empty($_REQUEST['oauth_error'])) {
139             $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
140             $reply = $this->api->checkLogin($this->bean);
141             if ( !$reply['success'] ) {
142                 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
143             } else {
144                 $this->bean->validated();
145             }
146         }
147         
148         // This is a tweak so that we can automatically close windows if requested by the external account system
149         if ( isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
150             if(!empty($_REQUEST['callbackFunction']) && !empty($_REQUEST['application'])){
151                 $js = '<script type="text/javascript">window.opener.' . $_REQUEST['callbackFunction'] . '("' . $_REQUEST['application'] . '"); window.close();</script>';
152             }else if(!empty($_REQUEST['refreshParentWindow'])){
153                 $js = '<script type="text/javascript">window.opener.location.reload();window.close();</script>';
154             }else{
155                 $js = '<script type="text/javascript">window.close();</script>';
156             }
157             echo($js);
158             return;
159         }            
160         
161         // redirect to detail view, as in save
162         return parent::post_save();
163     }
164
165     protected function pre_QuickSave(){
166         if(!empty($_REQUEST['application'])){
167             $eapmBean = EAPM::getLoginInfo($_REQUEST['application'],true);
168             if (!$eapmBean) {
169                 $this->bean->application = $_REQUEST['application'];
170                 $this->bean->assigned_user_id = $GLOBALS['current_user']->id;
171             }else{
172                 $this->bean = $eapmBean;
173             }
174             $this->pre_save();
175                     
176         }else{
177             sugar_die("Please pass an application name.");
178         }
179     }
180     
181         public function action_QuickSave(){
182         $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
183         $this->action_save();
184
185         if ( $this->api->authMethod == 'oauth' ) {
186             $this->action_oauth();
187         }
188         }
189
190     protected function post_QuickSave(){
191         $this->post_save();
192     }
193
194     protected function pre_Reauthenticate(){
195         $this->pre_save();
196     }
197
198     protected function action_Reauthenticate(){
199         if ( $this->api->authMethod == 'oauth' ) {
200             // OAuth beans have to be handled specially.
201             
202             $reply = $this->api->checkLogin();
203             if ( !$reply['success'] ) {
204                 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
205             } else {
206                 $this->bean->validated();
207             }
208         } else {
209             // Normal auth methods go through this.
210             $this->action_save();
211         }
212     }
213
214     protected function post_Reauthenticate(){
215         $this->post_save();
216     }
217
218     protected function action_FlushFileCache()
219     {
220         $api = ExternalAPIFactory::loadAPI($_REQUEST['api']);
221         if ( $api == false ) {
222             echo 'FAILED';
223             return;
224         }
225
226         if ( method_exists($api,'loadDocCache') ) {
227             $api->loadDocCache(true);
228         }
229
230         echo 'SUCCESS';
231     }
232
233     protected function remapAction() {
234         if ( $this->do_action == 'DetailView' ) {
235             $this->do_action = 'EditView';
236             $this->action = 'EditView';
237         }
238         
239         parent::remapAction();
240     }
241
242 }