/**
 * Parse Navigate CMS tags like:
 * <ul>
 * <li>&lt;nv object="list"&gt;&lt;/nv&gt;</li>
 * <li>&lt;nv object="search"&gt;&lt;/nv&gt;</li>
 * <li>&lt;nv object="conditional"&gt;&lt;/nv&gt;</li>
 * </ul>
 *
 * Generate the final HTML code for these special tags or convert them
 * to a simpler nv tags.
 *
 * @param $html
 * @return mixed
 */
function nvweb_template_parse_lists($html, $process_delayed = false)
{
    global $current;
    if ($process_delayed) {
        // time to process delayed nvlists and nvsearches
        foreach ($current['delayed_nvlists'] as $uid => $vars) {
            $content = nvweb_list($vars);
            $html = str_replace('<!--#' . $uid . '#-->', $content, $html);
        }
        foreach ($current['delayed_nvsearches'] as $uid => $vars) {
            $content = nvweb_search($vars);
            $html = str_replace('<!--#' . $uid . '#-->', $content, $html);
        }
        return $html;
    }
    // parse special navigate tags (includes, lists, searches...)
    $tags = nvweb_tags_extract($html, 'nv', true, true, 'UTF-8');
    foreach ($tags as $tag) {
        $content = '';
        $changed = false;
        switch ($tag['attributes']['object']) {
            case 'list':
                $template_end = nvweb_templates_find_closing_list_tag($html, $tag['offset']);
                $tag['length'] = $template_end - $tag['offset'] + strlen('</nv>');
                // remove tag characters
                $list = substr($html, $tag['offset'] + strlen($tag['full_tag']), $tag['length'] - strlen('</nv>') - strlen($tag['full_tag']));
                @(include_once NAVIGATE_PATH . '/lib/webgets/list.php');
                $vars = array_merge($tag['attributes'], array('template' => $list));
                if ($tag['attributes']['delayed'] == 'true') {
                    $list_uid = uniqid('nvlist-');
                    $current['delayed_nvlists'][$list_uid] = $vars;
                    $html = substr_replace($html, '<!--#' . $list_uid . '#-->', $tag['offset'], $tag['length']);
                    $changed = true;
                    continue;
                }
                $content = nvweb_list($vars);
                $html = substr_replace($html, $content, $tag['offset'], $tag['length']);
                $changed = true;
                break;
            case 'search':
                $template_end = nvweb_templates_find_closing_list_tag($html, $tag['offset']);
                $tag['length'] = $template_end - $tag['offset'] + strlen('</nv>');
                // remove tag characters
                $search = substr($html, $tag['offset'] + strlen($tag['full_tag']), $tag['length'] - strlen('</nv>') - strlen($tag['full_tag']));
                @(include_once NAVIGATE_PATH . '/lib/webgets/search.php');
                $vars = array_merge($tag['attributes'], array('template' => $search));
                if ($tag['attributes']['delayed'] == 'true') {
                    $search_uid = uniqid('nvsearch-');
                    $current['delayed_nvsearches'][$search_uid] = $vars;
                    $html = substr_replace($html, '<!--#' . $search_uid . '#-->', $tag['offset'], $tag['length']);
                    $changed = true;
                    continue;
                }
                $content = nvweb_search($vars);
                $html = substr_replace($html, $content, $tag['offset'], $tag['length']);
                $changed = true;
                break;
            case 'conditional':
                $template_end = nvweb_templates_find_closing_list_tag($html, $tag['offset']);
                $tag['length'] = $template_end - $tag['offset'] + strlen('</nv>');
                // remove tag characters
                $conditional = substr($html, $tag['offset'] + strlen($tag['full_tag']), $tag['length'] - strlen('</nv>') - strlen($tag['full_tag']));
                @(include_once NAVIGATE_PATH . '/lib/webgets/conditional.php');
                $vars = array_merge($tag['attributes'], array('_template' => $conditional));
                $content = nvweb_conditional($vars);
                $html = substr_replace($html, $content, $tag['offset'], $tag['length']);
                $changed = true;
                break;
        }
        if ($changed) {
            // ok, we've found and processed ONE special tag
            // now the HTML has changed, so the original positions of the special <nv> tags have also changed
            // we must finish the current loop and start a new one
            $html = nvweb_template_parse_lists($html);
            break;
        }
    }
    return $html;
}
Example #2
0
function nvweb_list_isolate_lists($item_html)
{
    $nested_lists_fragments = array();
    $nested_lists_tags = nvweb_tags_extract($item_html, 'nv', true, true, 'UTF-8');
    foreach ($nested_lists_tags as $tag) {
        $changed = false;
        switch ($tag['attributes']['object']) {
            case 'list':
            case 'search':
                $template_end = nvweb_templates_find_closing_list_tag($item_html, $tag['offset']);
                $tag['length'] = $template_end - $tag['offset'] + strlen('</nv>');
                // remove tag characters
                $list_template = substr($item_html, $tag['offset'] + strlen($tag['full_tag']), $tag['length'] - strlen('</nv>') - strlen($tag['full_tag']));
                $nested_list_vars = array_merge($tag['attributes'], array('template' => $list_template));
                $nested_list_uid = uniqid('nvlist-');
                $nested_lists_fragments[$nested_list_uid] = $nested_list_vars;
                $item_html = substr_replace($item_html, '<!--#' . $nested_list_uid . '#-->', $tag['offset'], $tag['length']);
                $changed = true;
                break;
        }
        // offsets may change due the replace
        if ($changed) {
            list($item_html, $nested_sub_lists_fragments) = nvweb_list_isolate_lists($item_html);
            $nested_lists_fragments = array_merge($nested_lists_fragments, $nested_sub_lists_fragments);
            break;
        }
    }
    return array($item_html, $nested_lists_fragments);
}