~dangarner/xibo/client-132

« back to all changes in this revision

Viewing changes to server/lib/pages/dataset.class.php

MergedĀ ~dangarner/xibo/server-layout-media-permissions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * Xibo - Digitial Signage - http://www.xibo.org.uk
 
4
 * Copyright (C) 2011 Daniel Garner
 
5
 *
 
6
 * This file is part of Xibo.
 
7
 *
 
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
 
11
 * any later version.
 
12
 *
 
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.
 
17
 *
 
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/>.
 
20
 */
 
21
defined('XIBO') or die('Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.');
 
22
 
 
23
class datasetDAO
 
24
{
 
25
    private $db;
 
26
    private $user;
 
27
 
 
28
    function __construct(database $db, user $user)
 
29
    {
 
30
        $this->db =& $db;
 
31
        $this->user =& $user;
 
32
 
 
33
        Kit::ClassLoader('dataset');
 
34
        Kit::ClassLoader('datasetcolumn');
 
35
        Kit::ClassLoader('datasetdata');
 
36
    }
 
37
 
 
38
    function on_page_load()
 
39
    {
 
40
            return "";
 
41
    }
 
42
 
 
43
    function echo_page_heading()
 
44
    {
 
45
            echo __("Layouts");
 
46
            return true;
 
47
    }
 
48
 
 
49
    function displayPage()
 
50
    {
 
51
        require('template/pages/dataset_view.php');
 
52
    }
 
53
 
 
54
    public function DataSetFilter()
 
55
    {
 
56
        $id = uniqid();
 
57
 
 
58
        $xiboGrid = <<<HTML
 
59
        <div class="XiboGrid" id="$id">
 
60
                <div class="XiboFilter">
 
61
                        <form onsubmit="return false">
 
62
                                <input type="hidden" name="p" value="dataset">
 
63
                                <input type="hidden" name="q" value="DataSetGrid">
 
64
                        </form>
 
65
                </div>
 
66
                <div class="XiboData">
 
67
 
 
68
                </div>
 
69
        </div>
 
70
HTML;
 
71
        echo $xiboGrid;
 
72
    }
 
73
 
 
74
    public function DataSetGrid()
 
75
    {
 
76
        $db =& $this->db;
 
77
        $user =& $this->user;
 
78
        $response = new ResponseManager();
 
79
 
 
80
        $msgEdit = __('Edit');
 
81
        $msgDelete = __('Delete');
 
82
        $msgPermissions = __('Permissions');
 
83
 
 
84
        $output = <<<END
 
85
        <div class="info_table">
 
86
        <table style="width:100%">
 
87
            <thead>
 
88
                <tr>
 
89
                <th>Name</th>
 
90
                <th>Description</th>
 
91
                <th>Owner</th>
 
92
                <th>$msgPermissions</th>
 
93
                <th>Action</th>
 
94
                </tr>
 
95
            </thead>
 
96
            <tbody>
 
97
END;
 
98
 
 
99
        foreach($this->user->DataSetList() as $dataSet)
 
100
        {
 
101
            $auth = $user->DataSetAuth($dataSet['datasetid'], true);
 
102
            $owner = $user->getNameFromID($dataSet['ownerid']);
 
103
            $groups = $this->GroupsForDataSet($dataSet['datasetid']);
 
104
 
 
105
            $output .= '<tr>';
 
106
            $output .= '    <td>' . $dataSet['dataset'] . '</td>';
 
107
            $output .= '    <td>' . $dataSet['description'] . '</td>';
 
108
            $output .= '    <td>' . $owner . '</td>';
 
109
            $output .= '    <td>' . $groups . '</td>';
 
110
            $output .= '    <td>';
 
111
 
 
112
            if ($auth->edit)
 
113
            {
 
114
                $output .= '<button class="XiboFormButton" href="index.php?p=dataset&q=DataSetDataForm&datasetid=' . $dataSet['datasetid'] . '&dataset=' . $dataSet['dataset'] . '"><span>' . __('View Data') . '</span></button>';
 
115
                $output .= '<button class="XiboFormButton" href="index.php?p=dataset&q=DataSetColumnsForm&datasetid=' . $dataSet['datasetid'] . '&dataset=' . $dataSet['dataset'] . '"><span>' . __('View Columns') . '</span></button>';
 
116
                $output .= '<button class="XiboFormButton" href="index.php?p=dataset&q=EditDataSetForm&datasetid=' . $dataSet['datasetid'] . '"><span>' . $msgEdit . '</span></button>';
 
117
            }
 
118
 
 
119
            if ($auth->del)
 
120
                $output .= '<button class="XiboFormButton" href="index.php?p=dataset&q=DeleteDataSetForm&datasetid=' . $dataSet['datasetid'] . '"><span>' . $msgDelete . '</span></button>';
 
121
 
 
122
            if ($auth->modifyPermissions)
 
123
                $output .= '<button class="XiboFormButton" href="index.php?p=dataset&q=PermissionsForm&datasetid=' . $dataSet['datasetid'] . '"><span>' . $msgPermissions . '</span></button>';
 
124
 
 
125
            $output .= '    </td>';
 
126
            $output .= '</tr>';
 
127
        }
 
128
 
 
129
        $output .= '</tbody></table></div>';
 
130
        $response->SetGridResponse($output);
 
131
        $response->Respond();
 
132
    }
 
133
 
 
134
    public function AddDataSetForm()
 
135
    {
 
136
        $db =& $this->db;
 
137
        $user =& $this->user;
 
138
        $response = new ResponseManager();
 
139
 
 
140
        $helpManager = new HelpManager($db, $user);
 
141
 
 
142
        $msgName = __('Name');
 
143
        $msgDesc = __('Description');
 
144
 
 
145
        $form = <<<END
 
146
        <form id="AddDataSetForm" class="XiboForm" method="post" action="index.php?p=dataset&q=AddDataSet">
 
147
            <table>
 
148
                <tr>
 
149
                    <td><label for="dataset" accesskey="n">$msgName<span class="required">*</span></label></td>
 
150
                    <td><input name="dataset" class="required" type="text" id="dataset" tabindex="1" /></td>
 
151
                </tr>
 
152
                <tr>
 
153
                    <td><label for="description" accesskey="d">$msgDesc</label></td>
 
154
                    <td><input name="description" type="text" id="description" tabindex="2" /></td>
 
155
                </tr>
 
156
            </table>
 
157
        </form>
 
158
END;
 
159
 
 
160
 
 
161
        $response->SetFormRequestResponse($form, __('Add DataSet'), '350px', '275px');
 
162
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('DataSet', 'Add') . '")');
 
163
        $response->AddButton(__('Cancel'), 'XiboDialogClose()');
 
164
        $response->AddButton(__('Add'), '$("#AddDataSetForm").submit()');
 
165
        $response->Respond();
 
166
    }
 
167
 
 
168
    /**
 
169
     * Add a dataset
 
170
     */
 
171
    public function AddDataSet()
 
172
    {
 
173
        $db =& $this->db;
 
174
        $user =& $this->user;
 
175
        $response = new ResponseManager();
 
176
 
 
177
        $dataSet = Kit::GetParam('dataset', _POST, _STRING);
 
178
        $description = Kit::GetParam('description', _POST, _STRING);
 
179
 
 
180
        $dataSetObject = new DataSet($db);
 
181
        if (!$dataSetId = $dataSetObject->Add($dataSet, $description, $this->user->userid))
 
182
            trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
183
 
 
184
        // Also add one column
 
185
        $dataSetColumn = new DataSetColumn($db);
 
186
        $dataSetColumn->Add($dataSetId, 'Col1', 1, null, 1);
 
187
            
 
188
        $response->SetFormSubmitResponse(__('DataSet Added'));
 
189
        $response->Respond();
 
190
    }
 
191
 
 
192
    public function EditDataSetForm()
 
193
    {
 
194
        $db =& $this->db;
 
195
        $user =& $this->user;
 
196
        $response = new ResponseManager();
 
197
 
 
198
        $helpManager = new HelpManager($db, $user);
 
199
 
 
200
        $dataSetId = Kit::GetParam('datasetid', _GET, _INT);
 
201
 
 
202
        $auth = $user->DataSetAuth($dataSetId, true);
 
203
        if (!$auth->edit)
 
204
            trigger_error(__('Access Denied'));
 
205
 
 
206
        // Get the information we already know
 
207
        $SQL = sprintf("SELECT DataSet, Description FROM dataset WHERE DataSetID = %d", $dataSetId);
 
208
 
 
209
        if (!$row = $db->GetSingleRow($SQL))
 
210
            trigger_error(__('Unable to get DataSet information'));
 
211
 
 
212
        $dataSet = $row['DataSet'];
 
213
        $description = $row['Description'];
 
214
 
 
215
        $msgName = __('Name');
 
216
        $msgDesc = __('Description');
 
217
 
 
218
        $form = <<<END
 
219
        <form id="EditDataSetForm" class="XiboForm" method="post" action="index.php?p=dataset&q=EditDataSet">
 
220
            <input type="hidden" name="datasetid" value="$dataSetId" />
 
221
            <table>
 
222
                <tr>
 
223
                    <td><label for="dataset" accesskey="n">$msgName<span class="required">*</span></label></td>
 
224
                    <td><input name="dataset" class="required" type="text" id="dataset" tabindex="1" value="$dataSet" /></td>
 
225
                </tr>
 
226
                <tr>
 
227
                    <td><label for="description" accesskey="d">$msgDesc</label></td>
 
228
                    <td><input name="description" type="text" id="description" tabindex="2" value="$description" /></td>
 
229
                </tr>
 
230
            </table>
 
231
        </form>
 
232
END;
 
233
 
 
234
 
 
235
        $response->SetFormRequestResponse($form, __('Edit DataSet'), '350px', '275px');
 
236
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('DataSet', 'Add') . '")');
 
237
        $response->AddButton(__('Cancel'), 'XiboDialogClose()');
 
238
        $response->AddButton(__('Edit'), '$("#EditDataSetForm").submit()');
 
239
        $response->Respond();
 
240
    }
 
241
 
 
242
    public function EditDataSet()
 
243
    {
 
244
        $db =& $this->db;
 
245
        $user =& $this->user;
 
246
        $response = new ResponseManager();
 
247
 
 
248
        $dataSetId = Kit::GetParam('datasetid', _POST, _INT);
 
249
 
 
250
        $auth = $user->DataSetAuth($dataSetId, true);
 
251
        if (!$auth->edit)
 
252
            trigger_error(__('Access Denied'));
 
253
 
 
254
        $dataSet = Kit::GetParam('dataset', _POST, _STRING);
 
255
        $description = Kit::GetParam('description', _POST, _STRING);
 
256
 
 
257
        $dataSetObject = new DataSet($db);
 
258
        if (!$dataSetObject->Edit($dataSetId, $dataSet, $description))
 
259
            trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
260
 
 
261
        $response->SetFormSubmitResponse(__('DataSet Edited'));
 
262
        $response->Respond();
 
263
    }
 
264
 
 
265
    /**
 
266
     * Return the Delete Form as HTML
 
267
     * @return
 
268
     */
 
269
    public function DeleteDataSetForm()
 
270
    {
 
271
        $db =& $this->db;
 
272
        $response = new ResponseManager();
 
273
        $helpManager = new HelpManager($db, $this->user);
 
274
 
 
275
        $dataSetId = Kit::GetParam('datasetid', _GET, _INT);
 
276
 
 
277
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
278
        if (!$auth->del)
 
279
            trigger_error(__('Access Denied'));
 
280
 
 
281
        // Translate messages
 
282
        $msgDelete              = __('Are you sure you want to delete this DataSet?');
 
283
        $msgYes                 = __('Yes');
 
284
        $msgNo                  = __('No');
 
285
 
 
286
        //we can delete
 
287
        $form = <<<END
 
288
        <form id="DataSetDeleteForm" class="XiboForm" method="post" action="index.php?p=dataset&q=DeleteDataSet">
 
289
            <input type="hidden" name="datasetid" value="$dataSetId">
 
290
            <p>$msgDelete</p>
 
291
        </form>
 
292
END;
 
293
 
 
294
        $response->SetFormRequestResponse($form, __('Delete this DataSet?'), '350px', '200px');
 
295
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('DataSet', 'Delete') . '")');
 
296
        $response->AddButton(__('Cancel'), 'XiboDialogClose()');
 
297
        $response->AddButton(__('Delete'), '$("#DataSetDeleteForm").submit()');
 
298
        $response->Respond();
 
299
    }
 
300
 
 
301
    public function DeleteDataSet()
 
302
    {
 
303
        $db =& $this->db;
 
304
        $user =& $this->user;
 
305
        $response = new ResponseManager();
 
306
 
 
307
        $dataSetId = Kit::GetParam('datasetid', _POST, _INT);
 
308
 
 
309
        $auth = $user->DataSetAuth($dataSetId, true);
 
310
        if (!$auth->del)
 
311
            trigger_error(__('Access Denied'));
 
312
 
 
313
        $dataSetObject = new DataSet($db);
 
314
        if (!$dataSetObject->Delete($dataSetId))
 
315
            trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
316
 
 
317
        $response->SetFormSubmitResponse(__('DataSet Deleted'));
 
318
        $response->Respond();
 
319
    }
 
320
 
 
321
    public function DataSetColumnsForm()
 
322
    {
 
323
        $db =& $this->db;
 
324
        $response = new ResponseManager();
 
325
        $helpManager = new HelpManager($db, $this->user);
 
326
 
 
327
        $dataSetId = Kit::GetParam('datasetid', _GET, _INT);
 
328
        $dataSet = Kit::GetParam('dataset', _GET, _STRING);
 
329
 
 
330
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
331
        if (!$auth->edit)
 
332
            trigger_error(__('Access Denied'));
 
333
 
 
334
        $msgEdit = __('Edit');
 
335
        $msgDelete = __('Delete');
 
336
 
 
337
        $form = <<<END
 
338
        <div class="info_table">
 
339
        <table style="width:100%">
 
340
            <thead>
 
341
                <tr>
 
342
                <th>Heading</th>
 
343
                <th>Data Type</th>
 
344
                <th>List Content</th>
 
345
                <th>Column Order</th>
 
346
                <th>Action</th>
 
347
                </tr>
 
348
            </thead>
 
349
            <tbody>
 
350
END;
 
351
 
 
352
        $SQL  = "";
 
353
        $SQL .= "SELECT DataSetColumnID, Heading, DataTypeID, ListContent, ColumnOrder ";
 
354
        $SQL .= "  FROM datasetcolumn ";
 
355
        $SQL .= sprintf(" WHERE DataSetID = %d ", $dataSetId);
 
356
        $SQL .= "ORDER BY ColumnOrder ";
 
357
 
 
358
        if (!$results = $db->query($SQL))
 
359
            trigger_error(__('Unable to get columns for DataSet'));
 
360
 
 
361
        while ($row = $db->get_assoc_row($results))
 
362
        {
 
363
            $form .= '<tr>';
 
364
            $form .= '  <td>' . $row['Heading'] . '</td>';
 
365
            $form .= '  <td>String</td>';
 
366
            $form .= '  <td>' . $row['ListContent'] . '</td>';
 
367
            $form .= '  <td>' . $row['ColumnOrder'] . '</td>';
 
368
            $form .= '  <td>';
 
369
            $form .= '      <button class="XiboFormButton" href="index.php?p=dataset&q=EditDataSetColumnForm&datasetid=' . $dataSetId . '&datasetcolumnid=' . $row['DataSetColumnID'] . '&dataset=' . $dataSet . '"><span>' . $msgEdit . '</span></button>';
 
370
 
 
371
            if ($auth->del)
 
372
                $form .= '      <button class="XiboFormButton" href="index.php?p=dataset&q=DeleteDataSetColumnForm&datasetid=' . $dataSetId . '&datasetcolumnid=' . $row['DataSetColumnID'] . '&dataset=' . $dataSet . '"><span>' . $msgDelete . '</span></button>';
 
373
 
 
374
            $form .= '  </td>';
 
375
            $form .= '</tr>';
 
376
        }
 
377
 
 
378
        $form .= '</tbody></table></div>';
 
379
        
 
380
        $response->SetFormRequestResponse($form, sprintf(__('Columns for %s'), $dataSet), '550px', '400px');
 
381
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('DataSet', 'ViewColumns') . '")');
 
382
        $response->AddButton(__('Close'), 'XiboDialogClose()');
 
383
        $response->AddButton(__('Add Column'), 'XiboFormRender("index.php?p=dataset&q=AddDataSetColumnForm&datasetid=' . $dataSetId . '&dataset=' . $dataSet . '")');
 
384
        $response->Respond();
 
385
    }
 
386
 
 
387
    public function AddDataSetColumnForm()
 
388
    {
 
389
        $db =& $this->db;
 
390
        $response = new ResponseManager();
 
391
        $helpManager = new HelpManager($db, $this->user);
 
392
 
 
393
        $dataSetId = Kit::GetParam('datasetid', _GET, _INT);
 
394
        $dataSet = Kit::GetParam('dataset', _GET, _STRING);
 
395
 
 
396
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
397
        if (!$auth->edit)
 
398
            trigger_error(__('Access Denied'));
 
399
 
 
400
        $msgHeading = __('Heading');
 
401
        $msgListContent = __('List Content');
 
402
        $msgColumnOrder = __('Column Order');
 
403
 
 
404
        $form = <<<END
 
405
        <form id="DataSetColumnEditForm" class="XiboForm" method="post" action="index.php?p=dataset&q=AddDataSetColumn">
 
406
            <input type="hidden" name="dataset" value="$dataSet" />
 
407
            <input type="hidden" name="datasetid" value="$dataSetId" />
 
408
            <table>
 
409
                <tr>
 
410
                    <td><label for="heading" accesskey="h">$msgHeading<span class="required">*</span></label></td>
 
411
                    <td><input name="heading" class="required" type="text" id="heading" tabindex="1" /></td>
 
412
                </tr>
 
413
                <tr>
 
414
                    <td><label for="listcontent" accesskey="l">$msgListContent</label></td>
 
415
                    <td><input name="listcontent" type="text" id="listcontent" tabindex="2" /></td>
 
416
                </tr>
 
417
                <tr>
 
418
                    <td><label for="columnorder" accesskey="c">$msgColumnOrder</label></td>
 
419
                    <td><input name="columnorder" type="text" id="columnorder" tabindex="3" /></td>
 
420
                </tr>
 
421
            </table>
 
422
        </form>
 
423
END;
 
424
 
 
425
        $response->SetFormRequestResponse($form, __('Add Column'), '450px', '400px');
 
426
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('DataSet', 'EditColumn') . '")');
 
427
        $response->AddButton(__('Cancel'), 'XiboFormRender("index.php?p=dataset&q=DataSetColumnsForm&datasetid=' . $dataSetId . '&dataset=' . $dataSet . '")');
 
428
        $response->AddButton(__('Save'), '$("#DataSetColumnEditForm").submit()');
 
429
        $response->Respond();
 
430
    }
 
431
 
 
432
    public function AddDataSetColumn()
 
433
    {
 
434
        $db =& $this->db;
 
435
        $user =& $this->user;
 
436
        $response = new ResponseManager();
 
437
 
 
438
        $dataSetId = Kit::GetParam('datasetid', _POST, _INT);
 
439
        $dataSet = Kit::GetParam('dataset', _POST, _STRING);
 
440
 
 
441
        $auth = $user->DataSetAuth($dataSetId, true);
 
442
        if (!$auth->edit)
 
443
            trigger_error(__('Access Denied'));
 
444
 
 
445
        $heading = Kit::GetParam('heading', _POST, _WORD);
 
446
        $listContent = Kit::GetParam('listcontent', _POST, _STRING);
 
447
        $columnOrder = Kit::GetParam('columnorder', _POST, _INT);
 
448
 
 
449
        $dataSetObject = new DataSetColumn($db);
 
450
        if (!$dataSetObject->Add($dataSetId, $heading, 1, $listContent))
 
451
            trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
452
 
 
453
        $response->SetFormSubmitResponse(__('Column Edited'));
 
454
        $response->hideMessage = true;
 
455
        $response->loadForm = true;
 
456
        $response->loadFormUri = 'index.php?p=dataset&q=DataSetColumnsForm&datasetid=' . $dataSetId . '&dataset=' . $dataSet;
 
457
        $response->Respond();
 
458
    }
 
459
 
 
460
    public function EditDataSetColumnForm()
 
461
    {
 
462
        $db =& $this->db;
 
463
        $response = new ResponseManager();
 
464
        $helpManager = new HelpManager($db, $this->user);
 
465
 
 
466
        $dataSetId = Kit::GetParam('datasetid', _GET, _INT);
 
467
        $dataSetColumnId = Kit::GetParam('datasetcolumnid', _GET, _INT);
 
468
        $dataSet = Kit::GetParam('dataset', _GET, _STRING);
 
469
 
 
470
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
471
        if (!$auth->edit)
 
472
            trigger_error(__('Access Denied'));
 
473
 
 
474
        // Get some information about this data set column
 
475
        $SQL = sprintf("SELECT Heading, ListContent, ColumnOrder FROM datasetcolumn WHERE DataSetColumnID = %d", $dataSetColumnId);
 
476
        
 
477
        if (!$row = $db->GetSingleRow($SQL))
 
478
            trigger_error(__('Unabled to get Data Column information'), E_USER_ERROR);
 
479
 
 
480
        $heading = Kit::ValidateParam($row['Heading'], _WORD);
 
481
        $listContent = Kit::ValidateParam($row['ListContent'], _STRING);
 
482
        $columnOrder = Kit::ValidateParam($row['ColumnOrder'], _INT);
 
483
 
 
484
        $msgHeading = __('Heading');
 
485
        $msgListContent = __('List Content');
 
486
        $msgColumnOrder = __('Column Order');
 
487
 
 
488
        $form = <<<END
 
489
        <form id="DataSetColumnEditForm" class="XiboForm" method="post" action="index.php?p=dataset&q=EditDataSetColumn">
 
490
            <input type="hidden" name="dataset" value="$dataSet" />
 
491
            <input type="hidden" name="datasetid" value="$dataSetId" />
 
492
            <input type="hidden" name="datasetcolumnid" value="$dataSetColumnId" />
 
493
            <table>
 
494
                <tr>
 
495
                    <td><label for="heading" accesskey="h">$msgHeading<span class="required">*</span></label></td>
 
496
                    <td><input name="heading" class="required" type="text" id="heading" tabindex="1" value="$heading" /></td>
 
497
                </tr>
 
498
                <tr>
 
499
                    <td><label for="listcontent" accesskey="l">$msgListContent</label></td>
 
500
                    <td><input name="listcontent" type="text" id="listcontent" tabindex="2" value="$listContent" /></td>
 
501
                </tr>
 
502
                <tr>
 
503
                    <td><label for="columnorder" accesskey="c">$msgColumnOrder</label></td>
 
504
                    <td><input name="columnorder" type="text" id="columnorder" tabindex="3" value="$columnOrder" /></td>
 
505
                </tr>
 
506
            </table>
 
507
        </form>
 
508
END;
 
509
 
 
510
        $response->SetFormRequestResponse($form, __('Edit Column'), '450px', '400px');
 
511
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('DataSet', 'EditColumn') . '")');
 
512
        $response->AddButton(__('Cancel'), 'XiboFormRender("index.php?p=dataset&q=DataSetColumnsForm&datasetid=' . $dataSetId . '&dataset=' . $dataSet . '")');
 
513
        $response->AddButton(__('Save'), '$("#DataSetColumnEditForm").submit()');
 
514
        $response->Respond();
 
515
    }
 
516
 
 
517
    public function EditDataSetColumn()
 
518
    {
 
519
        $db =& $this->db;
 
520
        $user =& $this->user;
 
521
        $response = new ResponseManager();
 
522
 
 
523
        $dataSetId = Kit::GetParam('datasetid', _POST, _INT);
 
524
        $dataSet = Kit::GetParam('dataset', _POST, _STRING);
 
525
 
 
526
        $auth = $user->DataSetAuth($dataSetId, true);
 
527
        if (!$auth->edit)
 
528
            trigger_error(__('Access Denied'));
 
529
            
 
530
        $dataSetColumnId = Kit::GetParam('datasetcolumnid', _POST, _INT);
 
531
        $heading = Kit::GetParam('heading', _POST, _WORD);
 
532
        $listContent = Kit::GetParam('listcontent', _POST, _STRING);
 
533
        $columnOrder = Kit::GetParam('columnorder', _POST, _INT);
 
534
 
 
535
        $dataSetObject = new DataSetColumn($db);
 
536
        if (!$dataSetObject->Edit($dataSetColumnId, $heading, 1, $listContent, $columnOrder))
 
537
            trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
538
 
 
539
        $response->SetFormSubmitResponse(__('Column Edited'));
 
540
        $response->hideMessage = true;
 
541
        $response->loadForm = true;
 
542
        $response->loadFormUri = 'index.php?p=dataset&q=DataSetColumnsForm&datasetid=' . $dataSetId . '&dataset=' . $dataSet;
 
543
        $response->Respond();
 
544
    }
 
545
 
 
546
    public function DeleteDataSetColumnForm()
 
547
    {
 
548
        $db =& $this->db;
 
549
        $response = new ResponseManager();
 
550
        $helpManager = new HelpManager($db, $this->user);
 
551
 
 
552
        $dataSetId = Kit::GetParam('datasetid', _GET, _INT);
 
553
        $dataSet = Kit::GetParam('dataset', _GET, _STRING);
 
554
 
 
555
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
556
        if (!$auth->edit)
 
557
            trigger_error(__('Access Denied'));
 
558
 
 
559
        $dataSetColumnId = Kit::GetParam('datasetcolumnid', _GET, _INT);
 
560
 
 
561
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
562
        if (!$auth->del)
 
563
            trigger_error(__('Access Denied'));
 
564
 
 
565
        // Translate messages
 
566
        $msgDelete              = __('Are you sure you want to delete this Column?');
 
567
        $msgYes                 = __('Yes');
 
568
        $msgNo                  = __('No');
 
569
 
 
570
        //we can delete
 
571
        $form = <<<END
 
572
        <form id="DataSetColumnDeleteForm" class="XiboForm" method="post" action="index.php?p=dataset&q=DeleteDataSetColumn">
 
573
            <input type="hidden" name="datasetid" value="$dataSetId">
 
574
            <input type="hidden" name="dataset" value="$dataSet">
 
575
            <input type="hidden" name="datasetcolumnid" value="$dataSetColumnId">
 
576
            <p>$msgDelete</p>
 
577
        </form>
 
578
END;
 
579
 
 
580
        $response->SetFormRequestResponse($form, __('Delete this Column?'), '350px', '200px');
 
581
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('DataSet', 'DeleteColumn') . '")');
 
582
        $response->AddButton(__('Cancel'), 'XiboFormRender("index.php?p=dataset&q=DataSetColumnsForm&datasetid=' . $dataSetId . '&dataset=' . $dataSet . '")');
 
583
        $response->AddButton(__('Delete'), '$("#DataSetColumnDeleteForm").submit()');
 
584
        $response->Respond();
 
585
    }
 
586
 
 
587
    public function DeleteDataSetColumn()
 
588
    {
 
589
        $db =& $this->db;
 
590
        $user =& $this->user;
 
591
        $response = new ResponseManager();
 
592
 
 
593
        $dataSetId = Kit::GetParam('datasetid', _POST, _INT);
 
594
        $dataSet = Kit::GetParam('dataset', _POST, _STRING);
 
595
 
 
596
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
597
        if (!$auth->edit)
 
598
            trigger_error(__('Access Denied'));
 
599
 
 
600
        $dataSetColumnId = Kit::GetParam('datasetcolumnid', _POST, _INT);
 
601
 
 
602
        $dataSetObject = new DataSetColumn($db);
 
603
        if (!$dataSetObject->Delete($dataSetColumnId))
 
604
            trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
605
 
 
606
        $response->SetFormSubmitResponse(__('Column Deleted'));
 
607
        $response->hideMessage = true;
 
608
        $response->loadForm = true;
 
609
        $response->loadFormUri = 'index.php?p=dataset&q=DataSetColumnsForm&datasetid=' . $dataSetId . '&dataset=' . $dataSet;
 
610
        $response->Respond();
 
611
    }
 
612
 
 
613
    public function DataSetDataForm()
 
614
    {
 
615
        $db =& $this->db;
 
616
        $response = new ResponseManager();
 
617
        $helpManager = new HelpManager($db, $this->user);
 
618
 
 
619
        $dataSetId = Kit::GetParam('datasetid', _GET, _INT);
 
620
        $dataSet = Kit::GetParam('dataset', _GET, _STRING);
 
621
 
 
622
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
623
        if (!$auth->edit)
 
624
            trigger_error(__('Access Denied'));
 
625
 
 
626
        // Get the max number of rows
 
627
        $SQL  = "";
 
628
        $SQL .= "SELECT MAX(RowNumber) AS RowNumber, COUNT(DISTINCT datasetcolumn.DataSetColumnID) AS ColNumber ";
 
629
        $SQL .= "  FROM datasetdata ";
 
630
        $SQL .= "   RIGHT OUTER JOIN datasetcolumn ";
 
631
        $SQL .= "   ON datasetcolumn.DataSetColumnID = datasetdata.DataSetColumnID ";
 
632
        $SQL .= sprintf("WHERE datasetcolumn.DataSetID = %d ", $dataSetId);
 
633
 
 
634
        Debug::LogEntry($db, 'audit', $SQL, 'dataset', 'DataSetDataForm');
 
635
 
 
636
        if (!$maxResult = $db->GetSingleRow($SQL))
 
637
        {
 
638
            trigger_error($db->error());
 
639
            trigger_error(__('Unable to find the number of data points'), E_USER_ERROR);
 
640
        }
 
641
 
 
642
        $maxRows = $maxResult['RowNumber'];
 
643
        $maxCols = $maxResult['ColNumber'];
 
644
 
 
645
        // Get some information about the columns in this dataset
 
646
        $SQL  = "SELECT Heading, DataSetColumnID, ListContent, ColumnOrder FROM datasetcolumn WHERE DataSetID = %d ";
 
647
        $SQL .= "ORDER BY ColumnOrder ";
 
648
        
 
649
        if (!$results = $db->query(sprintf($SQL, $dataSetId)))
 
650
        {
 
651
            trigger_error($db->error());
 
652
            trigger_error(__('Unable to find the column headings'), E_USER_ERROR);
 
653
        }
 
654
 
 
655
        $columnDefinition = array();
 
656
        
 
657
        $form  = '<div class="info_table">';
 
658
        $form .= '<table style="width:100%">';
 
659
        $form .= '   <tr>';
 
660
        $form .= '      <th>' . __('Row Number') . '</th>';
 
661
 
 
662
        while ($row = $db->get_assoc_row($results))
 
663
        {
 
664
            $columnDefinition[] = $row;
 
665
            $heading = $row['Heading'];
 
666
 
 
667
            $form .= ' <th>' . $heading . '</th>';
 
668
        }
 
669
 
 
670
        $form .= '</tr>';
 
671
 
 
672
        // Loop through the max rows
 
673
        for ($row = 1; $row <= $maxRows + 2; $row++)
 
674
        {
 
675
            $form .= '<tr>';
 
676
            $form .= '  <td>' . $row . '</td>';
 
677
 
 
678
            // $row is the current row
 
679
            for ($col = 0; $col < $maxCols; $col++)
 
680
            {
 
681
                $dataSetColumnId = $columnDefinition[$col]['DataSetColumnID'];
 
682
                $listContent = $columnDefinition[$col]['ListContent'];
 
683
                $columnOrder = $columnDefinition[$col]['ColumnOrder'];
 
684
 
 
685
                // Value for this Col/Row
 
686
                $value = '';
 
687
 
 
688
                if ($row <= $maxRows)
 
689
                {
 
690
                    // This is intended to be a blank row
 
691
                    $SQL  = "";
 
692
                    $SQL .= "SELECT Value ";
 
693
                    $SQL .= "  FROM datasetdata ";
 
694
                    $SQL .= "WHERE datasetdata.RowNumber = %d ";
 
695
                    $SQL .= "   AND datasetdata.DataSetColumnID = %d ";
 
696
                    $SQL = sprintf($SQL, $row, $dataSetColumnId);
 
697
 
 
698
                    Debug::LogEntry($db, 'audit', $SQL, 'dataset');
 
699
 
 
700
                    if (!$results = $db->query($SQL))
 
701
                    {
 
702
                        trigger_error($db->error());
 
703
                        trigger_error(__('Can not get the data row/column'), E_USER_ERROR);
 
704
                    }
 
705
 
 
706
                    if ($db->num_rows($results) == 0)
 
707
                    {
 
708
                        $value = '';
 
709
                    }
 
710
                    else
 
711
                    {
 
712
                        $valueRow = $db->get_assoc_row($results);
 
713
                        $value = $valueRow['Value'];
 
714
                    }
 
715
                }
 
716
                
 
717
                // Do we need a select list?
 
718
                if ($listContent != '')
 
719
                {
 
720
                    $listItems = explode(',', $listContent);
 
721
                    $selected = ($value == '') ? ' selected' : '';
 
722
                    $select = '<select name="value">';
 
723
                    $select.= '     <option value="" ' . $selected . '></option>';
 
724
 
 
725
                    for ($i=0; $i < count($listItems); $i++)
 
726
                    {
 
727
                        $selected = ($listItems[$i] == $value) ? ' selected' : '';
 
728
 
 
729
                        $select .= '<option value="' . $listItems[$i] . '" ' . $selected . '>' . $listItems[$i] . '</option>';
 
730
                    }
 
731
 
 
732
                    $select .= '</select>';
 
733
                }
 
734
                else
 
735
                {
 
736
                    $select = '<input type="text" name="value" value="' . $value . '">';
 
737
                }
 
738
 
 
739
                $action = ($value == '') ? 'AddDataSetData' : 'EditDataSetData';
 
740
                $fieldId = uniqid();
 
741
                
 
742
                $form .= <<<END
 
743
                <td>
 
744
                    <form id="$fieldId" class="XiboDataSetDataForm" action="index.php?p=dataset&q=$action">
 
745
                        <input type="hidden" name="fieldid" value="$fieldId">
 
746
                        <input type="hidden" name="datasetid" value="$dataSetId">
 
747
                        <input type="hidden" name="datasetcolumnid" value="$dataSetColumnId">
 
748
                        <input type="hidden" name="rownumber" value="$row">
 
749
                        $select
 
750
                    </form>
 
751
                </td>
 
752
END;
 
753
 
 
754
                
 
755
            } //cols loop
 
756
 
 
757
            $form .= '</tr>';
 
758
        } //rows loop
 
759
 
 
760
        $form .= '</table></div>';
 
761
        
 
762
        $response->SetFormRequestResponse($form, $dataSet, '750px', '600px', 'dataSetData');
 
763
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('DataSet', 'Data') . '")');
 
764
        $response->AddButton(__('Add Rows'), 'XiboFormRender("index.php?p=dataset&q=DataSetDataForm&datasetid=' . $dataSetId . '&dataset=' . $dataSet . '")');
 
765
        $response->AddButton(__('Done'), 'XiboDialogClose()');
 
766
        $response->Respond();
 
767
    }
 
768
 
 
769
    public function AddDataSetData()
 
770
    {
 
771
        $db =& $this->db;
 
772
        $user =& $this->user;
 
773
        $response = new ResponseManager();
 
774
 
 
775
        $response->uniqueReference = Kit::GetParam('fieldid', _POST, _WORD);
 
776
        $dataSetId = Kit::GetParam('datasetid', _POST, _INT);
 
777
        $dataSetColumnId = Kit::GetParam('datasetcolumnid', _POST, _INT);
 
778
        $rowNumber = Kit::GetParam('rownumber', _POST, _INT);
 
779
        $value = Kit::GetParam('value', _POST, _STRING);
 
780
 
 
781
        $auth = $user->DataSetAuth($dataSetId, true);
 
782
        if (!$auth->edit)
 
783
            trigger_error(__('Access Denied'));
 
784
 
 
785
        $dataSetObject = new DataSetData($db);
 
786
        if (!$dataSetObject->Add($dataSetColumnId, $rowNumber, $value))
 
787
            trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
788
 
 
789
        $response->SetFormSubmitResponse(__('Data Added'));
 
790
        $response->loadFormUri = 'index.php?p=dataset&q=EditDataSetData';
 
791
        $response->hideMessage = true;
 
792
        $response->keepOpen = true;
 
793
        $response->Respond();
 
794
    }
 
795
 
 
796
    public function EditDataSetData()
 
797
    {
 
798
        $db =& $this->db;
 
799
        $user =& $this->user;
 
800
        $response = new ResponseManager();
 
801
 
 
802
        $response->uniqueReference = Kit::GetParam('fieldid', _POST, _WORD);
 
803
        $dataSetId = Kit::GetParam('datasetid', _POST, _INT);
 
804
        $dataSetColumnId = Kit::GetParam('datasetcolumnid', _POST, _INT);
 
805
        $rowNumber = Kit::GetParam('rownumber', _POST, _INT);
 
806
        $value = Kit::GetParam('value', _POST, _STRING);
 
807
 
 
808
        $auth = $user->DataSetAuth($dataSetId, true);
 
809
        if (!$auth->edit)
 
810
            trigger_error(__('Access Denied'));
 
811
 
 
812
        if ($value == '')
 
813
        {
 
814
            $dataSetObject = new DataSetData($db);
 
815
            if (!$dataSetObject->Delete($dataSetColumnId, $rowNumber))
 
816
                trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
817
 
 
818
            $response->SetFormSubmitResponse(__('Data Deleted'));
 
819
            $response->loadFormUri = 'index.php?p=dataset&q=AddDataSetData';
 
820
        }
 
821
        else
 
822
        {
 
823
            $dataSetObject = new DataSetData($db);
 
824
            if (!$dataSetObject->Edit($dataSetColumnId, $rowNumber, $value))
 
825
                trigger_error($dataSetObject->GetErrorMessage(), E_USER_ERROR);
 
826
 
 
827
            $response->SetFormSubmitResponse(__('Data Edited'));
 
828
            $response->loadFormUri = 'index.php?p=dataset&q=EditDataSetData';
 
829
        }
 
830
 
 
831
        $response->hideMessage = true;
 
832
        $response->keepOpen = true;
 
833
        $response->Respond();
 
834
    }
 
835
 
 
836
    /**
 
837
     * Get a list of group names for a layout
 
838
     * @param <type> $layoutId
 
839
     * @return <type>
 
840
     */
 
841
    private function GroupsForDataSet($dataSetId)
 
842
    {
 
843
        $db =& $this->db;
 
844
 
 
845
        $SQL = '';
 
846
        $SQL .= 'SELECT `group`.Group ';
 
847
        $SQL .= '  FROM `group` ';
 
848
        $SQL .= '   INNER JOIN lkdatasetgroup ';
 
849
        $SQL .= '   ON `group`.GroupID = lkdatasetgroup.GroupID ';
 
850
        $SQL .= ' WHERE lkdatasetgroup.DataSetID = %d ';
 
851
 
 
852
        $SQL = sprintf($SQL, $dataSetId);
 
853
 
 
854
        if (!$results = $db->query($SQL))
 
855
        {
 
856
            trigger_error($db->error());
 
857
            trigger_error(__('Unable to get group information for dataset'), E_USER_ERROR);
 
858
        }
 
859
 
 
860
        $groups = '';
 
861
 
 
862
        while ($row = $db->get_assoc_row($results))
 
863
        {
 
864
            $groups .= $row['Group'] . ', ';
 
865
        }
 
866
 
 
867
        $groups = trim($groups);
 
868
        $groups = trim($groups, ',');
 
869
 
 
870
        return $groups;
 
871
    }
 
872
 
 
873
    public function PermissionsForm()
 
874
    {
 
875
        $db =& $this->db;
 
876
        $user =& $this->user;
 
877
        $response = new ResponseManager();
 
878
        $helpManager = new HelpManager($db, $user);
 
879
 
 
880
        $dataSetId = Kit::GetParam('datasetid', _GET, _INT);
 
881
 
 
882
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
883
 
 
884
        if (!$auth->modifyPermissions)
 
885
            trigger_error(__('You do not have permissions to edit this dataset'), E_USER_ERROR);
 
886
 
 
887
        // Form content
 
888
        $form = '<form id="DataSetPermissionsForm" class="XiboForm" method="post" action="index.php?p=dataset&q=Permissions">';
 
889
        $form .= '<input type="hidden" name="datasetid" value="' . $dataSetId . '" />';
 
890
        $form .= '<div class="dialog_table">';
 
891
        $form .= '  <table style="width:100%">';
 
892
        $form .= '      <tr>';
 
893
        $form .= '          <th>' . __('Group') . '</th>';
 
894
        $form .= '          <th>' . __('View') . '</th>';
 
895
        $form .= '          <th>' . __('Edit') . '</th>';
 
896
        $form .= '          <th>' . __('Delete') . '</th>';
 
897
        $form .= '      </tr>';
 
898
 
 
899
        // List of all Groups with a view/edit/delete checkbox
 
900
        $SQL = '';
 
901
        $SQL .= 'SELECT `group`.GroupID, `group`.`Group`, View, Edit, Del, `group`.IsUserSpecific ';
 
902
        $SQL .= '  FROM `group` ';
 
903
        $SQL .= '   LEFT OUTER JOIN lkdatasetgroup ';
 
904
        $SQL .= '   ON lkdatasetgroup.GroupID = group.GroupID ';
 
905
        $SQL .= '       AND lkdatasetgroup.DataSetID = %d ';
 
906
        $SQL .= ' WHERE `group`.GroupID <> %d ';
 
907
        $SQL .= 'ORDER BY `group`.IsEveryone DESC, `group`.IsUserSpecific, `group`.`Group` ';
 
908
 
 
909
        $SQL = sprintf($SQL, $dataSetId, $user->getGroupFromId($user->userid, true));
 
910
 
 
911
        if (!$results = $db->query($SQL))
 
912
        {
 
913
            trigger_error($db->error());
 
914
            trigger_error(__('Unable to get permissions for this dataset'), E_USER_ERROR);
 
915
        }
 
916
 
 
917
        while($row = $db->get_assoc_row($results))
 
918
        {
 
919
            $groupId = $row['GroupID'];
 
920
            $group = ($row['IsUserSpecific'] == 0) ? '<strong>' . $row['Group'] . '</strong>' : $row['Group'];
 
921
 
 
922
            $form .= '<tr>';
 
923
            $form .= ' <td>' . $group . '</td>';
 
924
            $form .= ' <td><input type="checkbox" name="groupids[]" value="' . $groupId . '_view" ' . (($row['View'] == 1) ? 'checked' : '') . '></td>';
 
925
            $form .= ' <td><input type="checkbox" name="groupids[]" value="' . $groupId . '_edit" ' . (($row['Edit'] == 1) ? 'checked' : '') . '></td>';
 
926
            $form .= ' <td><input type="checkbox" name="groupids[]" value="' . $groupId . '_del" ' . (($row['Del'] == 1) ? 'checked' : '') . '></td>';
 
927
            $form .= '</tr>';
 
928
        }
 
929
 
 
930
        $form .= '</table>';
 
931
        $form .= '</div>';
 
932
        $form .= '</form>';
 
933
 
 
934
        $response->SetFormRequestResponse($form, __('Permissions'), '350px', '500px');
 
935
        $response->AddButton(__('Help'), 'XiboHelpRender("' . $helpManager->Link('Layout', 'Permissions') . '")');
 
936
        $response->AddButton(__('Cancel'), 'XiboDialogClose()');
 
937
        $response->AddButton(__('Save'), '$("#DataSetPermissionsForm").submit()');
 
938
        $response->Respond();
 
939
    }
 
940
 
 
941
    public function Permissions()
 
942
    {
 
943
        $db =& $this->db;
 
944
        $user =& $this->user;
 
945
        $response = new ResponseManager();
 
946
        Kit::ClassLoader('datasetgroupsecurity');
 
947
 
 
948
        $dataSetId = Kit::GetParam('datasetid', _POST, _INT);
 
949
        $groupIds = Kit::GetParam('groupids', _POST, _ARRAY);
 
950
 
 
951
        $auth = $this->user->DataSetAuth($dataSetId, true);
 
952
 
 
953
        if (!$auth->modifyPermissions)
 
954
            trigger_error(__('You do not have permissions to edit this dataset'), E_USER_ERROR);
 
955
 
 
956
        // Unlink all
 
957
        $security = new DataSetGroupSecurity($db);
 
958
        if (!$security->UnlinkAll($dataSetId))
 
959
            trigger_error(__('Unable to set permissions'));
 
960
 
 
961
        // Some assignments for the loop
 
962
        $lastGroupId = 0;
 
963
        $first = true;
 
964
        $view = 0;
 
965
        $edit = 0;
 
966
        $del = 0;
 
967
 
 
968
        // List of groupIds with view, edit and del assignments
 
969
        foreach($groupIds as $groupPermission)
 
970
        {
 
971
            $groupPermission = explode('_', $groupPermission);
 
972
            $groupId = $groupPermission[0];
 
973
 
 
974
            if ($first)
 
975
            {
 
976
                // First time through
 
977
                $first = false;
 
978
                $lastGroupId = $groupId;
 
979
            }
 
980
 
 
981
            if ($groupId != $lastGroupId)
 
982
            {
 
983
                // The groupId has changed, so we need to write the current settings to the db.
 
984
                // Link new permissions
 
985
                if (!$security->Link($dataSetId, $groupId, $view, $edit, $del))
 
986
                    trigger_error(__('Unable to set permissions'));
 
987
 
 
988
                // Reset
 
989
                $lastGroupId = $groupId;
 
990
                $view = 0;
 
991
                $edit = 0;
 
992
                $del = 0;
 
993
            }
 
994
 
 
995
            switch ($groupPermission[1])
 
996
            {
 
997
                case 'view':
 
998
                    $view = 1;
 
999
                    break;
 
1000
 
 
1001
                case 'edit':
 
1002
                    $edit = 1;
 
1003
                    break;
 
1004
 
 
1005
                case 'del':
 
1006
                    $del = 1;
 
1007
                    break;
 
1008
            }
 
1009
        }
 
1010
 
 
1011
        // Need to do the last one
 
1012
        if (!$first)
 
1013
        {
 
1014
            if (!$security->Link($dataSetId, $lastGroupId, $view, $edit, $del))
 
1015
                    trigger_error(__('Unable to set permissions'));
 
1016
        }
 
1017
 
 
1018
        $response->SetFormSubmitResponse(__('Permissions Changed'));
 
1019
        $response->Respond();
 
1020
    }
 
1021
}
 
1022
?>