<?php

/**
 * vpanel-stack
 * https://git.stack-source.com/msb/vpanel-stack
 * Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
 * GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
 */

namespace Panel\Vmail;

class Autoresponders extends \Panel\Vmail {

  function beforeRoute($f3) {

    parent::beforeRoute($f3);

    $domain = $f3->get('PARAMS.domain');

    /* first confirm domain exists */
    if ($f3->call('\Panel::vGet', "vmail-domains-get.sh -d $domain -c")) {

      /* check if we are looking for specific autoresponder (email account) */
      if ($f3->exists('PARAMS.mbox')) {

        $mbox = $f3->get('PARAMS.mbox');

        /* get autoresponder */
        if ($autoresponders_array = $f3->call('\Panel::vGet', array("vmail-autoresponders-get.sh -e $mbox@$domain -c", FALSE))) {
          $f3->set('autoresponders_array', $autoresponders_array);
        }

      } else {

        /* get autoresponders */
        if ($autoresponders_array = $f3->call('\Panel::vGet', array("vmail-autoresponders-get.sh -d $domain -c", FALSE))) {
          $f3->set('autoresponders_array', $autoresponders_array);
        }

      }

    }

  }

  public static function get($f3) {

    $domain = $f3->get('PARAMS.domain');
    $autoresponders_array = $f3->get('autoresponders_array');

    /* convert data for frontend display */
    if (is_array($autoresponders_array) && count($autoresponders_array) > 0) {

      foreach($autoresponders_array as $k=>$autoresponder_array) {

        if ($autoresponder_array['status'] == 1) {
          $autoresponder_array['status'] = 'Enabled';
        } else {
          $autoresponder_array['status'] = 'Disabled';
        }

        $subject = $autoresponder_array['subject'];
        $subject = str_replace("\\'", "'", $subject);
        $subject = stripslashes($subject);
        $subject = substr_replace($subject, "", 0, 1);
        $subject = substr_replace($subject, "", -1);
        $autoresponder_array['subject'] = $subject;

        $body = $autoresponder_array['body'];
        $body = str_replace('\n', '<br>', $body);
        $body = str_replace("\\'", "'", $body);
        $body = stripslashes($body);
        $body = substr_replace($body, "", 0, 1);
        $body = substr_replace($body, "", -1);
        $autoresponder_array['body'] = $body;

        $autoresponders_array[$k] = $autoresponder_array;

      }

    } elseif ($f3->exists('PARAMS.mbox')) {

      /* request for specific email account, autorespondering does not exist, go directly to Add autoresponder form */
      $f3->clear('autoresponders_array');
      $mbox = $f3->get('PARAMS.mbox');
      $mapping = $f3->get('NAV.mapping');
      if ($mapping == 'vmail') {
        $f3->reroute("/Accounts/$mbox/Autoresponder/Add");
      } else {
        $f3->reroute("/Email/$domain/Accounts/$mbox/Autoresponder/Add");
      }

    }

    if ($f3->exists('PARAMS.mbox')) {

      $mbox = $f3->get('PARAMS.mbox');
      $f3->set('page_header', "Autoresponder for $mbox@$domain");
      $f3->set('autoresponders_array', $autoresponders_array[0]);
      echo \Template::instance()->render('vmail/autoresponders-autoresponder.html');

    } else {

      $f3->set('autoresponders_array', $autoresponders_array);
      $f3->set('page_header', "Email Autoresponders for $domain");
      echo \Template::instance()->render('vmail/autoresponders.html');

    }

  }

}