/exchange/code/trunk/administrator/components/com_exchange/controller.php
PHP | 330 lines | 229 code | 51 blank | 50 comment | 28 complexity | b3b0ac49cb083a336e96d8a86f744df8 MD5 | raw file
- <?php
- /**
- * @version $Id: controller.php 280 2010-09-18 02:14:15Z eddieajau $
- * @package NewLifeInIT
- * @subpackage com_exchange
- * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License version 2 or later.
- * @link http://www.theartofjoomla.com
- */
- // no direct access
- defined('_JEXEC') or die;
- jimport('joomla.application.component.controller');
- /**
- * @package NewLifeInIT
- * @subpackage com_exchange
- * @since 1.0
- */
- class ExchangeController extends JController
- {
- /**
- * Display the view
- */
- function display()
- {
- JHTML::stylesheet('default.css', 'administrator/components/com_exchange/media/css/');
- // Set the default view name from the Request
- $viewName = JRequest::getWord('view', 'export');
- $lName = JRequest::getVar('layout', 'default', '', 'word');
- if ($view = &$this->getView($viewName))
- {
- // Get/Create the model
- $model = & $this->getModel($viewName);
- switch ($viewName)
- {
- case 'migrate':
- $model->setState('adapter', JRequest::getVar('adapter'));
- break;
- }
- // Push the model into the view (as default)
- $view->setModel($model, true);
- $document = &JFactory::getDocument();
- $view->assignRef('document', $document);
- $view->setLayout($lName);
- $view->display();
- }
- $this->_submenu($viewName);
- }
- /**
- * Display the submenu bar
- */
- function _submenu($viewName)
- {
- JSubMenuHelper::addEntry('Export', 'index.php?option=com_exchange&view=export', $viewName == 'export');
- //JSubMenuHelper::addEntry('Import', 'index.php?option=com_exchange&view=import', $viewName == 'import');
- JSubMenuHelper::addEntry('Migrate', 'index.php?option=com_exchange&view=migrate', $viewName == 'migrate');
- //JSubMenuHelper::addEntry('Files', 'index.php?option=com_exchange&view=files', $viewName == 'files');
- }
- /**
- * export
- */
- function export()
- {
- global $mainframe;
- $db = &JFactory::getDBO();
- $tables = JRequest::getVar('tables', array(), 'post', 'array');
- $options = JRequest::getVar('options', array(), 'post', 'array');
- if (count($tables) < 1) {
- return JError::raiseNotice(500, 'No tables selected');
- }
- $source = JArrayHelper::getValue($options, 'source', '');
- $format = JArrayHelper::getValue($options, 'format', '');
- $output = JArrayHelper::getValue($options, 'output', '');
- $comment = JArrayHelper::getValue($options, 'comment', '');
- $showVars = false;
- $table_creates = array();
- $table_fields = array();
- $sourceStructure = eregi('s', $source);
- $sourceData = eregi('d', $source);
- // data format
- if ($sourceStructure) {
- // structure
- $table_creates = $db->getTableCreate($tables);
- }
- $table_fields = $db->getTableFields($tables);
- if ($sourceData) {
- // data
- }
- $buffer = '';
- $model = $this->getModel('export');
- // output format
- $adapters = $model->getAdapters();
- if (array_key_exists($format, $adapters)) {
- $class = 'ExportAdapter'.$format;
- $adapter = new $class;
- $buffer = $adapter->export($tables, $table_fields, $table_creates, $options);
- }
- $vars = new JObject;
- if ($format == 'sql')
- {
- $vars->set('date', date("M j, Y \a\\t H:i"));
- $vars->set('dbVersion', $db->getVersion());
- $vars->set('phpVersion', phpversion());
- $vars->set('dbName', $mainframe->getCfg('db'));
- $vars->set('dbHost', $mainframe->getCfg('host'));
- $vars->set('comment', str_replace("\n", "\n# ", $comment));
- $vars->set('siteName', $mainframe->getCfg('sitename'));
- }
- JRequest::setVar('view', 'output');
- $view = &$this->getView('output');
- $model->setState('buffer', $buffer);
- $model->setState('vars', $vars);
- $view->setModel($model, true);
- // file format
- switch ($output) {
- case 'screen':
- $view->display();
- break;
- case 'html':
- JRequest::setVar('layout', 'html');
- $view->display();
- break;
- case 'text':
- case 'tar':
- case 'gz':
- case 'bz2':
- JRequest::setVar('layout', 'tofile');
- ob_start();
- $view->display();
- $buffer = ob_get_contents();
- ob_end_clean();
- $basePath = JPath::clean(JPATH_COMPONENT.DS.'files');
- $file = $basePath . $GLOBALS['mosConfig_db'] . '_' . date("Ymd_His") . '.' .$format;
- //$archiveName = JPath::clean(dirname(__FILE__) . '/files/' . $row->option, false);
- if (JFile::write($file, $buffer)) {
- if ($output != 'text') {
- JFile::archive($file, $file, $output, '', $basePath, true, true);
- }
- $this->setRedirect('index.php?option=com_exchange', 'Done');
- } else {
- $this->setRedirect('index.php?option=com_exchange', 'Panic. Cannot write file');
- }
- break;
- default:
- JError::raiseNotice(500, 'Panic. Do not know about file type ' . $output);
- break;
- }
- }
- /**
- *
- */
- function deleteFiles()
- {
- die('Incomplete implementation');
- $files = JRequest::getVar('cid', array(), '', 'array');
- if (count($files) < 1) {
- return JError::raiseWarning(500, 'No files selected');
- }
- $basePath = JPath::clean(dirname(__FILE__) . '/files');
- foreach ($files as $file)
- {
- if (file_exists($basePath . $file))
- {
- if (unlink($basePath . $file)) {
- designerScreens::message(array($file, 'deleted.'));
- }
- else {
- designerScreens::message(array($file, 'failed to be deleted.'));
- }
- }
- else {
- designerScreens::message(array($file, 'doesn\'t exist.'));
- }
- }
- }
- /**
- *
- */
- function restoreList()
- {
- die('Incomplete implementation');
- global $mosConfig_absolute_path;
- $path = JPath::clean($mosConfig_absolute_path . '/administrator/components/com_exchange/files');
- $files = JFolder::files($path, '.');
- foreach ($files as $i=>$file) {
- $files[$i] = array(
- 'file' => $file,
- 'fsize' => number_format(filesize($path . $file)),
- 'mtime' => date ("d-m-Y H:i:s", filemtime($path . $file)),
- 'perms' => JFile::getPermissions($path . '/' . $file)
- );
- }
- require_once(JPATH_DESIGNER . '/f.export.html.php');
- exportScreens::restoreList($files);
- }
- /**
- *
- */
- function restore()
- {
- die('Incomplete implementation');
- $files = mosGetParam($_POST, 'cid', array());
- if (count($files) < 1) {
- designerScreens::message('No files selected');
- return false;
- }
- if (count($files) > 1) {
- designerScreens::message('Too many files selected');
- return false;
- }
- $file = $files[0];
- if (!eregi('\.sql$', $file)) {
- designerScreens::message('Can only support .sql files for now');
- return false;
- }
- $errors = array();
- populateDatabase($file, $errors);
- if (count($errors)) {
- $newErrors = array();
- foreach ($errors as $error) {
- $newErrors[] = stripslashes($error['msg']);
- }
- designerScreens::message($newErrors);
- return false;
- }
- $msg = JText::_('Done') . ' ' . $file;
- $this->setRedirect('index2.php?option=com_exchange&task=restoreList', $msg);
- }
- /**
- * Migrate function
- */
- function migrate()
- {
- $model = $this->getModel('migrate');
- $params = JRequest::getVar('params', array(), 'post', 'array');
- $model->setState('adapter', JRequest::getVar('adapter'));
- $model->setState('srcDBName', JArrayHelper::getValue($params, 'src_dbname', '', 'word'));
- $model->setState('srcPrefix', JArrayHelper::getValue($params, 'src_prefix', '', 'word'));
- $model->setState('destPrefix', JArrayHelper::getValue($params, 'dest_prefix', '', 'word'));
- $model->setState('srcFiles', JRequest::getVar('src_files', array(), 'files', 'array'));
- $model->setState('params', $params);
- if (($timeLimit = JArrayHelper::getValue($params, 'time_limit', 0, 'int')) > 0) {
- set_time_limit($timeLimit);
- }
- if (($memLimit = JArrayHelper::getValue($params, 'mem_limit'))) {
- ini_set('memory_limit', $memLimit);
- }
- $profiler = JProfiler::getInstance('Migration');
- $profiler->mark('Start');
- $model->migrate();
- $profiler->mark('Finished');
- // TODO: Push the log into a view
- JToolBarHelper::title(JText::_('JX Exchange: Migration Result'), 'logo');
- JToolBarHelper::back();
- JToolBarHelper::help('index', 'true');
- JHTML::stylesheet('default.css', 'administrator/components/com_exchange/media/css/');
- echo '<strong>'.implode('<br />', $profiler->getBuffer()).'</strong>';
- echo '<fieldset class="log"><legend>'.JText::_('JX Migration Log').'</legend><div>'.ExchangeLog::toHtml().'</div></fieldset>';
- $this->_submenu('migrate');
- }
- /**
- * Manually installs the component and supporting extensions
- */
- function install()
- {
- global $mainframe;
- $model = &$this->getModel('install');
- $result = $model->install();
- if (JError::isError($result)) {
- $mainframe->enqueueMessage($result->getMessage(), 'notice');
- }
- else {
- $mainframe->enqueueMessage(JText::_('Extension(s) successfully installed'), 'message');
- }
- $document = &JFactory::getDocument();
- $document->addStyleSheet(JURI::base().'components/com_exchange/media/css/default.css');
- JToolBarHelper::title('Exchange: Manual Install', 'logo');
- echo '<p><a href="index.php?option=com_exchange">'.JText::_('Return to Exchange').'</a></p>';
- }
- }