Example #1
0
 /**
  *  Sorted a nested array by '$sort_by'
  *  property on each sub-array. , if you want 
  *  to see the original php file look filters/dictsort.php
  */
 static function generator($cmp, $args, $redirected)
 {
     if (!$redirected) {
         $cmp->Error("dictsort must be redirected to a variable using AS <varname>");
     }
     if (count($args) != 2) {
         $cmp->Error("Dictsort must have two params");
     }
     if (!Haanga_AST::is_var($args[0])) {
         $cmp->Error("Dictsort: First parameter must be an array");
     }
     $var = $cmp->get_context($args[0]['var']);
     $cmp->set_context($redirected, $var);
     $redirected = hvar($redirected);
     $field = hvar('field');
     $key = hvar('key');
     $code = hcode();
     $body = hcode();
     $body->decl(hvar('field', $key), hvar('item', $args[1]));
     $code->decl($redirected, $args[0]);
     $code->decl($field, array());
     $code->do_foreach($redirected, 'item', $key, $body);
     $code->do_exec('array_multisort', $field, hconst('SORT_REGULAR'), $redirected);
     return $code;
 }
Example #2
0
 static function generator($cmp, $args, $assign = NULL)
 {
     if (!$cmp->getOption('allow_exec')) {
         $cmp->Error("Tag exec is disabled for security reasons");
     }
     $code = hcode();
     if (Haanga_AST::is_var($args[0])) {
         $args[0] = $args[0]['var'];
     } else {
         if (Haanga_AST::is_str($args[0])) {
             $args[0] = $args[0]['string'];
         } else {
             $cmp->Error("invalid param");
         }
     }
     $exec = hexec($args[0]);
     for ($i = 1; $i < count($args); $i++) {
         $exec->param($args[$i]);
     }
     $exec->end();
     if ($assign) {
         $code->decl($assign, $exec);
     } else {
         $cmp->do_print($code, $exec);
     }
     return $code;
 }
Example #3
0
 static function generator($compiler, $args)
 {
     if (Haanga_AST::is_str($args[0])) {
         return hexec('strlen', $args[0]);
     }
     return hexpr_cond(hexec('is_array', $args[0]), hexec('count', $args[0]), hexec('strlen', $args[0]));
 }
Example #4
0
 static function generator($compiler, $args)
 {
     $count = hexec('count', $args[0]);
     $strlen = hexec('strlen', $args[0]);
     $vars = hexec('count', hexec('get_object_vars', $args[0]));
     $guess = hexpr_cond(hexec('is_array', $args[0]), hexec('count', $args[0]), hexec('strlen', $args[0]));
     if (Haanga_AST::is_var($args[0])) {
         /* if it is a variable, best effort to detect
            its type at compile time */
         $value = $compiler->get_context($args[0]['var']);
         if (is_array($value)) {
             return $count;
         } else {
             if (is_string($value)) {
                 return $strlen;
             } else {
                 if (is_object($value)) {
                     return $vars;
                 } else {
                     return $gess;
                 }
             }
         }
     }
     if (Haanga_AST::is_str($args[0])) {
         return $strlen;
     }
     return $guess;
 }
Example #5
0
 static function generator($cmp, $args)
 {
     foreach ($args as $arg) {
         if (Haanga_AST::is_var($arg)) {
             $cmp->set_safe($arg['var']);
         }
     }
     return hcode();
 }
Example #6
0
 /**
  *  firstof tag
  *
  */
 static function generator($cmp, $args)
 {
     $count = count($args);
     $args = array_reverse($args);
     for ($i = 0; $i < $count; $i++) {
         if (isset($expr) && Haanga_AST::is_var($args[$i])) {
             $expr = hexpr_cond(hexpr(hexec('empty', $args[$i]), '==', FALSE), $args[$i], $expr);
         } else {
             $expr = $args[$i];
         }
     }
     return $expr;
 }
Example #7
0
 static function generator($compiler, $args)
 {
     if (count($args) != 1) {
         $compiler->Error("templatetag only needs one parameter");
     }
     if (Haanga_AST::is_var($args[0])) {
         $type = $args[0]['var'];
         if (!is_string($type)) {
             $compiler->Error("Invalid parameter");
         }
     } else {
         if (Haanga_AST::is_str($args[0])) {
             $type = $args[0]['string'];
         }
     }
     switch ($type) {
         case 'openblock':
             $str = '{%';
             break;
         case 'closeblock':
             $str = '%}';
             break;
         case 'openbrace':
             $str = '{';
             break;
         case 'closebrace':
             $str = '}';
             break;
         case 'openvariable':
             $str = '{{';
             break;
         case 'closevariable':
             $str = '}}';
             break;
         case 'opencomment':
             $str = '{#';
             break;
         case 'closecomment':
             $str = '#}';
             break;
         default:
             $compiler->Error("Invalid parameter");
             break;
     }
     $code = hcode();
     $compiler->do_print($code, Haanga_AST::str($str));
     return $code;
 }
Example #8
0
 public static function generator($cmp, $args, $redirected)
 {
     if (count($args) != 1) {
         $cmp->Error("inline needs one argument");
     }
     if ($redirected) {
         $cmp->Error("inline can't be redirected to one variable");
     }
     if (!Haanga_AST::is_str($args[0])) {
         $cmp->Error("The argument to inline must be an string");
     }
     $file = $args[0]['string'];
     if (class_exists('Haanga')) {
         $file = Haanga::getTemplatePath($file);
     }
     if (!is_file($file)) {
         $cmp->Error("{$file} is not a template");
     }
     return $cmp->getOpCodes(file_get_contents($file), $file);
 }
Example #9
0
 static function generator($compiler, $args)
 {
     if (count($args) > 1) {
         if (!Haanga_AST::is_str($args[1])) {
             $compiler->Error("pluralize: First parameter must be an string");
         }
         $parts = explode(",", $args[1]['string']);
         $singular = "";
         if (count($parts) == 1) {
             $plural = $parts[0];
         } else {
             $singular = $parts[0];
             $plural = $parts[1];
         }
     } else {
         $singular = "";
         $plural = "s";
     }
     return hexpr_cond(hexpr($args[0], '<=', 1), $singular, $plural);
 }
Example #10
0
 static function generator($cmp, $args, $declared)
 {
     static $cycle = 0;
     if (!isset($cmp->cycle)) {
         $cmp->cycle = array();
     }
     $code = hcode();
     $index = 'index_' . $cycle;
     $def = 'def_cycle_' . $cycle;
     if (count($args) == 1 && Haanga_AST::is_var($args[0]) && isset($cmp->cycle[$args[0]['var']])) {
         $id = $cmp->cycle[$args[0]['var']];
         $index = 'index_' . $id;
         $def = 'def_cycle_' . $id;
     } else {
         if (!$declared) {
             $code->do_if(hexpr(hexec('isset', hvar($def)), '==', FALSE));
         }
         $code->decl($def, $args);
         if (!$declared) {
             $code->do_endif();
         }
     }
     /* isset($var) == FALSE */
     $expr = hexpr(hexec('isset', hvar($index)), '==', FALSE);
     $inc = hexpr(hexpr(hexpr(hvar($index), '+', 1)), '%', hexec('count', hvar($def)));
     if (!$declared) {
         if (isset($id)) {
             $code->decl($index, $inc);
         } else {
             $code->decl($index, hexpr_cond($expr, 0, $inc));
         }
         $code->end();
         $var = hvar($def, hvar($index));
         $cmp->do_print($code, $var);
     } else {
         $code->decl($index, -1);
         $cmp->cycle[$declared] = $cycle;
     }
     $cycle++;
     return $code;
 }
Example #11
0
 static function generator($cmp, $args, $assign = NULL)
 {
     if (!$cmp->getOption('allow_exec')) {
         $cmp->Error("Tag exec is disabled for security reasons");
     }
     $code = hcode();
     if (Haanga_AST::is_var($args[0])) {
         $args[0] = $args[0]['var'];
     } else {
         if (Haanga_AST::is_str($args[0])) {
             $args[0] = $args[0]['string'];
         } else {
             $cmp->Error("invalid param");
         }
     }
     // fix for static calls {{{
     if (is_array($args[0])) {
         $end = end($args[0]);
         if (isset($end['class'])) {
             $args[0][key($args[0])]['class'] = substr($end['class'], 1);
         }
     }
     // }}}
     $exec = hexec($args[0]);
     for ($i = 1; $i < count($args); $i++) {
         $exec->param($args[$i]);
     }
     $exec->end();
     if ($assign) {
         $code->decl($assign, $exec);
         // make it global
         $code->decl($cmp->getScopeVariable($assign), hvar($assign));
     } else {
         $cmp->do_print($code, $exec);
     }
     return $code;
 }
Example #12
0
 function set_unsafe($name)
 {
     if (!Haanga_AST::is_Var($name)) {
         $name = hvar($name)->getArray();
     }
     unset($this->safes[serialize($name)]);
 }
Example #13
0
 function do_foreach($array, $value, $key, Haanga_AST $body)
 {
     foreach (array('array', 'value', 'key') as $var) {
         if (${$var} === NULL) {
             continue;
         }
         $var1 =& ${$var};
         if (is_string($var1)) {
             $var1 = hvar($var1);
         }
         if (is_object($var1)) {
             $var1 = $var1->getArray();
         }
         if (empty($var1['var'])) {
             throw new Exception("Can't iterate, apparently {$var} isn't a variable");
         }
         $var1 = $var1['var'];
     }
     $def = array('op' => 'foreach', 'array' => $array, 'value' => $value);
     if ($key) {
         $def['key'] = $key;
     }
     $this->stack[] = $def;
     $this->stack = array_merge($this->stack, $body->getArray(TRUE));
     $this->stack[] = array('op' => 'end_foreach');
     return $this;
 }