StandardEmailController.php 10.8 KB
<?php

namespace controllers;

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

class StandardEmailController extends \Phalcon\Mvc\Controller
{
    public function initialize()
    {
        $this->view->setTemplateAfter('common');
    }


    function indexAction()
    {

        $params         = $this->dispatcher->getParams();
        $currentPage    = !empty( $params['page']  ) ? $params['page'] : 1;

        // Набор данных для разбивки на страницы

        $data = \eventEmail::query()
            ->where("email_type = 'standard'")
            ->andWhere("project_id = {$this->session->get('project-id')}")
            ->execute();
        // Создаём пагинатор, отображаются 10 элементов на странице, начиная с текущей - $currentPage
       /* $paginator = new \Phalcon\Paginator\Adapter\Model(
            array(
                "data" => $data,
                "limit"=> 10,
                "page" => $currentPage
            )
        );

        // Получение результатов работы ппагинатора

        $page = $paginator->getPaginate();*/

        $this->view->setVars([
            'data' => $data
        ]);
    }

    function deleteAction($id)
    {
        $model = \eventEmail::query()
        ->where("id = $id")
        ->andWhere("project_id = {$this->session->get('project-id')}")
        ->execute();
        if($model->toArray()){
            $model->delete();
            $this->flash->error( 'Данные успешно удалены' );
            return $this->response->redirect([ 'for' => 'standard_email_index' ]);
        } else {
            $this->flash->error( 'Произошла ошибка при попытке удаления' );
            return $this->response->redirect([ 'for' => 'standard_email_index' ]);
        }
    }

    function updateAction($id)
    {

        $model      = new \eventEmail();
        $temp_model = new \emailTemplates();

        if( $this->request->isPost() )
        {
            $data['id']          = $this->request->getPost('id', 'int', NULL );
            $data['send']        = $this->request->getPost('send');
            $data['name']        = $this->request->getPost('name', 'string', NULL );
            $data['utm_status']  = $this->request->getPost('utm_status', 'int', NULL );
            $data['utm_campaign']= $this->request->getPost('utm_campaign', 'string', NULL );
            $data['utm_medium']  = $this->request->getPost('utm_medium', 'string', NULL );
            $data['utm_source']  = $this->request->getPost('utm_source', 'string', NULL );
            $data['utm_term']    = $this->request->getPost('utm_term', 'string', NULL );
            $data['utm_content'] = $this->request->getPost('utm_content', 'string', NULL );
            $data['status']      = $this->request->getPost('status', 'string', NULL );
            $data['template_id'] = $template_id  = $this->request->getPost('template_id', 'int', NULL );
            $data['users_id']    = $this->request->getPost('project_id');
            $data['email_type']        = 'standard';
            $data['utm_status']  = empty( $data['utm_status'] ) ? 0 : 1;
            $data['status']      = empty( $data['status'] ) ? 0 : 1;

            $template['project_id']        = $this->session->get('project-id');
            $template['title']          = $this->request->getPost('template_title', 'string', NULL );
            $template['text']           = $this->request->getPost('template_text');
            $template['directory']      = $this->request->getPost('directory', 'string', NULL );
            $template['name']           = $this->request->getPost('template_name', 'string', NULL );

            $parse['picture_url'] = $this->storage->getEmailTemplateUrl( 'temp', $template['directory']);
            $template['text'] = str_replace('%!picture_url!%', $parse['picture_url'], $template['text']);


            $eventModel = $model::findFirst("id = $id AND project_id = {$this->session->get('project-id')}");

            if( !empty( $data['name'] ) && !empty( $data['name'] ) )
            {
                if( $eventModel->save( $data ) )
                {


                    $template['event_id'] = $id;



                    $templModel = $temp_model::findFirst("id = $template_id AND project_id = {$this->session->get('project-id')}");
                    $templModel->save( $template );


                    if(isset($data['send']) && !empty($data['send'])){
                        $model = new \delivery();
                        if($data['utm_status'] && !empty($data['utm_campaign']) && !empty($data['utm_medium']) && !empty($data['utm_source'])) {

                            $template['text'] = $this->UTMParser->parse($data, $template);

                        }
                        if(isset($data['users_id']) && !empty($data['users_id'])) {
                          $this->MyMailer->Delivery->SendDelivery($model,$template,$data);
                        } else {
                            $this->MyMailer->Delivery->SendDeliveryForAll($model,$template,$data);
                        }
                    } else {
                        $this->flash->success( 'Сохранение прошло успешно' );
                    }
                    return $this->response->redirect([ 'for' => 'standard_email_index' ]);
                }
                else
                {
                    $this->flash->error( 'Произошла ошибка во время добавления.' );
                }
            }

        }


        $data      =  $model::findFirst("id = $id AND project_id = {$this->session->get('project-id')}");
        $templates =  $temp_model::find("event_id = 0 AND project_id = {$this->session->get('project-id')}");
        $temp      =  $temp_model::findFirst("id = $data->template_id AND project_id = {$this->session->get('project-id')}");
        $this->view->pick( 'standard_email/addEdit' );

        $this->view->setVars([
            'page' => $data,
            'templates' => $templates,
            'temp' => $temp
        ]);

    }

    function addAction()
    {
        $model = new \eventEmail();
        $temp_model = new \emailTemplates();

        if( $this->request->isPost() )
        {
            $data['project_id']     = $this->session->get('project-id');
            $data['send']        = $this->request->getPost('send');
            $data['name']        = $this->request->getPost('name', 'string', NULL );
            $data['utm_status']  = $this->request->getPost('utm_status', 'int', NULL );
            $data['utm_campaign']= $this->request->getPost('utm_campaign', 'string', NULL );
            $data['utm_medium']  = $this->request->getPost('utm_medium', 'string', NULL );
            $data['utm_term']    = $this->request->getPost('utm_term', 'string', NULL );
            $data['utm_content'] = $this->request->getPost('utm_content', 'string', NULL );
            $data['utm_source']  = $this->request->getPost('utm_source', 'string', NULL );
            $data['users_id']    = $this->request->getPost('project_id');
            $data['status']      = $this->request->getPost('status', 'string', NULL );
            $data['email_type']        = 'standard';

            $data['utm_status']  = empty( $data['utm_status'] ) ? 0 : 1;
            $data['status']      = empty( $data['status'] ) ? 0 : 1;

            $template['project_id']        = $this->session->get('project-id');
            $template['title']          = $this->request->getPost('template_title', 'string', NULL );
            $template['text']           = $this->request->getPost('template_text');
            $template['directory']      = $this->request->getPost('directory', 'string', NULL );
            $template['name']           = $this->request->getPost('template_name', 'string', NULL );

            $parse['picture_url'] = $this->storage->getEmailTemplateUrl( 'temp', $template['directory']);
            $template['text']     = str_replace('%!picture_url!%', $parse['picture_url'], $template['text']);

            if( !empty( $data['name'] ) && !empty( $data['name'] ) )
            {
                if( $model->save( $data ))
                {
                    $data['id'] = $model->id;

                    if($data['utm_status'] && !empty($data['utm_campaign']) && !empty($data['utm_medium']) && !empty($data['utm_source'])) {

                        $template['text'] = $this->UTMParser->parse($data, $template);

                    }

                    $template['event_id'] = $model->id;

                    $temp_model->save( $template );
                    $template_id       = $temp_model->id;

                    $eventModel = $model::findFirst("id = {$template['event_id']} AND project_id = {$this->session->get('project-id')}");

                    $eventModel->save(array('template_id'=>$template_id));

                    if(isset($data['send']) && !empty($data['send'])){
                        $delivery_model = new \delivery();
                        if($data['utm_status'] && !empty($data['utm_campaign']) && !empty($data['utm_medium']) && !empty($data['utm_source'])) {

                            $template['text'] = $this->UTMParser->parse($data, $template);

                        }

                        if(isset($data['users_id']) && !empty($data['users_id'])) {
                            $this->MyMailer->Delivery->SendDelivery($delivery_model,$template,$data);

                        } else {
                            $this->MyMailer->Delivery->SendDeliveryForAll($delivery_model,$template,$data);
                        }
                    } else {
                        $this->flash->success( 'Сохранение прошло успешно' );
                    }
                    return $this->response->redirect([ 'for' => 'standard_email_index' ]);
                }
                else
                {
                    $this->flash->error( 'Произошла ошибка во время добавления.' );
                }
            }
        }


        $templates =   $temp_model::find("event_id = 0 AND project_id = {$this->session->get('project-id')}");


        $this->view->pick( 'standard_email/addEdit' );

        $this->view->setVars([
            'templates' => $templates,
        ]);

    }

    public function getUsersLikeAction()
    {
        $this->view->setTemplateAfter('none');
        $like = $this->request->getPost('like', 'string', NULL );
        $users = \customersEmailList::find(array(
            "email LIKE '%$like%' AND project_id = {$this->session->get('project-id')}"
        ));
        $result = json_encode($users->toArray());
        $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
        echo $result;
    }



}