]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/EAPM/controller.php
Release 6.5.1
[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         if($this->module == 'EAPM') {
112             $this->set_redirect('index.php?module=Users&action=EditView&record=' . $_POST['assigned_user_id']);
113         }
114         // Override the redirect location to add the hash
115         $this->redirect_url = $this->redirect_url.'#tab5';
116         if ( $this->api->authMethod == 'oauth' && !$this->bean->deleted ) {
117             // It's OAuth, we have to handle this specially.
118             // We need to create a new window to handle the OAuth, and redirect this window back to the edit view
119             // So we will handle that in javascript.
120             $popup_warning_msg = string_format($GLOBALS['mod_strings']['LBL_ERR_POPUPS_DISABLED'], array($_SERVER['HTTP_HOST']) );
121             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>');
122
123             // To prevent the normal handler from issuing a header call and destroying our neat little javascript we'll
124             // end right here.
125             sugar_die('');
126         } else {
127             return;
128         }
129     }
130
131     protected function action_oauth()
132     {
133         if(empty($this->bean->id)) {
134             return $this->set_redirect('index.php');
135         }
136                 if(!$this->bean->ACLAccess('save')){
137                         ACLController::displayNoAccess(true);
138                         sugar_cleanup(true);
139                         return true;
140                 }
141         if(empty($_REQUEST['oauth_error'])) {
142             $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
143             $reply = $this->api->checkLogin($this->bean);
144             if ( !$reply['success'] ) {
145                 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
146             } else {
147                 $this->bean->validated();
148             }
149         }
150         
151         // This is a tweak so that we can automatically close windows if requested by the external account system
152         if ( isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
153             if(!empty($_REQUEST['callbackFunction']) && !empty($_REQUEST['application'])){
154                 $js = '<script type="text/javascript">window.opener.' . $_REQUEST['callbackFunction'] . '("' . $_REQUEST['application'] . '"); window.close();</script>';
155             }else if(!empty($_REQUEST['refreshParentWindow'])){
156                 $js = '<script type="text/javascript">window.opener.location.reload();window.close();</script>';
157             }else{
158                 $js = '<script type="text/javascript">window.close();</script>';
159             }
160             echo($js);
161             return;
162         }            
163         
164         // redirect to detail view, as in save
165         return parent::post_save();
166     }
167
168     protected function pre_QuickSave(){
169         if(!empty($_REQUEST['application'])){
170             $eapmBean = EAPM::getLoginInfo($_REQUEST['application'],true);
171             if (!$eapmBean) {
172                 $this->bean->application = $_REQUEST['application'];
173                 $this->bean->assigned_user_id = $GLOBALS['current_user']->id;
174             }else{
175                 $this->bean = $eapmBean;
176             }
177             $this->pre_save();
178                     
179         }else{
180             sugar_die("Please pass an application name.");
181         }
182     }
183     
184         public function action_QuickSave(){
185         $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
186         $this->action_save();
187
188         if ( $this->api->authMethod == 'oauth' ) {
189             $this->action_oauth();
190         }
191         }
192
193     protected function post_QuickSave(){
194         $this->post_save();
195     }
196
197     protected function pre_Reauthenticate(){
198         $this->pre_save();
199     }
200
201     protected function action_Reauthenticate(){
202         if ( $this->api->authMethod == 'oauth' ) {
203             // OAuth beans have to be handled specially.
204             
205             $reply = $this->api->checkLogin();
206             if ( !$reply['success'] ) {
207                 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
208             } else {
209                 $this->bean->validated();
210             }
211         } else {
212             // Normal auth methods go through this.
213             $this->action_save();
214         }
215     }
216
217     protected function post_Reauthenticate(){
218         $this->post_save();
219     }
220
221     protected function action_FlushFileCache()
222     {
223         $api = ExternalAPIFactory::loadAPI($_REQUEST['api']);
224         if ( $api == false ) {
225             echo 'FAILED';
226             return;
227         }
228
229         if ( method_exists($api,'loadDocCache') ) {
230             $api->loadDocCache(true);
231         }
232
233         echo 'SUCCESS';
234     }
235
236     protected function remapAction() {
237         if ( $this->do_action == 'DetailView' ) {
238             $this->do_action = 'EditView';
239             $this->action = 'EditView';
240         }
241         
242         parent::remapAction();
243     }
244
245 }