<?php
//ini_set('include_path', '../_class/');

include '../_class/class_user_parent.php';
foreach (glob("../_class/*.php") as $filename)
{
    //echo $filename;
    $skip = array(
        '../_class/class_user_parent.php',
        '../_class/class_Exception.php',
    );
    if (in_array($filename, $skip)) continue;

    include $filename;
}


/*
foreach ($_POST as $key => $value) {
    trigger_error($_SERVER['HTTP_HOST'], E_USER_NOTICE);
}
*/
if ($_SERVER['HTTP_HOST'] == 'badmintoncoach.hu') $sql = new sql('bc_mysql','root','','badminton_coach');
else $sql = new sql('localhost','tollashodos','uprRscU8bGpJ','tollashodos');

if (empty($_POST['uk_id'])) {
    echo json_encode(null);
}

else {
    $toNotify = new user_kid();
    $toNotify->set_user_data_by_id($_POST['uk_id']);

    if (null === $toNotify->get_uk_notify_email() || null === $toNotify->get_uk_notify_name()) {
        echo json_encode('missing-data');
    }
    else {
            $email_template_id = $sql->single_variable('select et_id from email_template where et_name = \'below_zero\'');
            if (null !== $email_template_id) {
                $emailTemplate = new email_template();
                $emailTemplate->set_et_data_by_id($email_template_id);

                $raw_subject = $emailTemplate->get_et_subject();
                $raw_message = $emailTemplate->get_et_message();
                //var_dump($toNotify_array);
                //foreach ($toNotify_array as $toNotify) {
                $personalizedSubject = $emailTemplate->personalize($raw_subject, array(
                    'uk_name' => $toNotify->get_uk_name(),
                ));

                $personalizedMessage = $emailTemplate->personalize($raw_message, array(
                    'notify_name' => $toNotify->get_uk_notify_name(),
                    'uk_name' => $toNotify->get_uk_name(),
                    'uk_balance' => $toNotify->get_uk_balance(),
                    'uk_password' => $toNotify->get_uk_password(),
                ));

                $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
                try {
                    //Server settings
                    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
                    $mail->isSMTP();                                      // Set mailer to use SMTP
                    $mail->CharSet = PHPMailer::CHARSET_UTF8;              // UTF-8
                    $mail->Host = 'mail.gginternet.com ';  // Specify main and backup SMTP servers
                    $mail->SMTPAuth = true;                               // Enable SMTP authentication
                    $mail->Username = 'hirlevel@tollaslabda.info';                 // SMTP username
                    $mail->Password = 'tollas12';                           // SMTP password
                    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
                    $mail->Port = 465;                                    // TCP port to connect to

                    //Recipients
                    $mail->setFrom($emailTemplate->get_et_from_email(), $emailTemplate->get_et_from_name());
                    $mail->addAddress($toNotify->get_uk_notify_email(), $toNotify->get_uk_notify_name());
                    //$mail->addAddress('tricsusz@gmail.com', 'Tóth Richárd');     // TEST


                    //Content
                    $mail->isHTML(true);                                  // Set email format to HTML
                    $mail->Subject = $personalizedSubject;
                    $mail->Body    = $personalizedMessage;
                    $mail->AltBody = 'Az Ön levelezője nem támogatja a HTML tartalom megjelenítését!';

                    //send mail
                    $mail->send();
                    //LOG SUCCESS
                    email_log::create_email_log(
                        $personalizedMessage,
                        $personalizedSubject,
                        $toNotify->get_uk_notify_name(),
                        $toNotify->get_uk_notify_email(),
                        $emailTemplate->get_et_id()
                    );

                    //Update kids last noti date
                    $sql->update_table('user_kid', array('uk_last_notification' => date('Y-m-d H:i:s')), array('uk_id' => $toNotify->get_uk_id()));

                    echo json_encode('success');

                } catch (Exception $e) {
                    //LOG ERROR
                    email_log::create_email_log(
                        $personalizedMessage,
                        $personalizedSubject,
                        $toNotify->get_uk_notify_name(),
                        $toNotify->get_uk_notify_email(),
                        $emailTemplate->get_et_id(),
                        mysql_escape_string($e)
                    );

                    echo json_encode(null);
                }

                //}
            }
    }
}

?>
