PageRenderTime 59ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jmd_csv.php

https://github.com/jmdeldin/jmd_csv
PHP | 224 lines | 173 code | 18 blank | 33 comment | 12 complexity | 9f23197f9fde7a48aa9ae996c8aa779d MD5 | raw file
  1. <?php
  2. $plugin = array(
  3. 'author' => 'Jon-Michael Deldin',
  4. 'author_uri' => 'http://jmdeldin.com',
  5. 'description' => 'Batch-import articles from a CSV',
  6. 'type' => 1,
  7. 'version' => '0.2',
  8. );
  9. if (0) {
  10. ?>
  11. # --- BEGIN PLUGIN HELP ---
  12. //inc <README>
  13. # --- END PLUGIN HELP ---
  14. <?php
  15. }
  16. # --- BEGIN PLUGIN CODE ---
  17. if (txpinterface === 'admin')
  18. {
  19. global $textarray;
  20. add_privs('jmd_csv', 1);
  21. register_callback('jmd_csv', 'jmd_csv');
  22. register_tab('extensions', 'jmd_csv', 'jmd_csv');
  23. // i18n
  24. $textarray = array_merge($textarray, array(
  25. 'jmd_csv_file' => 'CSV file:',
  26. 'jmd_csv_file_error' => 'Error reading CSV',
  27. 'jmd_csv_import' => 'Import',
  28. 'jmd_csv_import_csv' => 'Import CSV',
  29. 'jmd_csv_imported' => 'CSV imported successfully.',
  30. ));
  31. }
  32. /**
  33. * Interface for the CSV import.
  34. *
  35. * @param string $event
  36. * @param string $step
  37. */
  38. function jmd_csv($event, $step)
  39. {
  40. global $jmd_csv, $file_base_path;
  41. ob_start('jmd_csv_head');
  42. $jmd_csv = new JMD_CSV();
  43. if ($step === 'import')
  44. {
  45. $file = gps('file');
  46. if ($file)
  47. {
  48. $handle = fopen($file_base_path . DS . $file, 'r');
  49. if ($handle)
  50. {
  51. $jmd_csv->import($handle, gps('status'));
  52. $msg = gTxt('jmd_csv_imported');
  53. }
  54. else
  55. {
  56. $msg = gTxt('jmd_csv_file_error');
  57. }
  58. }
  59. }
  60. pageTop('jmd_csv', (isset($msg) ? $msg : ''));
  61. $gTxt = 'gTxt';
  62. $out = <<<EOD
  63. <fieldset id="jmd_csv">
  64. <legend>{$gTxt('jmd_csv_import_csv')}</legend>
  65. <div>
  66. <label>{$gTxt('jmd_csv_file')}
  67. {$jmd_csv->fileList()}
  68. </label>
  69. </div>
  70. <div>
  71. <label>{$gTxt('import_status')}
  72. {$jmd_csv->statusList()}
  73. </label>
  74. </div>
  75. <button type="submit">{$gTxt('jmd_csv_import')}</button>
  76. </fieldset>
  77. EOD;
  78. echo form($out . eInput('jmd_csv') . sInput('import'));
  79. }
  80. /**
  81. * Inserts CSS into the head.
  82. *
  83. * @param string $buffer
  84. */
  85. function jmd_csv_head($buffer)
  86. {
  87. $find = '</head>';
  88. $insert = <<<EOD
  89. <style type="text/css">
  90. #jmd_csv
  91. {
  92. margin: 0 auto;
  93. padding: 0.5em;
  94. width: 50%;
  95. }
  96. #jmd_csv legend
  97. {
  98. font-weight: 900;
  99. }
  100. #jmd_csv div
  101. {
  102. margin: 0 0 1em;
  103. }
  104. </style>
  105. EOD;
  106. return str_replace($find, $insert . $find, $buffer);
  107. }
  108. class JMD_CSV
  109. {
  110. /**
  111. * Returns a select box of available CSVs.
  112. */
  113. public function fileList()
  114. {
  115. $files = safe_column('filename', 'txp_file',
  116. 'category="jmd_csv"');
  117. if ($files)
  118. {
  119. $out = '<select name="file">';
  120. foreach ($files as $file)
  121. {
  122. $out .= '<option value="' . $file . '">' . $file . '</option>';
  123. }
  124. $out .= '</select>';
  125. return $out;
  126. }
  127. }
  128. /**
  129. * Returns a select box of article-statuses.
  130. */
  131. public function statusList()
  132. {
  133. $statuses = array(
  134. 'draft' => 1,
  135. 'hidden' => 2,
  136. 'pending' => 3,
  137. 'live' => 4,
  138. 'sticky' => 5,
  139. );
  140. $out = '<select name="status">';
  141. foreach ($statuses as $key => $value)
  142. {
  143. $out .= '<option value="' . $value .'">' . gTxt($key) . '</option>';
  144. }
  145. $out .= '</select>';
  146. return $out;
  147. }
  148. /**
  149. * Reads a CSV and inserts it into the textpattern table.
  150. *
  151. * @param resource $handle File opened with fopen()
  152. * @param int $status Article status.
  153. */
  154. public function import($handle, $status)
  155. {
  156. global $prefs, $txp_user;
  157. $row = 1;
  158. while (($csv = fgetcsv($handle, 0, ',')) !== FALSE)
  159. {
  160. $fields = count($csv);
  161. if ($row === 1)
  162. {
  163. for ($i = 0; $i < $fields; $i++)
  164. {
  165. $header[$i] = $csv[$i];
  166. }
  167. }
  168. else
  169. {
  170. $insert = '';
  171. foreach ($header as $key => $value)
  172. {
  173. // escape all fields
  174. $csv[$key] = doSlash($csv[$key]);
  175. if ($value === 'Title')
  176. {
  177. $url_title = stripSpace($csv[$key], 1);
  178. }
  179. if ($value === 'Body' || $value === 'Excerpt')
  180. {
  181. $insert .= "{$value}_html='{$csv[$key]}',";
  182. }
  183. $insert .= "{$value}='{$csv[$key]}',";
  184. }
  185. $uid = md5(uniqid(rand(),true));
  186. $insert .= <<<EOD
  187. AuthorID='{$txp_user}',
  188. LastModID='{$txp_user}',
  189. AnnotateInvite='{$prefs['comments_default_invite']}',
  190. url_title='{$url_title}',
  191. uid='{$uid}',
  192. feed_time=now(),
  193. Posted=now(),
  194. LastMod=now(),
  195. Status={$status},
  196. textile_body=0,
  197. textile_excerpt=0
  198. EOD;
  199. safe_insert('textpattern', $insert);
  200. }
  201. $row++;
  202. }
  203. }
  204. }
  205. # --- END PLUGIN CODE ---
  206. ?>