PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/component/admin/controllers/import.php

http://gigcalendar3.googlecode.com/
PHP | 333 lines | 288 code | 30 blank | 15 comment | 29 complexity | 1ec47e05b66c2e44ef2183e93f886ed4 MD5 | raw file
  1. <?php
  2. // No direct access to this file
  3. defined('_JEXEC') or die('Restricted access');
  4. // import Joomla controllerform library
  5. jimport('joomla.application.component.controllerform');
  6. require JPATH_ADMINISTRATOR.'/components/com_gigcal/entity_decode.php';
  7. /**
  8. * Import Controller
  9. */
  10. class GigCalControllerImport extends JControllerForm
  11. {
  12. private $bandlist = array();
  13. private $venuelist = array();
  14. private $giglist = array();
  15. private $localTimeZone;
  16. private $dataTimeZone;
  17. public function data()
  18. {
  19. $clearall = JRequest::getCmd('clearall') == 'on';
  20. JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_gigcal/tables');
  21. $this->localTimeZone = new DateTimeZone(date_default_timezone_get());
  22. $this->dataTimeZone = new DateTimeZone($_REQUEST['timezone']);
  23. if (!$clearall)
  24. {
  25. // Initialize the name maps
  26. $db =& JFactory::getDBO();
  27. $db->setQuery('SELECT id, bandname FROM #__gigcal_bands');
  28. $this->bandlist = $db->loadAssocList('bandname');
  29. $db->setQuery('SELECT id, venuename FROM #__gigcal_venues');
  30. $this->venuelist = $db->loadAssocList('venuename');
  31. $db->setQuery('SELECT id, gigname FROM #__gigcal_gigs_import');
  32. $this->giglist = $db->loadAssocList('gigname');
  33. }
  34. $filename = $_FILES['file1']['tmp_name'];
  35. $file = file_get_contents($filename);
  36. $xmlstring = entity_decode($file);
  37. libxml_use_internal_errors(true);
  38. $xml = simplexml_load_string($xmlstring);
  39. if ($xml)
  40. {
  41. $version = $xml->version;
  42. if ($version == '1.0' || $version == '1.0.4' || $version == '1.1')
  43. $this->_ImportData1($xml, $clearall);
  44. else
  45. JError::raiseError(500, 'Cannot import data in this ('.$version.') format');
  46. }
  47. else
  48. {
  49. echo 'XML data load error';
  50. foreach(libxml_get_errors() as $error)
  51. echo '<br />&nbsp;&nbsp;&nbsp;'.$error->message;
  52. }
  53. }
  54. public function settings()
  55. {
  56. JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_gigcal/tables');
  57. $filename = $_FILES['file2']['tmp_name'];
  58. $file = file_get_contents($filename);
  59. $xmlstring = entity_decode($file);
  60. libxml_use_internal_errors(true);
  61. $xml = simplexml_load_string($xmlstring);
  62. if ($xml)
  63. {
  64. $version = $xml->version;
  65. if ($version == '1.0' || $version == '1.0.4' || $version == '1.1')
  66. $this->_ImportSettings1($xml);
  67. else
  68. JError::raiseError(500, 'Cannot import data in this ('.$version.') format');
  69. }
  70. else
  71. {
  72. echo 'XML data load error';
  73. foreach(libxml_get_errors() as $error)
  74. echo '<br />&nbsp;&nbsp;&nbsp;'.$error->message;
  75. }
  76. }
  77. private function _ImportData1($xml, $clearall)
  78. {
  79. echo 'Importing gigcal '.$xml->version.' format data<br/><br/>';
  80. $this->_import1Bands($xml->gigcal_bands, $clearall);
  81. $this->_import1Venues($xml->gigcal_venues, $clearall);
  82. $this->_import1Gigs($xml->gigcal_gigs, $clearall);
  83. }
  84. private function _ImportSettings1($xml)
  85. {
  86. echo 'Importing gigcal '.$xml->version.' format settings<br/><br/>';
  87. $this->_importFields1($xml->gigcal_list_fields, 'list');
  88. $this->_importFields1($xml->gigcal_alist_fields, 'alist');
  89. $this->_importFields1($xml->gigcal_cal_fields, 'cal');
  90. $this->_importFields1($xml->gigcal_menu_fields, 'menu');
  91. $this->_importFields1($xml->gigcal_upcom_fields, 'upcom');
  92. $this->_importConfig1($xml->gigcal_config);
  93. }
  94. private function _Import1Bands($bands, $clearall)
  95. {
  96. $numbandsimported = 0;
  97. $bandtable =& JTable::getInstance('Band', 'GigCalTable');
  98. foreach ($bands->row as $band)
  99. {
  100. if ($clearall)
  101. {
  102. echo 'Clearing all bands<br/>';
  103. $db =& JFactory::getDBO();
  104. $db->setQuery('DELETE FROM #__gigcal_bands');
  105. $db->query();
  106. $db->setQuery('ALTER TABLE #__gigcal_bands AUTO_INCREMENT=1');
  107. $db->query();
  108. $clearall = false;
  109. }
  110. $bandtable->reset();
  111. if (array_key_exists((string)$band->bandname, $this->bandlist))
  112. {
  113. // Update the existing band record
  114. echo 'Updating band : '.$band->bandname.'<br/>';
  115. $bandtable->load($this->bandlist[(string)$band->bandname]['id']);
  116. }
  117. else
  118. {
  119. // import a new band record
  120. echo 'Importing band : '.(string)$band->bandname.'<br/>';
  121. $bandtable->set('id', null);
  122. $bandtable->set('bandname', (string)$band->bandname);
  123. $bandtable->set('published', 1);
  124. }
  125. $bandtable->set('website', (string)$band->website);
  126. $bandtable->set('contactname', (string)$band->contactname);
  127. $bandtable->set('contactphone', (string)$band->contactphone);
  128. $bandtable->set('contactemail', (string)$band->contactemail);
  129. $bandtable->set('city', (string)$band->city);
  130. $bandtable->set('state', (string)$band->state);
  131. $bandtable->set('notes', (string)$band->notes);
  132. if ($bandtable->store())
  133. {
  134. $numbandsimported++;
  135. $this->bandlist[(string)$band->bandname] = array('id'=>$bandtable->get('id'), 'bandname'=>$bandtable->get('bandname'));
  136. }
  137. else
  138. echo ' ERROR: '.$bandtable->getError().'<br/>';
  139. }
  140. echo $numbandsimported.' band(s) imported/updated<br/></br>';
  141. }
  142. private function _Import1Venues($venues, $clearall)
  143. {
  144. $numvenuesimported = 0;
  145. $venuetable =& JTable::getInstance('Venue', 'GigCalTable');
  146. foreach ($venues->row as $venue)
  147. {
  148. if ($clearall)
  149. {
  150. echo 'Clearing all venues<br/>';
  151. $db =& JFactory::getDBO();
  152. $db->setQuery('DELETE FROM #__gigcal_venues');
  153. $db->query();
  154. $db->setQuery('ALTER TABLE #__gigcal_venues AUTO_INCREMENT=1');
  155. $db->query();
  156. $clearall = false;
  157. }
  158. $venuetable->reset();
  159. if (array_key_exists((string)$venue->venuename, $this->venuelist))
  160. {
  161. // Update the existing venue record
  162. echo 'Updating venue : '.$venue->venuename.'<br/>';
  163. $venuetable->load($this->venuelist[(string)$venue->venuename]['id']);
  164. }
  165. else
  166. {
  167. // import a new venue record
  168. echo 'Importing venue : '.(string)$venue->venuename.'<br/>';
  169. $venuetable->set('id', null);
  170. $venuetable->set('venuename', (string)$venue->venuename);
  171. $venuetable->set('published', 1);
  172. }
  173. $venuetable->set('address1', (string)$venue->address1);
  174. $venuetable->set('address2', (string)$venue->address2);
  175. $venuetable->set('city', (string)$venue->city);
  176. $venuetable->set('state', (string)$venue->state);
  177. $venuetable->set('zip', (string)$venue->zip);
  178. $venuetable->set('country', (string)$venue->country);
  179. $venuetable->set('website', (string)$venue->website);
  180. $venuetable->set('phone', (string)$venue->phone);
  181. $venuetable->set('fax', (string)$venue->fax);
  182. $venuetable->set('contactname', (string)$venue->contactname);
  183. $venuetable->set('contactphone', (string)$venue->contactphone);
  184. $venuetable->set('contactemail', (string)$venue->contactemail);
  185. $venuetable->set('info', (string)$venue->info);
  186. if ($venuetable->store())
  187. {
  188. $numvenuesimported++;
  189. $this->venuelist[(string)$venue->venuename] = array('id'=>$venuetable->get('id'), 'venuename'=>$venuetable->get('venuename'));
  190. }
  191. else
  192. echo ' ERROR: '.$venuetable->getError().'<br/>';
  193. }
  194. echo $numvenuesimported.' venue(s) imported/updated<br/></br>';
  195. }
  196. private function _Import1Gigs($gigs, $clearall)
  197. {
  198. $numgigsimported = 0;
  199. $gigtable =& JTable::getInstance('Gig', 'GigCalTable');
  200. foreach ($gigs->row as $gig)
  201. {
  202. if ($clearall)
  203. {
  204. echo 'Clearing all gigs<br/>';
  205. $db =& JFactory::getDBO();
  206. $db->setQuery('DELETE FROM #__gigcal_gigs');
  207. $db->query();
  208. $db->setQuery('ALTER TABLE #__gigcal_gigs AUTO_INCREMENT=1');
  209. $db->query();
  210. $clearall=false;
  211. }
  212. $gigtable->reset();
  213. $band_id = $this->bandlist[(string)$gig->bandname]['id'];
  214. $venue_id = $this->venuelist[(string)$gig->venuename]['id'];
  215. $name = $band_id.'|'.$venue_id.'|'.(int)$gig->gigdate;
  216. if (array_key_exists($name, $this->giglist))
  217. {
  218. // Update the existing gig record
  219. echo 'Updating gig : '.$name.'<br/>';
  220. $gigtable->load($this->giglist[$name]['id']);
  221. }
  222. else
  223. {
  224. // import a new gig record
  225. echo 'Importing gig : '.$name.'<br/>';
  226. $gigtable->set('id', null);
  227. $gigtable->set('published', 1);
  228. }
  229. // Calculate the time offset asuming the data is being loaded that was prviously saved in a different time zone
  230. // This makes the final display show the same as it did on the original data source
  231. $localDateTime = new DateTime(gmdate('Y-m-d H:i:s', (int)$gig->gigdate));
  232. $dataDateTime = new DateTime(gmdate('Y-m-d H:i:s', (int)$gig->gigdate), $this->dataTimeZone);
  233. $localOffset = $this->dataTimeZone->getOffset($localDateTime);
  234. $dataOffset = $this->localTimeZone->getOffset($dataDateTime);
  235. $offset = $localOffset - $dataOffset;
  236. $gigtable->set('gigdate', date('Y-m-d H:i', (int)$gig->gigdate + $offset));
  237. $gigtable->set('band_id', $band_id);
  238. $gigtable->set('venue_id', $venue_id);
  239. $gigtable->set('gigtitle', (string)$gig->gigtitle);
  240. $gigtable->set('covercharge', (string)$gig->covercharge);
  241. $gigtable->set('saleslink', (string)$gig->saleslink);
  242. $gigtable->set('info', (string)$gig->info);
  243. if ($gigtable->store())
  244. {
  245. $numgigsimported++;
  246. $this->giglist[$name] = array('id'=>$gigtable->get('id'), 'name'=>$name);
  247. }
  248. else
  249. echo ' ERROR: '.$gigtable->getError().'<br/>';
  250. }
  251. echo $numgigsimported.' gig(s) imported/updated<br/></br>';
  252. }
  253. private function _ImportFields1($fields, $name)
  254. {
  255. $clearall = true;
  256. $numfieldsimported = 0;
  257. $table =& JTable::getInstance($name.'_Fields', 'GigCalTable');
  258. foreach ($fields->row as $field)
  259. {
  260. if ($clearall)
  261. {
  262. $db =& JFactory::getDBO();
  263. $db->setQuery('DELETE FROM #__gigcal_'.$name.'_fields');
  264. $db->query();
  265. $db->setQuery('ALTER TABLE #__gigcal_'.$name.'_fields AUTO_INCREMENT = 1');
  266. $db->query();
  267. $clearall = false;
  268. }
  269. $table->reset();
  270. // import a new record
  271. echo 'Importing '.$name.'_field : '.(string)$field->fieldname.'<br/>';
  272. $table->set('id', null);
  273. $table->set('fieldname', (string)$field->fieldname);
  274. $table->set('ordering', (string)$field->ordering);
  275. $table->set('published', (string)$field->published);
  276. if ($table->store())
  277. $numfieldsimported++;
  278. else
  279. echo ' ERROR: '.$table->getError().'<br/>';
  280. }
  281. echo $numfieldsimported.' '.$name.'_field(s) imported<br/></br>';
  282. }
  283. private function _ImportConfig1($fields)
  284. {
  285. $numfieldsimported = 0;
  286. $table =& JTable::getInstance('Config', 'GigCalTable');
  287. foreach ($fields->row as $field)
  288. {
  289. $table->reset();
  290. $tablefields = $table->getFields();
  291. foreach ($field as $key=>$data)
  292. if (in_array(strtolower($key), array_map('strtolower', array_keys($tablefields))))
  293. $table->set($key, (string)$data);
  294. else
  295. echo 'Key '.$key.' not recognised - ignoring<br/>';
  296. if ($table->store())
  297. $numfieldsimported++;
  298. else
  299. echo ' ERROR: '.$table->getError().'<br/>';
  300. }
  301. echo $numfieldsimported.' config(s) imported<br/></br>';
  302. }
  303. }