SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/QueryTo.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use InvalidArgumentException;
  17. use PDOStatement;
  18.  
  19.  
  20. /**
  21. * No description yet.
  22. **/
  23. class QueryTo
  24. {
  25.  
  26. /**
  27.   * Exports a database query result as a CSV file.
  28.   * @param PDOStatement|array $result Result set. N.B. the cursor on this statement WILL BE CLOSED by this function.
  29.   * @param array $modifiers ColModifier objects to apply result set before exporting their values,
  30.   * as column_name => ColModifier instance
  31.   * @param array $headings Headings to use on the first row of the CSV; as column_name => heading.
  32.   * The column name itself will be used if a specific heading isn't requested.
  33.   * @return string The CSV file
  34.   * @return bool False on error
  35.   */
  36. static public function csv($result, array $modifiers = [], array $headings = [])
  37. {
  38. $is_pdo = ($result instanceof PDOStatement);
  39. if (!$is_pdo and !is_array($result)) {
  40. throw new InvalidArgumentException('$result must be a PDOStatement or an array');
  41. }
  42.  
  43. if ($is_pdo) {
  44. if ($result->rowCount() == 0) {
  45. $result->closeCursor();
  46. return false;
  47. }
  48. } else if (count($result) == 0) {
  49. return false;
  50. }
  51.  
  52. $out = '';
  53.  
  54. // Header
  55. $j = 0;
  56. if ($is_pdo) {
  57. for ($i = 0; $i < $result->columnCount(); ++$i) {
  58. $col = $result->getColumnMeta($i);
  59. $name = $col['name'];
  60. if (@$modifiers[$name] === false) continue;
  61. if ($j++ > 0) $out .= ',';
  62. $out .= '"' . (isset($headings[$name]) ? $headings[$name] : $name) . '"';
  63. }
  64. } else {
  65. $first_row = Sprout::iterableFirstValue($result);
  66. foreach ($first_row as $name => $junk) {
  67. if (@$modifiers[$name] === false) continue;
  68. if ($j++ > 0) $out .= ',';
  69. $out .= '"' . (isset($headings[$name]) ? $headings[$name] : $name) . '"';
  70. }
  71. }
  72. $out .= "\n";
  73.  
  74. // Data
  75. foreach ($result as $row) {
  76. $j = 0;
  77. foreach ($row as $key => $val) {
  78. if (@$modifiers[$key] === false) continue;
  79. if (!empty($modifiers[$key])) {
  80. if (is_string($modifiers[$key])) $modifiers[$key] = new $modifiers[$key]();
  81. $val = $modifiers[$key]->modify($val, $key);
  82. }
  83.  
  84. $val = str_replace('"', '""', $val);
  85.  
  86. if ($j++ > 0) $out .= ',';
  87. $out .= '"' . $val . '"';
  88. }
  89. $out .= "\n";
  90. }
  91.  
  92. if ($is_pdo) $result->closeCursor();
  93.  
  94. return $out;
  95. }
  96.  
  97. /**
  98.   * Exports a database query result as an XML file.
  99.   *
  100.   * @param PDOStatement|array $result Result set. N.B. the cursor on this statement WILL BE CLOSED by this function.
  101.   * @param array $modifiers ColModifier objects to apply result set before exporting their values,
  102.   * as column_name => ColModifier instance
  103.   * @return string The XML file
  104.   * @return bool False on error
  105.   */
  106. static public function xml($result, array $modifiers = [])
  107. {
  108. $is_pdo = ($result instanceof PDOStatement);
  109. if (!$is_pdo and !is_array($result)) {
  110. throw new InvalidArgumentException('$result must be a PDOStatement or an array');
  111. }
  112.  
  113. if ($is_pdo) {
  114. if ($result->rowCount() == 0) {
  115. $result->closeCursor();
  116. return false;
  117. }
  118. } else if (count($result) == 0) {
  119. return false;
  120. }
  121.  
  122. $out = "<data>\n";
  123.  
  124. // Data
  125. foreach ($result as $row) {
  126. $out .= " <record";
  127.  
  128. if ($row['id']) {
  129. $row['id'] = Enc::xml($row['id']);
  130. $out .= " id=\"{$row['id']}\"";
  131. unset($row['id']);
  132. }
  133.  
  134. $out .= ">\n";
  135.  
  136. foreach ($row as $key => $val) {
  137. if (@$modifiers[$key] === false) continue;
  138. if (!empty($modifiers[$key])) {
  139. if (is_string($modifiers[$key])) $modifiers[$key] = new $modifiers[$key]();
  140. $val = $modifiers[$key]->modify($val, $key);
  141. }
  142.  
  143. $key = preg_replace('/[^a-z0-9]_/', '', strtolower($key));
  144.  
  145. $val = Enc::xml($val);
  146. $val = trim($val);
  147.  
  148. if (strpos($val , "\n") !== false) {
  149. $val = "\n" . $val . "\n ";
  150. }
  151.  
  152. $out .= " <{$key}>{$val}</{$key}>\n";
  153. }
  154.  
  155. $out .= " </record>\n";
  156. }
  157.  
  158. $out .= "</data>\n";
  159.  
  160. if ($is_pdo) $result->closeCursor();
  161.  
  162. return $out;
  163. }
  164. }
  165.  
  166.  
  167.