Mokuji /www/app/AdminModule/presenters/AdminPagesPresenter.php

Language PHP Lines 455
MD5 Hash b0311a67b3133b5deb2bf0d7f7b07cf9
Repository https://github.com/bazo/Mokuji.git View Raw File View Project SPDX
  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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
class Admin_PagesPresenter extends Admin_SecurePresenter
{
    protected $item, $html;
    private $categories, $new = false, $edit = false;
    
    public function actionDefault()
    {
        $this->view = 'pages';
        $this->template->new = $this->new;
        $this->template->edit = $this->edit;
        $this->template->link = $this->createComponentLink();
    }
    
    public function createComponentLink()
    {
        $link = Html::el('a')->href($this->link('newPage!'))->class('ajax')->add(Html::el('div')->id('create-new-link')->add(Html::el('div')->id('icon'))->add(Html::el('span')->add('Create new')));
        return $link;
    }
    
    //GRID
    public function createComponentPagesGrid($name)
    {
        $grid = new DataGrid($this, $name);
        $grid->rememberState = TRUE; // povolí ukládání stavů komponenty do session
        $grid->timeout = '+ 7 days';
        $ds = $this->model('Pages')->getDs();
        $grid->bindDataTable($ds);

        $grid->keyName = 'title';
        $grid->addColumn('homepage', '');
        $grid['homepage']->getHeaderPrototype()->class('homepage-column');
        $grid['homepage']->getCellPrototype()->class('homepage-column');;
        $grid['homepage']->formatCallback[] = array($this, 'homepageCallback');
        //$grid['creditLimit']->getCellPrototype()->style('text-align: center');
        $grid->addColumn('title', 'Title')->addFilter();

        $categories = $this->model('categories')->getPairs();
        $grid->addColumn('category', 'Category')->addSelectboxFilter($categories);
        $this->categories = array('0' => 'none') + $categories;
        $grid['category']->formatCallback[] = array($this, 'gridCategoryCallback');
        $grid['title']->formatCallback[] = array($this, 'createLink');

        $grid->addColumn('template', 'Template')->addSelectboxFilter();
        $grid['template']->getCellPrototype()->style('text-align: center');
        $grid['template']->formatCallback[] = array($this, 'templateFormatCallback');
        
        $grid->addColumn('published', 'Published')->addSelectboxFilter(array('0' => "No", '1' => "Yes"), TRUE);
        $grid['published']->getCellPrototype()->style('text-align: center');
        $grid['published']->formatCallback[] = array($this, 'publishFormatCallback');
        $grid->addDateColumn('publish_time', 'Publish time', '%d.%m.%Y %H:%M:%S');
        $grid['publish_time']->getHeaderPrototype()->style('text-align: center');
        $grid['publish_time']->getCellPrototype()->style('text-align: center');
        $grid->addActionColumn('Actions');
        $grid->addAction('Edit', 'editPage!', Html::el('span')->class('icon icon-edit'), $useAjax = TRUE);
        $grid->addAction('Delete', 'confirmForm:confirmDelete!', Html::el('span')->class('icon icon-delete'), $useAjax = TRUE);
        $renderer = $grid->getRenderer();
        $renderer->paginatorFormat = '%input%';
        $grid->setRenderer($renderer);
        return $grid;
    }

    public function templateFormatCallback($value, DibiRow $data)
    {
        $templates = $this->model('contentTemplates')->get('page');   
        $select = Html::el('select')->class('template-change')->{'data-id'}($data->id);
        foreach($templates as $template)
        {
            if($value == $template)
            $select->create('option')->value($template)->setText($template)->selected('selected');
            else $select->create('option')->value($template)->setText($template);
        }
        return $select;
    }
    
    public function homepageCallback($value, DibiRow $data) {
        if($value == 1) return Html::el('span')->class('icon icon-homepage')->title('Homepage');//->add('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
        if($value == 0) return Html::el('a')->add(Html::el('span')->class('icon icon-makehomepage')->title('Make homepage'))->href($this->link('makeHomepage!').'&title='.$data->title)->class('datagrid-ajax');
    }

    public function publishFormatCallback($value, DibiRow $data)
    {
        $checkbox = Html::el('input')->type('checkbox')->class('chboxPublished')->{'data-id'}($data->id);
        if ($value) $checkbox->checked = TRUE;
        return (string) $checkbox;
    }
    
    public function gridCategoryCallback($value, DibiRow $data)
    {
        $selected_cat = (int)$value;
        $select = Html::el('select')->class('category-change')->{'data-id'}($data->id);
        foreach($this->categories as $key => $category)
        {
            if($selected_cat == (int)$key)
            $select->create('option')->value($key)->setText($category)->selected('selected');
            else $select->create('option')->value($key)->setText($category);
        }
        return $select;
    }
    
    public function handleChangeCategory($page, $new_cat)
    {
        $id = (int)$page;
        $new_cat = $new_cat;
        fd($page, $new_cat);
        try{
            $this->model('pages')->update(array('id'=>$id, 'category'=>$new_cat));
            $this->flash('Category updated!');
        }
        catch(DibiDriverException $e)
        {
            $this->flash($e->getMessage());
        }
        $this->sendPayload();
    }
    
    public function handleChangeTemplate($page, $template)
    {
        $id = (int)$page;
        try{
            $this->model('pages')->update(array('id'=>$id, 'template'=>$template));
            $this->flash('Template updated!');
        }
        catch(DibiDriverException $e)
        {
            $this->flash($e->getMessage());
        }
        $this->sendPayload();
    }
    
    public function handleChangePublished()
    {
        $data = $this->getRequest()->getPost();
        $id = (int)$data['page'];
        $published = ($data['published'] == 'true') ? 1 : 0;
        $this->model('pages')->update(array('id'=>$id, 'published'=>$published));
        if($published == 1) $this->flash('Page published!');
        if($published == 0) $this->flash('Page set as draft!');
        $this->sendPayload();
    }
    
    public function handleNewPage()
    {
        $this->template->new = true;
        $this->invalidateControl('form');
        $this->flashCmd('openForm');    
    }

    public function handleMakeHomepage($title)
    {
        $this->model('pages')->makeHomepage($title);
        $this->invalidateControl('grid');
    }

    public function createLink($value, DibiRow $data)
    {
        return Html::el('a',$value)->href($this->link('editPage!',array('title' => $data['title'])))->class('inline-edit ajax');
    }
    
    function createComponentConfirmForm()
    {
        $form = new ConfirmationDialog();
        $form->addConfirmer(
                        'delete', // název signálu bude 'confirmDelete!'
                        array($this, 'deletePage'), // callback na funkci při kliku na YES
                        array($this, 'questionDelete') // otázka (může být i callback vracející string)
                        );
        return $form;
    }
    
    public function questionDelete($dialog, $params)
    {
        return 'Really delete Page '.$params['title'].'?';
    }
    
    public function deletePage($params, $dialog)
    {
        $title = $params;
        $webalized_title = String::webalize($title);
        try{
            $this->model('pages')->delete($title);
            $this->model('menuItems')->deleteRelated($webalized_title);
            $this['page']->redraw('Pages');
            $this->flash('Page '.$title.' deleted!');
            if(!$this->isAjax()) {$this->redirect('Pages:');}
        }
        catch(DibiDriverException $e)
        {
            $this->flash($e->getMessage());
        }    
    }

    public function handleEditPage()
    {
        $session = Environment::getSession('params');
        $session->params = $this->getRequest()->getParams();
        $this->template->edit = true;
        $this->invalidateControl('form');
        $this->flashCmd('openForm');
    }
    
    
    //END GRID
    
    //Form New Page
    
    public function createComponentFormNewPage($name)
    {
        $form = new LiveForm($this, $name);
        $form = $this->formNewEditFields($form);
        $form->addSubmit('btnSaveAsDraft', 'Save as draft')->onClick[] = array($this, 'formNewPageSave');
        $form->addSubmit('btnPublish', 'Publish')->onClick[] = array($this, 'formNewPageSave');
        $form['btnSaveAsDraft']->getControlPrototype()->class('ajax');
        $form['btnPublish']->getControlPrototype()->class('ajax');
        return $form;
    } 
    
    public function createComponentFormEditPage($name)
    {
        $session = Environment::getSession('params');
        $title = ($this->getParam('title') == '') ? $session->params['title'] : $this->getParam('title');
        
        if($title == '')
        {
            return 'Please select a page to edit.';
        }
        else{
            $form = new LiveForm($this, $name);
            $data = array();
            if($this->isAjax())$data = $this->model('pages')->getByTitle($title);
            $form = $this->formNewEditFields($form);
            $form->addHidden('id', $data->id);
            $form->addSubmit('btnSaveAsDraft', 'Save as draft')->onClick[] = array($this, 'formEditSubmitted');
            $form->addSubmit('btnPublish', 'Save')->onClick[] = array($this, 'formEditSubmitted');
            $form['btnSaveAsDraft']->getControlPrototype()->class('ajax');
            $form['btnPublish']->getControlPrototype()->class('ajax');
            $form->setDefaults( (array)$data);
            return $form;
        }
    } 
   
    private function formNewEditFields(AppForm $form)
    {
        $form->addGroup('Content')->setOption('container', Html::el('fieldset')->id('contents'));
            $form->addText('title','Title')->addRule(Form::FILLED, 'Fill title');
            
            $form->addTextarea('content', 'Text');
            $templates = $this->model('contentTemplates')->get('page');
        $form->addGroup('Properties')->setOption('container', Html::el('fieldset')->id('properties'));
            $categories = $this->model('categories')->getPairs();
            $categories = array('0' => 'none') + $categories;
            $form->addSelect('category', 'Category', $categories);
            $form->addCheckBox('homepage', 'Set as homepage?');
            $form->addSelect('template', 'Template', $templates);
            $form->addCheckBox('has_widgets', 'Has widgets');
            $form->addText('publish_time', 'Publish time');
            //$form->addText('time', '');
            $form->addText('keywords', 'keywords');
            $form->addTextarea('description', 'description');
        $form->addHidden('content_type')->setValue('page');
        if($form->name == 'formNewPage')
        {
            //$form->addGroup('Link')->setOption('container', Html::el('fieldset')->id('link'));
                $menus = $this->model('menu')->getPairs();
                $menus = array(0 => 'none') + $menus;
                $form->addSelect('link','Link in menu', $menus);
        }
        $form->addTextArea('css_files', 'Css files')->getControlPrototype()->{'data-ext'} = 'css';
        $form->addTextArea('js_files', 'Js files')->getControlPrototype()->{'data-ext'} = 'js';
        $form->addGroup('')->setOption('container', Html::el('fieldset')->id('buttons'));
            $form->addButton('btnClose', 'Close');
            $form->addSubmit('btnPreview', 'Preview')->onClick[] = array($this, 'formNewPagePreview');
            $form['btnPreview']->getControlPrototype()->class('ajax');
        return $form;
    }
    
    public function formNewPagePreview(Button $button)
    {
        $form = $button->getForm();
        $values = $form->getValues();
        try{
            //$request = new PresenterRequest('Front:Preview', 'POST', array('action' => 'preview'), array('values' => $values));
            //$presenter = new Front_PreviewPresenter();
            $request = new PresenterRequest('Front:Page', 'POST', array('action' => 'preview'), array('values' => $values));
            $presenter = new Front_PagePresenter();
            $response = $presenter->run($request); //ak by bolo $this, zmeni sa aktualny presenter a cele to prestane fungovat
            //$response = $presenter->run($request);
            ob_start();
            $response->send();
            $html = ob_get_clean();
            $html = preg_replace('~(\<a.*href\=")(?:.*)(".*\>)~iU', '$1#$2', $html);
            $session = Environment::getSession('preview');
            $session->html = $html;
            
            $this->template->response = $html;
            $this->view = 'preview_wrap';
            
            $this->refreshConfig(); //preview changes theme, could be fixed by using admin_theme, front_theme
            if(!$this->isAjax())
            {
                
            }
            else
            {
                $this->invalidateControl('preview');
                $this->flashCmd('preview');
            }
        }
        catch(DibiDriverException $e)
        {
            $this->flash($e->getMessage());
        }
    }
    
    public function formClose(Button $button)
    {
        $this->invalidateControl('form');
    }
    
    public function handlePreview()
    {
        $session = Environment::getSession('preview');
        $html  = $session->html;
        $session->remove();
        $this->view = 'preview';
        $files = $this->formatTemplateFiles($this->getName(), $this->view);
        foreach ($files as $file) 
        {
            if (is_file($file)) {
                $this->template->setFile($file);
                break;
            }
        }
        $this->template->response = $html;
        $this->getTemplate()->render();
        $this->terminate();
    }
    
    public function createComponentFormPreviewToolbar($name)
    {
        $form = new LiveForm($this, $name);
        $renderer = $form->getRenderer();
        $renderer->wrappers['controls']['container'] = null;;
        $renderer->wrappers['pair']['container'] = NULL;
        $renderer->wrappers['label']['container'] = null;
        $renderer->wrappers['control']['container'] = null;
        $form->addButton('btnClose', 'Close');
        $form->addSubmit('btnUpdate', 'Update')->onClick[] = array($this, 'formPreviewToolbarUpdate');
        $form['btnUpdate']->getControlPrototype()->class('ajax');
        return $form;
    } 
    
    public function formPreviewToolbarCancel(Button $button)
    {
        $this->flashCmd('closePreview');
        $this->sendPayload();
    }
    
    public function formPreviewToolbarUpdate(Button $button)
    {
        $this->flashCmd('copyToEditor');
        $this->flashCmd('closePreview');
        $this->sendPayload();
    }
    
    public function formNewPageSave(Button $button)
    {
        $form = $button->getForm();
        $values = $form->getValues();
        unset($values['btnClose']);
        if($form['btnPublish']->isSubmittedBy()) $values['published'] = 1;
        if($form['btnSaveAsDraft']->isSubmittedBy()) $values['published'] = 0;
        try{
            $this->model('pages')->save($values);
            $this->flash('Page created');
            if(!$this->isAjax())$this->redirect('Pages:');
            else
            {
                $this->invalidateControl('form');
                $this->invalidateControl('grid');
            }
        }
        catch(DibiDriverException $e)
        {
            $this->flash($e->getMessage());
        }
    }
    //END Form New Page
    
    public function formEditSubmitted(Button $button)
    {
        $session = Environment::getSession('params')->remove();
        $form = $button->getForm();
        if ($form->isValid()) {
            $values = $form->getValues();
            unset($values['btnClose']);
            try{
                $this->model('pages')->update($values);
                $this->flash('Page updated');
                if(!$this->isAjax())$this->redirect('Pages:');
            else
            {
                $this->invalidateControl('form');
                $this->invalidateControl('grid');
            }
            }
            catch(DibiDriverException $e)
            {
                $this->flash($e->getMessage());
            }
        }
    }
    //hodilo by sa refaktorovat
    public function handleBrowseFiles($dir,$ext)
    {
        $cache = Environment::getCache($this->getName());
        $ckey = __FUNCTION__.$dir.$ext;
        fd(Environment::getVariables());
        if(!isset($cache[$ckey]))
        {
            $root = WWW_DIR.'/';
            
            $response = '';
            if( file_exists($root . $dir) )
            {
                $files = scandir($root . $dir);
                natcasesort($files);
                if( count($files) > 2 ) {
                        $response .=  "<ul class=\"jqueryFileTree\" style=\"display: none;\">";

                        foreach( $files as $file ) {
                                if( file_exists($root . $dir . $file) && $file != '.' && $file != '..' && is_dir($root . $dir . $file)) {
                                        $response .=  "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . htmlentities($dir . $file) . "/\">" . htmlentities($file) . "</a></li>";
                                }
                        }

                        foreach( $files as $file ) {
                                $file_info = pathinfo($file);
                                if( file_exists($root . $dir . $file) && $file != '.' && $file != '..' && !is_dir($root . $dir . $file) && $file_info['extension'] == $ext ) {
                                        $ext = preg_replace('/^.*\./', '', $file);
                                        $response .=  "<li class=\"file ext_$ext\"><a href=\"#\" rel=\"" . htmlentities($dir . $file) . "\">" . htmlentities($file) . "</a></li>";
                                }
                        }
                        $response .=  "</ul>";
                }
            }
            $cache->save($ckey, $response, array(
                'expire' => time() + 60 * 10));
        }
        echo $cache[$ckey];
        $this->terminate();
    }
}
?>
Back to Top