<?php
/**
* @copyright ICZ Corporation (http://www.icz.co.jp/)
* @license See the LICENCE file
* @author <sencha@icz.co.jp>
* @version $Id$
*/

class Notice extends AppModel{

	var $name	=	'Notice';
	var $useTable	=	'T_NOTICE';
	var $primaryKey = 'NTC_ID';

	var $components = array('Auth');

	//プラグイン読み込み
	var $actsAs = array('Cakeplus.AddValidationRule');

	var $belongsTo = array(
		'User' =>array(
				'className'		=>	'User',
				'conditions'	=>	'',
				'order'			=>	'',
				'dependent'		=>	true,
				'foreignKey'	=>	'USR_ID',
		),
		'P_User' =>array(
				'className'		=>	'User',
				'conditions'	=>	'',
				'order'			=>	'',
				'dependent'		=>	true,
				'foreignKey'	=>	'P_USR_ID',
		),
		'Group' =>array(
				'className'		=>	'Group',
				'conditions'	=>	'',
				'order'			=>	'',
				'dependent'		=>	true,
				'foreignKey'	=>	'GRP_ID',
		),
		'Timeline' =>array(
				'className'		=>	'Timeline',
				'conditions'	=>	'',
				'order'			=>	'',
				'dependent'		=>	true,
				'foreignKey'	=>	'TML_ID',
		),
	);

	Public Function Get_Notice($_userid){

		$count = 0;
		$result = array();
		$result = $this->find('all',array('conditions'=>array('Notice.USR_ID'=>$_userid['User']['USR_ID'],'Notice.STATUS'=>0,'NOT'=>array('Notice.P_USR_ID'=>$_userid['User']['USR_ID'])),'order'=>'Notice.NTC_ID DESC'));
		if($result!=null){
			$count=count($result);
		}
// 		if(count($result)<6){
// 			$lim = 5-$count;
// 			$result_tmp = $this->find('all',array('limit'=>$lim,'conditions'=>array('Notice.STATUS'=>1,'Notice.USR_ID'=>$_userid['User']['USR_ID'],'NOT'=>array('Notice.P_USR_ID'=>$_userid['User']['USR_ID'])),'order'=>'Notice.NTC_ID DESC'));
// 			foreach($result_tmp as $key =>$val){
// 				array_push($result,$val);
// 			}
// 		}

		App::import('Model','User');
		$user = new User;
		foreach($result as $key => $val){
			if($val['Notice']['ACT_ID']==2){
				$tmp = $user->find('first',array('conditions'=>array('User.USR_ID'=>$val['Timeline']['USR_ID']),'fields'=>array('User.NAME')));
				if($tmp['User']['NAME']==$val['P_User']['NAME']){
					$tmp['User']['NAME']="自分";
				}elseif($tmp['User']['NAME']==$val['User']['NAME']){
					$tmp['User']['NAME']="あなた";
				}
				$result[$key]['Timeline']['NAME'] = $tmp['User']['NAME'];
			}
		}
		$result['Count']=$count;

		return $result;
	}

	/*
	 * @param $_data   投稿したデータ
	 * @param $_userid 親スレのデータ
	 */
	Public Function Save_Notice($_data,$_userid=null){
		$params=array();
		if($_userid!=null){
			if($_userid['Timeline']['ACT_ID']==3){
				App::import('Model','Join');
				$join = new Join;
				$joins = $join->find('all',array('conditions'=>array('Join.GRP_ID'=>$_userid['Timeline']['VAL_ID'])));
				foreach($joins as $key =>$val){
					if($val['Join']['USR_ID']==$_userid['Timeline']['USR_ID']){
						$params[$key]['USR_ID'] = $val['Join']['USR_ID'];
						$params[$key]['P_USR_ID'] = $_data['USR_ID'];
						$params[$key]['TML_ID'] = $_userid['Timeline']['TML_ID'];
						$params[$key]['GRP_ID'] = $_userid['Timeline']['VAL_ID'];
						$params[$key]['ACT_ID'] = 2;
						$params[$key]['CMT_ID'] = $_data['TML_ID'];
						$n=$this->find('first',array('fields'=>array('Notice.NTC_ID'),'conditions'=>array('Notice.USR_ID'=>$params[$key]['USR_ID'],'Notice.P_USR_ID'=>$params[$key]['P_USR_ID'],'Notice.TML_ID'=>$params[$key]['TML_ID'],'Notice.ACT_ID'=>$params[$key]['ACT_ID'])));
						if($n){
							$params[$key]['NTC_ID']=$n;
						}
						$params[$key]['STATUS'] = 0;
						$params[$key]['INSERT_DATE'] = date("Y-m-d H:i:s");
					}
				}
				$this->saveAll($params);
			}elseif($_userid['Timeline']['ACT_ID']==5){
				App::import('Model','Storage');
				$storage = new Storage;
				$storages = $storage->find('first',array('fields'=>array('Group.GRP_ID'),'conditions'=>array('Storage.FLE_ID'=>$_userid['Timeline']['VAL_ID'])));
				App::import('Model','Join');
				$join = new Join;
//				$joins = $join->find('all',array('conditions'=>array('Join.GRP_ID'=>$storages['Group']['GRP_ID'])));
				$joins = $join->find('all',array('conditions'=>array('Join.GRP_ID'=>$storages['Group']['GRP_ID'],'Join.USR_ID'=>$_userid['Timeline']['USR_ID'])));
				foreach($joins as $key =>$val){
					if($val['Join']['USR_ID']==$_userid['Timeline']['USR_ID']){
						$params[$key]['USR_ID'] = $val['Join']['USR_ID'];
						$params[$key]['P_USR_ID'] = $_data['USR_ID'];
						$params[$key]['TML_ID'] = $_userid['Timeline']['TML_ID'];
						$params[$key]['GRP_ID'] = $storages['Group']['GRP_ID'];
						$params[$key]['ACT_ID'] = 2;
						$params[$key]['CMT_ID'] = $_data['TML_ID'];
						$n=$this->find('first',array('fields'=>array('Notice.NTC_ID'),'conditions'=>array('Notice.USR_ID'=>$params[$key]['USR_ID'],'Notice.P_USR_ID'=>$params[$key]['P_USR_ID'],'Notice.TML_ID'=>$params[$key]['TML_ID'],'Notice.ACT_ID'=>$params[$key]['ACT_ID'])));
						if($n){
							$params[$key]['NTC_ID']=$n['Notice']['NTC_ID'];
						}
						$params[$key]['STATUS'] = 0;
						$params[$key]['INSERT_DATE'] = date("Y-m-d H:i:s");
					}
					$this->saveAll($params);
				}
			}else{
				$params['USR_ID'] = $_userid['Timeline']['USR_ID'];
				$params['P_USR_ID'] = $_data['USR_ID'];
				$params['TML_ID'] = $_data['VAL_ID'];
				$params['ACT_ID'] = 0;
				$params['CMT_ID'] = $_data['TML_ID'];
				$n=$this->find('first',array('fields'=>array('Notice.NTC_ID'),'conditions'=>array('Notice.USR_ID'=>$params['USR_ID'],'Notice.P_USR_ID'=>$params['P_USR_ID'],'Notice.TML_ID'=>$params['TML_ID'],'Notice.ACT_ID'=>$params['ACT_ID'])));
				if($n){
					$params['NTC_ID']=$n['Notice']['NTC_ID'];
				}
				$params['STATUS'] = 0;
				$params['INSERT_DATE'] = date("Y-m-d H:i:s");
				$this->save($params);
			}
		}else{
			if($_data['ACT_ID']==5){
				App::import('Model','Storage');
				$storage = new Storage;
				$storages = $storage->find('first',array('fields'=>array('Group.GRP_ID'),'conditions'=>array('Storage.FLE_ID'=>$_data['VAL_ID'])));

				App::import('Model','Join');
				$join = new Join;
				$joins = $join->find('all',array('conditions'=>array('Join.GRP_ID'=>$storages['Group']['GRP_ID'])));
				foreach($joins as $key =>$val){
					$params[$key]['USR_ID'] = $val['Join']['USR_ID'];
					$params[$key]['P_USR_ID'] = $_data['USR_ID'];
					$params[$key]['TML_ID'] = $_data['TML_ID'];
					$params[$key]['GRP_ID'] = $storages['Group']['GRP_ID'];
					$params[$key]['ACT_ID'] = 1;
					$n=$this->find('first',array('fields'=>array('Notice.NTC_ID'),'conditions'=>array('Notice.USR_ID'=>$params[$key]['USR_ID'],'Notice.P_USR_ID'=>$params[$key]['P_USR_ID'],'Notice.TML_ID'=>$params[$key]['TML_ID'],'Notice.ACT_ID'=>$params[$key]['ACT_ID'])));
					if($n){
						$params[$key]['NTC_ID']=$n['Notice']['NTC_ID'];
					}
					$params[$key]['STATUS'] = 0;
					$params[$key]['INSERT_DATE'] = date("Y-m-d H:i:s");
				}
				$this->saveAll($params);
			}else{
				App::import('Model','Join');
				$join = new Join;
				$joins = $join->find('all',array('conditions'=>array('Join.GRP_ID'=>$_data['VAL_ID'])));
				foreach($joins as $key =>$val){
					$params[$key]['USR_ID'] = $val['Join']['USR_ID'];
					$params[$key]['P_USR_ID'] = $_data['USR_ID'];
					$params[$key]['TML_ID'] = $_data['TML_ID'];
					$params[$key]['GRP_ID'] = $_data['VAL_ID'];
					$params[$key]['ACT_ID'] = 1;
					$n=$this->find('first',array('fields'=>array('Notice.NTC_ID'),'conditions'=>array('Notice.USR_ID'=>$params[$key]['USR_ID'],'Notice.P_USR_ID'=>$params[$key]['P_USR_ID'],'Notice.TML_ID'=>$params[$key]['TML_ID'],'Notice.ACT_ID'=>$params[$key]['ACT_ID'])));
					if($n){
						$params[$key]['NTC_ID']=$n['Notice']['NTC_ID'];
					}
					$params[$key]['STATUS'] = 0;
					$params[$key]['INSERT_DATE'] = date("Y-m-d H:i:s");
				}
				$this->saveAll($params);
			}
		}
	}
	Public Function Change($_userid){
		$result = array();
		$result = $this->find('all',array('conditions'=>array('Notice.USR_ID'=>$_userid,'Notice.STATUS'=>0)));
		$params=array();
		foreach($result as $key =>$val){
			$params[$key]['NTC_ID']=$val['Notice']['NTC_ID'];
			$params[$key]['STATUS']=1;
		}
		return $this->saveAll($params);
	}
	Public Function Delete_Notice($_userid){
		$params=array();
		$params=array('Notice.USR_ID'=>$_userid);
		$this->deleteAll($params);
		$params2=array();
		$params2=array('Notice.P_USR_ID'=>$_userid);
		$this->deleteAll($params2);

	}
}