<?php


namespace count_form\app\data;

class form
{
    function index(){
        $model = new \count_form\app\db\Entity();

        $entity = $model->getInstance('question');

        $sub_question = $model->getInstance('sub_question');



        $sub_data = $sub_question->getAllData();

        $data = $entity->getAllActiveData();

        $result = array('data'=>$data, 'sub_data'=>$sub_data);

        return $result;
    }


    function select()
    {
        if(isset($_POST)&&!empty($_POST)){
            $model = new \count_form\app\db\Entity();
            $email = $_POST['email'];
            if(!empty($email)){
                $entity = $model->getInstance('question');
                $exceptions = $this->getExceptions();
                $sub_question = $model->getInstance('sub_question');

                $data = $this->getData($_POST,$entity, $sub_question);

                $text = $this->getText($data);

                if(!in_array($email, $exceptions)) {
                    $this->saveData($model, $email, $text);
                    $this->sendData($email, $text);
                }
                session_start();
                $_SESSION['success'] = 'callback';

                 print "<script>parent.location.href='$home_way/success/'</script>";
            }

        }else{
            header("Location:/count_form.php/?p=form");
        }

    }

    function findSubQuestion($question, $sub_array)
    {
        foreach($sub_array as $row){
            if($row['question_id'] == $question['id']){
                return $row;
            }
        }
        return false;
    }

    function getData($_POST,$entity, $sub_question)
    {
        $post = $_POST['role'];
        $answers = $_POST['question'];
        $comment = htmlspecialchars($_POST['comment']);
        $email = htmlspecialchars($_POST['email']);



        $questionId = array();

        foreach($post as $row) {

            foreach($row as $key => $val ){

                $questionId[] = $val;

            }
        }

        $sub_quest_id = array();
        $sub_quest_data = array();
        foreach($answers as $quest => $val){

            $val = preg_replace('/\s+/', '', $val);

            if(!empty($val)){
                $sub_quest_id[] =  $quest;
                $sub_quest_data[$quest]['answer'] = $val;
            }
        }


        $questionId = implode(",", $questionId);

        $data = $entity->getSelectData($questionId);

        if(!empty($sub_quest_id)){
            $sub_quest = implode(",", $sub_quest_id);
            $sub_data = $sub_question->getSelectData($sub_quest);

            foreach($sub_data as $quest){
                $sub_quest_data[$quest['question_id']]['text'] = $quest['text'];
            }
        }


        $result = array('data'=>$data, 'sub_data'=>$sub_quest_data, 'comment'=>$comment);

        return $result;

    }

    function sendData($email, $text)
    {
        require_once "count_form/app/lib/PHPMailer_5.2.4/class.phpmailer.php";
        $mail = new \PHPMailer;

        $mail->IsSMTP();

        $mail->CharSet = 'UTF-8';

        $mail->Username = "seodevartweb@gmail.com";
        $mail->Password = "respublika2014";
        $mail->SetFrom($email, 'ArtWeb');
        $mail->Subject = '[AtrWeb] Заявка рассчет стоимости для '.$email;
        $mail->MsgHTML($text);
        $address = "seodevartweb@gmail.com";
        $mail->AddAddress($address);
        $mail->AddAddress('slava.up@gmail.com');
        $mail->AddAddress('m@artweb.ua');
        $mail->AddAddress('seo@artweb.ua');

        $mail->Send();


    }

    function saveData($model, $email, $text)
    {

        $entity = $model->getInstance('request');

        $entity->addData($email, $text);
        $this->sendRequestToSEO($email, $text);


    }

    function sendRequestToSEO($email, $text){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'http://backend.seo.dev.artwebua.com.ua/set_message');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, "email=$email&text=$text&type=calculate");
        $out = curl_exec($curl);
        // echo $out;
        curl_close($curl);
    }

    function getText($data){
        $filename = "count_form/app/view/form/select.php";


            if (is_file($filename)) {
                ob_start();
                include $filename;
                $text = ob_get_contents();
                ob_get_clean();
                return $text;

            }
            return false;

    }

    function selectChosenData(){

        if(isset($_POST['parent_id'])&& !empty($_POST['parent_id'])){
            $parent_id = (int)$_POST['parent_id'];

            $model = new \count_form\app\db\Entity();

            $entity = $model->getInstance('question');

            $sub_question = $model->getInstance('sub_question');



            $sub_data = $sub_question->getAllData();

            $data = $entity->getChosenData($parent_id);
		header('Content-type: text/html; charset=utf-8');
            foreach($data as $array){?>

            <ul class="sub-elements">
                <li>
                <label class="label_<?=$array['input_type']?>" for="input_<?=$array['id']?>" ><input id="input_<?=$array['id']?>"  type="<?=$array['input_type']?>" class= "form-input"  <?= $array['required']?'checked':'' ?> data-parent = "<?=$array['id']?>"  name="role[<?=$array['parent_id']?>][]" value="<?=$array['id']?>"> <?=$array['text']?></label>
                <?php  if($sub_question = $this->findSubQuestion($array,$sub_data)){?>
                <div  class="additional-question" data-key="<?= $sub_question['key_num'] ? $sub_question['key_num']:'' ?>" data-status="none">
                    <p><?= $sub_question['text']?></p>
                    <?php if($sub_question['input']=='textarea'):?>
                        <textarea class= "<?= $sub_question['required']?'required':'' ?>" id="question<?=$array['id']?>" name="question[<?=$array['id']?>]" > </textarea><?= $sub_question['required']?'<label class="required-label" for="question['.$array['id'].']">поле обязательно для заполнения</label>':'' ?>
                    <?php elseif($sub_question['input']=='text'):?>
                        <input type="text" id="question<?=$array['id']?>" class = "<?= $sub_question['required']?'required':'' ?>" value="" name="question[<?=$array['id']?>]"><?= $sub_question['required']?'<label class="required-label" for="question['.$array['id'].']">поле обязательно для заполнения</label>':'' ?>
                        </div>
                    <?php
                    endif;
                }
            echo  "</li> </ul>";
        }
        die();
        }
    }

    function getExceptions(){
        return array('boss@cialisonline-20mg.com',
            'reckonzr@hotmail.com',
            'past@genericcialis-usa.net',
            'hosannaas@hotmail.com',
            'systemaist@yandex.com',
            'frostydemi@hotmail.com',
            'saloret@mail.com',
            'larisadiz@mail.ru',
            'anthonyvahp@yandex.ru',
            'nekitt9@daufond.ru',
            'robertote@yandex.ru',
            'saloret@mail.com',
            'matthewtex@yandex.ru',
            'williamlype@yandex.ru',
            'qzfcdtnabo@roixog.com',
            'qzfcdtnabo@roixog.com',
            'scheiern175alecia@hotmail.com',
            'otitismit@hotmail.com',
            'blitheover@yahoo.com',
            'schmeichel499donelle@hotmail.com',
            'daryamof@mail.ru',
            'voxxo@mail.com',
            'anneguiguibin@aol.com',
            'woodhull240leona@hotmail.com',
            'kirstybodiamlla@aol.com',
            'josephol@yandex.ru',
            'michaelsrp@yandex.ru',
            'mellish095sonia@hotmail.com',
            'katyasern@mail.ru',
            'komperdell@wegas.ru',
            'robertmrt@yandex.ru',
            'lenniecackettumu@aol.com',
            'jeffreyrom@yandex.ru',
            'eugenefup@yandex.ru',
            'normanusex@yandex.ru',
            'aprilosborneat@aol.com',
            'subnetmab@hotmail.com',
            'stevenvome@yandex.ru',
            'colorulya@hotmail.com',
            'vtoroy@tattvavit.com',
            '123abc.hui@gmail.com',
            'charlesboxrt@yandex.ru',
            'tretiy@tattvavit.com',
            'duncedfeilg@hotmail.com',
            'ronaldorr@yandex.ru',
            'ranmercyei@hotmail.com',
            'chenjinhuayi@yandex.com',
            'emanuelei@yandex.ru',
            'miltonsog@yandex.ru',
            'charlesverae@yandex.ru',
            'ravispox@yandex.ru',
            'pi2erre24g7y@yandex.ru',
            'klarakappcou@aol.com',
            'kennethot@yandex.ru',
            'manuelknot@yandex.ru',
            'amenitybv@hotmail.com',
            'samuelot@yandex.ru',
            'thomascorn@yandex.ru',
            'uhhpoxa361@yandex.ru',
            'klemen148barclay@hotmail.com',
            'avans149olimpia@hotmail.com',
            'campione165margit@hotmail.com',
            'crebs959myesha@hotmail.com',
            'campione165margit@hotmail.com',
            'sarahem@mail.ru',
            'athirstour@hotmail.com',
            'andrewsif47@yandex.ru',
            'francissiper@yandex.ru',
            'normantutt@yandex.ru',
            'novosti-bitcoin@ro.ru',
            'albertenshtein77@yandex.ru',
            'durley848demetria@hotmail.com',
            'marvinsr@yandex.ru',
            'justinbom@yandex.ru',
            'susbnetmab@gmail.com',
            'douglasped@yandex.ru',
            'aer1os1oly@gmail.com',
            'jeremyseri@yandex.ru',
            'adessa016bertie@hotmail.com',
            'hectorgype@yandex.ru',
            'norbertma@yandex.ru',
            'siegers244sherilyn@hotmail.com',
            'williamfah@yandex.ru',
            'kennethlit@yandex.ru',
            'fernandobromes@gmail.com',
            'aerosoly@hotmail.com',
            'darrelmlt@yandex.ru',
            'bartonpier@yandex.ru',
            'danielpi@yandex.ru',
            'ronaldsalyu@yandex.ru',
            'williamptn@yandex.ru',
            'alfonsozowern@yandex.ru',
            'samuelceser@yandex.ru',
            'foucraftyau@hotmail.com',
            'antoniocicyer@yandex.ru',
            'josephmup@yandex.ru',
            'mike_omike@aol.com',
            'pehowic655judith@hotmail.com',
            'michaelrive@mail.ru',
            'robertkirl@yandex.ru',
            'matzen436katey@hotmail.com',
            'josephjulk@mail.ru',
            'hyesos-artem@teem-k.com',
            'richardfar@yandex.ru',
            'fill659janey@hotmail.com',
            'charlesmole@yandex.ru',
            'mashaklr@mail.ru',
            'renattosutti@gmail.com',
            'reheblis@gmail.com',
            'stephensr@yandex.ru',
            'michaelkek@yandex.ru',
            'gormally129dre@hotmail.com',
            'edwardmrt@yandex.ru',
            'michaelhelo@yandex.ru',
            'thomasmubs@yandex.ru',
            'claytoncand@yandex.ru',
            'curableinfra@hotmail.com',
            'samuelscot@yandex.ru',
            'fa1212fn@rostov-perimetr.ru',
            'phpfoxblognine@163.com',
            'lubricatejust@hotmail.com',
            'tik@cialisonline-20mg.com',
            'andrewhalk@yandex.ru',
            'luker106ema@hotmail.com',
            'galyakaf@mail.ru',
            'whyman864rozella@hotmail.com',
            'burgun295ngan@hotmail.com',
            'ryoteensseding1980@yandex.ru',
            'forumlurennine@163.com',
            'bonnievam@yandex.ru',
            'imagistzixfgp@hotmail.com',
            'cantorrupt@hotmail.com',
            'en@genericcialisusa.net',
            'veronikamaster@outlook.com',
            'mccallister444russ@hotmail.com',
            'renschler744nancey@hotmail.com',
            'fedoreytrewq@yandex.ru',
            'forumlrthree@163.com',
            'daggs044alyse@hotmail.com',
            'czar553filomena@hotmail.com',
            'ingwell033carolin@hotmail.com',
            'yeaney580kayleigh@hotmail.com',
            'jesspurlingcuc@aol.com',
            'wesch266kennith@hotmail.com',
            'colleenbaeziy@aol.com',
            'utrera607svetlana@hotmail.com',
            'marktelepuzik@yandex.ru',
            'richarddror@yandex.ru',
            'arthurmr@yandex.ru',
            'pyscher799emelda@hotmail.com',
            'singleterry385eireann@hotmail.com',
            'getz202sydney@hotmail.com',
            'georgepak@yandex.ru',
            'ackmann200corliss@hotmail.com',
            'lax@cialisonline-20mg.com',
            'user13@smmok-700nm.ru',
            'danielsip@yandex.ru',
            'edwardsag@yandex.ru',
            'gazzara587lacresha@hotmail.com',
            'carland661shelby@hotmail.com',
            'menucci584pa@hotmail.com',
            'gertruda@mail.com',
            'nitzkowski740barney@hotmail.com',
            'shorthandess@hotmail.com',
            'admixscope@hotmail.com',
            'cunliffe271taneka@hotmail.com',
            'nightetktk@outlook.com',
            'julialic@mail.ru',
            'nighte2tktk@mail.ru',
            'stevencich@yandex.ru',
            'kapitosha@wegas.ru',
            'pigmymubnpqke@hotmail.com',
            'vkk1@yourmoneybinary.com',
            'bandytzbxus@hotmail.com',
            'fpoej4374@yandex.ru',
            'lofty311jorja@hotmail.com',
            'krytha@wbskcw.com',
            'john@hotmail.com',
            'anthonymrk@yandex.ru',
            'lorenzokn@yandex.ru',
            'danielnawn@yandex.ru',
            'brandennity@yandex.ru',
            'patrickvus@yandex.ru',
            'michaelhopew@yandex.ru',
            'hututu0324@163.com',
            'tyhthyjhhhh@mixbox.pl',
            'nightetktk@hotmail.com',
            'georgeole@yandex.ru');
    }


}
