------------------------------------------------------------
revno: 895
committer: Roger Martin <[email protected]>
branch nick: aikiframework
timestamp: Fri 2011-09-16 22:16:12 +0200
message:
  views and css parser added
added:
  src/libs/css_parser.php
  src/libs/view_parser.php
modified:
  src/bootstrap.php
  src/libs/aiki.php
  src/libs/output.php
  src/libs/site.php
  src/libs/url.php
  src/libs/widgets.php
  src/sql/CreateTables.sql
  src/style.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 'src/bootstrap.php'
--- src/bootstrap.php	2011-08-20 21:29:58 +0000
+++ src/bootstrap.php	2011-09-16 20:16:12 +0000
@@ -139,10 +139,10 @@
  * @todo replace global membership by $aiki->membership
  */ 
 
+$aiki->load('url');
 $aiki->load("site"); 
 $membership = $aiki->load("membership");
 $aiki->load("languages");
-$aiki->load("url");
 
 if ($aiki->site->language()!="en"){
     include_once ("$AIKI_ROOT_DIR/libs/classes/dictionaryTableClass.php");
@@ -161,6 +161,8 @@
 $aiki->load("security");
 $aiki->load("parser");
 $aiki->load("php");
+$aiki->load("css_parser");
 $aiki->load("sql_markup");
+$aiki->load("view_parser");
 $aiki->load("image");
 $aiki->load("errors");

=== modified file 'src/libs/aiki.php'
--- src/libs/aiki.php	2011-09-07 21:21:22 +0000
+++ src/libs/aiki.php	2011-09-16 20:16:12 +0000
@@ -27,29 +27,29 @@
  */
 class aiki
 {
- 
+
     private $pretty_url; // aiki store the pretty_url because some lib, need access / modify this url
-    
+
     /**
      * return pretty url (path of url request)
      * Example www.foo.com/bar/something bar/something is the pretty url.
-     * @return string 
+     * @return string
      */
-    
+
     function pretty_url(){
         return $this->pretty_url;
     }
-    
-            
-    public function __construct(){        
-        $this->pretty_url = isset($_GET["pretty"]) ?  $_GET["pretty"] : "" ; 
+
+
+    public function __construct(){
+        $this->pretty_url = isset($_GET["pretty"]) ?  $_GET["pretty"] : "" ;
     }
-    
+
 
     /**
      * Loads an aiki library.
-     * 
-     * Attempts to load from class first *.php, then tries to load *.php 
+     *
+     * Attempts to load from class first *.php, then tries to load *.php
      * from extensions, then finally  tries classname/classname.php.
      *
      * @param   string $class name of class to be loaded
@@ -59,7 +59,7 @@
     public function load($class) {
         global $AIKI_ROOT_DIR;
 
-      
+
         if (isset($this->$class))
             return $this->$class;
 
@@ -93,10 +93,10 @@
      * @global array $db The global database object
 	 * @return array     The global configuration array
 	 */
-     
+
 	public function get_config($config) {
 		global $db;
-        
+
 		// get the config data stored in the database
 		$settings = $db->get_results("SELECT config_data FROM aiki_config");
 
@@ -115,7 +115,7 @@
 				$config = $config + $temp;
 			}
 		}
-        
+
 		return $config;
 	}
 
@@ -139,7 +139,7 @@
     /**
      * Converts text to special characters.
      *
-     * Works with HTML special characters and few other special characters 
+     * Works with HTML special characters and few other special characters
      * that PHP does not normally convert.
      *
      * @param   string   $text text to convert to special characters
@@ -150,7 +150,7 @@
         $text = htmlspecialchars($text);
 
         $html_chars = array(")", "(", "[", "]", "{", "|", "}", "<", ">", "_");
-        $html_entities = array("&#41;", "&#40;", "&#91;", "&#93;", "&#123;", 
+        $html_entities = array("&#41;", "&#40;", "&#91;", "&#93;", "&#123;",
                                "&#124;", "&#125;", "&#60;", "&#62;", "&#95;");
 
         $text = str_replace($html_chars, $html_entities,$text);
@@ -158,6 +158,137 @@
         return $text;
     }
 
+    /**
+     * test if var match condition
+     * Example ( *,foo) => true, (foo,foo)=>true, ( foo, !foo) false
+     * @param string condition
+     * @param string variable
+     *
+     * @return boolean
+     */
+    function match_pair_one( $condition, $var){
+        if ( $condition=='*' || $condition=='' || $condition==$var ||
+             (substr($condition,0,1)=="!" &&  $condition<>"!$var") ) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * test if var/var match condition
+     * Example ( asterisk/es,foo/es) => true, (foo,foo/fr)=>true, ( foo/!ru, foo/ru) false
+     * @param string condition
+     * @param string $first
+     * @param string $second
+     *
+     * @return boolean
+     */
+
+    function match_pair( $condition,$first, $second) {
+        //clean conditions,
+        $condition= strtr(
+                        $condition,
+                        array("\n" =>" ",
+                            ","  =>" ",
+                            "\r" =>" "));
+        $condition= preg_replace('/\s{2,}/', ' ', $condition); //clean double space
+        $condition= trim($condition);
+
+        $matches = explode (" ",$condition);
+
+        foreach ( $matches as $match) {
+            $pair = explode("/", $match,2);
+            if ( $this->match_pair_one($pair[0],$first ) &&
+                 ( !isset($pair[1]) || $this->match_pair_one($pair[1],$second) ) ){
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+    /**
+     * Search first innest block in a text.
+     * Example:
+     * inner_markup ( "(2*(x+1)(z)", "(", ")" ,$position) =>return true
+     *  and position of "x+1"
+     *
+     * @param string $string
+     * @param string $startDelim
+     * @param string $endDelim
+     * @param byval array $position array(0=>start-position, 1=>end-position)
+     *
+     * @return boolean
+     */
+
+    function inner_markup ( $string , $startDelim, $endDelim, &$position ){
+        $i= 10 ; //max level of recursion.
+
+        $start = strpos ($string, $startDelim);
+        if ( $start === false ) {
+            return false;
+        }
+
+        do {
+
+            $end  = strpos($string, $endDelim, $start);
+            if ( $end === false )
+                return false;
+            $nested = strpos ( $string, $startDelim, $start+1);
+            if ( $nested === false || $nested > $end ) {
+                $position = array ($start, $end) ;
+                return true;
+            }
+            $start = $nested;
+            $i--;
+        } while ($i);
+        return false;
+    }
+
+
+    /**
+     * Eval a expression thats contains basic operators (+,-*,/), 
+     * parentsis, and variables.
+     * 
+     * Example:
+     *  $x*2
+     *  ($x/2)-10
+     *
+     * @param string $expr String to be evaluated
+     * @param array $var Variable defintion. Passed by reference for speed.
+     *     will not be modified.
+     *
+     * @return number result
+     */
+
+    function eval_expression($expr, &$var){
+        $matches=0;
+        if ( $expr=="") {
+            return 0;
+        }elseif ( preg_match('/^[+-]?[0-9]*(\.[0-9]*)?$/', $expr, $matches) ) {
+            return (float) $expr;
+        } elseif ( preg_match('/^\$([a-z_]+[a-z_0-9]*)$/i', $expr, $matches) ) {        
+            return ( isset($var[$matches[1]]) ? $var[$matches[1]]: 0);
+        } elseif ( preg_match('/^(.*)\(([^\(\)]*)\)(.*)$/i', $expr, $matches) ) {
+            return meval ( $matches[1]. meval($matches[2],$var). $matches[3] , $var);
+        } elseif ( preg_match('~^(.*)([\+\-/\\\*%])(.*)$~', $expr, $matches)){
+             $op1= meval($matches[1], $var);
+             $op2= meval($matches[3], $var);
+             if( is_null($op1) || is_null($op2) ) {
+                 return NULL;
+             }
+             switch ($matches[2]){
+                 case "+": return $op1+$op2;
+                 case "-": return $op1-$op2;
+                 case "*": return $op1*$op2;
+                 case "/": return (int) ($op1/$op2);
+                 case "\\": return $op1/$op2;
+                 case "%": return $op1%$op2;
+             }
+        }
+        return NULL;
+    }
+
 
     /**
      * Replace Aiki vars with their assigned values.
@@ -168,7 +299,7 @@
      * @return string
      * @todo this function is seriously overloaded and needs to be rethought
      */
-    public function processVars($text) 
+    public function processVars($text)
     {
         global $aiki, $page, $membership, $config;
 
@@ -176,66 +307,66 @@
          * @todo Setting variables really doesn't have a place in this function
          */
         if ( function_exists('date_default_timezone_set') &&
-             function_exists('date_default_timezone_get') ) 
+             function_exists('date_default_timezone_get') )
         {
             if ( isset($config['timezone']) and !empty($config['timezone']) )
-                date_default_timezone_set($config['timezone']); 
+                date_default_timezone_set($config['timezone']);
             else
-                date_default_timezone_set(@date_default_timezone_get()); 
+                date_default_timezone_set(@date_default_timezone_get());
         }
-        
+
         $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[]= $config['url'];
+
+        if ( $prefix )      { $paths[] = $prefix; }
+        if ( $view_prefix)  { $paths[] = $view_prefix; }
+        if ( count($aiki->site->languages()) > 1 ){ $paths[] = $language; }
+
+        if ($config['pretty_urls'] == 0){
+            $text = preg_replace('/href\=\"\[root\](.*)\"/U',
+                                 'href="[root]?pretty=\\1"', $text);
+        }
+
         $aReplace = array (
-            "[userid]"      => $membership->userid,
-            "[full_name]" => $membership->full_name,
-            "[language]" => $aiki->site->language(),
-            "[username]" => $membership->username,
-            "[page]" => $page,
-            "[site_name]" => $aiki->site->site_name(),
-            "[site]" => $aiki->site->get_site(),
-            "[direction]" => $aiki->languages->dir,
-            "insertedby_username" => $membership->username,
-            "insertedby_userid" => $membership->userid,
-            "current_month" => $current_month,
-            "current_year" => $current_year,
-            "current_day" => $current_day
-            );
-        $text= strtr ( $text, $aReplace );
-        
-        
-        // calculate route, including if need, language.
-        $prefix = $aiki->site->prefix();
-        if ( count($aiki->site->languages()) > 1 ){                        
-            $route= $config['url']  .  ($prefix ? "/$prefix" : "" ) . '/' . $aiki->site->language();
-        } else {
-            $route= $config['url'] . ($prefix ? "/$prefix" : "" ) ;
-        }
-        $routes= array(
-            '[root]'          => $config['url'], 
+            '[userid]'    => $membership->userid,
+            '[full_name]' => $membership->full_name,
+            '[language]'  => $aiki->site->language(),
+            '[username]'  => $membership->username,
+            '[page]'      => $page,
+            '[site_name]' => $aiki->site->site_name(),
+            '[site]'      => $aiki->site->get_site(),
+            '[view]'      => $aiki->site->view(),
+            '[direction]' => $aiki->languages->dir,
+            'insertedby_username' => $membership->username,
+            'insertedby_userid' => $membership->userid,
+            'current_month' => $current_month,
+            'current_year' => $current_year,
+            'current_day' => $current_day,
+            '[root]'          => $config['url'],
             '[root-language]' => $config['url'].  "/" . $aiki->site->language(),
             '[site_prefix]'   => $prefix ,
-            '[route]'         => $route );        
+            '[view_prefix]'   => $view_prefix ,
+            '[route]'         => implode("/",$paths) );
+        $text= strtr ( $text, $aReplace );
 
-        if ($config['pretty_urls'] == 0){
-            $text = preg_replace('/href\=\"\[root\](.*)\"/U', 
-                                 'href="[root]?pretty=\\1"', $text);
-            $text = strtr( $text, $routes);
-            $text = str_replace( '=/' , '=', $text);
-        }else{
-            $text = strtr( $text, $routes);          
-        }
+        //@TODO by rg1024, this hack is necesary...
         $text = str_replace($config['url'].'/', $config['url'], $text);
 
-        // substitute all [POST[key]] and [GET[key]] 
+        // substitute all [POST[key]] and [GET[key]]
         $matches= array();
         if ( preg_match_all("/\[(POST|GET)\[(.*)\]\]/U", $text, $matches)){
-        
+
             foreach ($matches[0] as $i => $match) {
                 $method= $matches[1][$i];
-                $key   = $matches[2][$i];            
+                $key   = $matches[2][$i];
                 if ( $method=="GET" && isset($_GET[$key])) {
                     $value = $_GET[$key];
                 } elseif ($method=="POST" && isset($_POST[$key])) {
@@ -246,8 +377,8 @@
                 $replace[$match] = $value;
             }
             $text = strtr( $text, $replace);
-       }   
-    
+       }
+
     return $text;
     } // end of processVars method
 } // end of aiki class

=== added file 'src/libs/css_parser.php'
--- src/libs/css_parser.php	1970-01-01 00:00:00 +0000
+++ src/libs/css_parser.php	2011-09-16 20:16:12 +0000
@@ -0,0 +1,176 @@
+<?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');}
+
+/**
+ * Parses CSS inserting variables, evaluating expressions and 
+ * filtering conditional block. 
+ * 
+ *
+ */
+
+class css_parser {
+
+    /**
+     * Complete parse of a text.
+     *
+     * @param string $style Text to be filter
+     * @param array  $array initial variables.
+     * 
+     * @return string. Filtered css.
+     *
+     */
+
+    function parse( $style, $vars){
+        $style = $this->style_parse_conditional_css ( $style, $vars );
+        $style = $this->style_parse_declarations( $style, $vars );
+        $style = $this->style_parse_vars($style, $vars);    
+        return $style;
+    }
+
+
+    /**
+     * Parse conditional css.
+     * Filter all "(css( )css)": if not match delete block else clean
+     * begin and end delimiters
+     *
+     * @param string $style Text to be filter
+     * @param array  $array Contain site,language and view variable (as keys)
+     *
+     */
+
+    function style_parse_conditional_css( $style, $vars ){
+      global $aiki;
+      
+      $view    = $aiki->site->view();
+      $language= $aiki->site->language();
+      
+      $position = array();
+      while ( $aiki->inner_markup ( $style, "(css(", ")css)", $position) ){
+            $length = $position[1]+5-$position[0]; // 5 = len of ")css)"
+            $condition =  explode ( ":",  substr( $style, $position[0]+5,$length-10),2); // 5=(css( 10=(css(+)css)
+            if ( isset($condition[1]) && $aiki->match_pair($condition[0],$view,$language) ) {
+               $content= $condition[1];
+            } else {
+               $content="";
+            }
+            $style= substr_replace ( $style, $content, $position[0], $length);
+      }
+      return $style;
+    }
+
+    /**
+     * Parse variable block in style.
+     *
+     * @param string $style text (plain css) to be filter
+     * @param byref array $array var
+     *
+     * @return string $style filtered
+     */
+
+    function style_parse_declarations ( $style, &$var ){
+        global $aiki;
+        
+        $view    = $aiki->site->view();
+        $language= $aiki->site->language();
+              
+        if ( preg_match_all ( "#\(declarations\((.*)\)declarations\)#Us", $style, $matches)){
+            
+            foreach ($matches[1] as $i=>$declarations){
+                    $declarations= preg_replace ("#/\*.*\*/#Us","",$declarations);   // supress comentaries
+                    $lines= explode ("\n", $declarations);
+                    $firstline = true;
+                    
+                    foreach ($lines as $line ){                        
+                        $line= trim($line);  // clean line.
+                        
+                        // test first conditional line. It's optional
+                        if ( $firstline ) {
+                            if ($line=="" || $aiki->match_pair(substr($line,0,-1),$view,$language) ){
+                                $firstline = false;
+                                continue;
+                            } else {                              
+                                break;
+                            }   
+                        }
+                                                
+                        if ( !$line  || strpos($line,"=")===false ) {
+                            continue;
+                        }
+                        list($key,$value)= explode("=",$line,2);
+                        
+                        $key  = trim($key);
+                        $value= trim($value);
+                        
+                        if ( substr($key,0,1)!='$') {
+                            continue;
+                        }
+                        $key = substr($key,1);
+                                                            
+                        if ( $value && substr($value,0,1)=="(" && substr($value,-1,1)==")" ) {                        
+                            
+                            $value= substr($value,1,-1);                        
+                            $value= meval($value, $var) ;                            
+                        }
+
+                        $var[ $key ] = $value;
+
+                    }
+            }
+            $style= preg_replace ( "#\(declarations\(.*\)declarations\)#Us", "", $style);
+        }
+        return $style;
+    }
+
+
+    /**
+     * Insert variable values in text style
+     * begin and end delimiters
+     *
+     * @param string $style text (plain css) to be filter
+     * @param array $var variables.
+     *
+     * @return string $style parsed.
+     */
+
+    function style_parse_vars ( &$style, &$vars ){
+        global $aiki;
+        // variables
+        krsort($vars); // important! $username must be replace first than $user
+        foreach ($vars as $var=>$value){
+            $style= str_replace("\$$var", $value, $style);
+        }
+
+        // expressions
+        $empty= array();
+        if ( preg_match_all ('~\(([0-9'. preg_quote("+- /\*()%") .  ']*)\)~',$style, $matches) ) {
+            foreach ( $matches[1] as $i=>$match ){
+                $match = preg_replace('~\s~','',$match); //remove space necesary only for human ;-)
+                $expresions[ $matches[0][$i] ] = $aiki->eval_expression($match, $empty);
+            }
+            $style= strtr($style,$expresions);
+        }
+
+        return $style;
+
+    }
+
+
+}

=== modified file 'src/libs/output.php'
--- src/libs/output.php	2011-08-20 21:29:58 +0000
+++ src/libs/output.php	2011-09-16 20:16:12 +0000
@@ -152,11 +152,13 @@
 				else
 					$language = $config['default_language'];
 
+                $view=$aiki->site->view();// comodity
 				$header .= sprintf(
                     '<link rel="stylesheet" type="text/css" '.
-                    ' href="%sstyle.php?site=%s&amp;widgets=%s&amp;language=%s" />',
+                    ' href="%sstyle.php?site=%s&amp;%swidgets=%s&amp;language=%s" />',
                     $config['url'],
                     $aiki->site->get_site(),
+                    ( $view ?  "view={$view}&amp;" : ""),
                     implode("_", $layout->widgets_css),
                     $language);
 			}

=== modified file 'src/libs/site.php'
--- src/libs/site.php	2011-09-07 21:21:22 +0000
+++ src/libs/site.php	2011-09-16 20:16:12 +0000
@@ -30,27 +30,38 @@
     private $languages; // a array like [0]=>'en',[1]=>'fr'...
     private $need_translation;
     private $default_language;
-    private $pretty_url;
-    private $site_prefix; // 
-
-
-    /**
-     * return site prefix (beginin with "/" or blank space)
+    private $site_prefix; 
+    private $site_view;
+    private $site_view_prefix;
+
+    /**
+     * return site view 
+     * @return string 
+     */      
+
+    function view(){
+        return $this->site_view;        
+    }
+    
+     /**
+     * return site view prefix
+     * @return string 
+     */      
+
+    function view_prefix(){
+        return $this->site_view_prefix;        
+    }
+    
+    
+    /**
+     * return site prefix
      * @return string 
      */      
 
     function prefix(){
         return $this->site_prefix;        
     }
-
-    /**
-     * return the pretty url ( with site path removed )
-     * @return string 
-     */      
-
-    function pretty_url(){
-        return $this->pretty_url;        
-    }
+    
     
      /**
      * return the default language of a site.
@@ -129,49 +140,59 @@
         // determine site name
         if (isset($_GET['site'])) {
             $config['site'] = addslashes($_GET['site']);
-        } elseif ( !isset( $config['site'] )) {
-            $config['site'] = 'default';
-        }  
-        
-        // determine site by url (for multisite and apps)
-        $this->site_prefix = "";                    
-        $this->pretty_url= $aiki->pretty_url();        
-        if ( $this->pretty_url ){
-            $paths = explode("/", str_replace("|", "/", $this->pretty_url));
-            if ( $paths[0] ) {                
-                $site= $db->get_var("SELECT site_shortcut from aiki_sites where site_prefix='{$paths[0]}'" );
-                if ( $site ){
-                    $config['site'] = $site;                    
-                    $this->pretty_url = count($paths)==1 ? "" : substr( $this->pretty_url, strpos($this->pretty_url,"/")+1);
-                    $this->site_prefix = "{$paths[0]}";                    
-                }    
-            }                
-        } 
+        } else {
+            if ( !isset( $config['site'] )) {
+                $config['site'] = 'default';
+            }    
+         
+            // determine site by url (for multisite and apps)
+            $this->site_prefix = "";                            
+            $path = $aiki->url->first_url();
+            if ( $path != "homepage" ) {                
+                    $site= $db->get_var("SELECT site_shortcut from aiki_sites where site_prefix='$path'" );
+                    if ( $site ){
+                        $config['site'] = $site;                    
+                        $aiki->url->shift_url();
+                        $this->site_prefix = $path;                  
+                    }    
+            }                        
+        }
         
         // try read site information and test if is_active.
-        $info = $db->get_row("SELECT * from aiki_sites where site_shortcut='{$config['site']}' limit 1");
-        $error = false;
+        $info = $db->get_row("SELECT * from aiki_sites where site_shortcut='{$config['site']}' limit 1");        
         if ( is_null($info) ) {        
-            $error =  "Fatal Error: Wrong site name provided. " .
+            die( $aiki->message->error ( "Fatal Error: Wrong site name provided. " .
                       (defined('ENABLE_RUNTIME_INSTALLER') && ENABLE_RUNTIME_INSTALLER == FALSE ?
-                      "ENABLE_RUNTIME_INSTALLER is set to FALSE." : "");      
+                      "ENABLE_RUNTIME_INSTALLER is set to FALSE." : ""),
+                NULL, false) );      
         } elseif ( $info->is_active != 1) {
-            $error = $info->if_closed_output ? 
-                $info->if_closed_output : 
-                "Site {$config['site']} is closed.";
+            die( $aiki->messgae->error($info->if_closed_output ?  $info->if_closed_output :  "Site {$config['site']} is closed.",
+                NULL,
+                false) );
+        }
             
-        }
-        if ( $error ){
-            die( $aiki->message->error( $error, NULL, false) ); 
-        }    
-        
-        // language settings.
+        // determine view
+        if (isset($_GET['view'])) {
+            $this->site_view= addslashes($_GET['view']);             
+        } else {
+            $prefix = $aiki->url->first_url();
+            $view = $db->get_var("SELECT view_name, view_prefix FROM aiki_views ".
+                    " WHERE view_site='{$config['site']}' AND view_active='1' AND view_prefix='$prefix'");
+            if ( $view ){
+                $aiki->url->shift_url();            
+                $this->site_view = $view;
+                $this->site_view_prefix= $prefix;                        
+            }
+        }     
+                                
+        // define default language, list of allowed languages
         $this->default_language = ( $info->site_default_language ? 
             $info->site_default_language :
             "en");
         $this->languages        = ( $info->site_languages ? 
             explode(",",$info->site_languages) : 
-            array("en") );
+            array($this->default_language) );
+            
         $this->widget_language  = ( $info->widget_language ? 
             $info->widget_language : 
             $this->default_language );
@@ -180,8 +201,15 @@
             // correction: include default in allowed languages.
             $this->languages[]= $this->default_language;
         }                
+        
+        // determine language 
+        if (isset($_GET['language'])) {            
+            $this->language(addslashes($_GET['language']));
+        } elseif ( $this->language( $aiki->url->first_url()) ) {            
+            $aiki->url->shift_url();
+        }
         $this->need_translation = ( $this->default_language != $this->widget_language);
-        
+                
         // site names
         $this->site      = $config['site'];
         $this->site_name = $info->site_name;

=== modified file 'src/libs/url.php'
--- src/libs/url.php	2011-09-07 21:21:22 +0000
+++ src/libs/url.php	2011-09-16 20:16:12 +0000
@@ -37,6 +37,8 @@
      * @var string 
      * @access public
      */
+     
+     
 	public $url;
 	public $pretty;
   
@@ -46,11 +48,34 @@
      */
 	public $url_count;
 
+
+    /**
+	 * return first route of url 
+     * example: with foo/bar  -> return foo. In index return "homepage"
+     * @return string First part of url, or "homepage".
+     * 
+	 */
+    public function first_url(){
+        return current( $this->url);
+    }
+
+	/**
+	 * remove first route of url ( /foo/bar to /bar) .
+	 */
+    public function shift_url(){
+        if ( count($this->url) > 1) {
+             $this->pretty = substr($this->pretty, strlen($this->url[0])+1) ;
+             array_shift( $this->url );   
+        } else {
+            $this->url = array ("homepage");
+            $this->pretty="";            
+        }            
+    }
+
 	/**
 	 * Sets up the url for further processing.
 	 */
-	public function url(){
-        global $aiki;
+	public function url(){        
 		/**
 		 * 
 		 * url procces requests transformed by .htaccess by this rule:
@@ -59,24 +84,10 @@
 		 * So, in homepage (direct index.php)) 'pretty' doesn't exist, 		
 		 * 
 		 */
-		if ( $aiki->site->pretty_url() ) { 
-			$this->pretty= $aiki->site->pretty_url();
-			$this->url = explode("/", str_replace("|", "/", $this->pretty) );
-            
-            // check if url begins with a valid language.
-            if ( in_array( $this->url[0], $aiki->site->languages() ) ) {            
-                $aiki->site->language($this->url[0]);
-                if ( count($this->url) > 1) {
-                    $this->pretty = substr($this->pretty, strlen($this->url[0])+1) ;
-                    array_shift( $this->url );                    
-                } else {
-                    $this->url = array ("homepage");
-                    $this->pretty="";
-                }
-                
-            }
-            
-            
+                 
+		if ( isset($_GET['pretty']) )  { 
+			$this->pretty= $_GET['pretty'];
+			$this->url = explode("/", str_replace("|", "/", $this->pretty) );             
 		} else {
 			$this->url[0]="homepage";
 			$this->pretty="";

=== added file 'src/libs/view_parser.php'
--- src/libs/view_parser.php	1970-01-01 00:00:00 +0000
+++ src/libs/view_parser.php	2011-09-16 20:16:12 +0000
@@ -0,0 +1,94 @@
+<?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');}
+
+
+/*
+$vars = array ("view"=>"bluemarine", "lang"=>"this is", "language"=>"eu", "vie"=>"aa");
+
+
+$style = "body { background: url(\$site/body.png); }
+(css(bluemarine:
+(css(es: esto es para es)css)
+(css(eu: hau euskera da)css)
+)css)
+
+(css(mobil:
+ hau no tiene que salir.<br>
+)css)
+
+(css(eu:
+ hau bai <br>
+)css)
+
+
+view: \$view
+saludo: \$hola
+que: \$que vacio?
+color: \$red
+(declarations(
+red= #fff
+)declarations)
+
+vista: \$view
+
+(declarations(
+hola= 10
+que=
+)declarations)
+"; */
+
+
+/**
+ * viwe
+ *
+ *
+ */
+
+
+class view_parser  {
+
+    /**
+     * Parse conditional css. 
+     * Filter all "(css( )css)": if not match delete block else clean 
+     * begin and end delimiters
+     * 
+     * @param string $style Text to be filter
+     * @param array  $array Contain site,language and view variable (as keys) 
+     * 
+     */
+
+    function parse( $text, $view, $language ){  
+        global $aiki;    
+                        
+        $position = array();
+        while ( $aiki->inner_markup ( $text, "(view(", ")view)", $position) ){
+            $length = $position[1]+6-$position[0]; // 6 = len of ")view)" 
+            $condition = explode ( ":",  substr( $text, $position[0]+6,$length-12),2); // 6=(view( 12
+            if ( isset($condition[1]) && $aiki->match_pair($condition[0],$view,$language) ) {
+                $content= $condition[1];
+            } else {
+                $content="";
+            }
+            $text= substr_replace ( $text, $content, $position[0], $length);     
+        } 
+        return $text;
+    }
+
+}

=== modified file 'src/libs/widgets.php'
--- src/libs/widgets.php	2011-09-07 21:21:22 +0000
+++ src/libs/widgets.php	2011-09-16 20:16:12 +0000
@@ -382,6 +382,11 @@
 		 */
 		//$widget->widget = htmlspecialchars_decode($widget->widget);
 
+        $widget->widget = $aiki->view_parser->parse(
+            $widget->widget, 
+            $aiki->site->view(),
+            $aiki->site->language() );
+
 		$widget->widget = $aiki->input->requests($widget->widget);
 		$widget->widget = $aiki->processVars($widget->widget);
 

=== modified file 'src/sql/CreateTables.sql'
--- src/sql/CreateTables.sql	2011-09-07 21:21:22 +0000
+++ src/sql/CreateTables.sql	2011-09-16 20:16:12 +0000
@@ -139,6 +139,23 @@
 
 -- ------------------------------------------------------
 
+CREATE TABLE IF NOT EXISTS aiki_views (
+  view_id int(11) NOT NULL AUTO_INCREMENT,
+  view_name varchar(32) NOT NULL,
+  view_site varchar(32) NOT NULL,
+  view_active int(11) NOT NULL,
+  view_prefix varchar(32) NOT NULL,
+  view_use_prefix int(11) NOT NULL,
+  view_url varchar(255) NOT NULL,
+  view_short_description text NOT NULL,
+  view_description text NOT NULL,
+  PRIMARY KEY (view_id),
+  KEY view_prefix (view_prefix),
+  KEY view_site (view_site`)
+) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
+
+-- ------------------------------------------------------
+
 CREATE TABLE IF NOT EXISTS aiki_sites (
   site_id int(11) NOT NULL AUTO_INCREMENT,
   site_name varchar(255) NOT NULL,
@@ -149,8 +166,9 @@
   site_languages text NOT NULL,
   widget_language varchar(5) NOT NULL,
   site_prefix varchar(80) NOT NULL DEFAULT '',
-  PRIMARY KEY (`site_id`),
-  KEY `site_prefix` (`site_prefix`)
+  site_default_view varchar(32) NOT NULL,
+  PRIMARY KEY (site_id),
+  KEY site_prefix (site_prefix)
 ) ENGINE=MyISAM CHARSET=utf8;
 
 -- ------------------------------------------------------

=== modified file 'src/style.php'
--- src/style.php	2011-07-13 21:31:56 +0000
+++ src/style.php	2011-09-16 20:16:12 +0000
@@ -37,8 +37,6 @@
  */
 $widgets_list = isset($_GET['widgets']) ? addslashes($_GET['widgets']) : '';
 
-
-
 if ( $widgets_list != ''){
 	
 	$where = "id='" . str_replace('_', "' or id = '", $widgets_list). "'";
@@ -52,13 +50,28 @@
 
 	if ($get_widgets)
     {
+        
+        $style="";
 		foreach ( $get_widgets as $widget )
 		{
             /**
              * @todo need to be able to disable all output, if not in debug
              */
-			echo "\n/*CSS for the widget {$widget->widget_name}({$widget->id}) */\n";
-			echo stripcslashes($aiki->languages->L10n($widget->css));
+            if ( $widget->css != "" ) {
+                $style .="\n/*CSS for the widget {$widget->widget_name} (id {$widget->id}) */\n".
+                          stripcslashes($aiki->languages->L10n($widget->css));
+            }                                  
 		}
+        if ( $style ){            
+            // predefined vars.
+            $vars  = array (
+                "view"     => $aiki->site->view(),
+                "language" => $aiki->site->language(),
+                "site"     => $aiki->site->get_site());
+                            
+            $style = $aiki->css_parser->parse( $style, $vars );            
+        }   
+        echo $style;
+        
 	}
 }

_______________________________________________
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