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

	class API_GROUP extends API
	{

		var $name = '';
		var $permissions = '';
		var $application = '';
		var $perms = array();
		var $groupid = 0;
		var $contactable = false;

		var $defaultPerms = array(
			'question' => array (
				'edit' => false,
				'review' => false,
				'delete' => false,
				'approve' => false,
			),
			'news' => array (
				'edit' => false,
				'delete' => false,
			),
			'category' => array (
				'edit' => false,
				'delete' => false,
				'approve' => false,
			),
			'glossary' => array (
				'edit' => false,
				'delete' => false,
			),
			'user' => array (
				'edit' => false,
				'delete' => false,
			),
			'group' => array (
				'edit' => false,
				'delete' => false,
			),
			'comment' => array (
				'edit' => false,
				'delete' => false,
			),
			'setting' => array (
				'edit' => false,
			),
			'template' => array (
				'edit' => false,
			),
			'backup' => array (
				'create' => false,
				'delete' => false,
			),
			'rating' => array (
				'edit' => false,
				'delete' => false,
			),			
			'stats' => array (
				'viewarticle' => false,
				'viewuser' => false,
				'viewcategory' => false,
				'viewsearch' => false,
			),			
		);

		var $fields = array (
			'groupid',
			'name',
			'permissions',
			'application',
			'contactable',
		);

		var $applies = array();

		var $defaultApplies = array (
			'category' => 'all',
		);

		var $pk = 'groupid';

		/**
		* load
		* Load the group from the database
		*
		* @param int $id The id of the group to load from the database
		*
		* @return void
		*/
		function load($id)
		{
			parent::load($id);
			$this->perms = unserialize(str_replace('\"', '"', $this->permissions));
			$this->applies = unserialize(str_replace('\"', '"', $this->application));
			if (empty($this->perms)) {
				$this->perms = $this->getDefaultPerm();
			}
			if (empty($this->applies)) {
				$this->applies = $this->defaultApplies;
			}
		}

		/**
		* save
		* save the group back to the database
		*
		* @return bool was the save successful ?
		*/
		function save()
		{
			$_POST['permissions'] = serialize($this->perms);
			$_POST['application'] = serialize($this->applies);
			return parent::save();
		}

		/**
		* create
		* create a new group in the database
		*
		* @return bool was the creation successful ?
		*/
		function create()
		{
			if (!isset($_POST['contactable'])) {
				$_POST['contactable'] = 0;
			}
			$_POST['permissions'] = serialize($this->perms);
			$_POST['application'] = serialize($this->applies);
			return parent::create();
		}

		/**
		* delete
		* delete the group from the db
		*
		* @param int $id the id of the group to delete
		*
		* @return bool was the deletion successful ?
		*/
		function delete($id=0)
		{
			if (!parent::delete($id)) {
				return false;
			}

			// Delete the assocaitions in the members table
			$query = "DELETE FROM `".$this->tablePrefix."members`
			WHERE `groupid` IN (".((int) $id).")";
			$this->db->Query($query);

			return true;
		}

		/**
		* multiDelete
		* Delete multiple groups in one go
		*
		* @param array $ids The ids to delete
		*
		* @return bool was the deletion successful ?
		*/
		function multiDelete($ids=0)
		{
			if (!parent::multiDelete($ids)) {
				return false;
			}

			// Delete the assocaitions in the members table
			$query = "DELETE FROM `".$this->tablePrefix."members`
			WHERE `groupid` IN (".(implode(',', array_keys($ids))).")";
			$this->db->Query($query);

			return true;
		}

		/**
		* find
		* Find a group based on it's unique key
		*
		* @param string $field the name of the field to find
		* @param string $value the value of the field to find
		*
		* @return void
		*/
		function find($field, $value)
		{
			parent::find($field, $value);
			$this->perms = unserialize(str_replace('\"', '"', $this->permissions));
			$this->applies = unserialize(str_replace('\"', '"', $this->application));
			if (empty($this->perms)) {
				$this->perms = $this->getDefaultPerm();
			}
			if (empty($this->applies)) {
				$this->applies = $this->defaultApplies;
			}
		}

		/**
		* resetPerms
		* Reset the permissions back to their defaults
		*
		* @return void
		*/
		function resetPerms()
		{
			$this->perms = $this->getDefaultPerm();
		}

		/**
		* resetApplies
		* Reset the applies back to their defaults
		*
		* @return void
		*/
		function resetApplies()
		{
			$this->applies = $this->defaultApplies;
		}

		/**
		* __sleep
		* PHP magic sleep function
		*
		* @return array array of fields to include in the serialized var
		*/
		function __sleep()
		{
			return array_merge(parent::__sleep(), $this->fields);
		}

		/**
		* __wakeup
		* PHP magic wakuep function
		*
		* @return void
		*/
		function __wakeup()
		{
			$this->perms = unserialize(str_replace('\"', '"', $this->permissions));
			$this->applies = unserialize(str_replace('\"', '"', $this->application));
		}

		/**
		* getList
		* Get the list of groups from the database
		*
		* @return array The array of groups
		*/
		function getList($allRows=false)
		{
			if ($allRows) {
				$rows = "*";
			} else {
				$rows = "`groupid`, `name`";
			}
			$query = 'SELECT '.$rows.'
				FROM `'.$this->table.'`
				ORDER BY `name`';
			
			$result = $this->db->Query($query);
			$groups = array();
			
			while ($row = $this->db->Fetch($result)) {
				if ($allRows) {
					$groups[$row['groupid']] = $row;
				} else {
					$groups[$row['groupid']] = $row['name'];
				}
			}
			return $groups;
		}
		
		function isFrontendOnly() {
			return (bool)$this->applies['feonly'];
		}
		
		function getDefaultPerm() {
			if (!validUrl()) {			
				if (isset($this->defaultPerms['category']['approve'])) {
					unset($this->defaultPerms['category']['approve']);
				}
			}
			return $this->defaultPerms;
		}

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

		/**
		* validate_name
		*
		* Ensure the group name isn't too long or empty
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_name($var)
		{
			if (empty($var)) {
				return false;
			}

			if (strlen($var) > 50) {
				return false;
			}

			return true;
		}

		/**
		* validate_permissions
		*
		* Ensure the permissions are not empty
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_permissions($var)
		{
			return !empty($var);
		}

		/**
		* validate_application
		*
		* Ensure the application is not empty
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_application($var)
		{
			return !empty($var);
		}

		/**
		* validate_contactable
		*
		* Ensure the contactable var is a 1 or a 0
		*
		* @param string $var
		*
		* @return bool
		*/
		function validate_contactable($var)
		{
			return ($var == 0 || $var == 1);
		}

	}
?>
