2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
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');
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;
53 var $modified_user_id;
54 var $modified_by_name;
60 var $modified_user_link;
61 var $assigned_user_id;
62 var $assigned_user_name;
63 var $assigned_user_link;
66 var $validated = false;
72 var $disable_row_level_security = true;
74 function bean_implements($interface){
76 case 'ACL': return true;
81 static function getLoginInfo($application, $includeInactive = false)
85 $eapmBean = new EAPM();
87 if ( isset($_SESSION['EAPM'][$application]) && !$includeInactive ) {
88 if ( is_array($_SESSION['EAPM'][$application]) ) {
89 $eapmBean->fromArray($_SESSION['EAPM'][$application]);
94 $queryArray = array('assigned_user_id'=>$current_user->id, 'application'=>$application, 'deleted'=>0 );
95 if ( !$includeInactive ) {
96 $queryArray['validated'] = 1;
98 $eapmBean = $eapmBean->retrieve_by_string_fields($queryArray);
100 // Don't cache the include inactive results
101 if ( !$includeInactive ) {
102 if ( $eapmBean != null ) {
103 $_SESSION['EAPM'][$application] = $eapmBean->toArray();
105 $_SESSION['EAPM'][$application] = '';
111 if(isset($eapmBean->password)){
112 require_once("include/utils/encryption_utils.php");
113 $eapmBean->password = blowfishDecode(blowfishGetKey('encrypt_field'),$eapmBean->password);;
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;
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);
127 $where = $owner_where;
129 $where .= ' AND '. $owner_where;
133 return parent::create_new_list_query($order_by, $where, $filter, $params, $show_deleted,$join_type, $return_array, $parentbean, $singleSelect);
136 function save($check_notify = FALSE ) {
138 if ( !is_admin($GLOBALS['current_user']) ) {
139 $this->assigned_user_id = $GLOBALS['current_user']->id;
141 $parentRet = parent::save($check_notify);
143 // Nuke the EAPM cache for this record
144 if ( isset($_SESSION['EAPM'][$this->application]) ) {
145 unset($_SESSION['EAPM'][$this->application]);
151 function mark_deleted($id)
153 // Nuke the EAPM cache for this record
154 if ( isset($_SESSION['EAPM'][$this->application]) ) {
155 unset($_SESSION['EAPM'][$this->application]);
158 return parent::mark_deleted($id);
163 if(empty($this->id)) {
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);
175 // Nuke the EAPM cache for this record
176 if ( isset($_SESSION['EAPM'][$this->application]) ) {
177 unset($_SESSION['EAPM'][$this->application]);
182 protected function fillInName()
184 if ( !empty($this->application) ) {
185 $apiList = ExternalAPIFactory::loadFullAPIList(false, true);
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);
192 public function fill_in_additional_detail_fields()
195 parent::fill_in_additional_detail_fields();
198 public function fill_in_additional_list_fields()
201 parent::fill_in_additional_list_fields();
204 public function save_cleanup()
206 $this->oauth_token = "";
207 $this->oauth_secret = "";
208 $this->api_data = "";
212 * Given a user remove their associated accounts. This is called when a user is deleted from the system.
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);
222 // External API integration, for the dropdown list of what external API's are available
223 function getEAPMExternalApiDropDown() {
224 $apiList = ExternalAPIFactory::getModuleDropDown('',true, true);