]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Relationships/Relationship.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Relationships / Relationship.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 /*********************************************************************************
39
40  * Description:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46
47 class Relationship extends SugarBean {
48
49         var $object_name='Relationship';
50         var $module_dir = 'Relationships';
51         var $new_schema = true;
52         var $table_name = 'relationships';
53
54         var $id;
55         var $relationship_name;
56         var $lhs_module;
57         var $lhs_table;
58         var $lhs_key;
59         var $rhs_module;
60         var $rhs_table;
61         var $rhs_key;
62         var $join_table;
63         var $join_key_lhs;
64         var $join_key_rhs;
65         var $relationship_type;
66         var $relationship_role_column;
67         var $relationship_role_column_value;
68         var $reverse;
69
70         var $_self_referencing;
71
72         function Relationship() {
73                 parent::SugarBean();
74         }
75
76         /*returns true if the relationship is self referencing. equality check is performed for both table and
77          * key names.
78          */
79         function is_self_referencing() {
80                 if (empty($this->_self_referencing)) {
81                         $this->_self_referencing=false;
82
83                         //is it self referencing, both table and key name from lhs and rhs should  be equal.
84                         if ($this->lhs_table == $this->rhs_table && $this->lhs_key == $this->rhs_key) {
85                                 $this->_self_referencing=true;
86                         }
87                 }
88                 return $this->_self_referencing;
89         }
90
91         /*returns true if a relationship with provided name exists*/
92         function exists($relationship_name,&$db) {
93                 $query = "SELECT relationship_name FROM relationships WHERE deleted=0 AND relationship_name = '".$relationship_name."'";
94                 $result = $db->query($query,true," Error searching relationships table..");
95                 $row  =  $db->fetchByAssoc($result);
96                 if ($row != null) {
97                         return true;
98                 }
99
100                 return false;
101         }
102
103         function delete($relationship_name,&$db) {
104
105                 $query = "UPDATE relationships SET deleted=1 WHERE deleted=0 AND relationship_name = '".$relationship_name."'";
106                 $result = $db->query($query,true," Error updating relationships table for ".$relationship_name);
107
108         }
109
110
111         function get_other_module($relationship_name, $base_module, &$db){
112         //give it the relationship_name and base module
113         //it will return the module name on the other side of the relationship
114
115                 $query = "SELECT relationship_name, rhs_module, lhs_module FROM relationships WHERE deleted=0 AND relationship_name = '".$relationship_name."'";
116                 $result = $db->query($query,true," Error searching relationships table..");
117                 $row  =  $db->fetchByAssoc($result);
118                 if ($row != null) {
119
120                         if($row['rhs_module']==$base_module){
121                                 return $row['lhs_module'];
122                         }
123                         if($row['lhs_module']==$base_module){
124                                 return $row['rhs_module'];
125                         }
126                 }
127
128                 return false;
129
130
131         //end function get_other_module
132         }
133
134         function retrieve_by_sides($lhs_module, $rhs_module, &$db){
135         //give it the relationship_name and base module
136         //it will return the module name on the other side of the relationship
137
138                 $query = "SELECT * FROM relationships WHERE deleted=0 AND lhs_module = '".$lhs_module."' AND rhs_module = '".$rhs_module."'";
139                 $result = $db->query($query,true," Error searching relationships table..");
140                 $row  =  $db->fetchByAssoc($result);
141                 if ($row != null) {
142
143                         return $row;
144
145                 }
146
147                 return null;
148
149
150         //end function retrieve_by_sides
151         }
152
153         function retrieve_by_modules($lhs_module, $rhs_module, &$db, $type =''){
154         //give it the relationship_name and base module
155         //it will return the module name on the other side of the relationship
156
157                 $query = "      SELECT * FROM relationships
158                                         WHERE deleted=0
159                                         AND (
160                                         (lhs_module = '".$lhs_module."' AND rhs_module = '".$rhs_module."')
161                                         OR
162                                         (lhs_module = '".$rhs_module."' AND rhs_module = '".$lhs_module."')
163                                         )
164                                         ";
165                 if(!empty($type)){
166                         $query .= " AND relationship_type='$type'";
167                 }
168                 $result = $db->query($query,true," Error searching relationships table..");
169                 $row  =  $db->fetchByAssoc($result);
170                 if ($row != null) {
171
172                         return $row['relationship_name'];
173
174                 }
175
176                 return null;
177
178
179         //end function retrieve_by_sides
180         }
181
182
183         function retrieve_by_name($relationship_name) {
184
185                 if (empty($GLOBALS['relationships'])) {
186                         $this->load_relationship_meta();
187                 }
188
189 //              _ppd($GLOBALS['relationships']);
190
191                 if (array_key_exists($relationship_name, $GLOBALS['relationships'])) {
192
193                         foreach($GLOBALS['relationships'][$relationship_name] as $field=>$value)
194                         {
195                                         $this->$field = $value;
196                         }
197                 }
198                 else {
199                         $GLOBALS['log']->fatal('Error fetching relationship from cache '.$relationship_name);
200                         return false;
201                 }
202         }
203
204         function load_relationship_meta() {
205                 if (!file_exists(Relationship::cache_file_dir().'/'.Relationship::cache_file_name_only())) {
206                         $this->build_relationship_cache();
207                 }
208                 include(Relationship::cache_file_dir().'/'.Relationship::cache_file_name_only());
209                 $GLOBALS['relationships']=$relationships;
210         }
211
212         function build_relationship_cache() {
213                 $query="SELECT * from relationships where deleted=0";
214                 $result=$this->db->query($query);
215
216                 while (($row=$this->db->fetchByAssoc($result))!=null) {
217                         $relationships[$row['relationship_name']] = $row;
218                 }
219
220                 $rel_string='<?php ';
221                 $rel_string.='$relationships='.var_export($relationships,true);
222                 $rel_string.=' ?>';
223                 mkdir_recursive($this->cache_file_dir());
224                 $handle=sugar_fopen(Relationship::cache_file_dir().'/'.Relationship::cache_file_name_only(),'w');
225                 fwrite($handle,$rel_string);
226                 fclose($handle);
227         require_once("data/Relationships/RelationshipFactory.php");
228         SugarRelationshipFactory::deleteCache();
229         }
230
231
232         public static function cache_file_dir() {
233                 return sugar_cached("modules/Relationships");
234         }
235         public static function cache_file_name_only() {
236                 return 'relationships.cache.php';
237         }
238         
239         public static function delete_cache() {
240                 $filename=Relationship::cache_file_dir().'/'.Relationship::cache_file_name_only();
241                 if (file_exists($filename)) {
242                         unlink($filename);
243                 }
244         require_once("data/Relationships/RelationshipFactory.php");
245         SugarRelationshipFactory::deleteCache();
246         }
247
248
249         function trace_relationship_module($base_module, $rel_module1_name, $rel_module2_name=""){
250                 global $beanList;
251                 global $dictionary;
252
253                 $temp_module = get_module_info($base_module);
254
255                 $rel_attribute1_name = $temp_module->field_defs[strtolower($rel_module1_name)]['relationship'];
256                 $rel_module1 = $this->get_other_module($rel_attribute1_name, $base_module, $temp_module->db);
257                 $rel_module1_bean = get_module_info($rel_module1);
258
259                 if($rel_module2_name!=""){
260                         if($rel_module2_name == 'ProjectTask'){
261                                 $rel_module2_name = strtolower($rel_module2_name);
262                         }
263                         $rel_attribute2_name = $rel_module1_bean->field_defs[strtolower($rel_module2_name)]['relationship'];
264                         $rel_module2 = $this->get_other_module($rel_attribute2_name, $rel_module1_bean->module_dir, $rel_module1_bean->db);
265                         $rel_module2_bean = get_module_info($rel_module2);
266                         return $rel_module2_bean;
267
268                 } else {
269                         //no rel_module2, so return rel_module2 bean
270                         return $rel_module1_bean;
271                 }
272
273         //end function trace_relationship_module
274         }
275
276
277
278
279 }
280 ?>