3
* Xibo - Digital Signage - http://www.xibo.org.uk
4
* Copyright (C) 2011-13 Daniel Garner
6
* This file is part of Xibo.
8
* Xibo is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU Affero General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
13
* Xibo is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU Affero General Public License for more details.
18
* You should have received a copy of the GNU Affero General Public License
19
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
23
defined('XIBO') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");
25
class DataSetData extends Data
27
private $updateWatermark;
29
public function __construct() {
31
$this->updateWatermark = true;
33
parent::__construct();
37
* List all data for this dataset
38
* @param int $dataSetId The DataSet ID
40
public function GetData($dataSetId) {
42
if ($dataSetId == 0 || $dataSetId == '')
43
return $this->SetError(25001, __('Missing dataSetId'));
46
$dbh = \Xibo\Storage\PDOConnect::init();
48
$sth = $dbh->prepare('SELECT datasetdata.DataSetColumnID, datasetdata.RowNumber, datasetdata.Value
50
INNER JOIN datasetcolumn
51
ON datasetcolumn.DataSetColumnID = datasetdata.DataSetColumnID
52
WHERE datasetcolumn.DataSetID = :dataset_id');
54
$sth->execute(array('dataset_id' => $dataSetId));
56
$results = $sth->fetchAll();
58
// Check there are some columns returned
59
if (count($results) <= 0)
60
$this->ThrowError(__('No data'));
64
foreach($results as $row) {
66
$col['datasetcolumnid'] = \Xibo\Helper\Sanitize::int($row['DataSetColumnID']);
67
$col['rownumber'] = \Xibo\Helper\Sanitize::int($row['RowNumber']);
68
$col['value'] = \Xibo\Helper\Sanitize::string($row['Value']);
73
Log::notice(sprintf('Returning %d columns.', count($rows)), 'DataSetColumn', 'GetData');
77
catch (Exception $e) {
79
Log::error($e->getMessage());
81
if (!$this->IsError())
82
$this->SetError(1, __('Unknown Error'));
88
public function Add($dataSetColumnId, $rowNumber, $value)
90
if ($dataSetColumnId == 0 || $dataSetColumnId == '')
91
return $this->SetError(25001, __('Missing dataSetColumnId'));
93
if ($rowNumber == 0 || $rowNumber == '')
94
return $this->SetError(25001, __('Missing rowNumber'));
97
$dbh = \Xibo\Storage\PDOConnect::init();
99
$SQL = "INSERT INTO datasetdata (DataSetColumnID, RowNumber, Value) ";
100
$SQL .= " VALUES (:datasetcolumnid, :rownumber, :value) ";
102
$sth = $dbh->prepare($SQL);
104
'datasetcolumnid' => $dataSetColumnId,
105
'rownumber' => $rowNumber,
109
$id = $dbh->lastInsertId();
111
// Update the Water Mark
112
$this->UpdateWatermarkWithColumnId($dataSetColumnId);
114
Log::notice('Complete', 'DataSetData', 'Add');
118
catch (Exception $e) {
119
Log::error($e->getMessage());
120
return $this->SetError(25005, __('Could not add DataSet Data'));
124
public function Edit($dataSetColumnId, $rowNumber, $value)
126
if ($dataSetColumnId == 0 || $dataSetColumnId == '')
127
return $this->SetError(25001, __('Missing dataSetColumnId'));
129
if ($rowNumber == 0 || $rowNumber == '')
130
return $this->SetError(25001, __('Missing rowNumber'));
133
$dbh = \Xibo\Storage\PDOConnect::init();
135
$SQL = "UPDATE datasetdata SET Value = :value ";
136
$SQL .= " WHERE DataSetColumnID = :datasetcolumnid AND RowNumber = :rownumber";
138
$sth = $dbh->prepare($SQL);
140
'datasetcolumnid' => $dataSetColumnId,
141
'rownumber' => $rowNumber,
145
$this->UpdateWatermarkWithColumnId($dataSetColumnId);
147
Log::notice('Complete', 'DataSetData', 'Edit');
151
catch (Exception $e) {
152
Log::error($e->getMessage());
153
return $this->SetError(25005, __('Could not edit DataSet Data'));
157
public function Delete($dataSetColumnId, $rowNumber)
160
$dbh = \Xibo\Storage\PDOConnect::init();
162
$SQL = "DELETE FROM datasetdata ";
163
$SQL .= " WHERE DataSetColumnID = :datasetcolumnid AND RowNumber = :rownumber";
165
$sth = $dbh->prepare($SQL);
167
'datasetcolumnid' => $dataSetColumnId,
168
'rownumber' => $rowNumber
171
$this->UpdateWatermarkWithColumnId($dataSetColumnId);
173
Log::notice('Complete', 'DataSetData', 'Delete');
177
catch (Exception $e) {
178
Log::error($e->getMessage());
179
return $this->SetError(25005, __('Could not delete Data for Column/Row'));
183
public function DeleteAll($dataSetId) {
185
if ($dataSetId == 0 || $dataSetId == '')
186
return $this->SetError(25001, __('Missing dataSetId'));
189
$dbh = \Xibo\Storage\PDOConnect::init();
192
$SQL .= "DELETE FROM datasetdata WHERE DataSetColumnId IN ( ";
193
$SQL .= " SELECT DataSetColumnID FROM datasetcolumn WHERE DataSetID = :datasetid ";
196
$sth = $dbh->prepare($SQL);
198
'datasetid' => $dataSetId
201
$this->UpdateWatermark($dataSetId);
205
catch (Exception $e) {
206
Log::error($e->getMessage());
207
return $this->SetError(25005, __('Could not delete Data for entire DataSet'));
212
* Update the Water Mark to indicate the last data edit
213
* @param int $dataSetColumnId The Data Set Column ID
215
private function UpdateWatermarkWithColumnId($dataSetColumnId) {
217
if (!$this->updateWatermark)
221
$dbh = \Xibo\Storage\PDOConnect::init();
223
$sth = $dbh->prepare('SELECT DataSetID FROM `datasetcolumn` WHERE DataSetColumnID = :dataset_column_id');
225
'dataset_column_id' => $dataSetColumnId
228
$this->UpdateWatermark($sth->fetchColumn(0));
230
catch (Exception $e) {
232
Log::error($e->getMessage());
234
if (!$this->IsError())
235
$this->SetError(1, __('Unknown Error'));
242
* Update the Water Mark to indicate the last data edit
243
* @param int $dataSetId The Data Set ID to Update
245
private function UpdateWatermark($dataSetId) {
247
if ($dataSetId == 0 || $dataSetId == '')
248
return $this->SetError(25001, __('Missing dataSetId'));
250
if (!$this->updateWatermark)
253
Log::notice(sprintf('Updating water mark on DataSetId: %d', $dataSetId), 'DataSetData', 'UpdateWatermark');
256
$dbh = \Xibo\Storage\PDOConnect::init();
258
$sth = $dbh->prepare('UPDATE `dataset` SET LastDataEdit = :last_data_edit WHERE DataSetID = :dataset_id');
260
'last_data_edit' => time(),
261
'dataset_id' => $dataSetId
264
// Get affected Campaigns
266
$dataSet = new DataSet($this->db);
267
$campaigns = $dataSet->GetCampaignsForDataSet($dataSetId);
270
$display = new Display($this->db);
272
foreach ($campaigns as $campaignId) {
273
// Assess all displays
274
$campaigns = $display->NotifyDisplays($campaignId);
277
catch (Exception $e) {
279
Log::error($e->getMessage());
281
if (!$this->IsError())
282
$this->SetError(1, __('Unknown Error'));
288
public function ImportCsv($dataSetId, $csvFile, $spreadSheetMapping, $overwrite = false, $ignoreFirstRow = true) {
290
if ($dataSetId == 0 || $dataSetId == '')
291
return $this->SetError(25001, __('Missing dataSetId'));
293
if (!file_exists($csvFile))
294
return $this->SetError(25001, __('CSV File does not exist'));
296
if (!is_array($spreadSheetMapping) || count($spreadSheetMapping) <= 0)
297
return $this->SetError(25001, __('Missing spreadSheetMapping'));
299
Log::notice('spreadSheetMapping: ' . json_encode($spreadSheetMapping), 'DataSetData', 'ImportCsv');
301
$this->updateWatermark = false;
304
$dbh = \Xibo\Storage\PDOConnect::init();
306
// Are we overwriting or appending?
308
// We need to delete all the old data and start from row 1
309
if (!$this->DeleteAll($dataSetId))
315
// We need to get the MAX row number that currently exists in the data set
316
$sth = $dbh->prepare('SELECT IFNULL(MAX(RowNumber), 0) AS RowNumber FROM datasetdata INNER JOIN datasetcolumn ON datasetcolumn.dataSetColumnId = datasetdata.DataSetColumnID WHERE datasetcolumn.DataSetID = :datasetid');
318
'datasetid' => $dataSetId
321
if (!$row = $sth->fetch())
322
return $this->SetError(25005, __('Could not determine the Max row number'));
324
$rowNumber = \Xibo\Helper\Sanitize::int($row['RowNumber']);
328
// Match the file content with the column mappings
331
ini_set('auto_detect_line_endings', true);
335
$handle = fopen($csvFile, 'r');
336
while (($data = fgetcsv($handle)) !== FALSE ) {
338
// The CSV file might have headings, so ignore the first row.
346
for ($cell = 0; $cell < count($data); $cell++) {
348
// Insert the data into the correct column
349
if (isset($spreadSheetMapping[$cell])) {
351
if (!$this->Add($spreadSheetMapping[$cell], $rowNumber, $data[$cell]))
356
// Move on to the next row
363
// Change the auto detect setting back
364
ini_set('auto_detect_line_endings', false);
366
// Delete the temporary file
369
// TODO: Update list content definitions
371
$this->UpdateWatermark($dataSetId);
375
catch (Exception $e) {
377
Log::error($e->getMessage());
379
if (!$this->IsError())
380
$this->SetError(25005, __('Unable to Import'));