PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/includes/admin-import.php

https://gitlab.com/futuredrive630/wp-event-list
PHP | 305 lines | 259 code | 23 blank | 23 comment | 26 complexity | d0ee4c429303607b2959f5b1538eadda MD5 | raw file
  1. <?php
  2. if(!defined('WPINC')) {
  3. exit;
  4. }
  5. require_once(EL_PATH.'includes/options.php');
  6. require_once(EL_PATH.'admin/includes/admin-functions.php');
  7. require_once(EL_PATH.'includes/db.php');
  8. require_once(EL_PATH.'includes/categories.php');
  9. // This class handles all data for the admin new event page
  10. class EL_Admin_Import {
  11. private static $instance;
  12. private $options;
  13. private $functions;
  14. private $db;
  15. private $categories;
  16. private $import_data;
  17. private $example_file_path;
  18. public static function &get_instance() {
  19. // Create class instance if required
  20. if(!isset(self::$instance)) {
  21. self::$instance = new self();
  22. }
  23. // Return class instance
  24. return self::$instance;
  25. }
  26. private function __construct() {
  27. $this->options = &EL_Options::get_instance();
  28. $this->functions = &EL_Admin_Functions::get_instance();
  29. $this->db = &EL_Db::get_instance();
  30. $this->categories = &EL_Categories::get_instance();
  31. $this->example_file_path = EL_URL.'/files/events-import-example.csv';
  32. }
  33. public function show_import() {
  34. if(!current_user_can('edit_posts')) {
  35. wp_die(__('You do not have sufficient permissions to access this page.'));
  36. }
  37. echo '
  38. <div class="wrap">
  39. <div id="icon-edit-pages" class="icon32"><br /></div>
  40. <h2>'.__('Import Events','event-list').'</h2>';
  41. // Review import
  42. if(isset($_FILES['el_import_file'])) {
  43. $this->show_import_review();
  44. }
  45. // Finish import (add events)
  46. elseif(isset($_POST['reviewed_events'])) {
  47. $import_error = $this->import_events();
  48. $this->show_import_finished($import_error);
  49. }
  50. // Import form
  51. else {
  52. $this->show_import_form();
  53. }
  54. echo '
  55. </div>';
  56. }
  57. private function show_import_form() {
  58. echo '
  59. <h3>'.__('Step','event-list').' 1: '.__('Set import file and options','event-list').'</h3>
  60. <form action="" id="el_import_upload" method="post" enctype="multipart/form-data">
  61. '.$this->functions->show_option_table('import').'
  62. <input type="submit" name="button-upload-submit" id="button-upload-submit" class="button" value="'.__('Import Event Data','event-list').'" />
  63. </form>
  64. <br /><br />
  65. <h3>'.__('Example file','event-list').'</h4>
  66. <p>'.sprintf(__('You can download an example file %1$shere%2$s (CSV delimiter is a comma!)','event-list'), '<a href="'.$this->example_file_path.'">', '</a>').'</p>
  67. <p><em>'.__('Note','event-list').':</em> '.__('Do not change the column header and separator line (first two lines), otherwise the import will fail!','event-list').'</p>';
  68. }
  69. private function show_import_review() {
  70. $file = $_FILES['el_import_file']['tmp_name'];
  71. // check for file existence (upload failed?)
  72. if(!is_file($file)) {
  73. echo '<h3>'.__('Sorry, there has been an error.','event-list').'</h3>';
  74. echo __('The file does not exist, please try again.','event-list').'</p>';
  75. return;
  76. }
  77. // check for file extension (csv) first
  78. $file_parts = pathinfo($_FILES['el_import_file']['name']);
  79. if($file_parts['extension'] !== "csv") {
  80. echo '<h3>'.__('Sorry, there has been an error.','event-list').'</h3>';
  81. echo __('The file is not a CSV file.','event-list').'</p>';
  82. return;
  83. }
  84. // safe settings
  85. $this->safe_import_settings();
  86. // parse file
  87. $import_data = $this->parseImportFile($file);
  88. // parsing failed?
  89. if(is_wp_error($import_data)) {
  90. echo '<h3>'.__('Sorry, there has been an error.','event-list').'</h3>';
  91. echo '<p>' . esc_html($import_data->get_error_message()).'</p>';
  92. return;
  93. }
  94. // TODO: $this->import_data vs. $import_data ?
  95. $this->import_data = $import_data;
  96. $serialized = serialize($this->import_data);
  97. // show review page
  98. echo '
  99. <h3>'.__('Step','event-list').' 2: '.__('Event review and category selection','event-list').'</h3>
  100. <form method="POST" action="?page=el_admin_main&action=import">';
  101. wp_nonce_field('autosavenonce', 'autosavenonce', false, false);
  102. wp_nonce_field('closedpostboxesnonce', 'closedpostboxesnonce', false, false);
  103. wp_nonce_field('meta-box-order-nonce', 'meta-box-order-nonce', false, false);
  104. echo '
  105. <div id="poststuff">
  106. <div id="post-body" class="metabox-holder columns-2">
  107. <div id="post-body-content">';
  108. foreach($this->import_data as $event) {
  109. $this->show_event($event);
  110. }
  111. echo '
  112. </div>
  113. <div id="postbox-container-1" class="postbox-container">
  114. <div id="side-sortables" class="meta-box-sortables ui-sortable">';
  115. add_meta_box('event-categories', __('Categories'), array(&$this, 'render_category_metabox'),'event-list', 'advanced', 'default', null);
  116. add_meta_box('event-publish', __('Import','event-list'), array(&$this, 'render_publish_metabox'), 'event-list');
  117. do_meta_boxes('event-list', 'advanced', null);
  118. echo '
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. <input type="hidden" name="reviewed_events" id="reviewed_events" value="'.esc_html($serialized).'" />
  124. </form>';
  125. }
  126. private function show_import_finished($with_error) {
  127. if(!$with_error) {
  128. echo '
  129. <h3>'.__('Import with errors!','event-list').'</h3>
  130. '.sprintf(__('An error occurred during import! Please send your import file to %1$sthe administrator%2$s for analysis.','event-list'), '<a href="mailto:'.get_option('admin_email').'">', '</a>');
  131. }
  132. else {
  133. echo '
  134. <h3>'.__('Import successful!','event-list').'</h3>
  135. <a href="?page=el_admin_main">'.__('Go back to All Events','event-list').'</a>';
  136. }
  137. }
  138. private function show_event($event) {
  139. echo '
  140. <p>
  141. <span style="font-weight: bold;">'.__('Title','event-list').':</span> <span style="font-style: italic;">'.$event['title'].'</span><br />
  142. <span style="font-weight: bold;">'.__('Start Date','event-list').':</span> <span style="font-style: italic;">'.$event['start_date'].'</span><br />
  143. <span style="font-weight: bold;">'.__('End Date','event-list').':</span> <span style="font-style: italic;">'.$event['end_date'].'</span><br />
  144. <span style="font-weight: bold;">'.__('Time','event-list').':</span> <span style="font-style: italic;">'.$event['time'].'</span><br />
  145. <span style="font-weight: bold;">'.__('Location','event-list').':</span> <span style="font-style: italic;">'.$event['location'].'</span><br />
  146. <span style="font-weight: bold;">'.__('Details','event-list').':</span> <span style="font-style: italic;">'.$event['details'].'</span>
  147. </p>';
  148. }
  149. /**
  150. * @return WP_Error
  151. */
  152. private function parseImportFile($file) {
  153. $delimiter = ',';
  154. $header = array('title', 'start date', 'end date', 'time', 'location', 'details');
  155. $separator = array('sep=,');
  156. // list of events to import
  157. $events = array();
  158. $file_handle = fopen($file, 'r');
  159. $lineNum = 0;
  160. while(!feof($file_handle)) {
  161. $line = fgetcsv($file_handle, 1024);
  162. // skip empty line
  163. if(empty($line)) {
  164. continue;
  165. }
  166. if($lineNum === 0) {
  167. if($line === $separator) {
  168. continue;
  169. }
  170. if($line === $header) {
  171. $lineNum += 1;
  172. continue;
  173. }
  174. else {
  175. var_dump($line);
  176. var_dump($header);
  177. return new WP_Error('CSV_parse_error', __('There was an error when reading this CSV file.','event-list'));
  178. }
  179. }
  180. $events[] = array(
  181. 'title' => $line[0],
  182. 'start_date' => $line[1],
  183. 'end_date' => !empty($line[2]) ? $line[2] : $line[1],
  184. 'time' => $line[3],
  185. 'location' => $line[4],
  186. 'details' => $line[5],
  187. );
  188. $lineNum += 1;
  189. }
  190. //close file
  191. fclose($file_handle);
  192. return $events;
  193. }
  194. private function safe_import_settings() {
  195. foreach($this->options->options as $oname => $o) {
  196. if('import' == $o['section'] && isset($_POST[$oname])) {
  197. $this->options->set($oname, $_POST[$oname]);
  198. }
  199. }
  200. }
  201. public function render_publish_metabox() {
  202. echo '
  203. <div class="submitbox">
  204. <div id="delete-action"><a href="?page=el_admin_main" class="submitdelete deletion">'.__('Cancel').'</a></div>
  205. <div id="publishing-action"><input type="submit" class="button button-primary button-large" name="import" value="'.__('Import','event-list').'" id="import"></div>
  206. <div class="clear"></div>
  207. </div>';
  208. }
  209. public function render_category_metabox($post, $metabox) {
  210. echo '
  211. <div id="taxonomy-category" class="categorydiv">
  212. <div id="category-all" class="tabs-panel">';
  213. $cat_array = $this->categories->get_cat_array('name', 'asc');
  214. if(empty($cat_array)) {
  215. echo __('No categories available.');
  216. }
  217. else {
  218. echo '
  219. <ul id="categorychecklist" class="categorychecklist form-no-clear">';
  220. $level = 0;
  221. $event_cats = explode('|', substr($metabox['args']['event_cats'], 1, -1));
  222. foreach($cat_array as $cat) {
  223. if($cat['level'] > $level) {
  224. //new sub level
  225. echo '
  226. <ul class="children">';
  227. $level++;
  228. }
  229. while($cat['level'] < $level) {
  230. // finish sub level
  231. echo '
  232. </ul>';
  233. $level--;
  234. }
  235. $level = $cat['level'];
  236. $checked = in_array($cat['slug'], $event_cats) ? 'checked="checked" ' : '';
  237. echo '
  238. <li id="'.$cat['slug'].'" class="popular-catergory">
  239. <label class="selectit">
  240. <input value="'.$cat['slug'].'" type="checkbox" name="categories[]" id="categories" '.$checked.'/> '.$cat['name'].'
  241. </label>
  242. </li>';
  243. }
  244. echo '
  245. </ul>';
  246. }
  247. echo '
  248. </div>
  249. </div>';
  250. }
  251. private function import_events() {
  252. $reviewed_events = unserialize(stripslashes($_POST['reviewed_events']));
  253. $categories = isset($_POST['categories']) ? $_POST['categories'] : '';
  254. if(isset($categories)) {
  255. foreach($reviewed_events as &$event) {
  256. $event['categories'] = $categories;
  257. }
  258. }
  259. $ret = array();
  260. foreach($reviewed_events as &$event) {
  261. // check if dates have correct formats
  262. $start_date = DateTime::createFromFormat($this->options->get('el_import_date_format'), $event['start_date']);
  263. $end_date = DateTime::createFromFormat($this->options->get('el_import_date_format'), $event['end_date']);
  264. if($start_date) {
  265. $event['start_date'] = $start_date->format('Y-m-d');
  266. if($end_date) {
  267. $event['end_date'] = $end_date->format('Y-m-d');
  268. }
  269. else {
  270. $event['end_date'] = '';
  271. }
  272. $ret[] = $this->db->update_event($event);
  273. }
  274. else {
  275. return false;
  276. }
  277. }
  278. // TODO: Improve error messages
  279. return $ret;
  280. }
  281. }
  282. ?>