<?php

require_once 'eventics.civix.php';
use CRM_Eventics_ExtensionUtil as E;

/**
 * Implements hook_civicrm_config().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/ 
 */
function eventics_civicrm_config(&$config) {
  _eventics_civix_civicrm_config($config);
}

/**
 * Implements hook_civicrm_xmlMenu().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_xmlMenu
 */
function eventics_civicrm_xmlMenu(&$files) {
  _eventics_civix_civicrm_xmlMenu($files);
}

/**
 * Implements hook_civicrm_install().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
 */
function eventics_civicrm_install() {
  _eventics_civix_civicrm_install();
}

/**
 * Implements hook_civicrm_postInstall().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
 */
function eventics_civicrm_postInstall() {
  _eventics_civix_civicrm_postInstall();
}

/**
 * Implements hook_civicrm_uninstall().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
 */
function eventics_civicrm_uninstall() {
  _eventics_civix_civicrm_uninstall();
}

/**
 * Implements hook_civicrm_enable().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
 */
function eventics_civicrm_enable() {
  _eventics_civix_civicrm_enable();
}

/**
 * Implements hook_civicrm_disable().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
 */
function eventics_civicrm_disable() {
  _eventics_civix_civicrm_disable();
}

/**
 * Implements hook_civicrm_upgrade().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
 */
function eventics_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
  return _eventics_civix_civicrm_upgrade($op, $queue);
}

/**
 * Implements hook_civicrm_alterSettingsFolders().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsFolders
 */
function eventics_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
  _eventics_civix_civicrm_alterSettingsFolders($metaDataFolders);
}

/**
 * Implements hook_civicrm_alterMailContent().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterMailContent
 */
function eventics_civicrm_alterMailContent(&$content) {
  $name = CRM_Utils_Array::value('valueName', $content);

  if ($name == 'event_online_receipt') {
    // Civi::log()->debug('alterMailContent: ' . CRM_Utils_Array::value('valueName', $content), ['content' => $content ]);
  }
}

/**
 * Implements hook_civicrm_alterMailParams().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterMailParams
 */
function eventics_civicrm_alterMailParams(&$params, $context) {
  if ($context != 'messageTemplate') {
    return;
  }

  $name = CRM_Utils_Array::value('valueName', $params);

  if ($name == 'event_online_receipt') {
    $event_id = $params['tplParams']['event']['id'] ?? null;

    if (!$event_id) {
      return;
    }

    $start = $end = $type = 0;
    $info = CRM_Event_BAO_Event::getCompleteInfo($start, $type, $event_id, $end, FALSE);

    $template = CRM_Core_Smarty::singleton();
    $template->assign('events', $info);
    $template->assign('timezone', @date_default_timezone_get());

    // Send data to the correct template for formatting
    $calendar = $template->fetch('CRM/Core/Calendar/ICal.tpl');
    $calendar = preg_replace('/(?<!\r)\n/', "\r\n", $calendar);

    $config = CRM_Core_Config::singleton();
    $icsfile = tempnam($config->customFileUploadDir, 'ics');

    if ($icsfile !== FALSE) {
      rename($icsfile, $icsfile . '.ics');
      $icsfile .= '.ics';

      if (file_put_contents($icsfile, $calendar) !== FALSE) {
        $attachment = [
          'fullPath' => $icsfile,
          'mime_type' => 'text/calendar',
          'cleanName' => basename($icsfile),
        ];

        $params['attachments'] = [$attachment];
      }
    }
  }
}
