<?php

  // Inherit application level controller
  AngieApplication::useController('email_admin', EMAIL_FRAMEWORK_INJECT_INTO);

  /**
   * Framework level mailbox management controller
   *
   * @package angie.frameworks.email
   * @subpackage controllers
   */
  abstract class FwIncomingMailAdminController extends EmailAdminController {
    
    /**
     * Prepare controller
     */
    function __before() {
      parent::__before();
      
     
    } // __construct
    
    /**
     * Mailbox index
     */
    function settings() {
      
      if($this->request->isAsyncCall() || ($this->request->isApiCall() && $this->request->isSubmitted())) {
        
        $settings_data = $this->request->post('settings');
        
        if(!is_array($settings_data)) {
          $settings_data['conflict_notifications_delivery'] = ConfigOptions::getValue('conflict_notifications_delivery', false);
        }//if
        
        $this->response->assign('settings_data', $settings_data);
        
        if($this->request->isSubmitted()) {
          try {
      	    DB::beginWork('Updating incoming mail configuration @ ' . __CLASS__);
      	    
      	    ConfigOptions::setValue(array(
        	      'conflict_notifications_delivery' => (integer) $settings_data['conflict_notifications_delivery'],
      	    ));
        	  
            DB::commit('Incoming configuration updated @ ' . __CLASS__);
        	  
            $this->response->respondWithData(array(
        	    'conflict_notifications_delivery' => ConfigOptions::getValue('conflict_notifications_delivery')
        	  ), 'settings');
        	  
      	  } catch (Exception $e) {
      	    DB::rollback('Failed to update incoming mail configuration @ ' . __CLASS__);
      	    $this->response->exception($e);
      	  }//try
        }//if
      } else {
        $this->response->badRequest();
      }//if
    }//settings
    
    
  }//FwIncomingMailAdminController