EventTemplateManager.php 3.27 KB
<?php
namespace MyMailer;

class EventTemplateManager {

    public function prepareTemplate( $data){

        $template = $data->emailTemplates;

        $UTMParser = new \UTMParser();

        $template->text =  $UTMParser->parse($data->toArray(), $template->toArray());

        return $template;

    }


    public function insertItemData($post){
        if(isset($post['item_data']) && !empty($post['item_data'])){
            return json_decode($post['item_data']);
        } else {
            return array();
        }
    }


    public function insertEventData($event_name,$host,$projects_id,$type){

        $model = new \eventEmail();
        $data = $model->findFirst("name = '{$event_name}' AND email_type = '{$type}' AND project_id = {$projects_id} ");
        if($data instanceof \eventEmail){
            return $data;
        } else {
            throw new \Exception("EventData for event {$event_name} in project {$host} not found");
        }
    }

    /**
     *
     * Подготовка данных к отправке.
     * $item_data array()
     * $template object emailTemplates (this is model)
     * $post_data array() data from post
     * return obj
     ***/
    public function prepareEventData($item_data, \emailTemplates $template, $post_data){
        if($template->text_type =='static' ){


            if( $item_data ){
                $template->text  = $this->itemSet( $template, $item_data );
            }
            $template->text  = $this->dataSet( $template->text, $post_data );




        } else {

            if( $item_data ) {
                $template->text = $this->itemDynamicSet($template, $item_data);
            }
            $template->text = $this->dataSet( $template->text, $post_data );

        }
        return $template;
    }

    public function itemSet($template, $data){
        $num = count($data);

        $new_text = $template->text;

        for($i=0; $i<$num; $i++){
            foreach($data[$i] as $k => $v){

                $target = '{{item_'.$i.'_'.$k.'}}';
                $replacement = $v;
                $new_text = $this->replaceData($target, $replacement, $template->text);

            }
        }

        return $new_text;

    }


    public function dataSet($text, $data){

        foreach($data as $k => $v){

            $target = '{{'.$k.'}}';
            $replacement = $v;
            $text = $this->replaceData($target, $replacement, $text);

        }


        return $text;

    }

    public function replaceData($target, $replacement, $str)
    {
        $text = str_replace($target, $replacement, $str);
        return $text;
    }

    public function itemDynamicSet($template, $data){
        $num = count($data);
        $contentText = '';
        for($i=0; $i<$num; $i++){
            $contentTextOneBlock = $template->dynamic_content;

            foreach($data[$i] as $k => $v){

                $target = '{{'.$k.'}}';
                print "target = '{{'.$k.'}}'" ;
                $replacement = $v;
                print "replacement = $v" ;
                $contentTextOneBlock = $this->replaceData($target, $replacement, $contentTextOneBlock);

            }
            $contentText .= $contentTextOneBlock;
        }

        $new_text = $template->header. $contentText .$template->footer;


        return $new_text;

    }
}