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