ExportToJson

The Contact Form DB ExportToJson class.

Defined (1)

The class is defined in the following location(s).

/ExportToJson.php  
  1. class ExportToJson extends ExportBase implements CFDBExport { 
  2.  
  3. public function export($formName, $options = null) { 
  4. $this->setOptions($options); 
  5. $this->setCommonOptions(); 
  6.  
  7. $varName = 'cf7db'; 
  8. $html = false; // i.e. whether to create an HTML script tag and Javascript variable 
  9.  
  10. if ($options && is_array($options)) { 
  11.  
  12. if (isset($options['html'])) { 
  13. $html = $options['html']; 
  14.  
  15. if (isset($options['var'])) { 
  16. $varName = $options['var']; 
  17.  
  18. // Security Check 
  19. if (!$this->isAuthorized()) { 
  20. $this->assertSecurityErrorMessage(); 
  21. return; 
  22.  
  23. // Headers 
  24. $contentType = $html ? 'Content-Type: text/html; charset=UTF-8' : 'Content-Type: application/json; charset=UTF-8'; 
  25. $this->echoHeaders($contentType); 
  26.  
  27. // Get the data 
  28. $this->setDataIterator($formName); 
  29.  
  30. if ($this->isFromShortCode) { 
  31. ob_start(); 
  32.  
  33. // Break out sections: Before, Content, After 
  34. $before = ''; 
  35. $content = ''; 
  36. $after = ''; 
  37. if (isset($options['content'])) { 
  38. $contentParser = new CFDBShortCodeContentParser; 
  39. list($before, $content, $after) = $contentParser->parseBeforeContentAfter($options['content']); 
  40.  
  41. if ($before) { 
  42. // Allow for short codes in "before" 
  43. echo do_shortcode($before); 
  44.  
  45. if ($html) { 
  46. ?> 
  47. <script type="text/javascript" language="JavaScript"> 
  48. <!-- 
  49. var <?php echo $varName; ?> = <?php $this->echoJsonEncode(); ?>; 
  50. //--> 
  51. </script> 
  52. <?php 
  53.  
  54. else { 
  55. echo $this->echoJsonEncode(); 
  56.  
  57. if ($after) { 
  58. // Allow for short codes in "after" 
  59. echo do_shortcode($after); 
  60.  
  61. if ($this->isFromShortCode) { 
  62. // If called from a shortcode, need to return the text,  
  63. // otherwise it can appear out of order on the page 
  64. $output = ob_get_contents(); 
  65. ob_end_clean(); 
  66. return $output; 
  67.  
  68. protected function echoJsonEncode() { 
  69. $format = 'map'; 
  70. if ($this->options && isset($this->options['format'])) { 
  71. if ($this->options['format'] == 'array' || $this->options['format'] == 'arraynoheader') { 
  72. $format = $this->options['format']; 
  73. if (isset($this->options['header']) && $this->options['header'] == 'false') { 
  74. // Another way to turn off the header 
  75. $format = 'arraynoheader'; 
  76.  
  77. if ($format == 'map') { 
  78.  
  79. // Create the column name JSON values only once 
  80. $jsonEscapedColumns = array(); 
  81. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  82. $colDisplayValue = $col; 
  83. if ($this->headers && isset($this->headers[$col])) { 
  84. $colDisplayValue = $this->headers[$col]; 
  85. $jsonEscapedColumns[$col] = json_encode($colDisplayValue); 
  86.  
  87. echo "[\n"; 
  88. $firstRow = true; 
  89. while ($this->dataIterator->nextRow()) { 
  90. if ($firstRow) { 
  91. $firstRow = false; 
  92. else { 
  93. echo ", \n"; 
  94. echo '{'; 
  95. $firstCol = true; 
  96. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  97. if ($firstCol) { 
  98. $firstCol = false; 
  99. else { 
  100. echo ', '; 
  101. printf('%s:%s',  
  102. $jsonEscapedColumns[$col],  
  103. json_encode( 
  104. (isset($this->dataIterator->row[$col]) ? 
  105. $this->dataIterator->row[$col] : 
  106. ''))); 
  107. echo '}'; 
  108. echo "\n]"; 
  109. else { // 'array' || 'arraynoheader' 
  110. echo "[\n"; 
  111. $firstRow = true; 
  112. if ($format == 'array' || 
  113. // allow header option to override 
  114. (isset($this->options['header']) && $this->options['header'] == 'true')) { 
  115. // Add header 
  116. $firstCol = true; 
  117. echo '['; 
  118. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  119. if ($firstCol) { 
  120. $firstCol = false; 
  121. else { 
  122. echo ', '; 
  123. $colDisplayValue = $col; 
  124. if ($this->headers && isset($this->headers[$col])) { 
  125. $colDisplayValue = $this->headers[$col]; 
  126. echo json_encode($colDisplayValue); 
  127. echo ']'; 
  128. $firstRow = false; 
  129. // Export data rows 
  130. while ($this->dataIterator->nextRow()) { 
  131. if ($firstRow) { 
  132. $firstRow = false; 
  133. else { 
  134. echo ", \n"; 
  135. $firstCol = true; 
  136. echo '['; 
  137. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  138. if ($firstCol) { 
  139. $firstCol = false; 
  140. else { 
  141. echo ', '; 
  142. echo json_encode(isset($this->dataIterator->row[$col]) ? 
  143. $this->dataIterator->row[$col] : 
  144. ''); 
  145. echo "]"; 
  146. echo "\n]"; 
  147.