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

Language PHP Lines 139
MD5 Hash 7784da4b61f9545694369d1e7d7328e7
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
<?php
class Admin_MenuItemsPresenter extends Admin_SecurePresenter
{
	private /*$menu_items, $name,*/ $menu;
	
	public function actionDefault($id)
	{
            $this->menu = $this->model('menu')->getById($id);
            $this->template->menu = $this->menu->name;
            $this->template->menu_id = $this->menu->id;
            $this->view = 'menuitems';
            $this->invalidateControl('form');
	}
	
	public function handleItemMove()
	{
            $data = $this->getRequest()->getPost();
            $menu_id = $this->menu->id;
            if ($this->isAjax()) {
                    $item_id = $data['item_id'];
                    $parent_id = $data['parent_id'];
                    $prev_position = $data['prev_position'];
                    $parent_level = $data['parent_level'];
                    $level = $data['level'];
                    $diff = $parent_level - $level;
                    $this->model('menuItems')->moveItem( (int)$item_id, (int)$menu_id, (int)$parent_id, (int)$prev_position, (int)$parent_level, $diff);
                    $this->invalidateControl('menuDragDrop');
                    $this->flash('Menu updated');
            }
	}
	
	public function handleItemDelete()
	{
            $data = $this->getRequest()->getPost();
            $item = (int)$data['item'];
            try
            {
                    $this->model('menuItems')->deleteItem($item);
                    $this->presenter->flashMessage('item '.$item.' deleted');
                    $this->invalidateControl('menuDragDrop');
            }
            catch (DibiDriverException $e)
            {
                    $this->flash($e->getMessage());
            }
	}
	
	public function createComponentFormNewMenuItem($name)
	{
            $form = new LiveForm($this, $name);
            $form->addGroup('Add new item');

            $form->addText('title', 'Title')->addRule(Form::FILLED, 'Please enter a name.');
            $form->addRadioList('link_type', 'Link type',array('internal' => 'internal', 'external' => 'external'))->setDefaultValue('internal');
            $pages = $this->model('pages')->getPairs();
            $temp = array();
            if (isset($pages[""]))
            {
                    $temp = $pages[""];
                    unset($pages[""]);
            }
            $pages = array('none' => $temp) + $pages;
            unset($temp);
            $categories = $this->model('categories')->getPairs('slug', 'title');
            $items = array('homepagelink'=> 'Link to Homepage');
            foreach (array('none' => 'none') + $categories as $cat_slug => $cat_title) {
                    if($cat_title != 'none') $items[$cat_slug] = 'cat: '.$cat_title;
                    if( isset($pages[$cat_title]) ){
                            foreach ($pages[$cat_title] as $page_title => $data) {
                                    if($cat_title == 'none')
                                            $items[$data->page_slug] = 'page '.$page_title;
                                    else
                                            $items[$cat_slug.'/'.$data->page_slug] = 'article in '.$cat_title.': '.$page_title;				}
                    }
            }
            $form->addSelect('internal_url', 'Page', $items)->addRule(Form::FILLED, 'Please select a page.');
            $form->addText('external_url', 'Link')->addConditionOn($form['link_type'], Form::EQUAL, 'external')
                                            ->addRule(Form::FILLED, 'Please enter a link')
                                            ->addRule(Form::URL, 'Please enter a valid link');
            $items = $this->model('menuItems')->getByMenuId($this->menu->id)->fetchPairs('id_level','title');
            $items = array_merge(array('0;0' => 'none'), $items);
            $form->addSelect('parent_data', 'Parent', $items);
            $form->addButton('btnClose','Close');
            $form->addSubmit('save','Add')->onClick[] = array($this, 'formNewMenuItemSubmitted');
            $form['save']->getControlPrototype()->class('ajax');
            return $form;
	}
	
	public function formNewMenuItemSubmitted(Button $button)
	{
            $form = $button->getForm();
            $values = $form->getValues();
            //fd($values);exit;
            $parent_data = explode(';',$values['parent_data']);
            if($values['link_type'] == 'internal') $values['url'] = $values['internal_url'];
            else
            {
                if(substr($values['external_url'], 0, 6) == 'http://')
                $values['url'] = $values['external_url'];
                else  $values['url'] = 'http://'.$values['external_url'];
            }
            unset($values['internal_url']);
            unset($values['external_url']);
            $parent_id = $parent_data[0];
            $parent_level = $parent_data[1];
            unset($values['parent_data']);
            unset($values['btnClose']);
            $values['parent'] = $parent_id;
            $values['level'] = $parent_level + 1;
            $values['menu_id'] = $this->menu->id;
            try
            {
                $this->model('menuItems')->create($values);
                $this->invalidateControl('menuDragDrop');
                $this->invalidateControl('frmNewItem');
                $this->validateControl('formNewMenuItem');
                $this->flash('Item '.$values['title'].' created');
                //if(!$this->isAjax()) $this->redirect('this');
            }
            catch (DibiDriverException $e)
            {
                $this->flash($e->getMessage());

            }
            //$this->redirect('this');
	}
	
	public function createComponentMenuDragDrop()
	{
            $dragdrop = new MenuDragDrop();
            $dragdrop->setModel(new Admin_MenuItemsModel());
            $dragdrop->setTranslator(new Translator($this->lang));
            $dragdrop->onItemMove[] = array($this, 'handleItemMove');
            $dragdrop->onItemDelete[] = array($this, 'handleItemDelete');
            return $dragdrop;
	}
}
?>
Back to Top