Wordpress, PODS CMS data import template


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Template Name: Import Pods Data
  4. */
  5. ?>
  6.  
  7. <?php
  8. function curPageURL() {
  9. $pageURL = 'http';
  10. if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  11. $pageURL .= "://";
  12. if ($_SERVER["SERVER_PORT"] != "80") {
  13. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  14. } else {
  15. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  16. }
  17. return $pageURL;
  18. }
  19. ?>
  20.  
  21. <form action="<?php echo curPageURL(); ?>" method="post">
  22.  
  23. <label for="in_file">In File</label><br />
  24. <input type="text" name="in_file" id="in_file" /><br />
  25.  
  26. <label for="use_pod">Pod to Export</label><br />
  27. <input type="text" name="use_pod" id="use_pod" /><br />
  28.  
  29. <input type="submit" name="in_submit" />
  30.  
  31. </form>
  32.  
  33.  
  34. <?php
  35. if (isset($_POST['in_submit'])) {
  36.  
  37. $in_file = $_POST['in_file'];
  38. $use_pod = $_POST['use_pod'];
  39.  
  40. import($in_file, $use_pod);
  41. }
  42. ?>
  43.  
  44. <?php
  45.  
  46.  
  47. function import($in_file, $use_pod){
  48.  
  49. //$in_file = 'file.csv'; //This would open file.csv in the blog root.
  50. //$use_pod = 'newsletters'; //The name of the pod to use.
  51. $import = "csv"; // Can be "php" or "csv" (default: php)
  52.  
  53. global $user_ID , $user_level;
  54. get_currentuserinfo(); //Get the information about the current user.
  55. if ($user_ID && (10 == $user_level)): //Check to see if the user is logged in and is an administrator
  56. $php_data = pods_csv_import($in_file, ',', 5000);
  57. $Record = new PodAPI($use_pod, 'php');
  58. $Record->import($php_data);
  59.  
  60. echo "<pre>\n";
  61. var_dump($php_data);
  62. echo "</pre>\n";
  63.  
  64. else: //User isn't logged in or an administrator
  65. include(get_404_template()); //Nothing to see here, move along...
  66. exit(); //Exit.
  67. endif;
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. function pods_csv_import($filename, $delimiter = ',', $max_line_len = 1000) {
  75. $first_row = true;
  76. if (false == ($handle = fopen($filename, "r"))) die("<p>Could not open $filename</p>");
  77. while (($data = fgetcsv($handle, $max_line_len, $delimiter)) !== FALSE) {
  78. if ($first_row) {
  79. $field_names = $data;
  80. $first_row = false;
  81. } else {
  82. foreach ($field_names as $key => $field) {
  83. if ($data[$key] <> ($new_data = preg_replace('/^array\((.*)\)$/is', '$1', $data[$key]))) {
  84. $new_data = explode(",",trim($new_data));
  85. array_walk($new_data, 'comma_trans');
  86. $data[$key] = $new_data;
  87. }
  88. $tmp[$field] = $data[$key];
  89. }
  90. $out[] = $tmp;
  91. }
  92. }
  93. fclose($handle);
  94. return $out;
  95. }
  96.  
  97. function comma_trans(&$item)
  98. {
  99. $item = strtr($item, '&#44', ','); //Translates HTML &#44; to commas
  100. }
  101.  
  102. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.