| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Rev$ | $LastChangedBy$ |
|---|
| 4 | $LastChangedDate$ |
|---|
| 5 | +-------------------------------------------------------------------------+ |
|---|
| 6 | | Copyright (c) 2004 - 2010, Kreotek LLC | |
|---|
| 7 | | All rights reserved. | |
|---|
| 8 | +-------------------------------------------------------------------------+ |
|---|
| 9 | | | |
|---|
| 10 | | Redistribution and use in source and binary forms, with or without | |
|---|
| 11 | | modification, are permitted provided that the following conditions are | |
|---|
| 12 | | met: | |
|---|
| 13 | | | |
|---|
| 14 | | - Redistributions of source code must retain the above copyright | |
|---|
| 15 | | notice, this list of conditions and the following disclaimer. | |
|---|
| 16 | | | |
|---|
| 17 | | - Redistributions in binary form must reproduce the above copyright | |
|---|
| 18 | | notice, this list of conditions and the following disclaimer in the | |
|---|
| 19 | | documentation and/or other materials provided with the distribution. | |
|---|
| 20 | | | |
|---|
| 21 | | - Neither the name of Kreotek LLC nor the names of its contributore may | |
|---|
| 22 | | be used to endorse or promote products derived from this software | |
|---|
| 23 | | without specific prior written permission. | |
|---|
| 24 | | | |
|---|
| 25 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|---|
| 26 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|---|
| 27 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
|---|
| 28 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
|---|
| 29 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
|---|
| 30 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
|---|
| 31 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
|---|
| 32 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
|---|
| 33 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
|---|
| 34 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
|---|
| 35 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
|---|
| 36 | | | |
|---|
| 37 | +-------------------------------------------------------------------------+ |
|---|
| 38 | */ |
|---|
| 39 | if(!class_exists("phpbmsReport")) |
|---|
| 40 | include("report_class.php"); |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Basic CSV output report |
|---|
| 44 | * |
|---|
| 45 | * This class implements a report used for creating a CSV file through reporting |
|---|
| 46 | */ |
|---|
| 47 | class generalExport extends phpbmsReport { |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * $maintable |
|---|
| 51 | * @var string the SQL name of the main table to print. |
|---|
| 52 | */ |
|---|
| 53 | var $maintable = ""; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * function generalExport |
|---|
| 58 | * |
|---|
| 59 | * Initialization function |
|---|
| 60 | * |
|---|
| 61 | * @param object $db database object |
|---|
| 62 | * @param string $reportUUID UUID of report record |
|---|
| 63 | * @param string $tabledefUUID UUID of table definition intializing print |
|---|
| 64 | */ |
|---|
| 65 | function generalExport($db, $reportUUID, $tabledefUUID){ |
|---|
| 66 | |
|---|
| 67 | parent::phpbmsReport($db, $reportUUID, $tabledefUUID); |
|---|
| 68 | |
|---|
| 69 | $this->checkForDefaultSettings(); |
|---|
| 70 | |
|---|
| 71 | $therecord = $this->getTableDefInfo(); |
|---|
| 72 | |
|---|
| 73 | $this->maintable = $therecord["maintable"]; |
|---|
| 74 | |
|---|
| 75 | }//end function init |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * function checkForDefaultSettings |
|---|
| 80 | * |
|---|
| 81 | * Checks to make sure loaded report Settings exist and are correct |
|---|
| 82 | */ |
|---|
| 83 | function checkForDefaultSettings(){ |
|---|
| 84 | |
|---|
| 85 | if(!isset($this->settings["showHeader"])) |
|---|
| 86 | $this->settings["showHeader"] = 1; |
|---|
| 87 | |
|---|
| 88 | if(!isset($this->settings["fieldDelimiter"])) |
|---|
| 89 | $this->settings["fieldDelimiter"] = ","; |
|---|
| 90 | |
|---|
| 91 | if(!isset($this->settings["recordDelimiter"])) |
|---|
| 92 | $this->settings["recordDelimiter"] = "\n"; |
|---|
| 93 | |
|---|
| 94 | if(!isset($this->settings["fieldEncapsulation"])) |
|---|
| 95 | $this->settings["fieldEncapsulation"] = "\""; |
|---|
| 96 | |
|---|
| 97 | }//end function checkForDefaultSettings |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * function generate |
|---|
| 102 | * |
|---|
| 103 | * Creates body of the file. |
|---|
| 104 | */ |
|---|
| 105 | function generate(){ |
|---|
| 106 | |
|---|
| 107 | $fromFields = "*"; |
|---|
| 108 | |
|---|
| 109 | $columns = array(); |
|---|
| 110 | foreach($this->settings as $key=>$value){ |
|---|
| 111 | |
|---|
| 112 | if(strpos($key, "column") === 0){ |
|---|
| 113 | |
|---|
| 114 | $pos = substr($key,6)-1; |
|---|
| 115 | $columns[$pos]["field"] = $value; |
|---|
| 116 | |
|---|
| 117 | if(isset($this->settings["titleColumn".($pos + 1)])) |
|---|
| 118 | $columns[$pos]["title"] = $this->settings["titleColumn".($pos +1)]; |
|---|
| 119 | else |
|---|
| 120 | $columns[$pos]["title"] = ""; |
|---|
| 121 | |
|---|
| 122 | }//endif |
|---|
| 123 | |
|---|
| 124 | }//endforeach |
|---|
| 125 | |
|---|
| 126 | if(count($columns)){ |
|---|
| 127 | |
|---|
| 128 | ksort($columns); |
|---|
| 129 | |
|---|
| 130 | $fromFields = ""; |
|---|
| 131 | foreach($columns as $column){ |
|---|
| 132 | |
|---|
| 133 | $fromFields .= ", ".$column["field"]; |
|---|
| 134 | if($column["title"]) |
|---|
| 135 | $fromFields .= " AS `".$column["title"]."`"; |
|---|
| 136 | |
|---|
| 137 | $fromFields.="\n"; |
|---|
| 138 | |
|---|
| 139 | }//endforeach |
|---|
| 140 | |
|---|
| 141 | $fromFields = substr($fromFields, 1); |
|---|
| 142 | |
|---|
| 143 | }//endif |
|---|
| 144 | |
|---|
| 145 | if(isset($this->settings["fromTable"])) |
|---|
| 146 | $querytable = $this->settings["fromTable"]; |
|---|
| 147 | else |
|---|
| 148 | $querytable = $this->maintable; |
|---|
| 149 | |
|---|
| 150 | $querystatement = " |
|---|
| 151 | SELECT |
|---|
| 152 | ".$fromFields." |
|---|
| 153 | FROM |
|---|
| 154 | ".$querytable; |
|---|
| 155 | |
|---|
| 156 | $querystatement = $this->assembleSQL($querystatement); |
|---|
| 157 | |
|---|
| 158 | $queryresult = $this->db->query($querystatement); |
|---|
| 159 | |
|---|
| 160 | $num_fields = $this->db->numFields($queryresult); |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * generating column names. First row is just field names |
|---|
| 164 | */ |
|---|
| 165 | if($this->settings["showHeader"]) |
|---|
| 166 | for($i=0;$i<$num_fields;$i++) |
|---|
| 167 | $this->reportOutput .= $this->settings["fieldDelimiter"].$this->db->fieldName($queryresult, $i); |
|---|
| 168 | |
|---|
| 169 | $this->reportOutput = substr($this->reportOutput, strlen($this->settings["fieldDelimiter"])).$this->settings["recordDelimiter"]; |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Looping through retrieved records. |
|---|
| 173 | */ |
|---|
| 174 | while($therecord = $this->db->fetchArray($queryresult)){ |
|---|
| 175 | |
|---|
| 176 | $line = ""; |
|---|
| 177 | |
|---|
| 178 | foreach($therecord as $value) |
|---|
| 179 | $line .= $this->settings["fieldDelimiter"].$this->settings["fieldEncapsulation"].mysql_real_escape_string($value).$this->settings["fieldEncapsulation"]; |
|---|
| 180 | |
|---|
| 181 | $line = substr($line, strlen($this->settings["fieldDelimiter"])).$this->settings["recordDelimiter"]; |
|---|
| 182 | |
|---|
| 183 | $this->reportOutput .= $line; |
|---|
| 184 | |
|---|
| 185 | }//endwhile |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * removing last \n |
|---|
| 189 | */ |
|---|
| 190 | $this->reportOutput = substr($this->reportOutput, 0, strlen($this->reportOutput) - strlen($this->settings["recordDelimiter"])); |
|---|
| 191 | |
|---|
| 192 | }//end function generate |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * function show |
|---|
| 197 | * |
|---|
| 198 | * Outputing generated data |
|---|
| 199 | */ |
|---|
| 200 | function show(){ |
|---|
| 201 | |
|---|
| 202 | if(!isset($this->settings["filename"])) |
|---|
| 203 | $this->settings["filename"] = $this->maintable."-export.txt"; |
|---|
| 204 | echo "<pre>"; |
|---|
| 205 | //header("Content-type: text/plain"); |
|---|
| 206 | //header('Content-Disposition: attachment; filename="'.$this->settings["filename"].'"'); |
|---|
| 207 | |
|---|
| 208 | echo $this->reportOutput; |
|---|
| 209 | |
|---|
| 210 | }//end function show |
|---|
| 211 | |
|---|
| 212 | }//end class |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | /** |
|---|
| 216 | * PROCESSING |
|---|
| 217 | * ============================================================================= |
|---|
| 218 | */ |
|---|
| 219 | if(!isset($noOutput)){ |
|---|
| 220 | |
|---|
| 221 | session_cache_limiter('private'); |
|---|
| 222 | |
|---|
| 223 | require_once("../include/session.php"); |
|---|
| 224 | |
|---|
| 225 | checkForReportArguments(); |
|---|
| 226 | |
|---|
| 227 | $report = new generalExport($db, $_GET["rid"],$_GET["tid"]); |
|---|
| 228 | $report->setupFromPrintScreen(); |
|---|
| 229 | $report->generate(); |
|---|
| 230 | $report->show(); |
|---|
| 231 | |
|---|
| 232 | }//end if |
|---|
| 233 | |
|---|
| 234 | /** |
|---|
| 235 | * When adding a new report record, the add/edit needs to know what the class |
|---|
| 236 | * name is so that it can instantiate it, and grab it's default settings. |
|---|
| 237 | */ |
|---|
| 238 | if(isset($addingReportRecord)) |
|---|
| 239 | $reportClass ="generalExport"; |
|---|
| 240 | ?> |
|---|