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/>.
21
defined('XIBO') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");
23
class DataSetColumn extends Data
25
public function Add($dataSetId, $heading, $dataTypeId, $listContent, $columnOrder = 0, $dataSetColumnTypeId = 1, $formula = '')
27
Debug::LogEntry('audit', sprintf('IN - DataSetID = %d', $dataSetId), 'DataSetColumn', 'Add');
29
if ($dataSetId == 0 || $dataSetId == '')
30
return $this->SetError(25001, __('Missing dataSetId'));
32
if ($dataTypeId == 0 || $dataTypeId == '')
33
return $this->SetError(25001, __('Missing dataTypeId'));
35
if ($dataSetColumnTypeId == 0 || $dataSetColumnTypeId == '')
36
return $this->SetError(25001, __('Missing dataSetColumnTypeId'));
39
return $this->SetError(25001, __('Please provide a column heading.'));
42
$dbh = PDOConnect::init();
44
// Is the column order provided?
45
if ($columnOrder == 0)
48
$SQL .= "SELECT IFNULL(MAX(ColumnOrder), 1) AS ColumnOrder ";
49
$SQL .= " FROM datasetcolumn ";
50
$SQL .= "WHERE datasetID = :datasetid ";
52
$sth = $dbh->prepare($SQL);
54
'datasetid' => $dataSetId
57
if (!$row = $sth->fetch())
58
return $this->SetError(25005, __('Could not determine the Column Order'));
60
$columnOrder = Kit::ValidateParam($row['ColumnOrder'], _INT);
63
// Insert the data set column
64
$SQL = "INSERT INTO datasetcolumn (DataSetID, Heading, DataTypeID, ListContent, ColumnOrder, DataSetColumnTypeID, Formula) ";
65
$SQL .= " VALUES (:datasetid, :heading, :datatypeid, :listcontent, :columnorder, :datasetcolumntypeid, :formula) ";
67
$sth = $dbh->prepare($SQL);
69
'datasetid' => $dataSetId,
70
'heading' => $heading,
71
'datatypeid' => $dataTypeId,
72
'listcontent' => $listContent,
73
'columnorder' => $columnOrder,
74
'datasetcolumntypeid' => $dataSetColumnTypeId,
78
$id = $dbh->lastInsertId();
80
Debug::LogEntry('audit', 'Complete', 'DataSetColumn', 'Add');
84
catch (Exception $e) {
85
Debug::LogEntry('error', $e->getMessage());
86
return $this->SetError(25005, __('Could not add DataSet Column'));
90
public function Edit($dataSetColumnId, $heading, $dataTypeId, $listContent, $columnOrder, $dataSetColumnTypeId, $formula = '')
92
if ($dataSetColumnId == 0 || $dataSetColumnId == '')
93
return $this->SetError(25001, __('Missing dataSetColumnId'));
95
if ($dataTypeId == 0 || $dataTypeId == '')
96
return $this->SetError(25001, __('Missing dataTypeId'));
98
if ($dataSetColumnTypeId == 0 || $dataSetColumnTypeId == '')
99
return $this->SetError(25001, __('Missing dataSetColumnTypeId'));
102
return $this->SetError(25001, __('Please provide a column heading.'));
105
$dbh = PDOConnect::init();
108
if ($listContent != '')
110
$list = explode(',', $listContent);
112
// We can check this is valid by building up a NOT IN sql statement, if we get results.. we know its not good
115
for ($i=0; $i < count($list); $i++)
117
$list_val = $dbh->quote($list[$i]);
118
$select .= $list_val . ',';
121
$select = rtrim($select, ',');
123
// $select has been quoted in the for loop
124
$SQL = "SELECT DataSetDataID FROM datasetdata WHERE DataSetColumnID = :datasetcolumnid AND Value NOT IN (" . $select . ")";
126
$sth = $dbh->prepare($SQL);
128
'datasetcolumnid' => $dataSetColumnId
131
if ($row = $sth->fetch())
132
return $this->SetError(25005, __('New list content value is invalid as it doesnt include values for existing data'));
135
$SQL = "UPDATE datasetcolumn SET Heading = :heading, ListContent = :listcontent, ColumnOrder = :columnorder, DataTypeID = :datatypeid, DataSetColumnTypeID = :datasetcolumntypeid, Formula = :formula ";
136
$SQL .= " WHERE DataSetColumnID = :datasetcolumnid";
138
$sth = $dbh->prepare($SQL);
140
'datasetcolumnid' => $dataSetColumnId,
141
'heading' => $heading,
142
'datatypeid' => $dataTypeId,
143
'listcontent' => $listContent,
144
'columnorder' => $columnOrder,
145
'datasetcolumntypeid' => $dataSetColumnTypeId,
146
'formula' => $formula
149
Debug::LogEntry('audit', 'Complete for ' . $heading, 'DataSetColumn', 'Edit');
153
catch (Exception $e) {
154
Debug::LogEntry('error', $e->getMessage());
155
return $this->SetError(25005, __('Could not edit DataSet Column'));
159
public function Delete($dataSetColumnId)
161
if ($dataSetColumnId == 0 || $dataSetColumnId == '')
162
return $this->SetError(25001, __('Missing dataSetColumnId'));
165
$dbh = PDOConnect::init();
167
$sth = $dbh->prepare('DELETE FROM datasetcolumn WHERE DataSetColumnID = :datasetcolumnid');
169
'datasetcolumnid' => $dataSetColumnId
172
Debug::LogEntry('audit', 'Complete', 'DataSetColumn', 'Delete');
176
catch (Exception $e) {
177
Debug::LogEntry('error', $e->getMessage());
178
return $this->SetError(25005, __('Could not delete DataSet Column'));
182
// Delete All Data Set columns
183
public function DeleteAll($dataSetId)
185
if ($dataSetId == 0 || $dataSetId == '')
186
return $this->SetError(25001, __('Missing dataSetId'));
189
$dbh = PDOConnect::init();
191
$sth = $dbh->prepare('DELETE FROM datasetcolumn WHERE DataSetId = :datasetid');
193
'datasetid' => $dataSetId
196
Debug::LogEntry('audit', 'Complete', 'DataSetColumn', 'DeleteAll');
200
catch (Exception $e) {
201
Debug::LogEntry('error', $e->getMessage());
202
return $this->SetError(25005, __('Could not delete DataSet Column'));
206
public function GetColumns($dataSetId) {
208
if ($dataSetId == 0 || $dataSetId == '')
209
return $this->SetError(25001, __('Missing dataSetId'));
212
$dbh = PDOConnect::init();
214
$sth = $dbh->prepare('SELECT DataSetColumnID, Heading, datatype.DataType, datasetcolumntype.DataSetColumnType, ListContent, ColumnOrder
216
INNER JOIN `datatype`
217
ON datatype.DataTypeID = datasetcolumn.DataTypeID
218
INNER JOIN `datasetcolumntype`
219
ON datasetcolumntype.DataSetColumnTypeID = datasetcolumn.DataSetColumnTypeID
220
WHERE DataSetID = :datasetid
221
ORDER BY ColumnOrder ');
224
'datasetid' => $dataSetId
227
$results = $sth->fetchAll();
229
// Check there are some columns returned
230
if (count($results) <= 0)
231
$this->ThrowError(__('No columns'));
235
foreach($results as $row) {
237
$col['datasetcolumnid'] = Kit::ValidateParam($row['DataSetColumnID'], _INT);
238
$col['heading'] = Kit::ValidateParam($row['Heading'], _STRING);
239
$col['listcontent'] = Kit::ValidateParam($row['ListContent'], _STRING);
240
$col['columnorder'] = Kit::ValidateParam($row['ColumnOrder'], _INT);
241
$col['datatype'] = Kit::ValidateParam($row['DataType'], _STRING);
242
$col['datasetcolumntype'] = Kit::ValidateParam($row['DataSetColumnType'], _STRING);
247
Debug::LogEntry('audit', sprintf('Returning %d columns.', count($rows)), 'DataSetColumn', 'GetColumns');
251
catch (Exception $e) {
253
Debug::LogEntry('error', $e->getMessage());
255
if (!$this->IsError())
256
$this->SetError(1, __('Unknown Error'));