<?php
$show_init_form = false;	/* To display or not the initialization form */

/* Try to see if the call is from administration panel */
if(isset($admin_catdom_module_id) && $admin_catdom_module_id>0 && isset($_GET['action']) && $_GET['action'] == "custom_module") {
    $show_init_form = true;
    // Initialize defaults
    $module_location = "null";
    $module_mode = null;

    $def_ads = "no";
    $ws_ad_unit_id = "";

    $current_catdom_module_id = $admin_catdom_module_id;
}

if($show_init_form) {
    // Try to get any previously saved customization
    if(isset($module_params) && is_array($module_params)) {
        foreach ($module_params as $key=>$value) {
            ${$key} = $value;
        }
    }


    // Cinema related

    $cities_list = ['null' => "No location"];

    $q = "SELECT cinema_location FROM `im_cinemas` WHERE 1 GROUP BY cinema_location ORDER BY cinema_location ASC";
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) > 0) {
        while($row = mysql_fetch_assoc($r)) {
            $cities_list [$row['cinema_location']] = $row['cinema_location'];
        }
    }

    $modes = [
        'null' => 'Default',
        'playing' => 'Show Now playing movies'
    ];


    // Google ads related
    // Pull in the NuSOAP code
    require_once(dirname(__FILE__) . "/../wsbin/lib/nusoap.php");
    include_once(dirname(__FILE__) . "/../common/gads_content/functions.php");

    //$webservices_url = "http://localhost/webservices/cyws/srvdispatcher.php?wsdl";
    $webservices_url = "https://www.apps24.com/srvdispatcher.php?wsdl";

    $out_path_cache = dirname(__FILE__) . "/../..";
    $all_cache_folder = "$out_path_cache/all_cache/" . $app_conf["addonname"];
    $all_cache_folder = str_ireplace("//", "/", $all_cache_folder);
    $all_cache_normal_gads_content = "$all_cache_folder/gads_content";

    if(isset($module_params['ws_ad_unit_id']) && $module_params['ws_ad_unit_id'] >0) {
        $ad_unit_id = $module_params['ws_ad_unit_id'];
    }
    $ws_request_available_ad_units = array(
        'server_name' 		=> 'gads_content',
        'function_name'		=> 'getAdSenseContentUnits',
        'function_params'	=> ''
    );

    /* Check if cache folders are in place ******************************************************/
    if(!file_exists("$all_cache_folder") || !is_dir("$all_cache_folder")) {
        if( mkdir("$all_cache_folder", 0755) ) {

            if( mkdir("$all_cache_normal_gads_content", 0755) ) {
                //echo "<br/>$all_cache_normal_gads_content created";
            }
            else {
                //echo "<br/>Cannot create dir [$all_cache_normal_gads_content]";
            }
        }
        else {
            //echo "<br/>Cannot create dir [$all_cache_folder]";
        }
    }
    else {
        if(!file_exists("$all_cache_normal_gads_content") || !is_dir("$all_cache_normal_gads_content")) {
            if( mkdir("$all_cache_normal_gads_content", 0755) ) {
                //chmod("$general_path/all_cache/weather", 777);
                //echo "<br/>$all_cache_normal_gads_content created";
            }
            else {
                //echo "<br/>Cannot create dir [$all_cache_normal_gads_content]";
            }
        }
    }
    /* END Check for cache folders  *************************************************************/

    $have_it_on_cache = false;
    $md5 = md5(serialize($ws_request_available_ad_units));
    $cache_file = $all_cache_normal_gads_content . "/" . $md5;
    if(file_exists($cache_file)) {
        //echo "<br/>[$cache_file] exists";
        $cache_time = filemtime($cache_file);
        $current_time = time();
        if($current_time - $cache_time < 120) {
            $have_it_on_cache = true;
            //echo "<br/>$cache_file time is earlier than 15 min";
        }
        else {
            unlink($cache_file);
            //echo "<br/>$cache_file time is older than 15 min";
        }
    }
    if($have_it_on_cache) {
        $cache_get = file_get_contents($cache_file);

        if($cache_get != "") {
            $cache_get = unserialize($cache_get);
            $current_ad_units = $cache_get;
        }
    }
    else {
        //echo "<br/> --- here";
        $current_ad_units = getAdSenseContentUnits($webservices_url, $ws_request_available_ad_units);
        //echo "<br/>DO NOT Have it on cache";
        $cache_put = serialize($current_ad_units);
        if( true == ($fc = fopen($cache_file, "w") ) ) {
            fwrite($fc, $cache_put, strlen($cache_put));
            fclose($fc);
        }
    }


    ?>

    <form method="POST" action="custom_module_update.php" name="cinema_frm" id="cinema_frm">
        <div class="inline_container">
            <table cellpadding="0" cellspacing="0" border="0" width="100%">
                <tr>
                    <th colspan="2" align="left">
                        Cinema Module Customization
                    </th>
                </tr>
                <tr>
                    <td nowrap class="itdh">Select Town</td>
                    <td class="itd">
                        <select name="module_location">
                            <?php
                            foreach($cities_list as $loc=>$label) {
                                $selected = "";
                                if($loc == $module_location) $selected = 'selected="selected"';
                                echo '<option value="' . $loc . '" ' . $selected . '>' . $label . '</option>';
                            }

                            ?>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td nowrap class="itdh">Select Mode</td>
                    <td class="itd">
                        <select name="module_mode">
                            <?php
                            foreach($modes as $mode=>$label) {
                                $selected = "";
                                if($mode == $module_mode) $selected = 'selected="selected"';
                                echo '<option value="' . $mode . '" ' . $selected . '>' . $label . '</option>';
                            }

                            ?>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td class="itdh">Display Ads?</td>
                    <td class="itd_lb">
                        <select name="def_ads">
                            <option value="yes" <?php echo $def_ads == "yes" ? 'selected="selected"': '';?>>Yes</option>
                            <option value="no" <?php echo $def_ads == "no" ? 'selected="selected"': '';?>>No</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="itdh">Select Ad Type</td>
                    <td class="itd_lb">
                        <div style="width: 100%; height: 400px; overflow-y:auto;">
                            <table cellpadding="0" cellspacing="0" border="0" width="100%">
                                <?php
                                if(isset($current_ad_units) && count($current_ad_units) > 0) {
                                    $types = array("text_image" => "Text & Image", "text" => "Text Only", "image" => "Image Only");
                                    $current_type = "";
                                    $current_type_name = "";
                                    foreach ($current_ad_units as $i => $row) {
                                        $ws_ad_unit_id = $row['ws_ad_unit_id'];
                                        $ws_ad_unit_name = stripslashes($row['ws_ad_unit_name']);
                                        $ws_ad_unit_type = $row['ws_ad_unit_type'];
                                        $ws_ad_unit_format = $row['ws_ad_unit_format'];
                                        $ws_ad_unit_palette = $row['ws_ad_unit_palette_name'];
                                        $ws_ad_unit_code = $row['ws_ad_unit_code'];
                                        $ws_ad_unit_active = ($row['ws_ad_unit_active'] == 1) ? "Active" : "Inactive";

                                        $checked = "";
                                        if (isset($ad_unit_id) && $ad_unit_id == $ws_ad_unit_id) {
                                            $checked = " checked";
                                        }

                                        if ($ws_ad_unit_type != $current_type) {
                                            $current_type = $ws_ad_unit_type;
                                            $current_type_name = $types[$ws_ad_unit_type];

                                            ?>
                                            <tr>
                                                <td class="itdh" colspan="4"><strong><?php echo $current_type_name; ?></strong></td>
                                            </tr>
                                            <?php
                                        }
                                        ?>
                                        <tr>
                                            <td class="itd"><input type="radio" name="ws_ad_unit_id"
                                                                   value="<?php echo $ws_ad_unit_id; ?>" <?php echo $checked; ?>
                                                                   class="radio_locations"/></td>
                                            <td class="itd_lb"><?php echo $ws_ad_unit_name; ?></td>
                                            <td class="itd_lb"><?php echo $ws_ad_unit_format; ?></td>
                                            <td class="itd_lb"><?php echo $ws_ad_unit_palette; ?></td>
                                        </tr>
                                        <?php
                                    }
                                }
                                ?>
                            </table>
                        </div>
                    </td>
                </tr>


                <tr>
                    <td colspan="2" align="right" class="footer">
                        <input type="reset" value="Reset" name="submit" class="submit_button"/>
                        <input type="submit" value="Save" name="submit" class="submit_button"/>
                        <input type="hidden" value="<?php echo $current_catdom_module_id; ?>" name="admin_catdom_module_id" id="admin_catdom_module_id"/>
                    </td>
                </tr>
            </table>
        </div>

    </form>
    <?php

    $module_forms = array(
        "form_name" => "cinema_frm",
        "form_action" => "",
        "form_method" => "POST"
    );
}
else {
    // We are in the front-end
    // Initialize defaults
    $module_location = "null";
    $module_mode = null;
    $def_ads = "no";
    $ws_ad_unit_id = "";

    // Try to get any customization done
    if(isset($module_params) && is_array($module_params)) {
        foreach ($module_params as $key=>$value) {
            ${$key} = $value;
        }
    }
}
?>