------------------------------------------------------------
revno: 1114
committer: Roger Martin <[email protected]>
branch nick: aikiframework
timestamp: Thu 2012-03-01 23:20:20 +0100
message:
  working on engine2
modified:
  libs/Engine_v2.php
  libs/markup.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
=== modified file 'libs/Engine_v2.php'
--- libs/Engine_v2.php	2012-02-26 22:54:13 +0000
+++ libs/Engine_v2.php	2012-03-01 22:20:20 +0000
@@ -20,11 +20,11 @@
  * 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')) {	
+if (!defined('IN_AIKI')) {
 
 	die('No direct script access allowed');
-} 
-	
+}
+
 //@TODO move to bootstrap, when it is stable.
 include_once("libs/markup.php");
 
@@ -34,12 +34,12 @@
     // engine parameters
 	private $convert_widgets;
 	private $markup_codes;
-	
+
 	private $parsers; // array of parser (markup=>callback)
-	
+
 	private $widget_css;
 	private $widget_html;
-		
+
 	/*
 	 * Create layout
 	 */
@@ -49,7 +49,7 @@
 
 		// Parameters
 		$this->load_parameters($parameters);
-												
+
 		// @TODO javascript? id for some elements?
 		$this->target = array(
 			"body"=>"" ,
@@ -57,7 +57,7 @@
 			"css"=>array() );
 
 		$aiki->widgets->render($this);
-		
+
         // finally make html.
 		return $this->render_html();
 	}
@@ -66,19 +66,19 @@
 	function load_parameters($parameters) {
 		// Parameters
 		if ( is_array( $parameters ) ){
-			$parameters= array();				
-		}	
-		
+			$parameters= array();
+		}
+
 		// set markup to use
 		$markup = isset($parameters['markup'])	? $parameters['markup'] : 'aiki';
-		$this->markup_codes    = aiki_get_markup_codes ($markup ) ;		
-		
+		$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",
@@ -90,22 +90,23 @@
 			"script"      => "parse_script",
 			"t"			  => "parse_t",
 			"__"		  => "parse_translate");
-		
+
 		if ( isset($parameters["parsers-allowed"]) ){
-			$this->parsers = array_intersect ( 
-				$this->parsers , 
+			$this->parsers = array_intersect (
+				$this->parsers ,
 				array_flip(explode(",",$parameters["parsers-allowed"])) );
 		}
-		
+
 		if ( isset($parameters["parsers-disabled"]) ){
-			$this->parsers = array_diff_key ( 
-				$this->parsers , 
+			$this->parsers = array_diff_key (
+				$this->parsers ,
 				array_flip(explode(",",$parameters["parsers-disabled"])) );
 		}
+
 	}
 
 
-	function render_html(){	
+	function render_html(){
 		global $aiki;
 		$html  = $aiki->Output->header($this->target['css'],  $this->target['header']);
 		$html .= $aiki->Output->body($this->target['body']);
@@ -122,11 +123,11 @@
 
 	function parseWidget($widgetID){
 		global $aiki;
-		                			 
-		if ( is_object($widgetID) ){					
+
+		if ( is_object($widgetID) ){
 			$widget= $widgetID;
 		} else {
-			$widget = $aiki->widgets->get_widget($widgetID);			
+			$widget = $aiki->widgets->get_widget($widgetID);
 		}
 
 		if (!is_object($widget)){
@@ -145,15 +146,15 @@
 		    '/\[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		    
+
+		    // post-parser
 		    array ($aiki->languages,"L10n") // added for test purpose only
 		    ) ;
-                		
-        $content= $widget->widget;                			 		    
+
+        $content= $widget->widget;
 		foreach ( $Parsers as $pattern => $callback ){
 			if ( is_string($pattern) ){
 				$content = preg_replace_callback ( $pattern, $callback, $content);
@@ -161,34 +162,40 @@
 				$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}) -->";
-		}		
-    
+		}
+
     }
 
 
+    /*
+     * Parse recursivly the given text
+     */
+
 	function parse($text){
-		
-		// extract beign,tag,condition & true-block, else-block,rest
-		$match = extract_markup ( $widget,$this->markup_codes );
+
+		// extract begin,tag,condition & true-block, else-block,rest
+		$match = extract_markup ( $text,$this->markup_codes );
+
 		if (!is_array($match) ){
-			return $text;			
+			return $text;
 		}
-		   	
-		$parserToCall= $match[2]; // for comodity
-		
+
+	    // check if a parser exists and then call it
+		$parserToCall= $match[1];
 		if ( isset( $this->parsers[ $parserToCall]) ){
-			$result = call_user_func( array($this, $this->parsers[ $parserToCall] ), &$match[1], &$match[2]);
+			// call parser with argument, true block and false block
+			$result = call_user_func( array($this, $this->parsers[ $parserToCall] ), $match[2], $match[3], $match[4]);
 		} else {
 			$result = t("Parser $parserToCall not found");
 		}
-						
-		// return begin+result+parse the the rest..				
+
+		// return begin+result+parsing the the rest..
 	    return $match[0].
-	           $result.
-	           $this->parse($match[3]);
+	           $this->parse($result).
+	           $this->parse($match[5]);
 	}
 
 
@@ -196,11 +203,11 @@
      *  parse vars
      */
 
-	function parse_vars($match){
+	function parse_vars($parameters){
 		static $bufferReplace;
 		global $aiki, $page;
 
-		if ( $bufferReplace == NULL ) {
+		if ( is_null($bufferReplace) ) {
 			/* @TODO unified with aiki processVars*/
 
 			$pretty = $aiki->config->get('pretty_urls', 1);
@@ -215,7 +222,7 @@
 			$language = $aiki->site->language();
 			$prefix = $aiki->site->prefix();
 			$view_prefix= $aiki->site->view_prefix();
-				
+
 			$paths= array();
 			if ($prefix) {
 				$paths[] = $prefix;
@@ -228,52 +235,53 @@
 				$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(),
+				'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 );
-			}
+				'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];
+		$token=$parameters[0];
 		if ( isset($bufferReplace[$token]) ){
 			return $bufferReplace[$token];
 		} else {
-			return $match[0];
+			return $token;
 		}
 	}
 
+
 	function parse_get( $matchs){
 		$token= $matchs[0];
 		return $token && isset($_GET[ $token]) ? $_GET[$token] : "";
 	}
 
-	function parse_post( $token){
+	function parse_post( $matchs){
 		$token= $matchs[0];
 		return $token && isset($_POST[$token]) ? $_POST[$token] : "";
 	}
@@ -283,18 +291,29 @@
 	 * 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'" );
+		global $aiki;
+		$widget= $aiki->widgets->get_widget($matches[0]);
+		return  ( is_null($widget) ? "": $wiget->widget );
 	}
 
+
+	/*
+	 * Parse script
+	 */
 	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  ;	
+	/*
+	 * Parse if
+	 */
+	function parse_if($condition,$trueblock,$elseblock){
+		if (isset($condtion[0]) && $condition[0]) {
+			return $trueblock;
+		} else {
+			return $elseblock  ;
+		}
 	}
 
 	/**
@@ -307,12 +326,15 @@
 		if ( is_null($translate) ){
 			$translate= $aiki->site->language()!="en";
 		}
-		return $translate ? t($term): $term ;
+		if  ( !isset($term[0]) ) {
+			return "";
+		}
+		return $translate ? t($term[0]) : $term[0] ;
 	}
 
 
 	function parse_translate($term) {
-		return __($term);		
+		return isset($term[0]) ? __($term[0]): "" ;
 	}
 
 
@@ -320,23 +342,204 @@
 	 * Parse sql markup
 	 */
 
-	function parse_sql( $para, $true, $else ){
-		global $db;				
-		
-		$results = $db->get_results($para[0]);
-
+	private function _pagination($data, $template){
+
+		$prev= $data["previous"]==false  ? "" :
+			sprintf("<a href='?%s=%d' class='previous-link'><span>%s</span></a>",
+					$data["pagination-var"],$data["previous"],t("Previous") );
+		$next= $data["next"]==false  ? "" :
+			sprintf("<a href='?%s=%d' class='next-link'><span>%s</span></a>",
+					$data["pagination-var"],$data["previous"],t("Next") );
+
+		switch ($template){
+			case "previous-link": 	    return $prev;
+			case "next-link":		    return $next;
+			case "previous-next-links":	return "$prev $next";
+			default:
+				foreach ($data as $key=>$value){
+					$replace["[$key]"]=$value;
+				}
+				$replace["[next-link]"    ] = $next;
+				$replace["[previous-link]"] = $previous;
+				return strtr( $template, $replace);
+		}
+
+	}
+
+
+	function parse_sql( $para, $ifData, $ifnoData ){
+		global $db;
+		static $prevPaginations;
+
+		// default parameters
+		$parameters = array (
+			"columns"         =>"0",
+			"columns-prefix"  =>"col",
+			"columns-tag"     =>"div",
+			"record-tag"      =>"none",
+			"pagination"      => true,
+			"pagination-var"  => "page",
+			"pagination-add"  => true,
+			"pagination-template"=>"previous-next-links",
+			"records-per-page"=>20,
+			"dump"  =>"",
+			"store" => "last",
+			"limit" =>"500",
+		);
+
+		// read $parameters
+		if ( count($para)==0 ){
+			return "";
+		} elseif ( !is_array ($para[0]) ){
+			$parameters["SQL"]= $para[0];
+			if (isset($para[1])){
+				$parameters["pagination"]=$para[1];
+			}
+			if (isset($para[2])){
+				$parameters["dump"]=substr($para[2],1,-1);
+			}
+
+		} else {
+			foreach ($para[0] as $key=>$value){
+				$parameters[$key]=$value;
+			}
+		}
+
+		// Special modes
+		$SQL = $parameters["SQL"];
+
+		// this markus accepts a third syntax sql(key,store-in)
+		$store = !is_array( $para[0] ) && isset($para[1]) ?
+				$para[1] : $parameters["store"];
+
+		if ( !is_null($prevPaginations) && isset( $prevPaginations[$store]) ){
+			switch ( $SQL ){
+				case "previous":
+				case "page":
+				case "next":
+				case "records-per-page":
+				case "pagination-var":
+				case "count" :
+					return $prevPaginations[$store][$SQL];
+				case "previous-link":
+				case "previous-next-links":
+				case "next-link":
+					return $this->_pagination($data,$SQL);
+				case "pagination":
+					return $this->_pagination($data,$parameters["pagination-template"]);
+			}
+		}
+
+		// Check SQL, insert pagination and limit.
+		$temp=false;
+		if ( preg_match("#^(SELECT|SHOW|DESCRIBE) #i", $SQL, $temp) ){
+			return t("Not valid SQL. Must be SELECT, SHOW, DESCRIBE, or retrieve a value for a previus query");
+		}
+
+		$pagination = (bool) $parameters["pagination"];
+		if ($pagination ){
+			$page = isset($_GET[ $parameters["pagination-var"]]) ?
+				$_GET[ $parameters["pagination-var"]]:1;
+			$SQL .= " LIMIT ". ($page-1)*$parameters["records-per-page"].",". ($parameters["records-per-page"]+1);
+		} elseif ( $parameters["LIMIT"] ){
+			$SQL .= " LIMIT ". $parameters["LIMIT"];
+		}
+
+		// launch the query and prepare errors-debug.
+		$db->last_error= false;
+		$results = $db->get_results( substr($parameters["SQL"],1,-1));
+		if ( $db->last_error ){
+			return t("Error in SQL").
+			       ( is_debug_on() ? " ". $db->last_error : "" );
+		}
+
+		// this vars are for comodity.
+		$col = $parameters["columns"];
+		$colTag = $parameters["columns-tag"];
+		$tag = $parameters["record-tag"];
+
+		// here we store the results.
 		$html="";
 		if ($results) {
+			$i=0;
 			foreach ( $results as $row ) {
+				switch ($parameters["dump"]){
+					case "raw":
+						foreach ( $row as $field=>$value ){
+							$html .= $value . " ";
+						}
+						continue;
+						break;
+
+					case "table":
+						if ( !isset($thead) ){
+							$thead ="";
+							foreach ( $row as $field=>$value ){
+								$thead .= "<th>$field</td>";
+							}
+							$thead = "<tr>$thead</tr>\n";
+						}
+
+					    $tr ="";
+						foreach ( $row as $field=>$value ){
+							$tr .= "<td>$value</td>";
+						}
+						$html .= "<tr>$tr</tr>\n";
+						continue;
+						break;
+
+					case "dl":
+					    $dl ="";
+						foreach ( $row as $field=>$value ){
+							$dl .= "<dt>$field</dt><dd>$value</dd>";
+						}
+						$html .= "<dl>$dl</dl>\n";
+						continue;
+						break;
+				}
+
+				// when we reach here a normal dump must be done.
 				$fields= array();
 				foreach ( $row as $field=>$value ){
-					$fields[ "[$field]" ]= $value;
-				}
-				$html .= strtr( $true, $fields);
-			}
+					$fields[ "(($field))" ]= $value;
+				}
+				$html .= ( $col  ? sprintf("<{$colTag} class='col-%d' >", $i % $col) : "").
+				         ( $tag=="" || $tag=="none" ? "" : "<$tag>").
+				           strtr( $ifData, $fields).
+				         ( $tag=="" || $tag=="none" ? "" : "</$tag> ").
+				          ($col  ? "</{$colTag}>" : "" );
+				$i++;
+				if ( $pagination && $i== $parameters["records-per-page"] ){
+					$break;
+				}
+			}
+			if ( $parameters["dump"]=="table"){
+				return "<table><thead>$thead</thead><tbody>$html</tbody></table>";
+			}
+			$records = count( $results);
+
 		} else {
-			return $else;
-		}
+			$page=false;
+			$records=0;
+			$html= $ifnoData;
+		}
+
+		if ( $pagination ){
+			// we store pagination data.
+			$prevPagination[ $parameters["store"]] = array(
+				"previous"        => ( $page>0 ? false : $page-1),
+				"page"            => $page,
+				"next"            => $records > $parameters["records-per-page"] ? $page+1 : false,
+				"records-per-page"=> $records>$parameters["records-per-page"],
+				"pagination-var"  => $parameters["pagination-var"],
+				"count"           => $records );
+			if ($parameters["pagination-add"]){
+				$html .= $this->_pagination (
+					$prevPagination[ $parameters["store"]],
+					$parameters["pagination-template"]);
+			}
+		}
+
 		return $html;
 
 	}
@@ -348,7 +551,7 @@
 
 	private function parse_hits($hit, $true, $else ) {
 		global $db;
-		
+
 		if ( len($hit) == 3 ){
 			$db->query(
 					"UPDATE {$hit[0]}".
@@ -369,9 +572,9 @@
 
 	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;
@@ -415,7 +618,7 @@
 
 
 	function convert_widget($widget){
-	
+
 		// no_loop
 		if ( trim($widget->normal_select)!="" ){
 			$loop="";
@@ -429,9 +632,9 @@
 				$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/markup.php'
--- libs/markup.php	2012-02-26 22:54:13 +0000
+++ libs/markup.php	2012-03-01 22:20:20 +0000
@@ -156,8 +156,8 @@
 			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]=="[") {$ret[]= json_decode($input);}								
+			elseif ( preg_match('#^\{\s*"[^"]+"\s*\:\s*"[^"]*"#',$input, $temp)){ $ret[]= json_decode($input,true);}
+			elseif ( $input[0]=="[") {$ret[]= json_decode($input);}		
 			elseif ( $input[0]=="{") { 
 				$temp = extract_parameters( substr($input,1,-1));
 				$ret[]= eval_parameters( $temp[0] );}

_______________________________________________
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