|
1
|
|
|
<?php namespace XoopsModules\Smartobject; |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* You may not change or alter any portion of this comment or credits |
|
5
|
|
|
* of supporting developers from this source code or any supporting source code |
|
6
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
7
|
|
|
* |
|
8
|
|
|
* This program is distributed in the hope that it will be useful, |
|
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @copyright XOOPS Project https://xoops.org/ |
|
15
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
|
16
|
|
|
* @package |
|
17
|
|
|
* @since |
|
18
|
|
|
* @author XOOPS Development Team |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use CriteriaElement; |
|
22
|
|
|
use XoopsModules\Smartobject; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Contains the classes for easily exporting data |
|
26
|
|
|
* |
|
27
|
|
|
* @license GNU |
|
28
|
|
|
* @author marcan <[email protected]> |
|
29
|
|
|
* @link http://www.smartfactory.ca The SmartFactory |
|
30
|
|
|
* @package SmartObject |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* SmartObjectExport class |
|
35
|
|
|
* |
|
36
|
|
|
* Class to easily export data from SmartObjects |
|
37
|
|
|
* |
|
38
|
|
|
* @package SmartObject |
|
39
|
|
|
* @author marcan <[email protected]> |
|
40
|
|
|
* @link http://www.smartfactory.ca The SmartFactory |
|
41
|
|
|
*/ |
|
42
|
|
|
class Export |
|
43
|
|
|
{ |
|
44
|
|
|
public $handler; |
|
45
|
|
|
public $criteria; |
|
46
|
|
|
public $fields; |
|
47
|
|
|
public $format; |
|
48
|
|
|
public $filename; |
|
49
|
|
|
public $filepath; |
|
50
|
|
|
public $options; |
|
51
|
|
|
public $outputMethods = false; |
|
52
|
|
|
public $notDisplayFields; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Constructor |
|
56
|
|
|
* |
|
57
|
|
|
* @param PersistableObjectHandler $objectHandler SmartObjectHandler handling the data we want to export |
|
58
|
|
|
* @param \CriteriaElement $criteria containing the criteria of the query fetching the objects to be exported |
|
59
|
|
|
* @param array|bool $fields fields to be exported. If FALSE then all fields will be exported |
|
60
|
|
|
* @param bool|string $filename name of the file to be created |
|
61
|
|
|
* @param bool|string $filepath path where the file will be saved |
|
62
|
|
|
* @param string $format format of the ouputed export. Currently only supports CSV |
|
63
|
|
|
* @param array|bool $options options of the format to be exported in |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct( |
|
66
|
|
|
PersistableObjectHandler $objectHandler, |
|
67
|
|
|
\CriteriaElement $criteria = null, |
|
68
|
|
|
$fields = false, |
|
69
|
|
|
$filename = false, |
|
70
|
|
|
$filepath = false, |
|
|
|
|
|
|
71
|
|
|
$format = 'csv', |
|
72
|
|
|
$options = false |
|
73
|
|
|
) { |
|
74
|
|
|
$this->handler = $objectHandler; |
|
75
|
|
|
$this->criteria = $criteria; |
|
76
|
|
|
$this->fields = $fields; |
|
77
|
|
|
$this->filename = $filename; |
|
78
|
|
|
$this->format = $format; |
|
79
|
|
|
$this->options = $options; |
|
80
|
|
|
$this->notDisplayFields = false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Renders the export |
|
85
|
|
|
* @param $filename |
|
86
|
|
|
*/ |
|
87
|
|
|
public function render($filename) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->filename = $filename; |
|
90
|
|
|
|
|
91
|
|
|
$objects = $this->handler->getObjects($this->criteria); |
|
92
|
|
|
$rows = []; |
|
93
|
|
|
$columnsHeaders = []; |
|
94
|
|
|
$firstObject = true; |
|
95
|
|
|
foreach ($objects as $object) { |
|
96
|
|
|
$row = []; |
|
97
|
|
|
foreach ($object->vars as $key => $var) { |
|
98
|
|
|
if ((!$this->fields || in_array($key, $this->fields)) && !in_array($key, $this->notDisplayFields)) { |
|
99
|
|
|
if ($this->outputMethods && isset($this->outputMethods[$key]) |
|
100
|
|
|
&& method_exists($object, $this->outputMethods[$key])) { |
|
101
|
|
|
$method = $this->outputMethods[$key]; |
|
102
|
|
|
$row[$key] = $object->$method(); |
|
103
|
|
|
} else { |
|
104
|
|
|
$row[$key] = $object->getVar($key); |
|
105
|
|
|
} |
|
106
|
|
|
if ($firstObject) { |
|
107
|
|
|
// then set the columnsHeaders array as well |
|
108
|
|
|
$columnsHeaders[$key] = $var['form_caption']; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
$firstObject = false; |
|
113
|
|
|
$rows[] = $row; |
|
114
|
|
|
unset($row); |
|
115
|
|
|
} |
|
116
|
|
|
$data = []; |
|
117
|
|
|
$data['rows'] = $rows; |
|
118
|
|
|
$data['columnsHeaders'] = $columnsHeaders; |
|
119
|
|
|
$smartExportRenderer = new ExportRenderer($data, $this->filename, $this->filepath, $this->format, $this->options); |
|
|
|
|
|
|
120
|
|
|
$smartExportRenderer->execute(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set an array contaning the alternate methods to use instead of the default getVar() |
|
125
|
|
|
* |
|
126
|
|
|
* $outputMethods array example: 'uid' => 'getUserName'... |
|
127
|
|
|
* @param $outputMethods |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setOuptutMethods($outputMethods) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->outputMethods = $outputMethods; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/* |
|
135
|
|
|
* Set an array of fields that we don't want in export |
|
136
|
|
|
*/ |
|
137
|
|
|
/** |
|
138
|
|
|
* @param $fields |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setNotDisplayFields($fields) |
|
141
|
|
|
{ |
|
142
|
|
|
if (!$this->notDisplayFields) { |
|
143
|
|
|
if (is_array($fields)) { |
|
144
|
|
|
$this->notDisplayFields = $fields; |
|
145
|
|
|
} else { |
|
146
|
|
|
$this->notDisplayFields = [$fields]; |
|
147
|
|
|
} |
|
148
|
|
|
} else { |
|
149
|
|
|
if (is_array($fields)) { |
|
150
|
|
|
$this->notDisplayFields = array_merge($this->notDisplayFields, $fields); |
|
151
|
|
|
} else { |
|
152
|
|
|
$this->notDisplayFields[] = $fields; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.