<?php
require_once(__DIR__."/../apl_config.php");
require_once(__DIR__."/../apl_ver.php");
require_once(__DIR__."/../apl_settings.php");
require_once(__DIR__."/login_check.php");


$page_title="View Callbacks";
$page_message="View existing license verification callbacks. If any callback needs to be deleted, check the box near client or license code and click the 'Submit' button.";
$page_message_class="alert alert-info";
$page_header_file_no_data="callbacks_view.php";


$action_success=0; //will be changed to 1 later only if everything OK
$error_detected=0; //will be changed to 1 later if error occurs
$error_details=null; //will be filled with errors (if any)
$added_records=0;
$updated_records=0;
$removed_records=0;


if (!empty($_GET) && is_array($_GET) && array_walk($_GET, "sanitizeSubmittedData", array("script_filename"=>$script_filename, "html_fields"=>$FORM_FIELDS_WITH_TAGS))) //sanitize super variable with all GET values
    {
    extract($_GET, EXTR_SKIP); //extract sanitized data (don't overwrite existing variables)
    }


if (!empty($_POST) && is_array($_POST) && array_walk($_POST, "sanitizeSubmittedData", array("script_filename"=>$script_filename, "html_fields"=>$FORM_FIELDS_WITH_TAGS))) //sanitize super variable with all POST values
    {
    extract($_POST, EXTR_SKIP); //extract sanitized data (don't overwrite existing variables)
    }


if (isset($submit_ok))
    {
    if (!empty($callback_ids_array) && is_array($callback_ids_array))
        {
        foreach ($callback_ids_array as $callback_id)
            {
            if (filter_var($callback_id, FILTER_VALIDATE_INT))
                {
                $removed_records+=deleteRow("DELETE FROM apl_callbacks WHERE callback_id=?", array($callback_id), array("i"));
                }
            }

        if ($removed_records<1) //no records affected
            {
            $error_detected=1;
            $error_details.="Invalid record or database error.<br>";
            }
        else
            {
            $action_success=1;
            }
        }
    else
        {
        $error_detected=1;
        $error_details.="No record selected.<br>";
        }

    if ($action_success==1) //everything OK
        {
        $page_message="Deleted $removed_records callback(s) from the database.";
        createReport(strip_tags($page_message), $logged_admin_id, 1, $error_detected);
        $page_message_class="alert alert-success";
        }
    else //display error message
        {
        $page_message="The database could not be updated because of this error: <br><br>$error_details";
        $page_message_class="alert alert-danger";
        }
    }


if (empty($product_id)) //no product specified, display a list of products
    {
    $products_array=returnProductsArray();
    }
else //product specified, display callbacks from this product
    {
    if (!filter_var($product_id, FILTER_VALIDATE_INT) || empty($rows_array=fetchRow("SELECT * FROM apl_products WHERE product_id=?", array($product_id), array("i")))) //invalid record
        {
        header("Location: $page_header_file_no_data");
        exit();
        }

    $callbacks_array=returnCallbacksArray($product_id);
    }


//Twig templating starts
if (!isset($script_filename)) {$script_filename=basename($_SERVER['SCRIPT_FILENAME']);} //if $script_filename is not set yet (usually set in login_check.php), get it now (will be used in Twig forms)

Twig_Autoloader::register();
$loader=new Twig_Loader_Filesystem("../apl_templates"); //load files from templates directory
$twig=new Twig_Environment($loader); //create Twig environment

$twig->addExtension(new \nochso\HtmlCompressTwig\Extension());

echo $twig->render(basename(__DIR__)."/".basename(__FILE__, ".php").".twig", get_defined_vars()); //render requested template
//Twig templating ends