------------------------------------------------------------
revno: 1136
committer: Roger Martin <[email protected]>
branch nick: aikiframework
timestamp: Fri 2012-03-09 23:23:07 +0100
message:
  EngineV2 have forms and controls
added:
  libs/Controls.php
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
=== added file 'libs/Controls.php'
--- libs/Controls.php	1970-01-01 00:00:00 +0000
+++ libs/Controls.php	2012-03-09 22:23:07 +0000
@@ -0,0 +1,290 @@
+<?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
+ * @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
+ * @filesource
+ */
+
+/*if (!defined('IN_AIKI')) {
+	die('No direct script access allowed');
+}*/
+
+
+//@TODO MOVE TO AIKI LIBRARY
+function bydefault( &$array, $key, $value) {
+	return  isset($array[$key]) ? $array[$key]: $value;
+}
+
+
+
+/*
+ * form2 class
+ */
+
+
+class Forms2 {
+	private $defaults;
+	
+	
+	function __construct ( $parameters ){
+		// here are the default parameters
+		$defaults = array (
+			"method" => "get",
+			"action" => "?",
+			"id"     => uniqid("f_"), // by default a random unique
+			"http"   => "http", // @todo current protocol
+			"layout" => "table"
+		);
+		// read new parameters.
+		if ( is_array($parameters) ){
+			$this->defaults = $parameters + $defaults;
+		}
+		
+	}
+	
+	/**
+	 * 
+	 * Returns the HTML code for the form inserting the values
+	 * 
+	 */	
+	
+	function render($values=NULL){
+		$attributes="";
+		// here we insert form attributes
+		foreach ( array("class","id","action","method", "enctype", "onsubmit", "onreset") as $attribute ){
+			if ( isset($this->defaults[$attribute]) ){
+				$attributes .= " $attribute='". addslashes($this->defaults[$attribute]). "'" ;
+			}
+		}
+		
+		// yes, there are people who just wants a blank forms!		
+		if ( !isset($this->defaults["fields"]) || !is_array($this->defaults["fields"] ) ){
+			return "<form$attributes></form>\n";
+		}		 
+
+		return 
+			"<form$attributes>\n".
+		    Controls::inputs($this->defaults["fields"],$this,$values ).
+			"</form>";			
+	}
+	
+	
+	/**
+	 * 
+	 * Magic method to get forms parameters
+	 * 
+	 */	
+	function __get($what){
+		return isset($this->defaults[$what]) ? $this->defaults[$what]: NULL ;
+	}
+		
+}
+
+
+/**
+ * 
+ * Class for controls: input, select, textarea
+ * 
+ */	
+
+
+class Controls {
+
+	function make_list(&$list){
+		global $db;
+		if ( is_array($list) ) {
+			return $list;
+		}
+		
+		if ( substr($list,0,4)=="SQL:" ){
+			$ret= array();
+			if ($results= $db->query( substr($list,5), N_ARRAY )){
+				foreach ($results as $result){
+					$ret[$resul[0]]= $resul[1];
+				}
+			}
+			return $ret;
+		} elseif ( $list=="gender")  {
+			return array("Male", "Female");
+		} elseif ( $list=="yn"){
+			return array("Yes", "No");
+		}	
+		return array( explode("|",$list) );
+	}
+	
+	
+	/**
+	 * 
+	 * Return html code for a input hidden
+     * 
+     */	
+	
+	function input_hidden($field,$values=NULL){
+		$name = $field['name'];
+		if (is_null($values) ){
+			$value =  isset($field["value"]) ? $field["value"] : "";
+		} elseif (isset($values['value'])) {			
+			$value = $values["value"];
+		} elseif (isset($fields['value'])) {
+			$value = $fields['value'];
+		} else {
+			$value="";
+		}
+		return "<input type='hidden' name='{$field['name']}' value='". addslashes($value)."'>";			
+	}
+	
+	
+	/**
+	 * 
+	 * Return html code for generic input
+     * 
+     */	
+	
+	function input ( $field, $form=NULL, $values=NULL ) {
+
+		if ( is_null($form) ){
+			$layout = bydefault($field,'layout','table');
+			$formID = bydefault($field,'form',"aiki_form");
+		} else {
+			$layout = isset($form->layout) ? $form->layout :"table";
+			$formID = $form->id;
+		}
+		
+		
+		$name   = bydefault($field,'name',false);
+		$type   = bydefault($field,'type',"text");		
+		$label  = bydefault($field,'label',$name);
+		$more   = bydefault($field,'more' ,"");					
+		
+
+		// first, discard hidden, and find field label
+		switch ( $type ) {
+			case "hidden":
+				return Control::input_hidden($field,$values);
+			case "radios":
+				$label = "<span class='label'>$label</span>";
+				break;
+			default:		   
+				$label = "<label for='{$formID}_$name'>$label</label>";
+		}
+		
+		// now the hard work, input;
+		$input="";	
+		
+		$fieldValue = is_null($values) || !isset($values[$name]) ? NULL: $values[$name];	
+		$more       = $more ? "<span class='more'>$more</span>":"";
+		
+	
+		switch ( $type ){	
+			case "radios":			
+				$list  = Controls::make_list($field["list"]);
+				foreach ($list as $key=>$value){
+					if ( $fieldValue && $key==$fieldValue) {
+						$checked=" checked='checked'";
+					} else {
+						$checked="";
+					}
+					$input .=
+						"<input type='radio' value='$key' name='$name' id='{$formID}_$name_$key'$checked>".
+						"<label for='{$formID}_$name'>$value</label>";
+				}								
+				break;
+			
+			case "select":
+				$list = Controls::make_list($field["list"])	;
+				foreach ($list as $key=>$value){
+					if ( $fieldValue && $key==$fieldValue) {
+						$selected=" selected='selected'";
+					} else {
+						$selected="";
+					}
+					$input .="\n<option value='$key'$selected>$value</option>";		
+				}				
+				$input = "<select name='$name' id='{$formID}_$name'>$input\n</select>";
+				break;
+		
+			case "textarea":
+				$cols = bydefault($field,'cols',60);
+				$rows = bydefault($field,'rows',10);
+			    $input = sprintf(
+					"<textarea name='$name' id='{$formID}_$name' rows='$rows' cols='$cols' >%s</textarea>",
+					bydefault($values,$name,"") );
+				break;
+			
+			case "file" : //@TODO set form enctype.				
+			case "text":
+			case "password":		
+				$length = bydefault($field,'maxlength',0);
+				if ( $type=="string" && $fieldValue){
+					$attribValue= " value='" . addslashes($fieldValue)."'";
+				} else {
+					$attribValue="";
+				}
+				$input = "<input type='$type' name='$name' id='{$formID}_$name'$attribValue>";
+				break;	
+			//@TODO add HTML5 inputs.		
+		}
+		
+		$input .= $more;
+		// final layout of values.
+		$layouts= array(
+			"table" => "\n<tr><th>\$label</th><td>\$input</td></tr>",
+			"none"  => "\$label \$input",
+			"p-br"  => "\n<p>\$label<br>\$input</p>",
+			"p"     => "\n<p>\$label \$input</p>",
+			"dl"	=> "\n<dt>\$label</dt><dd>\$input</dd>");
+		$useLayout =  isset($layouts[$layout]) ?  $layouts[$layout] : $layout;
+		
+		return strtr($useLayout, array('$label'=>$label,'$input'=>$input));	
+			
+	}
+
+	
+	/**
+	 * 
+	 * Return html code for a list of input
+     * 
+     */	
+	function inputs( &$inputs, $form, &$values){
+		$hidden = "";
+		$ret = ""	;
+		foreach($inputs as $input) {
+			//hidden values 
+			switch ($input["type"]){
+				case "hidden":
+					$hidden .= Controls::input_hidden($input,$values);	
+					break;
+				case "html":
+					$ret .= $input["html"];
+					break;
+				default:
+					$ret .= Controls::input($input, $form, $values);		
+			}
+		}
+			
+		$layouts= array(
+			"table"    => "\n<table>\$inputs</table>",
+			"none"  => "\$inputs",
+			"p-br"  => "\n\$inputs",
+			"p"     => "\n\$inputs",
+			"dl"	=> "\n<dl>\$inputs</dl>");
+	
+		$layout    = isset($form->layout) ? $form->layout : 'table';	
+		$useLayout = isset($layouts[$layout]) ?  $layouts[$layout] : $layouts['table'];
+		
+		return $hidden.str_replace ( '$inputs', $ret, $useLayout);	
+	}
+
+}

=== modified file 'libs/Engine_v2.php'
--- libs/Engine_v2.php	2012-03-08 23:00:02 +0000
+++ libs/Engine_v2.php	2012-03-09 22:23:07 +0000
@@ -27,7 +27,7 @@
 
 //@TODO move to bootstrap, when it is stable.
 include_once("libs/markup.php");
-
+include_once("libs/Controls.php"); //
 
 class engine_v2 {
 
@@ -93,6 +93,9 @@
 			"t"			  => "parse_t",
 			"nl2p"        => "parse_nl2p",
 			"nl2br"       => "parse_nl2br",
+			"control"     => "parse_controls",
+			"form"        => "parse_forms",
+			"if"          => "parse_if",
 			"__"		  => "parse_translate");
 
 		// set special parsers 
@@ -645,6 +648,25 @@
 	}
 
 
+	/*
+	 * here are the (micro)-parser
+	 */
+
+	function parse_forms( $para, $true, $false){
+		$formParameters = array_shift($para) ;
+		$formParameters["fields"]= $para;
+		$form= new Forms2($formParameters);	
+		return $form->render();
+	}
+
+	function parse_controls( $para, $true, $false){
+		return Controls::input($para[0]);
+	}
+
+
+
+
+
 	function convert_widget($widget){
 
 		// no_loop

=== modified file 'libs/markup.php'
--- libs/markup.php	2012-03-08 23:00:02 +0000
+++ libs/markup.php	2012-03-09 22:23:07 +0000
@@ -88,7 +88,6 @@
 		$markup= aiki_get_markup_codes($markup);
 	}
 	
-	
 	// find the starting and ending tags.			
 	if ( !preg_match_all(	
 			"#{$markup[0]}|{$markup[1]}|". preg_quote($markup[2])."#",
@@ -198,7 +197,9 @@
 			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,true);}
+			// @TODO make only on regex
+			elseif ( preg_match('#^\s*\{\s*"[^"]+"\s*\:\s*"[^"]*"#',$input, $temp)){$ret[]= json_decode($input,true);}
+			elseif ( preg_match("#^\s*\{\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));
@@ -291,7 +292,7 @@
 				}
 				break;
 			
-			// we a begin (
+			// a begin (
 			case '[':
 			case '(':
 			case '{':

_______________________________________________
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