Friday, August 06, 2010

SugarCrm - Make Module Builder language files Subversion friendly

This posting is written based on the SugarCrm 5.5.2 codebase.

Every time you export a package from Module Builder the contents of the application / language files are randomly exported making it almost impossible to compare the export against existing code in Subversion.

In professional projects custom packages are exported and kept in SVN for improving team collaboration, tracking changes and maintenance. Every time you export a package after performing activities (in module builder) which can cause changes in application language file or module language file then the contents of the application/language files are found to be randomly placed. The contents are not sorted so it makes the task of merging or comparing with the previous versions difficult as order cannot be guessed /determined specially with large files. The situation is similar as finding a word from the dictionary if dictionary is not sorted in alphabetical/logical order.

This difficulty can be solved by sorting the contents of the application/language files in natural order before saving them to the application/language files.

This change is specifically for Module Builder only.

Screenshot for a typical language file




Screenshot for a typical language file after fix 



This proposed change to the SugarCrm 5.5.2 code is not upgrade safe.

You need to change the following files


1./include/utils/array_utils.php
A new utility function array_sort_keys() is added in this file.


/*Hack:When exporting from MB the content of the application language files are not ordered*/
/**
* This is used for natuaral sorting algorithm for the array.
* The prefered way of using the function is to use  array_sort_keys($array);
* @param mixed $array  The array to be sorted
* @param mixed $function   The function used for sorting.
*/
function array_sort_keys(&$array,$function='strnatcasecmp'){
    uksort($array, $function);
    foreach(array_keys($array) as $k)
    {
        if(gettype($array[$k])=="array")
        {
            array_sort_keys($array[$k], $function);
        }
    }
} 
/*Hack:When exporting from MB the content of the application language files are not ordered*/

2./include/utils/modules/ModuleBuilder/MB/MBLanguage.php
This array_sort_keys() function is called within MBLanguage::save function.


foreach($required as $k=>$v){
  if(empty($values[$k]) || $renameLang){
   $values[$k] = $v;   
  }
 }
 //Hack: When exporting from MB the content of the application language files are not ordered 
 array_sort_keys($values);
 //Hack: When exporting from MB the content of the application language files are not ordered 
 write_array_to_file('mod_strings', $values, $save_path .'/'.$lang,'w', $header);
}
$app_save_path = $this->path . '/../../language/application';
mkdir_recursive($app_save_path);
$key_changed = ($this->key_name != $key_name);

foreach($this->appListStrings as $lang=>$values){
 if(!$duplicate){
  unset($values['moduleList'][$this->key_name]);
 }
 $values['moduleList'][$key_name]= $this->label;
 $appFile = $header. "\n";
 require_once('include/utils/array_utils.php');
 $this->getGlobalAppListStringsForMB($values);
 //Hack: When exporting from MB the content of the application language files are not ordered
 array_sort_keys($values);
 //Hack: When exporting from MB the content of the application language files are not ordered


And here is the subversion diff / patch file
Index: include/utils/array_utils.php
===================================================================
--- include/utils/array_utils.php (revision 1)
+++ include/utils/array_utils.php (working copy)
@@ -271,4 +271,22 @@
     }
 }
 
+/*Hack:When exporting from MB the content of the application language files are not ordered*/
+/**
+* This is used for natuaral sorting algorithm for the array.
+* The prefered way of using the function is to use  array_sort_keys($array);
+* @param mixed $array  The array to be sorted
+* @param mixed $function   The function used for sorting.
+*/
+function array_sort_keys(&$array,$function='strnatcasecmp'){
+    uksort($array, $function);
+    foreach(array_keys($array) as $k)
+    {
+        if(gettype($array[$k])=="array")
+        {
+            array_sort_keys($array[$k], $function);
+        }
+    }
+} 
+/*Hack:When exporting from MB the content of the application language files are not ordered*/
 ?>
Index: modules/ModuleBuilder/MB/MBLanguage.php
===================================================================
--- modules/ModuleBuilder/MB/MBLanguage.php (revision 1)
+++ modules/ModuleBuilder/MB/MBLanguage.php (working copy)
@@ -172,6 +172,9 @@
       $values[$k] = $v;   
      }
     }
+    //Hack:When exporting from MB the content of the application language files are not ordered
+    array_sort_keys($values);
+    //Hack:When exporting from MB the content of the application language files are not ordered
     write_array_to_file('mod_strings', $values, $save_path .'/'.$lang,'w', $header);
    }
    $app_save_path = $this->path . '/../../language/application';
@@ -186,6 +189,9 @@
     $appFile = $header. "\n";
     require_once('include/utils/array_utils.php');
     $this->getGlobalAppListStringsForMB($values);
+    //Hack:When exporting from MB the content of the application language files are not ordered
+    array_sort_keys($values);
+    //Hack:When exporting from MB the content of the application language files are not ordered
     foreach($values as $key=>$array){
      if($duplicate){
       //keep the original when duplicating

No comments: