<?php
	require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'class.api.php');

	/**
	* API_SUBSCRIBER Class
	*/
	class API_SUBSCRIBER extends API
	{
		// {{{ Class variables
		/**
		* The name of the fields in the database
		*
		* @var array
		*/
		var $fields = array (
			'subscriberid',
			'questionid',
			'email',
			'subscribedate',
			'confirmed',
			'confirmcode',
		);

		/**
		* The primary key of the database table
		*
		* @var string
		*/
		var $pk = 'subscriberid';

		/**
		* The id of the subscriber
		*
		* @var integer
		*/
		var $subscriberid = 0;

		/**
		* The questionid this user has subscribed to
		*
		* @var integer
		*/
		var $questionid = 0;

		/**
		* The email address of the subscriber
		*
		* @var string
		*/
		var $email = '';

		/**
		* The date the user subscribed
		*
		* @var string
		*/
		var $subscribedate = '';

		/**
		* Has the user confirmed the subscription
		*
		* @var integer
		*/
		var $confirmed = 0;

		/**
		* The code used to ensure the user is who they say they are. It is sent
		* with the confirm email and used in the unsubscribe links.
		*
		* @var string
		*/
		var $confirmcode = '';
		// }}}

		// {{{ Functions that override the parents declarations
		/**
		* create
		*
		* create a subscriber in the database
		*
		* @return boolean
		*/
		function create()
		{
			// They have subscribed now
			$_POST['subscribedate'] = date('Y-m-d H:i:s', strtotime('now'));

			// They can't have confirmed the subscription already
			$_POST['confirmed'] = 0;

			// Generate a unique token for them
			$_POST['confirmcode'] = md5(uniqid(rand(), true));

			return parent::create();
		}

		// }}}

		// {{{ Functions unique to this class
		// }}}
		/**
		* validate_subscriberid
		*
		* Ensure the subscriberid is a pos int
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_subscriberid($var)
		{
			return $this->is_positive_int($var);
		}

		/**
		* validate_questionid
		*
		* Ensure the questionid is a pos int
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_questionid($var)
		{
			return $this->is_positive_int($var);
		}

		/**
		* validate_email
		*
		* Ensure the email is in a valid format
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_email($var)
		{
			return is_email_address($var);
		}

		/**
		* validate_subscriberdate
		*
		* Ensure the subscribe date is in a valid format
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_subscriberdate($var)
		{
			return $this->is_standard_date($var);
		}

		/**
		* validate_confirmed
		*
		* Ensure the confirmed value is 1 or 0
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_confirmed($var)
		{
			return ($var == 0 || $var == 1);
		}

	}

?>
