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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once('modules/Trackers/monitor/Monitor.php');
42 class TrackerManager {
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;
52 * Constructor for TrackerManager. Declared private for singleton pattern.
55 private function TrackerManager() {
56 require('modules/Trackers/config.php');
57 $this->metadata = $tracker_config;
58 self::$monitor_id = create_guid();
63 * This is a private method used to load the configuration settings whereby
64 * monitors may be disabled via the Admin settings interface
67 private function setup() {
68 if(!empty($this->metadata) && empty($GLOBALS['installing'])) {
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;
82 public function setMonitorId($id) {
83 self::$monitor_id = $id;
84 foreach($this->monitors as $monitor) {
85 $monitor->monitor_id = self::$monitor_id;
91 * Returns the monitor id associated with this TrackerManager instance
92 * @returns String id value
94 public function getMonitorId() {
95 return self::$monitor_id;
100 * Singleton method to return static instance of TrackerManager
101 * @returns static TrackerManager instance
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();
109 return self::$instance;
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
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];
125 if(isset($this->metadata) && isset($this->metadata[$name])) {
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
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];
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];
152 private function _getMonitor($name='', $monitorId='', $metadata='', $store=''){
153 $class = strtolower($name.'_monitor');
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);
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);
166 $monitor = new Monitor($name, $monitorId, $metadata, $store);
170 $monitor->setEnabled(empty($this->disabledMonitors[$monitor->name]));
176 * This method handles saving the monitors and their metrics to the mapped Store implementations
178 public function save() {
180 // Session tracker always saves.
181 if ( isset($this->monitors['tracker_sessions']) ) {
182 $this->monitors['tracker_sessions']->save();
183 unset($this->monitors['tracker_sessions']);
186 if(!$this->isPaused()){
187 foreach($this->monitors as $monitor) {
188 if(array_key_exists('Trackable', class_implements($monitor))) {
197 * Saves the monitor instance and then clears it
198 * If ignoreDisabled is set the ignore the fact of this monitor being disabled
200 public function saveMonitor($monitor, $flush=true, $ignoreDisabled = false) {
202 if(!$this->isPaused() && !empty($monitor)){
204 if((empty($this->disabledMonitors[$monitor->name]) || $ignoreDisabled) && array_key_exists('Trackable', class_implements($monitor))) {
206 $monitor->save($flush);
210 unset($this->monitors[strtolower($monitor->name)]);
218 * Method to unset the monitor so that it will not be saved
220 public function unsetMonitor($monitor) {
221 if(!empty($monitor)) {
222 unset($this->monitors[strtolower($monitor->name)]);
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
231 * Usage: TrackerManager::getInstance()->pauseTracking();
233 public function pause(){
234 self::$paused = true;
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
242 * * Usage: TrackerManager::getInstance()->unPauseTracking();
244 public function unPause(){
245 self::$paused = false;
251 * This function returns the current value of the private paused variable.
252 * The result indicates whether or not the TrackerManager is paused.
254 * @return boolean value indicating whether or not TrackerManager instance is paused.
256 public function isPaused() {
257 return self::$paused;
261 * getDisabledMonitors
262 * Returns an Array of Monitor's name(s) that hhave been set to disabled in the
263 * Administration section.
265 * @return Array of disabled Monitor's name(s) that hhave been set to disabled in the
266 * Administration section.
268 public function getDisabledMonitors() {
269 return $this->disabledMonitors;
273 * Set the disabled monitors
275 * @param array $disabledMonitors
277 public function setDisabledMonitors($disabledMonitors) {
278 $this->disabledMonitors = $disabledMonitors;
283 * Function to unset all Monitors loaded for a TrackerManager instance
286 public function unsetMonitors() {
287 $mons = $this->monitors;
288 foreach($mons as $key=>$m) {
289 $this->unsetMonitor($m);