EventAdder.php 4.26 KB
<?php
namespace MyMailer;

class EventAdder{

    public $post;
    public $host;

    public function __construct( $post){

        $project_model = new \projects();

        $this->host = $_SERVER['HTTP_ORIGIN'];

        $this->post = $post;

        $project = $project_model->getProjectId($this->host);

        $this->post['project_id'] = $project->id;

        $this->post['project_name'] = $project->name;

        if(!$this->post['project_id']){
            throw new \Exception("Project {$this->host} not found");
        } else {
            print_r($this->post);
        }

    }

    public function callEventFactory(){

        if(isset($this->post['event_type'])){
            switch ($this->post['event_type']) {
                case 'add_subscribe':


                    $event = new EventOnlineSubscribe();



                    $customer = $event->subscribeNewCustomer($this->post);

                    $event->addLatterListToNewCustomer($this->post,$customer);

                    $templateManager = new EventTemplateManager();

                    $data = $templateManager->insertEventData($this->post['event'],$this->host,$this->post['project_id'],'event_online');

                    $item_data = $templateManager->insertItemData($this->post);

                    $template = $templateManager->prepareTemplate($data);

                    $template = $templateManager->prepareEventData($item_data, $template, $this->post);

                    $MyMailer = new EventMailer();

                    $MyMailer->setFromName($this->post['project_name']);

                    $event->sendEventDelivery($this->post, $data, $template);

                    break;
                case 'online_event':

                    $templateManager = new EventTemplateManager();

                    $data = $templateManager->insertEventData($this->post['event'],$this->host,$this->post['project_id'],'event_online');

                    $item_data = $templateManager->insertItemData($this->post);

                    $template = $templateManager->prepareTemplate($item_data,$data);

                    $template = $templateManager->prepareEventData([], $template, $this->post);

                    $event = new EventOnline();

                    $event->sendEventDelivery( $this->post, $data, $template);

                    break;

                case 'spy_event':

                    $spyEvent = new EventSpy();

                    $customer = $spyEvent->getCustomer($this->post);


                    switch ($this->post['action']) {
                        case 'order_add':

                            $spyID =  $spyEvent->saveSpyData($this->post,$customer);
                            $spyEvent->saveSpyOrder($spyID, 'added',$this->post);

                            break;

                        case 'order_delete':
                            $spyID =  $spyEvent->getSpyData($this->post,$customer);
                            $spyEvent->saveSpyOrder($spyID, 'deleted',$this->post);

                            break;
                        case 'order_finish':


                            $model = new \spyEvent();

                            $item_data = $model->spyTimeAfter($this->post['project_id'], $customer->id)->toArray();

                            $templateManager = new EventTemplateManager();

                            $data = $templateManager->insertEventData($this->post['event'],$this->host,$this->post['project_id'],'event_online');

                            $template = $templateManager->prepareTemplate($data);

                            $template = $templateManager->prepareEventData($item_data, $template, $this->post);

                            $spyEvent->sendEventDelivery( $this->post, $data, $template);

                            $spyID =  $spyEvent->saveSpyData($this->post,$customer);
                            $spyEvent->finishSpyOrder($this->post,$customer);

                            break;

                        default:
                            throw new \Exception("Unknown event action {$this->post['action']}");
                    }

                    break;
                default:
                    throw new \Exception("Unknown event type");
            }
        } else {
            throw new \Exception("Unknown event type");
        }
    }
}