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.
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 ********************************************************************************/
40 //Load all relationship metadata
41 include_once("modules/TableDictionary.php");
42 require_once("data/BeanFactory.php");
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');
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.
58 abstract class SugarRelationship
63 protected $ignore_role_filter = false;
64 protected $self_referencing = false; //A relationship is self referencing when LHS module = RHS Module
66 protected static $beansToResave = array();
68 public abstract function add($lhs, $rhs, $additionalFields = array());
72 * @param $lhs SugarBean
73 * @param $rhs SugarBean
76 public abstract function remove($lhs, $rhs);
80 * @param $link Link2 loads the rows for this relationship that match the given link
83 public abstract function load($link, $params = array());
86 * Gets the query to load a link.
87 * This is currently public, but should prob be made protected later.
90 * @param $link Link Object to get query for.
91 * @return string|array query used to load this relationship
93 public abstract function getQuery($link, $params = array());
98 * @return string|array the query to join against the related modules table for the given link.
100 public abstract function getJoin($link);
104 * @param SugarBean $lhs
105 * @param SugarBean $rhs
108 public abstract function relationship_exists($lhs, $rhs);
112 * @return string name of the table for this relationship
114 public abstract function getRelationshipTable();
117 * @param $link Link2 removes all the beans associated with this link from the relationship
118 * @return boolean true if all beans were successfully removed or there
119 * were not related beans, false otherwise
121 public function removeAll($link)
123 $focus = $link->getFocus();
124 $related = $link->getBeans();
126 foreach($related as $relBean)
128 if (empty($relBean->id)) {
132 if ($link->getSide() == REL_LHS)
134 $sub_result = $this->remove($focus, $relBean);
138 $sub_result = $this->remove($relBean, $focus);
141 $result = $result && $sub_result;
148 * @param $rowID id of SugarBean to remove from the relationship
151 public function removeById($rowID){
152 $this->removeRow(array("id" => $rowID));
156 * @return string name of right hand side module.
158 public function getRHSModule()
160 return $this->def['rhs_module'];
164 * @return string name of left hand side module.
166 public function getLHSModule()
168 return $this->def['lhs_module'];
172 * @return String left link in relationship.
174 public function getLHSLink()
176 return $this->lhsLink;
180 * @return String right link in relationship.
182 public function getRHSLink()
184 return $this->rhsLink;
188 * @return array names of fields stored on the relationship
190 public function getFields()
192 return isset($this->def['fields']) ? $this->def['fields'] : array();
196 * @param array $row values to be inserted into the relationship
197 * @return bool|void null if new row was inserted and true if an existing row was updated
199 protected function addRow($row)
201 $existing = $this->checkExisting($row);
202 if (!empty($existing)) //Update the existing row, overriding the values with those passed in
203 return $this->updateRow($existing['id'], array_merge($existing, $row));
206 foreach($this->getFields() as $def)
208 $field = $def['name'];
209 if (isset($row[$field]))
211 $values[$field] = "'{$row[$field]}'";
214 $columns = implode(',', array_keys($values));
215 $values = implode(',', $values);
218 $query = "INSERT INTO {$this->getRelationshipTable()} ($columns) VALUES ($values)";
219 DBManagerFactory::getInstance()->query($query);
224 * @param $id id of row to update
225 * @param $values values to insert into row
226 * @return resource result of update satatement
228 protected function updateRow($id, $values)
231 //Unset the ID since we are using it to update the row
232 if (isset($values['id'])) unset($values['id']);
233 foreach($values as $field => $val)
235 $newVals[] = "$field='$val'";
238 $newVals = implode(",",$newVals);
240 $query = "UPDATE {$this->getRelationshipTable()} set $newVals WHERE id='$id'";
242 return DBManagerFactory::getInstance()->query($query);
246 * Removes one or more rows from the relationship table
247 * @param $where array of field=>value pairs to match
248 * @return bool|resource
250 protected function removeRow($where)
255 $date_modified = TimeDate::getInstance()->getNow()->asDb();
256 $stringSets = array();
257 foreach ($where as $field => $val)
259 $stringSets[] = "$field = '$val'";
261 $whereString = "WHERE " . implode(" AND ", $stringSets);
263 $query = "UPDATE {$this->getRelationshipTable()} set deleted=1 , date_modified = '$date_modified' $whereString";
265 return DBManagerFactory::getInstance()->query($query);
270 * Checks for an existing row who's keys match the one passed in.
272 * @return array|bool returns false if now row is found, otherwise the row is returned
274 protected function checkExisting($row)
276 $leftIDName = $this->def['join_key_lhs'];
277 $rightIDName = $this->def['join_key_rhs'];
278 if (empty($row[$leftIDName]) || empty($row[$rightIDName]))
281 $leftID = $row[$leftIDName];
282 $rightID = $row[$rightIDName];
283 //Check the relationship role as well
284 $roleCheck = $this->getRoleWhere();
286 $query = "SELECT * FROM {$this->getRelationshipTable()} WHERE $leftIDName='$leftID' AND $rightIDName='$rightID' $roleCheck AND deleted=0";
288 $db = DBManagerFactory::getInstance();
289 $result = $db->query($query);
290 $row = $db->fetchByAssoc($result);
300 * Gets the relationship role column check for the where clause
301 * @param string $table
304 protected function getRoleWhere($table = "", $ignore_role_filter = false)
306 $ignore_role_filter = $ignore_role_filter || $this->ignore_role_filter;
309 $table = $this->getRelationshipTable();
310 if (!empty($this->def['relationship_role_column']) && !empty($this->def["relationship_role_column_value"]) && !$ignore_role_filter )
313 $roleCheck = " AND $this->relationship_role_column";
315 $roleCheck = " AND $table.{$this->relationship_role_column}";
317 if (empty($this->def['relationship_role_column_value']))
319 $roleCheck.=' IS NULL';
321 $roleCheck.= " = '$this->relationship_role_column_value'";
328 * @param SugarBean $focus base bean the hooks is triggered from
329 * @param SugarBean $related bean being added/removed/updated from relationship
330 * @param string $link_name name of link being triggerd
331 * @return array base arguments to pass to relationship logic hooks
333 protected function getCustomLogicArguments($focus, $related, $link_name)
335 $custom_logic_arguments = array();
336 $custom_logic_arguments['id'] = $focus->id;
337 $custom_logic_arguments['related_id'] = $related->id;
338 $custom_logic_arguments['module'] = $focus->module_dir;
339 $custom_logic_arguments['related_module'] = $related->module_dir;
340 $custom_logic_arguments['link'] = $link_name;
341 $custom_logic_arguments['relationship'] = $this->name;
343 return $custom_logic_arguments;
347 * Call the before add logic hook for a given link
348 * @param SugarBean $focus base bean the hooks is triggered from
349 * @param SugarBean $related bean being added/removed/updated from relationship
350 * @param string $link_name name of link being triggerd
353 protected function callBeforeAdd($focus, $related, $link_name="")
355 $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
356 $focus->call_custom_logic('before_relationship_add', $custom_logic_arguments);
360 * Call the after add logic hook for a given link
361 * @param SugarBean $focus base bean the hooks is triggered from
362 * @param SugarBean $related bean being added/removed/updated from relationship
363 * @param string $link_name name of link being triggerd
366 protected function callAfterAdd($focus, $related, $link_name="")
368 $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
369 $focus->call_custom_logic('after_relationship_add', $custom_logic_arguments);
373 * @param SugarBean $focus
374 * @param SugarBean $related
375 * @param string $link_name
378 protected function callBeforeDelete($focus, $related, $link_name="")
380 $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
381 $focus->call_custom_logic('before_relationship_delete', $custom_logic_arguments);
385 * @param SugarBean $focus
386 * @param SugarBean $related
387 * @param string $link_name
390 protected function callAfterDelete($focus, $related, $link_name="")
392 $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
393 $focus->call_custom_logic('after_relationship_delete', $custom_logic_arguments);
397 * @param $optional_array clause to add to the where query when populating this relationship. It should be in the
398 * @param string $add_and
399 * @param string $prefix
402 protected function getOptionalWhereClause($optional_array) {
403 //lhs_field, operator, and rhs_value must be set in optional_array
404 foreach(array("lhs_field", "operator", "rhs_value") as $required){
405 if (empty($optional_array[$required]))
409 return $optional_array['lhs_field']."".$optional_array['operator']."'".$optional_array['rhs_value']."'";
413 * Adds a realted Bean to the list to be resaved along with the current bean.
415 * @param SugarBean $bean
418 public static function addToResaveList($bean)
420 if (!isset(self::$beansToResave[$bean->module_dir]))
422 self::$beansToResave[$bean->module_dir] = array();
424 self::$beansToResave[$bean->module_dir][$bean->id] = $bean;
432 public static function resaveRelatedBeans()
434 $GLOBALS['resavingRelatedBeans'] = true;
436 //Resave any bean not currently in the middle of a save operation
437 foreach(self::$beansToResave as $module => $beans)
439 foreach ($beans as $bean)
441 if (empty($bean->deleted) && empty($bean->in_save))
447 // Bug 55942 save the in-save id which will be used to send workflow alert later
448 if (isset($bean->id) && !empty($_SESSION['WORKFLOW_ALERTS']))
450 $_SESSION['WORKFLOW_ALERTS']['id'] = $bean->id;
456 $GLOBALS['resavingRelatedBeans'] = false;
458 //Reset the list of beans that will need to be resaved
459 self::$beansToResave = array();
463 * @return bool true if the relationship is a flex / parent relationship
465 public function isParentRelationship()
467 //check role fields to see if this is a parent (flex relate) relationship
468 if(!empty($this->def["relationship_role_column"]) && !empty($this->def["relationship_role_column_value"])
469 && $this->def["relationship_role_column"] == "parent_type" && $this->def['rhs_key'] == "parent_id")
476 public function __get($name)
478 if (isset($this->def[$name]))
479 return $this->def[$name];
483 case "relationship_type":
485 case 'relationship_name':
488 return $this->getLHSModule();
490 return $this->getRHSModule();
492 isset($this->def['lhs_table']) ? $this->def['lhs_table'] : "";
494 isset($this->def['rhs_table']) ? $this->def['rhs_table'] : "";
496 return array('lhs_table', 'lhs_key', 'rhs_module', 'rhs_table', 'rhs_key', 'relationship_type');
499 if (isset($this->$name))