<?php
/**
 * This file is part of the transcoding library. It contains the
 * definition of the {@link TranscodingActionDeletePopup} class.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package TransPythia
 * @version $Revision: 1.15 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */


/**
 * Include the {@link TranscodingAction} base class definition.
 */
require_once(dirname(__FILE__) . '/transcodingaction.php');


/**
 * Transcoding action that updates links to separate windows (i.e. popup
 * windows) so that they open in the same window when the requesting device
 * is a mobile device.
 * 
 * The DDR property reference that the transcoding action uses to tell whether
 * the requesting device is mobile or not may be defined as a "mobile_device"
 * option through a call to {@link TranscodingAction::setOption()}. The action
 * uses the is_wireless_device property of the WURFL namespace when the option
 * is not set.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package TransPythia
 * @version $Revision: 1.15 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */
class TranscodingActionDeletePopup extends TranscodingAction {
	
	/**
	 * Removes links that would normally trigger a popup window from the
	 * given content when the requesting device is a mobile device.
	 * 
	 * @param Evidence $evidence The evidence that identifies the requesting device.
	 * @param string $content The HTML content to transcode.
	 * @return string The transcoded content.
	 * @exception SystemException The evidence is not valid.
	 */
	public function apply($content, $evidence){
		$this->initPropertyValues($evidence);
		
		$this->initproperty('mobile_device',
			TranscodingAction::$WURFL_MOBILE_DEVICE,
			TranscodingAction::$WURFL_VOCABULARY,
			TranscodingAction::$WURFL_DEFAULT_ASPECT);
			
		$property = $this->getOption('mobile_device');
		$is_mobile_device = $this->getPropertyValuePr($property);
		
		if(!isset($is_mobile_device) || !$is_mobile_device->getBoolean()){
		  	return $content;
		}
		
		// Search and replace all occurrences of target="_blank" within
		// the markup. The replace may remove target="_blank" from comments
		// but does not suppress target="_blank" if it appears outside of a
		// link (Case-insentive search as HTML is case-insensitive)
		$markup = '(a|area|form|link|base)';
		$pattern = '(<'.$markup.'(\s[^>]*)?)\starget=["\']_blank["\']([^>]*>)';
		$adaptedContent = preg_replace('/'.$pattern.'/Usi', '\1\4', $content);
		
		return $adaptedContent;
	}
}

?>