<?php

/**
 *--------------------------------------
 * 单页
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-19
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class SinglePageModl extends Modl
{
	public function get_singlePageList($where = '', $order = '', $limit = 10)
	{
		$_SPL = $this->where($where)->order($order)->limit($limit)->select();
		if(!empty($_SPL))
		{
			foreach($_SPL as $k => $v)
			{
				/* url 更新 */
				if(empty($v['sp_url']))
				{
					$_r = $this->update_url($v['single_page_id']);
					$_SPL[$k]['sp_url'] = $_r['data'];
				}
			}
		}
		return $_SPL;
	}

	public function get_singlePageInfo($singlePageId)
	{
		$_SPI = $this->where('single_page_id=\''.$singlePageId.'\'')->find();
		if(!empty($_SPI))
		{
			/* url 更新 */
			if(empty($_SPI['sp_url']))
			{
				$_r = $this->update_url($_SPI['single_page_id']);
				$_SPI['sp_url'] = $_r['data'];
			}
		}
		return $_SPI;
	}

	public function add_single_page($data)
	{
		$result = array('data' => '', 'error' => '');

		$_t_id = $this->insert($data);
		if(false === $_t_id)
		{
			$result['error'] = L('ADD_FAILED');
			return $result;
		}
		$result['data'] = $_t_id;

		/* 更新附件 */
		M('Upload')->update_upload($_t_id, ASession::get('member_id'));

		return $result;
	}

	public function edit_single_page($data)
	{
		$result = array('data' => '', 'error' => '');

		if(false === $this->update($data))
		{
			$result['error'] = L('EDIT_FAILED');
			return $result;
		}

		/* 更新附件 */
		M('Upload')->update_upload($data['single_page_id'], ASession::get('member_id'));

		return $result;
	}

	public function delete_single_page($singlePageId)
	{
		$result = array('data' => '', 'error' => '');

		if(false === $this->delete($singlePageId))
		{
			$result['error'] = L('DELETE_FAILED');
			return $result;
		}

		/* 删除上传文件 */
		$_AUL = M('Upload')->where("`u_item_type` = 'single_page' and `u_item_id` = '".$singlePageId."'")->select();
		if(!empty($_AUL))
		{
			foreach($_AUL as $au)
			{
				/* 删除文件 */
				@unlink(realpath($_SERVER['DOCUMENT_ROOT'].$a['u_src']));
			}
		}
		/* 删除上传表信息 */
		M('Upload')->where("`u_item_type` = 'single_page' and `u_item_id` = '".$singlePageId."'")->delete();

		return $result;
	}

	public function update_url($singlePageId)
	{
		$result = array('data' => '', 'error' => '');

		$_SPI = $this->where('single_page_id = \''.$singlePageId.'\'')->find();
		if(empty($_SPI))
		{
			$result['error'] = L('ITEM_NOT_EXSIT');
			return $result;
		}

		$_o_html_switch = M('Option')->get_option('core/html_switch');
		if($_o_html_switch and $_SPI['sp_is_html'])
		{
			$url = __APP__.'/'.$_SPI['sp_html_naming'];
		}
		else
		{
			$url = Url::U('home@single_page/show_single_page?single_page_id='.$singlePageId);
		}
		$this->where('single_page_id = \''.$singlePageId.'\'')->set_field('sp_url', $url);
		$result['data'] = $url;
		return $result;
	}

}


?>