<?php if (!defined('VB_ENTRY')) die('Access denied.');
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 4.0.0 Alpha 5
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2009 Jelsoft Enterprises Ltd. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

/**
 * CMS Section Data Manager
 *
 * @package vBulletin
 * @author vBulletin Development Team
 * @version $Revision: 31871 $
 * @since $Date: 2009-08-25 10:54:54 -0500 (Tue, 25 Aug 2009) $
 * @copyright Jelsoft Enterprises Ltd.
 */
class vBCms_DM_Section extends vBCms_DM_Node
{
	/*******
	* Most of the work is already done for us, but we need to save ,
	* for each of the child nodes- any combination of delete, published, order, and public preview.
	*
	* For all the standard node and nodeinfo content we want to just let the generic
	********/


	/**
	 * vB_Item Class.
	 *
	 * @var string
	 */
	protected $item_class = 'vBCms_Item_Content_Section';

	/**
	 * Whether to reindex the content after an update.
	 *
	 * @var bool
	 */
	protected $index_search = true;

	/** A flag so if we're creating a new record from admincp we can have it
	* NOT save any children for child nodes ***/
	protected $save_children = true;


	/*Save==========================================================================*/

	protected function prepareFields()
	{
		$this->set('contenttypeid', vb_Types::instance()->getContentTypeID("vBCms_Section"));

		if ($this->set_fields['nodeid'])
		{
			$this->item_id = $this->set_fields['nodeid'];
		}
		parent::prepareFields();
	}

	/** allows us to toggle the save_children flag***/
	public function setSaveChildren($this_save)
	{
		$this->save_children = $this_save;
	}

	//Walk the list of entries.
	protected function postSave($result, $deferred, $replace, $ignore)
	{
		if (! $this->save_children)
		{
			return true;
		}

		vB::$vbulletin->input->clean_array_gpc('p', array(
			'ids' => vB_Input::TYPE_ARRAY));

		$orders = array();
		foreach (vB::$vbulletin->GPC['ids'] as $nodeid)
		{
			vB::$vbulletin->input->clean_array_gpc('p', array(
				"cb_preview_$nodeid" => vB_Input::TYPE_INT,
				"cb_delete_$nodeid" => vB_Input::TYPE_INT,
				"order_$nodeid" => vB_Input::TYPE_INT,
				"published_$nodeid" => vB_Input::TYPE_INT
				));

			//If we're deleting we need to instantiate the appropriate data manager
			// and use that to delete. Otherwise we know where the variables are and
			// can just update them directly.
			if (vB::$vbulletin->GPC_exists["cb_delete_$nodeid"])
			{
				if ($record = vB::$vbulletin->db->query_first("SELECT contenttype.class, package.class
				AS package FROM " . TABLE_PREFIX . "cms_node node
				INNER JOIN " . TABLE_PREFIX . "contenttype AS contenttype ON contenttype.contenttypeid = node.contenttypeid
				INNER JOIN " . TABLE_PREFIX . "package AS package ON package.packageid = contenttype.packageid
				 WHERE nodeid = " . $nodeid))
				{
					$item = vB_Item_Content::create($record['package'], $record['class'], $nodeid);
					$dm = $item->getDM();
					$item->loadDM($dm);
					$dm->delete();
				}
			}
			else
			{
				//First the two setting from the node table.
				vB::$vbulletin->db->query_write($sql = "UPDATE " . TABLE_PREFIX . "cms_node
					SET publicpreview = " .(vB::$vbulletin->GPC_exists["cb_preview_$nodeid"] ?
					"1, " : " 0, ") .
					(intval(vB::$vbulletin->GPC["published_$nodeid"]) > 1 ?
					"setpublish = 1, publishdate = (case when setpublish = 0 then " . (TIMENOW - 10000 ) . " else publishdate end) ": " setpublish = 0" ) .
					" WHERE nodeid = $nodeid");
				//Finally the order. This is fairly tricky. If we do the order updates
				// out of sequence, things get scrambled and we are sure to wind up incorrect. So we
				// need to index them and do them in order.

				if (intval( vB::$vbulletin->GPC["order_$nodeid"]))
				{
					$orders[$nodeid] = vB::$vbulletin->GPC["order_$nodeid"];
				}
			}

		}
		asort($orders);
		$min_sequence = 1;

		foreach ($orders as $nodeid => $order)
		{
			$order = max($min_sequence, intval($order));
			$min_sequence++;
			vBCms_ContentManager::setDisplayOrder($this->set_fields['nodeid'], $nodeid, $order);

		}


		return parent::postSave($result, $deferred, $replace, $ignore);
	}

	/********
	 * We need to remove records from cms_category and cms_nodecategory
	 *
	 *********/
	protected function postDelete()
	{
		vB::$vbulletin->db->query_write("DELETE FROM " . TABLE_PREFIX . "cms_nodecategory
		WHERE categoryid in (SELECT categoryid FROM " . TABLE_PREFIX . "cms_category
		WHERE parentnode = " . $this->set_fields['nodeid'] . ") OR nodeid = " . $this->set_fields['nodeid']);


		vB::$vbulletin->db->query_write("DELETE FROM " . TABLE_PREFIX . "cms_category WHERE parentnode =
			" . $this->set_fields['nodeid'] );
	}



}

/*======================================================================*\
|| ####################################################################

|| # CVS: $RCSfile$ - $Revision: 31871 $
|| ####################################################################
\*======================================================================*/