]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Relationships/RelationshipHandler.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Relationships / RelationshipHandler.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
40
41
42
43
44
45
46 class RelationshipHandler extends Relationship {
47         
48         var $db;                                                        //Database link by reference
49         
50         var $base_module;                                       //name of module
51         var $base_bean;                                         //actual object
52         var $base_vardef_field;                         //base's vardef field name of relationship with rel1
53         
54         var $rel1_module;                                       //name of related module
55         var $rel1_bean;                                         //actual related object
56         var $rel1_relationship_name;            //Relationship name between base and rel1 
57         var $rel1_vardef_field;                         //rel1's vardef field name of relationship with rel2
58         var $rel1_vardef_field_base;            //rel1's vardef field name of relationship with base
59         
60         var $rel2_module;                                       //name of related related module
61         var $rel2_bean;                                         //actual related related object
62         var $rel2_relationship_name;            //Relationship name between rel1 and rel2 
63         var $rel2_vardef_field;                         //rel2's vardef field name of relationship with rel1
64         
65         
66         var $base_array;                                        //Info array
67         var $rel1_array;                                        //Info array
68         var $rel2_array;                                        //Info array
69         
70         
71         /*
72         
73         info arrays contain:
74         
75                 'slabel' ->             singular module name in correct language
76                 'plabel' ->     plural module name in correct language
77         
78         
79         
80         */
81         
82         
83 ///////////////////////////Setup and populate functions//////////////////////////////
84
85         function RelationshipHandler(& $db, $base_module=""){
86                 
87                 $this->db = $db;
88                 $this->base_module = $base_module;
89                 
90         //end function RelationshipHandler      
91         }       
92
93         function set_rel_vardef_fields($base_vardef_field, $rel1_vardef_field=""){
94                 
95                 $this->base_vardef_field = $base_vardef_field;
96                 $this->rel1_vardef_field = $rel1_vardef_field;
97                 
98         //end function set_rel_vardef_fields    
99         }       
100
101
102         function set_rel_relationship_names($build_rel2=false){
103
104                 $this->rel1_relationship_name = $this->base_bean->field_defs[$this->base_vardef_field]['relationship'];
105
106         if($build_rel2==true){
107                 $this->rel2_relationship_name = $this->rel1_bean->field_defs[$this->rel1_vardef_field]['relationship']; 
108         }       
109         
110         //end function set_rel_relationship_names
111         }
112
113
114
115
116 ///////////////////////////////END Setup and populate functions/////////////////////
117         
118         
119         /*
120         set the build_rel2 to true if you want the rel2 info array as well
121         This function will build all the relationship info it can based on values set in the setup functions
122         When you use the info arrays (rel1_array) or (rel2_array), make sure you always check for empty values
123         */
124         function build_info($build_rel2=false){
125                 if($this->base_bean == null){
126                         $this->base_bean = get_module_info($this->base_module);
127                 }       
128                 
129                 if(empty($this->rel1_bean)){
130                         $this->build_rel1_info();
131                         $this->rel1_module = $this->rel1_bean->module_dir;
132                 }
133                 
134                 if($build_rel2==true && $this->rel2_bean==""){
135                         $this->build_rel2_info();       
136                         $this->rel2_module = $this->rel2_bean->module_dir;
137                 }       
138                 
139                 //translate the module titles to the proper language
140                 $this->build_module_labels($build_rel2);
141
142         //end function build_info
143         }
144
145         function build_rel1_info(){
146
147                         $this->rel1_bean = $this->trace_relationship_module($this->base_module, $this->base_vardef_field);      
148
149         //end function build_rel1_info
150         }       
151         
152         function build_rel2_info(){
153
154                         $this->rel2_bean = $this->trace_relationship_module($this->base_module, $this->base_vardef_field, $this->rel1_vardef_field);
155
156         //end function build_rel1_info
157         }               
158         
159         /*
160         Translates the module names to their singular and plural label and puts them in 
161         the info arrays.  Does it for base, rel1, and rel2 if specified
162         */
163         
164         function build_module_labels($build_rel2=false){
165                 global $app_list_strings;
166
167                 ///Base Module Labels
168                 if(!empty($app_list_strings['moduleList'][$this->base_bean->module_dir])){
169                         $this->base_array['plabel'] = $app_list_strings['moduleList'][$this->base_bean->module_dir];
170                 } else {
171                         $this->base_array['plabel'] = $this->base_bean->module_dir;
172                 }       
173                 if(!empty($app_list_strings['moduleListSingular'][$this->base_bean->module_dir])){
174                         $this->base_array['slabel'] = $app_list_strings['moduleListSingular'][$this->base_bean->module_dir];
175                 } else {
176                         $this->base_array['slabel'] = $this->base_bean->object_name;
177                 }       
178
179                 ///Rel1 Module Labels   
180                 if(!empty($app_list_strings['moduleList'][$this->rel1_bean->module_dir])){
181                         $this->rel1_array['plabel'] = $app_list_strings['moduleList'][$this->rel1_bean->module_dir];
182                 } else {
183                         $this->rel1_array['plabel'] = $this->rel1_bean->module_dir;
184                 }
185                 
186                 if(!empty($app_list_strings['moduleListSingular'][$this->rel1_bean->module_dir])){
187                         $this->rel1_array['slabel'] = $app_list_strings['moduleListSingular'][$this->rel1_bean->module_dir];
188                 } else {
189                         $this->rel1_array['slabel'] = $this->rel1_bean->object_name;
190                 }       
191                         
192                 
193                 //Rel2 Module Labels
194                 if($build_rel2==true){
195                         
196                         if(!empty($app_list_strings['moduleList'][$this->rel2_bean->module_dir])){
197                                 $this->rel2_array['plabel'] = $app_list_strings['moduleList'][$this->rel2_bean->module_dir];
198                         } else {
199                                 $this->rel2_array['plabel'] = $this->rel2_bean->module_dir;
200                         }
201                         if(!empty($app_list_strings['moduleListSingular'][$this->rel2_bean->module_dir])){
202                                 $this->rel2_array['slabel'] = $app_list_strings['moduleListSingular'][$this->rel2_bean->module_dir];
203                         } else {
204                                 $this->rel2_array['slabel'] = $this->rel2_bean->module_dir;
205                         }       
206                 //end if build_rel2 is true             
207                 }                       
208
209         //end function buld_module_lables
210         }       
211         
212         
213         
214         
215         
216         function build_related_list($type="base"){
217                 //type can be base, rel1
218                 
219                 $target_list = "";
220                 
221                 if($type=="base"){
222                         $target_list = $this->base_bean->get_linked_beans($this->base_vardef_field, $this->rel1_bean->object_name);     
223                 //Possibility exists that this is a new relationship, so capture via relationship fields
224                         if(empty($target_list)){
225                                 $target_list = search_filter_rel_info($this->base_bean, $this->rel1_bean->module_dir, $this->base_vardef_field);
226                         //end if the target list is empty       
227                         }       
228                 }       
229                 
230                 if($type=="rel1"){
231                         $target_list = $this->rel1_bean->get_linked_beans($this->rel1_vardef_field, $this->rel2_bean->object_name);     
232                         
233                         //Possibility exists that this is a new relationship, so capture via relationship fields
234                         if(empty($target_list)){
235                                 $target_list = search_filter_rel_info($this->rel1_bean, $this->rel2_bean->module_dir, $this->rel1_vardef_field);
236                         //end if the target list is empty       
237                         }       
238                 }               
239
240                 return $target_list;
241                 
242         //end function build_related_list       
243         }       
244         
245         
246
247         
248 ///////BEGIN Functions to find relationships/////////////////////////////////   
249         
250 function get_relationship_information(& $target_bean, $get_upstream_rel_field_name = false){
251         
252         $target_module_name = $target_bean->module_dir;
253         $current_module_name = $this->base_module;
254         
255         //Look for downstream connection
256         $rel_array = $this->retrieve_by_sides($current_module_name, $target_module_name, $this->db);
257
258         
259         //Does a downstream relationship exist
260         if($rel_array!=null){
261                 if($rel_array['relationship_type']=="many-to-many"){
262                         $target_bean->$rel_array['join_key_lhs'] = $this->base_bean->id;
263                         if($rel_array['relationship_role_column']!=""){
264                                 $target_bean->$rel_array['relationship_role_column'] = $rel_array['relationship_role_column_value'];
265                         }                               
266                 //end if many-to-many   
267                 }       
268                 
269                 if($rel_array['relationship_type']=="one-to-many"){
270                         $target_bean->$rel_array['rhs_key'] = $this->base_bean->id;
271                         if($rel_array['relationship_role_column']!=""){
272                                 $target_bean->$rel_array['relationship_role_column'] = $rel_array['relationship_role_column_value'];
273                         }
274                 //end if one-to-many
275                 }
276                 
277                 return;         
278         //end if downstream relationship exists
279         }
280         
281         
282         
283         //Look for upstream connection
284         $rel_array = $this->retrieve_by_sides($target_module_name, $current_module_name, $this->db);
285         
286         //Does an upstream relationship exist
287         if($rel_array!=null){
288                 if($rel_array['relationship_type']=="many-to-many"){
289                         $target_bean->$rel_array['join_key_rhs'] = $this->base_bean->id;
290                         if($rel_array['relationship_role_column']!=""){
291                                 $target_bean->$rel_array['relationship_role_column'] = $rel_array['relationship_role_column_value'];
292                         }                               
293                 //end if many-to-many   
294                 }       
295                 
296                 if($rel_array['relationship_type']=="one-to-many"){
297                         $this->$rel_array['rhs_key'] = $this->base_bean->id;
298                         if($rel_array['relationship_role_column']!=""){
299                                 $this->$rel_array['relationship_role_column'] = $rel_array['relationship_role_column_value'];
300                         }
301                 //end if one-to-many
302                 }
303                 
304                 
305                 ///Fill additional id field if necessary
306                 if(($id_name = $this->traverse_rel_meta($current_module_name, $target_bean, $rel_array['relationship_name'])) != null){
307                         $target_bean->$id_name = $this->base_bean->id;
308             if($get_upstream_rel_field_name) {
309                 $target_bean->new_rel_relname = $id_name;
310                 $target_bean->new_rel_id = $this->base_bean->id;
311             }
312                 }
313         
314         //end if an upstream relationship exists
315         }               
316         
317 //end function get_relationship_information
318 }       
319
320 function traverse_rel_meta($base_module, & $target_bean, $target_rel_name){
321         $id_name = null;
322
323         //returns name of variable to store id in
324         //if none exists, then returns null
325         foreach($target_bean->field_defs as $field_array){
326                 
327                 if(!empty($field_array['relationship']) && $field_array['relationship']==$target_rel_name){
328                         
329                         $id_name = $this->get_id_name($target_bean, $field_array['name']);
330                         return $id_name;
331                 //end if rel name match         
332                 }       
333
334         //end foreach field def
335         }
336         
337         return null;
338         
339 //end function traverse_rel_meta
340 }       
341         
342
343 function get_id_name(& $target_bean, $field_name){
344         
345         foreach($target_bean->relationship_fields as $target_id => $rel_name){
346                 
347                 if($rel_name == $field_name){
348                         //relationship id found
349                         return $target_id;
350                 //end if match          
351                 }       
352         //end foreach   
353         }       
354         
355         return null;
356 //end function get_id_name      
357 }       
358
359 ///////////////////////////END functions to find relationships //////////////////////
360
361
362 function process_by_rel_bean($rel1_module){
363         
364         $this->rel1_relationship_name = $this->retrieve_by_modules($this->base_module, $rel1_module, $this->db);
365         $this->rel1_module = $rel1_module;
366         $this->rel1_bean = get_module_info($this->rel1_module);
367         
368 //end function process_by_rel_bean      
369 }       
370
371
372 function get_rel1_vardef_field_base($field_defs){
373         foreach($field_defs as $field_array){
374                 
375                 if(!empty($field_array['relationship']) && $field_array['relationship']==$this->rel1_relationship_name){
376                         
377                         $this->rel1_vardef_field_base = $field_array['name'];
378                 //end if rel name match         
379                 }       
380
381         //end foreach field def
382         }
383         
384         return null;
385         
386         
387 //end get_rel1_vardef_field_base
388 }
389
390
391 function get_farthest_reach(){
392         
393         if($this->rel1_vardef_field!=""){
394                 //the farthest reach is rel2
395                 $this->build_info(true);
396                 return $this->rel2_bean;
397         } 
398
399         //the farthest reach is rel1    
400         $this->build_info(false);
401         return $this->rel1_bean;                
402         
403 //end function get_farthest_reach       
404 }
405         
406 //end class RelationshipHandler 
407 }
408
409 ?>