]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/EAPM/controller.php
Release 6.2.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-2011 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         return $this->set_redirect($url);
63     }
64
65     public function pre_save()
66     {
67         parent::pre_save();
68         $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
69         if(empty($this->api)) {
70             return $this->failed(translate('LBL_AUTH_UNSUPPORTED', $this->bean->module_dir));
71         }
72         if(empty($this->bean->id)){
73             $eapmBean = EAPM::getLoginInfo($this->bean->application,true);
74             if($eapmBean){
75                 SugarApplication::appendErrorMessage(translate('LBL_APPLICATION_FOUND_NOTICE', $this->bean->module_dir));
76                 $this->bean->id = $eapmBean->id;
77             }
78         }
79         $this->bean->validated = false;
80         $this->bean->save_cleanup();
81         $this->api->loadEAPM($this->bean);
82     }
83
84     protected function post_save()
85     {
86         if(!$this->bean->deleted) {
87             // do not load bean here since password is already encoded
88             if ( $this->api->authMethod != 'oauth' ) {
89                 // OAuth beans have to be handled specially.
90                 
91                 $reply = $this->api->checkLogin();
92                 if ( !$reply['success'] ) {
93                     return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
94                 } else {
95                     $this->bean->validated();
96                 }
97             }
98         }
99         if($this->return_module == 'Users'){
100             $this->return_action = 'EditView';
101         }
102         parent::post_save();
103         // Override the redirect location to add the hash
104         $this->redirect_url = $this->redirect_url.'#tab5';
105         if ( $this->api->authMethod == 'oauth' && !$this->bean->deleted ) {
106             // It's OAuth, we have to handle this specially.
107             // We need to create a new window to handle the OAuth, and redirect this window back to the edit view
108             // So we will handle that in javascript.
109             $popup_warning_msg = string_format($GLOBALS['mod_strings']['LBL_ERR_POPUPS_DISABLED'], array($_SERVER['HTTP_HOST']) );
110             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>');
111
112             // To prevent the normal handler from issuing a header call and destroying our neat little javascript we'll
113             // end right here.
114             sugar_die('');
115         } else {
116             return;
117         }
118     }
119
120     protected function action_oauth()
121     {
122         if(empty($this->bean->id)) {
123             return $this->set_redirect('index.php');
124         }
125                 if(!$this->bean->ACLAccess('save')){
126                         ACLController::displayNoAccess(true);
127                         sugar_cleanup(true);
128                         return true;
129                 }
130         if(empty($_REQUEST['oauth_error'])) {
131             $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
132             $reply = $this->api->checkLogin($this->bean);
133             if ( !$reply['success'] ) {
134                 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
135             } else {
136                 $this->bean->validated();
137             }
138         }
139         
140         // This is a tweak so that we can automatically close windows if requested by the external account system
141         if ( isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
142             if(!empty($_REQUEST['callbackFunction']) && !empty($_REQUEST['application'])){
143                 $js = '<script type="text/javascript">window.opener.' . $_REQUEST['callbackFunction'] . '("' . $_REQUEST['application'] . '"); window.close();</script>';
144             }else if(!empty($_REQUEST['refreshParentWindow'])){
145                 $js = '<script type="text/javascript">window.opener.location.reload();window.close();</script>';
146             }else{
147                 $js = '<script type="text/javascript">window.close();</script>';
148             }
149             echo($js);
150             return;
151         }            
152         
153         // redirect to detail view, as in save
154         return parent::post_save();
155     }
156
157     protected function pre_QuickSave(){
158         if(!empty($_REQUEST['application'])){
159             $eapmBean = EAPM::getLoginInfo($_REQUEST['application'],true);
160             if (!$eapmBean) {
161                 $this->bean->application = $_REQUEST['application'];
162                 $this->bean->assigned_user_id = $GLOBALS['current_user']->id;
163             }else{
164                 $this->bean = $eapmBean;
165             }
166             $this->pre_save();
167                     
168         }else{
169             sugar_die("Please pass an application name.");
170         }
171     }
172     
173         public function action_QuickSave(){
174         $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
175         $this->action_save();
176
177         if ( $this->api->authMethod == 'oauth' ) {
178             $this->action_oauth();
179         }
180         }
181
182     protected function post_QuickSave(){
183         $this->post_save();
184     }
185
186     protected function pre_Reauthenticate(){
187         $this->pre_save();
188     }
189
190     protected function action_Reauthenticate(){
191         if ( $this->api->authMethod == 'oauth' ) {
192             // OAuth beans have to be handled specially.
193             
194             $reply = $this->api->checkLogin();
195             if ( !$reply['success'] ) {
196                 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
197             } else {
198                 $this->bean->validated();
199             }
200         } else {
201             // Normal auth methods go through this.
202             $this->action_save();
203         }
204     }
205
206     protected function post_Reauthenticate(){
207         $this->post_save();
208     }
209
210     protected function action_FlushFileCache()
211     {
212         $api = ExternalAPIFactory::loadAPI($_REQUEST['api']);
213         if ( $api == false ) {
214             echo 'FAILED';
215             return;
216         }
217
218         if ( method_exists($api,'loadDocCache') ) {
219             $api->loadDocCache(true);
220         }
221
222         echo 'SUCCESS';
223     }
224
225     protected function remapAction() {
226         if ( $this->do_action == 'DetailView' ) {
227             $this->do_action = 'EditView';
228             $this->action = 'EditView';
229         }
230         
231         parent::remapAction();
232     }
233
234 }