<?php

  /**
   * on_load_control_tower event handler
   *
   * @package angie.frameworks.email
   * @subpackage handlers
   */

  /**
   * Handle on_load_control_tower event
   *
   * @param ControlTower $control_tower
   * @param User $user
   */
  function email_handle_on_load_control_tower(ControlTower &$control_tower, User &$user) {

    // ---------------------------------------------------
    //  Email to Comment
    // ---------------------------------------------------

    $reply_to_comment_label = null;
    if(ConfigOptions::getValue('control_tower_check_reply_to_comment')) {
      if(IncomingMailboxes::testReplyToComments(ApplicationMailer::getDefaultSender()->getEmail())) {
        $reply_to_comment_ok = true;
      } else {
        $reply_to_comment_ok = false;
      } // if
    } else {
      $reply_to_comment_ok = true;

    } // if

    $control_tower->indicators()->add('mail_to_comment', array(
      'label' => lang('Reply to Comment'),
      'value' => $reply_to_comment_label,
      'value' => null,
      'url' => Router::assemble('email_admin_reply_to_comment'),
      'is_ok' => $reply_to_comment_ok,
    ));

    // ---------------------------------------------------
    //  Queue Count
    // ---------------------------------------------------

    if(ConfigOptions::getValue('control_tower_check_email_queue')) {
      $queue_count_label = OutgoingMessages::countUnsent();
      $queue_count_label = $queue_count_label ? $queue_count_label : null;
      $queue_count_ok = empty($queue_count_label);
    } else {
      $queue_count_ok = true;
      $queue_count_label = null;
    } // if

    $control_tower->indicators()->add('queue', array(
      'label' => lang('Mailing Queue'),
      'value' => $queue_count_label,
      'url' => Router::assemble('outgoing_messages_admin'),
      'is_ok' => $queue_count_ok,
    ));

    // ---------------------------------------------------
    //  Conflicts Count
    // ---------------------------------------------------

    if(ConfigOptions::getValue('control_tower_check_email_conflicts')) {
      $conflicts_count_label = IncomingMails::countConflicts();
      $conflicts_count_ok = empty($conflicts_count_label);
    } else {
      $conflicts_count_ok = true;
      $conflicts_count_label = null;
    } // if

    $control_tower->indicators()->add('conflicts', array(
      'label' => lang('Mail Conflicts'),
      'value' => $conflicts_count_label,
      'url' => Router::assemble('incoming_email_admin_conflict'),
      'is_ok' => $conflicts_count_ok,
    ));

  } // email_handle_on_load_control_tower