PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/exchange/code/trunk/administrator/components/com_exchange/controller.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 330 lines | 229 code | 51 blank | 50 comment | 28 complexity | b3b0ac49cb083a336e96d8a86f744df8 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: controller.php 280 2010-09-18 02:14:15Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_exchange
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later.
  8. * @link http://www.theartofjoomla.com
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die;
  12. jimport('joomla.application.component.controller');
  13. /**
  14. * @package NewLifeInIT
  15. * @subpackage com_exchange
  16. * @since 1.0
  17. */
  18. class ExchangeController extends JController
  19. {
  20. /**
  21. * Display the view
  22. */
  23. function display()
  24. {
  25. JHTML::stylesheet('default.css', 'administrator/components/com_exchange/media/css/');
  26. // Set the default view name from the Request
  27. $viewName = JRequest::getWord('view', 'export');
  28. $lName = JRequest::getVar('layout', 'default', '', 'word');
  29. if ($view = &$this->getView($viewName))
  30. {
  31. // Get/Create the model
  32. $model = & $this->getModel($viewName);
  33. switch ($viewName)
  34. {
  35. case 'migrate':
  36. $model->setState('adapter', JRequest::getVar('adapter'));
  37. break;
  38. }
  39. // Push the model into the view (as default)
  40. $view->setModel($model, true);
  41. $document = &JFactory::getDocument();
  42. $view->assignRef('document', $document);
  43. $view->setLayout($lName);
  44. $view->display();
  45. }
  46. $this->_submenu($viewName);
  47. }
  48. /**
  49. * Display the submenu bar
  50. */
  51. function _submenu($viewName)
  52. {
  53. JSubMenuHelper::addEntry('Export', 'index.php?option=com_exchange&view=export', $viewName == 'export');
  54. //JSubMenuHelper::addEntry('Import', 'index.php?option=com_exchange&view=import', $viewName == 'import');
  55. JSubMenuHelper::addEntry('Migrate', 'index.php?option=com_exchange&view=migrate', $viewName == 'migrate');
  56. //JSubMenuHelper::addEntry('Files', 'index.php?option=com_exchange&view=files', $viewName == 'files');
  57. }
  58. /**
  59. * export
  60. */
  61. function export()
  62. {
  63. global $mainframe;
  64. $db = &JFactory::getDBO();
  65. $tables = JRequest::getVar('tables', array(), 'post', 'array');
  66. $options = JRequest::getVar('options', array(), 'post', 'array');
  67. if (count($tables) < 1) {
  68. return JError::raiseNotice(500, 'No tables selected');
  69. }
  70. $source = JArrayHelper::getValue($options, 'source', '');
  71. $format = JArrayHelper::getValue($options, 'format', '');
  72. $output = JArrayHelper::getValue($options, 'output', '');
  73. $comment = JArrayHelper::getValue($options, 'comment', '');
  74. $showVars = false;
  75. $table_creates = array();
  76. $table_fields = array();
  77. $sourceStructure = eregi('s', $source);
  78. $sourceData = eregi('d', $source);
  79. // data format
  80. if ($sourceStructure) {
  81. // structure
  82. $table_creates = $db->getTableCreate($tables);
  83. }
  84. $table_fields = $db->getTableFields($tables);
  85. if ($sourceData) {
  86. // data
  87. }
  88. $buffer = '';
  89. $model = $this->getModel('export');
  90. // output format
  91. $adapters = $model->getAdapters();
  92. if (array_key_exists($format, $adapters)) {
  93. $class = 'ExportAdapter'.$format;
  94. $adapter = new $class;
  95. $buffer = $adapter->export($tables, $table_fields, $table_creates, $options);
  96. }
  97. $vars = new JObject;
  98. if ($format == 'sql')
  99. {
  100. $vars->set('date', date("M j, Y \a\\t H:i"));
  101. $vars->set('dbVersion', $db->getVersion());
  102. $vars->set('phpVersion', phpversion());
  103. $vars->set('dbName', $mainframe->getCfg('db'));
  104. $vars->set('dbHost', $mainframe->getCfg('host'));
  105. $vars->set('comment', str_replace("\n", "\n# ", $comment));
  106. $vars->set('siteName', $mainframe->getCfg('sitename'));
  107. }
  108. JRequest::setVar('view', 'output');
  109. $view = &$this->getView('output');
  110. $model->setState('buffer', $buffer);
  111. $model->setState('vars', $vars);
  112. $view->setModel($model, true);
  113. // file format
  114. switch ($output) {
  115. case 'screen':
  116. $view->display();
  117. break;
  118. case 'html':
  119. JRequest::setVar('layout', 'html');
  120. $view->display();
  121. break;
  122. case 'text':
  123. case 'tar':
  124. case 'gz':
  125. case 'bz2':
  126. JRequest::setVar('layout', 'tofile');
  127. ob_start();
  128. $view->display();
  129. $buffer = ob_get_contents();
  130. ob_end_clean();
  131. $basePath = JPath::clean(JPATH_COMPONENT.DS.'files');
  132. $file = $basePath . $GLOBALS['mosConfig_db'] . '_' . date("Ymd_His") . '.' .$format;
  133. //$archiveName = JPath::clean(dirname(__FILE__) . '/files/' . $row->option, false);
  134. if (JFile::write($file, $buffer)) {
  135. if ($output != 'text') {
  136. JFile::archive($file, $file, $output, '', $basePath, true, true);
  137. }
  138. $this->setRedirect('index.php?option=com_exchange', 'Done');
  139. } else {
  140. $this->setRedirect('index.php?option=com_exchange', 'Panic. Cannot write file');
  141. }
  142. break;
  143. default:
  144. JError::raiseNotice(500, 'Panic. Do not know about file type ' . $output);
  145. break;
  146. }
  147. }
  148. /**
  149. *
  150. */
  151. function deleteFiles()
  152. {
  153. die('Incomplete implementation');
  154. $files = JRequest::getVar('cid', array(), '', 'array');
  155. if (count($files) < 1) {
  156. return JError::raiseWarning(500, 'No files selected');
  157. }
  158. $basePath = JPath::clean(dirname(__FILE__) . '/files');
  159. foreach ($files as $file)
  160. {
  161. if (file_exists($basePath . $file))
  162. {
  163. if (unlink($basePath . $file)) {
  164. designerScreens::message(array($file, 'deleted.'));
  165. }
  166. else {
  167. designerScreens::message(array($file, 'failed to be deleted.'));
  168. }
  169. }
  170. else {
  171. designerScreens::message(array($file, 'doesn\'t exist.'));
  172. }
  173. }
  174. }
  175. /**
  176. *
  177. */
  178. function restoreList()
  179. {
  180. die('Incomplete implementation');
  181. global $mosConfig_absolute_path;
  182. $path = JPath::clean($mosConfig_absolute_path . '/administrator/components/com_exchange/files');
  183. $files = JFolder::files($path, '.');
  184. foreach ($files as $i=>$file) {
  185. $files[$i] = array(
  186. 'file' => $file,
  187. 'fsize' => number_format(filesize($path . $file)),
  188. 'mtime' => date ("d-m-Y H:i:s", filemtime($path . $file)),
  189. 'perms' => JFile::getPermissions($path . '/' . $file)
  190. );
  191. }
  192. require_once(JPATH_DESIGNER . '/f.export.html.php');
  193. exportScreens::restoreList($files);
  194. }
  195. /**
  196. *
  197. */
  198. function restore()
  199. {
  200. die('Incomplete implementation');
  201. $files = mosGetParam($_POST, 'cid', array());
  202. if (count($files) < 1) {
  203. designerScreens::message('No files selected');
  204. return false;
  205. }
  206. if (count($files) > 1) {
  207. designerScreens::message('Too many files selected');
  208. return false;
  209. }
  210. $file = $files[0];
  211. if (!eregi('\.sql$', $file)) {
  212. designerScreens::message('Can only support .sql files for now');
  213. return false;
  214. }
  215. $errors = array();
  216. populateDatabase($file, $errors);
  217. if (count($errors)) {
  218. $newErrors = array();
  219. foreach ($errors as $error) {
  220. $newErrors[] = stripslashes($error['msg']);
  221. }
  222. designerScreens::message($newErrors);
  223. return false;
  224. }
  225. $msg = JText::_('Done') . ' ' . $file;
  226. $this->setRedirect('index2.php?option=com_exchange&task=restoreList', $msg);
  227. }
  228. /**
  229. * Migrate function
  230. */
  231. function migrate()
  232. {
  233. $model = $this->getModel('migrate');
  234. $params = JRequest::getVar('params', array(), 'post', 'array');
  235. $model->setState('adapter', JRequest::getVar('adapter'));
  236. $model->setState('srcDBName', JArrayHelper::getValue($params, 'src_dbname', '', 'word'));
  237. $model->setState('srcPrefix', JArrayHelper::getValue($params, 'src_prefix', '', 'word'));
  238. $model->setState('destPrefix', JArrayHelper::getValue($params, 'dest_prefix', '', 'word'));
  239. $model->setState('srcFiles', JRequest::getVar('src_files', array(), 'files', 'array'));
  240. $model->setState('params', $params);
  241. if (($timeLimit = JArrayHelper::getValue($params, 'time_limit', 0, 'int')) > 0) {
  242. set_time_limit($timeLimit);
  243. }
  244. if (($memLimit = JArrayHelper::getValue($params, 'mem_limit'))) {
  245. ini_set('memory_limit', $memLimit);
  246. }
  247. $profiler = JProfiler::getInstance('Migration');
  248. $profiler->mark('Start');
  249. $model->migrate();
  250. $profiler->mark('Finished');
  251. // TODO: Push the log into a view
  252. JToolBarHelper::title(JText::_('JX Exchange: Migration Result'), 'logo');
  253. JToolBarHelper::back();
  254. JToolBarHelper::help('index', 'true');
  255. JHTML::stylesheet('default.css', 'administrator/components/com_exchange/media/css/');
  256. echo '<strong>'.implode('<br />', $profiler->getBuffer()).'</strong>';
  257. echo '<fieldset class="log"><legend>'.JText::_('JX Migration Log').'</legend><div>'.ExchangeLog::toHtml().'</div></fieldset>';
  258. $this->_submenu('migrate');
  259. }
  260. /**
  261. * Manually installs the component and supporting extensions
  262. */
  263. function install()
  264. {
  265. global $mainframe;
  266. $model = &$this->getModel('install');
  267. $result = $model->install();
  268. if (JError::isError($result)) {
  269. $mainframe->enqueueMessage($result->getMessage(), 'notice');
  270. }
  271. else {
  272. $mainframe->enqueueMessage(JText::_('Extension(s) successfully installed'), 'message');
  273. }
  274. $document = &JFactory::getDocument();
  275. $document->addStyleSheet(JURI::base().'components/com_exchange/media/css/default.css');
  276. JToolBarHelper::title('Exchange: Manual Install', 'logo');
  277. echo '<p><a href="index.php?option=com_exchange">'.JText::_('Return to Exchange').'</a></p>';
  278. }
  279. }