1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<?php
class JsonExportFilter extends BeditaExportFilter
{
protected $typeName = 'BE-Json';
protected $mimeTypes = array('text/json', 'application/json');
public $defaultExtension = 'json';
public $label = 'JSON data';
public $options = array();
public function export(array $objects, array $options = array()) {
$tmpDir = TMP . 'json' . DS . md5(time());
if(!is_dir($tmpDir)) {
if(!is_dir(TMP . 'json')) {
if(@mkdir(TMP . 'json', 0755, true) === false) {
throw new BeditaException("Unable to create TMP json dir");
}
}
if(@mkdir($tmpDir, 0755, true) === false) {
throw new BeditaException("Unable to create $tmpDir");
}
}
$fileName = $tmpDir . DS . $options['filename'];
if (!strrpos($options['filename'],'.json') || strrpos($options['filename'],'.json') === (strlen($options['filename'])-4)) {
$fileName.= '.json';
}
$options['filename'] = $fileName;
$options['returnType'] = 'JSON';
$options['no-media'] = true;
$options['all'] = false;
if(Configure::read('debug') > 0) {
$options['logLevel'] = 3;
}
$dataTransfer = ClassRegistry::init('DataTransfer');
$content = $dataTransfer->export($objects, $options);
$folder = new Folder($tmpDir);
if (!$folder->delete($tmpDir)) {
$this->log('Error deleting temporary folder ' . $tmpDir,'error');
}
$res = array();
$res['content'] = $content;
$res['contentType'] = 'text/json';
$res['size'] = strlen($res['content']);
$res = array_merge($dataTransfer->getResult(), $res);
return $res;
}
}