]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - data/Relationships/SugarRelationship.php
Release 6.5.8
[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-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 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  * @api
57  */
58 abstract class SugarRelationship
59 {
60     protected $def;
61     protected $lhsLink;
62     protected $rhsLink;
63     protected $ignore_role_filter = false;
64     protected $self_referencing = false; //A relationship is self referencing when LHS module = RHS Module
65
66     protected static $beansToResave = array();
67
68     public abstract function add($lhs, $rhs, $additionalFields = array());
69
70     /**
71      * @abstract
72      * @param  $lhs SugarBean
73      * @param  $rhs SugarBean
74      * @return boolean
75      */
76     public abstract function remove($lhs, $rhs);
77
78     /**
79      * @abstract
80      * @param $link Link2 loads the rows for this relationship that match the given link
81      * @return void
82      */
83     public abstract function load($link, $params = array());
84
85     /**
86      * Gets the query to load a link.
87      * This is currently public, but should prob be made protected later.
88      * See Link2->getQuery
89      * @abstract
90      * @param  $link Link Object to get query for.
91      * @return string|array query used to load this relationship
92      */
93     public abstract function getQuery($link, $params = array());
94
95     /**
96      * @abstract
97      * @param Link2 $link
98      * @return string|array the query to join against the related modules table for the given link.
99      */
100     public abstract function getJoin($link);
101
102     /**
103      * @abstract
104      * @param SugarBean $lhs
105      * @param SugarBean $rhs
106      * @return bool
107      */
108     public abstract function relationship_exists($lhs, $rhs);
109
110     /**
111      * @abstract
112      * @return string name of the table for this relationship
113      */
114     public abstract function getRelationshipTable();
115
116     /**
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
120      */
121     public function removeAll($link)
122     {
123         $focus = $link->getFocus();
124         $related = $link->getBeans();
125         $result = true;
126         foreach($related as $relBean)
127         {
128             if (empty($relBean->id)) {
129                 continue;
130             }
131
132             if ($link->getSide() == REL_LHS)
133             {
134                 $sub_result = $this->remove($focus, $relBean);
135             }
136             else
137             {
138                 $sub_result = $this->remove($relBean, $focus);
139             }
140
141             $result = $result && $sub_result;
142         }
143
144         return $result;
145     }
146
147     /**
148      * @param $rowID id of SugarBean to remove from the relationship
149      * @return void
150      */
151     public function removeById($rowID){
152         $this->removeRow(array("id" => $rowID));
153     }
154
155     /**
156      * @return string name of right hand side module.
157      */
158     public function getRHSModule()
159     {
160         return $this->def['rhs_module'];
161     }
162
163     /**
164      * @return string name of left hand side module.
165      */
166     public function getLHSModule()
167     {
168         return $this->def['lhs_module'];
169     }
170
171     /**
172      * @return String left link in relationship.
173      */
174     public function getLHSLink()
175     {
176         return $this->lhsLink;
177     }
178
179     /**
180      * @return String right link in relationship.
181      */
182     public function getRHSLink()
183     {
184         return $this->rhsLink;
185     }
186
187     /**
188      * @return array names of fields stored on the relationship
189      */
190     public function getFields()
191     {
192         return isset($this->def['fields']) ? $this->def['fields'] : array();
193     }
194
195     /**
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
198      */
199     protected function addRow($row)
200     {
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));
204
205         $values = array();
206         foreach($this->getFields() as  $def)
207         {
208             $field = $def['name'];
209             if (isset($row[$field]))
210             {
211                 $values[$field] = "'{$row[$field]}'";
212             }
213         }
214         $columns = implode(',', array_keys($values));
215         $values = implode(',', $values);
216         if (!empty($values))
217         {
218             $query = "INSERT INTO {$this->getRelationshipTable()} ($columns) VALUES ($values)";
219             DBManagerFactory::getInstance()->query($query);
220         }
221     }
222
223     /**
224      * @param $id id of row to update
225      * @param $values values to insert into row
226      * @return resource result of update satatement
227      */
228     protected function updateRow($id, $values)
229     {
230         $newVals = array();
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)
234         {
235             $newVals[] = "$field='$val'";
236         }
237
238         $newVals = implode(",",$newVals);
239
240         $query = "UPDATE {$this->getRelationshipTable()} set $newVals WHERE id='$id'";
241
242         return DBManagerFactory::getInstance()->query($query);
243     }
244
245     /**
246      * Removes one or more rows from the relationship table
247      * @param $where array of field=>value pairs to match
248      * @return bool|resource
249      */
250     protected function removeRow($where)
251     {
252         if (empty($where))
253             return false;
254
255         $date_modified = TimeDate::getInstance()->getNow()->asDb();
256         $stringSets = array();
257         foreach ($where as $field => $val)
258         {
259             $stringSets[] = "$field = '$val'";
260         }
261         $whereString = "WHERE " . implode(" AND ", $stringSets);
262
263         $query = "UPDATE {$this->getRelationshipTable()} set deleted=1 , date_modified = '$date_modified' $whereString";
264
265         return DBManagerFactory::getInstance()->query($query);
266
267     }
268
269     /**
270      * Checks for an existing row who's keys match the one passed in.
271      * @param  $row
272      * @return array|bool returns false if now row is found, otherwise the row is returned
273      */
274     protected function checkExisting($row)
275     {
276         $leftIDName = $this->def['join_key_lhs'];
277         $rightIDName = $this->def['join_key_rhs'];
278         if (empty($row[$leftIDName]) ||  empty($row[$rightIDName]))
279             return false;
280
281         $leftID = $row[$leftIDName];
282         $rightID = $row[$rightIDName];
283         //Check the relationship role as well
284         $roleCheck = $this->getRoleWhere();
285
286         $query = "SELECT * FROM {$this->getRelationshipTable()} WHERE $leftIDName='$leftID' AND $rightIDName='$rightID' $roleCheck AND deleted=0";
287
288         $db = DBManagerFactory::getInstance();
289         $result = $db->query($query);
290         $row = $db->fetchByAssoc($result);
291         if (!empty($row))
292         {
293             return $row;
294         } else{
295             return false;
296         }
297     }
298
299     /**
300      * Gets the relationship role column check for the where clause
301      * @param string $table
302      * @return string
303      */
304     protected function getRoleWhere($table = "", $ignore_role_filter = false)
305     {
306         $ignore_role_filter = $ignore_role_filter || $this->ignore_role_filter;
307         $roleCheck = "";
308         if (empty ($table))
309             $table = $this->getRelationshipTable();
310         if (!empty($this->def['relationship_role_column']) && !empty($this->def["relationship_role_column_value"]) && !$ignore_role_filter )
311         {
312             if (empty($table))
313                 $roleCheck = " AND $this->relationship_role_column";
314             else
315                 $roleCheck = " AND $table.{$this->relationship_role_column}";
316             //role column value.
317             if (empty($this->def['relationship_role_column_value']))
318             {
319                 $roleCheck.=' IS NULL';
320             } else {
321                 $roleCheck.= " = '$this->relationship_role_column_value'";
322             }
323         }
324         return $roleCheck;
325     }
326
327     /**
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
332      */
333     protected function getCustomLogicArguments($focus, $related, $link_name)
334     {
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;
342
343         return $custom_logic_arguments;
344     }
345
346     /**
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
351      * @return void
352      */
353     protected function callBeforeAdd($focus, $related, $link_name="")
354     {
355         $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
356         $focus->call_custom_logic('before_relationship_add', $custom_logic_arguments);
357     }
358
359     /**
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
364      * @return void
365      */
366     protected function callAfterAdd($focus, $related, $link_name="")
367     {
368         $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
369         $focus->call_custom_logic('after_relationship_add', $custom_logic_arguments);
370     }
371
372     /**
373      * @param  SugarBean $focus
374      * @param  SugarBean $related
375      * @param string $link_name
376      * @return void
377      */
378     protected function callBeforeDelete($focus, $related, $link_name="")
379     {
380         $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
381         $focus->call_custom_logic('before_relationship_delete', $custom_logic_arguments);
382     }
383
384     /**
385      * @param  SugarBean $focus
386      * @param  SugarBean $related
387      * @param string $link_name
388      * @return void
389      */
390     protected function callAfterDelete($focus, $related, $link_name="")
391     {
392         $custom_logic_arguments = $this->getCustomLogicArguments($focus, $related, $link_name);
393         $focus->call_custom_logic('after_relationship_delete', $custom_logic_arguments);
394     }
395
396     /**
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
400      * @return string
401      */
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]))
406                 return "";
407         }
408
409         return $optional_array['lhs_field']."".$optional_array['operator']."'".$optional_array['rhs_value']."'";
410     }
411
412     /**
413      * Adds a realted Bean to the list to be resaved along with the current bean.
414      * @static
415      * @param  SugarBean $bean
416      * @return void
417      */
418     public static function addToResaveList($bean)
419     {
420         if (!isset(self::$beansToResave[$bean->module_dir]))
421         {
422             self::$beansToResave[$bean->module_dir] = array();
423         }
424         self::$beansToResave[$bean->module_dir][$bean->id] = $bean;
425     }
426
427     /**
428      *
429      * @static
430      * @return void
431      */
432     public static function resaveRelatedBeans()
433     {
434         $GLOBALS['resavingRelatedBeans'] = true;
435
436         //Resave any bean not currently in the middle of a save operation
437         foreach(self::$beansToResave as $module => $beans)
438         {
439             foreach ($beans as $bean)
440             {
441                 if (empty($bean->deleted) && empty($bean->in_save))
442                 {
443                     $bean->save();
444                 }
445                 else
446                 {
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']))
449                     {
450                         $_SESSION['WORKFLOW_ALERTS']['id'] = $bean->id;
451                     }
452                 }
453             }
454         }
455
456         $GLOBALS['resavingRelatedBeans'] = false;
457
458         //Reset the list of beans that will need to be resaved
459         self::$beansToResave = array();
460     }
461
462     /**
463      * @return bool true if the relationship is a flex / parent relationship
464      */
465     public function isParentRelationship()
466     {
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")
470         {
471             return true;
472         }
473         return false;
474     }
475
476     public function __get($name)
477     {
478         if (isset($this->def[$name]))
479             return $this->def[$name];
480
481         switch($name)
482         {
483             case "relationship_type":
484                 return $this->type;
485             case 'relationship_name':
486                 return $this->name;
487             case "lhs_module":
488                 return $this->getLHSModule();
489             case "rhs_module":
490                 return $this->getRHSModule();
491             case "lhs_table" :
492                 isset($this->def['lhs_table']) ? $this->def['lhs_table'] : "";
493             case "rhs_table" :
494                 isset($this->def['rhs_table']) ? $this->def['rhs_table'] : "";
495             case "list_fields":
496                 return array('lhs_table', 'lhs_key', 'rhs_module', 'rhs_table', 'rhs_key', 'relationship_type');
497         }
498
499         if (isset($this->$name))
500             return $this->$name;
501
502         return null;
503     }
504 }