PageController.php 2.92 KB
<?php
/**
 * Created by PhpStorm.
 * User: Vitaliy
 * Date: 12.06.14
 * Time: 12:02
 */

namespace controllers;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class PageController extends \Phalcon\Mvc\Controller
{
    function indexAction()
    {
       $data = \users::findFirst("id = '10'");
        $test = \userToServices::findFirst("user_id = '10'");
        $this->view->setVars([
            'data' => $data,
            'test' => $test
        ]);
    }

    function loginAction()
    {
        if($this->request->getPost()) {
            $email          = $this->request->getPost('email', 'email');
            $password       = $this->request->getPost('password', 'string');
            $password       = $this->common->hashPasswd( $password );
            $model = \users::findFirst(array(
                "email = '$email'",
                "password => '$password'",
            ));
            if($model instanceof \users) {
                $this->session->set("user-name", $model->name);
                $this->session->set("user-status", $model->status);
                return $this->response->redirect('index_page');
            } else {
                echo "Пользователя с такими данными не существует";
            }
        }


        $this->view->setVars([

        ]);

    }

    function logoutAction()
    {
        $this->session->destroy();
        return $this->response->redirect('login_page');
    }

    public function downloadImagesAction()
    {

        if ($this->request->hasFiles() == true) {

            $data['directory'] = $this->request->getPost('directory' );

            foreach ($this->request->getUploadedFiles() as $file){

                $allowed_filetypes = array('.jpg','.JPG', '.png', '.PNG', '.gif', '.GIF');

                $ext = substr($file->getName() ,strpos($file->getName() ,'.'),strlen($file->getName() )-1);

                if(!$data['directory']) {
                    $data['directory'] = md5(microtime());
                }


                if(in_array($ext,$allowed_filetypes))
                {
                    $image_path = $this->storage->getEmailTemplatePath( 'temp', $data['directory']);

                    if(!file_exists($image_path))
                    {
                        mkdir( $image_path, 0777, true );
                    }
                    $file->moveTo($image_path.$file->getName());
                    $data['message'] = 'Загрузка файла '.$file->getName().' выполнена успешно.';
                } else {
                    $data['message'] = 'Произошла ошибка. Не верный формат файла.';
                }
                $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);


                echo json_encode($data);


            }
        }
    }


}