예제 #1
0
파일: section.php 프로젝트: hungnv0789/vhtm
	/**
	 * Creates a new, empty content item to add to a node.
	 *
	 * @param vBCms_DM_Node $nodedm				- The DM of the node that the content is being created for
	 * @return int | false						- The id of the new content or false if not applicable
	 */
	public function createDefaultContent(vBCms_DM_Node $nodedm)
	{
		global $vbphrase;
		require_once DIR . '/includes/functions_databuild.php';
		fetch_phrase_group('cpcms');

		vB::$vbulletin->input->clean_array_gpc('r', array(
			'nodeid' => vB_Input::TYPE_UINT,
			'parentnode' => vB_Input::TYPE_UINT
		));


			//We should have a nodeid, but a parentnode is even better.
		($hook = vBulletinHook::fetch_hook('vbcms_section_defaultcontent_start')) ? eval($hook) : false;

		if ($nodedm->getSet('parentnode'))
		{
			$parentnode = $nodedm->getField('parentnode');
		}
		else
		{
			if ($this->parent_node)
			{
				$parentnode = $this->parent_node;
			}
			else if (vB::$vbulletin->GPC_exists['parentnode'] AND intval(vB::$vbulletin->GPC['parentnode'] ))
			{
				$parentnode = vB::$vbulletin->GPC['parentnode'];
			}
			else if (vB::$vbulletin->GPC_exists['nodeid'] AND intval(vB::$vbulletin->GPC['nodeid'] )
				and $record = vB::$vbulletin->db->query_first("SELECT contenttypeid, nodeid, parentnode FROM " .
				TABLE_PREFIX . "cms_node where nodeid = " . vB::$vbulletin->GPC['nodeid'] ))
			{
				$parentnode = vB_Types::instance()->getContentTypeID("vBCms_Section") == $record['contenttypeid'] ?
					$record['nodeid'] : $record['parentnode'];
			}
			
		}

		if (!is_a($nodedm, 'vBCms_DM_Section'))
		{
			$nodedm = new vBCms_DM_Section();
			$nodedm->set('parentnode', $parentnode);
		}
		$nodedm->set('contenttypeid', vB_Types::instance()->getContentTypeID("vBCms_Section"));
		$nodedm->set('contentid', 0);
		$nodedm->set('item_id', 0);
		
		$title = (vB::$vbulletin->GPC_exists['section_title']?
			vB::$vbulletin->GPC['section_title'] :
			(vB::$vbulletin->GPC_exists['title']?
			vB::$vbulletin->GPC['title'] : $vbphrase['new_section']));
		$nodedm->set('title', $title);
		$nodedm->getValidURL($title);
		$nodedm->set('html_title', $title);

		//set the default configuration.
		$this->config = array();
		$this->config['items_perhomepage'] = 7;
		$this->config['section_priority'] = 2;
		$this->config['content_layout'] = 1;
		$nodedm->set('config', $this->config);

		if (!($nodeid = $nodedm->save()))
		{
			throw (new vB_Exception_Content('Failed to create default content for contenttype ' . get_class($this)));
		}
		($hook = vBulletinHook::fetch_hook('vbcms_section_defaultcontent_end')) ? eval($hook) : false;

		return $nodeid;
	}
예제 #2
0
	public static function fixNodeLR($sectionid = false, $nodeleft = 0, $sectiontypeid = false, $findmissing = true)
	{
		//We pull the list of items that are children of the current node.
		global $vbulletin;
		global $vbphrase;
		$result = '';

		if (! intval($sectiontypeid))
		{
			$sectiontypeid = vB_Types::instance()->getContentTypeID("vBCms_Section");
		}

		//There can be nodes that don't have a valid parent. In that case, we make a
		//"lost and found" section and put them into it. If we do this first, then
		//we will automatically fix it.
		if ($findmissing)
		{
			if ($rst = $vbulletin->db->query_read("SELECT n1.nodeid FROM " .
			TABLE_PREFIX . "cms_node n1 LEFT JOIN " .
			TABLE_PREFIX . "cms_node n2 ON n2.nodeid = n1.parentnode AND n2.nodeid <> n1.nodeid
				AND n2.contenttypeid = $sectiontypeid
 			WHERE n2.nodeid IS NULL AND n1.contenttypeid <> $sectiontypeid;"))
			{
				$orphans = array();
				while($record = $record = $vbulletin->db->fetch_array($rst))
				{
					$orphans[] = $record['nodeid'];
				}
			}

			//Do we have orphans?
			if (count($orphans))
			{
				//We need to make a lost and found folder.
				$record = $vbulletin->db->query_first("SELECT node.nodeid FROM " .
				TABLE_PREFIX . "cms_node AS node WHERE	node.parentnode IS NULL LIMIT 1");
				//Now create a node.
				$vbulletin->GPC_exists['parentnode'] = $vbulletin->GPC_exists['sectionid'] = 1;
				$vbulletin->GPC['parentnode'] = $vbulletin->GPC['sectionid'] = $record['nodeid'];

				if ($content = vBCms_Content::create('vBCms', 'Section'))
				{
					require_once DIR . '/includes/functions_misc.php';
					$nodedm = new vBCms_DM_Section();
					$nodedm->set('parentnode', $record['nodeid']);
					$nodedm->set('contenttypeid', $sectiontypeid);
					vB::$vbulletin->GPC_exists['title'] = 1;
					vB::$vbulletin->GPC['title'] = fetch_phrase('lost_found', 'cpcms');
					// create content handler
					$content = vBCms_Content::create('vBCms', 'Section');
					$content->setParentNode( $record['nodeid']);
					if (! $sectionid = $content->createDefaultContent($nodedm))
					{
							throw (new vB_Exception_DM('Could not create new node for content: ' . print_r($nodedm->getErrors())));
					}
					$result = fetch_phrase('check_lost_found', 'cpcms');
				}
				vB_Cache::instance()->event('sections_updated');
				vB_Cache::instance()->cleanNow();

				//We have everything we need. Let's update
				$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "cms_node SET parentnode = $sectionid, permissionsfrom = 1
				WHERE nodeid IN(" . implode(',', $orphans) . ")");

			}
			self::fixNodeLR(false, 0, $sectiontypeid, false);
			return $result;
		}
		else
		{
			$rst = $vbulletin->db->query_read($sql = "SELECT node.nodeid, contenttypeid FROM " .
			TABLE_PREFIX . "cms_node AS node INNER JOIN " .
			TABLE_PREFIX . "cms_nodeinfo AS info ON info.nodeid = node.nodeid WHERE
			node.parentnode " . (intval($sectionid) ? "= $sectionid" : "IS NULL")  . " ORDER BY nodeleft" );

			$nodes = array();
			while ($record = $vbulletin->db->fetch_array($rst))
			{
				$nodes[] = $record;
			}
			$vbulletin->db->free_result($rst);

			$childnodeleft = intval($nodeleft) + 1;

			if ($sectionid)
			{
				//Find out if we should assign permissionsfrom for this record, or inherit from a parent.
				if ($permission_record = $vbulletin->db->query_first("SELECT * FROM " . TABLE_PREFIX .
					"cms_permissions WHERE nodeid = $sectionid LIMIT 1;" ))
				{
					$permissionsfrom = $sectionid;
				}
				else
				{
					$permission_record = $vbulletin->db->query_first("SELECT permissionsfrom FROM " . TABLE_PREFIX .
					"cms_node WHERE nodeid = $sectionid LIMIT 1;" );
					$permissionsfrom = $permission_record['permissionsfrom'];
				}
			}
			else
			{
				//We are at the root. Our early code created orphan nodecategory records. Let's find and
				// remove any.
				$vbulletin->db->query_write("CREATE TEMPORARY TABLE cms_nc_orphans AS
					select nc.nodeid FROM " . TABLE_PREFIX .
					"cms_nodecategory nc LEFT JOIN " . TABLE_PREFIX .
					"cms_node AS node on node.nodeid = nc.nodeid
						WHERE node.nodeid IS NULL;");

				$vbulletin->db->query_write("DELETE FROM " . TABLE_PREFIX .
					"cms_nodecategory WHERE nodeid IN (SELECT nodeid from cms_nc_orphans);");

				$vbulletin->db->query_write("DROP TEMPORARY TABLE cms_nc_orphans");

				$permissionsfrom = false;
			}

			foreach ($nodes as $node)
			{
				if (intval($node['contenttypeid']) == intval($sectiontypeid))
				{
					if ($permissionsfrom)
					{
						$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "cms_node SET permissionsfrom =
						 $permissionsfrom WHERE nodeid = " . $node['nodeid']);
					}
					$childnodeleft = self::fixNodeLR($node['nodeid'], $childnodeleft, $sectiontypeid, false);
				}
				else
				{
					$rst = $vbulletin->db->query_write($sql = "UPDATE " .
					TABLE_PREFIX . "cms_node SET nodeleft = $childnodeleft, noderight = " .
					($childnodeleft + 1) . ", permissionsfrom = $permissionsfrom WHERE nodeid = " . $node['nodeid'] );
					$childnodeleft += 2;
				}
			}
			if (intval($sectionid))
			{
				$rst = $vbulletin->db->query_write($sql = "UPDATE " .
				TABLE_PREFIX . "cms_node set nodeleft = $nodeleft, noderight = " .
				$childnodeleft . ($permissionsfrom ? ", permissionsfrom = $permissionsfrom" : '') .
				" WHERE nodeid = $sectionid " );
				return $childnodeleft + 1;
			}
		}

		self::fixCategoryLR();

		return $result;
	}