2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
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' ;
44 class ViewRelationship extends SugarView
47 * @see SugarView::_getModuleTitleParams()
49 protected function _getModuleTitleParams($browserTitle = false)
54 translate('LBL_MODULE_NAME','Administration'),
55 ModuleBuilderController::getModuleTitle(),
59 function overrideDefinitionFromPOST(
63 require_once 'modules/ModuleBuilder/parsers/relationships/AbstractRelationship.php' ;
64 foreach ( AbstractRelationship::$definitionKeys as $key )
66 if(!empty($_REQUEST ['ajaxLoad']) && in_array($key, array('label', 'rhs_label', 'lhs_label')) ){
69 if (! empty ( $_REQUEST [ $key ] ))
71 $definition [ $key ] = $_REQUEST [ $key ] ;
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 ) ;
85 if (!$this->fromModuleBuilder)
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'];
97 $mb = new ModuleBuilder ( ) ;
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 ) ;
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);
118 $cardinality = array ( MB_ONETOONE => translate ( 'LBL_ONETOONE' ) , MB_ONETOMANY => translate ( 'LBL_ONETOMANY' ) , MB_MANYTOONE=> translate ( 'LBL_MANYTOONE' ), MB_MANYTOMANY => translate ( 'LBL_MANYTOMANY' ), ) ;
120 if (!$this->fromModuleBuilder)
121 unset($cardinality[MB_MANYTOONE]);
123 $relationships = $module->getRelationships () ;
125 // if a description for this relationship already exists, then load it so it can be modified
126 if (! empty ( $_REQUEST [ 'relationship_name' ] ))
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 ;
138 if(!empty($_REQUEST['rhs_module'])){
139 $definition['rhs_label'] = $_REQUEST['rhs_module'];
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 ;
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 ) ;
154 if (empty($definition ['rhs_label']))
156 $definition ['rhs_label'] = translate($definition [ 'rhs_module' ]);
158 if (empty($definition ['lhs_label']))
160 $definition ['lhs_label'] = translate($definition [ 'lhs_module' ]);
162 $relationship = RelationshipFactory::newRelationship ( $definition ) ;
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);
168 if (empty ( $_REQUEST [ 'relationship_name' ] ))
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
173 // fix up the available cardinality options
174 $relationship_type = $relationship->getType () ;
175 if (count ( $lhs_subpanels ) == 0 || count ( $rhs_subpanels ) == 0)
177 unset ( $cardinality [ MB_MANYTOMANY ] ) ;
179 if (count ( $rhs_subpanels ) == 0)
181 unset ( $cardinality [ MB_ONETOMANY ] ) ;
184 if (isset ($definition['rhs_module']) && $definition['rhs_module'] == 'Activities') {
185 $cardinality = array( MB_ONETOMANY => translate ( 'LBL_ONETOMANY' ));
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 ] ) ;
193 if (isset ($definition['rhs_module']) && $definition['rhs_module'] == 'Campaigns' && isset($cardinality [ MB_MANYTOMANY ])) {
194 unset ( $cardinality [ MB_MANYTOMANY ] ) ;
195 unset ( $cardinality [ MB_MANYTOONE ] );
197 if (! isset($cardinality[$relationship->getType()]))
200 $definition [ 'relationship_type' ] = key ( $cardinality ) ;
201 $relationship = RelationshipFactory::newRelationship ( $definition ) ;
204 $this->smarty->assign ( 'is_new', true ) ;
206 $this->smarty->assign ( 'is_new', false ) ;
209 //Remove Activities if one-to-many is not availible
210 if (!isset($cardinality [ MB_ONETOMANY ]) && isset ($relatableModules['Activities'])) {
211 unset ($relatableModules['Activities']);
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 () ) ) ;
225 $relatable = array ( ) ;
226 foreach ( $relatableModules as $name => $dummy )
228 $relatable [ $name ] = translate ( $name ) ;
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());
239 switch ( $relationship->relationship_type)
245 if (empty ( $relationship->relationship_column_name ))
247 $validRoleColumnFields = array ( ) ;
248 foreach ( $fields as $field )
250 $validRoleColumnFields [] = $field ;
252 $this->smarty->assign ( 'relationship_role_column_enum', $validRoleColumnFields ) ;
254 if (! empty ( $relationship->relationship_role_column_value ))
255 $this->smarty->assign ( 'relationship_role_column_value', $relationship->relationship_role_column_value ) ;
258 if (! empty ( $relationship->relationship_role_column_value ))
259 $this->smarty->assign ( 'relationship_role_column_value', $relationship->relationship_role_column_value ) ;
263 //see if we use the new system
264 if (isset ( $_REQUEST [ 'json' ] ) && $_REQUEST [ 'json' ] == 'false')
266 echo $this->smarty->fetch ( 'modules/ModuleBuilder/tpls/studioRelationship.tpl' ) ;
269 $ac->addSection ( 'east', $module->name . ' ' . $GLOBALS [ 'mod_strings' ] [ 'LBL_RELATIONSHIPS' ], $this->smarty->fetch ( 'modules/ModuleBuilder/tpls/studioRelationship.tpl' ) ) ;
270 echo $ac->getJavascript () ;