]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - data/Relationships/SugarRelationship.php
Release 6.3.0
[Github/sugarcrm.git] / data / Relationships / SugarRelationship.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 global $dictionary;
40 //Load all relationship metadata
41 include_once("modules/TableDictionary.php");
42 require_once("data/BeanFactory.php");
43
44
45 define('REL_LHS','LHS');
46 define('REL_RHS','RHS');
47 define('REL_BOTH','BOTH_SIDES');
48 define('REL_MANY_MANY', 'many-to-many');
49 define('REL_ONE_MANY', 'one-to-many');
50 define('REL_ONE_ONE', 'one-to-one');
51 /**
52  * A relationship is between two modules.
53  * It contains at least two links.
54  * Each link represents a connection from one record to the records linked in this relationship.
55  * Links have a context(focus) bean while relationships do not.
56  *
57  *
58  */
59 abstract class SugarRelationship
60 {
61     protected $def;
62     protected $lhsLink;
63     protected $rhsLink;
64     protected $ignore_role_filter = false;
65     protected $self_referencing = false; //A relationship is self referencing when LHS module = RHS Module
66
67     protected static $beansToResave = array();
68
69     public abstract function add($lhs, $rhs, $additionalFields = array());
70
71     /**
72      * @abstract
73      * @param  $lhs SugarBean
74      * @param  $rhs SugarBean
75      * @return void
76      */
77     public abstract function remove($lhs, $rhs);
78
79     /**
80      * @abstract
81      * @param $link Link2 loads the rows for this relationship that match the given link
82      * @return void
83      */
84     public abstract function load($link);
85
86     /**
87      * Gets the query to load a link.
88      * This is currently public, but should prob be made protected later.
89      * See Link2->getQuery
90      * @abstract
91      * @param  $link Link Object to get query for.
92      * @return string|array query used to load this relationship
93      */
94     public abstract function getQuery($link, $params = array());
95
96     /**
97      * @abstract
98      * @param Link2 $link
99      * @return string|array the query to join against the related modules table for the given link.
100      */
101     public abstract function getJoin($link);
102
103     /**
104      * @abstract
105      * @param SugarBean $lhs
106      * @param SugarBean $rhs
107      * @return bool
108      */
109     public abstract function relationship_exists($lhs, $rhs);
110
111     /**
112      * @abstract
113      * @return string name of the table for this relationship
114      */
115     public abstract function getRelationshipTable();
116
117     /**
118      * @param  $link Link2 removes all the beans associated with this link from the relationship
119      * @return void
120      */
121     public function removeAll($link)
122     {
123         $focus = $link->getFocus();
124         $related = $link->getBeans();
125         foreach($related as $relBean)
126         {
127             if (empty($relBean->id)) {
128                 continue;
129             }
130
131             if ($link->getSide() == REL_LHS)
132                 $this->remove($focus, $relBean);
133             else
134                 $this->remove($relBean, $focus);
135         }
136     }
137
138     /**
139      * @param $rowID id of SugarBean to remove from the relationship
140      * @return void
141      */
142     public function removeById($rowID){
143         $this->removeRow(array("id" => $rowID));
144     }
145
146     /**
147      * @return string name of right hand side module.
148      */
149     public function getRHSModule()
150     {
151         return $this->def['rhs_module'];
152     }
153
154     /**
155      * @return string name of left hand side module.
156      */
157     public function getLHSModule()
158     {
159         return $this->def['lhs_module'];
160     }
161
162     /**
163      * @return String left link in relationship.
164      */
165     public function getLHSLink()
166     {
167         return $this->lhsLink;
168     }
169
170     /**
171      * @return String right link in relationship.
172      */
173     public function getRHSLink()
174     {
175         return $this->rhsLink;
176     }
177
178     /**
179      * @return array names of fields stored on the relationship
180      */
181     public function getFields()
182     {
183         return isset($this->def['fields']) ? $this->def['fields'] : array();
184     }
185
186     /**
187      * @param array $row values to be inserted into the relationship
188      * @return bool|void null if new row was inserted and true if an exesting row was updated
189      */
190     protected function addRow($row)
191     {
192         $existing = $this->checkExisting($row);
193         if (!empty($existing)) //Update the existing row, overriding the values with those passed in
194             return $this->updateRow($existing['id'], array_merge($existing, $row));
195
196         $values = array();
197         foreach($this->getFields() as  $def)
198         {
199             $field = $def['name'];
200             if (isset($row[$field]))
201                 $values[$field] = "'{$row[$field]}'";
202             else
203                 $values[$field] = "''";
204         }
205         $columns = implode(',', array_keys($values));
206         $values = implode(',', $values);
207         if (!empty($values))
208         {
209             $query = "INSERT INTO {$this->getRelationshipTable()} ($columns) VALUES ($values)";
210             DBManagerFactory::getInstance()->query($query);
211         }
212     }
213
214     /**
215      * @param $id id of row to update
216      * @param $values values to insert into row
217      * @return resource result of update satatement
218      */
219     protected function updateRow($id, $values)
220     {
221         $newVals = array();
222         //Unset the ID since we are using it to update the row
223         if (isset($values['id'])) unset($values['id']);
224         foreach($values as $field => $val)
225         {
226             $newVals[] = "$field='$val'";
227         }
228
229         $newVals = implode(",",$newVals);
230
231         $query = "UPDATE {$this->getRelationshipTable()} set $newVals WHERE id='$id'";
232
233         return DBManagerFactory::getInstance()->query($query);
234     }
235
236     /**
237      * Removes one or more rows from the relationship table
238      * @param $where array of field=>value pairs to match
239      * @return bool|resource
240      */
241     protected function removeRow($where)
242     {
243         if (empty($where))
244             return false;
245
246         $date_modified = TimeDate::getInstance()->getNow()->asDb();
247         $stringSets = array();
248         foreach ($where as $field => $val)
249         {
250             $stringSets[] = "$field = '$val'";
251         }
252         $whereString = "WHERE " . implode(" AND ", $stringSets);
253
254         $query = "UPDATE {$this->getRelationshipTable()} set deleted=1 , date_modified = '$date_modified' $whereString";
255
256         return DBManagerFactory::getInstance()->query($query);
257
258     }
259
260     /**
261      * Checks for an existing row who's keys match the one passed in.
262      * @param  $row
263      * @return array|bool returns false if now row is found, otherwise the row is returned
264      */
265     protected function checkExisting($row)
266     {
267         $leftIDName = $this->def['join_key_lhs'];
268         $rightIDName = $this->def['join_key_rhs'];
269         if (empty($row[$leftIDName]) ||  empty($row[$rightIDName]))
270             return false;
271
272         $leftID = $row[$leftIDName];
273         $rightID = $row[$rightIDName];
274         //Check the relationship role as well
275         $roleCheck = $this->getRoleWhere();
276
277         $query = "SELECT * FROM {$this->getRelationshipTable()} WHERE $leftIDName='$leftID' AND $rightIDName='$rightID' $roleCheck AND deleted=0";
278
279         $db = DBManagerFactory::getInstance();
280         $result = $db->query($query);
281         $row = $db->fetchByAssoc($result);
282         if (!empty($row))
283         {
284             return $row;
285         } else{
286             return false;
287         }
288     }
289
290     /**
291      * Gets the relationship role column check for the where clause
292      * @param string $table
293      * @return string
294      */
295     protected function getRoleWhere($table = "", $ignore_role_filter = false)
296     {
297         $ignore_role_filter = $ignore_role_filter || $this->ignore_role_filter;
298         $roleCheck = "";
299         if (empty ($table))
300             $table = $this->getRelationshipTable();
301         if (!empty($this->def['relationship_role_column']) && !empty($this->def["relationship_role_column_value"]) && !$ignore_role_filter )
302         {
303             if (empty($table))
304                 $roleCheck = " AND $this->relationship_role_column";
305             else
306                 $roleCheck = " AND $table.{$this->relationship_role_column}";
307             //role column value.
308             if (empty($this->def['relationship_role_column_value']))
309             {
310                 $roleCheck.=' IS NULL';
311             } else {
312                 $roleCheck.= " = '$this->relationship_role_column_value'";
313             }
314         }
315         return $roleCheck;
316     }
317
318     /**
319      * @param SugarBean $focus base bean the hooks is triggered from
320      * @param SugarBean $related bean being added/removed/updated from relationship
321      * @param string $link_name name of link being triggerd
322      * @return array base arguments to pass to relationship logic hooks
323      */
324     protected function getCustomLogicArguments($focus, $related, $link_name)
325     {
326         $custom_logic_arguments = array();
327         $custom_logic_arguments['id'] = $focus->id;
328         $custom_logic_arguments['related_id'] = $related->id;
329         $custom_logic_arguments['module'] = $focus->module_dir;
330         $custom_logic_arguments['related_module'] = $related->module_dir;
331         $custom_logic_arguments['link'] = $link_name;
332         $custom_logic_arguments['relationship'] = $this->name;
333
334         return $custom_logic_arguments;
335     }
336
337     /**
338      * Call the after add logic hook for a given link
339      * @param  SugarBean $focus base bean the hooks is triggered from
340      * @param  SugarBean $related bean being added/removed/updated from relationship
341      * @param string $link_name name of link being triggerd
342      * @return void
343      */
344     protected function callAfterAdd($focus, $related, $link_name="")
345     {
346         $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
347         $focus->call_custom_logic('after_relationship_add', $custom_logic_arguments);
348     }
349
350     /**
351      * @param  SugarBean $focus
352      * @param  SugarBean $related
353      * @param string $link_name
354      * @return void
355      */
356     protected function callAfterDelete($focus, $related, $link_name="")
357     {
358         $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
359         $focus->call_custom_logic('after_relationship_delete', $custom_logic_arguments);
360     }
361
362     /**
363      * Adds a realted Bean to the list to be resaved along with the current bean.
364      * @static
365      * @param  SugarBean $bean
366      * @return void
367      */
368     public static function addToResaveList($bean)
369     {
370         if (!isset(self::$beansToResave[$bean->module_dir]))
371         {
372             self::$beansToResave[$bean->module_dir] = array();
373         }
374         self::$beansToResave[$bean->module_dir][$bean->id] = $bean;
375     }
376
377     /**
378      * 
379      * @static
380      * @return void
381      */
382     public static function resaveRelatedBeans()
383     {
384         $GLOBALS['resavingRelatedBeans'] = true;
385
386         //Resave any bean not currently in the middle of a save operation
387         foreach(self::$beansToResave as $module => $beans)
388         {
389             foreach ($beans as $bean)
390             {
391                 if (empty($bean->deleted) && empty($bean->in_save))
392                 {
393                     $bean->save();
394                 }
395             }
396         }
397
398         $GLOBALS['resavingRelatedBeans'] = false;
399
400         //Reset the list of beans that will need to be resaved
401         self::$beansToResave = array();
402     }
403
404     /**
405      * @return bool true if the relationship is a flex / parent relationship
406      */
407     public function isParentRelationship()
408     {
409         //check role fields to see if this is a parent (flex relate) relationship
410         if(!empty($this->def["relationship_role_column"]) && !empty($this->def["relationship_role_column_value"])
411            && $this->def["relationship_role_column"] == "parent_type" && $this->def['rhs_key'] == "parent_id")
412         {
413             return true;
414         }
415         return false;
416     }
417
418     public function __get($name)
419     {
420         if (isset($this->def[$name]))
421             return $this->def[$name];
422
423         switch($name)
424         {
425             case "relationship_type":
426                 return $this->type;
427             case 'relationship_name':
428                 return $this->name;
429             case "lhs_module":
430                 return $this->getLHSModule();
431             case "rhs_module":
432                 return $this->getRHSModule();
433             case "lhs_table" :
434                 isset($this->def['lhs_table']) ? $this->def['lhs_table'] : "";
435             case "rhs_table" :
436                 isset($this->def['rhs_table']) ? $this->def['rhs_table'] : "";
437             case "list_fields":
438                 return array('lhs_table', 'lhs_key', 'rhs_module', 'rhs_table', 'rhs_key', 'relationship_type');
439         }
440
441         if (isset($this->$name))
442             return $this->$name;
443
444         return null;
445     }
446 }