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.
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 /*********************************************************************************
40 * Description: TODO: To be written.
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
47 class Relationship extends SugarBean {
49 var $object_name='Relationship';
50 var $module_dir = 'Relationships';
51 var $new_schema = true;
52 var $table_name = 'relationships';
55 var $relationship_name;
65 var $relationship_type;
66 var $relationship_role_column;
67 var $relationship_role_column_value;
70 var $_self_referencing;
72 function Relationship() {
76 /*returns true if the relationship is self referencing. equality check is performed for both table and
79 function is_self_referencing() {
80 if (empty($this->_self_referencing)) {
81 $this->_self_referencing=false;
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;
88 return $this->_self_referencing;
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);
103 function delete($relationship_name,&$db) {
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);
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
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);
120 if($row['rhs_module']==$base_module){
121 return $row['lhs_module'];
123 if($row['lhs_module']==$base_module){
124 return $row['rhs_module'];
131 //end function get_other_module
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
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);
150 //end function retrieve_by_sides
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
157 $query = " SELECT * FROM relationships
160 (lhs_module = '".$lhs_module."' AND rhs_module = '".$rhs_module."')
162 (lhs_module = '".$rhs_module."' AND rhs_module = '".$lhs_module."')
166 $query .= " AND relationship_type='$type'";
168 $result = $db->query($query,true," Error searching relationships table..");
169 $row = $db->fetchByAssoc($result);
172 return $row['relationship_name'];
179 //end function retrieve_by_sides
183 function retrieve_by_name($relationship_name) {
185 if (empty($GLOBALS['relationships'])) {
186 $this->load_relationship_meta();
189 // _ppd($GLOBALS['relationships']);
191 if (array_key_exists($relationship_name, $GLOBALS['relationships'])) {
193 foreach($GLOBALS['relationships'][$relationship_name] as $field=>$value)
195 $this->$field = $value;
199 $GLOBALS['log']->fatal('Error fetching relationship from cache '.$relationship_name);
204 function load_relationship_meta() {
205 if (!file_exists(Relationship::cache_file_dir().'/'.Relationship::cache_file_name_only())) {
206 $this->build_relationship_cache();
208 include(Relationship::cache_file_dir().'/'.Relationship::cache_file_name_only());
209 $GLOBALS['relationships']=$relationships;
212 function build_relationship_cache() {
213 $query="SELECT * from relationships where deleted=0";
214 $result=$this->db->query($query);
216 while (($row=$this->db->fetchByAssoc($result))!=null) {
217 $relationships[$row['relationship_name']] = $row;
220 $rel_string='<?php ';
221 $rel_string.='$relationships='.var_export($relationships,true);
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);
227 require_once("data/Relationships/RelationshipFactory.php");
228 SugarRelationshipFactory::deleteCache();
232 public static function cache_file_dir() {
233 return sugar_cached("modules/Relationships");
235 public static function cache_file_name_only() {
236 return 'relationships.cache.php';
239 public static function delete_cache() {
240 $filename=Relationship::cache_file_dir().'/'.Relationship::cache_file_name_only();
241 if (file_exists($filename)) {
244 require_once("data/Relationships/RelationshipFactory.php");
245 SugarRelationshipFactory::deleteCache();
249 function trace_relationship_module($base_module, $rel_module1_name, $rel_module2_name=""){
253 $temp_module = get_module_info($base_module);
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);
259 if($rel_module2_name!=""){
260 if($rel_module2_name == 'ProjectTask'){
261 $rel_module2_name = strtolower($rel_module2_name);
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;
269 //no rel_module2, so return rel_module2 bean
270 return $rel_module1_bean;
273 //end function trace_relationship_module