]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - data/Relationships/RelationshipFactory.php
Release 6.3.0
[Github/sugarcrm.git] / data / Relationships / RelationshipFactory.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-2011 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
39
40 require_once("data/Relationships/SugarRelationship.php");
41
42 class SugarRelationshipFactory {
43     static $rfInstance;
44
45     protected $relationships;
46
47     protected function __construct(){
48         //Load the relationship definitions from the cache.
49         $this->loadRelationships();
50     }
51
52     /**
53      * @static
54      * @return SugarRelationshipFactory
55      */
56     public static function getInstance()
57     {
58         if (is_null(self::$rfInstance))
59             self::$rfInstance = new SugarRelationshipFactory();
60         return self::$rfInstance;
61     }
62
63     public static function rebuildCache()
64     {
65         self::getInstance()->buildRelationshipCache();
66     }
67
68     public static function deleteCache()
69     {
70         $file = self::getInstance()->getCacheFile();
71         if(sugar_is_file($file))
72         {
73             unlink($file);
74         }
75     }
76
77     /**
78      * @param  $relationshipName String name of relationship to load
79      * @return void
80      *
81      *
82      *
83      */
84     public function getRelationship($relationshipName)
85     {
86         if (empty($this->relationships[$relationshipName])) {
87             $GLOBALS['log']->error("Unable to find relationship $relationshipName");
88             return false;
89         }
90
91         $def = $this->relationships[$relationshipName];
92
93         $type = isset($def['true_relationship_type']) ? $def['true_relationship_type'] : $def['relationship_type'];
94         switch($type)
95         {
96             case "many-to-many":
97                 if (isset($def['rhs_module']) && $def['rhs_module'] == 'EmailAddresses')
98                 {
99                     require_once("data/Relationships/EmailAddressRelationship.php");
100                     return new EmailAddressRelationship($def);
101                 }
102                 require_once("data/Relationships/M2MRelationship.php");
103                 return new M2MRelationship($def);
104             break;
105             case "one-to-many":
106                 require_once("data/Relationships/One2MBeanRelationship.php");
107                 //If a relationship has no table or join keys, it must be bean based
108                 if (empty($def['true_relationship_type']) || (empty($def['table']) && empty($def['join_table'])) || empty($def['join_key_rhs'])){
109                     return new One2MBeanRelationship($def);
110                 }
111                 else {
112                     return new One2MRelationship($def);
113                 }
114                 break;
115             case "one-to-one":
116                 if (empty($def['true_relationship_type'])){
117                     require_once("data/Relationships/One2OneBeanRelationship.php");
118                     return new One2OneBeanRelationship($def);
119                 }
120                 else {
121                     require_once("data/Relationships/One2OneRelationship.php");
122                     return new One2OneRelationship($def);
123                 }
124                 break;
125         }
126
127         $GLOBALS['log']->fatal ("$relationshipName had an unknown type $type ");
128
129         return false;
130     }
131
132     public function getRelationshipDef($relationshipName)
133     {
134         if (empty($this->relationships[$relationshipName])) {
135             $GLOBALS['log']->error("Unable to find relationship $relationshipName");
136             return false;
137         }
138
139         return $this->relationships[$relationshipName];
140     }
141
142
143     protected function loadRelationships()
144     {
145         if(sugar_is_file($this->getCacheFile()))
146         {
147             include($this->getCacheFile());
148             $this->relationships = $relationships;
149         } else {
150             $this->buildRelationshipCache();
151         }
152     }
153
154     protected function buildRelationshipCache()
155     {
156         global $beanList, $dictionary, $buildingRelCache;
157         if ($buildingRelCache)
158             return;
159         $buildingRelCache = true;
160         include_once("modules/TableDictionary.php");
161
162         if (empty($beanList))
163             include("include/modules.php");
164         //Reload ALL the module vardefs....
165         foreach($beanList as $moduleName => $beanName)
166         {
167             VardefManager::loadVardef($moduleName, BeanFactory::getObjectName($moduleName));
168         }
169
170         $relationships = array();
171
172         //Grab all the relationships from the dictionary.
173         foreach ($dictionary as $key => $def)
174         {
175             if (!empty($def['relationships']))
176             {
177                 foreach($def['relationships'] as $relKey => $relDef)
178                 {
179                     if ($key == $relKey) //Relationship only entry, we need to capture everything
180                         $relationships[$key] = array_merge(array('name' => $key), $def, $relDef);
181                     else {
182                         $relationships[$relKey] = array_merge(array('name' => $relKey), $relDef);
183                         if(!empty($relationships[$relKey]['join_table']) && empty($relationships[$relKey]['fields'])
184                             && isset($dictionary[$relationships[$relKey]['join_table']]['fields'])) {
185                             $relationships[$relKey]['fields'] = $dictionary[$relationships[$relKey]['join_table']]['fields'];
186                         }
187                     }
188                 }
189             }
190         }
191         //Save it out
192         sugar_mkdir(dirname($this->getCacheFile()), null, true);
193         $out="<?php \n \$relationships=" . var_export($relationships, true) .";";
194         sugar_file_put_contents($this->getCacheFile(), $out);
195
196         $this->relationships = $relationships;
197         $buildingRelCache = false;
198     }
199
200         protected function getCacheFile() {
201                 return "{$GLOBALS['sugar_config']['cache_dir']}Relationships/relationships.cache.php";
202         }
203
204
205
206 }