ThinkPHP的controller里变量输出位置不正确导致变量输出无效,原因不明??

下面的代码是使用ThinkPHP的一个controller文件。遇到了一个问题,不知道原因。

在_initialize method里进行变量赋值后,发现在projectDetial所关联的tpl文件projectDetail.html里,此变量赋值不成功。此外,在projectDetail调用的该类下的其他method里进行模板变量赋值也无效。把这些无效的变量输出移到projectDetial后,变量输出生效了。但是,之前,前者是有效的?是什么原因导致现在的状况?

<?php
class ProjectAction extends IndexbaseAction{
	protected $user = array();
	protected $add_integral = 30;	//认领项目成功后该用户增加的积分
	protected $angle_num = 3;	//不同angle认证项目改变项目状态的最低次数
	// 项目状态  0待认证 1待认领 2进行中 3已完成 -1已终止
	protected $status = array(
		0=>'待认证',
		1=>'待认领',
		2=>'进行中',
		3=>'已完成',
		-1=>'已终止',
		);
	protected $help_type = array(1=>'募集捐款',2=>'募集物资',3=>'招募志愿者');
	protected $project = array();
	protected $agency = array();

	public function _initialize(){
		
		 
		//需要检测登录的方法数组
		$login_arr = array(
			'start','starte','done','projectDetail','center','manager','donated','volunteer','volunteer','titbit','fsend','schedule','plan','delPlan','changeConsent','getProjectRecruit','getProjectRecruitInfo','addschedule','editschedule','addNotice');
		if(in_array(ACTION_NAME,$login_arr)){
			//检查用户是否登录
			checkUserInfo();
		}
		
		import('ORG.Util.Cookie');
		import('@.TagLib.TagLibHtml');
		session('[start]');
		$user = cookie('user_info');
		if(!empty($user)){
			$this->assign('user',$user);
		}
		$user_info = getUserInfo();
		$this->user = $user_info;

		$this->assign('user_info',$user_info);
		//如果是机构就取出机构资料
		if($user_info['isAgency'] == 1){
			$where = array(
				'user_id' => $user_info['user_id'],
				'ispass' => 1,
			);
			$agency_info = M('Agency')->where($where)->find();
			$this->agency = $agency_info;
			if(!empty($agency_info['documented'])){
				$agency_info['documented'] = explode(',', $agency_info['documented']);
			}
			$this->assign('agency_info',$agency_info);
		}
		//取得项目详情
		$pid = $this->_get('pid')?$this->_get('pid'):"";

		if(!empty($pid)){
			$this->assign('pid',$pid);
			$where = array(
				'project_id' => $pid,
			);
			$project_info = M('Project')->where($where)->find();
			$this->project = $project_info;
			$this->assign('project_info',$project_info);
		}
		
		
		
		
		
		
		/* 
		$this->sup_list_pic = display_pro_list_sup_pic($is_support,$project_id); */
		
		// var_dump($this->is_support);
		// var_dump($this->is_follow);

		
		
		
		// $this->user['user_id'] = 28;	//测试用

		
	}
	
	public function getVolunteer()
	{
		
		$project_id = 41;	//test
		
		$this->vos = volunteer($project_id);
		
		$this->ajaxReturn($this->vos,"关注成功!",1);
		
	}
	
	public function getMaterial()
	{
		
		$project_id = 41;	//test
		
		$this->vos = material($project_id);
		
		$this->ajaxReturn($this->vos,"关注成功!",1);
		
	}
	
	/*ggh start*/
	
	/**
	 *项目浏览页(已认领)_捐款表输出页面
	 */
	function display_contribution_form()
	{
		$name = 'pagetheme';
		$value = '<span> %totalRow% %header% %nowPage%/%totalPage% 页 </span> %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%';
		$this->__set($name,$value);
		
		$click_name = 'onclick';
		$click_value = 'οnclick="return goto(this,2)"';
		$this->__set($click_name,$click_value);
		
		$model = M('project_donation');
		$where = '';
		$count = $model->count('donation_id');
		// var_dump($count);
		$page = $this->pageBox($count,6);		
		
		
		$list = $model->where($where)->limit($page->firstRow . "," . $page->listRows)->order('addtime DESC')->select();
		
		$page_d = $page->show();
		// var_dump($page_d);
		$this->assign('list_d',$list);
		$this->assign('page_d',$page_d);		
		$this->display();
	}
	
	
	/**
	 *项目浏览页(已认领)_物资捐赠表cdc_project_donation
	 */
	function display_donation_form()
	{		
		$name = 'pagetheme';
		$value = '<span> %totalRow% %header% %nowPage%/%totalPage% 页 </span> %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%';
		$this->__set($name,$value);
		
		$click_name = 'onclick';
		$click_value = 'οnclick="return goto(this,1)"';
		$this->__set($click_name,$click_value);
		
		$model = M('project_donation');
		$where = '';
		$count = $model->count('donation_id');
		// var_dump($count);
		$page = $this->pageBox($count,6);		
		
		
		$list = $model->where($where)->limit($page->firstRow . "," . $page->listRows)->order('addtime DESC')->select();
		
		$page_d = $page->show();
		// var_dump($page_d);
		$this->assign('list_d',$list);
		$this->assign('page_d',$page_d);		
		$this->display();
	}
	
	
	/**
	 *检测某名字的公益机构是否存在
	 */
	function checkAgencyExists()
	{
		$agency_name = $this->_get('agency_name');
		$model = M('agency');
		$num = $model->where("agency_name='$agency_name'")->count();
		if($num>=1)
		{
			//成功后返回客户端新增的用户ID,并返回提示信息和操作状态
			$this->ajaxReturn($num,"通过检测!",1);

		}
		else
		{
			$this->ajaxReturn(0,"不存在此公益机构!",0);
		}
	}
	
	/**
	 *检测某username的爱心使者是否存在
	 */
	function checkAngelExists()
	{
		$username = $this->_get('username');
		$model = M('user');
		$num = $model->where("username='$username' AND isAngel=1")->count();
		if($num>=1)
		{
			//成功后返回客户端新增的用户ID,并返回提示信息和操作状态
			$this->ajaxReturn($num,"通过检测!",1);

		}
		else
		{
			$this->ajaxReturn(0,"不存在此爱心使者!",0);
		}
	}
	
	
	
	//格式化转发次数
	function convertRepostNum()
	{
		$project_id = $this->project_info['project_id'];
		$nums = getRepostNum($project_id);
		// $nums = 50;
		$str = strval($nums);
		$len = strlen($str);
		
		//var_dump($len);
		
		for($i=$len;$i<6;$i++)
		{
			$str = '0' . $str;
		}
		//var_dump($str);
		
		for($j=0;$j<strlen($str);$j++)
		{
			$arr[] = $str[$j];
		}
		//var_dump($arr);
		return $arr;
		
	}
	
	//项目已经认领页面的在线捐款
	function projectDnation()
	{
		
		echo time();
	}
	
	//项目已经认领页面的在线捐物
	function projectMaterail()
	{
		$user_id = $this->user['user_id'];
		$project_id = $this->_post('project_id');
		$name = $this->_post('m_name');
		$phone = $this->_post('m_phone');
		$addtime = time();
		
		$datas = array();
		
		$materials_ids = $this->_post('materials_ids');
		$numbers = $this->_post('numbers');
		
		$sum_num = array_sum($numbers);
		
		$new_model = M();
		// 启动事务:
		$new_model->startTrans(); 
		
		//更新cdc_project中的已收物资数量
		
		$material_model = M('project_materials');
		
		foreach($numbers as $k=>$v)
		{
			$materials_id = $materials_ids[$k];
			$data['user_id'] = $this->user['user_id']?$this->user['user_id']:0;
			$data['project_id'] = $project_id;
			$data['name'] = $name;
			$data['addtime'] = $addtime;
			$data['phone'] = $phone;
			$data['number'] = $v;
			$data['materials_id'] = $materials_id;
			
			$m_data = intval($v);
			
			$m_res = $material_model->where('materials_id=' . $materials_id)->setInc('quantity_received',$m_data);
			
			if(!$m_res)
			{
				// 事务回滚
				$new_model->rollback();
				
				$this->ajaxReturn(0,"捐赠失败1!",0);
			}
			
			$datas[] = $data;
		}
		
		$sql = "INSERT INTO cdc_project_donation (user_id,project_id,name,addtime,phone,number,materials_id) VALUES ";
		foreach($datas as $data)
		{
			$user_id = intval($data['user_id']);
			$project_id = intval($data['project_id']);
			$name = $data['name'];
			$addtime = intval($data['addtime']);
			$phone = intval($data['phone']);
			$number = intval($data['number']);
			$materials_id = intval($data['materials_id']);
			$sql .= "($user_id,$project_id,'$name',$addtime,$phone,$number,$materials_id),";
		}	
		$sql = rtrim($sql,',');
		$model = new Model(); // 实例化一个model对象 没有对应任何数据表		
		$res = $model->query($sql);
		$rows = mysql_affected_rows();
		
		
		
		
		
		
		if($rows)
		{
			// 提交事务
			$new_model->commit();
			$this->ajaxReturn($rows,"捐赠成功!",1);
		}
		else
		{
			// 事务回滚
			$new_model->rollback();

			$this->ajaxReturn(0,"捐赠失败!",0);
		}
	}
	
	//项目已经认领页面的在线报名
	function projectVolunteer()
	{
		// echo time();
		$model = M('project_recruit');
		
		//  在project_recruit模型中启动事务
		$model->startTrans();

		
		$volunteer_id = $this->_post('vo_title');
		$data['volunteer_id'] = $volunteer_id;
		$data['user_id'] = $this->user['user_id']?$this->user['user_id']:0;
		$data['name'] = $this->_post('vo_name');
		$data['phone'] = $this->_post('vo_phone');
		$data['project_id'] = $this->_post('project_id');
		$data['addtime'] = time();

		$res = $model->add($data);
		
		if($res==false)
		{
			// 事务回滚
			$model->rollback(); 
			$this->ajaxReturn(0,"报名失败!",0);
		}
		
		//更新已经报名的数量
		$volunteer_model = M('project_volunteer');
		$res = $volunteer_model->where('volunteer_id=' . $volunteer_id)->setInc('quantity_number'); // 用户的积分加1
		
		if($res)
		{
			// 提交事务
			$model->commit(); 

			$this->ajaxReturn($this->vos,"报名成功!",1);
		}
		else
		{
			// 事务回滚
			$model->rollback(); 
			$this->ajaxReturn(0,"报名失败!",0);
		}

		// echo $model->getLastSQL();
		
	}
	
	
	
	// 发起项目--add
	public function start()
	{		
		$cats = $this->getAllCat();
		
		$this->assign('cats',$cats);
		$this->display ();		
	}
	
	// 发起项目--edit
	public function starte()
	{
		$name=$this->getActionName();
		$model = M ( $name );
		$pk = $model->getPk ();
		$id = $_REQUEST ['pid'];
		$vo = $model->field(true)->where(array($pk=>$id))->find();
		
		$vo['help_type'] = help_type($id);
		
		$materials = material($id);	//项目所需的物资
		$volunteer = volunteer($id);	//项目所需的志愿者		
		$vo['materials'] = $materials;
		$vo['volunteer'] = $volunteer;
		
		
		/*
		// 证明材料和标题图片。这些代码处理只传一张标题图片
		if(empty($vo['title_pic']))
		{
			$title_pics = '';
		}
		else
		{
			$title_pics = explode(',',$vo['title_pic']);
		}
		*/
		// 标题图片
		$title_pic = $vo['title_pic']; 
		$testimonial_materials = explode(',',$vo['testimonial_material']);
		
		
		
		$this->title_pic = $title_pic;
		$this->testimonial_materials = $testimonial_materials;
		
		/**
		 推送类别 1机构 2爱心使者 start
		 */
		$agency_type = 1;
		$angle_type = 2;
		
		$agency_pushes = get_push_data($id,$agency_type);
		
		$angle_pushes = get_push_data($id,$angle_type);
		
		
		$agency_push_names = $this->convertAgencyPushes($agency_pushes);
		$angle_push_names = $this->convertAnglePushes($angle_pushes);
		
		
		$this->agency_push_names = $agency_push_names;
		$this->angle_push_names = $angle_push_names;
		
		/**
		 推送类别 1机构 2爱心使者 end
		 */
		
		
		$cats = getAllCat();	//所有项目分类
		$vo['cats'] = $cats;
		$this->assign ( 'vo', $vo );
		
		$this->display();
	}
	
	/**
	 *将推送记录数组转为公益机构名称
	 *@param array $agency_pushes 推送记录数组
	 *@param array $agency_names 公益机构名称
	 */
	public function convertAgencyPushes($agency_pushes)
	{		
		$agency_names = array();
		foreach($agency_pushes as $v)
		{
			$agency_id = $v['user_id'];			
			$agency_names[] = getAgencyName($agency_id);			
		}
		return $agency_names;		
	}
	
	/**
	 *将推送记录数组转为爱心使者名称
	 *@param array $angle_pushes 推送记录数组
	 *@param array $angle_names 爱心使者名称
	 */
	public function convertAnglePushes($angle_pushes)
	{		
		$angle_names = array();
		foreach($angle_pushes as $v)
		{
			$user_id = $v['user_id'];
			$angle_names[] = getName($user_id);			
		}
		return $angle_names;		
	}
	
	/**
	 *支持项目
	 */
	function projectSupport()
	{
		$model = M('project_support');
		
		
		$data['user_id'] = $this->_post('user_id');
		$data['project_id'] = $this->_post('project_id');
		
		$res = $this->ifSupport($data,$model);
		
		
		
		if($res)
		{			// echo $model->getLastSQL();
			$this->ajaxReturn(0,"你已经支持过了",0);
		}		
		
		$data['addtime'] = time();
		
		$res = $model->add($data);
		
		if($res)
		{
			$this->ajaxReturn($result,"支持成功!",1);
		}
		else
		{			// echo $model->getLastSQL();
			$this->ajaxReturn(0,"你已经支持过了",0);
		}
	}
	
	/**
	 *检测是否已经存在此支持记录
	 *@param array $data
	 *@return boolean true--存在/false--不存在
	 */
	 function ifSupport($data,$model)
	 {
		$is_exists = $model->where($data)->count();
		$res = ($is_exists>0)?true:false;
		return $res;
	 }
	 
	 /**
	 *检测是否已经存在此关注记录
	 *@param array $data
	 *@return boolean true--存在/false--不存在
	 */
	 function ifFollow($data,$model)
	 {
		$is_exists = $model->where($data)->count();
		$res = ($is_exists>0)?true:false;
		return $res;
	 }
	
	/**
	 *关注项目
	 */
	function projectFollow()
	{
		$model = M('project_follow');
		/* 自动插入时间为何失败,要弄清楚
		$auto = array ( 
			array('addtime','time',1,'function') // 对addtime字段在新增的时候使time获取值
		);
		$model-> setProperty("_auto",$auto);
		$model->create();
		$res = $model->add(); */
		
		$data['user_id'] = $this->_post('user_id');
		$data['project_id'] = $this->_post('project_id');
		
		$res = $this->ifSupport($data,$model);
		
		if($res)
		{			// echo $model->getLastSQL();
			$this->ajaxReturn(0,"你已经关注过了",0);
		}
		
		$data['addtime'] = time();
		
		$res = $model->add($data);
		
		if($res)
		{
			$this->ajaxReturn($result,"关注成功!",1);
		}
		else
		{			// echo $model->getLastSQL();
			$this->ajaxReturn(0,"你已经关注过了",0);
		}
	}
	
	/**
	 *取消关注
	 */
	function removeFollow()
	{
		$model = M('project_follow');
		
		$data['user_id'] = $this->user['user_id'];
		$data['project_id'] = $this->_post('project_id');
		
		$res = $model->where($data)->delete(); // 删除数据
		
		if($res)
		{
			$this->ajaxReturn($result,"取消关注成功!",1);
		}
		else
		{			// echo $model->getLastSQL();
			$this->ajaxReturn(0,"取消关注过失败",0);
		}

	}
	
	/**
	 *取消支持
	 */
	function removeSupport()
	{
		$model = M('project_support');		
		
		$data['user_id'] = $this->user['user_id'];
		$data['project_id'] = $this->_post('project_id');
		
		$res = $model->where($data)->delete(); // 删除数据
		// echo $model->getLastSQL();
		
		if($res)
		{
			$this->ajaxReturn($result,"取消支持成功!",1);
		}
		else
		{			// echo $model->getLastSQL();
			$this->ajaxReturn(0,"取消支持失败",0);
		}
	}
	
	/**
	 *处理项目发起页的编辑和新增
	 */
	public function done()
	{		
		// $name=$this->getActionName();
		$model = M('project');
		//  启动事务
		$model->startTrans();

		C('TOKEN_ON',false);		
		
		if(isset($_POST['project_id']))://start{			
			$this->assign('project_id',$_POST['project_id']);
			
			$model->create();
			
			$type = $_POST['type'];
		
			$model->type = implode(",",$type);
			
			$model->starttime = convertTime($_POST['starttime']);
			$model->endtime = convertTime($_POST['endtime']);
			
			$model->user_id = $this->user['user_id'];
			
			
			//标题图片
			$title_pic = $_POST['title_pic']?$_POST['title_pic']:'';
			// $model->title_pic = implode(',',$title_pic);
			$model->title_pic = $title_pic;
			
			//证明材料
			$testimonial_material = $_POST['prove_pic'];
			$model->testimonial_material = implode(',',$testimonial_material);
			
			// var_dump($title_pic);exit;
			
			$res = $model->save();
			if($res !== false)
			{
				$project_id = $_POST['project_id'];
				$res_m = $this->delMaterial($project_id);	//删除项目所需的物资
				$res_v = $this->delVolunteer($project_id);	//删除项目所需的志愿者
				
				$res_p = $this->delPush($project_id);	//删除定向推送记录
				
				if( ($res_m===FALSE)||($res_m===FALSE)||($res_m===FALSE) )
				{
					$model->rollback(); 

				}
				
				$this->add_materials($project_id,$model);		//增加项目所需的物资
				$this->add_volunteers($project_id,$model);		//增加项目所需的志愿者				
				
				$this->addAgencyPushes($project_id,$model);	//批量增加定向公益机构推送记录
				$this->addAngelPushes($project_id,$model);		//批量增加定向爱心使者机构推送记录	
				
				// 提交事务
				$model->commit(); 				
			}
			else
			{
				// $this->error ('更新失败!');
				// 事务回滚
				$model->rollback(); 
				$this->error ('project更新失败!');
			}
		else:	//发起项目新增
		
		if (false === $model->create ()) {
			// 事务回滚
			$model->rollback(); 
			$this->error ( $model->getError () );
		}
		
		$model->addtime = time();
		$type = $_POST['type'];
		
		$model->type = implode(",",$type);
		
		$model->starttime = convertTime($_POST['starttime']);
		$model->endtime = convertTime($_POST['endtime']);
		
		$model->user_id = $this->user['user_id'];
		
		//标题图片
		$title_pic = $_POST['title_pic'];
		$model->title_pic = implode(',',$title_pic);
		
		//证明材料
		$testimonial_material = $_POST['prove_pic'];
		$model->testimonial_material = implode(',',$testimonial_material);
		
		
		
		
		//保存当前数据对象
		$list=$model->add();		
		if ($list!==false) { //保存成功
			//插入项目所需的物资 insert table cdc_project_materials
			$project_id = $list;
			$this->assign('project_id',$project_id);
			
			$this->add_materials($project_id);		
			//插入项目所需的志愿者 insert table cdc_project_volunteer			
			$this->add_volunteers($project_id);
			
			$this->addAgencyPushes($project_id,$model);	//批量增加定向公益机构推送记录
			$this->addAngelPushes($project_id,$model);		//批量增加定向爱心使者机构推送记录
			
			// 提交事务
			$model->commit();
			
		} else {
			//失败提示
			// $this->error ('新增失败!');
			// 事务回滚
			$model->rollback(); 
			$this->error ('project新增失败!');
		}
		endif;	//end}
		
		
		
		
		$this->display();
	}
	
	/**
	 *批量增加定向公益机构推送记录
	 */
	 function addAgencyPushes($project_id,$p_model)
	 {		
		$type = 1;		
		$name_arr = $this->_post('agency_name');
		foreach($name_arr as $name)
		{
			$agency_id = get_agency_id($name);
			if(!$agency_id)
			{
				// 事务回滚
				// var_dump($p_model);exit;
				$p_model->rollback(); 
				$this->error('不存在名为【' . $name . '】的公益机构');
			}
			$this->addPush($project_id,$type,$name);
		}
		
	 }
	 
	 /**
	 *批量增加定向爱心使者机构推送记录
	 */
	 function addAngelPushes($project_id,$p_model)
	 {
		$type = 2;		
		$name_arr = $this->_post('angle_name');
		foreach($name_arr as $name)
		{
			$agency_id = get_user_id($name);
			if(!$agency_id)
			{
				// 事务回滚
				$p_model->rollback(); 
				$this->error('不存在名为【' . $name . '】的爱心使者');
			}
			$this->addPush($project_id,$type,$name);
		}
	 }
	/**
	 *增加定向推送记录--公益机构/爱心使者--start.html
	 *@param $data_arr 公益机构或爱心使者的数据
	 *@param integer $type 1机构 2爱心使者
	 *@param string $name 公益机构或爱心使者的name
	 */
	function addPush($project_id,$type,$name,$p_model)
	{
		if($type==2)
		{				
			$user_id = get_user_id($name);
		}
		elseif($type==1)
		{
			$user_id = get_agency_id($name);
		}
		$model = M('project_push');
		$data['user_id'] = $user_id;
		$data['type'] = $type;
		$data['project_id'] = $project_id;		
		$res = $model->add($data);
		if($res == FALSE)
		{
			// $this->error ('新增失败!');
			// var_dump(mysql_error());
			// 事务回滚
			$p_model->rollback(); 
			$this->error ('push新增失败!');
		}
	}
	
	/**
	 *删除定向推送记录--公益机构/爱心使者--start.html
	 *@param integer $project_id 项目ID
	 */
	function delPush($project_id)
	{
		$model = M('project_push');
		$res = $model->where("project_id=$project_id")->delete(); // 删除project_id为5的用户数据
		return $res;
	}
	
	/**
	 *@author ggh
	 *@description 项目详情页的机构认领项目
	 */
	function doClaim()
	{
		$msg = '认领失败';
		$msg_success = '认领成功';
		$project_id = $_POST['project_id'];
		// var_dump($project_id);exit;
		// echo time();exit;
		if(empty($project_id))
		{			
			//错误后返回错误的操作状态和提示信息
			// echo 1;
			$this->ajaxReturn(0,$msg,0);

		}
		
		
		// 项目状态  0待认证 1待认领 2进行中 3已完成 -1已终止
		$add_integral = $this->add_integral;	//认领项目后机构所获得的积分
		$stauts = 2;	//认领之后,就是进行中。需要用到事务
		
		$this->assign('add_integral',$add_integral);
		
		$project_model = M('project');
		
		//  启动事务
		$project_model->startTrans();
		
		$data_status = array();		
		
		//change the status of the agency
		
		$conditon['project_id'] = $project_id;
		$status_res = $project_model->where($conditon)->setField('status',$stauts);
		if($status_res === false)
		{
			// echo $project_model->getLastSQL();
			$project_model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,$msg,0);
		}
		
		//update table cdc_project claim_agencyid
		//此处比较纠结
		$user_id = $this->user['user_id'];
		// var_dump($user_id);exit;
		$claim_agencyid = $this->getAgencyId($user_id);
		// var_dump($claim_agencyid);
		
		
		
		$status_agencyid = $project_model->where($conditon)->setField('claim_agencyid',$claim_agencyid);
		if($status_agencyid === false)
		{
			// echo $project_model->getLastSQL();
			$project_model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,$msg,0);
		}
		
		//update table cdc_user integral		
		$user_model = M('user');
		$condition_user['user_id'] = $user_id;
		$res_user = $user_model->where($condition_user)->setInc('integral',$add_integral); // 用户的积分增加
		// var_dump($res_user);
		
		if($res_user === false)
		{
			// echo $user_model->getLastSQL();
			$project_model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,$msg,0);
		}
		else
		{
			//成功后返回客户端新增的用户ID,并返回提示信息和操作状态
			 // 提交事务
			$project_model->commit(); 
			$this->ajaxReturn($res_user,$msg_success,1);
		}
		

	}
	
	/**
	 *根据user_id获取agency_id
	 *@param integer $user_id 用户ID
	 *@return integer $agency_id 机构ID
	 */
	function getAgencyId($user_id)
	{
		$agency_model = M('agency');
		$claim_agencyid = $agency_model->where(array('user_id'=>$user_id))->getField('agency_id');
		// echo $agency_model->getLastSQL();
		return $claim_agencyid;
	}
	
	
	/**
	 *发起项目提交之后,查看“详情页面”
	 */
	function projectDetail()
	{
		$project_id = $this->_get('pid');		
		
		$project_rec = get_rec_project();	//获取推荐项目
		$this->assign('project_rec',$project_rec);
		
		$repost_nums = $this->convertRepostNum();		//项目转发次数		
		
		$this->assign('repost_nums',$repost_nums);		
		
		
		$titbits = get_titbits($project_id);
		if($titbits == false)
		{
			$titbits = array();
		}
		
		$this->titbits = $titbits;
		// var_dump($titbits);exit;
		
		
		if(!isset($project_id))
		{
			$project_id = $this->_get('project_id');
		}
		
		//最新情况
		$this->notice = get_new_notice($project_id);
		
		$this->project_message($project_id);
		
		$this->assign('project_id',$project_id);

		$this->assign('add_integral',$this->add_integral);
		
		// var_dump($project_id);exit;
		$model = M('project');
		$condition['project_id'] = $project_id;
		$pDetail = $model->where($condition)->find();
		$pDetail['isSupport'] = checkSupport($pDetail['project_id']);
		$pDetail['isFollow'] = checkFollow($pDetail['project_id']);
		// var_dump($pDetail);exit;
		
		
		//证明材料,多张图片
		$testimonial_material = $pDetail['testimonial_material']?explode(',',$pDetail['testimonial_material']):'';
		$this->testimonial_material = $testimonial_material;
		
		$this->assign('pDetail',$pDetail);
		
		//执行情况
		if(($pDetail['status']==2) || ($pDetail['status']==3))
		{
			$this->notices = get_pro_notice($project_id);
		}
		
		$cat_id = $pDetail['cat_id'];
		$other_projects = get_project_of_cat($cat_id,$project_id);
		// var_dump($other_projects);exit;
		$this->assign('other_projects',$other_projects);
		
		$person_sum = $this->get_person_sum($project_id);
		$this->assign('person_sum',$person_sum);
		
		$materials = material($project_id);	//项目所需的物资
		$volunteer = volunteer($project_id);	//项目所需的志愿者
		
		
		
		$this->materials = $materials;
		$this->volunteer = $volunteer;
		
		
		$this->project_message($project_id);
		
		$this->identifyList();
		
		
		// 已认领详情页初次加载页面时展示的数据
		$randProData = $this->getRandProCo();
		$this->assign('randProData',$randProData);
		
		
		
		
		
		$this->display();
	}
	
	/**
	 *认证
	 */
	function identify()
	{
		$user_id = $this->user['user_id'];
		
		$project_id = $this->_post('project_id');
		$angle_num = $this->_post('identify_angle_num');
		$indentify_agency_num = $this->_post('indentify_agency_num');
		$add_time = strtotime($this->_post('add_time'));
		$pic = $this->_post('pic');
		$remark = $this->_post('remark');
		
		if(isAngle($user_id))
		{
			$this->angleIdentify($user_id,$project_id,$angle_num,$add_time,$pic,$remark);
		}
		elseif(isAgency($user_id))
		{
			$this->agencyIdentify($user_id,$project_id,$add_time,$pic,$remark,$indentify_agency_num);
		}
		else
		{
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"只有爱心使者和机构才能认证!",0);
		}
		
	}
	
	
	/**
	 *项目认证详情内容.当前2条数据一页
	 */
	function identifyList()
	{
		$project_id = $this->_get('pid')?$this->_get('pid'):"";
		if(empty($project_id)){
			$this->error('无效的PID参数');
		}
		$model = M('project_auth');
		$order['addtime'] = 'desc';	
		$map['project_id'] = $project_id;
		
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		// $list = $model->where('status=1')->order('create_time')->page($_GET['p'].',25')->select();
		$page_size = 2;
		$current_page = isset($_GET['p'])?$_GET['p']:0;
		$list = $model->where($map)->order($order)->page($current_page . ',' . $page_size)->select();
		
		// echo $model->getLastSQL();
		// var_dump($list);exit;	
		
		// var_dump($list);
		/* 
		; */
		$this->assign('plist_identify',$list);// 赋值数据集
		import("ORG.Util.PageMessage");// 导入分页类
		// $count      = $model->where('status=1')->count();// 查询满足要求的总记录数
		$count      = $model->where($map)->count();// 查询满足要求的总记录数
		$Page       = new PageMessage($count,$page_size);// 实例化分页类 传入总记录数和每页显示的记录数
		
		// $Page->url = __ROOT__ . '/Project/project_message/project_id/' . $project_id . '/p/';
		
		
		
		//分页跳转的时候保证查询条件
		foreach($map as $key=>$val)
		{
			$Page->parameter   .=   "$key=".urlencode($val).'&';
		}
		
		
		
		

		
		$show       = $Page->show();// 分页显示输出
		// var_dump($show);
		$this->assign('page_identify',$show);// 赋值分页输出
	}
	
	
	
	
	/**
	 *机构认证
	 *@param integer $user_id 认证该项目的用户的user_id
	 *@param integer $project_id 项目ID
	 */
	function agencyIdentify($user_id,$project_id,$add_time,$pic,$remark,$indentify_agency_num)
	{
		$model = M('project');
		
		//  在model模型中启动事务
		$model->startTrans();
		$data = array('status'=>1);
		$condition_project['project_id'] = $project_id;
		
		//indentify_agency_num等于0时表明该项目未被认证过
		
		// var_dump($indentify_agency_num);exit;
		if($indentify_agency_num == 0)
		{
			$res = $model->where($condition_project)->setField($data);
			if(!$res)
			{
				// 事务回滚
				$model->rollback(); 
				//错误后返回错误的操作状态和提示信息
				$this->ajaxReturn(0,"认证失败--1!",0);
			}
		}
		
		
		$res = $model->where($condition_project)->setInc('indentify_agency_num',1); // indentify_agency_num加1

		
		if(!$res)
		{
			// 事务回滚
			$model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"认证失败--2!",0);
		}
		
		
		//update table cdc_user integral		
		$user_model = M('user');
		$condition_user['user_id'] = $user_id;
		$res_user = $user_model->where($condition_user)->setInc('integral',$this->add_integral); // 用户的积分增加
		// var_dump($res_user);
		
		if($res_user === false)
		{
			// echo $user_model->getLastSQL();
			$model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"认证失败--u!",0);
		}		
		
		$res = $this->addProjectAuth($project_id,$user_id,$add_time,$pic,$remark);		
		
		if($res)
		{
			 // 提交事务
			$model->commit(); 
			$this->ajaxReturn($res,"认证成功!",1);

		}
		else
		{
			
			// 事务回滚
			$model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"认证失败--3!",0);

		}
	}
	
	/**
	 *angle认证
	 *@param integer $user_id 认证该项目的用户的user_id
	 *@param integer $project_id 项目ID
	 *@param integer $angle_num angle认证项目的次数
	 */
	function angleIdentify($user_id,$project_id,$angle_num,$add_time,$pic,$remark)
	{
		// 防止同一个angle对同一个project多次认证
		$res = $this->authIsExist($user_id,$project_id);
		if($res)
		{
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"你已经认证过了",0);
		}	
		
		$model = M('project');
		//  在model模型中启动事务
		$model->startTrans();
		$data = array('status'=>1);
		$condition_project['project_id'] = $project_id;
		if($angle_num==($this->angle_num-1))
		{
			$res = $model->where($condition_project)->setField($data);
			if(!$res)
			{
				// 事务回滚
				$model->rollback(); 
				//错误后返回错误的操作状态和提示信息
				$this->ajaxReturn(0,"认证失败!",0);
			}
		}		
		$res = $model->where($condition_project)->setInc('identify_angle_num',1); // identify_angle_num加1
		if(!$res)
		{
			// 事务回滚
			$model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"认证失败!",0);
		}
		
		
		//update table cdc_user integral		
		$user_model = M('user');
		$condition_user['user_id'] = $user_id;
		$res_user = $user_model->where($condition_user)->setInc('integral',$this->add_integral); // 用户的积分增加
		// var_dump($res_user);
		
		if($res_user === false)
		{
			// echo $user_model->getLastSQL();
			$model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"认证失败!",0);
		}
		
		$res = $this->addProjectAuth($project_id,$user_id,$add_time,$pic,$remark);
		
		if($res)
		{
			 // 提交事务
			$model->commit(); 
			$this->ajaxReturn(1,"认证成功!",1);

		}
		else
		{
			// 事务回滚
			$model->rollback(); 
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"认证失败!",0);

		}
	}
	
	/**
	 *insert into cdc_project_auth
	 *@param integer $project_id 项目ID
	 *@param integer $user_id 用户ID
	 *@param integer $add_time 认证时间
	 *@param string $pic 上传图片的地址
	 *@param string $remark 认证备注
	 *@return mixed $res 执行insert后的返回值
	 */
	function addProjectAuth($project_id,$user_id,$add_time,$pic,$remark)
	{
		$auth_model = M('project_auth');
		$auth_data['project_id'] = $project_id;
		$auth_data['user_id'] = $user_id;
		$auth_data['remark'] = $remark;
		$auth_data['pic'] = $pic;
		$auth_data['addtime'] = $add_time;
		$res = $auth_model->add($auth_data);
		// echo $auth_model->getLastSQL();
		// var_dump($res);
		return $res;
	}
	
	/**
	 *检测cdc_project_auth中是否存在某用户对某项目的认证记录
	 *@param integer $user_id 用户ID
	 *@param integer $project_id 项目ID
	 *@return boolean true--存在/false--不存在
	 */
	function authIsExist($user_id,$project_id)
	{
		$model = M('project_auth');
		$condition['user_id'] = $user_id;
		$condition['project_id'] = $project_id;
		$num = $model->where($condition)->count();
		if($num>=1)
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	
	/**
	 *提交花絮 
	 */
	 // function doMessage($project_id,$project_userid,$project_agencyid,$user_id,$content)
	 function doTitbit()
	 {
				
		// echo time();exit;
		// 项目花絮表
		$model = M('titbit');
		
		$model->startTrans(); 
		
		
		
		$model->create();
		$model->addtime = time();
		
		
		$pk = $model->add();
		// echo $model->getLastSQL();exit;
		
		
		
		if(!$pk)
		{			
			$model->rollback(); 
			$this->error('发布失败');
		}		
		
		// var_dump($pk);
		
		//使用事务,没有commit之前,能获取最新插入数据的primary key吗?
		$titbit_id = $pk;
		
		$albums = $this->_post('card_pic');
		
		
		if(!empty($albums))
		{
			$sql = "INSERT INTO cdc_titbit_album (titbit_id,images) VALUES ";
			foreach($albums as $album)
			{
				$sql .= " ($pk,'$album'),";
			}
			// var_dump($sql);
			
			
			$sql = rtrim($sql,',');
			
			
			// var_dump($sql);exit;
			
			
			$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
			$res = $Model->query($sql);
			// echo $Model->getLastSQL();EXIT;
			
			
			$nums = mysql_affected_rows();		
			
			if(!$nums)
			{
				// $this->ajaxReturn(0,"发布失败!",0);
				//错误页面的默认跳转页面是返回前一页,通常不需要设置
				$model->rollback(); 
				$this->error('发布失败2');
			}
		}
		
		
		$model->commit();
		//设置成功后跳转页面的地址,默认的返回页面是$_SERVER['HTTP_REFERER']
		
		$this->success('发布成功');
		
	 }
	
	/**
	 *提交花絮 
	 */
	 // function doMessage($project_id,$project_userid,$project_agencyid,$user_id,$content)
	 function doTitbit2()
	 {
				
		// 项目留言表
		$model = M('titbit');
		
		$data['project_id'] = $this->_post('project_id');
		$data['user_id'] = $this->user['user_id'];
		$data['content'] = $_POST['message_content2'];
		$data['addtime'] = time();
		
		$res = $model->add($data);
		// echo $model->getLastSQL();exit;
		
		if($res)
		{			
			//设置成功后跳转页面的地址,默认的返回页面是$_SERVER['HTTP_REFERER']
			$this->success('新增成功');
		}
		else
		{
			// $this->ajaxReturn(0,"发布失败!",0);
			//错误页面的默认跳转页面是返回前一页,通常不需要设置
			$this->error('新增失败');
		}
	 }
	
	
	
	
	/**
	 *提交项目留言 
	 */
	 // function doMessage($project_id,$project_userid,$project_agencyid,$user_id,$content)
	 function doMessage()
	 {
				
		// 项目留言表
		$model = M('project_comment');
		
		$auto = array(
			array('addtime','time',1,'function'), // 对create_time字段在更新的时候写入当前时间戳
		);
		
		$model-> setProperty("_auto",$auto);
		$res = $model->create();
		$res = $model->add();
		
		if($res)
		{
			$msg = '留言成功。';
		}
		else
		{
			$msg = '留言失败。';
		}
		echo  $msg;
	 }
	 
	 /**
	  *@author ggh
	  *@description 项目留言,分页
	  *@param integer $project_id 项目ID
	  */
	  function project_message($project_id)
	  {
		
		$project_id = $this->_get('pid')?$this->_get('pid'):"";
		if(empty($project_id)){
			$this->error('无效的PID参数');
		}
		$model = M('project_comment');
		$order['addtime'] = 'desc';	
		$map['project_id'] = $project_id;
		
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		// $list = $model->where('status=1')->order('create_time')->page($_GET['p'].',25')->select();
		//$page_size = 2;
		//$current_page = isset($_GET['p'])?$_GET['p']:0;
		//$list = $model->where($map)->order($order)->page($current_page . ',' . $page_size)->select();
		
		// echo $model->getLastSQL();
		// var_dump($list);exit;	
		
		// var_dump($list);
		/* 
		; */
		//$this->assign('plist',$list);// 赋值数据集
		//import("ORG.Util.PageMessage");// 导入分页类
		// $count      = $model->where('status=1')->count();// 查询满足要求的总记录数
		//$count      = $model->where($map)->count();// 查询满足要求的总记录数
		//$Page       = new PageMessage($count,$page_size);// 实例化分页类 传入总记录数和每页显示的记录数
		
		// $Page->url = __ROOT__ . '/Project/project_message/project_id/' . $project_id . '/p/';
		
		
		
		
		//分页跳转的时候保证查询条件
		/* foreach($map as $key=>$val)
		{
			$Page->parameter   .=   "$key=".urlencode($val).'&';
		}
		 */
		$count = $model->where($map)->count();
		$page = $this->page($count,10);
		$list = $model->where($map)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
		// var_dump($list);
		$this->assign('plist',$list);
		$this->assign('page',$page->show());
		
		

		
		//$show       = $Page->show();// 分页显示输出
		// var_dump($show);
		//$this->assign('page',$show);// 赋值分页输出
	  }
	
	
	/**
	 *@author ggh
     *@description 获取某个项目的参捐人总数
     *@param integer $project_id 项目ID
     *@return integer $res 某个项目的参捐人总数
     */
    function get_person_sum($project_id)
   {
	   // 项目捐款表
	   $table_money = 'cdc_project_contribution';
	   $sum_moeny = $this->getOnePersonSum($table_money,$project_id);
	   
	   // 物资捐赠表
	   $table_material = 'cdc_project_donation';
	   $sum_material = $this->getOnePersonSum($table_material,$project_id);
	   
	   // 志愿者报名表
	   $table_volunteer = 'cdc_project_recruit';
	   $sum_volunteer = $this->getOnePersonSum($table_volunteer,$project_id);
	   
	   $res = $sum_moeny + $sum_material + $sum_volunteer;
	   
	   return $res;	   
	
    }
	
	
	/**
	 *获取某个捐赠项目的参捐人总数
	 *@param object $table table name
	 *@param integer $project_id 项目ID
	 *@return integer $sum 某个捐赠项目的参捐人总数
	 */
	function getOnePersonSum($table,$project_id)
	{		
		
		$Model = new Model();// 实例化一个model对象 没有对应任何数据表
		$res = $Model->query("SELECT COUNT(DISTINCT(user_id)) AS sum FROM $table WHERE project_id=$project_id LIMIT 1");
		
		if($res == false)
		{
			$sum = 0;
		}
		else
		{
			$sum = $res[0]['sum'];
		}
		
		return $sum;
	}
	
	/**
	 *@author ggh
	 *@description 项目列表页
	 */
	function projectList()
	{
		$model = M('project');
		$map = array();
		// var_dump($_POST);
		
		// 爱心快递
		$love_data = get_new_money();
		$this->assign('love_data',$love_data);
		
		// var_dump($res);exit;
		
		
		$this->pList();
		
		
		
		$this->assign('pstatus',$this->status);
		$this->assign('htype',$this->help_type);
		$this->display();
	}
	
	/**
	 *@author ggh
	 *@description 获取项目列表的的项目数据
	 */
	function pList()
	{		
		$model = M('project'); // 实例化project对象
		$page_size = 10;	//每页10条数据
		$map = array();
		
		
		
		$type2 = $this->_get('t');	//爱心捐款等分类
		
		$starttime = strtotime($this->_post('starttime'));
		$endtime = strtotime($this->_post('endtime'));
		
		$province = $this->_post('province');
		$city = $this->_post('city');
		
		$status = $this->_post('status');
		$type = $this->_post('type');
		
		// var_dump($status);
		if(!empty($type2))
		{
			$type = $type2;
			$map['status'] = 2;		//在初始状态时选出已认领未完成项目,即进行中,status=2
		}
		
		if(!(empty($starttime)||empty($endtime)))
		{
			$map['addtime']  = array('between',"$starttime,$endtime");
		}
		
		if(!empty($province))
		{
			$map['province']  = $province;
		}
		if(!empty($city))
		{
			$map['city']  = $city;
		}
		// 此处比较纠结
		if(isset($status)&&($status!==''))
		{
			$map['status']  = $status;
		}
		if(!empty($type))
		{			
			$map['type']  = array('like',"%$type%");
		}
		if(!empty($type2) && $type == 4){	//已认领未完成
			// $map['type']  = array('like',"%2%");
			$map['status'] = 2;
			unset($map['type']);
		}
		$t_g = $this->_get('t');
		$t_p = $this->_post('type');
		if(isset($t_g))
		{
			$t = $t_g;
		}
		elseif(isset($t_p))
		{
			$t = $t_p;
		}
		else
		{
			$t = '';
		}
		$name = $this->_post('name')?$this->_post('name'):"";
		if(!empty($name)){
			$map['title'] = array('like',"%{$name}%");
		}
		
		$this->assign('t',$t);
		
		
		
		
		$order['project_id'] = 'desc';
		
		
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		// $list = $model->where('status=1')->order('create_time')->page($_GET['p'].',25')->select();
		$current_page = isset($_GET['p'])?$_GET['p']:0;
		$list = $model->where($map)->order($order)->page($current_page . ',' . $page_size)->select();
		// echo $model->getLastSQL();
		// var_dump($list);//exit;
		
		$list = $this->addPerToList($list);
		// var_dump($list);exit;
		/* 
		; */
		$this->assign('plist',$list);// 赋值数据集
		import("ORG.Util.Page");// 导入分页类
		// $count      = $model->where('status=1')->count();// 查询满足要求的总记录数
		$count      = $model->where($map)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
		
		//分页跳转的时候保证查询条件
		foreach($map as $key=>$val)
		{
			$Page->parameter   .=   "$key=".urlencode($val).'&';
		}

		
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		// $this->display(); // 输出模板

	}
	
	/**
	 *删除项目所需的物资记录
	 *@param integer $project_id 项目ID
	 */
	function delMaterial($project_id)
	{
		$model = M("project_materials"); // 实例化material对象
		$res = $model->where("project_id=$project_id")->delete(); // 删除project_id为$project_id的数据
		return $res;
	}
	
	/**
	 *删除项目所需的志愿者记录
	 *@param integer $project_id 项目ID
	 */
	function delVolunteer($project_id)
	{
		$model = M("project_volunteer"); // 实例化project_volunteer对象
		$res = $model->where("project_id=$project_id")->delete(); // 删除project_id为$project_id的数据
		return $res;
	}
	
	/**
	 *批量新增项目所需的物资
	 *@param integer $project_id 项目ID
	 *@param object $model_p M('project')
	 */
	function add_materials($project_id,$model_p)
	{
		$res = FALSE;
		$arr_name = $_POST['material_name'];
		$arr_guide = $_POST['material_guide'];
		$arr_quantity_required = $_POST['material_quantity_required'];
		$data_materials = $this->f2s_material($project_id,$arr_name,$arr_guide,$arr_quantity_required);
		foreach($data_materials as $mv)
		{			
			$res = $this->add_material($mv);
			if($res==FALSE)
			{
				// $this->error ('新增失败!');
				// var_dump(mysql_error());
				$model_p->rollback();
				$this->error ('materials新增失败!');
			}
		}
	}
	
	/**
	 *批量新增项目所需的志愿者
	 *@param integer $project_id 项目ID
	 *@param object $model_p M('project')
	 */
	function add_volunteers($project_id,$model_p)
	{
		$res = FALSE;
		$arr_name = $_POST['volunteer_name'];
		$arr_skill = $_POST['volunteer_skill'];
		$arr_number = $_POST['volunteer_number'];
		$arr_endtime = $_POST['volunteer_endtime'];
		$data_volunteers = $this->f2s_volunteer($project_id,$arr_name,$arr_skill,$arr_number,$arr_endtime);	
		foreach($data_volunteers as $vv)
		{
			$res = $this->add_volunteer($vv);
			if($res==FALSE)
			{
				// $this->error ('新增失败!');
				// var_dump(mysql_error());
				$model_p->rollback();
				$this->error ('volunteers新增失败!');
			}
		}
	}
	
	/**
	 *新增项目所需的物资 cdc_project_materials
	 */
	function add_material($data)
	{
		$model = M("project_materials"); // 实例化material对象		
		$res = $model->add($data);
		return $res;

	}
	
	/**
	 *新增项目所需的志愿者 table 
	 */
	function add_volunteer($data)
	{
		$model = M("project_volunteer"); // 实例化project_volunteer对象		
		$res = $model->add($data);
		return $res;

	}
	
	
	/**
	 *update table cdc_volunteer
	 */
	function update_material($data)
	{
		$model = M("project_materials"); // 实例化material对象
		// 要修改的数据对象属性赋值		
		$res = $model->where('materials_id=' . $data['materials_id'])
			->data($data)->save(); // 根据条件保存修改的数据
		return $res;
	}
	
	/**
	 *update table cdc_volunteer
	 */
	function update_volunteer($data)
	{
		$model = M("project_volunteer"); // 实例化volunteer对象
		// 要修改的数据对象属性赋值		
		$res = $model->where('volunteer_id=' . $data['volunteer_id'])
			->save($data); // 根据条件保存修改的数据
		// $res = $model->save($data);
		return $res;
	}
	
	
	/**
	 *转换项目所需物资数据
	 *@param integer $project_id 物资所属的项目的ID
	 *@param 
	 */
	function f2s_material($project_id,$arr_name,$arr_guide,$arr_quantity_required)
	{
		$res = array();
		foreach($arr_name as $k=>$v)
		{
			$res[] = array(
				'project_id' => $project_id,
				'name' => $v,
				'guide' => $arr_guide[$k],
				'quantity_required' => $arr_quantity_required[$k],				
			);
		}
		return $res;
		
	}
	
	/**
	 *转换项目所需志愿者
	 *@param integer $project_id 需要志愿者的项目的ID
	 *@param array $arr_name 岗位名称
	 *@param array $arr_skill 要求资历 / 技能
	 *@param array $arr_number 名额
	 *@param array $arr_endtime 截止报名日期
	 */
	function f2s_volunteer($project_id,$arr_name,$arr_skill,$arr_number,$arr_endtime)
	{
		$res = array();
		foreach($arr_name as $k=>$v)
		{
			$res[] = array(
				'project_id' => $project_id,
				'name' => $v,
				'skill' => $arr_skill[$k],
				'number' => $arr_number[$k],	
				'endtime' => convertTime($arr_endtime[$k]),
			);
		}		
		
		return $res;
	}
	
	/**
	 *所有项目分类
	 */
	public function getAllCat()
	{
		$model = M('project_category');
		$list = $model->order('addtime')->select();		
		return $list;

	}
	
	/**
	 *@author ggh
	 *@description 获取项目已经收到的物资等与所需的物资等的百分比
	 *@param integer $project_id
	 *@return array $per_array 百分比
	 *  'percent' => 
        array (size=3)
          'm_per' => float 0
          'v_per' => float 0
          'mo_per' => float 0
	 */
	function getPerArr($project_id)
	{
		$res = array();
		$m_per = $this->getMaterialPercent($project_id);
		$v_per = $this->getVolunteerPercent($project_id);
		$mo_per = $this->getMoneyPercent($project_id);
		
		$res = array(
			'm_per' => $m_per,
			'v_per' => $v_per,
			'mo_per' => $mo_per,
		);
		
		return $res;
	}
	
	/**
	 *处理项目所需物资等与已经收到的物资等的百分比
	 *小于100时正常显示,大于等于100时显示100
	 */
	function getFormatPercent($amout_required,$amout_received)
	{
		if($amout_received>=$amout_required)
		{
			$percent = 100;
		}
		else
		{
			$percent = round($amout_received/$amout_required,3) * 100;
		}
		return $percent;
	}
	
	
	/**
	 *@author ggh
	 *@description 获取项目所需物资与已经收到的物资的百分比
	 *@param integer $project_id 项目ID
	 *@return mixed $percent 获取项目所需物资与已经收到的物资的百分比
	 */
	function getMaterialPercent($project_id)
	{
		$model = M('project_materials');
		$condition['project_id'] = $project_id;
		$amout_required = $model->where($condition)->sum('quantity_required');
		$amout_received = $model->where($condition)->sum('quantity_received');
		
		$percent = $this->getFormatPercent($amout_required,$amout_received);
		return $percent;
		

	}
	
	
	/**
	 *@author ggh
	 *@description 获取一个项目的所需志愿者的总数量与已经报名的志愿者数量的百分比
	 *@param integer $project_id 项目ID
	 *@return mixed $percent 获取一个项目的所需志愿者的总数量与已经报名的志愿者数量的百分比
	 */
	function getVolunteerPercent($project_id)
	{
		$model = M('project_volunteer');
		$condition['project_id'] = $project_id;
		$amout_required = $model->where($condition)->sum('number');
		$amout_received = $model->where($condition)->sum('quantity_number');
		
		$percent = $this->getFormatPercent($amout_required,$amout_received);
		return $percent;
		

	}
	
	/**
	 *@author ggh
	 *@description 获取一个项目的所需资金的总数量与已经收到的捐款的百分比
	 *@param integer $project_id 项目ID
	 *@return mixed $percent 获取一个项目的所需资金的总数量与已经收到的捐款的百分比
	 */
	function getMoneyPercent($project_id)
	{
		$model = M('project');
		$condition['project_id'] = $project_id;
		$amout_required = $model->where($condition)->getField('required_funds');
		$amout_received = $model->where($condition)->getField('donate_funds');		
		$percent = $this->getFormatPercent($amout_required,$amout_received);
		return $percent;
	}
	
	/**
	 *@ggh
	 *@description 将收到的捐款等与所需捐款等物资加入项目数据
	 *@param array $list 项目数据
	 *@return array $list 加入对应的百分比等数据后的项目数据
	 */
	function addPerToList($list)
	{
		$res = array();
		foreach($list as $k=>$v)
		{
			$project_id = $v['project_id'];
			$percent = $this->getPerArr($project_id);
			$v['percent'] = $percent;
			$list[$k] = $v;
		}
		return $list;
	}
	
	/**
	 *公告列表
	 */
	function proNoList()
	{
		$this->getNoticeList();
		$this->display();
	}
	
	/**
	 *获取公告列表数据
	 */
	function getNoticeList()
	{
		if(isset($_POST['id']))
		{
			$search_type = intval($_POST['type']);
			switch($search_type)
			{
				case 1:
					$map['agency_id'] = $_POST['id'];					
					break;
				case 2:
					$map['project_id'] = $_POST['id'];
					break;
			}
		}
		else
		{
			$map = '';
		}
		$User = M('project_notice'); // 实例化project_notice对象
		import('ORG.Util.Page');// 导入分页类
		$count      = $User->where($map)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,1);// 实例化分页类 传入总记录数和每页显示的记录数
		
		if(!empty($map))
		{
			//分页跳转的时候保证查询条件
			foreach($map as $key=>$val)
			{
				$Page->parameter   .=   "$key=".urlencode($val).'&';
			}
		}


		
		
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $User->where($map)->order('addtime')->limit($Page->firstRow.','.$Page->listRows)->select();
		// var_dump($list);
		$this->assign('nList',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	}
	
	
	
	/**
	 *项目公告搜索关键词提示
	 */
	function noticeKeyWords()
	{
		// 搜索类型
		$type = $this->_get('serach_type');
		$kwd = $this->_get('kwd');
		// print_r($type);exit;
		switch(intval($type))
		{
			case 1:
				$res = $this->keyWordsAgency($kwd);				
				break;
			case 2:
				$res = $this->keyWordsProName($kwd);
				break;				
		}
		if($res)
		{
			//成功后返回客户端新增的用户ID,并返回提示信息和操作状态
			$this->ajaxReturn($res,intval($type),1);
		}
		else
		{
			//成功后返回客户端新增的用户ID,并返回提示信息和操作状态
			$this->ajaxReturn(0,"新增成功222!",0);
		}
	}
	
	/**
	 *项目公告搜索关键词--发布机构
	 */
	function keyWordsAgency($kwd)
	{
		if(empty($kwd))
		{
			return false;
		}
		$model = M('agency');
		$map['agency_name'] = array("like","%$kwd%");
		$res = $model->where($map)->select();
		// echo $model->getLastSQL();
		return $res;

	}
	
	/**
	 *项目公告搜索关键词--项目名称
	 */
	function keyWordsProName($kwd)
	{
		if(empty($kwd))
		{
			return false;
		}
		$model = M('project');
		$map['title'] = array("like","%$kwd%");
		$res = $model->where($map)->select();
		return $res;

	}
	
	/**
	 *转发
	 */
	function repost()
	{
		$model = M('project_repost');
		
		$auto = array(  
            array('addtime','time',Model::MODEL_INSERT,'function'), 
        );           
        $model-> setProperty("_auto",$auto); 
		if(!$model->create())
		{			
			exit($model->getError());
		}
		else
		{
			$model->user_id = $this->user['user_id'];
			// $model->addtime = time();
			$res = $model->add();
			if($res)
			{
				$this->ajaxReturn($res,"转发成功!",1);
			}
			else
			{
				$this->ajaxReturn(0,"转发失败!",0);
			}
		}
	}
	
	/**
	 *花絮获取swf地址和标题
	 */
	function getSwf()
	{
		$url = $this->_get('video_url')?$this->_get('video_url'):'';
		
		if(empty($url))
		{
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"请输入网址!",0);
		}
		$html = $this->getHtml($url);		
		$data = $this->getYoukuSwf($html);
		if($data)
		{
			//成功后返回客户端新增的用户ID,并返回提示信息和操作状态
			$this->ajaxReturn($data,"新增成功!",1);
			// return $data;
		}
		else
		{
			//错误后返回错误的操作状态和提示信息
			$this->ajaxReturn(0,"获取视频地址失败!",0);
		}
	}
	
	/**
	 *花絮获取swf地址和标题--暂不使用
	 */
	function getSwf2($url)
	{
		$html = $this->getHtml($url);
		$data = $this->getYoukuSwf($html);
		if($data)
		{
			return $data;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 *获取网页源码
	 */
	function getHtml($url)
	{
		if($html = file_get_contents($url))
		{
			return $html;
		}
		else
		{
			return false;
		}
	}
	
	
	/**
	 *获取youku swf url
	 *@param string $html包含视频的网页源码
	 *@return mixed $data(swf地址和title)or false
	 */
	function getYoukuSwf($html)
	{
		//从视频地址中获取<input type="text" class="form_input form_input_s" id="link2" value="http://player.youku.com/player.php/Type/Folder/Fid/20678940/Ob/1/sid/XNzE1NTg5NzA4/v.swf" >'
		$pattern1 = '#<input[^>]*?id="link2"[^>]*?>#is';
		if(preg_match_all($pattern1,$html,$match))
		{
			$input_html = $match[0][0];
			
		
		}
		else
		{
			
			// var_dump($match);exit;
			return false;
		}
		//从上面字符串中获取value值
		$pattern2 = '#value="(.*?)"#is';
		if(preg_match_all($pattern2,$input_html,$match2))
		{
			$swf = $match2[1][0];
			
			$pattern3 = '#<span[^>]*?id="subtitle">(.*?)<\/span>#is';
			if(preg_match_all($pattern3,$html,$match3))
			{
				$title = $match3[1][0];
			}
			else
			{
				$title = '爱愿望宣传视频';
			}
			$data['swf'] = $swf;
			$data['title'] = $title;
			return $data;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 *已认领项目详情页面的猜你关注--ajax数据
	 */	
	public function getRandProject(){
		$model = D('Project');
		$list = $model->getrandlist(3);
		
		// 将项目数据中的cat_id转为项目分类名,addtime转为需要的时间格式
		$list = $this->proCid2Cname($list);
		
		
		
		// var_dump($list);exit;
		if($list){
			$this->ajaxReturn($list,"",1);
		}else{
			$this->ajaxReturn("","",0);
		}
		
	}
	
	
	/**
	 *将项目数据中的cat_id转为项目分类名
	 *addtime转为需要的时间格式
	 *本可以通过连表查询实现
	 */
	public function proCid2Cname($pro_arr)
	{		
		foreach($pro_arr as $k=>$v)
		{
			// $v['cat_id'] = get_project_cat_name($v['cat_id']);
			$pro_arr[$k]['cat_id'] = get_project_cat_name($v['cat_id']);
			$pro_arr[$k]['addtime'] = showDate($v['addtime']);
		}
		return $pro_arr;
	}	
	
	/**
	 *已认领项目详情页面的猜你关注--普通数据
	 */
	public function getRandProCo()
	{
		$model = D('Project');
		$list = $model->getrandlist(3);
		$randProData = $list?$list:'';
		return $randProData;
		// $this->randProData = $randProData;
	}
	
	/**
	 *已认领项目详情页面的最新项目
	 */
	function getNewPro()
	{
		$model = M('project');
		$data = $model->order('project_id DESC')->limit(3)->select();
		
		// 将项目数据中的cat_id转为项目分类名,addtime转为需要的时间格式
		$data = $this->proCid2Cname($data);
		
		if($data)
		{
			$this->ajaxReturn($data,"",1);
		}
		else
		{
			$this->ajaxReturn(0,'无数据',0);
		}
		
	}
	
	/**
	 *已认领项目详情页面的最热项目
	 */
	function getHotPro()
	{
		//取得最热的项目
		$model = D('Project');
		$data = $model->getHotList(3);
		
		// 将项目数据中的cat_id转为项目分类名,addtime转为需要的时间格式
		$data = $this->proCid2Cname($data);
		
		if($data)
		{
			$this->ajaxReturn($data,1,1);
		}
		else
		{
			$this->ajaxReturn(0,'无数据',0);
		}
		
	}
	
	
	
	
	
	
	
	
	/*ggh end*/
	
	/*cdc start*/
	
	/**
	 * 项目捐赠状态
	 * donated
	 */
	public function donated(){
		//取得项目捐款列表
		$model = M('ProjectContribution');
		$where = array(
			'project_id'=>$this->project['project_id'],
		);
		$count = $model->where($where)->count('contr_id');
		$page = $this->page($count,10);
		$list = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('addtime DESC')->select();
		$this->assign('contr_list',$list);
		$this->assign('contr_page',$page->show());

		//需要的物资列表
		$model = M('ProjectMaterials');
		$count = $model->where($where)->count('materials_id');
		$page = $this->page($count,10);
		$list = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
		foreach($list as $key => $value){
			$list[$key]['remaining'] = $value['quantity_required'] - $value['quantity_received'];
		}
		$this->assign('mater_list',$list);
		$this->assign('mater_page',$page->show());

		//已捐增的物资列表
		$model = M('ProjectDonation');
		$where = array(
			'cdc_project_donation.project_id'=>$this->project['project_id'],
		);
		$count = $model->where($where)->count('donation_id');
		$page = $this->page($count,10);
		$list = $model->join('cdc_project_materials AS pm ON cdc_project_donation.materials_id = pm.materials_id')->where($where)->limit($page->firstRow . ',' . $page->listRows)->field('cdc_project_donation.*,pm.name AS m_name')->select();
		$this->assign('donation_list',$list);
		$this->assign('donation_page',$page->show());
		$this->display();
	}
	/**
	 * 项目志愿者
	 * volunteer
	 */
	public function volunteer(){
		$where = array(
			'project_id' => $this->project['project_id'],
		);
		//所需志愿者名单
		$model = M('ProjectVolunteer');
		$count = $model->where($where)->count('volunteer_id');
		$page = $this->page($count,10);
		$list = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
		$str = "";
		foreach($list as $key => $value){
			$list[$key]['remaining'] = $value['number'] - $value['quantity_number'];
			$str .= $value['volunteer_id'] . ",";
		}
		$str = substr($str,0,-1);
		$this->assign('volunteer_list',$list);
		$this->assign('volunteer_page',$page->show());

		//招募志愿者名单
		$model = M('ProjectRecruit');
		$where = array(
			'cdc_project_recruit.volunteer_id' => array('in',$str),
		);
		$count = $model->where($where)->count('id');
		$page = $this->page($count,10);
		$list = $model->join('cdc_project_volunteer AS pv ON cdc_project_recruit.volunteer_id = pv.volunteer_id')->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('addtime DESC')->field('pv.name AS p_name,cdc_project_recruit.*')->select();
		$this->assign('recruit_list',$list);
		$this->assign('recruit_page',$page->show());
		$this->display();
	}
	/**
	 * 改变志愿者报名状态
	 * changeConsent
	 */
	public function changeConsent(){
		$id = $this->_post('id')?$this->_post('id'):"";
		$consent_status = $this->_post('consent_status')?$this->_post('consent_status'):"";
		if(empty($id)){
			$this->ajaxReturn("","无效的id参数",0);
		}
		if(empty($consent_status)){
			$this->ajaxReturn("","无效的status参数",0);
		}
		$where = array(
			'id' => $id,
		);
		$data = array(
			'consent_status' => $consent_status,
		);
		$res = M('ProjectRecruit')->where($where)->save($data);
		if($res){
			$this->ajaxReturn("","",1);
		}else{
			$this->ajaxReturn("","操作失败",0);
		}
	}
	/**
	 * 改变志愿者报名状态
	 * changeConsent
	 */
	public function changeAttend(){
		$id = $this->_post('id')?$this->_post('id'):"";
		$attend_status = $this->_post('attend_status')?$this->_post('attend_status'):"";
		if(empty($id)){
			$this->ajaxReturn("","无效的id参数",0);
		}
		if(empty($attend_status)){
			$this->ajaxReturn("","无效的status参数",0);
		}
		$where = array(
			'id' => $id,
		);
		$data = array(
			'attend_status' => $attend_status,
		);
		$res = M('ProjectRecruit')->where($where)->save($data);
		if($res){
			$this->ajaxReturn("","",1);
		}else{
			$this->ajaxReturn("","操作失败",0);
		}
	}

	/**
	 * 发布公告
	 * addNotice
	 */
	public function addNotice(){
		$model = D('ProjectNotice');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$model->addtime = time();
		$res = $model->add();
		if($res){
			$this->success("添加成功",U('Project/operation',array('pid'=>$_POST['project_id'])));
		}
	}
	/**
	 * 项目执行情况
	 * operation
	 */
	public function operation(){
		//取出项目的公告列表
		$model = M('ProjectNotice');
		$where = array(
			'project_id' => $this->project['project_id'],
		);
		$list = $model->where($where)->order('addtime DESC')->select();
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 花絮
	 * titbit
	 */
	public function titbit(){
		$model = M('Titbit');
		$where = array(
			'project_id' => $this->project['project_id'],
		);
		$count = $model->where($where)->count('titbit_id');
		$page = $this->page($count,5);
		$list = $model->where($where)->limit($page->firstRow . "," . $page->listRows)->order('addtime DESC')->select();
		$model = M('TitbitComment');
		$from = M('TitbitAlbum');
		foreach($list as $key => $value){
			$where = array(
				'titbit_id' => $value['titbit_id'],
			);
			$list[$key]['commentCount'] = $model->where($where)->count('comment_id');
			$list[$key]['firstimg'] = $from->where($where)->getField('images');
		}
		$this->assign('list',$list);
		$this->assign('page',$page->show());
		$this->display();
	}

	/**
	 * 日程
	 * schedule
	 */
	public function schedule(){
		$model = D('ProjectSchedule');
		$where = array(
			'project_id' => $this->project['project_id'],
		);
		$list = $model->where($where)->order('addtime DESC')->select();
		$this->assign('list',$list);

		$this->display();
	}
	/**
	 * 添加日程
	 * addschedule
	 */
	public function addschedule(){
		//取出项目的物资列表
		$model = D('ProjectMaterials');
		$where = array(
			'project_id' => $this->project['project_id'],
		);
		$list = $model->where($where)->select();
		$count = $model->where($where)->count();
		$this->assign('count',$count);
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 编辑日程
	 * editschedule
	 */
	public function editschedule(){
		$sid = $this->_get('sid')?$this->_get('sid'):"";
		if(empty($sid)){
			$this->error("无效的sid参数");
		}
		$where = array(
			's_id' => $sid,
		);
		$model = M('ProjectSchedule');
		$data = $model->where($where)->find();
		$this->assign('data',$data);
		//取出项目的物资列表
		$model = M('ProjectScheduleMaterials');
		$list = $model->join('cdc_project_materials AS m ON cdc_project_schedule_materials.materials_id = m.materials_id')->where($where)->field('cdc_project_schedule_materials.*,m.name,m.guide')->select();
		$this->assign('materials_list',$list);

		//取得物资总数
		$model = D('ProjectMaterials');
		$where = array(
			'project_id' => $this->project['project_id'],
		);
		$count = $model->where($where)->count();
		$this->assign('count',$count);
		$this->display();
	}
	/**
	 * 添加日程处理
	 * doaddschedule
	 */
	public function doaddschedule(){
		$model = D('ProjectSchedule');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$model->starttime = strtotime($_POST['starttime']);
		$model->endtime = strtotime($_POST['endtime']);
		$model->addtime = time();
		$res = $model->add();
		if($res){
			//出日程物资分配
			$model = M('ProjectScheduleMaterials');
			$data = array(
				's_id' => $res,
			);

			foreach($_POST['quantity_received'] as $key => $value){
				if(!empty($value)){
					$data['quantity_allotted'] = $value;
					$data['has_number'] = $_POST['has_number'][$key];
					$data['materials_id'] = $_POST['materials_id'][$key];
					$result = $model->data($data)->add();
				}
				
			}
			$this->success('添加日程成功',U('Project/schedule',array('pid'=>$_POST['project_id'])));

		}else{
			$this->error("添加日程失败");
		}
	}
	/**
	 * 添加日程处理
	 * doeditschedule
	 */
	public function doeditschedule(){
		$model = D('ProjectSchedule');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$model->starttime = strtotime($_POST['starttime']);
		$model->endtime = strtotime($_POST['endtime']);
		$res = $model->save();
		if($res){
			//出日程物资分配
			$model = M('ProjectScheduleMaterials');
			$data = array(
				's_id' => $res,
			);
			$model->where($where)->delete();
			foreach($_POST['quantity_allotted'] as $key => $value){
				if(!empty($value)){
					$data['quantity_allotted'] = $value;
					$data['has_number'] = $_POST['has_number'][$key];
					$data['materials_id'] = $_POST['materials_id'][$key];
					$result = $model->data($data)->add();
				}
				
			}
			$this->success('编辑日程成功',U('Project/schedule',array('pid'=>$_POST['project_id'])));

		}else{
			$this->error("编辑日程失败");
		}
	}
	/**
	 * ajax取得物资列表
	 * getMaterList
	 */
	public function getMaterList(){
		$ids = $this->_post('ids')?$this->_post('ids'):"";
		$project_id = $this->_post('project_id')?$this->_post('project_id'):"";
		$ids = substr($ids,0,-1);
		//取出项目的物资列表
		$model = D('ProjectMaterials');
		$where = array(
			'project_id' => $project_id,
		);
		if(!empty($ids)){
			$where['materials_id'] = array('NOT IN',$ids);
		}
		$list = $model->where($where)->select();
		if($list){
			$this->ajaxReturn($list,"",1);
		}else{
			$this->ajaxReturn("","无更多的捐赠物资可供选择",0);
		}

	}
	/**
	 * ajax取得物资详情
	 * getMaterInfo
	 */
	public function getMaterInfo(){
		$materials_id = $this->_post('materials_id')?$this->_post('materials_id'):"";
		$where = array(
			'materials_id' => $materials_id,
		);
		$data = M('ProjectMaterials')->where($where)->find();
		if($data){
			$this->ajaxReturn($data,"",1);
		}else{
			$this->error("","",0);
		}
	}
	/**
	 * 规划
	 * plan
	 */
	public function plan(){
		$sid = $this->_get('sid')?$this->_get('sid'):"";
		$pid = $this->_get('pid')?$this->_get('pid'):"";
		if(empty($sid)){
			$this->error("无效的sid参数");
		}
		$where = array(
			's_id' => $sid,
		);
		$model = M('ProjectSchedule');
		$data = $model->where($where)->find();
		$this->assign('data',$data);

		//取出日程规划志愿者名单
		$model = M('ProjectSchedulePlan');
		$where = array(
			'cdc_project_schedule_plan.s_id' => $sid,
		);
		$list = $model->join('cdc_project_recruit AS pr ON cdc_project_schedule_plan.recruit_id = pr.id')->where($where)->field('cdc_project_schedule_plan.*,pr.user_id,pr.name,pr.phone')->select();
		$this->assign('list',$list);

		$this->display();
	}
	/**
	 * 规划
	 * plan
	 */
	public function addplan(){
		$model = M('ProjectSchedulePlan');
		$s_id = $this->_post('s_id')?$this->_post('s_id'):"";
		$project_id = $this->_post('project_id')?$this->_post('project_id'):"";
		//删除日程规划
		$where = array(
			's_id' => $s_id,
		);
		$res = $model->where($where)->delete();
		//插入日程规划
		$data = array(
			's_id' => $s_id,
			'project_id' => $project_id,
		);
		foreach($_POST['recruit_id'] as $key => $value){
			$data['recruit_id'] = $value;
			$data['post'] = $_POST['post'][$key];
			$data['job_content'] = $_POST['job_content'][$key];
			$data['starttime'] = strtotime($_POST['starttime'][$key]);
			$data['endtime'] = strtotime($_POST['endtime'][$key]);
			$res = $model->data($data)->add();
		}
		if($res){
			$this->success('编辑日程规划成功',U('Project/plan',array('pid'=>$project_id,'sid'=>$s_id)));
		}else{
			$this->error('编辑日程规划失败');
		}
		
		
	}
	/*
     * 获取项目报了名的志愿者
     * getProjectRecruit
     * 
     */
	public function getProjectRecruit(){
		$project_id = $this->_post('project_id')?$this->_post('project_id'):"";
		$str = $this->_post('str')?$this->_post('str'):"";
		if(empty($project_id)){
			$this->ajaxReturn("","无效的项目id参数",0);
		}
		if(!empty($str)){
			$str = substr($str,0,-1);
		}
		$where = array(
			'project_id' => $project_id,
			'id' => array('NOT IN',$str),
		);
		$list = M('ProjectRecruit')->where($where)->field('id,name')->select();
		if($list){
			$this->ajaxReturn($list,"",1);
		}else{
			$this->ajaxReturn("","已经没有志愿者可以选择",0);
		}

	}
	/*
     * 获取项目报了名的志愿者详情
     * getProjectRecruitInfo
     * 
     */
	public function getProjectRecruitInfo(){
		$id = $this->_post('id')?$this->_post('id'):"";
		$where = array(
			'id' => $id,
		);
		$data = M('ProjectRecruit')->where($where)->field('user_id,id')->find();
		$data['sex'] = getUserSex($data['user_id']);
		$data['type'] = getUserType($data['user_id']);
		if($data){
			$this->ajaxReturn($data,"",1);
		}else{
			$this->ajaxReturn("","",0);
		}
	}

	/*
     * 删除日程规划
     * delPlan
     * 
     */
	public function delPlan(){
		$id = $this->_get('id')?$this->_get('id'):"";
		if(empty($id)){
			$this->ajaxReturn("","id参数无效",0);
		}
		$model = M('ProjectSchedulePlan');
		$where = array(
			'id' => $id,
		);
		$res = $model->where($where)->delete();
		if($res){
			$this->ajaxReturn("","",1);
		}else{
			$this->ajaxReturn("","",0);
		}
	}
	/*
     * 删除日程规划
     * delPlan
     * 
     */
	public function delSchedule(){
		$s_id = $this->_get('id')?$this->_get('id'):"";
		if(empty($id)){
			$this->ajaxReturn("","s_id参数无效",0);
		}
		$model = M('ProjectSchedule');
		$where = array(
			's_id' => $s_id,
		);
		$res = $model->where($where)->delete();

		if($res){
			M('ProjectSchedulePlan')->where($where)->delete();
			M('ProjectScheduleMaterials')->where($where)->delete();
			$this->ajaxReturn("","",1);
		}else{
			$this->ajaxReturn("","",0);
		}
	}


	/*cdc end*/
    
      //wing s
    
    /**
     * 项目编辑
     * 
     * */
     //public function edit(){
//        $project_id=$this->_get('pid');
//        if(!$project_id){
//            $this->error ('错误操作!');
//        }
//        $project=M('project');
//        $data=$project->where('project_id='.$project_id.' AND (status=0 OR status=1)')->find();
//        
//        $this->assign ( $data );
//        
//        $cats = $this->getAllCat();
//		$this->assign('cats',$cats);
//        
//        $typearr=explode(',',$data['type']);
//        if(in_array('1',$typearr)){
//            $typearr['jk']=1;
//        }
//        if(in_array('2',$typearr)){
//            $typearr['wz']=2;
//        }
//        if(in_array('3',$typearr)){
//            $typearr['zm']=3;
//        }
//       
//        $this->assign('typearr',$typearr);
//        $this->display();
//     }


    public function edit(){
        $name=$this->getActionName();
		$model = M ( $name );
		$pk = $model->getPk ();
		$id = $_REQUEST ['pid'];
		$vo = $model->field(true)->where(array($pk=>$id,'user_id'=>$this->user['user_id']))->find();
        
        if($vo['status']!=0 && $vo['status']!=1){
            $this->error ('此项目已被认领!不能再编辑');
        }
        
		$vo['help_type'] = help_type($id);
		
		$materials = material($id);	//项目所需的物资
		$volunteer = volunteer($id);	//项目所需的志愿者		
		$vo['materials'] = $materials;
		$vo['volunteer'] = $volunteer;
		// 标题图片
		$title_pic = $vo['title_pic']; 
		$testimonial_materials = explode(',',$vo['testimonial_material']);
		
		
		
		$this->title_pic = $title_pic;
		$this->testimonial_materials = $testimonial_materials;
		/**
		 推送类别 1机构 2爱心使者 start
		 */
		$agency_type = 1;
		$angle_type = 2;
		
		$agency_pushes = get_push_data($id,$agency_type);
		
		$angle_pushes = get_push_data($id,$angle_type);
		
		
		$agency_push_names = $this->convertAgencyPushes($agency_pushes);
		$angle_push_names = $this->convertAnglePushes($angle_pushes);
		
		
		$this->agency_push_names = $agency_push_names;
		$this->angle_push_names = $angle_push_names;
		
		/**
		 推送类别 1机构 2爱心使者 end
		 */
		
		
		$cats = getAllCat();	//所有项目分类
		$vo['cats'] = $cats;
		$this->assign ( 'vo', $vo );
		
		$this->display();
        
    }
    
     public function editsave(){
        if($this->_post('project_id')){//start{			
			$this->assign('project_id',$_POST['project_id']);
			$model = M('project');
			$model->create();
			
			$type = $_POST['type'];
		
			$model->type = implode(",",$type);
			
			$model->starttime = convertTime($_POST['starttime']);
			$model->endtime = convertTime($_POST['endtime']);
			
			$model->user_id = $this->user['user_id'];
			//标题图片
			$title_pic = $_POST['title_pic']?$_POST['title_pic']:'';
			// $model->title_pic = implode(',',$title_pic);
			$model->title_pic = $title_pic;
			
			//证明材料
			$testimonial_material = $_POST['prove_pic'];
			$model->testimonial_material = implode(',',$testimonial_material);
			
			$res = $model->where('user_id='.$this->user['user_id'].'AND project_id='.$_POST['project_id'])->save();
			if($res !== false)
			{
				$project_id = $_POST['project_id'];
				$res_m = $this->delMaterial($project_id);	//删除项目所需的物资
				$res_v = $this->delVolunteer($project_id);	//删除项目所需的志愿者
				
				$res_p = $this->delPush($project_id);	//删除定向推送记录
				
				if( ($res_m===FALSE)||($res_m===FALSE)||($res_m===FALSE) )
				{
					$model->rollback(); 

				}
				
				$this->add_materials($project_id,$model);		//增加项目所需的物资
				$this->add_volunteers($project_id,$model);		//增加项目所需的志愿者				
				
				$this->addAgencyPushes($project_id,$model);	//批量增加定向公益机构推送记录
				$this->addAngelPushes($project_id,$model);		//批量增加定向爱心使者机构推送记录	
                $this->success('更新成功');
			}
			else
			{
				$this->error ('project更新失败!');
			}
        }else{
             $this->error ('错误操作!');
        }
     }
     
     
     /**
	 * 项目管理
	 * manager
	 */
	public function manager(){
	   $name=$this->getActionName();
		$model = M ( $name );
		$pk = $model->getPk ();
		$id = $_REQUEST ['pid'];
		$vo = $model->field(true)->where(array($pk=>$id,'claim_agencyid'=>$this->agency['agency_id']))->find();
        
        if($vo['status']==0 || $vo['status']==1){
            $this->error ('此项目已被认领!不能再编辑');
        }
        
		$vo['help_type'] = help_type($id);
		
		$materials = material($id);	//项目所需的物资
		$volunteer = volunteer($id);	//项目所需的志愿者	
        
        foreach($materials as $key=>$val){
            
           $materials[$key]['surplus'] = $val['quantity_required']-$val['quantity_received'];
           if($val['quantity_required']-$val['quantity_received']<=0){
                $materials[$key]['typestr']='已完成';
           }elseif($val['quantity_required']-$val['quantity_received']>0){
                $materials[$key]['typestr']='未完成';
           }
        }
        
        foreach($volunteer as $key=>$val){
            
           $volunteer[$key]['surplus'] = $val['number']-$val['quantity_number'];
           if($val['number']-$val['quantity_number']<=0){
                $volunteer[$key]['typestr']='已完成';
           }elseif($val['number']-$val['quantity_number']>0){
                $volunteer[$key]['typestr']='未完成';
           }
        }
        	
		$vo['materials'] = $materials;
		$vo['volunteer'] = $volunteer;
		// 标题图片
		$title_pic = $vo['title_pic']; 
		$testimonial_materials = explode(',',$vo['testimonial_material']);
		
		
		
		$this->title_pic = $title_pic;
		$this->testimonial_materials = $testimonial_materials;
		/**
		 推送类别 1机构 2爱心使者 start
		 */
		$agency_type = 1;
		$angle_type = 2;
		
		$agency_pushes = get_push_data($id,$agency_type);
		
		$angle_pushes = get_push_data($id,$angle_type);
		
		
		$agency_push_names = $this->convertAgencyPushes($agency_pushes);
		$angle_push_names = $this->convertAnglePushes($angle_pushes);
		
		
		$this->agency_push_names = $agency_push_names;
		$this->angle_push_names = $angle_push_names;
		
		/**
		 推送类别 1机构 2爱心使者 end
		 */
		
		
		$cats = getAllCat();	//所有项目分类
		$vo['cats'] = $cats;
		$this->assign ( 'vo', $vo );
		
		$this->display();
	}
     
     
      public function managersave(){
        if($this->_post('project_id')){//start{			
			$this->assign('project_id',$_POST['project_id']);
			$model = M('project');
			$model->create();
			
			$type = $_POST['type'];
		
			$model->type = implode(",",$type);
			
			$model->truestarttime = convertTime($_POST['truestarttime']);
			$model->trueendtime = convertTime($_POST['trueendtime']);
			
			$model->claim_agencyid = $this->agency['agency_id'];
			//标题图片
			$title_pic = $_POST['title_pic']?$_POST['title_pic']:'';
			// $model->title_pic = implode(',',$title_pic);
			$model->title_pic = $title_pic;
			
			//证明材料
			$testimonial_material = $_POST['prove_pic'];
			$model->testimonial_material = implode(',',$testimonial_material);
			
			$res = $model->where('claim_agencyid='.$this->agency['agency_id'].' AND project_id='.$_POST['project_id'])->save();
        
			if($res !== false)
			{
				$project_id = $_POST['project_id'];
				$this->success('更新成功');
			}
			else
			{
				$this->error ('project更新失败!');
			}
        }else{
             $this->error ('错误操作!');
        }
     }
     
     	/**
	 * 群发
	 * fsend
	 */
	public function fsend(){
	    $pid=$this->_get('pid');
        if(!$pid){
            $this->error ('错误操作!');
        }
	   	$this->assign('project_id',$pid);
		$this->display();
	}
    
    public function fsendsave(){
      $pid=$this->_post('project_id'); 
      if(!$pid){
            $this->error ('错误操作!');
        }
      $model = M('user_message');
	  $title=$this->_post('title');
      $content=$_POST['content'];        
      $list=M('project_recruit')->where('project_id='.$pid)->select();
      if(!$list){
        $this->error ('暂时没有志愿者!');
      }else{
          foreach($list as $val){
              $data['from_userid']=  $this->user['user_id'];
              $data['type']= 1;
              $data['addtime']= time();
              $data['to_userid']=  $val['user_id'];
              $data['title']=$title;
              $data['content']=$content;
              $model->add($data);
          }
          $this->success('发送成功');
      }
	   
	}
    
    
    	/**
	 * 项目中心
	 * center
	 */
	public function center(){
	   
       
	    $type=$this->_get('type');
        switch ($type)
        {
      //我发起的  待认证  待认领
        case 1:
           $table='project';
           $where='p.user_id ='.$this->user['user_id'].' AND (status=0 OR status=1)';
           $noothertime='';
          break;  
      //我关注的
        case 2:
           $table='project_follow';
           $where='po.user_id ='.$this->user['user_id'].'';
           $join='cdc_project_follow AS po ON p.project_id = po.project_id';
           $noothertime='p.*,po.addtime AS poaddtime';
          break;
       //我支持的
        case 3:
           $table='project_support'; 
           $where='po.user_id ='.$this->user['user_id'].'';
           $join='cdc_project_support AS po ON p.project_id = po.project_id';
           $noothertime='p.*,po.addtime AS poaddtime';
          break;
       //我转发的
        case 4:
           $table='project_repost';
           $where='po.user_id ='.$this->user['user_id'].'';
           $join='cdc_project_repost AS po ON p.project_id = po.project_id';
           $noothertime='p.*,po.addtime AS poaddtime';
          break;
       //我认证的 
        case 5:
           $table='project_auth';
           $where='po.user_id ='.$this->user['user_id'].'';
           $join='cdc_project_auth AS po ON p.project_id = po.project_id';
           $noothertime='p.*,po.addtime AS poaddtime';
          break;
       //我认领的  已完成  进行中
        default:
           $table='project'; 
           $where='claim_agencyid ='.$this->agency['agency_id'].' AND (status=2 OR status=3)';
           $noothertime='';
        }
        
        $model = M('project');
		$count = $model->where($where)->count();
		$page = $this->page($count,10);
        $page->parameter=   "type=".urlencode($type).'&';
        $list = $model->table('cdc_project AS p')->field($noothertime)->join($join)->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('project_id DESC')->select();
      
        $this->assign('list',$list);
        $this->assign('page',$page->show());
        $this->assign('type',$type);
		$this->display();
	}
    //wing e
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值