]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Trackers/TrackerManager.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Trackers / TrackerManager.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 require_once('modules/Trackers/monitor/Monitor.php');
39
40
41
42 class TrackerManager {
43
44 private static $instance;
45 private static $monitor_id;
46 private $metadata = array();
47 private $monitors = array();
48 private $disabledMonitors = array();
49 private static $paused = false;
50
51 /**
52  * Constructor for TrackerManager.  Declared private for singleton pattern.
53  *
54  */
55 private function TrackerManager() {
56         require('modules/Trackers/config.php');
57         $this->metadata = $tracker_config;
58     self::$monitor_id = create_guid();
59 }
60
61 /**
62  * setup
63  * This is a private method used to load the configuration settings whereby
64  * monitors may be disabled via the Admin settings interface
65  *
66  */
67 private function setup() {
68         if(!empty($this->metadata) && empty($GLOBALS['installing'])) {
69         
70                 $admin = new Administration();
71                 $admin->retrieveSettings('tracker');
72                 foreach($this->metadata as $key=>$entry) {
73                    if(isset($entry['bean'])) {
74                           if(!empty($admin->settings['tracker_'. $entry['name']])) {
75                                  $this->disabledMonitors[$entry['name']] = true;
76                           }
77                    }
78                 }
79         }       
80 }
81
82 public function setMonitorId($id) {
83     self::$monitor_id = $id;
84     foreach($this->monitors as $monitor) {
85        $monitor->monitor_id = self::$monitor_id;        
86     }   
87 }
88
89 /**
90  * getMonitorId
91  * Returns the monitor id associated with this TrackerManager instance
92  * @returns String id value
93  */
94 public function getMonitorId() {
95     return self::$monitor_id;
96 }
97
98 /**
99  * getInstance
100  * Singleton method to return static instance of TrackerManager
101  * @returns static TrackerManager instance 
102  */
103 static function getInstance(){  
104     if (!isset(self::$instance)) {
105         self::$instance = new TrackerManager();
106                 //Set global variable for tracker monitor instances that are disabled
107         self::$instance->setup();  
108     } // if
109     return self::$instance;
110 }
111
112 /**
113  * getMonitor
114  * This method returns a Monitor instance based on the monitor name.
115  * @param $name The String value of the monitor's name to retrieve
116  * @return Monitor instance corresponding to name or a BlankMonitor instance if one could not be found
117  */
118 public function getMonitor($name) {
119         //don't waste our time on disabled monitors
120         if($name!='tracker_sessions' && !empty($this->disabledMonitors[$name]))return false;
121         if(isset($this->monitors[$name])) {
122            return $this->monitors[$name];       
123         }
124         
125         if(isset($this->metadata) && isset($this->metadata[$name])) {
126        
127           
128        try {
129                $instance = $this->_getMonitor($this->metadata[$name]['name'], //name
130                                                            self::$monitor_id, //monitor_id
131                                        $this->metadata[$name]['metadata'],
132                                        $this->metadata[$name]['store'] //store 
133                                        );
134                $this->monitors[$name] = $instance;
135                return $this->monitors[$name];
136        } catch (Exception $ex) {
137            $GLOBALS['log']->error($ex->getMessage());
138            $GLOBALS['log']->error($ex->getTraceAsString());
139            require_once('modules/Trackers/monitor/BlankMonitor.php');
140            $this->monitors[$name] = new BlankMonitor();
141            return $this->monitors[$name];
142        }
143
144     } else {
145        $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_MONITOR_NOT_CONFIGURED'] . "($name)");
146        require_once('modules/Trackers/monitor/BlankMonitor.php');
147        $this->monitors[$name] = new BlankMonitor();
148        return $this->monitors[$name];    
149     }
150 }
151
152 private function _getMonitor($name='', $monitorId='', $metadata='', $store=''){
153         $class = strtolower($name.'_monitor');
154         $monitor = null;
155         if(file_exists('custom/modules/Trackers/monitor/'.$class.'.php')){              
156                 require_once('custom/modules/Trackers/monitor/'.$class.'.php');
157                 if(class_exists($class)){                               
158                         $monitor = new $class($name, $monitorId, $metadata, $store);
159                 }
160         }elseif(file_exists('modules/Trackers/monitor/'.$class.'.php')){                
161                 require_once('modules/Trackers/monitor/'.$class.'.php');
162                 if(class_exists($class)){
163                         $monitor = new $class($name, $monitorId, $metadata, $store);
164                 }
165         }else{
166                 $monitor = new Monitor($name, $monitorId, $metadata, $store);
167         }
168         
169         
170         $monitor->setEnabled(empty($this->disabledMonitors[$monitor->name]));
171         return $monitor;
172 }
173
174 /**
175  * save
176  * This method handles saving the monitors and their metrics to the mapped Store implementations
177  */
178 public function save() {
179
180     // Session tracker always saves.
181     if ( isset($this->monitors['tracker_sessions']) ) {
182         $this->monitors['tracker_sessions']->save();
183         unset($this->monitors['tracker_sessions']);
184     }
185
186     if(!$this->isPaused()){     
187                 foreach($this->monitors as $monitor) {
188                         if(array_key_exists('Trackable', class_implements($monitor))) {
189                            $monitor->save();
190                     }
191         }
192     }
193 }
194
195 /**
196  * saveMonitor
197  * Saves the monitor instance and then clears it
198  * If ignoreDisabled is set the ignore the fact of this monitor being disabled
199  */
200 public function saveMonitor($monitor, $flush=true, $ignoreDisabled = false) {
201         
202         if(!$this->isPaused() && !empty($monitor)){
203                 
204                 if((empty($this->disabledMonitors[$monitor->name]) || $ignoreDisabled) && array_key_exists('Trackable', class_implements($monitor))) {  
205                            
206                    $monitor->save($flush);
207                    
208                    if($flush) {
209                            $monitor->clear();
210                            unset($this->monitors[strtolower($monitor->name)]);
211                    }
212             }
213         }
214 }
215
216 /**
217  * unsetMonitor
218  * Method to unset the monitor so that it will not be saved
219  */
220 public function unsetMonitor($monitor) {
221    if(!empty($monitor)) {
222       unset($this->monitors[strtolower($monitor->name)]);
223    }
224 }
225
226 /**
227  * pause
228  * This function is to be called by a client in order to pause tracking through the lifetime of a Request.
229  * Tracking can be started again by calling unPauseTracking
230  *
231  * Usage: TrackerManager::getInstance()->pauseTracking();
232  */
233 public function pause(){
234         self::$paused = true;
235 }
236
237 /**
238  * unPause
239  * This function is to be called by a client in order to unPause tracking through the lifetime of a Request.
240  * Tracking can be paused by calling pauseTracking
241  *
242  *  * Usage: TrackerManager::getInstance()->unPauseTracking();
243  */
244 public function unPause(){
245         self::$paused = false;
246 }
247
248
249 /**
250  * isPaused
251  * This function returns the current value of the private paused variable.
252  * The result indicates whether or not the TrackerManager is paused.
253  * 
254  * @return boolean value indicating whether or not TrackerManager instance is paused.
255  */
256 public function isPaused() {
257    return self::$paused;
258 }
259
260 /**
261  * getDisabledMonitors
262  * Returns an Array of Monitor's name(s) that hhave been set to disabled in the
263  * Administration section.
264  * 
265  * @return Array of disabled Monitor's name(s) that hhave been set to disabled in the
266  * Administration section.
267  */
268 public function getDisabledMonitors() {
269         return $this->disabledMonitors;
270 }
271
272 /**
273  * Set the disabled monitors
274  *
275  * @param array $disabledMonitors
276  */
277 public function setDisabledMonitors($disabledMonitors) {
278         $this->disabledMonitors = $disabledMonitors;
279 }
280
281 /**
282  * unsetMonitors
283  * Function to unset all Monitors loaded for a TrackerManager instance
284  * 
285  */
286 public function unsetMonitors() {
287         $mons = $this->monitors;
288         foreach($mons as $key=>$m) {
289                 $this->unsetMonitor($m);
290         }
291 }
292
293 }
294 ?>