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