Location: PHPKode > projects > EXtensible Production Suite (XPS) > xpsdev/core/controls/module_control.php
<?php

/***********************************************************************************************************
*
* <p>Contains system API</p>
*
* @author heidtc < hide@address.com >, cookbt < hide@address.com >
*
************************************************************************************************************/
Class Module_Control
{
	private $Database_Control;
	private $Message_Control;
	private $Setting_Control;
	private $Module;

	/***************************************************
	* <p>constructor for module control.</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @param	Object [$DC] The database control
	* @param	Object [$MC] The message control
	***************************************************/
	public function __construct($DC, $MC, $SC)
	{
		$this->Database_Control = $DC;
		$this->Message_Control = $MC;
		$this->Setting_Control = $SC;

		require_once( MODELS . '/module.php' );
		$this->Module = new Module;
	}

	/***************************************************
	* <p>handle module installation tasks</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @return	type description
	***************************************************/
	public function module_installation($params=array())
	{
		$message = $params['module_id'];
		$mod_params['MODULE_ID'] 	= $params['module_id'];
		$mod_params['MODULE_NAME'] 	= $this->Setting_Control->get_setting("module_name", $mod_params['MODULE_ID']);
		$mod_params['IS_ACTIVE'] 	= 0;
		$mod_params['DESCRIPTION'] 	= $this->Setting_Control->get_setting("description", $mod_params['MODULE_ID']);
		$mod_params['VERSION'] 		= $this->Setting_Control->get_setting("version", $mod_params['MODULE_ID']);
        $mod_params['ADMIN_ONLY'] = $this->Setting_Control->get_setting('admin_only', $mod_params['MODULE_ID']);

		$bool = $this->Database_Control->execute_sql('module_install', 'core', $mod_params);

		if($bool)
		{
			$message .= $this->Message_Control->get_message('module_install_complete','core') . '<br />';
		}
		else
		{
			$message .= $this->Message_Control->get_message('module_install_failed','core') . '<br />';
		}

		$table_log = NULL;

		$path = MODULES . '/' . $mod_params['MODULE_ID'] . '/install/install.' . $this->Setting_Control->get_setting('db_type', 'core') . '.sql';


		if(file_exists($path))
		{
			$content = file_get_contents($path);
			$file_array = explode(';', $content);

			foreach($file_array as $line)
			{
				if(trim($line) != '')
				{
					$inserted = $this->Database_Control->execute_sql_line($line);

					if($inserted == true)
					{
						$table_log = $this->Module->create_module_log($line);
						$table_log['MODULE_ID'] = $params['module_id'];

						//we assume the log is created successfully (for now)
						$this->Database_Control->execute_sql('module_log_add', 'core', $table_log);
					}
				}
			}
		}

		if($bool === false)
		{
			$this->module_uninstallation($params['module_id']);
			return $message;
		}

		$path = MODULES . '/' . $mod_params['MODULE_ID'] . '/install/install.widgets.xml';

		if(file_exists($path))
		{
			$dom = new DOMDocument;
			$dom->preserveWhiteSpace = false;
			$dom->load($path);
			$xpath = new DOMXpath($dom);

			$widget_params = array();
			$array = array();
		 	$widgets = $xpath->query('//widget');

		 	foreach($widgets as $widget)
		 	{
		 		$array = null;

				foreach($widget->childNodes as $child)
		 		{
		 			$array[$child->nodeName] = $child->nodeValue;
		 		}

                $wid_id = explode('.', $array['url']);

		 		$widget_params['WIDGET_ID'] = $wid_id[0];
				$widget_params['MODULE_ID'] = $params['module_id'];
				$widget_params['WIDGET_NAME'] = $array['name'];
				$widget_params['DESCRIPTION'] = $array['description'];
				$widget_params['URL'] = $array['url'];
				$widget_params['VERSION'] = $array['version'];
				$widget_params['CONTAINER_ID'] = 'inactive_widget_container';

		 		$success = $this->Database_Control->execute_sql('widget_install', 'core', $widget_params);

		 		if($success)
		 		{
		 			$message .= $widget_params['WIDGET_NAME'] . ' widget successfully installed<br />';
		 		}
		 		else
		 		{
		 			$message .= $widget_params['WIDGET_NAME'] . ' widget install failed<br />';
		 		}
		 	}

		}

		return $message;
	}

	/***************************************************
	* <p>handle module uninstallation tasks</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @return	type description
	***************************************************/
	public function module_uninstallation($module_id)
	{
		$this->kill_logs($module_id, 'foreign');
		$this->kill_logs($module_id, 'procedure');
		$this->kill_logs($module_id, 'view');
		$this->kill_logs($module_id, 'table');

		$message = $module_id;

		//if module already installed fully
		$mod_params = array();
		$mod_params['MODULE_ID'] = $module_id;
		$record = $this->Database_Control->get_recordset('module_information', 'core', $mod_params);
		
		if($record != null)
		{
			$bool = $this->Database_Control->execute_sql('module_delete', 'core', $mod_params);
			if($bool)
			{
				$message .= $this->Message_Control->get_message('module_uninstall_complete','core');
			}
			else
			{
				$message .= $this->Message_Control->get_message('module_uninstall_failed','core');
			}
		}
		else
		{
			$message .= $this->Message_Control->get_message('module_uninstall_complete','core');
		}
		return $message;
	}

	/***************************************************
	***************************************************/
	public function kill_logs($module_id, $type)
	{
	 	$bool = false;
		$params['MODULE_ID'] = $module_id;
	 	$params['MODULE_OBJECT_TYPE'] = $type;
	 
		$records = $this->Database_Control->get_recordset('get_module_log', 'core', $params);
		
		if(is_array($records))
		{
			foreach($records as $record)
			{
				$bool = false;
				$bool = $this->Database_Control->execute_sql_line('DROP ' . $type . ' ' . $record['MODULE_OBJECT_NAME'] . ';');
				$record['MODULE_ID'] = $module_id;
				$record['MODULE_OBJECT_TYPE'] = $type;
				unset($record[0]);
				
				if($bool == true)
				{
					$this->Database_Control->execute_sql('remove_log_entry', 'core', $record);
				}
			}
		}
	}

	/***************************************************
	* <p>handle module activation tasks</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @return	boolean
	***************************************************/
	public function activate_module($module_id)
	{
		$params = array();
		$params['MODULE_ID'] = $module_id;
		$params['IS_ACTIVE'] = 1;

		$bool = $this->Database_Control->execute_sql('update_module_activation', 'core', $params);

		$message = $module_id;
		if($bool)
		{
			$message .= $this->Message_Control->get_message('module_activate_complete','core');
		}
		else
		{
			$message .= $this->Message_Control->get_message('module_activate_failed','core');
		}

		return $message;
	}

	/***************************************************
	* <p>handle module deactivation tasks</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @return	boolean
	***************************************************/
	public function deactivate_module($module_id)
	{
		$params = array();
		$params['MODULE_ID'] = $module_id;
		$params['IS_ACTIVE'] = 0;

		$bool = $this->Database_Control->execute_sql('update_module_activation', 'core', $params);

		$message = $module_id;
		if($bool)
		{
			$message .= $this->Message_Control->get_message('module_deactivate_complete','core');
		}
		else
		{
			$message .= $this->Message_Control->get_message('module_deactivate_failed','core');
		}

		return $message;
	}



	/***************************************************
	* <p>handle module list creation for admin view</p>
	*
	* @access   public
	* @author 	heidtc <hide@address.com>
	* @return	array
	***************************************************/
	public function list_modules($limit = 0, $offset = 0, $order_by = 'MODULE_NAME ASC')
	{
		$sql_id = 'list_all_modules';
		$params = array('LIMIT' => $limit,
						'OFFSET' => $offset,
						'ORDER_BY' => $order_by,
						'table_name' => 'XPS_MODULE',
						'js_funct' => 'launchCoreModule',
						'mod_id' => 'ModuleAdmin',
						);

		$page_numbers	= $this->Database_Control->get_page_numbers($sql_id, 'core', $params);
		$modules 		= $this->Database_Control->get_recordset($sql_id, 'core', $params);
		$sort			= explode(' ', $order_by);

		return array('page_numbers'=>$page_numbers, 'modules'=>$modules, 'sort'=>$sort);
	}

	/***************************************************
	* <p>finds modules that have not been installed yet</p>
	*
	* @access   public
	* @author 	Brian Cook <hide@address.com>
	* @return	list
	***************************************************/
	public function find_new_modules()
	{
		$module_names = array();
		$modules_not_installed = array();
		$modules_installed_list = $this->Database_Control->get_recordset('list_installed_modules', 'core');
		$bad_file_names = explode(';', $this->Setting_Control->get_setting('unallowed_directory_names','core') );
        foreach($bad_file_names as $key => $val)
        {
            $bad_file_names[$key] = trim($bad_file_names[$key]);
        }

		foreach($modules_installed_list as $module_info)
		{
			array_push($module_names, $module_info['MODULE_ID']);
		}

		$path = MODULES;
		$dir_handle = opendir($path) or die($this->Message_Control->get_message('open_dir_fail'));
        while ($file = readdir($dir_handle))
	 	{
			if(!in_array($file, $module_names) and !in_array($file, $bad_file_names))
	 		{
				array_push($modules_not_installed, $file);
	 	 	}
	 	}
	 	return $modules_not_installed;
	}

	/***************************************************
	* <p>handle module information gathering</p>
	*
	* @access   public
	* @author 	Brian Cook <hide@address.com>
	* @return	recordset
	***************************************************/
	public function get_module_information($module_id)
	{
		$params = array();
		$params['MODULE_ID'] = $module_id;
		return $this->Database_Control->get_recordset('module_information', 'core', $params);
	}

	/***************************************************
	* <p>handle module name update</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @return	boolean
	***************************************************/
	public function module_update_name($module_id, $module_name)
	{
		$params = array();
		$params['MODULE_ID'] = $module_id;
		$params['MODULE_NAME'] = $module_name;
		return $this->Database_Control->execute_sql('module_update_name', 'core', $params);
	}

	/***************************************************
	* <p>handle module description update</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @return	boolean
	***************************************************/
	public function module_update_description($module_id, $module_desc)
	{
		$params = array();
		$params['MODULE_ID'] = $module_id;
		$params['DESCRIPTION'] = $module_desc;
		return $this->Database_Control->execute_sql('module_update_desc', 'core', $params);
	}

	/***************************************************
	* <p>handle module version update</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @return	boolean
	***************************************************/
	public function module_update_version($module_id, $version_number)
	{
		$params = array();
		$params['MODULE_ID'] = $module_id;
		$params['VERSION'] = $version_number;
		return $this->Database_Control->execute_sql('module_update_ver', 'core', $params);
	}
}
Return current item: EXtensible Production Suite (XPS)