-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportTripleSDataWriter.php
320 lines (298 loc) · 11.7 KB
/
exportTripleSDataWriter.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* exportTripleSDataWriter part of exportTripleS Plugin for LimeSurvey
* Writer for the plugin
*
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2014-2023 Denis Chenu <http://sondages.pro>
* @license AGPL v3
* @version 3.1.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
Yii::import('application.helpers.admin.export.*');
class exportTripleSDataWriter extends Writer
{
private $output;
private $hasOutputHeader;
private $aColunInfo = array();
public $pluginSettings = array();
private $position = 0;
private $ident = 1;
protected $customFieldmap = array();
//protected $oSurvey;
protected $iSurveyId;
protected $sLanguageCode;
function __construct($settings)
{
mb_internal_encoding('utf-8'); // @important
$this->output = '';
$this->hasOutputHeader = false;
$basedir = dirname(__FILE__); // this will give you the / directory
if (intval(App()->getConfig('versionnumber')) < 4) {
Yii::setPathOfAlias('exportTripleS', $basedir . DIRECTORY_SEPARATOR . 'legacy');
} else {
Yii::setPathOfAlias('exportTripleS', $basedir);
}
foreach ($settings as $name => $value) {
$this->pluginSettings[$name] = $value;
}
}
public function init(\SurveyObj $oSurvey, $sLanguageCode, \FormattingOptions $oOptions)
{
parent::init($oSurvey, $sLanguageCode, $oOptions);
//$this->oSurvey=$oSurvey;
$this->iSurveyId = $oSurvey->id;
$this->sLanguageCode = $sLanguageCode;
$now = date("Ymd-His");
$oOptions->headingFormat = "full"; // force to use own code
$oOptions->answerFormat = "short"; // force to use own code
Yii::import('exportTripleS.tripleSHelper');
$tripleSfunction = new tripleSHelper($this->pluginSettings);
$tripleSfunction->iSurveyId = $this->iSurveyId;
$tripleSfunction->sLanguageCode = $this->sLanguageCode;
$this->customFieldmap = $tripleSfunction->createTripleSFieldmap($oSurvey, $sLanguageCode, $oOptions);
if ($this->pluginSettings['stringAnsi'] == "ansi") {
setlocale(LC_ALL, $this->getLocaleLanguage($this->sLanguageCode));
}
if ($oOptions->output == 'display') {
header('Content-Encoding: UTF-8');
if (!$this->pluginSettings['debugMode']) {
header("Content-Disposition: attachment; filename=survey_{$oSurvey->id}_{$now}_triples.dat");
}
//if(intval($this->pluginSettings['debugMode'])<2)
header("Content-type: text/plain; charset=UTF-8");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
$this->handle = fopen('php://output', 'w');
} elseif ($oOptions->output == 'file') {
$this->handle = fopen($this->filename, 'w');
}
}
protected function out($content)
{
fwrite($this->handle, $content . "\n");
}
protected function outputRecord($headers, $values, FormattingOptions $oOptions)
{
if (!$this->hasOutputHeader) {
$return = "\xEF\xBB\xBF"; // UTF-8 BOM
$this->hasOutputHeader = true;
}
$aValues = array();
$aColumns = $oOptions->selectedColumns;
foreach ($values as $key => $value) {
$sColumn = $aColumns[$key];
if ($this->pluginSettings['debugMode'] > 2) {
$aValues[] = $sColumn . "|";
}
$aValues[] = $this->tripleSgetValue($value, $sColumn);
if ($this->pluginSettings['debugMode'] > 2) {
$aValues[] = "-";
}
}
echo implode($aValues) . "\n";
}
private function tripleSgetValue($sValue, $sColumn)
{
if (isset($this->customFieldmap[$sColumn])) {
if ($this->pluginSettings['debugMode'] >= 4) {
return array('column' => $sColumn,'value' => $sValue,'triples' => $this->customFieldmap[$sColumn]);
}
$return = "";// Some column need 2 function
foreach ($this->customFieldmap[$sColumn] as $aTripleS) {
if (isset($aTripleS['@attributes']['type'])) {
$function = "getValue" . $aTripleS['@attributes']['type'];
$return .= $this->$function($sValue, $aTripleS);
}
}
return $return;
} else {
return "";
}
}
public function close()
{
fclose($this->handle);
}
private function getValueCharacter($sValue, $aTriplesField)
{
if ($aTriplesField['info']['type'] == "D") {
$numCar = strlen("YYYY-MM-DD HH:ii:ss");
if (is_null($sValue)) {
return str_repeat(" ", $numCar);
}
if (strlen($sValue) < $numCar) {
return str_repeat(" ", $numCar); /* invalid date */
}
$oDate = new DateTime($sValue);
if ($oDate->format("Y-m-d H:i:s") == substr($sValue, 0, $numCar)) {
return $oDate->format("Y-m-d H:i:s");
}
return str_repeat(" ", $numCar); /* invalid date */
}
$iSize = $aTriplesField['size'];
if (is_null($sValue)) {
return str_repeat(" ", $iSize);
}
$sValue = self::filterStringForTripleS($sValue, $this->pluginSettings['stringAnsi'] == "ansi");
if ($this->pluginSettings['stringAnsi'] == "ansi") {
return str_pad(substr($sValue, 0, $iSize), $iSize, " ", STR_PAD_RIGHT);
}
return self::mb_str_pad(mb_substr($sValue, 0, $iSize), $iSize, " ", STR_PAD_RIGHT, 'UTF8');
}
private function getValueQuantity($sValue, $aTriplesField)
{
if ($aTriplesField['info']['type'] == "D") {
if (is_null($sValue)) {
return str_repeat(" ", 8 + 6);
}
$oDate = new DateTime($sValue);
return $oDate->format("YmdHis");
}
$sMin = $aTriplesField['values']['range']['@attributes']['from'];
$sMax = $aTriplesField['values']['range']['@attributes']['to'];
$iSize = max(strlen($sMin), strlen($sMax));
if (is_null($sValue)) {
return str_repeat(" ", $iSize);
}
if ($sValue == "" || $sValue == " ") {
return str_repeat(" ", $iSize);
}
$aSize = explode(".", $sMax);
if (isset($aSize[1])) {
$iDecimalLength = strlen($aSize[1]);
} else {
$iDecimalLength = 0;
}
$aValue = explode(".", $sValue);
$aMax = explode(".", $sMax);
$aMin = explode(".", $sMin);
if ($aValue[0] < $aMin[0]) {
$sNewValue = $aMin[0];
} elseif ($aValue[0] > $aMax[0]) {
$sNewValue = $aMax[0];
} else {
$sNewValue = $aValue[0];
}
if ($iDecimalLength) {
$sNewValue .= ".";
if (isset($aValue[1])) {
$sNewValue .= str_pad(substr($aValue[1], 0, $iDecimalLength), $iDecimalLength, "0");
} else {
$sNewValue .= str_repeat("0", $iDecimalLength);
}
}
// Fix min Max ?
return str_pad($sNewValue, $iSize, " ", STR_PAD_LEFT);
}
private function getValueSingle($sValue, $aTriplesField)
{
$iStart = intval($aTriplesField['position']['@attributes']['start']);
$iFinish = intval($aTriplesField['position']['@attributes']['finish']);
$iSize = $iFinish - $iStart + 1;
if (is_null($sValue)) {
return str_repeat(" ", $iSize);
}
if (isset($aTriplesField['info']['replace'])) {
$sValue = isset($aTriplesField['info']['replace'][$sValue]) ? $aTriplesField['info']['replace'][$sValue] : "";
}
// else test if code exist
if ($sValue == "" && $iSize >= strlen($this->pluginSettings['listChoiceNoANswer'])) {
$sValue = $this->pluginSettings['listChoiceNoANswer'];
}
return str_pad($sValue, $iSize, " ");
}
/**
* return date part of SQL date-time
* @param $sValue
* @param $aTriplesField
* @retrun string
*/
private function getValueDate($sValue, $aTriplesField)
{
if (is_null($sValue) || strlen($sValue) < 10) {
return str_repeat(" ", 8); /* Not sure for this one : non set for missing datetime in TripleS book */
}
if (strlen($sValue) < 10) {
return str_repeat(" ", 8); /* invalid date */
}
$oDate = new DateTime($sValue);
if ($oDate->format("Y-m-d") == substr($sValue, 0, 10)) {
return $oDate->format("Ymd");
}
return str_repeat(" ", 8); /* invalid date */
}
private function getValueTime($sValue, $aTriplesField)
{
if (is_null($sValue)) {
return str_repeat(" ", 4); // Not sure for this one : non set for missing datetime in TripleS book
}
if (strlen($sValue) < 19) {
return str_repeat(" ", 4); /* invalid time */
}
$oDate = new DateTime($sValue);
if ($oDate->format("H:i:s") == substr($sValue, 11, 19)) {
return $oDate->format("Hi");
}
return str_repeat(" ", 4); /* invalid time */
}
private function getValueLogical($sValue, $aTriplesField)
{
if (is_null($sValue)) {
return " ";
}
return (int)(bool)$sValue;
}
/*
* Filter string : no line feed
*
* @param string $string to filter
* @return string filtered string
*/
public static function filterStringForTripleS($string, $bAnsi = false)
{
if ($bAnsi) {
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
}
if (version_compare(substr(PCRE_VERSION, 0, strpos(PCRE_VERSION, ' ')), '7.0') > -1) {
return preg_replace(array('~\R~u'), array(' '), $string);
}
return preg_replace("/[\n\r]/", " ", $string);
}
private static function mb_str_pad($str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = null)
{
$encoding = $encoding === null ? mb_internal_encoding() : $encoding;
$padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT;
$padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT;
$pad_len -= mb_strlen($str, $encoding);
$targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len;
$strToRepeatLen = mb_strlen($pad_str, $encoding);
$repeatTimes = ceil($targetLen / $strToRepeatLen);
$repeatedString = str_repeat($pad_str, max(0, $repeatTimes)); // safe if used with valid unicode sequences (any charset)
$before = $padBefore ? mb_substr($repeatedString, 0, floor($targetLen), $encoding) : '';
$after = $padAfter ? mb_substr($repeatedString, 0, ceil($targetLen), $encoding) : '';
return $before . $str . $after;
}
private function getLocaleLanguage($sLanguageCode)
{
$aLanguageLocale = array(
'fr' => 'fr_FR',
'de' => 'de_DE',
'de-informal' => 'de_DE',
);
if (isset($aLanguageLocale[$sLanguageCode])) {
return $aLanguageLocale[$sLanguageCode];
}
return 'en_US';
}
}