<?php
/**
 * Private Sales fix
 */
class modContent extends modContent_parent
{
    /**
     * Ids of contents which can be accessed without any restrictions when private sales is ON
     * @var array
     */
    protected $_aPsAllowedContents = array( "oxagb", "oxrightofwithdrawal", "oximpressum" );
	 
    /**
	 * Returns name of template to render..
	 *
	 * @return string
	 */
	public function render()
    {
        if ( !$this->_canShowContent( $this->getContentId() ) ) {
            oxUtils::getInstance()->redirect( $this->getConfig()->getShopHomeURL() . 'cl=account' );
        }
		
	    return parent::render();
	}
	
    /**
     * Checks if content can be shown
     *
     * @param string $sContentId id of content to display
     *
     * @return bool
     */
    protected function _canShowContent( $sContentId )
    {
        $blCan = true;
        if ( $this->getConfig()->getConfigParam( 'blPsLoginEnabled' ) &&
             !$this->getUser() && !in_array( $sContentId, $this->_aPsAllowedContents ) ) {
            $blCan = false;
        }
        return $blCan;
    }
	
    /**
     * Returns true if user forces to display plain template or
     * if private sales switched ON and user is not logged in
     *
     * @return bool
     */
    public function showPlainTemplate()
    {
        $blPlain = oxConfig::getParameter( 'plain' );
        if ( $blPlain === null ) {
            $blPlain = false;
            $oUser   = $this->getUser();
            if ( $this->getConfig()->getConfigParam( 'blPsLoginEnabled' ) &&
                 ( !$oUser || ( $oUser && !$oUser->isTermsAccepted() ) ) ) {
                $blPlain = true;
            }
        }
        return $blPlain;
    }
	
    /**
     * Template variable getter. Returns active content id.
     * If no content id specified, uses "impressum" content id
     *
     * @return object
     */
    public function getContentId()
    {
        if ( $this->_sContentId === null ) {
            $oConfig    = $this->getConfig();
            $sContentId = oxConfig::getParameter( 'oxcid' );

            if ( !$sContentId ) {
                //trying to load content id from tpl variable
                //usage of tpl variable as content id is deprecated
                $sContentId = oxConfig::getParameter( 'tpl' );
            }

            if ( !$sContentId ) {
                //get default content id (impressum)
                $sContentId = parent::getContentId();
            }

            $this->_sContentId = false;
            $oContent = oxNew( 'oxcontent' );
            if ( $oContent->load( $sContentId ) && $oContent->oxcontents__oxactive->value ) {
                $this->_sContentId = $sContentId;
                $this->_oContent = $oContent;
            }
        }
        return $this->_sContentId;
    }
}