2 /*********************************************************************************
3 * SugarCRM is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
37 require_once('modules/ModuleBuilder/MB/MBModule.php');
41 var $is_uninstallable = true;
42 var $description = '';
43 var $has_images = true;
44 var $modules = array();
45 var $date_modified = '';
49 function MBPackage($name){
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);
65 * Loads the translated module titles from the selected language into.
66 * Will override currently loaded string to reflect undeployed label changes.
69 * @param $languge String language identifyer
71 function loadModuleTitles($languge = '')
75 $language = $GLOBALS['current_language'];
77 global $app_list_strings;
78 $packLangFilePath = $this->getPackageDir() . "/language/application/" . $language . ".lang.php";
79 if (file_exists($packLangFilePath))
82 require($packLangFilePath);
86 function getModule($name, $force=true){
87 if(!$force && !empty($this->modules[$name]))return;
88 $path = $this->getPackageDir();
90 $this->modules[$name] = new MBModule($name, $path, $this->name, $this->key);
93 function deleteModule($name){
94 $this->modules[$name]->delete();
95 unset($this->modules[$name]);
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();
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');
112 'acceptable_sugar_versions' =>
116 'acceptable_sugar_flavors' =>
120 'readme'=>'$this->readme',
122 'author' => '$this->author',
123 'description' => '$this->description',
125 'is_uninstallable' => $is_uninstallable,
126 'name' => '$pre$this->name',
127 'published_date' => '$date',
129 'version' => '$time',
130 'remove_tables' => 'prompt',
135 function buildInstall($path){
136 $installdefs = array ('id' => $this->name,
138 'layoutdefs'=>array(),
139 'relationships'=>array(),
141 if($this->has_images){
142 $installdefs['image_dir'] = '<basepath>/icons';
144 foreach(array_keys($this->modules) as $module){
145 $this->modules[$module]->build($path);
146 $this->modules[$module]->addInstallDefs($installdefs);
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,
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);
173 return "\n".'$installdefs = ' . var_export_helper($installdefs). ';';
177 function getPackageDir(){
178 return MB_PACKAGE_PATH . '/' . $this->name;
181 function getBuildDir(){
182 return MB_PACKAGE_BUILD . '/' . $this->name;
185 function getZipDir(){
186 return $this->getPackageDir() . '/zips';
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'];
204 $this->loadModules(true);
208 $path = $this->getPackageDir();
209 if(mkdir_recursive($path)){
210 $fp = sugar_fopen($path .'/manifest.php', 'w');
213 //Save all the modules when we save a package
214 $this->updateModulesMetaData(true);
215 fwrite($fp, $this->getManifest() );
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)){
232 $manifest = $this->getManifest().$this->buildInstall($path);
233 $fp = sugar_fopen($this->getBuildDir() .'/manifest.php', 'w');
234 fwrite($fp, $manifest);
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');
243 $package_dir = $this->getPackageDir();
244 $date = date('Y_m_d_His');
245 $zipDir = $this->getZipDir();
246 if(!file_exists($zipDir))mkdir_recursive($zipDir);
248 chdir($this->getBuildDir());
249 zip_dir('.',$cwd . '/'. $zipDir. '/'. $this->name. $date. '.zip');
252 header('Location:' . $zipDir. '/'. $this->name. $date. '.zip');
255 'zip'=>$zipDir. '/'. $this->name. $date. '.zip',
256 'manifest'=>$this->getBuildDir(). '/manifest.php',
257 'name'=>$this->name. $date,
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();
271 function populateFromPost(){
272 $this->description = $_REQUEST['description'];
273 $this->author = $_REQUEST['author'];
274 $this->key = $_REQUEST['key'];
275 $this->readme = $_REQUEST['readme'];
278 function rename($new_name){
279 $old= $this->getPackageDir();
280 $this->name = $new_name;
281 $new = $this->getPackageDir();
282 if(file_exists($new)){
285 if(rename($old, $new)){
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();
303 function copy($new_name){
304 $old= $this->getPackageDir();
307 $this->name = $new_name;
308 $new= $this->getPackageDir();
309 while(file_exists($new)){
311 $this->name = $new_name . $count;
312 $new= $this->getPackageDir();
315 $new = $this->getPackageDir();
316 if(copy_recursive($old, $new)){
317 $this->updateModulesMetaData();
325 return rmdir_recursive($this->getPackageDir());
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',
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);
355 if($va == 'metadata'){
356 $this->getCustomMetadataManifestForModule($value, $installdefs);
360 if (is_dir("$path/Extension"))
362 $this->getExtensionsManifestForPackage($path, $installdefs);
364 return "\n".'$installdefs = ' . var_export_helper($installdefs). ';';
367 private function getLanguageManifestForModule($module, &$installdefs)
369 $lang_path = 'custom/modules/' . $module . '/language';
370 foreach(scandir($lang_path) as $langFile)
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,
383 private function getCustomFieldsManifestForModule($module, &$installdefs)
385 $db = DBManagerFactory::getInstance();
386 $result=$db->query("SELECT * FROM fields_meta_data where custom_module='$module'");
387 while($row = $db->fetchByAssoc($result)){
389 foreach($row as $col=>$res){
391 case 'custom_module':
392 $installdefs['custom_fields'][$name]['module'] = $res;
395 $installdefs['custom_fields'][$name]['require_option'] = $res;
398 $installdefs['custom_fields'][$name]['label'] = $res;
401 $installdefs['custom_fields'][$name]['require_option'] = $res;
404 $installdefs['custom_fields'][$name]['mass_update'] = $res;
407 $installdefs['custom_fields'][$name]['comments'] = $res;
410 $installdefs['custom_fields'][$name]['help'] = $res;
413 $installdefs['custom_fields'][$name]['max_size'] = $res;
416 $installdefs['custom_fields'][$name][$col] = $res;
422 private function getCustomMetadataManifestForModule($module, &$installdefs)
424 $meta_path = 'custom/modules/' . $module . '/metadata';
425 foreach(scandir($meta_path) as $meta_file)
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,
435 $installdefs['copy'][] = array(
436 'from'=> '<basepath>/SugarModules/modules/'. $module . '/metadata/'. $meta_file,
437 'to'=> 'custom/modules/'. $module . '/metadata/' . $meta_file,
439 $installdefs['copy'][] = array(
440 'from'=> '<basepath>/SugarModules/modules/'. $module . '/metadata/'. $meta_file,
441 'to'=> 'custom/working/modules/'. $module . '/metadata/' . $meta_file,
448 private function getExtensionsManifestForPackage($path, &$installdefs)
450 $extPath = "$path/Extension/modules";
451 foreach(scandir($extPath) as $moduleDir)
453 if(substr($moduleDir, 0, 1) != '.' && is_dir("$extPath/$moduleDir/Ext")){
454 foreach(scandir("$extPath/$moduleDir/Ext") as $type)
456 if(substr($type, 0, 1) != '.' && is_dir("$extPath/$moduleDir/Ext/$type")){
457 foreach(scandir("$extPath/$moduleDir/Ext/$type") as $file)
459 if(substr($file, 0, 1) != '.' && strtolower(substr($file, -4)) == ".php")
461 $installdefs['copy'][] = array(
462 'from'=> "<basepath>/Extension/modules/$moduleDir/Ext/$type/$file",
463 'to'=> "custom/Extension/modules/$moduleDir/Ext/$type/$file",
474 //return an array which contain the name of fields_meta_data table's columns
475 function getColumnsName(){
477 $meta = new FieldsMetaData();
479 foreach($meta->getFieldDefinitions() as $key=>$value) {
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");
501 //Convert modstring files to extension compatible arrays
502 $this->convertLangFilesToExtensions("$pathmod/language");
504 $pathext="$path/Extension/modules/$mod/Ext";
505 if (!empty($extensions) && mkdir_recursive($pathext))
507 foreach($extensions as $type => $files)
509 sugar_mkdir("$pathext/$type");
510 foreach($files as $file => $filePath)
512 copy ($filePath, "$pathext/$type/$file");
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);;
523 if(file_exists('modules/ModuleBuilder/MB/LICENSE.txt')){
524 copy('modules/ModuleBuilder/MB/LICENSE.txt', $path . '/LICENSE.txt');
526 else if(file_exists('LICENSE.txt')){
527 copy('LICENSE.txt', $path . '/LICENSE.txt');
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);
534 chdir($this->getBuildDir());
535 zip_dir('.',$cwd . '/'. $zipDir. '/'. $this->name. $date. '.zip');
537 if($clean && file_exists($this->getBuildDir()))rmdir_recursive($this->getBuildDir());
539 header('Location:' . $zipDir. '/'. $this->name. $date. '.zip');
541 return $zipDir. '/'. $this->name. $date. '.zip';
544 private function convertLangFilesToExtensions($langDir)
546 if (is_dir($langDir))
548 foreach(scandir($langDir) as $langFile)
550 $mod_strings = array();
551 if (strcasecmp(substr($langFile, -4), ".php") != 0)
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 )
557 $out .= override_value_to_string("mod_strings", $lbl_key, $lbl_val) . "\n";
560 sugar_file_put_contents("$langDir/$langFile", $out);
564 private function copyCustomDropdownValuesForModules($modules, $path)
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)
572 $app_list_strings = array();
573 if (strcasecmp(substr($langFile, -4), ".php") != 0)
575 include("custom/include/language/$langFile");
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);
582 mkdir_recursive("$path/SugarModules/include/language/");
583 sugar_file_put_contents("$path/SugarModules/include/language/$lang.$this->name.php", $out);
585 $app_list_strings = $backStrings;
590 function getCustomDropDownStringsForModules($modules, $list_strings) {
591 global $beanList, $beanFiles;
593 foreach($modules as $module)
595 if (!empty($beanList[$module]))
597 require_once($beanFiles[$beanList[$module]]);
598 $bean = new $beanList[$module]();
599 foreach($bean->field_defs as $field => $def)
601 if (isset($def['options']) && isset($list_strings[$def['options']]))
603 $options[$def['options']] = $list_strings[$def['options']];
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){
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'] => "");
623 if ($module != false ){
624 $path=$path . $module . '/';
626 $scanlisting = scandir($path);
627 $dirlisting = array();
628 foreach ($scanlisting as $value){
629 if(is_dir($path . $value) == true && $value != '.' && $value != '..') {
630 $dirlisting[] = $value;
633 if(empty($dirlisting)){
634 return array($mod_strings['LBL_EC_NOCUSTOM'] => "");
636 if ($module == false ){
637 foreach ($dirlisting as $value){
638 if(!file_exists('modules/' . $value . '/metadata/studio.php'))
640 $custommodules[$value]=$this->getCustomModules($value);
641 foreach ($custommodules[$value] as $va){
644 $return[$value][$va] = $mod_strings['LBL_EC_CUSTOMFIELD'];
647 $return[$value][$va] = $mod_strings['LBL_EC_CUSTOMLAYOUT'];
651 $return[$value][$va] = $mod_strings['LBL_EC_CUSTOMFIELD'];
654 $return[$value . " " . $mod_strings['LBL_EC_EMPTYCUSTOM']] = "";
657 $return[$value][$va] = $mod_strings['LBL_UNDEFINED'];
669 private function getExtensionsList($module, $excludeRelationships = true)
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 ();
678 if (is_dir($modExtPath))
680 $extFolders = scandir($modExtPath);
681 foreach($extFolders as $extFolder)
683 if (!is_dir("$modExtPath/$extFolder") || substr($extFolder, 0, 1) == ".")
686 foreach( scandir("$modExtPath/$extFolder") as $extFile)
689 if (substr($extFile, 0, 1) == "." || strtolower(substr($extFile, -4)) != ".php")
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)]))
699 if (!isset($ret[$extFolder]))
700 $ret[$extFolder] = array();
702 $ret[$extFolder][$extFile ] = "$modExtPath/$extFolder/$extFile";
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.
713 * @param $moduleName (module must be from whithin this package)
714 * @return array Field defs
716 function getRelationshipsForModule($moduleName) {
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) {
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,
743 return "\n".'$installdefs = ' . var_export_helper($installdefs). ';';
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);
758 if(file_exists('modules/ModuleBuilder/MB/LICENSE.txt')){
759 copy('modules/ModuleBuilder/MB/LICENSE.txt', $tmppath . '/LICENSE.txt');
761 else if(file_exists('LICENSE.txt')){
762 copy('LICENSE.txt', $tmppath . '/LICENSE.txt');
764 $readme_contents = $this->readme;
765 $readmefp = sugar_fopen($tmppath.'/README.txt','w');
766 fwrite($readmefp, $readme_contents);
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);
776 zip_dir('.',$cwd . '/'. $zipDir. '/project_'. $this->name. $date. '.zip');
778 if($clean && file_exists($tmppath))rmdir_recursive($tmppath);
780 header('Location:' . $zipDir. '/project_'. $this->name. $date. '.zip');
782 return $zipDir. '/project_'. $this->name. $date. '.zip';