Tuesday, September 07, 2010

Add support for hide/show subpanel at either end of relationship in module builder in SugarCrm 5.5.2

There may be situations where we do not want to show specific subpanel under specific module. In the official SugarCrm release there is no way to turn this on/off for a specific relationship between 2 modules in a package.

This hack describes how to hide/show (default is show) a subpanel at either end of a relationship. The hide/show setting is saved as part of the relationship information and is set on the Edit Relationship screen in Module Builder.

To use it go to Admin -> Module Builder. Select package, then module then relationships of module. Create or Edit relationship.


The hide subpanel checkbox will be shown for one-to-many relationships and many-many relationships. The check box will only be shown on the many side of a relationship. This means that on a many-one it is only shown on the "left" side and on a many-many relationship it is shown on both.

When any checkbox is checked, the relationship is saved and the package deployed the subpanel in question will not be shown for the module this change is relevant for.

This hack is not upgrade-safe.

This is the SVN path for a clean 5.5.2 code base with the changes applied.

Index: modules/ModuleBuilder/controller.php
===================================================================
--- modules/ModuleBuilder/controller.php (revision 1)
+++ modules/ModuleBuilder/controller.php (working copy)
@@ -168,7 +168,11 @@
             $zip = $mb->getPackage ( $load ) ;
             require_once ('ModuleInstall/PackageManager/PackageManager.php') ;
             $pm = new PackageManager ( ) ;
-            $info = $mb->packages [ $load ]->build ( false ) ;
+            
+            //HACK : force clean build
+            $info = $mb->packages [ $load ]->build ( false, true ) ;
+            //HACK : force clean build
+
             mkdir_recursive ( $GLOBALS [ 'sugar_config' ] [ 'cache_dir' ] . '/upload/upgrades/module/') ;
             rename ( $info [ 'zip' ], $GLOBALS [ 'sugar_config' ] [ 'cache_dir' ] . '/' . 'upload/upgrades/module/' . $info [ 'name' ] . '.zip' ) ;
             copy ( $info [ 'manifest' ], $GLOBALS [ 'sugar_config' ] [ 'cache_dir' ] . '/' . 'upload/upgrades/module/' . $info [ 'name' ] . '-manifest.php' ) ;
Index: modules/ModuleBuilder/parsers/relationships/AbstractRelationship.php
===================================================================
--- modules/ModuleBuilder/parsers/relationships/AbstractRelationship.php (revision 1)
+++ modules/ModuleBuilder/parsers/relationships/AbstractRelationship.php (working copy)
@@ -80,6 +80,7 @@
         'relationship_type' , 
         'relationship_role_column' , 
         'relationship_role_column_value' , 
+  'hideModuleSubpanel' ,// - hide subpanel  to store comma separated string of module name and relationship name.
         'reverse' ) ;
 
     /*
@@ -268,6 +269,18 @@
       array('widget_class' => "SubPanelTopCreateButton"),
       array('widget_class' => 'SubPanelTopSelectButton', 'mode'=>'MultiSelect')
   );
+  // Start: Module builder - edit relationship - new checkbox - hide subpanel
+     // Following is written to get comma separated string of module name and relationship name
+  // This string contains module name and relationship name of subpanel which we want to hide.
+     // We unset array of subpanel present in string. 
+  $pieces = explode(",",$this->definition['hideModuleSubpanel']);
+  for($i=0;$i<count($pieces);$i++){ 
+   if($sourceModule==$pieces[$i] && $relationshipName==$pieces[$i+1]){
+    unset($subpanelDefinition);
+    $subpanelDefinition=array();
+   }
+  }  
+  /* End */
         
         return array ( $subpanelDefinition );
     }   
Index: modules/ModuleBuilder/parsers/relationships/AbstractRelationships.php
===================================================================
--- modules/ModuleBuilder/parsers/relationships/AbstractRelationships.php (revision 1)
+++ modules/ModuleBuilder/parsers/relationships/AbstractRelationships.php (working copy)
@@ -152,6 +152,14 @@
         }
         }
         
+  // Start: Module builder - edit relationship - new checkbox - hide subpanel
+  // Following is written to make comma separated string of module name and relationship name
+  // This string contains module name and relationship name of subpanel which we want to hide.
+  if($_REQUEST['moduleName']!=""){
+   $hideModuleSubpanel=implode(",",$_REQUEST['moduleName']);
+   $definition['hideModuleSubpanel']=$hideModuleSubpanel;
+  }     
+  // End
         $newRelationship = RelationshipFactory::newRelationship ( $definition ) ;
         // TODO: error handling in case we get a badly formed definition and hence relationship
         $this->add ( $newRelationship ) ;
@@ -255,7 +263,9 @@
      * @param AbstractRelationship The relationship object
      * @return string A globally unique relationship name
      */
-    protected function getUniqueName ($relationship)
+    //HACK : Hide Subpanel, it made public so it can be accessed from outside.
+    public function getUniqueName ($relationship)
+    //HACK : Hide Subpanel
     {
         $allRelationships = $this->getRelationshipList () ;
         $basename = $relationship->getName () ;
Index: modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php
===================================================================
--- modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php (revision 1)
+++ modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php (working copy)
@@ -281,22 +281,29 @@
     protected function saveSubpanelDefinitions ($basepath , $installDefPrefix , $relationshipName , $subpanelDefinitions)
     {
         mkdir_recursive ( "$basepath/layoutdefs/" ) ;
+  //HACK : Hide subpanel
         
         foreach ( $subpanelDefinitions as $moduleName => $definitions )
         {
             $filename = "$basepath/layoutdefs/{$moduleName}.php" ;
-            
+            $addToInstallDefs = false;
             foreach ( $definitions as $definition )
             {
                $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->saveSubpanelDefinitions(): saving the following to {$filename}" . print_r ( $definition, true ) ) ;
                if (empty($definition ['get_subpanel_data']) || $definition ['subpanel_name'] == 'history' ||  $definition ['subpanel_name'] == 'activities') {
                  $definition ['get_subpanel_data'] = $definition ['subpanel_name'];
                }
+               
+               if(!empty($definition ['get_subpanel_data'])){
                write_array_to_file ( 'layout_defs["' . $moduleName . '"]["subpanel_setup"]["' . strtolower ( $definition [ 'get_subpanel_data' ] ) . '"]', $definition, $filename, "a" ) ;
+                $addToInstallDefs = true;
+               } 
             }
-            
-            $installDefs [ $moduleName ] = array ( 'from' => "{$installDefPrefix}/relationships/layoutdefs/{$moduleName}.php" , 'to_module' => $moduleName ) ;
+            if($addToInstallDefs){
+             $installDefs [ $moduleName ] = array ( 'from' => "{$installDefPrefix}/relationships/layoutdefs/{$moduleName}.php" , 'to_module' => $moduleName ) ;
+         }
         }
+  //HACK : Hide subpanel
         return $installDefs ;
     }
 
Index: modules/ModuleBuilder/tpls/studioRelationship.tpl
===================================================================
--- modules/ModuleBuilder/tpls/studioRelationship.tpl (revision 1)
+++ modules/ModuleBuilder/tpls/studioRelationship.tpl (working copy)
@@ -150,6 +150,49 @@
     
     
             </tr>
+            {*HACK : Hide subpanel checkbox *}
+            {if $rel.relationship_type == 'one-to-many' }
+            <tr>
+                <td>&nbsp;</td>
+                <td>&nbsp;</td>
+                <td>&nbsp;</td>
+
+                <td align="right" scope="row">Hide {sugar_translate label=$rel.rhs_module} Subpanel On {sugar_translate label=$rel.lhs_module}</td>
+                <td><input type="checkbox" name="moduleName[]" id="moduleName[]" value="{$rel.rhs_module},{$rel_name},"
+                {if in_array($rel.rhs_module,$moduleName)} checked {/if} 
+                /></td>
+
+                <td>&nbsp;</td>
+                <td>&nbsp;</td>
+            </tr>
+            {/if}            
+            {if $rel.relationship_type == 'many-to-many' }
+                <tr>
+                <td align="right" scope="row">Hide {sugar_translate label=$rel.lhs_module} Subpanel On {sugar_translate label=$rel.rhs_module}</td>
+                <td><input type="checkbox" name="moduleName[]" id="moduleName[]" value="{$rel.lhs_module},{$rel_name}," 
+                {if in_array($rel.lhs_module,$moduleName)} checked {/if}  /></td>
+                <td>&nbsp;</td>
+
+                <td align="right" scope="row">Hide {sugar_translate label=$rel.rhs_module} Subpanel On {sugar_translate label=$rel.lhs_module}</td>
+                <td><input type="checkbox" name="moduleName[]" id="moduleName[]" value="{$rel.rhs_module},{$rel_name},"
+                {if in_array($rel.rhs_module,$moduleName)} checked {/if}  /></td>
+
+                <td>&nbsp;</td>
+                <td>&nbsp;</td>
+                </tr>            
+
+            {/if}
+            {if $rel.relationship_type == 'many-to-one' }
+                <tr>
+                <td align="right" scope="row">Hide {sugar_translate label=$rel.lhs_module} Subpanel On {sugar_translate label=$rel.rhs_module}</td>
+                <td><input type="checkbox" name="moduleName[]" id="moduleName[]" value="{$rel.lhs_module},{$rel_name}," 
+                {if in_array($rel.lhs_module,$moduleName)} checked {/if}  /></td>
+                <td>&nbsp;</td>
+                <td>&nbsp;</td>
+                <td>&nbsp;</td>
+                </tr>            
+            {/if}            
+            {*HACK : Hide subpanel checkbox *}
    <tr>
                 {* add in the extended relationship condition *}
                 {* comment out for now as almost no expressed need for this - to revert, uncomment and test, test, test...
Index: modules/ModuleBuilder/views/view.relationship.php
===================================================================
--- modules/ModuleBuilder/views/view.relationship.php (revision 1)
+++ modules/ModuleBuilder/views/view.relationship.php (working copy)
@@ -194,6 +194,22 @@
         $this->smarty->assign ( 'translated_cardinality', $cardinality ) ;
         $this->smarty->assign ( 'selected_cardinality', translate ( $relationship->getType () ) ) ;
         
+        // Start: Module builder - edit relationship - new checkbox - hide subpanel
+  // Following is written to get comma separated string of module name and relationship name
+     // This string contains module name and relationship name of subpanel which we want to hide.
+  // We pass this string to stdioRelationship.tpl file to show checkbox check depending 
+  // checkbox check by user .
+    $moduleName=explode(",",$relationship->hideModuleSubpanel);
+    $this->smarty->assign ( 'moduleName',$moduleName) ;
+
+        if(empty($relationship->relationship_name)){             
+            //$rel_name = $relationship->lhs_module.'_'.$relationship->rhs_module;
+            $rel_name = $relationships->getUniqueName($relationship);
+        } else {
+            $rel_name = $relationship->relationship_name;
+        }
+        $this->smarty->assign ('rel_name', $rel_name) ;
+  // End  
         $relatable = array ( ) ;
         foreach ( $relatableModules as $name => $dummy )
         {

You can download the modified core files and the SVN diff/patch file here HideSubpanel.zip.


No comments: