Posted By

stewartduffy on 05/13/11


Tagged

php


Versions (?)

Wordpress, PODS CMS data export template


 / Published in: PHP
 

1) name the file something like "template-pods-export.php" 2) create a new Pods Page & choose the newly created template 3) goto your worpress URL/export 4) enter filename.csv & pod name you want to export. 5) Submit. 6) Away Laughing

  1. <?php
  2. /*
  3. Template Name: Export Pods Data
  4. */
  5. ?>
  6.  
  7.  
  8. <?php
  9. function curPageURL() {
  10. $pageURL = 'http';
  11. if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  12. $pageURL .= "://";
  13. if ($_SERVER["SERVER_PORT"] != "80") {
  14. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  15. } else {
  16. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  17. }
  18. return $pageURL;
  19. }
  20. ?>
  21.  
  22. <form action="<?php echo curPageURL(); ?>" method="post">
  23.  
  24. <label for="out_file">Output File</label><br />
  25. <input type="text" name="out_file" id="out_file" /><br />
  26.  
  27. <label for="use_pod">Pod to Export</label><br />
  28. <input type="text" name="use_pod" id="use_pod" /><br />
  29.  
  30. <input type="submit" name="export_submit" />
  31.  
  32. </form>
  33.  
  34.  
  35. <?php
  36. if (isset($_POST['export_submit'])) {
  37.  
  38. $out_file = $_POST['out_file'];
  39. $use_pod = $_POST['use_pod'];
  40.  
  41. export($out_file, $use_pod);
  42. }
  43. ?>
  44.  
  45. <?php
  46.  
  47. function export($out_file, $use_pod){
  48.  
  49. $export = "csv"; // Can be "php" or "csv" (default: php)
  50. $Record = new PodAPI($use_pod, $export);
  51. $data = $Record->export();
  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.  
  57. if (($fp = fopen($out_file, 'w')) === FALSE) { //Attempt to open $out_file for writing and check for success.
  58. die ("Could not open $out_file. Export aborted."); //Fail if unable to.
  59. }
  60.  
  61.  
  62. if ('csv' == strtolower($export)) { //If a CSV file is desired
  63. $data_keys = array_keys($data[0]); //Get the pod column labels
  64. fputcsv($fp, $data_keys); //Write the column labels as the first line of the CSV file.
  65. foreach ($data as $line) { //Loop through the data line by line
  66. foreach ($line as &$field) { //Loop through each line of data by field
  67. if (is_array($field)) { //If the field is a PICK column
  68. array_walk($field, 'comma_trans'); //Translate any commas in the field to HTML "&#44;"
  69. $field = 'array(' . implode(', ', $field) . ')'; //Implode the items into a comma separated list wrapped in array().
  70. }
  71. }
  72. fputcsv($fp, $line); //Write the line to the file.
  73. }
  74. } else { //Otherwise, output the data as PHP
  75. fwrite($fp, var_export($data, TRUE));
  76. }
  77. fclose($fp); //Close the file
  78.  
  79. echo "Export complete. <a href='$out_file'>Open $out_file</a><br /><br />\n";
  80.  
  81. } else { //User doesn't have permission
  82. include(get_404_template()); //Nothing to see here, move along...
  83. exit(); //Exit.
  84. }
  85.  
  86. }
  87.  
  88.  
  89. function comma_trans(&$item)
  90. {
  91. $item = strtr($item, ',', '&#44'); //Translates any commas into their HTML entity.
  92. }
  93. ?>

Report this snippet  

You need to login to post a comment.