<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Engine_Form_Element_FloodControl extends Zend_Form_Element_Select
{
  public $helper = 'formFloodControl';

  protected $_registerInArrayValidator = false;
  
  protected $_autoInsertNotEmptyValidator = false;
  
  public function init()
  {
    $this->setMultiOptions(array(
      'minute' => 'Minute(s)',
      'hour' => 'Hour(s)',
      'day' => 'Day(s)'
    ));
  }

  public function setValue($value)
  {
    if( is_string($value) && preg_match('/^(\d+)\s+(\w+)$/', $value, $matches) ) {
      $value = array($matches[1], $matches[2]);
    }
    if( !is_array($value) || count($value) != 2 || '' === $value[0] ) {
      $value = null;
    }
    return parent::setValue($value);
  }

  public function isValid($value, $context = null)
  {
    $this->setValue($value);
    $value = $this->getValue();
    
    if( '' === $value || null === $value ) {
      if( !$this->isRequired() && $this->getAllowEmpty() ) {
        return true;
      } else {
        $this->addError('Value is required and can\'t be empty');
        return false;
      }
    }

    // Process
    $numValue = $value[0];
    $selValue = $value[1];

    // Validate number
  if( !is_numeric($numValue) || (int) $numValue != $numValue ) {
    $this->addError('Please enter a valid integer.');
    return false;
  }


    // Make composite options
    $options = array();
    foreach( $this->options as $k => $v ) {
      if( is_array($v) ) {
        $options = array_merge($options, $v);
      } else {
        $options[$k] = $v;
      }
    }

    // Validate selection
    if( !isset($options[$selValue]) ) {
      $this->addError('Please select an option.');
      return false;
    }

    $valid = parent::isValid($value, $context);
    return $valid;
  }

  /**
   * Load default decorators
   *
   * @return void
   */
  public function loadDefaultDecorators()
  {
    if( $this->loadDefaultDecoratorsIsDisabled() )
    {
      return;
    }

    $decorators = $this->getDecorators();
    if( empty($decorators) )
    {
      $this->addDecorator('ViewHelper');
      Engine_Form::addDefaultDecorators($this);
    }
  }

  protected function _getErrorMessages()
  {
    $translator = $this->getTranslator();
    $messages   = $this->getErrorMessages();
    $value      = $this->getValue();
    foreach ($messages as $key => $message) {
      if (null !== $translator) {
          $message = $translator->translate($message);
      }
//      if (($this->isArray() || is_array($value))
//          && !empty($value)
//      ) {
//          $aggregateMessages = array();
//          foreach ($value as $val) {
//              $aggregateMessages[] = str_replace('%value%', $val, $message);
//          }
//          $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
//      } else {
          $messages[$key] = str_replace('%value%', $value, $message);
//      }
    }
    return $messages;
  }
}
