------------------------------------------------------------
revno: 1112
committer: Roger Martin <[email protected]>
branch nick: aikiframework
timestamp: Sun 2012-02-26 22:02:15 +0100
message:
  Engine purephp add, v8 renamed as v2
removed:
  libs/Engine_v8.php
added:
  libs/Engine_purephp.php
  libs/Engine_v2.php
modified:
  libs/widgets.php


--
lp:aikiframework
https://code.launchpad.net/~aikiframework-devel/aikiframework/trunk

Your team Aiki Framework Developers is subscribed to branch lp:aikiframework.
To unsubscribe from this branch go to 
https://code.launchpad.net/~aikiframework-devel/aikiframework/trunk/+edit-subscription
=== added file 'libs/Engine_purephp.php'
--- libs/Engine_purephp.php	1970-01-01 00:00:00 +0000
+++ libs/Engine_purephp.php	2012-02-26 21:02:15 +0000
@@ -0,0 +1,124 @@
+<?php
+
+/**
+ * Aiki Framework (PHP)
+ *
+ * LICENSE
+ *
+ * This source file is subject to the AGPL-3.0 license that is bundled
+ * with this package in the file LICENSE.
+ *
+ * @author	  Roger Martin (rg1024) - Aikilab http://www.aikilab.com
+ * @copyright   (c) 2008-2011 Aiki Lab Pte Ltd
+ * @license	 http://www.fsf.org/licensing/licenses/agpl-3.0.html
+ * @link		http://www.aikiframework.org
+ * @category	Aiki
+ * @package	 Library
+ * @version 0.01 ALPHA!!
+ * @filesource
+ * PARENT
+ * update aiki_widgets inner join aiki_widget_porsi on aiki_widgets.father_widget=aiki_widget_porsi.id set parent_widget= aiki_widget_porsi.widget_name
+ */
+
+if (!defined('IN_AIKI')) {	
+
+	die('No direct script access allowed');
+} 
+	
+//@TODO move to bootstrap, when it is stable.
+include_once("markup.php");
+
+class engine_purephp {
+
+    // engine parameters
+	private $mode ="stream";
+	
+	private $widget_css;
+	private $widget_html;
+		
+	/*
+	 * Create layout
+	 */
+
+	function layout( $parameters = NULL ){
+		global $aiki;
+
+		// Parameters
+		$this->load_parameters($parameters);
+												
+		// @TODO javascript? id for some elements?
+		$this->target = array(
+			"body"=>"" ,
+			"header"=>"",
+			"css"=>array() );
+
+		$aiki->widgets->render($this);
+		
+        // finally make html.
+		return $this->render_html();
+	}
+
+
+	function load_parameters($parameters) {
+		// Parameters
+		if ( is_array( $parameters ) ){
+			return;
+		}	
+		$modes = array("stream"=>true, "direct"=>true, "return"=>true);
+		
+		// set mode to use
+		if ( isset($parameters["mode"]) && isset($modes[$parameters["mode"]])){
+			$this->mode= $parameters["mode"];
+		}
+							
+	}
+
+	function render_html(){	
+		global $aiki;
+		if ($this->mode != "direct") {
+			$html  = $aiki->Output->header($this->target['css'],  $this->target['header']);
+			$html .= $aiki->Output->body($this->target['body']);
+			$html .= $aiki->Output->end();
+			return $html;
+		}		
+	}
+
+
+	/**
+	 *  parse a given widget
+	 */
+
+
+	function parseWidget($widgetID){
+		global $aiki;
+		                			 
+		if ( is_object($widgetID) ){					
+			$widget= $widgetID;
+		} else {
+			$widget = $aiki->widgets->get_widget($widgetID);			
+		}
+				
+		switch ( $this->mode){
+			case "direct":
+				eval($widget->widget);
+				break;
+			
+			case "stream":
+				ob_start();
+				eval($widget->widget);
+				$content = ob_get_contents();
+				ob_end_clean();
+				break;
+			
+			case "return":
+				$content= eval($widget->widget);
+				
+		}
+		
+		if ( is_debug_on() && $this->mode!="direct" ) {
+			return "\n<!-- start {$widget->widget_name} ({$widget->id}) -->$content\n<!-- end {$widget->widget_name} ({$widget->id}) -->";
+		}		
+    
+    }
+
+}

=== added file 'libs/Engine_v2.php'
--- libs/Engine_v2.php	1970-01-01 00:00:00 +0000
+++ libs/Engine_v2.php	2012-02-26 21:02:15 +0000
@@ -0,0 +1,440 @@
+<?php
+
+/**
+ * Aiki Framework (PHP)
+ *
+ * LICENSE
+ *
+ * This source file is subject to the AGPL-3.0 license that is bundled
+ * with this package in the file LICENSE.
+ *
+ * @author	  Roger Martin (rg1024) - Aikilab http://www.aikilab.com
+ * @copyright   (c) 2008-2011 Aiki Lab Pte Ltd
+ * @license	 http://www.fsf.org/licensing/licenses/agpl-3.0.html
+ * @link		http://www.aikiframework.org
+ * @category	Aiki
+ * @package	 Library
+ * @version 0.01 ALPHA!!
+ * @filesource
+ * PARENT
+ * update aiki_widgets inner join aiki_widget_porsi on aiki_widgets.father_widget=aiki_widget_porsi.id set parent_widget= aiki_widget_porsi.widget_name
+ */
+
+if (!defined('IN_AIKI')) {	
+
+	die('No direct script access allowed');
+} 
+	
+//@TODO move to bootstrap, when it is stable.
+include_once("markup.php");
+
+
+class engine_v2 {
+
+    // engine parameters
+	private $convert_widgets;
+	private $markup_codes;
+	
+	private $parsers; // array of parser (markup=>callback)
+	
+	private $widget_css;
+	private $widget_html;
+		
+	/*
+	 * Create layout
+	 */
+
+	function layout( $parameters = NULL ){
+		global $db, $aiki;
+
+		// Parameters
+		$this->load_parameters($parameters);
+												
+		// @TODO javascript? id for some elements?
+		$this->target = array(
+			"body"=>"" ,
+			"header"=>"",
+			"css"=>array() );
+
+		$aiki->widgets->render($this);
+		
+        // finally make html.
+		return $this->render_html();
+	}
+
+
+	function load_parameters($parameters) {
+		// Parameters
+		if ( is_array( $parameters ) ){
+			$parameters= array();				
+		}	
+		
+		// set markup to use
+		$markup = isset($parameters['markup'])	? $parameters['markup'] : 'aiki';
+		$this->markup_codes    = aiki_get_markup_codes ($markup ) ;		
+		
+		// must convert widgets?
+		if  (isset($parmameter['convert-widgets'] )) {
+			$this->convert_widgets = 1;
+			$this->markup          = 'aiki';
+		}
+		
+		// set parsers
+		$this->parsers = array (
+		    ""            => "parse_vars",
+		    "widget"      => "parse_widget",
+			"permissions" => "parse_permissions",
+			"view"        => "parse_view",
+			"noaiki"      => "parse_noaiki",
+			"sql"         => "parse_sql",
+			"script"      => "parse_script",
+			"t"			  => "parse_t",
+			"__"		  => "parse_translate");
+		
+		if ( isset($parameters["parsers-allowed"]) ){
+			$this->parsers = array_intersect ( 
+				$this->parsers , 
+				array_flip(explode(",",$parameters["parsers-allowed"])) );
+		}
+		
+		if ( isset($parameters["parsers-disabled"]) ){
+			$this->parsers = array_diff_key ( 
+				$this->parsers , 
+				array_flip(explode(",",$parameters["parsers-disabled"])) );
+		}
+	}
+
+
+	function render_html(){	
+		global $aiki;
+		$html  = $aiki->Output->header($this->target['css'],  $this->target['header']);
+		$html .= $aiki->Output->body($this->target['body']);
+		$html .= $aiki->Output->end();
+
+		return $html;
+	}
+
+
+	/**
+	 *  parse a given widget
+	 */
+
+
+	function parseWidget($widgetID){
+		global $aiki;
+		                			 
+		if ( is_object($widgetID) ){					
+			$widget= $widgetID;
+		} else {
+			$widget = $aiki->widgets->get_widget($widgetID);			
+		}
+
+		if (!is_object($widget)){
+			return "";
+		}
+
+
+		// high level parser  can be:
+		// regex (string)  => function | array(object,method).
+		// a numeric index => function | array(object,method).
+		// @TODO make a structure.
+
+		$Parsers = array (
+		    // preparsers
+		    '/\(\([a-z0-9_]\)\)/i'                    => array( $this, "parse_vars"),
+		    '/\[GET\[([a-z0-9]+)\]\]/i'				   => array( $this, "parse_get"),
+		    '/\[POST\[([a-z0-9]+)\]\]/i'			   => array( $this, "parse_post"),
+		    "/\(template\(([a-z0-9_]*)\)template\)/Ui" => array( $this, "parse_template"),
+		    
+		    // here we call the parse
+		    1 => array ($this, "parse" ),
+		    
+		    // post-parser		    
+		    array ($aiki->languages,"L10n") // added for test purpose only
+		    ) ;
+                		
+        $content= $widget->widget;                			 		    
+		foreach ( $Parsers as $pattern => $callback ){
+			if ( is_string($pattern) ){
+				$content = preg_replace_callback ( $pattern, $callback, $content);
+			} else {
+				$content = call_user_func   ( $callback, $content);
+			}
+		}
+        
+		if ( is_debug_on() ){
+			return "\n<!-- start {$widget->widget_name} ({$widget->id}) -->{$content}\n<!-- end {$widget->widget_name} ({$widget->id}) -->";
+		}		
+    
+    }
+
+
+	function parse($text){
+		
+		// extract beign,tag,condition & true-block, else-block,rest
+		$match = extract_markup ( $widget,$this->markup_codes );
+		if (!is_array($match) ){
+			return $text;			
+		}
+		   	
+		$parserToCall= $match[2]; // for comodity
+		
+		if ( isset( $this->parsers[ $parserToCall]) ){
+			$result = call_user_func( array($this, $this->parsers[ $parserToCall] ), &$match[1], &$match[2]);
+		} else {
+			$result = t("Parser $parserToCall not found");
+		}
+						
+		// return begin+result+parse the the rest..				
+	    return $match[0].
+	           $result.
+	           $this->parse($match[3]);
+	}
+
+
+    /**
+     *  parse vars
+     */
+
+	function parse_vars($match){
+		static $bufferReplace;
+		global $aiki, $page;
+
+		if ( $bufferReplace == NULL ) {
+			/* @TODO unified with aiki processVars*/
+
+			$pretty = $aiki->config->get('pretty_urls', 1);
+			$url = $aiki->config->get('url');
+
+			$current_month = date("n");
+			$current_year = date("Y");
+			$current_day = date("j");
+
+			// calculate view, prefix, route
+			$view = $aiki->site->view();
+			$language = $aiki->site->language();
+			$prefix = $aiki->site->prefix();
+			$view_prefix= $aiki->site->view_prefix();
+				
+			$paths= array();
+			if ($prefix) {
+				$paths[] = $prefix;
+			}
+
+			if ($view_prefix) {
+				$paths[] = $view_prefix;
+			}
+			if ( count($aiki->site->languages()) > 1 ) {
+				$paths[] = $language;
+			}
+			$paths = implode("/",$paths);
+			
+			if ( isset($_SERVER["HTTPS"])) {
+				$url = str_replace("http://";, "https://";, $url);
+			}	
+
+			$trimedUrl = preg_replace('#/$#',"",$url); // reg: remove ending /
+							
+			$bufferReplace = array(
+				'[$userid]'	=> $aiki->membership->userid,
+				'[$full_name]' => $aiki->membership->full_name,
+				'[$username]'  => $aiki->membership->username,
+				'[$user_group_level]' => $aiki->membership->group_level,
+				'[$user_permissions]' => $aiki->membership->permissions,			
+				'[$language]'  => $aiki->site->language(),		   
+				'[$page]'	  => $page,
+				'[$site_name]' => $aiki->site->site_name(),
+				'site'	       => $aiki->site->get_site(),
+				'[$view]'	  => $aiki->site->view(),
+				'[$direction]' => $aiki->languages->dir,
+				'[$insertedby_username]' => $aiki->membership->username,
+				'[$insertedby_userid]' => $aiki->membership->userid,
+				'[$current_month]' => $current_month,
+				'[$current_year]' => $current_year,
+				'[$current_day]' => $current_day,
+				'[$root]'		  => $url,
+				'[$root-language]' => $trimedUrl .  "/" . $aiki->site->language(),
+				'[$site_prefix]'   => $prefix ,
+				'[$view_prefix]'   => $view_prefix ,
+				'[$route]'		 => $trimedUrl.  "/". $paths,
+				'[$route-local]'	 => $paths );
+			}
+
+		$token = $match[1];
+		if ( isset($bufferReplace[$token]) ){
+			return $bufferReplace[$token];
+		} else {
+			return $match[0];
+		}
+	}
+
+	function parse_get( $matchs){
+		$token= $matchs[0];
+		return $token && isset($_GET[ $token]) ? $_GET[$token] : "";
+	}
+
+	function parse_post( $token){
+		$token= $matchs[0];
+		return $token && isset($_POST[$token]) ? $_POST[$token] : "";
+	}
+
+
+	/*
+	 * Parse template
+	 */
+	function parse_template($matches){
+		global $db;
+		$id= $this->get_widget_id($matches[1]);
+		return  is_null($id) ? "": $db->get_var ("SELECT widget FROM aiki_widgets WHERE id='$id'" );
+	}
+
+	function parse_script($cond,$trueblock,$elseblock){
+		global $aiki;
+		return $aiki->AikiScript->parser($trueblock,false);
+	}
+
+	function parse_if($cond,$trueblock,$elseblock){	
+		return ( is_array($cond)? first($cond): $cond ) ? $trueblock : $elseblock  ;	
+	}
+
+	/**
+	 * translation
+	 */
+
+	function parse_t($term) {
+		static $translate;
+		global $aiki;
+		if ( is_null($translate) ){
+			$translate= $aiki->site->language()!="en";
+		}
+		return $translate ? t($term): $term ;
+	}
+
+
+	function parse_translate($term) {
+		return __($term);		
+	}
+
+
+	/*
+	 * Parse sql markup
+	 */
+
+	function parse_sql( $para, $true, $else ){
+		global $db;				
+		
+		$results = $db->get_results($para[0]);
+
+		$html="";
+		if ($results) {
+			foreach ( $results as $row ) {
+				$fields= array();
+				foreach ( $row as $field=>$value ){
+					$fields[ "[$field]" ]= $value;
+				}
+				$html .= strtr( $true, $fields);
+			}
+		} else {
+			return $else;
+		}
+		return $html;
+
+	}
+
+	/*
+	 * Parse hits
+	 * @TODO implements trigger.
+	 */
+
+	private function parse_hits($hit, $true, $else ) {
+		global $db;
+		
+		if ( len($hit) == 3 ){
+			$db->query(
+					"UPDATE {$hit[0]}".
+					" SET {$hit[2]}={$hit[2]}+1".
+					" WHERE {$hit[1]}");
+		} elseif (is_debug_on() ) {
+				return sprintf( __("BAD HITS PARAMETERS: 3 expected, %d  given"), len($hit) );
+		}
+		return "";
+	}
+
+
+	function parse_widget( $cond, $true, $false ){
+		//@TODO
+		//return  $this->parse($widgetId, $select);
+	}
+
+
+	function parse_view( $para, $true, $false){
+		global $aiki;
+		
+		list($view,$language)= exlode("/",$filter[1]."/*",2);
+	
+		if  ( match_pair_one( $view, $aiki->site->view()) &&
+		      match_pair_one( $language, $aiki->site->language() )){
+			return $true;
+		}
+		return $false;
+
+	}
+
+
+	function parse_permission($para, $true, $false){
+		global $aiki, $db;
+		if ( strpos($widget,"||") !== false ){
+			list($filter,$content) = explode("||", $widget, 2);
+		} else {
+			return $widget;
+		}
+
+		/* fake permission */
+		if ( trim($filter) == "user" ){
+			return $content;
+		}
+		return "";
+
+		$sql = "SELECT group_level" .
+			   " FROM  aiki_users_groups".
+			   " WHERE group_permissions='". addslashes($filter) ."'";
+
+		$get_group_level = $db->get_var($sql);
+
+		if ( trim($filter) == $aiki->membership->permissions ||
+			$aiki->membership->group_level < $get_group_level ) {
+			return $content;
+		}
+
+		return "";
+	}
+
+	function parse_noaiki(&$cond, &$text, $else){
+		return $text;
+	}
+
+
+	function convert_widget($widget){
+	
+		// no_loop
+		if ( trim($widget->normal_select)!="" ){
+			$loop="";
+			$bottom="";
+			$resul=false;
+			if ( preg_match ( "#\(noloop\((.*)\)noloop\)#m",$widget->widget,$resul)){
+				$loop= $resul[1];
+				$widget= str_replace($resul[0],"",$widget->widget);
+			}
+			if ( preg_match ( "#\(noloop_bottom\((.*)\)noloop_bottom\)#m",$widget->widget,$resul)){
+				$bottom= $resul[1];
+				$widget= str_replace($resul[0],"",$widget->widget);
+			}
+						
+			$widget->widget = "$loop(sql(\"{$widget->normal_select}\"||{$widget->widget})sql)$bottom";
+		}			
+		return $widget;
+	}
+
+
+
+}

=== removed file 'libs/Engine_v8.php'
--- libs/Engine_v8.php	2012-02-25 22:20:11 +0000
+++ libs/Engine_v8.php	1970-01-01 00:00:00 +0000
@@ -1,726 +0,0 @@
-<?php
-
-/**
- * Aiki Framework (PHP)
- *
- * LICENSE
- *
- * This source file is subject to the AGPL-3.0 license that is bundled
- * with this package in the file LICENSE.
- *
- * @author	  Roger Martin (rg1024) - Aikilab http://www.aikilab.com
- * @copyright   (c) 2008-2011 Aiki Lab Pte Ltd
- * @license	 http://www.fsf.org/licensing/licenses/agpl-3.0.html
- * @link		http://www.aikiframework.org
- * @category	Aiki
- * @package	 Library
- * @version 0.01 ALPHA!!
- * @filesource
- * PARENT
- * update aiki_widgets inner join aiki_widget_porsi on aiki_widgets.father_widget=aiki_widget_porsi.id set parent_widget= aiki_widget_porsi.widget_name
- */
-
-if (!defined('IN_AIKI')) {	
-
-	die('No direct script access allowed');
-} 
-	
-
-/*
- * move to aiki, so all engine case share this code
- */
-
-function aiki_get_markup_codes($which){
-	/* @TODO this complicate array can be generated by a auxiliary programs, who with a given
-	 * syntax ( for example (TAG(TRUE-BLOCK)else(FALSE-BLOCK)TAG) calculate array
-	 */
-	
-	// 0  begin regex
-	// 1  end regex
-	// 2  else literal
-	// 3  end search string with %s
-	// 4  begin signal (first character of a begin tag)
-	// 5  trim characters that will be deleted from tag.
-	// 6  first character from true-block
-	
-   $markups= array (
-        //  (TAG()TRUE-BLOCK)else(FALSE-BLOCK)TAG)
-		"aiki" => array("\([A-z0-9_]*\(", "\)[A-z0-9]*\)", ")else(", ")%s)","(", "#\((.*)\(#","" ),				
-		//  (TAG( para {TRUE-BLOCK}else{FALSE-BLOCK})TAG)
-		"aiki2" => array("\([A-z0-9_]*\(", "\}\)[A-z0-9]*\)", "})else({", "})%s)","(", "#\((.*)\(#","" ),
-		// (TAG(COND){true-block}else{false-block}TAG)
-		"(c)"=> array("\([A-z0-9_]+\(","\}[A-z0-9]+\)", "}else{", "}%s)", "(","#\((.*)\(#", "{" ),
-		// <%TAG(COND){true-block}else{false-block}%>
-		"<%"=> array("<%[A-z0-9_]+\(","\}%>", "}else{", "}%>", "<","#<%(.*)\(#","" )
-		);
-
-	return ( isset ($markups[$which]) ? $markups[$which] : $markups["aiki"] );	
-	
-}
-
-/**
- * extract the first markup 
- * return array
- *     0 => text before tag
- *     1 => tag
- *     2 => array of parameters
- *     3 => true-block
- *     4 => false-block
- *     5 => text after tag
- */
-
-function extract_markup(&$string, $markup)  {
-		
-	if (!is_array($markup) ){
-		$markup= aiki_get_markup_codes($markup);
-	}
-				
-	if ( !preg_match_all(	
-			"#{$markup[0]}|{$markup[1]}|". preg_quote($markup[2])."#",
-			$string,
-			$matches,
-			PREG_OFFSET_CAPTURE)) {
-		return $string ;
-	};
-	
-	$matches= $matches[0];
-	$max= count($matches);
-	preg_match ( $markup[5], $matches[0][0],$temp);
-	$tag = $temp[1];	
-	
-	$search= sprintf( $markup[3], $tag );	
-	$level = 0;
-	$else  = 0; // where start the else block
-	
-	for($i=1;$i<$max;$i++){
-		$found = $matches[$i][0]; // for comodity		
-		if ( $found==$markup[2] ) {
-			if ($level==0 ){ // else are in the middle
-				$else= $matches[$i][1];
-			}
-		} elseif ($level==0 && $found==$search ) {
-			// we found ending tag
-			$offset= strlen($search);
-			$start = $matches[0][1]+$offset;	
-			$elselen= strlen($markup[2]);	
-			$trueBlock= ( $else ? substr( $string,$start, $else-$start) : substr( $string,$start, $matches[$i][1]-$start));
-			$parameters = extract_parameters( $trueBlock, $markup[6]);			
-			return array( 											
-				substr( $string,0,$start-$offset ),
-				$tag,
-				$parameters[0],
-				$parameters[1],
-				( $else ? substr( $string,$else+$elselen, $matches[$i][1]-$else-$elselen) : NULL), 
-				substr($string, $matches[$i][1]+$offset));
-		} elseif ( $found[0] =="(" ){
-			$level++;
-		} else {
-			$level--;
-		}			
-	}	
-	return $string;	
-}
-
-
-function eval_parameters( $originals ){
-	
-	$ret = array();
-	$temp= false;
-	
-	foreach ( $originals as $input){
-			if ( $input==="" )         { $ret[]="";}
-			elseif ( $input==="0" )    { $ret[]=0;}
-			elseif ( $input==="false" ){ $ret[]=false;}	
-			elseif ( $input==="true"  ){ $ret[]=true;}	
-			elseif ( $input[0]=="'" || $input[0]=='"' ) { $ret[]= substr($input,1,-1);}
-			elseif ( preg_match('#^\s*[+\-]?[1-9][0-9]*\s*$#', $input, $temp))       { $ret[]= (int)   $input;}
-			elseif ( preg_match('#^\s*[+\-]?[1-9][0-9]*\.[0-9]*\s*$#', $input, $temp)){ $ret[]= (float) $input;}		
-			elseif ( preg_match('#^\{\s*"[^"]+"\s*\:\s*"[^"]*"#',$input, $temp)){ $ret[]= json_decode($input);}
-			elseif ( $input[0]=="{") { 
-				$temp = extract_parameters( substr($input,1,-1));
-				$ret[]= eval_parameters( $temp[0] );}
-			elseif ( preg_match ('#^\s*aiki\-\>(.*)$#',$input,$temp )){
-				$ret[]= "calling aiki->".$temp[1]; //@TODO implements
-			}elseif ( preg_match ('#^\s*([A-z_0-9]+)\(.*$#', $input, $temp )){				
-				// @TODO check if it is a allowed functions
-				$ret[] = "calling {$temp[1]}";
-				
-			} else {
-				$ret[]=$input;
-			}
-	}
-	
-	return $ret;
-		
-}
-
-
-/**
- * extract the parameters present at the begining of text
- * @return array
- *     0 => array of parameters.
- *     1 => rest of string
- * example: extract_parameters ( "foo'bar",foo(bar),{foo,bar,foo()} ) rest"
- *   return a array 
- *        0=>array ( "foo'bar", "foo(bar)", "{foo,bar,foo()}")
- *        1=> " rest"
- */
-
-function extract_parameters( $string, $find="") {	
-		
-	$max= strlen($string);
-	
-	$state= 0;
-	$current= false;
-	$end = false;	
-	$level =0;
-	$ret= array();
-	
-	
-	for($i=0; $i<$max && !$end; $i++){
-		$add = false;
-		$char = $string[$i];
-		
-		switch ( $char ){
-				
-			case "'": 
-				switch ($state){					
-					case 0: $state=1; break;
-					case 1: if ($level==0 && !$escaped ){ $add= true;}					
-				}					
-				break;
-			case '"':
-				switch ($state){
-					case 0: $state=2; break;
-					case 2: if ( $level==0 && !$escaped ) { $add= true;}
-				}				
-				break;
-			// do we found a other arg?
-			case ',':
-				if (($state==0 || $state==3) && $level==0) { 
-					$add = true;
-					$char= false;
-				}
-				break;
-			
-			// we a begin (
-			case '(':
-			case '{':
-				if ($state==0 || $state==3) {
-					$level++;
-					$state=3;
-				}
-				break;		
-			
-			// do we found a ending { ?
-			case '}':
-			case ')':
-				if ($state==0 || $state ==3 ) {					
-					if ( $level == 0 ) { 
-						$add = true;
-						$end = true;
-						$char = false;
-					} elseif ($level==1) {								
-						$add = true;
-						$level=0;;
-					} else {
-					   $level--;
-					}			
-				}
-				break;			
-			case " ":										
-			    break;		
-			default :
-				if ( $state== 0 ){
-					$state=3;
-				}						
-		}	
-		
-		if ( $state != 0 ){
-			$current .= $char;
-		}
-				
-		if ( $add ){
-			if ( $current !== false ){
-				$ret[]    = $current;
-				$current  = false;
-			} 
-			$state    = 0;			
-		}
-		
-		$escaped= ( $char=="\\" );
-	} // for
-	if ( $current !== false ){		
-		$ret[]    = $current;
-	}
-	
-	// for the (a,b) { begin ..supress the { @TODO improve.
-    if ( $find !="") {		
-		$temp = stripos ($string,$find,$i);
-		if ( $temp !==false ){
-			$i=$temp+1;
-		}
-    }
-		
-	return array ( $ret, substr( $string,$i));
-}
-
-
-class engine_v8 {
-
-    // engine parameters
-	private $convert_widgets;
-	private $markup_codes;
-	
-	private $parsers; // array of parser (markup=>callback)
-	
-	private $widget_css;
-	private $widget_html;
-		
-	/*
-	 * Create layout
-	 */
-
-	function layout( $parameters = NULL ){
-		global $db, $aiki;
-
-		// Parameters
-		$this->load_parameters($parameters);
-												
-		// @TODO javascript? id for some elements?
-		$this->target = array(
-			"body"=>"" ,
-			"header"=>"",
-			"css"=>array() );
-
-		// the widget is given directly or
-		if (isset($_GET["widget"])) {
-			if ($getWidgetId = $aiki->widgets->get_widget_id($_GET['widget'])) {
-				return $this->parse($getWidgetId);
-			}
-			return;//all work is done
-		}
-
-		// or in url,
-		// search widget and test there is a unique response
-		$module_widgets = $aiki->widgets->get_candidate_widgets();
-		$unique_widget_exists = false;
-		if ($module_widgets) {
-			foreach($module_widgets as $tested_widget){
-				if ($tested_widget->display_urls != "*"){
-					$unique_widget_exists = true;
-					break;
-				}
-			}
-		}
-
-		// Error 404 page not found
-	    $allMatch=false;
-		if (!$unique_widget_exists) {
-			// first look for widget that responds error_404,
-			// else use config error_404.
-			$module_widgets= $aiki->widgets->get_Page_Not_Found_Widgets();
-			if ( $module_widgets ) {
-				$aiki->Errors->pageNotFound(false);
-				$allMatch = true;
-			} else {
-				return $aiki->Errors->pageNotFound(true);
-			}
-		}
-
-		// now filter canditate widgets, before create content
-		foreach ( $module_widgets as $parent ) {
-
-			// first parent
-			if ( $allMatch or
-			    ($aiki->url->match($parent->display_urls) && !$aiki->url->match($parent->kill_urls)) ) {
-				if ( $parent->have_css == 1) {
-					$this->target["css"][] = $parent->id;
-				}
-				$this->target[$parent->widget_target] .= $this->parseWidget($parent->id);
-
-				// children..
-				/* @TODO..a function */
-				if ( is_array($descendants = $aiki->widgets->get_candidate_widgets($parent->id)) ){
-					foreach ($descendants as $descendant){
-						if ( $aiki->url->match($descendant->display_urls) && !$aiki->url->match($descendant->kill_urls) ) {
-							$this->target["css"][] = $descendant->id;
-							$this->target[$descendant->widget_target] .= $this->parseWidget($descendant->id);
-						}
-					}
-				}
-			}
-		}
-
-        // finally make html.
-		return $this->render_html();
-	}
-
-
-	function load_parameters($parameters) {
-		// Parameters
-		if ( is_array( $parameters ) ){
-			$parameters= array();				
-		}	
-		
-		// set markup to use
-		$markup = isset($parmameters['markup'])	? $parmameters['markup'] : 'aiki';
-		$this->markup_codes    = aiki_get_markup_codes ($markup ) ;		
-		
-		// must convert widgets?
-		if  (isset($parmameter['convert-widgets'] )) {
-			$this->convert_widgets = 1;
-			$this->markup          = 'aiki';
-		}
-		
-		// set parsers
-		$this->parsers = array (
-		    ""            => "parse_vars",
-		    "widget"      => "parse_widget",
-			"permissions" => "parse_permissions",
-			"view"        => "parse_view",
-			"noaiki"      => "parse_noaiki",
-			"sql"         => "parse_sql",
-			"script"      => "parse_script",
-			"t"			  => "parse_t",
-			"__"		  => "parse_translate");
-		
-		if ( isset($parameters["parsers-allowed"]) ){
-			$this->parsers = array_intersect ( 
-				$this->parsers , 
-				array_flip(explode(",",$parameters["parsers-allowed"])) );
-		}
-		
-		if ( isset($parameters["parsers-disabled"]) ){
-			$this->parsers = array_diff_key ( 
-				$this->parsers , 
-				array_flip(explode(",",$parameters["parsers-disabled"])) );
-		}
-	}
-
-
-	function render_html(){	
-		global $aiki;
-		$html  = $aiki->Output->header($this->target['css'],  $this->target['header']);
-		$html .= $aiki->Output->body($this->target['body']);
-		$html .= $aiki->Output->end();
-
-		return $html;
-	}
-
-
-	/**
-	 *  parse a given widget
-	 */
-
-
-	function parseWidget($widgetID){
-		global $aiki, $db;
-
-		// high level parser  can be:
-		// regex (string)  => function | array(object,method).
-		// a numeric index => function | array(object,method).
-		// @TODO make a structure.
-
-		$Parsers = array (
-		    // preparsers
-		    '/\(\([a-z0-9_]\)\)/i'                    => array( $this, "parse_vars"),
-		    '/\[GET\[([a-z0-9]+)\]\]/i'				   => array( $this, "parse_get"),
-		    '/\[POST\[([a-z0-9]+)\]\]/i'			   => array( $this, "parse_post"),
-		    "/\(template\(([a-z0-9_]*)\)template\)/Ui" => array( $this, "parse_template"),
-		    
-		    // here we call the parse
-		    1 => array ($this, "parse" ),
-		    
-		    // post-parser		    
-		    array ($aiki->languages,"L10n") // added for test purpose only
-		    ) ;
-                			 		
-		$widgetData = $db->get_row ("SELECT widget,widget_name FROM aiki_widgets WHERE id=" . (int) $widgetID );
-		$widget    = $widgetData->widget;
-		$widgetName= $widgetData->widget_name;
-    
-		foreach ( $Parsers as $pattern => $callback ){
-			if ( is_string($pattern) ){
-				$widget = preg_replace_callback ( $pattern, $callback, $widget);
-			} else {
-				$widget = call_user_func   ( $callback, $widget);
-			}
-		}
-        
-		if ( is_debug_on() ){
-			return "\n<!-- start {$widgetName} ($widgetID) -->" . $widget . "\n<!-- end {$widgetName} ($widgetID) -->";
-		}		
-    
-    }
-
-
-	function parse($text){
-		
-		// extract beign,tag,condition & true-block, else-block,rest
-		$match = extract_markup ( $widget,$this->markup_codes );
-		if (!is_array($match) ){
-			return $text;			
-		}
-		   	
-		$parserToCall= $match[2]; // for comodity
-		
-		if ( isset( $this->parsers[ $parserToCall]) ){
-			$result = call_user_func( array($this, $this->parsers[ $parserToCall] ), &$match[1], &$match[2]);
-		} else {
-			$result = t("Parser $parserToCall not found");
-		}
-						
-		// return begin+result+parse the the rest..				
-	    return $match[0].
-	           $result.
-	           $this->parse($match[3]);
-	}
-
-
-    /**
-     *  parse vars
-     */
-
-	function parse_vars($match){
-		static $bufferReplace;
-		global $aiki, $page;
-
-		if ( $bufferReplace == NULL ) {
-			/* @TODO unified with aiki processVars*/
-
-			$pretty = $aiki->config->get('pretty_urls', 1);
-			$url = $aiki->config->get('url');
-
-			$current_month = date("n");
-			$current_year = date("Y");
-			$current_day = date("j");
-
-			// calculate view, prefix, route
-			$view = $aiki->site->view();
-			$language = $aiki->site->language();
-			$prefix = $aiki->site->prefix();
-			$view_prefix= $aiki->site->view_prefix();
-				
-			$paths= array();
-			if ($prefix) {
-				$paths[] = $prefix;
-			}
-
-			if ($view_prefix) {
-				$paths[] = $view_prefix;
-			}
-			if ( count($aiki->site->languages()) > 1 ) {
-				$paths[] = $language;
-			}
-			$paths = implode("/",$paths);
-			
-			if ( isset($_SERVER["HTTPS"])) {
-				$url = str_replace("http://";, "https://";, $url);
-			}	
-
-			$trimedUrl = preg_replace('#/$#',"",$url); // reg: remove ending /
-							
-			$bufferReplace = array(
-				'[$userid]'	=> $aiki->membership->userid,
-				'[$full_name]' => $aiki->membership->full_name,
-				'[$username]'  => $aiki->membership->username,
-				'[$user_group_level]' => $aiki->membership->group_level,
-				'[$user_permissions]' => $aiki->membership->permissions,			
-				'[$language]'  => $aiki->site->language(),		   
-				'[$page]'	  => $page,
-				'[$site_name]' => $aiki->site->site_name(),
-				'site'	       => $aiki->site->get_site(),
-				'[$view]'	  => $aiki->site->view(),
-				'[$direction]' => $aiki->languages->dir,
-				'[$insertedby_username]' => $aiki->membership->username,
-				'[$insertedby_userid]' => $aiki->membership->userid,
-				'[$current_month]' => $current_month,
-				'[$current_year]' => $current_year,
-				'[$current_day]' => $current_day,
-				'[$root]'		  => $url,
-				'[$root-language]' => $trimedUrl .  "/" . $aiki->site->language(),
-				'[$site_prefix]'   => $prefix ,
-				'[$view_prefix]'   => $view_prefix ,
-				'[$route]'		 => $trimedUrl.  "/". $paths,
-				'[$route-local]'	 => $paths );
-			}
-
-		$token = $match[1];
-		if ( isset($bufferReplace[$token]) ){
-			return $bufferReplace[$token];
-		} else {
-			return $match[0];
-		}
-	}
-
-	function parse_get( $matchs){
-		$token= $matchs[0];
-		return $token && isset($_GET[ $token]) ? $_GET[$token] : "";
-	}
-
-	function parse_post( $token){
-		$token= $matchs[0];
-		return $token && isset($_POST[$token]) ? $_POST[$token] : "";
-	}
-
-
-	/*
-	 * Parse template
-	 */
-	function parse_template($matches){
-		global $db;
-		$id= $this->get_widget_id($matches[1]);
-		return  is_null($id) ? "": $db->get_var ("SELECT widget FROM aiki_widgets WHERE id='$id'" );
-	}
-
-	function parse_script($cond,$trueblock,$elseblock){
-		global $aiki;
-		return $aiki->AikiScript->parser($trueblock,false);
-	}
-
-	function parse_if($cond,$trueblock,$elseblock){	
-		return ( is_array($cond)? first($cond): $cond ) ? $trueblock : $elseblock  ;	
-	}
-
-	/**
-	 * translation
-	 */
-
-	function parse_t($term) {
-		static $translate;
-		global $aiki;
-		if ( is_null($translate) ){
-			$translate= $aiki->site->language()!="en";
-		}
-		return $translate ? t($term): $term ;
-	}
-
-
-	function parse_translate($term) {
-		return __($term);		
-	}
-
-
-	/*
-	 * Parse sql markup
-	 */
-
-	function parse_sql( $para, $true, $else ){
-		global $db;				
-		
-		$results = $db->get_results($para[0]);
-
-		$html="";
-		if ($results) {
-			foreach ( $results as $row ) {
-				$fields= array();
-				foreach ( $row as $field=>$value ){
-					$fields[ "[$field]" ]= $value;
-				}
-				$html .= strtr( $true, $fields);
-			}
-		} else {
-			return $else;
-		}
-		return $html;
-
-	}
-
-	/*
-	 * Parse hits
-	 * @TODO implements trigger.
-	 */
-
-	private function parse_hits($hit, $true, $else ) {
-		global $db;
-		
-		if ( len($hit) == 3 ){
-			$db->query(
-					"UPDATE {$hit[0]}".
-					" SET {$hit[2]}={$hit[2]}+1".
-					" WHERE {$hit[1]}");
-		} elseif (is_debug_on() ) {
-				return sprintf( __("BAD HITS PARAMETERS: 3 expected, %d  given"), len($hit) );
-		}
-		return "";
-	}
-
-
-	function parse_widget( $cond, $true, $false ){
-		//@TODO
-		//return  $this->parse($widgetId, $select);
-	}
-
-
-	function parse_view( $para, $true, $false){
-		global $aiki;
-		
-		list($view,$language)= exlode("/",$filter[1]."/*",2);
-	
-		if  ( match_pair_one( $view, $aiki->site->view()) &&
-		      match_pair_one( $language, $aiki->site->language() )){
-			return $true;
-		}
-		return $false;
-
-	}
-
-
-	function parse_permission($para, $true, $false){
-		global $aiki, $db;
-		if ( strpos($widget,"||") !== false ){
-			list($filter,$content) = explode("||", $widget, 2);
-		} else {
-			return $widget;
-		}
-
-		/* fake permission */
-		if ( trim($filter) == "user" ){
-			return $content;
-		}
-		return "";
-
-		$sql = "SELECT group_level" .
-			   " FROM  aiki_users_groups".
-			   " WHERE group_permissions='". addslashes($filter) ."'";
-
-		$get_group_level = $db->get_var($sql);
-
-		if ( trim($filter) == $aiki->membership->permissions ||
-			$aiki->membership->group_level < $get_group_level ) {
-			return $content;
-		}
-
-		return "";
-	}
-
-	function parse_noaiki(&$cond, &$text, $else){
-		return $text;
-	}
-
-
-	function convert_widget($widget){
-	
-		// no_loop
-		if ( trim($widget->normal_select)!="" ){
-			$loop="";
-			$bottom="";
-			$resul=false;
-			if ( preg_match ( "#\(noloop\((.*)\)noloop\)#m",$widget->widget,$resul)){
-				$loop= $resul[1];
-				$widget= str_replace($resul[0],"",$widget->widget);
-			}
-			if ( preg_match ( "#\(noloop_bottom\((.*)\)noloop_bottom\)#m",$widget->widget,$resul)){
-				$bottom= $resul[1];
-				$widget= str_replace($resul[0],"",$widget->widget);
-			}
-						
-			$widget->widget = "$loop(sql(\"{$widget->normal_select}\"||{$widget->widget})sql)$bottom";
-		}			
-		return $widget;
-	}
-
-
-
-}

=== modified file 'libs/widgets.php'
--- libs/widgets.php	2012-02-21 21:00:07 +0000
+++ libs/widgets.php	2012-02-26 21:02:15 +0000
@@ -129,4 +129,81 @@
         return ( $record ? $db->get_row($searchSQL) : $db->get_var($searchSQL));
     }
 
+
+	/**
+     * render widget with a engine.
+     *
+     * Results are stored in engine (attribute target)
+     * A engine object must have: a attribute called target, a method calles
+     * parseWidget.
+     * 
+     * @param  object engine
+     */
+
+	function render( $engine) { 
+	global $aiki;
+	
+	// the widget is given directly or
+	if (isset($_GET["widget"])) {
+		if ($getWidget = $aiki->widgets->get_widget($_GET['widget'])) {
+			return $engine->parseWidget($getWidget);
+		}
+		return;//all work is done
+	}
+
+	// or in url,
+	// search widget and test there is a unique response
+	$module_widgets = $aiki->widgets->get_candidate_widgets();
+	$unique_widget_exists = false;
+	if ($module_widgets) {
+		foreach($module_widgets as $tested_widget){
+			if ($tested_widget->display_urls != "*"){
+				$unique_widget_exists = true;
+				break;
+			}
+		}
+	}
+
+	// Error 404 page not found
+	$allMatch=false;
+	if (!$unique_widget_exists) {
+		// first look for widget that responds error_404,
+		// else use config error_404.
+		$module_widgets= $aiki->widgets->get_Page_Not_Found_Widgets();
+		if ( $module_widgets ) {
+			$aiki->Errors->pageNotFound(false);
+			$allMatch = true;
+		} else {
+			return $aiki->Errors->pageNotFound(true);
+		}
+	}
+
+	// now filter canditate widgets, before create content
+	foreach ( $module_widgets as $parent ) {
+
+		// first parent
+		if ( $allMatch or
+			($aiki->url->match($parent->display_urls) && !$aiki->url->match($parent->kill_urls)) ) {
+			if ( $parent->have_css == 1) {
+				$engine->target["css"][] = $parent->id;
+			}
+			$engine->target[$parent->widget_target] .= $engine->parseWidget($parent->id);
+
+			// children..
+			/* @TODO..a function */
+			if ( is_array($descendants = $aiki->widgets->get_candidate_widgets($parent->id)) ){
+				foreach ($descendants as $descendant){
+					if ( $aiki->url->match($descendant->display_urls) && !$aiki->url->match($descendant->kill_urls) ) {
+						$engine->target["css"][] = $descendant->id;
+						$engine->target[$descendant->widget_target] .= $engine->parseWidget($descendant->id);
+					}
+				}
+			}
+		}
+		
+	}
+
+}
+
+
 }

_______________________________________________
Mailing list: https://launchpad.net/~aikiframework-devel
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~aikiframework-devel
More help   : https://help.launchpad.net/ListHelp

Reply via email to