Framework  3.9
api_helper.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8 Copyright (c) 2007-2010 Sonjara, Inc
9 
10 Permission is hereby granted, free of charge, to any person
11 obtaining a copy of this software and associated documentation
12 files (the "Software"), to deal in the Software without
13 restriction, including without limitation the rights to use,
14 copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the
16 Software is furnished to do so, subject to the following
17 conditions:
18 
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21 
22 Except as contained in this notice, the name(s) of the above
23 copyright holders shall not be used in advertising or otherwise
24 to promote the sale, use or other dealings in this Software
25 without prior written authorization.
26 
27 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
38 require_once realpath(dirname(__FILE__)."/search_form.inc");
39 require_once realpath(dirname(__FILE__)."/data_view.inc");
40 
47 abstract class APIFormatter
48 {
49  var $parent;
50 
51  function getClass()
52  {
53  return $this->parent->class;
54  }
55 
63  public abstract function format($items);
64 }
65 
70 {
71  public function format($items)
72  {
73  header("Content-Type: text/json");
74  echo toJSON($items);
75  }
76 }
77 
83 {
84  public function format($items)
85  {
86  header("Content-Type: text/xml");
87  echo toXML($this->getClass()."List", $items);
88  }
89 }
90 
96 {
97  public function format($items)
98  {
99  $view = new DataListView($items);
100  if (count($items))
101  {
102  $obj = $items[0];
103 
104  foreach($obj->getFields() as $field => $type)
105  {
106  if ($obj->filter && $obj->filter->isExcluded($field)) continue;
107  $view->column($field, "{".$field."}");
108  }
109  }
110 
111  $view->writeExcelFile($this->getClass()."Results.xls");
112  }
113 }
114 
121 {
122  public function format($items)
123  {
124  $file = fopen('php://temp/maxmemory:'. (12*1024*1024), 'r+');
125  if (count($items))
126  {
127  $obj = $items[0];
128  $columns = array();
129  foreach($obj->getFields() as $field => $type)
130  {
131  if ($obj->filter && $obj->filter->isExcluded($field)) continue;
132  $columns[] = $field;
133  }
134 
135  fputcsv($file, $columns);
136 
137  foreach($items as $obj)
138  {
139  $values = array();
140  foreach($obj->getFields() as $field => $type)
141  {
142  if ($obj->filter && $obj->filter->isExcluded($field)) continue;
143  $values[] = $obj->get($field);
144  }
145 
146  fputcsv($file, $values);
147  }
148 
149  rewind($file);
150  $output = stream_get_contents($file);
151  fclose($file);
152 
153  header("Content-Type: text/csv");
154  echo $output;
155  }
156  }
157 }
158 
176 {
177  var $file;
179 
186  public function __construct($file, $mimeType = "text/xml")
187  {
188  $this->file = $file;
189  $this->mimeType = $mimeType;
190  }
191 
192  function format($items)
193  {
194  $class = $this->getClass();
195  $template = file_get_contents($this->file);
196 
197  $expr = "\{".$class."\}(.*?)\{\/".$class."\}";
198 
199  $matches = array();
200 
201  if (!preg_match("/$expr/si", $template, $matches))
202  {
203  throw new FakoliException("Badly formatted template $file - $expr<br><pre>$template<pre>");
204  }
205 
206  $format = $matches[1];
207 
208  $result = formatItems($items, $format);
209 
210  $out = str_replace($matches[0], $result, $template);
211 
212  header("Content-Type: $this->mimeType");
213  echo $out;
214  }
215 }
216 
233 {
234  var $class;
238  var $format = "json";
239  var $formatterMap = array();
240 
249  {
250  $this->class = $class;
251  $this->searchFilter = $searchFilter;
252  $this->outputFilter = $outputFilter;
253  $this->constraints = $constraints;
254  $this->format = isset($_GET["_format"]) ? $_GET["_format"] : "json";
255 
256  $this->registerFormat("json", new APIJSONFormatter());
257  $this->registerFormat("xml", new APICanonicalXMLFormatter());
258  $this->registerFormat("excel", new APIExcelFormatter());
259  $this->registerFormat("csv", new APICSVFormatter());
260  }
261 
266  function query()
267  {
268  $target = new $this->class;
269  $target->filter = $this->searchFilter;
270 
271  $parameters = new SearchParameters($target);
272  $parameters->fromGET();
273 
274  $constraints = (Sconstraints) ? $constraints : "WHERE 1=1 ";
275 
276  $query = $parameters->generateConstraint();
277 
278  $results = Query::create($this->class, $query)
279  ->filter($this->outputFilter)
280  ->execute();
281 
282  if (!array_key_exists($this->format, $this->formatterMap))
283  {
284  throw new FakoliException("Unknown format");
285  }
286 
287  return $this->formatterMap[$this->format]->format($results);
288  }
289 
296  function registerFormat($name, $formatter)
297  {
298  $formatter->parent = $this;
299  $this->formatterMap[$name] = $formatter;
300  }
301 }?>
Provides CSV output formatting for APIHelper.
Definition: api_helper.inc:121
format($items)
Override this function to provide your format definition.
Definition: api_helper.inc:122
Standard XML formatter - formats items as XML in Fakoli's canonical DataItem form.
Definition: api_helper.inc:83
format($items)
Override this function to provide your format definition.
Definition: api_helper.inc:84
Excel Formatter - formats items as a Microsoft Excel file.
Definition: api_helper.inc:96
format($items)
Override this function to provide your format definition.
Definition: api_helper.inc:97
Base class for APIFormatters.
Definition: api_helper.inc:48
format($items)
Override this function to provide your format definition.
APIHelper is a class designed to simplify the sharing of data between applications.
Definition: api_helper.inc:233
registerFormat($name, $formatter)
Register a format and its associated formatter.
Definition: api_helper.inc:296
__construct($class, $searchFilter=null, $outputFilter=null, $constraints="")
Creates an APIHelper for the specified class.
Definition: api_helper.inc:248
query()
Process an API query.
Definition: api_helper.inc:266
Standard JSON formatter - formats items as a JSON serialized array of objects.
Definition: api_helper.inc:70
format($items)
Override this function to provide your format definition.
Definition: api_helper.inc:71
APITemplate provides a simple template mechanism for transforming APIHelper output to meet 3rd-party ...
Definition: api_helper.inc:176
__construct($file, $mimeType="text/xml")
Creates a new APITemplate.
Definition: api_helper.inc:186
format($items)
Override this function to provide your format definition.
Definition: api_helper.inc:192
DataListView displays a list of DataItems (or InnerJoinResults) in tabular format.
Definition: data_view.inc:56
static create($class, $constraints="")
Static factory method to create a new Query.
Definition: query.inc:358
The SearchParameters class interprets the set of input parameters for a search and generates the corr...
formatItems($items, $template, $separator="")
Format a list of DataItems using the specified templated.
Definition: data_item.inc:2176
toXML($tag, $objects, $header=null)
Definition: data_item.inc:2080
toJSON($items)
Definition: data_item.inc:2102