<?php
if (!defined('_FEXEC')) { die('No direct'); }

if (!function_exists('run_open_events_cron')) {
  function run_open_events_cron(): void {
    $now = date('Y-m-d H:i:s');

    $sql = "
      SELECT id, categories_id
      FROM event_open_schedule
      WHERE processed = 0
        AND open_at <= '" . tep_db_input($now) . "'
      ORDER BY open_at ASC
    ";
    $rs = tep_db_query($sql);
    while ($row = tep_db_fetch_array($rs)) {
      $cid = (int)$row['categories_id'];

      tep_db_query("
        UPDATE " . TABLE_CATEGORIES . "
           SET categories_status = 1
         WHERE categories_id = " . $cid . " AND categories_status = 0
      ");

      tep_db_query("
        UPDATE event_open_schedule
           SET processed = 1, processed_at = '" . tep_db_input($now) . "'
         WHERE id = " . (int)$row['id'] . " LIMIT 1
      ");
    }
  }
}
