<?php
if (!class_exists('vBShop_Item_Core'))
{
	die('No direct access allowed.');
}

class vBShop_Item_moderateforum extends vBShop_Item_Core
{	
	/**
	* The constructor
	*
	* @param	vBulletin	vBulletin registry
	* @param	array		Item info
	*/	
	public function __construct(&$registry, &$item)
	{
		parent::__construct($registry, $item);
	}
	
	/**
	* Function to call before every action
	*/	
	public function action()
	{
		if (!parent::action())
		{
			// This type is inactive
			return false;
		}
		
		// We made it!
		return true;
	}
	
	/**
	* Prints out an AdminCP form
	*/	
	public function print_admin_form($code)
	{
		global $vbphrase;

		require_once(DIR . '/includes/class_bitfield_builder.php');
		if (vB_Bitfield_Builder::build(false) !== false)
		{
			$myobj =& vB_Bitfield_Builder::init();
			if (sizeof($myobj->data['misc']['moderatorpermissions']) != sizeof($this->registry->bf_misc_moderatorpermissions)
				OR
				sizeof($myobj->data['misc']['moderatorpermissions2']) != sizeof($this->registry->bf_misc_moderatorpermissions2))
			{
				$myobj->save($db);
				define('CP_REDIRECT', $this->registry->scriptpath);
				print_stop_message('rebuilt_bitfields_successfully');
			}
		}
		else
		{
			echo "<strong>error</strong>\n";
			print_r(vB_Bitfield_Builder::fetch_errors());
		}

		// add moderator - set default values
		$moderator = array();
		foreach ($myobj->data['misc']['moderatorpermissions'] AS $permission => $option)
		{
			$moderator["$permission"] = $option['default'] ? 1 : 0;
		}
		foreach ($myobj->data['misc']['moderatorpermissions2'] AS $permission => $option)
		{
			$moderator["$permission"] = $option['default'] ? 1 : 0;
		}
		
		print_forum_chooser($vbphrase['forum_and_children'], 				'item[code][forumid]', 									$moderator['forumid']);		

		// post permissions
		print_description_row($vbphrase['post_thread_permissions'], false, 2, 'thead');
		print_yes_no_row($vbphrase['can_edit_posts'], 						'item[code][modperms][caneditposts]', 					$moderator['caneditposts']);
		print_yes_no_row($vbphrase['can_delete_posts'], 					'item[code][modperms][candeleteposts]', 				$moderator['candeleteposts']);
		print_yes_no_row($vbphrase['can_physically_delete_posts'], 			'item[code][modperms][canremoveposts]', 				$moderator['canremoveposts']);
		// thread permissions
		print_yes_no_row($vbphrase['can_open_close_threads'], 				'item[code][modperms][canopenclose]', 					$moderator['canopenclose']);
		print_yes_no_row($vbphrase['can_edit_threads'], 					'item[code][modperms][caneditthreads]', 				$moderator['caneditthreads']);
		print_yes_no_row($vbphrase['can_manage_threads'], 					'item[code][modperms][canmanagethreads]', 				$moderator['canmanagethreads']);
		print_yes_no_row($vbphrase['can_edit_polls'], 						'item[code][modperms][caneditpoll]', 					$moderator['caneditpoll']);
		// moderation permissions
		print_description_row($vbphrase['forum_permissions'], false, 2, 'thead');
		print_yes_no_row($vbphrase['can_post_announcements'], 				'item[code][modperms][canannounce]', 					$moderator['canannounce']);
		print_yes_no_row($vbphrase['can_moderate_posts'], 					'item[code][modperms][canmoderateposts]', 				$moderator['canmoderateposts']);
		print_yes_no_row($vbphrase['can_moderate_attachments'], 			'item[code][modperms][canmoderateattachments]', 		$moderator['canmoderateattachments']);
		print_yes_no_row($vbphrase['can_mass_move_threads'], 				'item[code][modperms][canmassmove]', 					$moderator['canmassmove']);
		print_yes_no_row($vbphrase['can_mass_prune_threads'], 				'item[code][modperms][canmassprune]', 					$moderator['canmassprune']);
		print_yes_no_row($vbphrase['can_set_forum_password'], 				'item[code][modperms][cansetpassword]', 				$moderator['cansetpassword']);
		// visitor messaging permissions
		print_description_row($vbphrase['visitor_message_permissions'], false, 2, 'thead');
		print_yes_no_row($vbphrase['can_edit_posts'], 						'item[code][modperms][caneditvisitormessages]', 		$moderator['caneditvisitormessages']);
		print_yes_no_row($vbphrase['can_delete_posts'], 					'item[code][modperms][candeletevisitormessages]', 		$moderator['candeletevisitormessages']);
		print_yes_no_row($vbphrase['can_physically_delete_posts'], 			'item[code][modperms][canremovevisitormessages]', 		$moderator['canremovevisitormessages']);
		print_yes_no_row($vbphrase['can_moderate_posts'], 					'item[code][modperms][canmoderatevisitormessages]', 	$moderator['canmoderatevisitormessages']);

		// Social Groups
		print_description_row($vbphrase['social_group_permissions'], false, 2, 'thead');
		print_yes_no_row($vbphrase['can_edit_social_groups'], 				'item[code][modperms][caneditsocialgroups]', 			$moderator['caneditsocialgroups']);
		print_yes_no_row($vbphrase['can_delete_social_groups'], 			'item[code][modperms][candeletesocialgroups]', 			$moderator['candeletesocialgroups']);
		print_yes_no_row($vbphrase['can_transfer_social_groups'], 			'item[code][modperms][cantransfersocialgroups]', 		$moderator['cantransfersocialgroups']);

		print_yes_no_row($vbphrase['can_edit_pictures'], 					'item[code][modperms][caneditgrouppicture]', 			$moderator['caneditgrouppicture']);
		print_yes_no_row($vbphrase['can_delete_pictures'], 					'item[code][modperms][candeletegrouppicture]', 			$moderator['candeletegrouppicture']);
		print_yes_no_row($vbphrase['can_moderate_pictures'], 				'item[code][modperms][canmoderategrouppicture]', 		$moderator['canmoderategrouppicture']);

		print_yes_no_row($vbphrase['can_edit_posts'], 						'item[code][modperms][caneditgroupmessages]', 			$moderator['caneditgroupmessages']);
		print_yes_no_row($vbphrase['can_moderate_posts'], 					'item[code][modperms][canmoderategroupmessages]', 		$moderator['canmoderategroupmessages']);
		print_yes_no_row($vbphrase['can_delete_posts'], 					'item[code][modperms][candeletegroupmessages]', 		$moderator['candeletegroupmessages']);
		print_yes_no_row($vbphrase['can_physically_delete_posts'], 			'item[code][modperms][canremovegroupmessages]', 		$moderator['canremovegroupmessages']);
		print_yes_no_row($vbphrase['can_edit_discussions'], 				'item[code][modperms][caneditdiscussions]', 			$moderator['caneditdiscussions']);
		print_yes_no_row($vbphrase['can_moderate_discussions'], 			'item[code][modperms][canmoderatediscussions]', 		$moderator['canmoderatediscussions']);
		print_yes_no_row($vbphrase['can_delete_discussions'], 				'item[code][modperms][candeletediscussions]', 			$moderator['candeletediscussions']);
		print_yes_no_row($vbphrase['can_physically_delete_discussions'], 	'item[code][modperms][canremovediscussions]', 			$moderator['canremovediscussions']);

		// user permissions
		print_description_row($vbphrase['user_permissions'], false, 2, 'thead');
		print_yes_no_row($vbphrase['can_view_ip_addresses'], 				'item[code][modperms][canviewips]', 					$moderator['canviewips']);
		print_yes_no_row($vbphrase['can_view_whole_profile'], 				'item[code][modperms][canviewprofile]', 				$moderator['canviewprofile']);
		print_yes_no_row($vbphrase['can_ban_users'], 						'item[code][modperms][canbanusers]', 					$moderator['canbanusers']);
		print_yes_no_row($vbphrase['can_restore_banned_users'], 			'item[code][modperms][canunbanusers]', 					$moderator['canunbanusers']);
		print_yes_no_row($vbphrase['can_edit_user_signatures'], 			'item[code][modperms][caneditsigs]', 					$moderator['caneditsigs']);
		print_yes_no_row($vbphrase['can_edit_user_avatars'], 				'item[code][modperms][caneditavatar]', 					$moderator['caneditavatar']);
		print_yes_no_row($vbphrase['can_edit_user_profile_pictures'], 		'item[code][modperms][caneditprofilepic]', 				$moderator['caneditprofilepic']);
		print_yes_no_row($vbphrase['can_edit_user_reputation_comments'], 	'item[code][modperms][caneditreputation]', 				$moderator['caneditreputation']);

		// album permissions
		print_description_row($vbphrase['user_album_permissions'], false, 2, 'thead');
		print_yes_no_row($vbphrase['can_edit_albums_pictures'], 			'item[code][modperms][caneditalbumpicture]', 			$moderator['caneditalbumpicture']);
		print_yes_no_row($vbphrase['can_delete_albums_pictures'], 			'item[code][modperms][candeletealbumpicture]', 			$moderator['candeletealbumpicture']);
		print_yes_no_row($vbphrase['can_moderate_pictures'], 				'item[code][modperms][canmoderatepictures]', 			$moderator['canmoderatepictures']);
	 	print_yes_no_row($vbphrase['can_edit_picture_comments'], 			'item[code][modperms][caneditpicturecomments]', 		$moderator['caneditpicturecomments']);
	 	print_yes_no_row($vbphrase['can_delete_picture_comments'], 			'item[code][modperms][candeletepicturecomments]', 		$moderator['candeletepicturecomments']);
	 	print_yes_no_row($vbphrase['can_remove_picture_comments'], 			'item[code][modperms][canremovepicturecomments]', 		$moderator['canremovepicturecomments']);
	 	print_yes_no_row($vbphrase['can_moderate_picture_comments'], 		'item[code][modperms][canmoderatepicturecomments]', 	$moderator['canmoderatepicturecomments']);

		// new thread/new post email preferences
		print_description_row($vbphrase['email_preferences'], false, 2, 'thead');
		print_yes_no_row($vbphrase['receive_email_on_new_thread'], 			'item[code][modperms][newthreademail]', 				$moderator['newthreademail']);
		print_yes_no_row($vbphrase['receive_email_on_new_post'], 			'item[code][modperms][newpostemail]', 					$moderator['newpostemail']);

		//($hook = vBulletinHook::fetch_hook('admin_moderator_form')) ? eval($hook) : false;
	}
	
	/**
	* Activate an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function activate($purchaseid, $recipient = null, $dorebuild = true)
	{
		global $vbphrase;
		
		require_once(DIR . '/includes/adminfunctions.php');

		// Ensure this is set
		$recipient = (!$recipient ? $this->registry->userinfo : $recipient);
		
		// Ensure this gets ran
		if (!parent::activate($purchaseid, $recipient, $dorebuild))
		{
			// Couldn't activate
			return false;
		}

		// Save the new moderator
		$code['moderatorid'] = $this->saveModerator($recipient['userid']);

		// Update flag
		$this->registry->db->query_write("
			UPDATE " . TABLE_PREFIX . "dbtech_vbshop_purchase
			SET configuration = " . $this->registry->db->sql_prepare(trim(serialize($code))) . "
			WHERE purchaseid = " . intval($purchaseid) . "
		");
		
		// Build purchase cache
		VBSHOP::build_purchases($recipient);		
	}
	
	/**
	* Deactivate an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function deactivate($purchaseid, $recipient = null, $dorebuild = true)
	{
		global $vbphrase;
		
		require_once(DIR . '/includes/adminfunctions.php');

		// Ensure this is set
		$recipient = (!$recipient ? $this->registry->userinfo : $recipient);		
		
		// Ensure this gets ran
		if (!parent::deactivate($purchaseid, $recipient, $dorebuild))
		{
			// Couldn't deactivate
			return false;
		}
		
		$moderator = VBSHOP::$db->fetchRow('
			SELECT moderator.*, user.username
			FROM $moderator AS moderator
			LEFT JOIN $user AS user USING (userid)
			WHERE moderatorid = ?
		', array(
			$this->config['configuration']['moderatorid']
		));
		if (!$moderator)
		{
			// Excluded forum
			return true;
		}

		$moddata =& datamanager_init('Moderator', $this->registry, ERRTYPE_STANDARD);
		$moddata->set_existing($moderator);
		$moddata->delete(true);
		unset($moddata);		
	}

	/**
	* Re-adds a moderartor
	*
	* @return	integer		Moderator ID
	*/	
	protected function saveModerator($userid)
	{
		$moddata =& datamanager_init('Moderator', $this->registry, ERRTYPE_SILENT);
			$moddata->set('userid', $userid);
			$moddata->set('forumid', $this->config['code']['forumid']);
		foreach ($this->registry->bf_misc_moderatorpermissions as $key => $val)
		{
			if ($this->config['code']['modperms'][$key])
			{
				$moddata->set_bitfield('permissions', $key, $val);
			}
		}
		foreach ($this->registry->bf_misc_moderatorpermissions2 as $key => $val)
		{
			if ($this->config['code']['modperms'][$key])
			{
				$moddata->set_bitfield('permissions2', $key, $val);
			}
		}
		$modid = $moddata->save();

		return $modid;
	}	
}
?>