]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/EAPM/EAPM.php
Release 6.5.0
[Github/sugarcrm.git] / modules / EAPM / EAPM.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 require_once('data/SugarBean.php');
38 require_once('include/SugarObjects/templates/basic/Basic.php');
39 require_once('include/externalAPI/ExternalAPIFactory.php');
40 require_once('include/SugarOauth.php');
41
42 class EAPM extends Basic {
43         var $new_schema = true;
44         var $module_dir = 'EAPM';
45         var $object_name = 'EAPM';
46         var $table_name = 'eapm';
47         var $importable = false;
48                 var $id;
49                 var $type;
50                 var $name;
51                 var $date_entered;
52                 var $date_modified;
53                 var $modified_user_id;
54                 var $modified_by_name;
55                 var $created_by;
56                 var $created_by_name;
57                 var $description;
58                 var $deleted;
59                 var $created_by_link;
60                 var $modified_user_link;
61                 var $assigned_user_id;
62                 var $assigned_user_name;
63                 var $assigned_user_link;
64                 var $password;
65                 var $url;
66                 var $validated = false;
67                 var $oauth_token;
68                 var $oauth_secret;
69                 var $application;
70                 var $consumer_key;
71                 var $consumer_secret;
72                 var $disable_row_level_security = true;
73
74         function bean_implements($interface){
75                 switch($interface){
76                         case 'ACL': return true;
77                 }
78                 return false;
79 }
80
81    static function getLoginInfo($application, $includeInactive = false)
82    {
83        global $current_user;
84
85        $eapmBean = new EAPM();
86
87        if ( isset($_SESSION['EAPM'][$application]) && !$includeInactive ) {
88            if ( is_array($_SESSION['EAPM'][$application]) ) {
89                $eapmBean->fromArray($_SESSION['EAPM'][$application]);
90            } else {
91                return null;
92            }
93        } else {
94            $queryArray = array('assigned_user_id'=>$current_user->id, 'application'=>$application, 'deleted'=>0 );
95            if ( !$includeInactive ) {
96                $queryArray['validated'] = 1;
97            }
98            $eapmBean = $eapmBean->retrieve_by_string_fields($queryArray);
99            
100            // Don't cache the include inactive results
101            if ( !$includeInactive ) {
102                if ( $eapmBean != null ) {
103                    $_SESSION['EAPM'][$application] = $eapmBean->toArray();
104                } else {
105                    $_SESSION['EAPM'][$application] = '';
106                    return null;
107                }
108            }
109        }
110
111        if(isset($eapmBean->password)){
112            require_once("include/utils/encryption_utils.php");
113            $eapmBean->password = blowfishDecode(blowfishGetKey('encrypt_field'),$eapmBean->password);;
114        }
115
116        return $eapmBean;
117     }
118
119     function create_new_list_query($order_by, $where,$filter=array(),$params=array(), $show_deleted = 0,$join_type='', $return_array = false,$parentbean=null, $singleSelect = false) {
120         global $current_user;
121
122         if ( !is_admin($GLOBALS['current_user']) ) {
123             // Restrict this so only admins can see other people's records
124             $owner_where = $this->getOwnerWhere($current_user->id);
125             
126             if(empty($where)) {
127                 $where = $owner_where;
128             } else {
129                 $where .= ' AND '.  $owner_where;
130             }
131         }
132         
133         return parent::create_new_list_query($order_by, $where, $filter, $params, $show_deleted,$join_type, $return_array, $parentbean, $singleSelect);
134     }
135
136    function save($check_notify = FALSE ) {
137        $this->fillInName();
138        if ( !is_admin($GLOBALS['current_user']) ) {
139            $this->assigned_user_id = $GLOBALS['current_user']->id;
140        }
141        $parentRet = parent::save($check_notify);
142
143        // Nuke the EAPM cache for this record
144        if ( isset($_SESSION['EAPM'][$this->application]) ) {
145            unset($_SESSION['EAPM'][$this->application]);
146        }
147
148        return $parentRet;
149    }
150
151    function mark_deleted($id)
152    {
153        // Nuke the EAPM cache for this record
154        if ( isset($_SESSION['EAPM'][$this->application]) ) {
155            unset($_SESSION['EAPM'][$this->application]);
156        }
157        
158        return parent::mark_deleted($id);
159    }
160
161    function validated()
162    {
163        if(empty($this->id)) {
164            return false;
165        }
166         // Don't use save, it will attempt to revalidate
167        $adata = $GLOBALS['db']->quote($this->api_data);
168        $GLOBALS['db']->query("UPDATE eapm SET validated=1,api_data='$adata'  WHERE id = '{$this->id}' AND deleted = 0");
169        if(!$this->deleted && !empty($this->application)) {
170            // deactivate other EAPMs with same app
171            $sql = "UPDATE eapm SET deleted=1 WHERE application = '{$this->application}' AND id != '{$this->id}' AND deleted = 0 AND assigned_user_id = '{$this->assigned_user_id}'";
172            $GLOBALS['db']->query($sql,true);
173        }
174
175        // Nuke the EAPM cache for this record
176        if ( isset($_SESSION['EAPM'][$this->application]) ) {
177            unset($_SESSION['EAPM'][$this->application]);
178        }
179
180    }
181
182         protected function fillInName()
183         {
184         if ( !empty($this->application) ) {
185             $apiList = ExternalAPIFactory::loadFullAPIList(false, true);
186         }
187             if(!empty($apiList) && isset($apiList[$this->application]) && $apiList[$this->application]['authMethod'] == "oauth") {
188                 $this->name = sprintf(translate('LBL_OAUTH_NAME', $this->module_dir), $this->application);
189             }
190         }
191
192         public function fill_in_additional_detail_fields()
193         {
194             $this->fillInName();
195             parent::fill_in_additional_detail_fields();
196         }
197
198         public function fill_in_additional_list_fields()
199         {
200             $this->fillInName();
201             parent::fill_in_additional_list_fields();
202         }
203
204         public function save_cleanup()
205         {
206             $this->oauth_token = "";
207         $this->oauth_secret = "";
208         $this->api_data = "";
209         }
210
211     /**
212      * Given a user remove their associated accounts. This is called when a user is deleted from the system.
213      * @param  $user_id
214      * @return void
215      */
216     public function delete_user_accounts($user_id){
217         $sql = "DELETE FROM {$this->table_name} WHERE assigned_user_id = '{$user_id}'";
218         $GLOBALS['db']->query($sql,true);
219     }
220 }
221
222 // External API integration, for the dropdown list of what external API's are available
223 function getEAPMExternalApiDropDown() {
224     $apiList = ExternalAPIFactory::getModuleDropDown('',true, true);
225
226     return $apiList;
227
228 }