PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/www/app/AdminModule/presenters/AdminCategoriesPresenter.php

https://github.com/bazo/Mokuji
PHP | 131 lines | 114 code | 13 blank | 4 comment | 2 complexity | d9c6b5d2a199cfbc8ba3e4e3daabf11c MD5 | raw file
  1<?php
  2class Admin_CategoriesPresenter extends Admin_SecurePresenter
  3{
  4	protected $item;
  5	
  6	public function actionDefault()
  7	{
  8		$this->view = 'categories';
  9        $this->template->edit = false;
 10        $this->template->link = $this->createAddNewLink();
 11	}
 12
 13    public function createAddNewLink()
 14    {
 15        $link = Html::el('a')->href($this->link('newCategory!'))->class('ajax')->add(Html::el('div')->id('create-new-link')->add(Html::el('div')->id('icon'))->add(Html::el('span')->add('Create new')));
 16        return $link;
 17    }
 18
 19	//GRID
 20	public function createComponentCategoriesGrid($name)
 21	{
 22            $grid = new DataGrid($this, $name);
 23            $ds = $this->model('categories')->getDs();
 24            $grid->bindDataTable($ds);
 25
 26            $grid->keyName = 'id';
 27            $grid->addColumn('title', 'Title')->addFilter();
 28
 29            $grid->addActionColumn('Actions');
 30            $grid->addAction('Edit', 'editCategory!', Html::el('span')->class('icon icon-edit'), $useAjax = TRUE);
 31            $grid->addAction('Delete', 'confirmForm:confirmDelete!', Html::el('span')->class('icon icon-delete'), $useAjax = TRUE);
 32
 33            return $grid;
 34	}
 35	
 36	function createComponentConfirmForm()
 37    {
 38        $form = new ConfirmationDialog();
 39        $form->addConfirmer(
 40                        'delete', // název signálu bude 'confirmDelete!'
 41                        array($this, 'deleteCategory'), // callback na funkci při kliku na YES
 42                        'Really delete category?'  // otázka (může být i callback vracející string)
 43                        );
 44        return $form;
 45    }
 46	
 47	public function deleteCategory($params, $dialog)
 48	{
 49            $id = $params;
 50            try{
 51                    $this->model('categories')->delete($id);
 52                    $this->flash('Category deleted!');
 53                    if(!$this->isAjax()) $this->redirect('Categories:');
 54                    else
 55                    {
 56                            $this['page']->refresh('categories');
 57                    }
 58            }
 59            catch(DibiDriverException $e)
 60            {
 61                    $this->flash($e->getMessage());
 62            }
 63		
 64	}
 65	//END GRID
 66		
 67	//Form New Category
 68	public function createComponentFormCategory($name)
 69	{
 70        $form = new LiveForm($this, $name);
 71        $form->addText('title','Title')->addRule(Form::FILLED, 'Fill title');
 72        try{
 73                $categories = $this->model('categories')->getIdLevel();
 74        }
 75        catch(DibiDriverException $e)
 76        {
 77                $this->flash($e->getMessage());
 78                $categories = array();
 79        }
 80        $categories = array('0;0' => 'none') + $categories;
 81        $form->addSelect('parent', 'Parent', $categories);
 82        $form->addTextarea('description', 'Description');
 83        $templates = array('category' => 'default');
 84        $form->addSelect('template', 'template', $templates);
 85        $form->addButton('btnClose', 'Close');
 86        $form->addSubmit('btnSave', 'Save')->onClick[] =array($this, 'formNewCategorySubmitted');
 87	    return $form;
 88	} 
 89	
 90	public function formNewCategorySubmitted($button)
 91	{
 92            $form = $button->getForm();
 93            $values = $form->getValues();
 94            unset($values['btnClose']);
 95            try
 96            {
 97                $this->model('categories')->save($values);
 98                $this->flash('Category '.$values['title'].' created');
 99                if(!$this->isAjax())$this->redirect('Categories:');
100                else
101                {
102                    $this->invalidateControl('grid');
103                    $this->invalidateControl('form');
104                }
105            }
106            catch(DibiDriverException $e)
107            {
108                    $this->flash($e->getMessage());
109            }
110	}
111	//END Form New Category
112    public function formClose(Button $button)
113    {
114        $this->invalidateControl('form');
115    }
116
117    public function handleNewCategory()
118    {
119        $this->template->edit = true;
120        $this->invalidateControl('form');
121        $this->flashCmd('openForm');
122    }
123
124    public function handleEditCategory()
125    {
126        $this->template->edit = true;
127        $this->invalidateControl('form');
128        $this->flashCmd('openForm');
129    }
130}
131?>