Exemplo n.º 1
0
/**
 * Стартовая страница сервиса
 *
 * @param array &$Controller объект контроллера
 *
 * @return &array объект отображения
 */
function &indexAction(array &$Controller)
{
    $Request =& getRequest($Controller);
    $errorMsg = '';
    if (Request\isPost($Request)) {
        // Auth
        $login = Request\getPostParam($Request, 'login');
        $password = Request\getPostParam($Request, 'password');
        $loginAs = Request\getPostParam($Request, 'loginAs');
        if ($loginAs !== 'customer' && $loginAs !== 'executor' || is_null($login) || is_null($password)) {
            $errorMsg = 'Неверные данные';
        } else {
            $ServiceManager =& getServiceManager($Controller);
            if ($loginAs == 'customer') {
                // Заказчик
                $CustomerRepository =& ServiceManager\get($ServiceManager, 'CustomerRepository');
                if (!CustomerRepository\validateAuth($CustomerRepository, $login, $password)) {
                    $errorMsg = 'Неверный логин/пароль';
                } else {
                    ServiceManager\set($ServiceManager, 'ActiveUser', CustomerRepository\fetch($CustomerRepository, $login));
                    Session\setActiveUserData($login, 'customer');
                    header('Location: /customer', true, 307);
                    exit;
                }
            } else {
                // Исполнитель
                $ExecutorRepository =& ServiceManager\get($ServiceManager, 'ExecutorRepository');
                if (!ExecutorRepository\validateAuth($ExecutorRepository, $login, $password)) {
                    $errorMsg = 'Неверный логин/пароль';
                } else {
                    ServiceManager\set($ServiceManager, 'ActiveUser', ExecutorRepository\fetch($ExecutorRepository, $login));
                    Session\setActiveUserData($login, 'executor');
                    header('Location: /executor', true, 307);
                    exit;
                }
            }
        }
    } else {
        $loginAs = 'customer';
    }
    $View =& View\construct();
    View\setTemplateName($View, 'index\\index');
    View\setVariables($View, ['errorMsg' => $errorMsg, 'loginAs' => $loginAs]);
    return $View;
}
Exemplo n.º 2
0
/**
 * Запуск приложения
 *
 * @param array &$Application
 */
function run(array &$Application)
{
    $ServiceManager =& getServiceManager($Application);
    /**
     * У нас нет ни dispatch, ни forward, ни event, поэтому далее простой код
     */
    // Определим роутинг
    $Router =& Router\construct(ServiceManager\get($ServiceManager, 'Config'), ServiceManager\get($ServiceManager, 'Request'), $Application);
    Router\match($Router);
    $controllerName = Router\getControllerName($Router);
    $actionName = Router\getActionName($Router);
    $routeName = Router\getRouteName($Router);
    if (getMode($Application) === MODE_WEB) {
        if ($routeName === 'root') {
            header('Location: /index', true, 307);
            return;
        }
        $ActiveUser = null;
        $userData = Session\getActiveUserData();
        if (!is_null($userData)) {
            if ($userData[1] == 'customer') {
                // Заказчик
                if ($routeName !== 'customer' && ($routeName !== 'index' || $actionName !== 'logout')) {
                    header('Location: /customer', true, 307);
                    return;
                }
                $CustomerRepository =& CustomerRepository\construct(MemcachedFactory\create(ServiceManager\getFactory($ServiceManager, 'Memcached'), 'cache'), ServiceManager\getFactory($ServiceManager, 'Mysql'));
                $ActiveUser =& CustomerRepository\fetch($CustomerRepository, $userData[0]);
            } else {
                // Исполнитель
                if ($routeName !== 'executor' && ($routeName !== 'index' || $actionName !== 'logout')) {
                    header('Location: /executor', true, 307);
                    return;
                }
                $ExecutorRepository =& ServiceManager\get($ServiceManager, 'ExecutorRepository');
                $ActiveUser =& ExecutorRepository\fetch($ExecutorRepository, $userData[0]);
            }
            Session\regenerate();
        } else {
            if ($routeName !== 'index') {
                header('Location: /index', true, 307);
                return;
            }
        }
        ServiceManager\set($ServiceManager, 'ActiveUser', $ActiveUser);
    }
    // Создадим контроллер, вызовем action
    load('Avaritia\\Controller\\' . $controllerName);
    $controllerNamespace = 'Avaritia\\Controller\\' . $controllerName . '\\';
    // У контроллера должен быть конструктор
    if (!function_exists($controllerNamespace . 'construct')) {
        trigger_error('У контроллера [' . $controllerName . '] отсутствует конструктор', E_USER_ERROR);
    }
    $Controller =& call_user_func_array($controllerNamespace . 'construct', [&$ServiceManager]);
    $Request =& ServiceManager\get($ServiceManager, 'Request');
    // Полное имя функции экшена для вызова
    $actionFunction = $controllerNamespace . (Request\isXmlHttpRequest($Request) ? 'cmd' . ucfirst($actionName) : $actionName . 'Action');
    if (!function_exists($actionFunction)) {
        header('HTTP/1.0 404 Not Found');
        exit;
    }
    if (Request\isXmlHttpRequest($Request)) {
        // ajax запросы просто возвращают массив данных
        $View =& View\construct();
        View\setVariables($View, call_user_func_array($actionFunction, [&$Controller]));
        View\setRenderStrategy($View, View\RENDER_STRATEGY_JSON);
    } else {
        $View =& call_user_func_array($actionFunction, [&$Controller]);
    }
    ServiceManager\set($ServiceManager, 'View', $View);
    echo Response\toString(ServiceManager\get($ServiceManager, 'Response'));
}