View Single Post
Old 12-18-2008, 10:09 AM   #1 (permalink)
triumvirat
The Wanderer
 
triumvirat's Avatar
 
Join Date: Dec 2008
Location: Russia, Moscow
Posts: 14
Thanks: 0
triumvirat is on a distinguished road
Default MVC and big methods

to criticize please my controller. This class is controller of MVC model and I don't know, how true this code. Me bemuse the big methods...

sorry my english :Р

PHP Code:
<?php
class Backend_Group_Controller extends Backend_Controller
{
    private 
$group;
    private 
$group_mapper;

    public function 
__construct($controller_name)
    {
        
parent::__construct($controller_name);

        
$this->actview = array('main''edit');
    }

    
// Основной метод, запускается при любом action
    
public function run($action)
    {
        
$this->group_mapper = new Group_Mapper();

        if (isset(
$this->request->id))
        {
            if (!
is_numeric($this->request->id))
            {
                
$redirect = new Redirect();
                
$redirect->setType('alert');
                
$redirect->setMessage('Указан неверный идентификатор группы.');
                
$redirect->setRedirectUrl('/admin/group/');
                
$redirect->run();
            }

            
$this->group $this->group_mapper->findById($this->request->id);

            if (!
$this->group->id)
            {
                
$redirect = new Redirect();
                
$redirect->setType('alert');
                
$redirect->setMessage('Группы с идентификатором <strong>'.$this->request->id.'</strong> '.
                                      
'не существует.');
                
$redirect->setRedirectUrl('/admin/group/');
                
$redirect->run();
            }
        }

        
parent::run($action);
    }

    
// Списко групп пользователей.
    
public function main()
    {
        
$params = array('order' => array('id' => 'ASC'));

        
$this->view->main($this->group_mapper->getObjectList($params));
    }

    
// Удаление группы пользователей.
    
public function delete()
    {
        if (!isset(
$this->request->id))
        {
            
$redirect = new Redirect();
            
$redirect->setType('alert');
            
$redirect->setMessage('Не указан идентификатор группы.');
            
$redirect->setRedirectUrl('/admin/group/');
            
$redirect->run();
        }

        
$this->group_mapper->delete($this->group);

        
$redirect = new Redirect();
        
$redirect->setMessage('Группа <strong>'.Format::hsc($this->group->group_name).'</strong> удалена.');
        
$redirect->setRedirectUrl('/admin/group/');
        
$redirect->run();
    }

    
// Редактирование и добавление административной группы.
    
public function edit()
    {
        
$this->view->setErrorKeys($this->group_mapper->getModelAttributes());

        if (empty(
$this->group))
        {
            
$this->group $this->group_mapper->createNew();
        }

        
// Массив для формирования чекбоксов с существующими actions.
        
$this->view->form_actions $this->view_getGroupActions();

        
// Неизменяемые данные формы.
        
$this->view->setHeader($this->group->group_name);
        
$this->view->id $this->group->id;
        
$this->group_mapper->loadUsersInGroup($this->group);
        
$this->view->setGroupUser($this->group->group_user);

        if (
POST)
        {
            
$validator = new Group_Validator('Group');
            
$validator->checkGroupName('group_name');

            
// добавление - создаем объект group на основе полученных данных
            
$this->group $this->group_mapper->createFromArray($this->request->getData());

            
// Ошибки заполнния формы
            
if ($err $validator->getErrors())
            {
                
$this->view->setErrorMessage($err);
            }
            else
            {
                
$this->group_mapper->save($this->group);

                
$redirect = new Redirect();
                
$redirect->setMessage('Данные группы <strong><a href="/admin/group/edit/?id='.$this->group->id.'">'.
                                      
Format::hsc($this->group->group_name).'</a></strong> '.
                                      
'сохранены.');
                
$redirect->setRedirectUrl('/admin/group/');
                
$redirect->run();
            }
        }

        
$this->view->group_name $this->group->group_name;
        
$this->view->group_active $this->group->group_active;
        
$this->view->group_global $this->group->group_global;
        
$this->view->group_actions $this->group->group_actions;
    }


    
/*
    * Возвращает массив для формирования checkbox-ов actions.
    */
    
private function view_getGroupActions()
    {
        
$this->db Database::getInstance();
        
$data = array();

        
$res $this->db->query('SELECT
                                       controller.id AS id_controller,
                                       controller.controller_name,
                                       controller.controller_description,
                                       action.id AS id_action,
                                       action.action_name,
                                       action.action_global
                                   FROM
                                       controller,
                                       action
                                   WHERE
                                       controller.id = action.id_controller'
);

        while (
$row $res->fetch_assoc())
        {
             if (!isset(
$data[$row['id_controller']]))
             {
                 
$data[$row['id_controller']]['name'] = $row['controller_name'];
                 
$data[$row['id_controller']]['description'] = $row['controller_description'];
                 
$data[$row['id_controller']]['actions'] = array();
             }

             
$data[$row['id_controller']]['actions'][$row['id_action']]['name'] = $row['action_name'];
             
$data[$row['id_controller']]['actions'][$row['id_action']]['global'] = $row['action_global'];
        }

        return 
$data;
    }
}
?>
triumvirat is offline  
Reply With Quote