mbpackage.php

来自「SugarCRM5.1 开源PHP客户关系管理系统」· PHP 代码 · 共 580 行 · 第 1/2 页

PHP
580
字号
<?php
/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by * SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc. *  * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation with the addition of the following permission added * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. *  * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more * details. *  * You should have received a copy of the GNU General Public License along with * this program; if not, see http://www.gnu.org/licenses or write to the Free * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA. *  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road, * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com. *  * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. *  * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * SugarCRM" logo. If the display of the logo is not reasonably feasible for * technical reasons, the Appropriate Legal Notices must display the words * "Powered by SugarCRM". ********************************************************************************/
require_once('modules/ModuleBuilder/MB/MBModule.php');

class MBPackage{
	var $name;
	var $is_uninstallable = true;
	var $description = '';
	var $has_images = true;
	var $modules = array();
	var $date_modified = '';
	var $author = '';	var $key = '';
	var $readme='';
	function MBPackage($name){
		$this->name = $name;
		$this->load();		
	}
	function loadModules($force=false){
		if(!file_exists(MB_PACKAGE_PATH . '/' . $this->name .'/modules'))return;
		$d = dir(MB_PACKAGE_PATH . '/' . $this->name .'/modules');
		while($e = $d->read()){
			if(substr($e, 0, 1) != '.' && is_dir(MB_PACKAGE_PATH . '/'. $this->name. '/modules/' . $e)){
				$this->getModule($e, $force);
			}
		}
	}
	
	function getModule($name, $force=true){		if(!$force && !empty($this->modules[$name]))return;
		$path = $this->getPackageDir();		
		$this->modules[$name] = new MBModule($name, $path, $this->name, $this->key);
	}
	
	function deleteModule($name){
		$this->modules[$name]->delete();
		unset($this->modules[$name]);
	}
	
function getManifest($version_specific = false){
	$date = gmdate('Y-m-d H:i:s');
	$time = time();
	$this->description = to_html($this->description);
	$is_uninstallable = ($this->is_uninstallable ? 'true' : 'false');
	$flavor = "'" . $GLOBALS['sugar_flavor'] . "'";	if($GLOBALS['sugar_flavor'] == 'CE')$flavor = "'CE', 'PRO','ENT'";
	$version = (!empty($version_specific))?"'" . $GLOBALS['sugar_version'] . "'" : '';
	$header = file_get_contents('modules/ModuleBuilder/MB/header.php');
	return  <<<EOQ
	$header
	\$manifest = array (
		 'acceptable_sugar_versions' => 
		  array (
	     	$version
		  ),
		  'acceptable_sugar_flavors' =>
		  array(
		  	$flavor
		  ),		  'readme'=>'$this->readme',
		  'key'=>'$this->key',
		  'author' => '$this->author',
		  'description' => '$this->description',
		  'icon' => '',
		  'is_uninstallable' => $is_uninstallable,
		  'name' => '$this->name',
		  'published_date' => '$date',
		  'type' => 'module',
		  'version' => '$time',
		  'remove_tables' => 'prompt',
		  );
EOQ;
}
	
function buildInstall($path){
	$installdefs = array ('id' => $this->name,
		'beans'=>array(),
		'layoutdefs'=>array(),
		'relationships'=>array(),
	);
	if($this->has_images){
		$installdefs['image_dir'] = '<basepath>/icons'; 
	}
	foreach(array_keys($this->modules) as $module){
		$this->modules[$module]->build($path);
		$this->modules[$module]->addInstallDefs($installdefs);
	}
	$this->path = $this->getPackageDir(); 
	if(file_exists($this->path . '/language')){
		$d= dir($this->path . '/language');
		while($e = $d->read()){
			$lang_path = $this->path .'/language/' . $e;
			if(substr($e, 0, 1) != '.' && is_dir($lang_path)){
				$f = dir($lang_path);
				while($g = $f->read()){
					if(substr($g, 0, 1) != '.' && is_file($lang_path.'/'. $g)){
						$lang = substr($g, 0, strpos($g, '.'));
						$installdefs['language'][] = array(
						'from'=> '<basepath>/SugarModules/language/'.$e . '/'. $g,
						'to_module'=> $e,
						'language'=> $lang	
						);
					}
				}
			}
		}
			
		copy_recursive( $this->path . '/language/', $path . '/language/');
		$icon_path = $path . '/../icons/default/images/';
		mkdir_recursive($icon_path);
		copy_recursive($this->path . '/icons/', $icon_path);
	}
	return "\n".'$installdefs = ' . var_export_helper($installdefs). ';';

}
	
	function getPackageDir(){
		return MB_PACKAGE_PATH . '/' . $this->name;
	}
	
	function getBuildDir(){
		return MB_PACKAGE_BUILD . '/' . $this->name;
	}
	
	function getZipDir(){
		return $this->getPackageDir() . '/zips';
	}
	
	
	function load(){
		$path = $this->getPackageDir();
		if(file_exists($path .'/manifest.php')){
			require($path . '/manifest.php');
			if(!empty($manifest)){
				$this->date_modified = $manifest['published_date'];
				$this->is_uninstallable = $manifest['is_uninstallable'];
				$this->author = $manifest['author'];				$this->key = $manifest['key'];
				$this->description = $manifest['description'];
				if(!empty($manifest['readme']))
					$this->readme = $manifest['readme'];
			}
		}		$this->loadModules(true);
	}

	function save(){
		$path = $this->getPackageDir();
		if(mkdir_recursive($path)){
			$fp = sugar_fopen($path .'/manifest.php', 'w');
			
			
			//Save all the modules when we save a package			$this->updateModulesMetaData(true);
			fwrite($fp, $this->getManifest() );
			fclose($fp);
		}
		
		
		
		
	}
	
	function build($export=true, $clean = true){
		$this->loadModules();
		require_once('include/utils/zip_utils.php');
		$package_path = $this->getPackageDir();
		$path = $this->getBuildDir() . '/SugarModules';
		if($clean && file_exists($path))rmdir_recursive($path);
		if(mkdir_recursive($path)){
			
			$manifest = $this->getManifest().$this->buildInstall($path);
			$fp = sugar_fopen($this->getBuildDir() .'/manifest.php', 'w');
			fwrite($fp, $manifest);
			fclose($fp);
			
		}
		if(file_exists('modules/ModuleBuilder/MB/LICENSE.txt')){
			copy('modules/ModuleBuilder/MB/LICENSE.txt', $this->getBuildDir() . '/LICENSE.txt');
		}else if(file_exists('LICENSE.txt')){
			copy('LICENSE.txt', $this->getBuildDir() . '/LICENSE.txt');
		}
		$package_dir = $this->getPackageDir();
		$date = date('Y_m_d_His');
		$zipDir = $this->getZipDir();
		if(!file_exists($zipDir))mkdir_recursive($zipDir);
		$cwd = getcwd();
		chdir($this->getBuildDir());
		zip_dir('.',$cwd . '/'. $zipDir. '/'. $this->name. $date. '.zip');
 	 	chdir($cwd);
		if($export){
			header('Location:' . $zipDir. '/'. $this->name. $date. '.zip');
 	 	}
 	 	return array(
 	 		'zip'=>$zipDir. '/'. $this->name. $date. '.zip',
 	 		'manifest'=>$this->getBuildDir(). '/manifest.php',
 	 		'name'=>$this->name. $date,
 	 		);
	}
	
	
	function getNodes(){
		$this->loadModules();
		$node = array('name'=>$this->name, 'action'=>'module=ModuleBuilder&action=package&package=' . $this->name, 'children'=>array());
		foreach(array_keys($this->modules) as $module){
			$node['children'][] = $this->modules[$module]->getNodes();
		}
		return $node;
	}
	
	function populateFromPost(){
		$this->description = $_POST['description'];
		$this->author = $_POST['author'];		$this->key = $_POST['key'];
		$this->readme = $_POST['readme'];
	}
	
	function rename($new_name){
		$old= $this->getPackageDir();
		$this->name = $new_name;
		$new = $this->getPackageDir();
		if(file_exists($new)){
			return false;	
		}
		if(rename($old, $new)){			return true;		}					return false;
	}		function updateModulesMetaData($save=false){						foreach(array_keys($this->modules) as $module){				$this->modules[$module]->key_name = $this->key . '_' . $this->modules[$module]->name;				$this->modules[$module]->renameMetaData($this->modules[$module]->getModuleDir());				$this->modules[$module]->renameLanguageFiles($this->modules[$module]->getModuleDir());				if($save)$this->modules[$module]->save();			}			}
	
	function copy($new_name){
		$old= $this->getPackageDir();
		
		$count = 0;
		$this->name = $new_name;
		$new= $this->getPackageDir();
		while(file_exists($new)){
			$count++;
			$this->name = $new_name . $count;
			$new= $this->getPackageDir();
		}
		
		$new = $this->getPackageDir();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?