]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/MB/MBPackage.php
Merge pull request #100 from collinlee/master
[Github/sugarcrm.git] / modules / ModuleBuilder / MB / MBPackage.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/MBModule.php');
38
39 class MBPackage{
40     var $name;
41     var $is_uninstallable = true;
42     var $description = '';
43     var $has_images = true;
44     var $modules = array();
45     var $date_modified = '';
46     var $author = '';
47     var $key = '';
48     var $readme='';
49     function MBPackage($name){
50         $this->name = $name;
51         $this->load();
52         
53     }
54     function loadModules($force=false){
55         if(!file_exists(MB_PACKAGE_PATH . '/' . $this->name .'/modules'))return;
56         $d = dir(MB_PACKAGE_PATH . '/' . $this->name .'/modules');
57         while($e = $d->read()){
58             if(substr($e, 0, 1) != '.' && is_dir(MB_PACKAGE_PATH . '/'. $this->name. '/modules/' . $e)){
59                 $this->getModule($e, $force);
60             }
61         }
62     }
63     
64     /**
65      * Loads the translated module titles from the selected language into.
66      * Will override currently loaded string to reflect undeployed label changes.
67      * $app_list_strings
68      * @return 
69      * @param $languge String language identifyer
70      */
71     function loadModuleTitles($languge = '') 
72     {
73         if (empty($language))
74         {
75             $language = $GLOBALS['current_language'];
76         }
77         global $app_list_strings;
78         $packLangFilePath = $this->getPackageDir() . "/language/application/" . $language . ".lang.php";
79         if (file_exists($packLangFilePath))
80         {
81             
82             require($packLangFilePath);
83         }
84     }
85     
86     function getModule($name, $force=true){
87         if(!$force && !empty($this->modules[$name]))return;
88         $path = $this->getPackageDir();
89         
90         $this->modules[$name] = new MBModule($name, $path, $this->name, $this->key);
91     }
92     
93     function deleteModule($name){
94         $this->modules[$name]->delete();
95         unset($this->modules[$name]);
96     }
97     
98 function getManifest($version_specific = false, $for_export = false){
99     //If we are exporting the package, we must ensure a different install key
100     $pre = $for_export ? MB_EXPORTPREPEND : "";
101     $date = TimeDate::getInstance()->nowDb();
102     $time = time();
103     $this->description = to_html($this->description);
104     $is_uninstallable = ($this->is_uninstallable ? true : false);
105     if($GLOBALS['sugar_flavor'] == 'CE') {
106         $flavors = array('CE','PRO','ENT');
107     } else {
108         $flavors = array($GLOBALS['sugar_flavor']);
109     }
110     $version = (!empty($version_specific))?$GLOBALS['sugar_version']:'';
111
112     // Build an array and use var_export to build this file
113     $manifest = array(
114         array('acceptable_sugar_versions' => array($version)),
115         array('acceptable_sugar_flavors' => $flavors),
116         'readme' => $this->readme,
117         'key' => $this->key,
118         'author' => $this->author,
119         'description' => $this->description,
120         'icon' => '',
121         'is_uninstallable' => $is_uninstallable,
122         'name' => $pre.$this->name,
123         'published_date' => $date,
124         'type' => 'module',
125         'version' => $time,
126         'remove_tables' => 'prompt',
127     );
128         
129               
130
131     $header = file_get_contents('modules/ModuleBuilder/MB/header.php');
132     
133     return $header."\n// THIS CONTENT IS GENERATED BY MBPackage.php\n".'$manifest = '.var_export_helper($manifest).";\n\n";
134 /*
135     return  <<<EOQ
136     $header
137     \$manifest = array (
138          'acceptable_sugar_versions' => 
139           array (
140             $version
141           ),
142           'acceptable_sugar_flavors' =>
143           array(
144             $flavor
145           ),
146           'readme'=>'$this->readme',
147           'key'=>'$this->key',
148           'author' => '$this->author',
149           'description' => '$this->description',
150           'icon' => '',
151           'is_uninstallable' => $is_uninstallable,
152           'name' => '$pre$this->name',
153           'published_date' => '$date',
154           'type' => 'module',
155           'version' => '$time',
156           'remove_tables' => 'prompt',
157           );
158 EOQ;
159 */
160 }
161     
162 function buildInstall($path){
163     $installdefs = array ('id' => $this->name,
164         'beans'=>array(),
165         'layoutdefs'=>array(),
166         'relationships'=>array(),
167     );
168     if($this->has_images){
169         $installdefs['image_dir'] = '<basepath>/icons'; 
170     }
171     foreach(array_keys($this->modules) as $module){
172         $this->modules[$module]->build($path);
173         $this->modules[$module]->addInstallDefs($installdefs);
174     }
175     $this->path = $this->getPackageDir(); 
176     if(file_exists($this->path . '/language')){
177         $d= dir($this->path . '/language');
178         while($e = $d->read()){
179             $lang_path = $this->path .'/language/' . $e;
180             if(substr($e, 0, 1) != '.' && is_dir($lang_path)){
181                 $f = dir($lang_path);
182                 while($g = $f->read()){
183                     if(substr($g, 0, 1) != '.' && is_file($lang_path.'/'. $g)){
184                         $lang = substr($g, 0, strpos($g, '.'));
185                         $installdefs['language'][] = array(
186                         'from'=> '<basepath>/SugarModules/language/'.$e . '/'. $g,
187                         'to_module'=> $e,
188                         'language'=> $lang  
189                         );
190                     }
191                 }
192             }
193         }
194             
195         copy_recursive( $this->path . '/language/', $path . '/language/');
196         $icon_path = $path . '/../icons/default/images/';
197         mkdir_recursive($icon_path);
198         copy_recursive($this->path . '/icons/', $icon_path);
199     }
200     return "\n".'$installdefs = ' . var_export_helper($installdefs). ';';
201
202 }
203     
204     function getPackageDir(){
205         return MB_PACKAGE_PATH . '/' . $this->name;
206     }
207     
208     function getBuildDir(){
209         return MB_PACKAGE_BUILD . '/' . $this->name;
210     }
211     
212     function getZipDir(){
213         return $this->getPackageDir() . '/zips';
214     }
215     
216     
217     function load(){
218         $path = $this->getPackageDir();
219         if(file_exists($path .'/manifest.php')){
220             require($path . '/manifest.php');
221             if(!empty($manifest)){
222                 $this->date_modified = $manifest['published_date'];
223                 $this->is_uninstallable = $manifest['is_uninstallable'];
224                 $this->author = $manifest['author'];
225                 $this->key = $manifest['key'];
226                 $this->description = $manifest['description'];
227                 if(!empty($manifest['readme']))
228                     $this->readme = $manifest['readme'];
229             }
230         }
231         $this->loadModules(true);
232     }
233
234     function save(){
235         $path = $this->getPackageDir();
236         if(mkdir_recursive($path)){
237             //Save all the modules when we save a package
238             $this->updateModulesMetaData(true);
239             sugar_file_put_contents_atomic($path .'/manifest.php', $this->getManifest());
240         }
241     }
242     
243     function build($export=true, $clean = false){
244         $this->loadModules();
245         require_once('include/utils/zip_utils.php');
246         $package_path = $this->getPackageDir();
247         $path = $this->getBuildDir() . '/SugarModules';
248         if($clean && file_exists($path))rmdir_recursive($path);
249         if(mkdir_recursive($path)){
250             
251             $manifest = $this->getManifest().$this->buildInstall($path);
252             $fp = sugar_fopen($this->getBuildDir() .'/manifest.php', 'w');
253             fwrite($fp, $manifest);
254             fclose($fp);
255             
256         }
257         if(file_exists('modules/ModuleBuilder/MB/LICENSE.txt')){
258             copy('modules/ModuleBuilder/MB/LICENSE.txt', $this->getBuildDir() . '/LICENSE.txt');
259         }else if(file_exists('LICENSE.txt')){
260             copy('LICENSE.txt', $this->getBuildDir() . '/LICENSE.txt');
261         }
262         $package_dir = $this->getPackageDir();
263         $date = date('Y_m_d_His');
264         $zipDir = $this->getZipDir();
265         if(!file_exists($zipDir))mkdir_recursive($zipDir);
266         $cwd = getcwd();
267         chdir($this->getBuildDir());
268         zip_dir('.',$cwd . '/'. $zipDir. '/'. $this->name. $date. '.zip');
269         chdir($cwd);
270         if($export){
271             header('Location:' . $zipDir. '/'. $this->name. $date. '.zip');
272         }
273         return array(
274             'zip'=>$zipDir. '/'. $this->name. $date. '.zip',
275             'manifest'=>$this->getBuildDir(). '/manifest.php',
276             'name'=>$this->name. $date,
277             );
278     }
279     
280     
281     function getNodes(){
282         $this->loadModules();
283         $node = array('name'=>$this->name, 'action'=>'module=ModuleBuilder&action=package&package=' . $this->name, 'children'=>array());
284         foreach(array_keys($this->modules) as $module){
285             $node['children'][] = $this->modules[$module]->getNodes();
286         }
287         return $node;
288     }
289     
290     function populateFromPost(){
291         $this->description = trim($_REQUEST['description']);
292         $this->author = trim($_REQUEST['author']);
293         $this->key = trim($_REQUEST['key']);
294         $this->readme = trim($_REQUEST['readme']);
295     }
296     
297     function rename($new_name){
298         $old= $this->getPackageDir();
299         $this->name = $new_name;
300         $new = $this->getPackageDir();
301         if(file_exists($new)){
302             return false;   
303         }
304         if(rename($old, $new)){
305             return true;
306         }
307             
308         return false;
309     }
310     
311     function updateModulesMetaData($save=false){
312             foreach(array_keys($this->modules) as $module){
313                 $old_name = $this->modules[$module]->key_name;
314                 $this->modules[$module]->key_name = $this->key . '_' . $this->modules[$module]->name;
315                 $this->modules[$module]->renameMetaData($this->modules[$module]->getModuleDir(), $old_name);
316                 $this->modules[$module]->renameLanguageFiles($this->modules[$module]->getModuleDir());
317                 if($save)$this->modules[$module]->save();
318             }
319         
320     }
321     
322     function copy($new_name){
323         $old= $this->getPackageDir();
324         
325         $count = 0;
326         $this->name = $new_name;
327         $new= $this->getPackageDir();
328         while(file_exists($new)){
329             $count++;
330             $this->name = $new_name . $count;
331             $new= $this->getPackageDir();
332         }
333         
334         $new = $this->getPackageDir();
335         if(copy_recursive($old, $new)){
336             $this->updateModulesMetaData();
337             return true;
338         }
339         return false;
340         
341     }
342     
343     function delete(){
344         return rmdir_recursive($this->getPackageDir());
345     }
346     
347     
348         //creation of the installdefs[] array for the manifest when exporting customizations
349     function customBuildInstall($modules, $path, $extensions = array()){
350         $columns=$this->getColumnsName();
351         $installdefs = array ('id' => $this->name);
352         $include_path="$path/SugarModules/include/language";
353         if(file_exists($include_path) && is_dir($include_path)){
354             $dd= dir($include_path);
355             while($gg = $dd->read()){
356                 if(substr($gg, 0, 1) != '.' && is_file($include_path . '/' . $gg)){
357                     $lang = substr($gg, 0, strpos($gg, '.'));
358                     $installdefs['language'][] = array(
359                     'from'=> '<basepath>/SugarModules/include/language/'. $gg,
360                     'to_module'=> 'application',
361                     'language'=>$lang    
362                     );
363                 }
364             }
365         }
366         
367         foreach($modules as $value){
368             $custom_module = $this->getCustomModules($value);
369             foreach($custom_module as $va){
370                 if ($va == 'language'){
371                     $this->getLanguageManifestForModule($value, $installdefs);
372                     $this->getCustomFieldsManifestForModule($value, $installdefs);
373                 }//fi
374                 if($va == 'metadata'){
375                     $this->getCustomMetadataManifestForModule($value, $installdefs);
376                 }//fi
377             }//foreach
378         }//foreach
379         if (is_dir("$path/Extension"))
380         {
381             $this->getExtensionsManifestForPackage($path, $installdefs);
382         }
383         return "\n".'$installdefs = ' . var_export_helper($installdefs). ';';
384     }
385     
386     private function getLanguageManifestForModule($module, &$installdefs)
387     {
388         $lang_path = 'custom/modules/' . $module . '/language';
389         foreach(scandir($lang_path) as $langFile)
390         {
391                 if(substr($langFile, 0, 1) != '.' && is_file($lang_path . '/' . $langFile)){
392                     $lang = substr($langFile, 0, strpos($langFile, '.'));
393                     $installdefs['language'][] = array(
394                         'from'=> '<basepath>/SugarModules/modules/' . $module . '/language/'. $langFile,
395                         'to_module'=> $module,
396                         'language'=>$lang
397                     );
398                 }
399         }  
400     }
401     
402     private function getCustomFieldsManifestForModule($module, &$installdefs)
403     {
404         $db = DBManagerFactory::getInstance();
405         $result=$db->query("SELECT *  FROM fields_meta_data where custom_module='$module'");
406         while($row = $db->fetchByAssoc($result)){
407                 $name = $row['id'];
408                 foreach($row as $col=>$res){
409                         switch ($col) {
410                                 case 'custom_module':
411                                         $installdefs['custom_fields'][$name]['module'] = $res;
412                                         break;
413                                 case 'required':
414                                         $installdefs['custom_fields'][$name]['require_option'] = $res;
415                                         break;
416                                 case 'vname':
417                                         $installdefs['custom_fields'][$name]['label'] = $res;
418                                         break;
419                                 case 'required':
420                                         $installdefs['custom_fields'][$name]['require_option'] = $res;
421                                         break;
422                                 case 'massupdate':
423                                         $installdefs['custom_fields'][$name]['mass_update'] = $res;
424                                         break;
425                                 case 'comments':
426                                         $installdefs['custom_fields'][$name]['comments'] = $res;
427                                         break;
428                                 case 'help':
429                                         $installdefs['custom_fields'][$name]['help'] = $res;
430                                         break;
431                                 case 'len':
432                                         $installdefs['custom_fields'][$name]['max_size'] = $res;
433                                         break;
434                                 default:
435                                         $installdefs['custom_fields'][$name][$col] = $res;
436                         }//switch
437                 }//foreach
438         }//while
439     }
440     
441     private function getCustomMetadataManifestForModule($module, &$installdefs)
442     {
443         $meta_path = 'custom/modules/' . $module . '/metadata';
444         foreach(scandir($meta_path) as $meta_file)
445         {
446                 if(substr($meta_file, 0, 1) != '.' && is_file($meta_path . '/' . $meta_file)){
447                         if($meta_file == 'listviewdefs.php'){
448                                 $installdefs['copy'][] = array(
449                                 'from'=> '<basepath>/SugarModules/modules/'. $module . '/metadata/'. $meta_file,
450                                 'to'=> 'custom/modules/'. $module . '/metadata/' . $meta_file,   
451                                 );
452                         }
453                         else{
454                                 $installdefs['copy'][] = array(
455                                 'from'=> '<basepath>/SugarModules/modules/'. $module . '/metadata/'. $meta_file,
456                                 'to'=> 'custom/modules/'. $module . '/metadata/' . $meta_file,   
457                                 );
458                                 $installdefs['copy'][] = array(
459                                 'from'=> '<basepath>/SugarModules/modules/'. $module . '/metadata/'. $meta_file,
460                                 'to'=> 'custom/working/modules/'. $module . '/metadata/' . $meta_file,   
461                                 );
462                         }
463                 }
464         }
465     }
466     
467     private function getExtensionsManifestForPackage($path, &$installdefs)
468     {
469         $extPath = "$path/Extension/modules";
470         foreach(scandir($extPath) as $moduleDir)
471         {
472                 if(substr($moduleDir, 0, 1) != '.' && is_dir("$extPath/$moduleDir/Ext")){
473                         foreach(scandir("$extPath/$moduleDir/Ext") as $type)
474                         {
475                                 if(substr($type, 0, 1) != '.' && is_dir("$extPath/$moduleDir/Ext/$type")){
476                                         foreach(scandir("$extPath/$moduleDir/Ext/$type") as $file)
477                                         {
478                                                 if(substr($file, 0, 1) != '.' && strtolower(substr($file, -4)) == ".php")
479                                                 {
480                                                         $installdefs['copy'][] = array(
481                                                 'from'=> "<basepath>/Extension/modules/$moduleDir/Ext/$type/$file",
482                                                 'to'=> "custom/Extension/modules/$moduleDir/Ext/$type/$file",   
483                                             );
484                                                 }
485                                         }
486                                 }
487                         }
488             }
489         }
490     }
491
492     
493     //return an array which contain the name of fields_meta_data table's columns 
494     function getColumnsName(){
495          
496         $meta = new FieldsMetaData();
497         $arr = array(); 
498          foreach($meta->getFieldDefinitions() as $key=>$value) {
499             $arr[] = $key;
500         }
501         return $arr;
502     }
503
504
505     //creation of the custom fields ZIP file (use getmanifest() and customBuildInstall() )  
506     function exportCustom($modules, $export=true, $clean = true){
507         $path=$this->getBuildDir();
508         if($clean && file_exists($path))rmdir_recursive($path);
509         //Copy the custom files to the build dir
510         foreach($modules as $mod){
511                 $extensions = $this->getExtensionsList($mod);
512             $pathmod="$path/SugarModules/modules/$mod";
513             if(mkdir_recursive($pathmod)){
514                 if(file_exists("custom/modules/$mod")){
515                     copy_recursive("custom/modules/$mod", "$pathmod");
516                     //Don't include cached extension files
517                     if (is_dir("$pathmod/Ext"))
518                         rmdir_recursive("$pathmod/Ext");
519                 }
520                 //Convert modstring files to extension compatible arrays
521                     $this->convertLangFilesToExtensions("$pathmod/language");
522             }
523             $pathext="$path/Extension/modules/$mod/Ext";
524             if (!empty($extensions) && mkdir_recursive($pathext))
525             {
526                 foreach($extensions as $type => $files)
527                 {
528                     sugar_mkdir("$pathext/$type");
529                         foreach($files as $file => $filePath)
530                     {
531                         copy  ($filePath, "$pathext/$type/$file");
532                     }
533                 }
534             }
535         }
536         
537         $this->copyCustomDropdownValuesForModules($modules,$path);
538         if(file_exists($path)){
539             $manifest = $this->getManifest(true).$this->customBuildInstall($modules,$path);
540             sugar_file_put_contents($path .'/manifest.php', $manifest);;
541         }
542         if(file_exists('modules/ModuleBuilder/MB/LICENSE.txt')){
543             copy('modules/ModuleBuilder/MB/LICENSE.txt', $path . '/LICENSE.txt');
544         }
545         else if(file_exists('LICENSE.txt')){
546             copy('LICENSE.txt', $path . '/LICENSE.txt');
547         }
548         require_once('include/utils/zip_utils.php');
549         $date = date('Y_m_d_His');
550         $zipDir = $this->getZipDir();
551         if(!file_exists($zipDir))mkdir_recursive($zipDir);
552         $cwd = getcwd();
553         chdir($this->getBuildDir());
554         zip_dir('.',$cwd . '/'. $zipDir. '/'. $this->name. $date. '.zip');
555         chdir($cwd);
556         if($clean && file_exists($this->getBuildDir()))rmdir_recursive($this->getBuildDir());
557         if($export){
558             header('Location:' . $zipDir. '/'. $this->name. $date. '.zip');
559         }
560         return $zipDir. '/'. $this->name. $date. '.zip';
561     }
562     
563     private function convertLangFilesToExtensions($langDir)
564     {
565         if (is_dir($langDir))
566         {
567             foreach(scandir($langDir) as $langFile)
568             {
569                 $mod_strings = array();
570                 if (strcasecmp(substr($langFile, -4), ".php") != 0)
571                     continue;
572                 include("$langDir/$langFile");
573                 $out = "<?php \n // created: " . date('Y-m-d H:i:s') . "\n";
574                 foreach($mod_strings as $lbl_key => $lbl_val ) 
575                 {
576                     $out .= override_value_to_string("mod_strings", $lbl_key, $lbl_val) . "\n";
577                 }
578                 $out .= "\n?>\n";
579                 sugar_file_put_contents("$langDir/$langFile", $out);
580             }
581         }
582     }
583     private function copyCustomDropdownValuesForModules($modules, $path)
584     {
585         if(file_exists("custom/include/language")){
586             if(mkdir_recursive("$path/SugarModules/include")){
587                 global $app_list_strings;
588                 $backStrings = $app_list_strings;
589                 foreach(scandir("custom/include/language") as $langFile)
590                 {
591                     $app_list_strings = array();
592                     if (strcasecmp(substr($langFile, -4), ".php") != 0)
593                        continue;
594                     include("custom/include/language/$langFile");
595                     $out = "<?php \n";
596                     $lang = substr($langFile, 0, -9);
597                     $options = $this->getCustomDropDownStringsForModules($modules, $app_list_strings); 
598                     foreach($options as $name => $arr) {
599                         $out .= override_value_to_string('app_list_strings', $name, $arr);
600                     }
601                     mkdir_recursive("$path/SugarModules/include/language/");
602                     sugar_file_put_contents("$path/SugarModules/include/language/$lang.$this->name.php", $out);
603                 }
604                 $app_list_strings = $backStrings;
605             }
606         }
607     }
608     
609     function getCustomDropDownStringsForModules($modules, $list_strings) {
610         global $beanList, $beanFiles;
611         $options = array();
612         foreach($modules as $module)
613         {
614             if (!empty($beanList[$module]))
615             {
616                 require_once($beanFiles[$beanList[$module]]);
617                 $bean = new $beanList[$module]();
618                 foreach($bean->field_defs as $field => $def) 
619                 {
620                     if (isset($def['options']) && isset($list_strings[$def['options']]))
621                     {
622                         $options[$def['options']] = $list_strings[$def['options']];
623                     }
624                 }
625             }
626         }
627         return $options;
628     }
629
630
631
632     //if $module=false : return an array with custom module and there customizations.
633     //if $module=!false : return an array with the directories of custom/module/$module.
634     function getCustomModules($module=false){
635         global $mod_strings;
636         $path='custom/modules/';
637                 $extPath = 'custom/Extension/modules/';
638         if(!file_exists($path) || !is_dir($path)){
639             return array($mod_strings['LBL_EC_NOCUSTOM'] => "");
640         }
641         else{
642             if ($module != false ){
643                 $path=$path . $module . '/';
644             }
645             $scanlisting = scandir($path);
646             $dirlisting = array();
647             foreach ($scanlisting as $value){
648                 if(is_dir($path . $value) == true && $value != '.' && $value != '..') {
649                     $dirlisting[] = $value;
650                 }
651             }
652                         if(empty($dirlisting)){
653                 return array($mod_strings['LBL_EC_NOCUSTOM'] => "");
654             }
655             if ($module == false ){
656                 foreach ($dirlisting as $value){
657                         if(!file_exists('modules/' . $value . '/metadata/studio.php'))
658                                 continue;
659                     $custommodules[$value]=$this->getCustomModules($value);
660                     foreach ($custommodules[$value] as $va){
661                         switch ($va) {
662                         case 'language':
663                                 $return[$value][$va] = $mod_strings['LBL_EC_CUSTOMFIELD'];
664                             break;
665                         case 'metadata':
666                             $return[$value][$va] = $mod_strings['LBL_EC_CUSTOMLAYOUT'];
667                             break;
668                         case 'Ext':
669                             
670                                                         $return[$value][$va] = $mod_strings['LBL_EC_CUSTOMFIELD'];
671                             break;
672                         case '':
673                             $return[$value . " " . $mod_strings['LBL_EC_EMPTYCUSTOM']] = "";
674                             break;
675                         default:
676                             $return[$value][$va] = $mod_strings['LBL_UNDEFINED'];
677                         }
678                     }
679                 }
680                 return $return;
681             }
682             else{
683                 return $dirlisting;
684             }
685         }
686     }
687         
688         private function getExtensionsList($module, $excludeRelationships = true)
689         {
690                 require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
691                 $extPath = 'custom/Extension/modules/';
692                 $modExtPath = $extPath . $module . '/Ext';
693                 $rels = new DeployedRelationships($module);
694                 $relList = $rels->getRelationshipList ();
695                 
696                 $ret = array();
697                 if (is_dir($modExtPath))
698                 {
699             $extFolders = scandir($modExtPath);
700                         foreach($extFolders as $extFolder)
701                         {
702                 if (!is_dir("$modExtPath/$extFolder") || substr($extFolder, 0, 1) == ".")
703                                     continue;
704                 
705                                 foreach( scandir("$modExtPath/$extFolder") as $extFile)
706                                 {
707                                         
708                                         if (substr($extFile, 0, 1) == "." || strtolower(substr($extFile, -4)) != ".php")
709                         continue;
710                                         //Exclude relattionship extension files
711                     if ($excludeRelationships && (
712                         (substr($extFile, 0, 6) == "custom" && isset($relList[substr($extFile, 6, strlen($extFile) - 10)])) ||
713                         (substr($extFile, 6, 6) == "custom" && isset($relList[substr($extFile, 12, strlen($extFile) - 16)]))
714                     )) {
715                        continue;
716                     }
717                                         
718                                         if (!isset($ret[$extFolder]))
719                                            $ret[$extFolder] = array();
720                                            
721                                         $ret[$extFolder][$extFile  ] =  "$modExtPath/$extFolder/$extFile";
722                                 }
723                         }
724                 }
725                 return $ret;
726         }
727     
728     /**
729      * Returns a set of field defs for fields that will exist when this package is deployed
730      * based on the relationships in all of its modules.
731      * 
732      * @param $moduleName (module must be from whithin this package)
733      * @return array Field defs
734      */
735     function getRelationshipsForModule($moduleName) {
736         $ret = array();
737         if (isset($this->modules[$moduleName])) {
738                 $keyName = $this->modules[$moduleName]->key_name;
739                 foreach($this->modules as $mName => $module) {
740                         $rels = $module->getRelationships();
741                         $relList = $rels->getRelationshipList();
742                         foreach($relList as $rName ) {
743                             $rel = $rels->get ( $rName ) ;
744                              if ($rel->lhs_module == $keyName || $rel->rhs_module == $keyName) {
745                         $ret[$rName] =  $rel;
746                              }
747                         }
748                 }
749         }
750         return $ret; 
751     }
752     
753
754     
755     function exportProjectInstall($package, $for_export){
756         $pre = $for_export ? MB_EXPORTPREPEND : "";
757         $installdefs = array ('id' => $pre . $this->name);
758         $installdefs['copy'][] = array(
759             'from'=> '<basepath>/' . $this->name,
760             'to'=> 'custom/modulebuilder/packages/'. $this->name,   
761         );
762         return "\n".'$installdefs = ' . var_export_helper($installdefs). ';';
763
764     }
765     
766     
767     
768     function exportProject($package, $export=true, $clean = true){
769         $tmppath="custom/modulebuilder/projectTMP/";
770         if(file_exists($this->getPackageDir())){
771             if(mkdir_recursive($tmppath)){
772                 copy_recursive($this->getPackageDir(), $tmppath ."/". $this->name);
773                 $manifest = $this->getManifest(true, $export).$this->exportProjectInstall($package, $export);
774                 $fp = sugar_fopen($tmppath .'/manifest.php', 'w');
775                 fwrite($fp, $manifest);
776                 fclose($fp);
777                 if(file_exists('modules/ModuleBuilder/MB/LICENSE.txt')){
778                     copy('modules/ModuleBuilder/MB/LICENSE.txt', $tmppath . '/LICENSE.txt');
779                 }
780                 else if(file_exists('LICENSE.txt')){
781                     copy('LICENSE.txt', $tmppath . '/LICENSE.txt');
782                 }
783                 $readme_contents = $this->readme;
784                 $readmefp = sugar_fopen($tmppath.'/README.txt','w');
785                 fwrite($readmefp, $readme_contents);
786                 fclose($readmefp);
787             }
788         }
789         require_once('include/utils/zip_utils.php');
790         $date = date('Y_m_d_His');
791         $zipDir = "custom/modulebuilder/packages/ExportProjectZips";
792         if(!file_exists($zipDir))mkdir_recursive($zipDir);
793         $cwd = getcwd();
794         chdir($tmppath);
795         zip_dir('.',$cwd . '/'. $zipDir. '/project_'. $this->name. $date. '.zip');
796         chdir($cwd);
797         if($clean && file_exists($tmppath))rmdir_recursive($tmppath);
798         if($export){
799             header('Location:' . $zipDir. '/project_'. $this->name. $date. '.zip');
800         }
801         return $zipDir. '/project_'. $this->name. $date. '.zip';
802     }
803     
804     
805 }
806 ?>