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 ********************************************************************************/
39 require_once("data/Relationships/One2MRelationship.php");
42 * Represents a one to many relationship that is table based.
45 class One2MBeanRelationship extends One2MRelationship
47 //Type is read in sugarbean to determine query construction
48 var $type = "one-to-many";
50 public function __construct($def)
52 parent::__construct($def);
56 * @param $lhs SugarBean left side bean to add to the relationship.
57 * @param $rhs SugarBean right side bean to add to the relationship.
58 * @param $additionalFields key=>value pairs of fields to save on the relationship
59 * @return boolean true if successful
61 public function add($lhs, $rhs, $additionalFields = array())
63 // test to see if the relationship exist if the relationship between the two beans
64 // exist then we just fail out with false as we don't want to re-trigger this
65 // the save and such as it causes problems with the related() in sugarlogic
66 if($this->relationship_exists($lhs, $rhs) && !empty($GLOBALS['resavingRelatedBeans'])) return false;
68 $lhsLinkName = $this->lhsLink;
69 $rhsLinkName = $this->rhsLink;
71 //Since this is bean based, we know updating the RHS's field will overwrite any old value,
72 //But we need to use delete to make sure custom logic is called correctly
73 if ($rhs->load_relationship($rhsLinkName))
75 $oldLink = $rhs->$rhsLinkName;
76 $prevRelated = $oldLink->getBeans(null);
77 foreach($prevRelated as $oldLHS)
79 $this->remove($oldLHS, $rhs, false);
83 //Make sure we load the current relationship state to the LHS link
84 if ((isset($lhs->$lhsLinkName) && is_a($lhs->$lhsLinkName, "Link2")) || $lhs->load_relationship($lhsLinkName)) {
85 $lhs->$lhsLinkName->load();
88 if (empty($_SESSION['disable_workflow']) || $_SESSION['disable_workflow'] != "Yes")
90 $this->callBeforeAdd($lhs, $rhs);
91 $this->callBeforeAdd($rhs, $lhs);
94 $this->updateFields($lhs, $rhs, $additionalFields);
96 if (empty($_SESSION['disable_workflow']) || $_SESSION['disable_workflow'] != "Yes")
98 //Need to call save to update the bean as the relationship is saved on the main table
99 //We don't want to create a save loop though, so make sure we aren't already in the middle of saving this bean
100 SugarRelationship::addToResaveList($rhs);
102 $this->updateLinks($lhs, $lhsLinkName, $rhs, $rhsLinkName);
104 $this->callAfterAdd($lhs, $rhs);
105 $this->callAfterAdd($rhs, $lhs);
108 //One2MBean relationships require that the RHS bean be saved or else the relationship will not be saved.
109 //If we aren't already in a relationship save, intitiate a save now.
110 if (empty($GLOBALS['resavingRelatedBeans']))
111 SugarRelationship::resaveRelatedBeans();
114 protected function updateLinks($lhs, $lhsLinkName, $rhs, $rhsLinkName)
116 if (isset($lhs->$lhsLinkName))
117 $lhs->$lhsLinkName->addBean($rhs);
118 //RHS only has one bean ever, so we don't need to preload the relationship
119 if (isset($rhs->$rhsLinkName))
120 $rhs->$rhsLinkName->beans = array($lhs->id => $lhs);
123 protected function updateFields($lhs, $rhs, $additionalFields)
125 //Now update the RHS bean's ID field
126 $rhsID = $this->def['rhs_key'];
127 $rhs->$rhsID = $lhs->id;
128 foreach($additionalFields as $field => $val)
133 if(!empty($this->def["relationship_role_column"]) && !empty($this->def["relationship_role_column_value"]))
135 $roleField = $this->def["relationship_role_column"];
136 $rhs->$roleField = $this->def["relationship_role_column_value"];
140 public function remove($lhs, $rhs, $save = true)
142 $rhsID = $this->def['rhs_key'];
144 //If this relationship has already been removed, we can just return
145 if ($rhs->$rhsID != $lhs->id)
150 if (empty($_SESSION['disable_workflow']) || $_SESSION['disable_workflow'] != "Yes")
152 $this->callBeforeDelete($lhs, $rhs);
153 $this->callBeforeDelete($rhs, $lhs);
156 if ($save && !$rhs->deleted)
158 $rhs->in_relationship_update = TRUE;
162 if (empty($_SESSION['disable_workflow']) || $_SESSION['disable_workflow'] != "Yes")
164 $this->callAfterDelete($lhs, $rhs);
165 $this->callAfterDelete($rhs, $lhs);
170 * @param $link Link2 loads the relationship for this link.
173 public function load($link, $params = array())
175 $relatedModule = $link->getSide() == REL_LHS ? $this->def['rhs_module'] : $this->def['lhs_module'];
177 //The related bean ID is stored on the RHS table.
178 //If the link is RHS, just grab it from the focus.
179 if ($link->getSide() == REL_RHS)
181 $rhsID = $this->def['rhs_key'];
182 $id = $link->getFocus()->$rhsID;
185 $rows[$id] = array('id' => $id);
188 else //If the link is LHS, we need to query to get the full list and load all the beans.
190 $db = DBManagerFactory::getInstance();
191 $query = $this->getQuery($link, $params);
194 $GLOBALS['log']->fatal("query for {$this->name} was empty when loading from {$this->lhsLink}\n");
195 return array("rows" => array());
197 $result = $db->query($query);
198 while ($row = $db->fetchByAssoc($result, FALSE))
205 return array("rows" => $rows);
208 public function getQuery($link, $params = array())
210 //There was an old signature with $return_as_array as the second parameter. We should respect this if $params is true
211 if ($params === true){
212 $params = array("return_as_array" => true);
215 if ($link->getSide() == REL_RHS) {
220 $lhsKey = $this->def['lhs_key'];
221 $rhsTable = $this->def['rhs_table'];
222 $rhsTableKey = "{$rhsTable}.{$this->def['rhs_key']}";
223 $deleted = !empty($params['deleted']) ? 1 : 0;
224 $where = "WHERE $rhsTableKey = '{$link->getFocus()->$lhsKey}' AND {$rhsTable}.deleted=$deleted";
226 //Check for role column
227 if(!empty($this->def["relationship_role_column"]) && !empty($this->def["relationship_role_column_value"]))
229 $roleField = $this->def["relationship_role_column"];
230 $roleValue = $this->def["relationship_role_column_value"];
231 $where .= " AND $rhsTable.$roleField = '$roleValue'";
234 //Add any optional where clause
235 if (!empty($params['where'])){
236 $add_where = is_string($params['where']) ? $params['where'] : "$rhsTable." . $this->getOptionalWhereClause($params['where']);
237 if (!empty($add_where))
238 $where .= " AND $add_where";
241 $from = $this->def['rhs_table'];
243 if (empty($params['return_as_array'])) {
244 //Limit is not compatible with return_as_array
245 $query = "SELECT id FROM $from $where";
246 if (!empty($params['limit']) && $params['limit'] > 0) {
247 $offset = isset($params['offset']) ? $params['offset'] : 0;
248 $query = DBManagerFactory::getInstance()->limitQuery($query, $offset, $params['limit'], false, "", false);
255 'select' => "SELECT {$this->def['rhs_table']}.id",
256 'from' => "FROM {$this->def['rhs_table']}",
263 public function getJoin($link, $params = array(), $return_array = false)
265 $linkIsLHS = $link->getSide() == REL_LHS;
266 $startingTable = (empty($params['left_join_table_alias']) ? $this->def['lhs_table'] : $params['left_join_table_alias']);
268 $startingTable = (empty($params['right_join_table_alias']) ? $this->def['rhs_table'] : $params['right_join_table_alias']);
269 $startingKey = $linkIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key'];
270 $targetTable = $linkIsLHS ? $this->def['rhs_table'] : $this->def['lhs_table'];
271 $targetTableWithAlias = $targetTable;
272 $targetKey = $linkIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key'];
273 $join_type= isset($params['join_type']) ? $params['join_type'] : ' INNER JOIN ';
276 //Set up any table aliases required
277 if ( ! empty($params['join_table_alias']))
279 $targetTableWithAlias = $targetTable. " ".$params['join_table_alias'];
280 $targetTable = $params['join_table_alias'];
283 //First join the relationship table
284 $join .= "$join_type $targetTableWithAlias ON $startingTable.$startingKey=$targetTable.$targetKey AND $targetTable.deleted=0\n"
285 //Next add any role filters
286 . $this->getRoleWhere(($linkIsLHS) ? $targetTable : $startingTable) . "\n";
291 'type' => $this->type,
292 'rel_key' => $targetKey,
293 'join_tables' => array($targetTable),
295 'select' => "$targetTable.id",
301 public function getSubpanelQuery($link, $params = array(), $return_array = false)
304 $linkIsLHS = $link->getSide() == REL_RHS;
305 $startingTable = (empty($params['left_join_table_alias']) ? $this->def['lhs_table'] : $params['left_join_table_alias']);
307 $startingTable = (empty($params['right_join_table_alias']) ? $this->def['rhs_table'] : $params['right_join_table_alias']);
308 $startingKey = $linkIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key'];
309 $targetTable = $linkIsLHS ? $this->def['rhs_table'] : $this->def['lhs_table'];
310 $targetKey = $linkIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key'];
311 $join_type= isset($params['join_type']) ? $params['join_type'] : ' INNER JOIN ';
314 $alias = empty($params['join_table_alias']) ? "{$link->name}_rel": $params['join_table_alias'];
315 $alias = $GLOBALS['db']->getValidDBName($alias, false, 'alias');
317 $tableInRoleFilter = "";
318 if (($targetTable == "meetings" ||
319 $targetTable == "notes" ||
320 $targetTable == "tasks" ||
321 $targetTable == "calls") && $linkIsLHS == false) {
322 if (substr($alias, -25) == "activities_1_meetings_rel" ||
323 substr($alias, -22) == "activities_1_tasks_rel" ||
324 substr($alias, -22) == "activities_1_calls_rel" ||
325 substr($alias, -23) == "activities_1_emails_rel" ||
326 substr($alias, -22) == "activities_1_notes_rel")
327 $tableInRoleFilter = $alias;
329 else if (($startingTable == "meetings" ||
330 $startingTable == "notes" ||
331 $startingTable == "tasks" ||
332 $startingTable == "calls" ||
333 $startingTable == "emails") && empty($linkIsLHS)) {
334 if (substr($alias, -23) == "activities_meetings_rel" ||
335 substr($alias, -20) == "activities_tasks_rel" ||
336 substr($alias, -20) == "activities_calls_rel" ||
337 substr($alias, -21) == "activities_emails_rel" ||
338 substr($alias, -20) == "activities_notes_rel")
339 $tableInRoleFilter = $startingTable;
342 //Set up any table aliases required
343 $targetTableWithAlias = "$targetTable $alias";
344 $targetTable = $alias;
346 $query .= "$join_type $targetTableWithAlias ON $startingTable.$startingKey=$targetTable.$targetKey AND $targetTable.deleted=0\n"
347 //Next add any role filters
348 . $this->getRoleWhere($tableInRoleFilter) . "\n";
350 if (!empty($params['return_as_array'])) {
351 $return_array = true;
357 'type' => $this->type,
358 'rel_key' => $targetKey,
359 'join_tables' => array($targetTable),
360 'where' => "WHERE $startingTable.$startingKey='{$link->focus->id}'",
369 * Check to see if the relationship already exist.
371 * If it does return true otherwise return false
373 * @param SugarBean $lhs Left hand side of the relationship
374 * @param SugarBean $rhs Right hand side of the relationship
377 public function relationship_exists($lhs, $rhs)
379 // we need the key that is stored on the rhs to compare tok
380 $lhsIDName = $this->def['rhs_key'];
382 return (isset($rhs->fetched_row[$lhsIDName]) && $rhs->$lhsIDName == $rhs->fetched_row[$lhsIDName] && $rhs->$lhsIDName == $lhs->id);
385 public function getRelationshipTable()
387 if (isset($this->def['table']))
388 return $this->def['table'];
390 return $this->def['rhs_table'];