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

Language PHP Lines 165
MD5 Hash be1168f308ecc11e3041fa4c013f3a6f
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
<?php
class Admin_MenusPresenter extends Admin_SecurePresenter
{
	
	private $new = false, $edit =false;
	
	public function actionDefault()
	{
            $this->template->new = $this->new;
            $this->template->edit = $this->edit;
            $this->template->link = $this->createAddNewLink();
	}
	
	public function createAddNewLink()
	{
            $link = Html::el('a')->href($this->link('newMenu!'))->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;
	}
	
	public function createComponentFormNewMenu($name)
	{
            $form = new LiveForm($this, $name);
            $form->addText('name', 'Name')->addRule(Form::FILLED, 'Please enter a name.');
            $templates = $this->model('Menu')->getTemplates();
            $form->addSelect('template', 'Template', $templates);
            $form->addButton('btnClose','Close');
            $form->addSubmit('save','Save')->onClick[] = array($this, 'formNewMenuSubmitted');
            $form['save']->getControlPrototype()->class('ajax');
            return $form;
	}
	
	public function createComponentFormEditMenu($name)
	{
            $menu_id = $this->getParam('id');
            
            $templates = $this->model('Menu')->getTemplates();
            $data = $this->model('Menu')->getById($menu_id);

            $form = new LiveForm($this, $name);
            $form->addText('name', 'Name')->addRule(Form::FILLED, 'Please enter a name.')->setValue($data->name);
            $form->addSelect('template', 'Template', $templates);
            $form->addHidden('id')->setValue($menu_id); 
            $form->setDefaults($data);
            $form->addButton('btnClose','Close');
            $form->addSubmit('save','Save')->onClick[] = array($this, 'formEditMenuSubmitted');
            return $form;
	}
    
	public function formNewMenuSubmitted($button)
	{
            $form = $button->getForm();
            $values = $form->getValues();
            $name = $values['name'];
            unset($values['btnClose']);
            try
            {
                $this->model('menu')->createMenu($values);
                $this->flash('Menu '.$name.' created successfully');
                $this->invalidateControl('form');
                $this->invalidateControl('grid');
            }
            catch (DibiDriverException $e)
            {
                $this->flash($e->getMessage());
            }
	}

        public function formEditMenuSubmitted($button)
	{
            $form = $button->getForm();
            $values = $form->getValues();
            $name = $values['name'];
            unset($values['btnClose']);
            try
            {
                $this->model('menu')->update($values);
                $this->flash('Menu '.$name.' updated successfully');
                $this->invalidateControl('form');
                //$this->redirect('MenuItems:Default', array('name' => $name));
                $this->invalidateControl('grid');
            }
            catch (DibiDriverException $e)
            {
                $this->flash($e->getMessage());
            }
	}
	
	public function formMenuClose($button)
	{
            $this->invalidateControl('form');
	}
	
	public function createComponentGrid($name)
	{
            $grid = new DataGrid($this, $name);
            $ds = $this->model('menu')->getDs();
            $grid->bindDataTable($ds);

            $grid->keyName = 'id';
            $grid->addColumn('name', 'Name')->addTextFilter();
            $grid['name']->formatCallback[] = array($this, 'createNameLink');
            // přidáme sloupec pro akce
            $grid->addActionColumn('Actions');

            // a naplníme datagrid akcemi pomocí továrničky
            $grid->addAction('Items', 'MenuItems:default', Html::el('span')->class('icon icon-items'), $useAjax = true);
            $grid->addAction('Edit', 'editMenu!', Html::el('span')->class('icon icon-edit'), $useAjax = true);
            $grid->addAction('Delete', 'confirmForm:confirmDelete!', Html::el('span')->class('icon icon-delete'), $useAjax = true);
            return $grid;
	} 	
	
	public function createNameLink($value, DibiRow $data)
	{
            return Html::el('a',$value)->class('ajax')->href($this->link('MenuItems:default', array('id' => $data['id'])));
	}
	
	function createComponentConfirmForm()
        {
            $form = new ConfirmationDialog();
            $form->addConfirmer(
                            'delete', // název signálu bude 'confirmDelete!'
                            array($this, 'deleteMenu'), // 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 menu '.$params['name'].'?';
	}
	
	public function deleteMenu($params, $dialog)
	{
            $name = $params;
            try{
                    $menu_id = $this->model('menu')->getByName($name)->id;
                    $this->model('menu')->deleteById($menu_id);
                    $this->flash('Menu '.$name.' deleted!');
                    $this->invalidateControl('grid');
            }
            catch(DibiDriverException $e)
            {
                    $this->flash($e->getMessage());
            }
	}
	
	public function handleNewMenu()
	{
            $this->template->new = true;

            $this->invalidateControl('form');
            $this->flashCmd('openForm');
	}
	
	public function handleEditMenu($name)
	{
            $this->template->menu = $name;
            $this->template->edit = true;
            $this->invalidateControl('form');
            $this->flashCmd('openForm');	
	}
}
?>
Back to Top