<?php if (!defined('VB_ENTRY')) die('Access denied.');
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 4.0.0 Release Candidate 2                              # ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2009 vBulletin Solutions Inc. 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: 34173 $
 * @since $Date: 2009-12-08 10:31:19 -0600 (Tue, 08 Dec 2009) $
 * @copyright vBulletin Solutions Inc.
 */
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));
		//The parent classes insist on setting new=1, but we want new=0;
		vB::$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "cms_node
		SET new = 0 WHERE nodeid = " . (isset($this->set_fields['nodeid']) ? $this->set_fields['nodeid'] : $this->primary_id));

		$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();
					$dm->delete();
				}
			}
			else
			{
				//Check 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"];
				}

				//We need to save publish and preview status
				$sql = "UPDATE " . TABLE_PREFIX . "cms_node SET setpublish = ";
				//Check the publish status
				if (vB::$vbulletin->GPC_exists["published_$nodeid"] AND (vB::$vbulletin->GPC["published_$nodeid"] == 2))
				{
				 	$sql .= " 1, publishdate = " . TIMENOW;
				}
				else
				{
					$sql .= " 0 ";
				}
				//Check the preview status
				if (vB::$vbulletin->GPC_exists["cb_preview_$nodeid"])
				{
					$sql .= ", publicpreview = 1 WHERE nodeid = $nodeid";
				}
				else
				{
					$sql .= ", publicpreview = 0 WHERE nodeid = $nodeid";
				}
				vB::$vbulletin->db->query_write($sql);
	
			}

		}
		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: 34173 $
|| ####################################################################
\*======================================================================*/