TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 12-18-2008, 10:09 AM   #1 (permalink)
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
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:59 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design