<?php
/**
 * This file is part of the transcoding library. It contains the
 * definition of the {@link TranscodingActionCSSAdaptation} class.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package TransPythia
 * @version $Revision: 1.4 $
 * @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');


if(!defined('px_per_em')){
	define('px_per_em', 14);
}
if(!defined('pt_per_em')){
	define('pt_per_em', 12);
}

if(!defined('limit_px')){
	define('limit_px', 100);
}

define('test','/home/lequeux/projet_w3c/wordpress/wp-content/themes/default/style.css_bak');


/**
 * Transcoding action that adapts CSS content to improve
 * content layout on mobile devices. 
 * 
 * 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.
 * 
 * As opposed to most of the other transcoding actions of this library, this
 * action operates on CSS content, not on XHTML content.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package TransPythia
 * @version $Revision: 1.4 $
 * @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 TranscodingActionCSSAdaptation extends TranscodingAction{
	
	/**
	 * Adapts the CSS content to better suit common capabilities of mobile devices.
	 * 
	 * @param string $content The HTML content to transcode.
	 * @param Evidence $evidence The evidence that identifies the requesting device.
	 * @return string The transcoded content.
	 * @exception SystemException The evidence is not valid.
	 */
	public function apply($content=test){
		require_once( dirname(__FILE__) . '/cssparser.php' );
		$problems = array('px'=>array(), 'pt'=>array(), 'backgroundimage'=>array());
		$warning = array();
		
		$css = new cssparser();
		$css->Parse($content);
		foreach($css->css as $markup=>$rules){
			if(count($rules) > 0){
				foreach($rules as $name=>$rule){
					if(strpos($rule, 'px') !== false){
						if(strpos($name, 'border') !== false ||
							strpos($name, 'margin') !== false ||
							strpos($name, 'padding') !== false){
							preg_match_all('/([0-9\.]*)px/', $rule, $matches);
							foreach($matches[0] as $i=>$match){
								if(intval($matches[1][$i]) >  limit_px){
									$warning[] = $markup . '{'.$name.':'.$rule.'}'
										.' => value is too big. This could be a problem.';
								}
							}
						}
						else{
							$problems['px'][] = array($markup,$name,$rule);
						}
					}
					if(strpos($rule, 'pt') !== false){
						if(strpos($name, 'border') !== false ||
							strpos($name, 'margin') !== false ||
							strpos($name, 'padding') !== false){
							preg_match_all('/([0-9\.]*)pt/', $rule, $matches);
							foreach($matches[0] as $i=>$match){
								if(intval($matches[1][$i]) >  limit_px){
									$warning[] = $markup . '{'.$name.':'.$rule.'}'
										.' => value is too big. This could be a problem.';
								}
							}
						}
						else{
							$problems['pt'][] = array($markup,$name,$rule);
						}
					}
					if(strpos($rule, 'url') !== false){
						if(strpos($name, 'background-image') !== false){
							$warning[] = $markup . '{'.$name.':'.$rule.'}'
								. ' => background-image deprecated. You should use background-color.';
						}
						else if(strpos($name, 'background') !== false){
							$problems['backgroundimage'][] = array($markup, $name, $rule);
						}
					}
					if(strpos($name, 'float') !== false){
						$warning[] = $markup . '{'.$name.':'.$rule.'}'
							. ' => float is deprecated if you use it with a margin/padding rule';
					}
				}
			}
		}
		
		
		echo '<H1>Transformations done :</h1>';
		if(count($problems['px']) > 0){
			echo '<H2>Problems with px :</h2>';
			foreach($problems['px'] as $i => $problem){
				echo $i . ') [' . $problem[0] . '] => '.$problem[1] . ' : ' . $problem[2];
				$value = $problem[2];
				preg_match_all('/([0-9\.]*)px/', $value, $matches);
				foreach($matches[0] as $i=>$val){
					$em_val = round($matches[1][$i] / px_per_em, 2);
					$css->css[$problem[0]][$problem[1]] = str_replace($val, $em_val . 'em', $css->css[$problem[0]][$problem[1]]);
					echo '=> changed to ' . $em_val . 'em';
				}
				echo '<br />';
			}
		}
		if(count($problems['pt']) > 0){
			echo '<H2>Problems with pt :</h2>';
			foreach($problems['pt'] as $i => $problem){
				echo $i . ') [' . $problem[0] . '] => '.$problem[1] . ' : ' . $problem[2];
				$value = $problem[2];
				preg_match_all('/([0-9\.]*)pt/', $value, $matches);
				foreach($matches[0] as $i=>$val){
					$em_val = round($matches[1][$i] / pt_per_em, 2);
					$css->css[$problem[0]][$problem[1]] = str_replace($val, $em_val . 'em', $css->css[$problem[0]][$problem[1]]);
					echo '=> changed to ' . $em_val . 'em';
				}
				echo '<br />';
			}
		}
		if(count($problems['backgroundimage']) > 0){
			echo '<H2>Problems with background images :</h2>';
			foreach($problems['backgroundimage'] as $i => $problem){
				echo $i . ') [' . $problem[0] . '] => '.$problem[1] . ' : ' . $problem[2];
				$value = $problem[2];
				
				preg_match_all('/#[a-fA-F0-9]{3,6}/', $problem[2], $matches);
				if(strtolower($problem[1]) == 'background' && count($matches[0]) > 0 ){
					$color = $matches[0][0];
				}
				
				if(!isset($color)){
					echo 'Need to set default (WARNING)';
				}
				else{
					$css->css[$problem[0]][$problem[1]] = preg_replace('|url\([^ ]*\)|', '', $problem[2]);
					echo '=> changed to ' . $color;
				}
				echo '<br />';
			}
		}
		
		echo '<h1>Warnings :</h1>';
		foreach($warning as $i=>$warn){
			echo $i . ') ' . $warn . '<br />';
		}
		
		$handle = fopen($content.'_modified', 'w');
		fwrite($handle, $css->GetCSS());
		fclose($handle);
	}
	
}

$test = new TranscodingActionCSSAdaptation();
echo 'begin to parse : '.test.' <br />';
$test->apply();


echo '<br /> end <br />';


?>