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