Exemple #1
0
 public function transformToFloxim()
 {
     $tokens = $this->tokenize();
     $tree = $this->makeTree($tokens);
     $unnamed_replaces = array();
     $tree->apply(function (HtmlToken $n) use(&$unnamed_replaces) {
         if ($n->name == 'text') {
             return;
         }
         if (preg_match('~\\{[\\%|\\$]~', $n->source)) {
             $n->source = Html::parseFloximVarsInAtts($n->source);
         }
         $subroot = $n->hasAttribute('fx:omit') ? '' : ' subroot="true"';
         if (($n->name == 'script' || $n->name == 'style') && !$n->hasAttribute('fx:raw')) {
             $n->setAttribute('fx:raw', 'true');
         }
         if ($n->hasAttribute('fx:raw')) {
             $raw_value = $n->getAttribute('fx:raw');
             if ($raw_value != 'false') {
                 $n->addChildFirst(HtmlToken::create('{raw}'));
                 $n->addChild(HtmlToken::create('{/raw}'));
             }
             $n->removeAttribute('fx:raw');
         }
         if ($n->name == 'meta' && ($layout_id = $n->getAttribute('fx:layout'))) {
             $layout_name = $n->getAttribute('fx:name');
             $tpl_tag = '{template id="' . $layout_id . '" name="' . $layout_name . '" of="layout:show"}';
             $tpl_tag .= '{apply id="_layout_body"}';
             $content = $n->getAttribute('content');
             $vars = explode(",", $content);
             foreach ($vars as $var) {
                 $var = trim($var);
                 $negative = false;
                 if (preg_match("~^!~", $var)) {
                     $negative = true;
                     $var = preg_replace("~^!~", '', $var);
                 }
                 $tpl_tag .= '{$' . $var . ' select="' . ($negative ? 'false' : 'true') . '" /}';
             }
             $tpl_tag .= '{/call}{/template}';
             $n->parent->addChildBefore(HtmlToken::create($tpl_tag), $n);
             $n->remove();
             return;
         }
         if ($fx_replace = $n->getAttribute('fx:replace')) {
             $replace_atts = explode(",", $fx_replace);
             foreach ($replace_atts as $replace_att) {
                 if (!isset($unnamed_replaces[$replace_att])) {
                     $unnamed_replaces[$replace_att] = 0;
                 }
                 $var_name = 'replace_' . $replace_att . '_' . $unnamed_replaces[$replace_att];
                 $unnamed_replaces[$replace_att]++;
                 $default_val = $n->getAttribute($replace_att);
                 switch ($replace_att) {
                     case 'src':
                         $var_title = fx::alang('Picture', 'system');
                         break;
                     case 'href':
                         $var_title = fx::alang('Link', 'system');
                         break;
                     default:
                         $var_title = $replace_att;
                         break;
                 }
                 $n->setAttribute($replace_att, '{%' . $var_name . ' title="' . $var_title . '"}' . $default_val . '{/%' . $var_name . '}');
                 $n->removeAttribute('fx:replace');
             }
         }
         if ($var_name = $n->getAttribute('fx:var')) {
             if (!preg_match("~^[\$\\%]~", $var_name)) {
                 $var_name = '%' . $var_name;
             }
             $n->addChildFirst(HtmlToken::create('{' . $var_name . '}'));
             $n->addChild(HtmlToken::create('{/' . $var_name . '}'));
             $n->removeAttribute('fx:var');
         }
         $tpl_id = $n->getAttribute('fx:template');
         $macro_id = $n->getAttribute('fx:macro');
         if ($tpl_id || $macro_id) {
             if ($macro_id) {
                 $tpl_id = $macro_id;
             }
             if (preg_match("~\\[(.+?)\\]~s", $tpl_id, $tpl_test)) {
                 $tpl_test = preg_replace("~[\r\n]~", ' ', $tpl_test[1]);
                 $tpl_id = preg_replace("~\\[.+?\\]~s", '', $tpl_id);
             }
             $tpl_macro_tag = '{template id="' . $tpl_id . '" ';
             if ($macro_id) {
                 $tpl_macro_tag .= ' is_macro="true" ';
             }
             $tpl_macro_tag .= $subroot;
             if ($n->hasAttribute('fx:abstract')) {
                 $tpl_macro_tag .= ' is_abstract="true" ';
                 $n->removeAttribute('fx:abstract');
             }
             if ($tpl_for = $n->getAttribute('fx:of')) {
                 $tpl_macro_tag .= ' of="' . $tpl_for . '"';
                 $n->removeAttribute('fx:of');
             }
             if ($tpl_test || ($tpl_test = $n->getAttribute('fx:test'))) {
                 $tpl_macro_tag .= ' test="' . $tpl_test . '" ';
                 $n->removeAttribute('fx:test');
             }
             if ($tpl_name = $n->getAttribute('fx:name')) {
                 $tpl_macro_tag .= ' name="' . $tpl_name . '"';
                 $n->removeAttribute('fx:name');
             }
             if ($n->offset && $n->end_offset) {
                 $tpl_macro_tag .= ' offset="' . $n->offset[0] . ',' . $n->end_offset[1] . '" ';
             }
             if ($tpl_size = $n->getAttribute('fx:size')) {
                 $tpl_macro_tag .= ' size="' . $tpl_size . '" ';
                 $n->removeAttribute('fx:size');
             }
             if ($tpl_suit = $n->getAttribute('fx:suit')) {
                 $tpl_macro_tag .= ' suit="' . $tpl_suit . '"';
                 $n->removeAttribute('fx:suit');
             }
             if ($n->hasAttribute('fx:priority')) {
                 $tpl_priority = $n->getAttribute('fx:priority');
                 $tpl_macro_tag .= ' priority="' . $tpl_priority . '" ';
                 $n->removeAttribute('fx:priority');
             }
             $tpl_macro_tag .= '}';
             $n->wrap($tpl_macro_tag, '{/template}');
             $n->removeAttribute('fx:template');
             $n->removeAttribute('fx:macro');
         }
         if ($n->hasAttribute('fx:each')) {
             $each_id = $n->getAttribute('fx:each');
             $each_id = trim($each_id, '{}');
             $each_id = str_replace('"', '\\"', $each_id);
             $each_macro_tag = '{each ';
             $each_macro_tag .= $subroot;
             $each_macro_tag .= ' select="' . $each_id . '"';
             if ($each_as = $n->getAttribute('fx:as')) {
                 $each_macro_tag .= ' as="' . $each_as . '"';
                 $n->removeAttribute('fx:as');
             }
             if ($each_key = $n->getAttribute('fx:key')) {
                 $each_macro_tag .= ' key="' . $each_key . '"';
                 $n->removeAttribute('fx:key');
             }
             if ($prefix = $n->getAttribute('fx:prefix')) {
                 $each_macro_tag .= ' prefix="' . $prefix . '"';
                 $n->removeAttribute('fx:prefix');
             }
             if ($extract = $n->getAttribute('fx:extract')) {
                 $each_macro_tag .= ' extract="' . $extract . '"';
                 $n->removeAttribute('fx:extract');
             }
             if ($separator = $n->getAttribute('fx:separator')) {
                 $each_macro_tag .= ' separator="' . $separator . '"';
                 $n->removeAttribute('fx:separator');
             }
             $each_macro_tag .= '}';
             $n->wrap($each_macro_tag, '{/each}');
             $n->removeAttribute('fx:each');
         }
         if ($area_id = $n->getAttribute('fx:area')) {
             $n->removeAttribute('fx:area');
             $area = '{area id="' . $area_id . '" ';
             if ($area_size = $n->getAttribute('fx:size')) {
                 $area .= 'size="' . $area_size . '" ';
                 $n->removeAttribute('fx:size');
             } elseif ($area_size = $n->getAttribute('fx:area-size')) {
                 // use when fx:area and fx:template are placed on the same node
                 $area .= 'size="' . $area_size . '" ';
                 $n->removeAttribute('fx:area-size');
             }
             if ($area_suit = $n->getAttribute('fx:suit')) {
                 $area .= 'suit="' . $area_suit . '" ';
                 $n->removeAttribute('fx:suit');
             }
             if ($area_render = $n->getAttribute('fx:area-render')) {
                 $area .= 'render="' . $area_render . '" ';
                 $n->removeAttribute('fx:area-render');
             }
             if ($area_name = $n->getAttribute('fx:area-name')) {
                 $area .= 'name="' . $area_name . '" ';
                 $n->removeAttribute('fx:area-name');
             }
             $area .= '}';
             $n->addChildFirst(HtmlToken::create($area));
             $n->addChild(HtmlToken::create('{/area}'));
         }
         if ($n->hasAttribute('fx:item')) {
             $item_att = $n->getAttribute('fx:item');
             $n->removeAttribute('fx:item');
             $n->wrap('{item' . ($item_att ? ' test="' . $item_att . '"' : '') . $subroot . '}', '{/item}');
         }
         if ($n->hasAttribute('fx:aif')) {
             $if_test = $n->getAttribute('fx:aif');
             $ep = new ExpressionParser();
             $empty_cond = $ep->build($if_test);
             $class_code = '<?php echo (' . $empty_cond . ' ? "" : " fx_view_hidden ");?>';
             $n->addClass($class_code);
             $n->removeAttribute('fx:aif');
             $if_test .= ' || $_is_admin';
             $n->wrap('{if test="' . $if_test . '"}', '{/if}');
         }
         if ($n->hasAttribute('fx:if')) {
             $if_test = $n->getAttribute('fx:if');
             $n->removeAttribute('fx:if');
             $n->wrap('{if test="' . $if_test . '"}', '{/if}');
         }
         if ($with_each = $n->getAttribute('fx:with-each')) {
             $n->removeAttribute('fx:with-each');
             $weach_macro_tag = '{with-each ' . $with_each . '}';
             if ($separator = $n->getAttribute('fx:separator')) {
                 $weach_macro_tag .= '{separator}' . $separator . '{/separator}';
                 $n->removeAttribute('fx:separator');
             }
             $n->wrap($weach_macro_tag, '{/with-each}');
         }
         if ($with = $n->getAttribute('fx:with')) {
             $n->removeAttribute('fx:with');
             $n->wrap('{with select="' . $with . '" ' . $subroot . '}', '{/with}');
         }
         if ($n->hasAttribute('fx:separator')) {
             $n->wrap('{separator}', '{/separator}');
             $n->removeAttribute('fx:separator');
         }
         if ($elseif_test = $n->getAttribute('fx:elseif')) {
             $n->removeAttribute('fx:elseif');
             $n->wrap('{elseif test="' . $elseif_test . '"}', '{/elseif}');
         }
         if ($n->hasAttribute('fx:else')) {
             $n->removeAttribute('fx:else');
             $n->wrap('{else}', '{/else}');
         }
         if ($n->hasAttribute('fx:add')) {
             $add_mode = $n->getAttribute('fx:add');
             $n->removeAttribute('fx:add');
             $n->wrap('<?php $this->pushMode("add", "' . $add_mode . '"); ?>', '<?php $this->popMode("add"); ?>');
         }
         if ($n->hasAttribute('fx:omit')) {
             $omit = $n->getAttribute('fx:omit');
             if (empty($omit) || $omit == 'true') {
                 $omit = true;
             } else {
                 $ep = new ExpressionParser();
                 $omit = $ep->compile($ep->parse($omit));
             }
             $n->omit = $omit;
             $n->removeAttribute('fx:omit');
         }
         if ($n->hasAttribute('fx:e')) {
             $e_value = $n->getAttribute('fx:e');
             $n->addClass('{bem_element}' . $e_value . '{/bem_element}');
             $n->removeAttribute('fx:e');
         }
         if ($n->hasAttribute('fx:b')) {
             $b_value = $n->getAttribute('fx:b');
             $n->addClass('{bem_block}' . $b_value . '{/bem_block}');
             $n->removeAttribute('fx:b');
             $n->parent->addChildAfter(HtmlToken::create('<?php $this->bemStopBlock(); ?>'), $n);
         }
     });
     $res = $tree->serialize();
     //fx::log($res);
     return $res;
 }
Exemple #2
0
 public function addTemplateRecordMeta($html, $collection, $index, $is_subroot)
 {
     // do nothing if html is empty
     if (!trim($html)) {
         return $html;
     }
     $entity_atts = $this->getTemplateRecordAtts($collection, $index);
     if ($is_subroot) {
         $html = preg_replace_callback("~^(\\s*?)(<[^>]+>)~", function ($matches) use($entity_atts) {
             $tag = Template\HtmlToken::createStandalone($matches[2]);
             $tag->addMeta($entity_atts);
             return $matches[1] . $tag->serialize();
         }, $html);
         return $html;
     }
     $proc = new Template\Html($html);
     $html = $proc->addMeta($entity_atts);
     return $html;
 }
Exemple #3
0
 protected function addInfoblockMeta($html_result)
 {
     $controller_meta = $this->getResultMeta();
     if (!fx::isAdmin() && (!isset($controller_meta['ajax_access']) || !$controller_meta['ajax_access'])) {
         return $html_result;
     }
     $ib_info = array('id' => $this['id']);
     if (($vis = $this->getVisual()) && $vis['id']) {
         $ib_info['visual_id'] = $vis['id'];
     }
     $ib_info['controller'] = $this->getPropInherited('controller') . ':' . $this->getPropInherited('action');
     $ib_info['name'] = $this['name'];
     $meta = array('data-fx_infoblock' => $ib_info, 'class' => 'fx_infoblock fx_infoblock_' . $this['id']);
     foreach ($this->infoblock_meta as $meta_key => $meta_val) {
         // register only non-empty props
         if ($meta_val && !is_array($meta_val) || count($meta_val) > 0) {
             $meta['data-fx_' . $meta_key] = $meta_val;
         }
     }
     if (isset($_POST['_ajax_base_url'])) {
         $meta['data-fx_ajax_base_url'] = $_POST['_ajax_base_url'];
     }
     if ($this->isFake()) {
         $meta['class'] .= ' fx_infoblock_fake';
         if (!$this->getIbController()) {
             $controller_meta['hidden_placeholder'] = fx::alang('Fake infoblock data', 'system');
         }
     }
     if (isset($controller_meta['hidden']) && $controller_meta['hidden']) {
         $meta['class'] .= ' fx_infoblock_hidden';
     }
     if (count($controller_meta) > 0 && fx::isAdmin()) {
         $meta['data-fx_controller_meta'] = $controller_meta;
     }
     if ($this->isLayout()) {
         $meta['class'] .= ' fx_unselectable';
         $html_result = preg_replace_callback('~<body[^>]*?>~is', function ($matches) use($meta) {
             $body_tag = Template\HtmlToken::createStandalone($matches[0]);
             $body_tag->addMeta($meta);
             return $body_tag->serialize();
         }, $html_result);
     } elseif ($this->output_is_subroot && preg_match("~^(\\s*?)(<[^>]+?>)~", $html_result)) {
         $html_result = preg_replace_callback("~^(\\s*?)(<[^>]+?>)~", function ($matches) use($meta) {
             $tag = Template\HtmlToken::createStandalone($matches[2]);
             $tag->addMeta($meta);
             return $matches[1] . $tag->serialize();
         }, $html_result);
     } else {
         $html_proc = new Template\Html($html_result);
         $html_result = $html_proc->addMeta($meta, mb_strlen($html_result) > 10000);
     }
     return $html_result;
 }
Exemple #4
0
 protected function prepareFileData($file_data, $file)
 {
     // convert fx::attributes to the canonical Smarty-syntax
     $T = new Html($file_data);
     try {
         $file_data = $T->transformToFloxim();
     } catch (\Exception $e) {
         fx::debug('Floxim html parser error', $e->getMessage(), $file);
     }
     // remove fx-comments
     $file_data = preg_replace("~\\{\\*.*?\\*\\}~s", '', $file_data);
     $file_data = trim($file_data);
     if (!preg_match("~^{(template|preset)~", $file_data)) {
         $file_data = $this->wrapFile($file, $file_data);
     }
     return $file_data;
 }
Exemple #5
0
 protected static function replaceFieldsInText($html)
 {
     $html = preg_replace_callback("~###fxf(\\d+)###~", function ($matches) {
         $replacement = Field::$replacements[$matches[1]];
         if (isset($replacement[1]['html']) && $replacement[1]['html'] || isset($replacement[1]['type']) && $replacement[1]['type'] == 'html') {
             $tag_name = 'div';
         } else {
             $tag_name = Html::getWrapperTag($replacement[2]);
         }
         $tag = HtmlToken::createStandalone('<' . $tag_name . '>');
         $tag->addMeta(array('class' => 'fx_template_var', 'data-fx_var' => $replacement[1]));
         //fx_template_field::$replacements[$matches[1]] = null;
         Field::$fields_to_drop[] = $matches[1];
         $res = $tag->serialize() . self::replaceFields($replacement[2]) . '</' . $tag_name . '>';
         return $res;
     }, $html);
     return $html;
 }