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