]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/views/view.relationship.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / views / view.relationship.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 require_once ('modules/ModuleBuilder/MB/AjaxCompose.php') ;
38 require_once ('modules/ModuleBuilder/MB/ModuleBuilder.php') ;
39 require_once ('modules/ModuleBuilder/Module/StudioModuleFactory.php') ;
40 require_once ('modules/ModuleBuilder/Module/StudioBrowser.php') ;
41 require_once ('modules/ModuleBuilder/parsers/constants.php') ;
42 require_once 'modules/ModuleBuilder/parsers/relationships/RelationshipFactory.php' ;
43
44 class ViewRelationship extends SugarView
45 {
46     /**
47          * @see SugarView::_getModuleTitleParams()
48          */
49         protected function _getModuleTitleParams($browserTitle = false)
50         {
51             global $mod_strings;
52             
53         return array(
54            translate('LBL_MODULE_NAME','Administration'),
55            ModuleBuilderController::getModuleTitle(),
56            );
57     }
58
59     function overrideDefinitionFromPOST(
60         $definition
61         )
62     {
63         require_once 'modules/ModuleBuilder/parsers/relationships/AbstractRelationship.php' ;
64         foreach ( AbstractRelationship::$definitionKeys as $key )
65         {
66                 if(!empty($_REQUEST ['ajaxLoad']) && in_array($key, array('label', 'rhs_label', 'lhs_label')) ){
67                         continue;
68                 }
69             if (! empty ( $_REQUEST [ $key ] ))
70             {
71                 $definition [ $key ] = $_REQUEST [ $key ] ;
72             }
73         }
74         return $definition ;
75     }
76
77     function display()
78     {
79         $selected_lang = (!empty($_REQUEST['relationship_lang'])?$_REQUEST['relationship_lang']:$_SESSION['authenticated_user_language']);
80         $this->smarty = new Sugar_Smarty ( ) ;
81         $ac = new AjaxCompose ( ) ;
82         $this->fromModuleBuilder = isset ( $_REQUEST [ 'MB' ] ) || (!empty ( $_REQUEST [ 'view_package' ] ) && $_REQUEST [ 'view_package' ] != 'studio') ;
83         $this->smarty->assign ( 'fromModuleBuilder', $this->fromModuleBuilder ) ;
84         
85         if (!$this->fromModuleBuilder)
86         {
87             $module = StudioModuleFactory::getStudioModule( $_REQUEST [ 'view_module' ] ) ;
88             $moduleName = $_REQUEST [ 'view_module' ] ;
89             $fields = $module->fields ;
90             require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
91             $relatableModules = DeployedRelationships::findRelatableModules () ;
92                 $appStrings = return_app_list_strings_language( $selected_lang ) ;
93                 $modStrings = return_module_language( $selected_lang, $_REQUEST [ 'view_module' ], true ) ;
94                 $appStrings = $appStrings['moduleList'];
95         } else
96         {
97             $mb = new ModuleBuilder ( ) ;
98                         $mb->getPackages();
99                         //display the latest module name rather than what is in or not in the loaded app_list_strings.
100                         $mb->getPackage($_REQUEST ['view_package'])->loadModuleTitles();
101             $module = $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
102             $moduleName = empty($module->key_name) ? $module->getModuleName() : $module->key_name;
103             $this->smarty->assign ( 'view_package', $_REQUEST [ 'view_package' ] ) ;
104             $mbvardefs = $module->getVardefs () ;
105             $fields = $mbvardefs [ 'fields' ] ;
106             require_once 'modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php' ;
107             $relatableModules = UndeployedRelationships::findRelatableModules () ;
108             $appStrings = $module->getModStrings( $selected_lang ) ;
109         }
110         
111         ksort( $relatableModules ) ;
112         $lhs_subpanels = $module->getProvidedSubpanels () ;
113         // Fix to re-add sorting of the subpanel names so that the 'default' subpanel always appears first in the list. 
114         // This assumes that subpanels are usually named ForXYZ which is the case currently, and hence 'default' will be sorted first. 
115         //I f this assumption is incorrect, then a better solution would be to remove 'default' from the subpanel list, then sort, and finally array_unshift it back on.
116         natcasesort($lhs_subpanels);
117                 
118         $cardinality = array ( MB_ONETOONE => translate ( 'LBL_ONETOONE' ) , MB_ONETOMANY => translate ( 'LBL_ONETOMANY' ) , MB_MANYTOONE=> translate ( 'LBL_MANYTOONE' ), MB_MANYTOMANY => translate ( 'LBL_MANYTOMANY' ), ) ;
119         
120         if (!$this->fromModuleBuilder)
121             unset($cardinality[MB_MANYTOONE]);
122             
123         $relationships = $module->getRelationships () ;
124         
125         // if a description for this relationship already exists, then load it so it can be modified
126         if (! empty ( $_REQUEST [ 'relationship_name' ] ))
127         {
128             $relationship = $relationships->get ( $_REQUEST [ 'relationship_name' ] ) ;
129             $relationship->setName($_REQUEST [ 'relationship_name' ] );
130             $definition = $relationship->getDefinition();
131             if (!$this->fromModuleBuilder){
132                         $modStrings = return_module_language( $selected_lang, $relationship->rhs_module, true ) ;
133                     $definition['lhs_label'] = isset($modStrings[$relationship->getTitleKey()])?$modStrings[$relationship->getTitleKey()] : $relationship->lhs_module;
134                     $modStrings = return_module_language( $selected_lang, $relationship->lhs_module, true ) ;
135                         $definition['rhs_label'] = isset($modStrings[$relationship->getTitleKey(true)])?$modStrings[$relationship->getTitleKey(true)] : $relationship->rhs_module ;
136                         }else{
137                                 #30624
138                                 if(!empty($_REQUEST['rhs_module'])){
139                                         $definition['rhs_label'] = $_REQUEST['rhs_module'];
140                                 }
141                         }
142         } else
143         {
144             $definition = array ( ) ;
145             $firstModuleDefinition = each ( $relatableModules ) ;
146             $definition [ 'rhs_module' ] = $firstModuleDefinition [ 'key' ] ;
147             $definition [ 'lhs_module' ] = $moduleName ;
148             $definition [ 'lhs_label' ] = translate($moduleName);
149             $definition [ 'relationship_type' ] = MB_MANYTOMANY ;
150         }
151         // load the relationship from post - required as we can call view.relationship.php from Ajax when changing the rhs_module for example           
152         $definition = $this->overrideDefinitionFromPOST ( $definition ) ;
153         
154         if (empty($definition ['rhs_label']))
155         {
156                 $definition ['rhs_label'] = translate($definition [ 'rhs_module' ]);
157         }
158         if (empty($definition ['lhs_label']))
159         {
160             $definition ['lhs_label'] = translate($definition [ 'lhs_module' ]);
161         }
162         $relationship = RelationshipFactory::newRelationship ( $definition ) ;
163         
164         $rhs_subpanels = $relatableModules [ $relationship->rhs_module ] ;
165         // Fix to re-add sorting of the subpanel names so that the 'default' subpanel always appears first in the list. This assumes that subpanels are usually named ForXYZ which is the case currently, and hence 'default' will be sorted first. If this assumption is incorrect, then a better solution would be to remove 'default' from the subpanel list, then sort, and finally array_unshift it back on.
166         natcasesort($rhs_subpanels);
167         
168         if (empty ( $_REQUEST [ 'relationship_name' ] ))
169         {
170             // tidy up the options for the view based on the modules participating in the relationship and the cardinality
171             // some modules (e.g., Knowledge Base/KBDocuments) lack subpanels. That means they can't be the lhs of a 1-many or many-many, or the rhs of a many-many for example          
172
173             // fix up the available cardinality options
174             $relationship_type = $relationship->getType () ;
175             if (count ( $lhs_subpanels ) == 0 || count ( $rhs_subpanels ) == 0)
176             {
177                 unset ( $cardinality [ MB_MANYTOMANY ] ) ;
178             }
179             if (count ( $rhs_subpanels ) == 0)
180             {
181                 unset ( $cardinality [ MB_ONETOMANY ] ) ;
182             }
183
184             if (isset ($definition['rhs_module']) && $definition['rhs_module'] == 'Activities') {
185                 $cardinality = array( MB_ONETOMANY => translate ( 'LBL_ONETOMANY' ));
186             }
187             //Bug 23139, Campaigns module current cannot display custom subpanels, so we need to ban it from any
188             //relationships that would require a new subpanel to be shown in Campaigns.
189                 if (isset ($definition['lhs_module']) && $definition['lhs_module'] == 'Campaigns') {
190                 unset ( $cardinality [ MB_MANYTOMANY ] ) ;
191                 unset ( $cardinality [ MB_ONETOMANY ] ) ;
192             }
193                 if (isset ($definition['rhs_module']) && $definition['rhs_module'] == 'Campaigns' && isset($cardinality [ MB_MANYTOMANY ])) {
194                 unset ( $cardinality [ MB_MANYTOMANY ] ) ;
195                 unset ( $cardinality [ MB_MANYTOONE ] );
196             }
197             if (! isset($cardinality[$relationship->getType()]))
198             {
199                 end ($cardinality) ;
200                 $definition [ 'relationship_type' ] = key ( $cardinality ) ;
201                 $relationship = RelationshipFactory::newRelationship ( $definition ) ;
202             }
203             
204             $this->smarty->assign ( 'is_new', true ) ;
205         } else {
206                 $this->smarty->assign ( 'is_new', false ) ;
207         }
208         
209         //Remove Activities if one-to-many is not availible
210         if (!isset($cardinality [ MB_ONETOMANY ]) && isset ($relatableModules['Activities'])) {
211                 unset ($relatableModules['Activities']);
212         }
213         
214         
215         // now enforce the relationship_only requirement - that is, only construct the underlying relationship and link fields, and not the UI, if the subpanel code will have troubles displaying the UI                
216         $relationships->enforceRelationshipOnly ( $relationship ) ;
217         $this->smarty->assign ( 'view_module', $_REQUEST['view_module'] ) ;
218         $this->smarty->assign ( 'rel', $relationship->getDefinition () ) ;
219         $this->smarty->assign ( 'mod_strings', $GLOBALS [ 'mod_strings' ] ) ;
220         $this->smarty->assign ( 'module_key', $relationship->lhs_module ) ;
221         $this->smarty->assign ( 'cardinality', array_keys ( $cardinality ) ) ;
222         $this->smarty->assign ( 'translated_cardinality', $cardinality ) ;
223         $this->smarty->assign ( 'selected_cardinality', translate ( $relationship->getType () ) ) ;
224         
225         $relatable = array ( ) ;
226         foreach ( $relatableModules as $name => $dummy )
227         {
228             $relatable [ $name ] = translate ( $name ) ;
229         }
230         unset($relatable [ 'KBDocuments' ] ) ;
231         natcasesort ( $relatable ) ;
232         $this->smarty->assign ( 'relatable', array_keys ( $relatable ) ) ;
233         $this->smarty->assign ( 'translated_relatable', $relatable ) ;
234         $this->smarty->assign ( 'rhspanels', $rhs_subpanels ) ;
235         $this->smarty->assign ( 'lhspanels', $lhs_subpanels ) ;
236         $this->smarty->assign('selected_lang', $selected_lang);
237                 $this->smarty->assign('available_languages',get_languages());
238         
239         switch ( $relationship->relationship_type)
240         {
241             case MB_ONETOONE :
242                 break ;
243             
244             case MB_ONETOMANY :
245                 if (empty ( $relationship->relationship_column_name ))
246                 {
247                     $validRoleColumnFields = array ( ) ;
248                     foreach ( $fields as $field )
249                     {
250                         $validRoleColumnFields [] = $field ;
251                     }
252                     $this->smarty->assign ( 'relationship_role_column_enum', $validRoleColumnFields ) ;
253                 }
254                 if (! empty ( $relationship->relationship_role_column_value ))
255                     $this->smarty->assign ( 'relationship_role_column_value', $relationship->relationship_role_column_value ) ;
256                 break ;
257             case MB_MANYTOMANY :
258                 if (! empty ( $relationship->relationship_role_column_value ))
259                     $this->smarty->assign ( 'relationship_role_column_value', $relationship->relationship_role_column_value ) ;
260                 break ;
261         }
262         
263         //see if we use the new system
264         if (isset ( $_REQUEST [ 'json' ] ) && $_REQUEST [ 'json' ] == 'false')
265         {
266             echo $this->smarty->fetch ( 'modules/ModuleBuilder/tpls/studioRelationship.tpl' ) ;
267         } else
268         {
269             $ac->addSection ( 'east', $module->name . ' ' . $GLOBALS [ 'mod_strings' ] [ 'LBL_RELATIONSHIPS' ], $this->smarty->fetch ( 'modules/ModuleBuilder/tpls/studioRelationship.tpl' ) ) ;
270             echo $ac->getJavascript () ;
271         }
272     }
273 }