#!/usr/bin/perl
# Access a field, updating the heartbeat with info on all the fields.
# Link to the names of all the fields.
#use Mooix::Thing;
#use Mooix::Root;
run sub {
	my $this=shift;
	my ($field) = $0 =~ /(\w+)$/;
	return if $field eq 'access';
	$field=".$field";
	
	if (@_) {
		if ($field =~ /date/) {
			# Try to parse english date.
			eval "use Date::Manip";
			if (! $@) {
				my @args=$#_;
				my $date=ParseDate(\@_);
				if ($#_ < $#args ) {
					$date=UnixDate($date, "%s");
					if ($date > 0) {
						# Need to do daylight
						# savings time correction,
						# as Date::Manip does not.
						if ((localtime)[8]) {
							# assuming DST is
							# an hour..
							$date+=60*60;
						}
						$_[0]=$date;
					}
				}
				if (! @_) {
					@_=@args;
				}
			}
		}
		
		$this->$field(@_);

		# Unregister old from heartbeat. Don't restart heartbeat,
		# since it will be restarted by the subsequent add anyway.
		$Mooix::Root->system->heartbeat->remove(
			item => $this->item,
			noupdate => 1,
		);
		
		# Register with the heartbeat.
		$this->item($Mooix::Root->system->heartbeat->add(
			object => $this,
			method => "event",
			interval => int($this->_interval),
			date => int($this->_date),
			startdate => int($this->_startdate),
			enddate => int($this->_enddate),
			probability => $this->_probability,
		));
	}
	
	if ($field =~ /date/) {
		# Format as date.
		return scalar localtime($this->$field);
	}
	else {
		return $this->$field;
	}
}
