~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Controller/Help.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
20
20
 */
21
21
namespace Xibo\Controller;
22
 
 
23
 
use Xibo\Exception\AccessDeniedException;
24
 
use Xibo\Factory\HelpFactory;
25
 
use Xibo\Service\ConfigServiceInterface;
26
 
use Xibo\Service\DateServiceInterface;
27
 
use Xibo\Service\LogServiceInterface;
28
 
use Xibo\Service\SanitizerServiceInterface;
29
 
 
30
 
/**
31
 
 * Class Help
32
 
 * @package Xibo\Controller
33
 
 */
 
22
use baseDAO;
 
23
use FormManager;
 
24
use Xibo\Helper\ApplicationState;
 
25
use Xibo\Helper\Theme;
 
26
 
 
27
defined('XIBO') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");
 
28
 
34
29
class Help extends Base
35
30
{
36
 
    /**
37
 
     * @var HelpFactory
38
 
     */
39
 
    private $helpFactory;
40
 
 
41
 
    /**
42
 
     * Set common dependencies.
43
 
     * @param LogServiceInterface $log
44
 
     * @param SanitizerServiceInterface $sanitizerService
45
 
     * @param \Xibo\Helper\ApplicationState $state
46
 
     * @param \Xibo\Entity\User $user
47
 
     * @param \Xibo\Service\HelpServiceInterface $help
48
 
     * @param DateServiceInterface $date
49
 
     * @param ConfigServiceInterface $config
50
 
     * @param HelpFactory $helpFactory
51
 
     */
52
 
    public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $helpFactory)
53
 
    {
54
 
        $this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);
55
 
 
56
 
        $this->helpFactory = $helpFactory;
57
 
    }
58
 
 
59
 
    /**
60
 
     * Help Page
 
31
    private $helpLink;
 
32
 
 
33
    /**
 
34
     * No display page functionaility
 
35
     * @return
61
36
     */
62
37
    function displayPage()
63
38
    {
64
 
        $this->getState()->template = 'help-page';
65
 
    }
66
 
 
67
 
    public function grid()
68
 
    {
69
 
        $helpLinks = $this->helpFactory->query($this->gridRenderSort(), $this->gridRenderFilter());
70
 
 
71
 
        foreach ($helpLinks as $row) {
72
 
            /* @var \Xibo\Entity\Help $row */
 
39
        // Configure the theme
 
40
        $id = uniqid();
 
41
        Theme::Set('id', $id);
 
42
        Theme::Set('form_meta', '<input type="hidden" name="p" value="help"><input type="hidden" name="q" value="Grid">');
 
43
        Theme::Set('pager', ApplicationState::Pager($id));
 
44
 
 
45
        // Call to render the template
 
46
        Theme::Set('header_text', __('Help Links'));
 
47
        Theme::Set('form_fields', array());
 
48
        $this->getState()->html .= Theme::RenderReturn('grid_render');
 
49
    }
 
50
 
 
51
    function actionMenu()
 
52
    {
 
53
 
 
54
        return array(
 
55
            array('title' => __('Add Help Link'),
 
56
                'class' => 'XiboFormButton',
 
57
                'selected' => false,
 
58
                'link' => 'index.php?p=help&q=AddForm',
 
59
                'help' => __('Add a new Help page'),
 
60
                'onclick' => ''
 
61
            )
 
62
        );
 
63
    }
 
64
 
 
65
    public function Grid()
 
66
    {
 
67
 
 
68
        $user = $this->getUser();
 
69
        $response = $this->getState();
 
70
 
 
71
        //display the display table
 
72
        $SQL = <<<SQL
 
73
        SELECT HelpID, Topic, Category, Link
 
74
          FROM `help`
 
75
        ORDER BY Topic, Category
 
76
SQL;
 
77
 
 
78
        // Load results into an array
 
79
        $helplinks = $db->GetArray($SQL);
 
80
 
 
81
        if (!is_array($helplinks)) {
 
82
            trigger_error($db->error());
 
83
            trigger_error(__('Error getting list of helplinks'), E_USER_ERROR);
 
84
        }
 
85
 
 
86
        $cols = array(
 
87
            array('name' => 'topic', 'title' => __('Topic')),
 
88
            array('name' => 'category', 'title' => __('Category')),
 
89
            array('name' => 'link', 'title' => __('Link'))
 
90
        );
 
91
        Theme::Set('table_cols', $cols);
 
92
 
 
93
        $rows = array();
 
94
 
 
95
        foreach ($helplinks as $row) {
 
96
 
 
97
            $row['helpid'] = \Xibo\Helper\Sanitize::int($row['HelpID']);
 
98
            $row['topic'] = \Xibo\Helper\Sanitize::string($row['Topic']);
 
99
            $row['category'] = \Xibo\Helper\Sanitize::string($row['Category']);
 
100
            $row['link'] = \Xibo\Helper\Sanitize::string($row['Link']);
 
101
 
 
102
            $row['buttons'] = array();
73
103
 
74
104
            // we only want to show certain buttons, depending on the user logged in
75
 
            if ($this->getUser()->userTypeId == 1) {
 
105
            if ($user->usertypeid == 1) {
76
106
 
77
107
                // Edit
78
 
                $row->buttons[] = array(
 
108
                $row['buttons'][] = array(
79
109
                    'id' => 'help_button_edit',
80
 
                    'url' => $this->urlFor('help.edit.form', ['id' => $row->helpId]),
 
110
                    'url' => 'index.php?p=help&q=EditForm&HelpID=' . $row['helpid'],
81
111
                    'text' => __('Edit')
82
112
                );
83
113
 
84
114
                // Delete
85
 
                $row->buttons[] = array(
 
115
                $row['buttons'][] = array(
86
116
                    'id' => 'help_button_delete',
87
 
                    'url' => $this->urlFor('help.delete.form', ['id' => $row->helpId]),
 
117
                    'url' => 'index.php?p=help&q=DeleteForm&HelpID=' . $row['helpid'],
88
118
                    'text' => __('Delete')
89
119
                );
90
120
 
91
121
                // Test
92
 
                $row->buttons[] = array(
 
122
                $row['buttons'][] = array(
93
123
                    'id' => 'help_button_test',
94
 
                    'url' => $this->getHelp()->link($row->topic, $row->category),
 
124
                    'url' => Help::Link($row['topic'], $row['category']),
95
125
                    'text' => __('Test')
96
126
                );
97
127
            }
 
128
 
 
129
            $rows[] = $row;
98
130
        }
99
131
 
100
 
        $this->getState()->template = 'grid';
101
 
        $this->getState()->recordsTotal = $this->helpFactory->countLast();
102
 
        $this->getState()->setData($helpLinks);
 
132
        Theme::Set('table_rows', $rows);
 
133
 
 
134
        $output = Theme::RenderReturn('table_render');
 
135
 
 
136
        $response->SetGridResponse($output);
 
137
 
103
138
    }
104
139
 
105
 
    /**
106
 
     * Add Form
107
 
     */
108
 
    public function addForm()
 
140
    public function AddForm()
109
141
    {
110
 
        if ($this->getUser()->userTypeId != 1)
111
 
            throw new AccessDeniedException();
112
 
 
113
 
        $this->getState()->template = 'help-form-add';
 
142
        $response = $this->getState();
 
143
 
 
144
        // Set some information about the form
 
145
        Theme::Set('form_id', 'HelpAddForm');
 
146
        Theme::Set('form_action', 'index.php?p=help&q=Add');
 
147
 
 
148
        $formFields = array();
 
149
        $formFields[] = FormManager::AddText('Topic', __('Topic'), NULL,
 
150
            __('The Topic for this Help Link'), 't', 'maxlength="254" required');
 
151
 
 
152
        $formFields[] = FormManager::AddText('Category', __('Category'), NULL,
 
153
            __('The Category for this Help Link'), 'c', 'maxlength="254" required');
 
154
 
 
155
        $formFields[] = FormManager::AddText('Link', __('Link'), NULL,
 
156
            __('The Link to open for this help topic and category'), 'c', 'maxlength="254" required');
 
157
 
 
158
        Theme::Set('form_fields', $formFields);
 
159
 
 
160
        $response->SetFormRequestResponse(NULL, __('Add Help Link'), '350px', '325px');
 
161
        $response->AddButton(__('Cancel'), 'XiboDialogClose()');
 
162
        $response->AddButton(__('Save'), '$("#HelpAddForm").submit()');
 
163
 
114
164
    }
115
165
 
116
166
    /**
117
167
     * Help Edit form
118
 
     * @param int $helpId
119
168
     */
120
 
    public function editForm($helpId)
 
169
    public function EditForm()
121
170
    {
122
 
        if ($this->getUser()->userTypeId != 1)
123
 
            throw new AccessDeniedException();
124
 
 
125
 
        $help = $this->helpFactory->getById($helpId);
126
 
 
127
 
        $this->getState()->template = 'help-form-edit';
128
 
        $this->getState()->setData([
129
 
            'help' => $help
130
 
        ]);
 
171
 
 
172
        $user = $this->getUser();
 
173
        $response = $this->getState();
 
174
 
 
175
        $helpId = \Xibo\Helper\Sanitize::getInt('HelpID');
 
176
 
 
177
        // Pull the currently known info from the DB
 
178
        $SQL = "SELECT HelpID, Topic, Category, Link FROM `help` WHERE HelpID = %d ";
 
179
        $SQL = sprintf($SQL, $helpId);
 
180
 
 
181
        if (!$row = $db->GetSingleRow($SQL)) {
 
182
            trigger_error($db->error());
 
183
            trigger_error(__('Error getting Help Link'));
 
184
        }
 
185
 
 
186
        // Set some information about the form
 
187
        Theme::Set('form_id', 'HelpEditForm');
 
188
        Theme::Set('form_action', 'index.php?p=help&q=Edit');
 
189
        Theme::Set('form_meta', '<input type="hidden" name="HelpID" value="' . $helpId . '" />');
 
190
 
 
191
        $formFields = array();
 
192
        $formFields[] = FormManager::AddText('Topic', __('Topic'), \Xibo\Helper\Sanitize::string($row['Topic']),
 
193
            __('The Topic for this Help Link'), 't', 'maxlength="254" required');
 
194
 
 
195
        $formFields[] = FormManager::AddText('Category', __('Category'), \Xibo\Helper\Sanitize::string($row['Category']),
 
196
            __('The Category for this Help Link'), 'c', 'maxlength="254" required');
 
197
 
 
198
        $formFields[] = FormManager::AddText('Link', __('Link'), \Xibo\Helper\Sanitize::string($row['Link']),
 
199
            __('The Link to open for this help topic and category'), 'c', 'maxlength="254" required');
 
200
 
 
201
        Theme::Set('form_fields', $formFields);
 
202
 
 
203
        $response->SetFormRequestResponse(NULL, __('Edit Help Link'), '350px', '325px');
 
204
        $response->AddButton(__('Cancel'), 'XiboDialogClose()');
 
205
        $response->AddButton(__('Save'), '$("#HelpEditForm").submit()');
 
206
 
131
207
    }
132
208
 
133
209
    /**
134
210
     * Delete Help Link Form
135
 
     * @param int $helpId
136
211
     */
137
 
    public function deleteForm($helpId)
 
212
    public function DeleteForm()
138
213
    {
139
 
        if ($this->getUser()->userTypeId != 1)
140
 
            throw new AccessDeniedException();
141
 
 
142
 
        $help = $this->helpFactory->getById($helpId);
143
 
 
144
 
        $this->getState()->template = 'help-form-delete';
145
 
        $this->getState()->setData([
146
 
            'help' => $help
147
 
        ]);
 
214
 
 
215
        $response = $this->getState();
 
216
        $helpId = \Xibo\Helper\Sanitize::getInt('HelpID');
 
217
 
 
218
        // Set some information about the form
 
219
        Theme::Set('form_id', 'HelpDeleteForm');
 
220
        Theme::Set('form_action', 'index.php?p=help&q=Delete');
 
221
        Theme::Set('form_meta', '<input type="hidden" name="HelpID" value="' . $helpId . '" />');
 
222
 
 
223
        Theme::Set('form_fields', array(FormManager::AddMessage(__('Are you sure you want to delete?'))));
 
224
 
 
225
        $response->SetFormRequestResponse(NULL, __('Delete Help Link'), '350px', '175px');
 
226
        $response->AddButton(__('No'), 'XiboDialogClose()');
 
227
        $response->AddButton(__('Yes'), '$("#HelpDeleteForm").submit()');
 
228
 
148
229
    }
149
230
 
150
231
    /**
151
232
     * Adds a help link
152
233
     */
153
 
    public function add()
 
234
    public function Add()
154
235
    {
155
 
        if ($this->getUser()->userTypeId != 1)
156
 
            throw new AccessDeniedException();
157
 
 
158
 
        $help = $this->helpFactory->createEmpty();
159
 
        $help->topic = $this->getSanitizer()->getString('topic');
160
 
        $help->category = $this->getSanitizer()->getString('category');
161
 
        $help->link = $this->getSanitizer()->getString('link');
162
 
 
163
 
        $help->save();
164
 
 
165
 
        // Return
166
 
        $this->getState()->hydrate([
167
 
            'message' => sprintf(__('Added %s'), $help->topic),
168
 
            'id' => $help->helpId,
169
 
            'data' => $help
170
 
        ]);
 
236
 
 
237
 
 
238
        $response = $this->getState();
 
239
 
 
240
        $topic = \Xibo\Helper\Sanitize::getString('Topic');
 
241
        $category = \Xibo\Helper\Sanitize::getString('Category');
 
242
        $link = \Xibo\Helper\Sanitize::getString('Link');
 
243
 
 
244
        // Deal with the Edit
 
245
 
 
246
        $helpObject = new Help($db);
 
247
 
 
248
        if (!$helpObject->Add($topic, $category, $link))
 
249
            trigger_error($helpObject->GetErrorMessage(), E_USER_ERROR);
 
250
 
 
251
        $response->SetFormSubmitResponse(__('Help Link Added'), false);
 
252
 
171
253
    }
172
254
 
173
255
    /**
174
256
     * Edits a help link
175
 
     * @param int $helpId
176
257
     */
177
 
    public function edit($helpId)
 
258
    public function Edit()
178
259
    {
179
 
        if ($this->getUser()->userTypeId != 1)
180
 
            throw new AccessDeniedException();
181
 
 
182
 
        $help = $this->helpFactory->getById($helpId);
183
 
        $help->topic = $this->getSanitizer()->getString('topic');
184
 
        $help->category = $this->getSanitizer()->getString('category');
185
 
        $help->link = $this->getSanitizer()->getString('link');
186
 
 
187
 
        $help->save();
188
 
 
189
 
        // Return
190
 
        $this->getState()->hydrate([
191
 
            'message' => sprintf(__('Edited %s'), $help->topic),
192
 
            'id' => $help->helpId,
193
 
            'data' => $help
194
 
        ]);
 
260
 
 
261
 
 
262
        $response = $this->getState();
 
263
 
 
264
        $helpId = \Xibo\Helper\Sanitize::getInt('HelpID');
 
265
        $topic = \Xibo\Helper\Sanitize::getString('Topic');
 
266
        $category = \Xibo\Helper\Sanitize::getString('Category');
 
267
        $link = \Xibo\Helper\Sanitize::getString('Link');
 
268
 
 
269
        // Deal with the Edit
 
270
 
 
271
        $helpObject = new Help($db);
 
272
 
 
273
        if (!$helpObject->Edit($helpId, $topic, $category, $link))
 
274
            trigger_error($helpObject->GetErrorMessage(), E_USER_ERROR);
 
275
 
 
276
        $response->SetFormSubmitResponse(__('Help Link Edited'), false);
 
277
 
195
278
    }
196
279
 
197
 
    /**
198
 
     * Delete
199
 
     * @param int $helpId
200
 
     * @throws \Xibo\Exception\NotFoundException
201
 
     */
202
 
    public function delete($helpId)
 
280
    public function Delete()
203
281
    {
204
 
        if ($this->getUser()->userTypeId != 1)
205
 
            throw new AccessDeniedException();
206
 
 
207
 
        $help = $this->helpFactory->getById($helpId);
208
 
        $help->delete();
209
 
 
210
 
        // Return
211
 
        $this->getState()->hydrate([
212
 
            'message' => sprintf(__('Deleted %s'), $help->topic)
213
 
        ]);
 
282
 
 
283
 
 
284
        $response = $this->getState();
 
285
 
 
286
        $helpId = \Xibo\Helper\Sanitize::getInt('HelpID');
 
287
 
 
288
        // Deal with the Edit
 
289
 
 
290
        $helpObject = new Help($db);
 
291
 
 
292
        if (!$helpObject->Delete($helpId))
 
293
            trigger_error($helpObject->GetErrorMessage(), E_USER_ERROR);
 
294
 
 
295
        $response->SetFormSubmitResponse(__('Help Link Deleted'), false);
 
296
 
214
297
    }
215
298
}
 
299
 
 
300
?>