<?php

# EDZÉS ZÁROLÁS

if ($this->is_id()) {

    # EDZÉS ZÁROLÁS/FELOLDÁS

    $locked = $sql->single_variable('select tr_locked from training where tr_id =' . $this->get_id());
    $sql->update_table('training', array(
            'tr_locked' => ($locked?0:1)
        ),
        array(
            'tr_id' => $this->get_id()
        )
    );
    log::register(($locked?'training_open':'training_close'), $this->get_id());

    $kid_ids = $sql->assoc_array('SELECT pr_user_kid_uk_id FROM presence WHERE pr_training_tr_id = ' . $this->get_id());
    foreach ($kid_ids as $k_array) {
        $kid = new user_kid();
        $kid->set_user_data_by_id($k_array['pr_user_kid_uk_id']);

        $kid->update_balance();
    }

    //SEND NOTIFICATION

    /*
    if (!$locked) {
        //get kids from training
        $kid_ids = $sql->assoc_array('SELECT pr_user_kid_uk_id FROM presence WHERE pr_training_tr_id = ' . $this->get_id());

        //iterate and collect them
        $kid_array = array();
        foreach ($kid_ids as $k_array) {
            $kid = new user_kid();
            $kid->set_user_data_by_id($k_array['pr_user_kid_uk_id']);

            $kid->update_balance();

            if (null === $kid->get_uk_last_notification() && null !== $kid->get_uk_notify_email() && $kid->get_uk_balance() < 0) {
                $kid_array[] = $kid;
            }
            elseif (null !== $kid->get_uk_last_notification() && null !== $kid->get_uk_notify_email() && $kid->get_uk_balance() < 0) {
                $notify_hours = $sql->single_variable(' SELECT
                                                            setv_int
                                                        FROM
                                                            setting_value
                                                                JOIN
                                                            setting ON setv_setting_set_id = set_id
                                                        WHERE
                                                            set_name = "Értesítések közt eltelt idő";
                                                        ;');
                if (null !== $notify_hours) {
                    $timestamp = strtotime($kid->get_uk_last_notification()) + $notify_hours*60;
                    $expireDate = date('Y-m-d H:i:s', $timestamp);

                    if (time() >= $timestamp) {
                        $kid_array[] = $kid;
                    }
                }

            }


        }

        //send email
        $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($kid_array);
            foreach ($kid_array as $kid) {
                $personalizedSubject = $emailTemplate->personalize($raw_subject, array(
                    'uk_name' => $kid->get_uk_name(),
                ));

                $personalizedMessage = $emailTemplate->personalize($raw_message, array(
                    'notify_name' => $kid->get_uk_notify_name(),
                    'uk_name' => $kid->get_uk_name(),
                    'uk_balance' => $kid->get_uk_balance(),
                    'uk_password' => $kid->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->addBCC($kid->get_uk_notify_email(), $kid->get_uk_notify_name());
                    $mail->addBCC('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,
                        $kid->get_uk_notify_name(),
                        $kid->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' => $kid->get_uk_id()));

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

            }
        }
    }
    */

    header('Location: /admin/presence/' . $this->get_id());
}

?>
