]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/resource/ResourceManager.php
Release 6.5.0
[Github/sugarcrm.git] / include / resource / ResourceManager.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
38 /**
39  * ResourceManager.php
40  * This class is responsible for resource management of SQL queries, file usage, etc.
41  */
42 class ResourceManager {
43
44 private static $instance;
45 private $_observers = array();
46
47 /**
48  * The constructor; declared as private
49  */
50 private function ResourceManager() {
51         
52 }
53
54 /**
55  * getInstance
56  * Singleton method to return static instance of ResourceManager
57  * @return The static singleton ResourceManager instance
58  */
59 static public function getInstance(){   
60     if (!isset(self::$instance)) {
61         self::$instance = new ResourceManager();
62     } // if
63     return self::$instance;
64 }
65
66 /**
67  * setup
68  * Handles determining the appropriate setup based on client type.
69  * It will create a SoapResourceObserver instance if the $module parameter is set to 
70  * 'Soap'; otherwise, it will try to create a WebResourceObserver instance.
71  * @param $module The module value used to create the corresponding observer
72  * @return boolean value indicating whether or not an observer was successfully setup
73  */
74 public function setup($module) {
75         //Check if config.php exists
76         if(!file_exists('config.php') || empty($module)) {
77            return false;        
78         }
79     
80     if($module == 'Soap') {
81       require_once('include/resource/Observers/SoapResourceObserver.php');
82       $observer = new SoapResourceObserver('Soap');             
83     } else {
84       require_once('include/resource/Observers/WebResourceObserver.php');
85       $observer = new WebResourceObserver($module);     
86     }
87     
88         //Load config
89         if(!empty($observer->module)) {
90                 $limit = 0;
91                 
92                 if(isset($GLOBALS['sugar_config']['resource_management'])) {
93                            $res = $GLOBALS['sugar_config']['resource_management'];
94                         if(!empty($res['special_query_modules']) && 
95                            in_array($observer->module, $res['special_query_modules']) &&
96                            !empty($res['special_query_limit']) &&
97                            is_int($res['special_query_limit']) &&
98                            $res['special_query_limit'] > 0) {
99                            $limit = $res['special_query_limit'];
100                         } else if(!empty($res['default_limit']) && is_int($res['default_limit']) && $res['default_limit'] > 0) {
101                            $limit = $res['default_limit'];
102                         }
103                 } //if
104                 
105                 if($limit) {
106                    
107                    $db = DBManagerFactory::getInstance();                       
108                    $db->setQueryLimit($limit);
109                    $observer->setLimit($limit);
110                    $this->_observers[] = $observer;
111                 }
112                 return true;
113         } 
114         
115         return false;
116 }
117
118 /**
119  * notifyObservers
120  * This method notifies the registered observers with the provided message.
121  * @param $msg Message from language file to notify observers with
122  */
123 public function notifyObservers($msg) {
124         
125         if(empty($this->_observers)) {
126            return;      
127         }
128
129     //Notify observers limit has been reached
130     if(empty($GLOBALS['app_strings'])) {
131        $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);     
132     }
133     $limitMsg = $GLOBALS['app_strings'][$msg];
134     foreach( $this->_observers as $observer) {
135             $limit = $observer->limit;
136             $module = $observer->module;
137             eval("\$limitMsg = \"$limitMsg\";");
138             $GLOBALS['log']->fatal($limitMsg);
139             $observer->notify($limitMsg);
140     }           
141 }
142
143
144 /*
145  * getObservers
146  * Returns the observer instances that have been setup for the ResourceManager instance
147  * @return Array of ResourceObserver(s)
148  */
149 function getObservers() {
150     return $this->_observers;
151 }
152         
153 }
154 ?>