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

class vBShop_Item_createforum extends vBShop_Item_Core
{
	/**
	* The constructor
	*
	* @param	vBulletin	vBulletin registry
	* @param	array		Item info
	*/	
	public function __construct(&$registry, &$item)
	{
		$item['_nodeactivate'] = true;
		parent::__construct($registry, $item);
		
		// Set purchase template
		$this->config['purchasetemplate'] = 'dbtech_vbshop_configure_createforum';
	}
	
	/**
	* 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;
		
		print_forum_chooser($vbphrase['dbtech_vbshop_excluded_forums'], 'item[code][excludedforums][]', $code['excludedforums'], NULL, true, true);
	}
	
	/**
	* Configure an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function configure($purchaseid, $recipient = null)
	{
		global $vbphrase;
		
		if (!$this->can_configure($purchaseid))
		{
			// Can't configure this item
			return false;
		}
		
		// Ensure this is set
		$recipient = (!$recipient ? $this->registry->userinfo : $recipient);
		
		// Get the form code
		$code 			= $this->registry->input->clean_gpc('p', 'code', 			TYPE_ARRAY);
		$noConfigure 	= $this->registry->input->clean_gpc('p', '_noConfigure', 	TYPE_BOOL);

		if ($noConfigure)
		{
			// We're done, we wanted to let the recipient configure it themselves
			return true;
		}
		
		if ($code['title'] AND $code['description'])
		{
			if (in_array($code['parentid'], $this->config['code']['excludedforums']))
			{
				// Excluded forum
				eval(standard_error(fetch_error('dbtech_vbshop_invalid_data', $vbphrase['forum'])));
			}	

			$code['title'] = htmlspecialchars_uni($code['title']);
			$code['description'] = htmlspecialchars_uni($code['description']);

			if ($this->config['configuration']['forumid'] AND $this->registry->forumcache[$this->config['configuration']['forumid']])
			{
				$code['forumid'] = $this->config['configuration']['forumid'];
				$forumdata =& datamanager_init('Forum', $this->registry, ERRTYPE_STANDARD);
					$forumdata->set_existing($this->registry->forumcache[$this->config['configuration']['forumid']]);
					$forumdata->set('title', fetch_censored_text($code['title']));
					$forumdata->set('description', fetch_censored_text($code['description']));
					$forumdata->set('parentid', $code['parentid']);
				$forumdata->save();
			}
			else
			{
				$forum = array(
					'title' => fetch_censored_text($code['title']),
					'description' => fetch_censored_text($code['description']),
					'link' => '',
					'displayorder' => 1,
					'daysprune' => -1,
					'parentid' => $code['parentid'],
					'showprivate' => 0,
					'newthreademail' => '',
					'newpostemail' => '',
					'options' => array(
						'moderatenewpost' => 0,
						'moderatenewthread' => 0,
						'moderateattach' => 0,
						'styleoverride' => 0,
						'canhavepassword' => 1,
						'cancontainthreads' => 1,
						'active' => 1,
						'allowposting' => 1,
						'indexposts' => 1,
						'displaywrt' => 1,
						'canreputation' => 1,
						'allowhtml' => 0,
						'allowbbcode' => 1,
						'allowimages' => 1,
						'allowvideos'  => 1,
						'allowsmilies' => 1,
						'allowicons' => 1,
						'allowratings' => 1,
						'countposts' => 1,
						'showonforumjump' => 1,
			   			'prefixrequired' => 0
					),

					'styleid' => '',
					'password' => '',
					'defaultsortfield' => 'lastpost',
					'defaultsortorder' => 'desc',
		   			'imageprefix' => '',
				);				
				$forumdata =& datamanager_init('Forum', $this->registry, ERRTYPE_STANDARD);
				foreach ($forum as $varname => $value)
				{
					if ($varname == 'options')
					{
						foreach ($value AS $key => $val)
						{
							$forumdata->set_bitfield('options', $key, $val);
						}
					}
					else
					{
						$forumdata->set($varname, $value);
					}
				}
				$code['forumid'] = $forumdata->save();
			}
		}
		
		if ($this->config['notifications_config'])
		{
			$userids = explode(',', $this->config['notifications_config']);
			foreach ((array)$userids as $userid)
			{
				// Send PM
				VBSHOP::sendPM(
					$userid,
					construct_phrase($vbphrase['dbtech_vbshop_new_configuration_x'],
						$this->config['title']
					),
					construct_phrase($vbphrase['dbtech_vbshop_x_just_configured_y'],
						$recipient['username'],
						$this->config['title'],
						$vbphrase['dbtech_vbshop_parent_forum'] . ': ' . ($this->registry->forumcache[$code['forumid']]['title']) . "\n" . 
							$vbphrase['title'] . ': ' . ($code['title'] ? $code['title'] : 'N/A') . "\n" . 
							$vbphrase['description'] . ': ' . ($code['description'] ? $code['description'] : 'N/A')
					)
				);
			}
		}
		
		// 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);				
	}

	/**
	* Form to purchase an item
	*/	
	public function render()
	{
		foreach ($this->registry->forumcache as $forumid => $forum)
		{
			if (in_array($forumid, $this->config['code']['excludedforums']))
			{
				// Skip this
				continue;
			}

			// Create the number option template
			$templater = vB_Template::create('option');
				$templater->register('optionvalue', 	$forumid);
				$templater->register('optiontitle', 	$forum['title']);
				$templater->register('optionselected', 	($forumid == $this->config['configuration']['parentid'] ? 'selected="selected"' : ''));
			$this->config['forumoptions'] .= $templater->render();
		}

		return parent::render();
	}	
}
?>