<?php

class Brivium_Credits_DataWriter_ThreadWatch extends XFCP_Brivium_Credits_DataWriter_ThreadWatch
{
	protected function _postSave()
	{
		$creditModel = $this->_getCreditModel();
		
		if(isset($GLOBALS['BRC_CPT_actionWatch']) && !empty($GLOBALS['BRC_CPT_actionWatch']['user_id'])){
			if($GLOBALS['BRC_CPT_actionWatch']['user_id']!=$this->get('user_id')){
				$creditModel->setIsWaitSubmit(true);
				if($creditModel->updateUserCredit('watchThread',$this->get('user_id'))){
					$dataCredit = array(
						'user_action_id' 	=>	$this->get('user_id'),
					);
					$creditModel->updateUserCredit('threadGetWatched',$GLOBALS['BRC_CPT_actionWatch']['user_id'],$dataCredit);
				}
				$creditModel->commitUpdate();
			}
		}else{
			$creditModel->updateUserCredit('watchThread',$this->get('user_id'));
		}
		return parent::_postSave();
	}

	/**
	 * Post-delete handler
	 */
	protected function _postDelete()
	{
		$creditModel = $this->_getCreditModel();
		$dataCredit = array(
			'reverted' 	=>	true,
		);
		if(isset($GLOBALS['BRC_CPT_actionWatch']) && !empty($GLOBALS['BRC_CPT_actionWatch']['user_id'])){
			if($GLOBALS['BRC_CPT_actionWatch']['user_id']!=$this->get('user_id')){
				$creditModel->setIsWaitSubmit(true);
				if($creditModel->updateUserCredit('watchThread',$this->get('user_id'),$dataCredit)){
					$dataCredit['user_action_id'] = $this->get('user_id');
					$creditModel->updateUserCredit('threadGetWatched',$GLOBALS['BRC_CPT_actionWatch']['user_id'],$dataCredit);
				}
				$creditModel->commitUpdate();
			}
		}else{
			$creditModel->updateUserCredit('watchThread',$this->get('user_id'),$dataCredit);
		}
		return parent::_postDelete();
	}
	/**
	 * Gets the action model.
	 *
	 * @return Brivium_Credits_Model_Credit
	 */
	protected function _getCreditModel()
	{
		return $this->getModelFromCache('Brivium_Credits_Model_Credit');
	}
	
}