<commit_before><?php

require_once 'Swat/SwatControl.php';
require_once 'Swat/SwatFlydown.php';
require_once 'Swat/SwatState.php';
require_once 'Date.php';

// TODO: figure out why the valid-ranges are getting having the time inproperly
// 		 offset.

/**
 * A time entry widget
 *
 * @package   Swat
 * @copyright 2004-2005 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SwatTimeEntry extends SwatControl implements SwatState
{
	const  HOUR   = 1;
	const  MINUTE = 2;
	const  SECOND = 4;

	/**
	 * Time of this time entry widget
	 *
	 * The year, month and day fields of the Date object are unused and
	 * undefined.
	 *
	 * @var Date
	 */
	public $value = null;

	/**
	 * Required time parts
	 *
	 * Bitwise combination of SwatTime::HOUR, SwatTime::MINUTE
	 * and SwatTime::SECOND.
	 *
	 * @var integer
	 */
	public $required;

	/**
	 * Displayed time parts
	 *
	 * Bitwise combination of SwatTime::HOUR, SwatTime::MINUTE
	 * and SwatTime::SECOND.
	 *
	 * @var integer
	 */
	public $display;

	/**
	 * Start time of the valid range (inclusive)
	 *
	 * Defaults to 00:00:00. The year, month and day fields of the Date object
	 * are ignored and undefined.
	 *
	 * @var Date
	 */
	public $valid_range_start;

	/**
	 * End time of the valid range (inclusive)
	 *
	 * Defaults to 23:59:59. The year, month and day fields of the Date object
	 * are ignored and undefined.
	 *
	 * @var Date
	 */
	public $valid_range_end;

	/**
	 * A reference to the internal hour flydown
	 *
	 * @var SwatFlydown
	 */
	private $hour_flydown;

	/**
	 * A reference to the internal minute flydown
	 *
	 * @var SwatFlydown
	 */
	private $minute_flydown;

	/**
	 * A reference to the internal second flydown
	 *
	 * @var SwatFlydown
	 */
	private $second_flydown;

	/**
	 * A reference to the internal am/pm flydown
	 *
	 * @var SwatFlydown
	 */
	private $am_pm_flydown;

	/**
	 * An internal flag telling whether internal widgets have been
	 * created or not
	 *
	 * @var boolean
	 */
	private $created = false;

	/**
	 * Initializes this widget
	 *
	 * Sets default required and display parts and sets default valid range
	 * for this time entry.
	 */
	public function init()
	{
		$this->display  = self::HOUR | self::MINUTE;
		$this->required = $this->display;

		$date = new SwatDate();
		$date->setYear(0);
		$date->setMonth(1);
		$date->setDay(1);
		$date->setHour(0);
		$date->setMinute(0);
		$date->setSecond(0);
		$date->setTZ('UTC');

		$this->valid_range_start = clone $date;

		$date->setHour(23);
		$date->setMinute(59);
		$date->setSecond(59);
		$this->valid_range_end = clone $date;
	}

	/**
	 * Displays this time entry
	 *
	 * Creates internal widgets if they do not exits then displays required
	 * javascript, then displays internal widgets.
	 */
	public function display()
	{
		$this->createFlydowns();
		$this->displayJavascript();

		if ($this->display & self::HOUR)
			$this->hour_flydown->display();

		if ($this->display & self::MINUTE)
			$this->minute_flydown->display();

		if ($this->display & self::SECOND)
			$this->second_flydown->display();

		if ($this->display & self::HOUR)
			$this->am_pm_flydown->display();
	}

	/**
	 * Processes this time entry
	 *
	 * Creates internal widgets if they do not exist and then assigns their
	 * values based on the time entered by the user. If the time is not valid,
	 * an error message is attached to this time entry.
	 */
	public function process()
	{
		$this->createFlydowns();

		if ($this->display & self::HOUR) {
			$this->hour_flydown->process();
			$this->am_pm_flydown->process();
			$hour = intval($this->hour_flydown->value);
			$ampm = $this->am_pm_flydown->value;

			if ($this->required & self::HOUR && $hour === null) {
				$msg = Swat::_('Hour is Required.');
				$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));
			}

			if ($this->required & self::HOUR && $ampm === null) {
				$msg = Swat::_('AM/PM is Required.');
				$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));
			}

		if ($ampm == 'pm') {
				$hour += 12;
				if ($hour == 24)
					$hour = 0;
			}
		} else {
			$hour = 0;
		}

		if ($this->display & self::MINUTE) {
			$this->minute_flydown->process();
			$minute = intval($this->minute_flydown->value);

			if ($this->required & self::MINUTE && $minute === null) {
				$msg = Swat::_('Minute is Required.');
				$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));
			}
		} else {
			$minute = 0;
		}

		if ($this->display & self::SECOND) {
			$this->second_flydown->process();
			$second = intval($this->second_flydown->value);

			if ($this->required & self::SECOND && $second === null) {
				$msg = Swat::_('Second is Required.');
				$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));
			}
		} else {
			$second = 0;
		}

		$this->value = new SwatDate();
		$this->value->setYear(0);
		$this->value->setMonth(1);
		$this->value->setDay(1);
		$this->value->setHour($hour);
		$this->value->setMinute($minute);
		$this->value->setSecond($second);
		$this->value->setTZ('UTC');

		$this->validateRanges();
	}

	/**
	 * Gets the current state of this time entry widget
	 *
	 * @return boolean the current state of this time entry widget.
	 *
	 * @see SwatState::getState()
	 */
	public function getState()
	{
		if ($this->value === null)
			return null;
		else
			return $this->value->getDate();	
	}

	/**
	 * Sets the current state of this time entry widget
	 *
	 * @param boolean $state the new state of this time entry widget.
	 *
	 * @see SwatState::setState()
	 */
	public function setState($state)
	{
		$this->value = new SwatDate($state)
	}

	/**
	 * Creates all internal widgets required for this date entry
	 */
	private function createFlydowns()
	{
		if ($this->created) return;

		$this->created = true;

		if ($this->display & self::HOUR)
			$this->createHourFlydown();

		if ($this->display & self::MINUTE)
			$this->createMinuteFlydown();

		if ($this->display & self::SECOND)
			$this->createSecondFlydown();

		if ($this->display & self::HOUR)
			$this->createAmPmFlydown();
	}

	/**
	 * Creates the hour flydown for this time entry
	 */
	private function createHourFlydown()
	{
		$this->hour_flydown = new SwatFlydown($this->id.'_hour');
		$this->hour_flydown->onchange = sprintf("timeSet('%s', this);",
			$this->id);

		for ($i = 1; $i <= 12; $i++)
			$this->hour_flydown->addOption($i, $i);
	}

	/**
	 * Creates the minute flydown for this time entry
	 */
	private function createMinuteFlydown()
	{
		$this->minute_flydown = new SwatFlydown($this->id.'_minute');
		$this->minute_flydown->onchange =
			sprintf("timeSet('%s', this);", $this->id);

		for ($i = 0; $i <= 59; $i++)
			$this->minute_flydown->addOption($i,
				str_pad($i, 2, '0', STR_PAD_LEFT));
	}

	/**
	 * Creates the second flydown for this time entry
	 */
	private function createSecondFlydown()
	{
		$this->second_flydown = new SwatFlydown($this->id.'_second');
		$this->second_flydown->onchange =
			sprintf("timeSet('%s', this);", $this->id);

		for ($i = 0; $i <= 59; $i++)
			$this->second_flydown->addOptions($i,
				str_pad($i, 2 ,'0', STR_PAD_LEFT));
	}

	/**
	 * Creates the am/pm flydown for this time entry
	 */
	private function createAmPmFlydown()
	{
		$this->am_pm_flydown = new SwatFlydown($this->id.'_ampm');
		$this->am_pm_flydown->addOptionsByArray(array('am' => 'AM', 'pm' => 'PM'));
		$this->am_pm_flydown->onchange =
			sprintf("timeSet('%s', this);", $this->id);
	}

	/**
	 * Makes sure the date the user entered is within the valid range
	 *
	 * If the time is not within the valid range, this method attaches an
	 * error message to this time entry.
	 */
	private function validateRanges()
	{
		$this->valid_range_start->setYear(0);
		$this->valid_range_start->setMonth(1);
		$this->valid_range_start->setDay(1);

		$this->valid_range_end->setYear(0);
		$this->valid_range_end->setMonth(1);
		$this->valid_range_end->setDay(1);

		if (Date::compare($this->value, $this->valid_range_start, true) == -1) {

			$msg = sprintf(Swat::_('The time you have entered is invalid. '.
				'It must be after %s.'),
				$this->displayTime($this->valid_range_start));

			$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));

		} elseif (Date::compare($this->value, $this->valid_range_end, true) == 1) {

			$msg = sprintf(Swat::_('The time you have entered is invalid. '.
				'It must be before %s.'),
				$this->displayTime($this->valid_range_end));

			$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));

		}
	}

	/**
	 * Converts a time to a human readable string
	 *
	 * This is a convenience method.
	 *
	 * @param Date $time the time to convert to a string.
	 *
	 * @return string the time converted to a string.
	 */
	private function displayTime($time)
	{
		return $time->format('%r'); // TODO: %X
	}

	/**
	 * Outputs the javascript required for this control
	 */
	private function displayJavascript()
	{
		echo '<script type="text/javascript" src="swat/javascript/swat-find-index.js"></script>';
		echo '<script type="text/javascript" src="swat/javascript/swat-time.js"></script>';
	}
}

?>
<commit_msg>Remove unnecessary spaces before constants.<commit_after><?php

require_once 'Swat/SwatControl.php';
require_once 'Swat/SwatFlydown.php';
require_once 'Swat/SwatState.php';
require_once 'Date.php';

// TODO: figure out why the valid-ranges are getting having the time inproperly
// 		 offset.

/**
 * A time entry widget
 *
 * @package   Swat
 * @copyright 2004-2005 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SwatTimeEntry extends SwatControl implements SwatState
{
	const HOUR   = 1;
	const MINUTE = 2;
	const SECOND = 4;

	/**
	 * Time of this time entry widget
	 *
	 * The year, month and day fields of the Date object are unused and
	 * undefined.
	 *
	 * @var Date
	 */
	public $value = null;

	/**
	 * Required time parts
	 *
	 * Bitwise combination of SwatTime::HOUR, SwatTime::MINUTE
	 * and SwatTime::SECOND.
	 *
	 * @var integer
	 */
	public $required;

	/**
	 * Displayed time parts
	 *
	 * Bitwise combination of SwatTime::HOUR, SwatTime::MINUTE
	 * and SwatTime::SECOND.
	 *
	 * @var integer
	 */
	public $display;

	/**
	 * Start time of the valid range (inclusive)
	 *
	 * Defaults to 00:00:00. The year, month and day fields of the Date object
	 * are ignored and undefined.
	 *
	 * @var Date
	 */
	public $valid_range_start;

	/**
	 * End time of the valid range (inclusive)
	 *
	 * Defaults to 23:59:59. The year, month and day fields of the Date object
	 * are ignored and undefined.
	 *
	 * @var Date
	 */
	public $valid_range_end;

	/**
	 * A reference to the internal hour flydown
	 *
	 * @var SwatFlydown
	 */
	private $hour_flydown;

	/**
	 * A reference to the internal minute flydown
	 *
	 * @var SwatFlydown
	 */
	private $minute_flydown;

	/**
	 * A reference to the internal second flydown
	 *
	 * @var SwatFlydown
	 */
	private $second_flydown;

	/**
	 * A reference to the internal am/pm flydown
	 *
	 * @var SwatFlydown
	 */
	private $am_pm_flydown;

	/**
	 * An internal flag telling whether internal widgets have been
	 * created or not
	 *
	 * @var boolean
	 */
	private $created = false;

	/**
	 * Initializes this widget
	 *
	 * Sets default required and display parts and sets default valid range
	 * for this time entry.
	 */
	public function init()
	{
		$this->display  = self::HOUR | self::MINUTE;
		$this->required = $this->display;

		$date = new SwatDate();
		$date->setYear(0);
		$date->setMonth(1);
		$date->setDay(1);
		$date->setHour(0);
		$date->setMinute(0);
		$date->setSecond(0);
		$date->setTZ('UTC');

		$this->valid_range_start = clone $date;

		$date->setHour(23);
		$date->setMinute(59);
		$date->setSecond(59);
		$this->valid_range_end = clone $date;
	}

	/**
	 * Displays this time entry
	 *
	 * Creates internal widgets if they do not exits then displays required
	 * javascript, then displays internal widgets.
	 */
	public function display()
	{
		$this->createFlydowns();
		$this->displayJavascript();

		if ($this->display & self::HOUR)
			$this->hour_flydown->display();

		if ($this->display & self::MINUTE)
			$this->minute_flydown->display();

		if ($this->display & self::SECOND)
			$this->second_flydown->display();

		if ($this->display & self::HOUR)
			$this->am_pm_flydown->display();
	}

	/**
	 * Processes this time entry
	 *
	 * Creates internal widgets if they do not exist and then assigns their
	 * values based on the time entered by the user. If the time is not valid,
	 * an error message is attached to this time entry.
	 */
	public function process()
	{
		$this->createFlydowns();

		if ($this->display & self::HOUR) {
			$this->hour_flydown->process();
			$this->am_pm_flydown->process();
			$hour = intval($this->hour_flydown->value);
			$ampm = $this->am_pm_flydown->value;

			if ($this->required & self::HOUR && $hour === null) {
				$msg = Swat::_('Hour is Required.');
				$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));
			}

			if ($this->required & self::HOUR && $ampm === null) {
				$msg = Swat::_('AM/PM is Required.');
				$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));
			}

		if ($ampm == 'pm') {
				$hour += 12;
				if ($hour == 24)
					$hour = 0;
			}
		} else {
			$hour = 0;
		}

		if ($this->display & self::MINUTE) {
			$this->minute_flydown->process();
			$minute = intval($this->minute_flydown->value);

			if ($this->required & self::MINUTE && $minute === null) {
				$msg = Swat::_('Minute is Required.');
				$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));
			}
		} else {
			$minute = 0;
		}

		if ($this->display & self::SECOND) {
			$this->second_flydown->process();
			$second = intval($this->second_flydown->value);

			if ($this->required & self::SECOND && $second === null) {
				$msg = Swat::_('Second is Required.');
				$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));
			}
		} else {
			$second = 0;
		}

		$this->value = new SwatDate();
		$this->value->setYear(0);
		$this->value->setMonth(1);
		$this->value->setDay(1);
		$this->value->setHour($hour);
		$this->value->setMinute($minute);
		$this->value->setSecond($second);
		$this->value->setTZ('UTC');

		$this->validateRanges();
	}

	/**
	 * Gets the current state of this time entry widget
	 *
	 * @return boolean the current state of this time entry widget.
	 *
	 * @see SwatState::getState()
	 */
	public function getState()
	{
		if ($this->value === null)
			return null;
		else
			return $this->value->getDate();	
	}

	/**
	 * Sets the current state of this time entry widget
	 *
	 * @param boolean $state the new state of this time entry widget.
	 *
	 * @see SwatState::setState()
	 */
	public function setState($state)
	{
		$this->value = new SwatDate($state)
	}

	/**
	 * Creates all internal widgets required for this date entry
	 */
	private function createFlydowns()
	{
		if ($this->created) return;

		$this->created = true;

		if ($this->display & self::HOUR)
			$this->createHourFlydown();

		if ($this->display & self::MINUTE)
			$this->createMinuteFlydown();

		if ($this->display & self::SECOND)
			$this->createSecondFlydown();

		if ($this->display & self::HOUR)
			$this->createAmPmFlydown();
	}

	/**
	 * Creates the hour flydown for this time entry
	 */
	private function createHourFlydown()
	{
		$this->hour_flydown = new SwatFlydown($this->id.'_hour');
		$this->hour_flydown->onchange = sprintf("timeSet('%s', this);",
			$this->id);

		for ($i = 1; $i <= 12; $i++)
			$this->hour_flydown->addOption($i, $i);
	}

	/**
	 * Creates the minute flydown for this time entry
	 */
	private function createMinuteFlydown()
	{
		$this->minute_flydown = new SwatFlydown($this->id.'_minute');
		$this->minute_flydown->onchange =
			sprintf("timeSet('%s', this);", $this->id);

		for ($i = 0; $i <= 59; $i++)
			$this->minute_flydown->addOption($i,
				str_pad($i, 2, '0', STR_PAD_LEFT));
	}

	/**
	 * Creates the second flydown for this time entry
	 */
	private function createSecondFlydown()
	{
		$this->second_flydown = new SwatFlydown($this->id.'_second');
		$this->second_flydown->onchange =
			sprintf("timeSet('%s', this);", $this->id);

		for ($i = 0; $i <= 59; $i++)
			$this->second_flydown->addOptions($i,
				str_pad($i, 2 ,'0', STR_PAD_LEFT));
	}

	/**
	 * Creates the am/pm flydown for this time entry
	 */
	private function createAmPmFlydown()
	{
		$this->am_pm_flydown = new SwatFlydown($this->id.'_ampm');
		$this->am_pm_flydown->addOptionsByArray(array('am' => 'AM', 'pm' => 'PM'));
		$this->am_pm_flydown->onchange =
			sprintf("timeSet('%s', this);", $this->id);
	}

	/**
	 * Makes sure the date the user entered is within the valid range
	 *
	 * If the time is not within the valid range, this method attaches an
	 * error message to this time entry.
	 */
	private function validateRanges()
	{
		$this->valid_range_start->setYear(0);
		$this->valid_range_start->setMonth(1);
		$this->valid_range_start->setDay(1);

		$this->valid_range_end->setYear(0);
		$this->valid_range_end->setMonth(1);
		$this->valid_range_end->setDay(1);

		if (Date::compare($this->value, $this->valid_range_start, true) == -1) {

			$msg = sprintf(Swat::_('The time you have entered is invalid. '.
				'It must be after %s.'),
				$this->displayTime($this->valid_range_start));

			$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));

		} elseif (Date::compare($this->value, $this->valid_range_end, true) == 1) {

			$msg = sprintf(Swat::_('The time you have entered is invalid. '.
				'It must be before %s.'),
				$this->displayTime($this->valid_range_end));

			$this->addMessage(new SwatMessage($msg, SwatMessage::USER_ERROR));

		}
	}

	/**
	 * Converts a time to a human readable string
	 *
	 * This is a convenience method.
	 *
	 * @param Date $time the time to convert to a string.
	 *
	 * @return string the time converted to a string.
	 */
	private function displayTime($time)
	{
		return $time->format('%r'); // TODO: %X
	}

	/**
	 * Outputs the javascript required for this control
	 */
	private function displayJavascript()
	{
		echo '<script type="text/javascript" src="swat/javascript/swat-find-index.js"></script>';
		echo '<script type="text/javascript" src="swat/javascript/swat-time.js"></script>';
	}
}

?>

{'fraction_non_alphanumeric': 0.16477987421383647, 'fraction_numerical': 0.007547169811320755, 'mean_word_length': 3.496188836980575, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 293, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2060433', 'n_tokens_mistral': 7058, 'n_tokens_neox': 6200, 'n_words': 1582}