Example #1
0
function block_group_form($item)
{
    global $DB;
    global $website;
    global $layout;
    global $theme;
    global $current_version;
    $navibars = new navibars();
    $naviforms = new naviforms();
    if (empty($item->id)) {
        $navibars->title(t(23, 'Blocks') . ' / ' . t(506, 'Groups') . ' / ' . t(38, 'Create'));
    } else {
        $navibars->title(t(23, 'Blocks') . ' / ' . t(506, 'Groups') . ' / ' . t(170, 'Edit') . ' [' . $item->id . ']');
    }
    if (empty($item->id)) {
        $navibars->add_actions(array('<a href="#" onclick="navigate_tabform_submit(0);"><img height="16" align="absmiddle" width="16" src="img/icons/silk/accept.png"> ' . t(34, 'Save') . '</a>'));
    } else {
        $navibars->add_actions(array('<a href="#" onclick="navigate_tabform_submit(0);"><img height="16" align="absmiddle" width="16" src="img/icons/silk/accept.png"> ' . t(34, 'Save') . '</a>', '<a href="#" onclick="navigate_delete_dialog();"><img height="16" align="absmiddle" width="16" src="img/icons/silk/cancel.png"> ' . t(35, 'Delete') . '</a>'));
        $layout->add_script('
            function navigate_delete_dialog()
            {
                navigate_confirmation_dialog(
                    function() { window.location.href = "?fid=blocks&act=block_group_delete&id=' . $item->id . '"; }, 
                    null, null, "' . t(35, 'Delete') . '"
                );
            }
        ');
    }
    $navibars->add_actions(array('<a href="?fid=' . $_REQUEST['fid'] . '&act=0"><img height="16" align="absmiddle" width="16" src="img/icons/silk/brick.png"> ' . t(23, 'Blocks') . '</a>', '<a href="?fid=' . $_REQUEST['fid'] . '&act=block_types_list"><img height="16" align="absmiddle" width="16" src="img/icons/silk/brick_edit.png"> ' . t(167, 'Types') . '</a>'));
    if (!empty($item->id)) {
        $notes = grid_notes::comments('block_group', $item->id);
        $navibars->add_actions(array('<a href="#" onclick="javascript: navigate_display_notes_dialog();"><span class="navigate_grid_notes_span" style=" width: 20px; line-height: 16px; ">' . count($notes) . '</span><img src="img/skins/badge.png" width="20px" height="18px" style="margin-top: -2px;" class="grid_note_edit" align="absmiddle" /> ' . t(168, 'Notes') . '</a>'));
    }
    $navibars->add_actions(array(!empty($item->id) ? '<a href="?fid=' . $_REQUEST['fid'] . '&act=block_group_edit"><img height="16" align="absmiddle" width="16" src="img/icons/silk/add.png"> ' . t(38, 'Create') . '</a>' : '', '<a href="?fid=' . $_REQUEST['fid'] . '&act=block_groups_list"><img height="16" align="absmiddle" width="16" src="img/icons/silk/application_view_list.png"> ' . t(39, 'List') . '</a>'));
    $navibars->form();
    if (!empty($item->id)) {
        $navibars->add_tab(t(23, "Blocks"));
        $allowed_types = array();
        if (!empty($item->code)) {
            for ($bg = 0; $bg < count($theme->block_groups); $bg++) {
                if ($theme->block_groups[$bg]->id == $item->code) {
                    if (isset($theme->block_groups[$bg]->allowed_types)) {
                        $allowed_types = $theme->block_groups[$bg]->allowed_types;
                    }
                    break;
                }
            }
        }
        $blocks_selected = array();
        if (!is_array($item->blocks)) {
            $item->blocks = array();
        }
        $navibars->add_tab_content($naviforms->hidden('blocks_group_selection', json_encode($item->blocks)));
        $navibars->add_tab_content($naviforms->hidden('blocks-order', ""));
        $block_types = block::types();
        $lang = $website->languages_published[0];
        $extensions_blocks = extension::blocks();
        for ($p = 0; $p < count($item->blocks); $p++) {
            unset($block);
            switch ($item->blocks[$p]['type']) {
                case "block":
                    $block = new block();
                    $block->load($item->blocks[$p]['id']);
                    if (empty($block) || empty($block->type)) {
                        continue;
                    }
                    $blocks_selected[] = '
                        <div class="block_group_block ui-state-default" data-block-id="' . $block->id . '" data-block-type="block" data-block-uid="' . $item->blocks[$p]['uid'] . '">
                            <div class="actions">
                                <a href="?fid=blocks&act=edit&id=' . $block->id . '"><img src="' . NAVIGATE_URL . '/img/icons/silk/pencil.png" /></a>
                                <a href="#" onclick="navigate_blocks_selection_remove(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cancel.png" /></a>
                            </div>
                            <div class="title">' . $block->dictionary[$lang]['title'] . '</div>
                            <div class="subcomment"><span style="float: right;">ID ' . $block->id . '</span><img src="img/icons/silk/brick.png" /> ' . $theme->t($block->type) . '</div>
                        </div>
                    ';
                    break;
                case "block_type":
                    for ($bt = 0; $bt < count($block_types); $bt++) {
                        if ($block_types[$bt]['id'] == $item->blocks[$p]['id']) {
                            $block = $block_types[$bt];
                            break;
                        }
                    }
                    $blocks_selected[] = '
                        <div class="block_group_block ui-state-default" data-block-id="' . $block['code'] . '" data-block-type="block_type" data-block-uid="' . $item->blocks[$p]['uid'] . '">
                            <div class="actions">
                                <a href="#" data-block-group="' . $block['block_group'] . '" data-block-type-code="' . $block['code'] . '" data-block-type-title="(span)" onclick="navigate_blocks_block_type_title(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/text_horizontalrule.png" /><span class="hidden">' . $item->blocks[$p]['title'] . '</span></a>
                                <a href="#" onclick="navigate_blocks_selection_remove(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cancel.png" /></a>
                            </div>
                            <div class="title" title="' . $block['description'] . '">' . $block['title'] . '</div>
                            <div class="subcomment">
                                <span style="float: right;">ID ' . $block['code'] . '</span>
                                <img src="img/icons/silk/brick_link.png" /> ' . $block['count'] . ' ' . ($block['count'] == 1 ? t(437, "Block") : t(23, "Blocks")) . '
                            </div>
                        </div>
                    ';
                    break;
                case "block_group_block":
                    if (is_array($theme->block_groups)) {
                        foreach ($theme->block_groups as $key => $bg) {
                            for ($i = 0; $i < count($bg->blocks); $i++) {
                                if ($bg->blocks[$i]->id == $item->blocks[$p]['id']) {
                                    $block = array('code' => $bg->blocks[$i]->id, 'type' => $bg->blocks[$i]->id, 'title' => $theme->t($bg->blocks[$i]->title), 'description' => $theme->t($bg->blocks[$i]->description), 'properties' => $bg->blocks[$i]->properties, 'block_group' => $bg->id);
                                    break;
                                }
                            }
                        }
                    }
                    $blocks_selected[] = '
                        <div class="block_group_block ui-state-default" data-block-id="' . $block['code'] . '" data-block-type="block_group_block"  data-block-uid="' . $item->blocks[$p]['uid'] . '">
                            <div class="actions">
                                ' . (empty($block['properties']) ? '' : '<a href="#" data-block-group="' . $block['block_group'] . '" data-block-group-block="' . $block['code'] . '" data-block-group-action="settings" onclick="navigate_blocks_group_block_settings(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cog.png" /></a>') . '
                                <a href="#" onclick="navigate_blocks_selection_remove(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cancel.png" /></a>
                            </div>
                            <div class="title" title="' . $block['description'] . '">' . $block['title'] . '</div>
                            <div class="subcomment"><span style="float: right;">ID ' . $block['type'] . '</span><img src="img/icons/silk/bricks.png" /> ' . $theme->t($block['type']) . '</div>
                        </div>
                    ';
                    break;
                case "extension":
                    $block = $item->blocks[$p];
                    for ($be = 0; $be < count($extensions_blocks); $be++) {
                        if ($block['id'] == $extensions_blocks[$be]->id) {
                            $extension = new extension();
                            $extension->load($block['extension']);
                            $blocks_selected[] = '
                                <div class="block_group_block ui-state-default" data-block-id="' . $block['id'] . '" data-block-type="extension" data-block-extension="' . $block['extension'] . '"  data-block-uid="' . $item->blocks[$p]['uid'] . '">
                                    <div class="actions">
                                        ' . (empty($extensions_blocks[$be]->properties) ? '' : '<a href="#" data-block-group="' . $item->code . '" data-block-id="' . $block['id'] . '" data-block-extension="' . $block['extension'] . '" data-block-group-action="settings" onclick="navigate_block_group_extension_block_settings(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cog.png" /></a>') . '
                                        <a href="#" onclick="navigate_blocks_selection_remove(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cancel.png" /></a>
                                    </div>
                                    <div class="title">' . $extension->t($extensions_blocks[$be]->title) . '</div>
                                    <div class="subcomment"><span style="float: right;">ID ' . $block['id'] . '</span><img src="img/icons/silk/plugin.png" /> ' . $extension->title . '</div>
                                </div>
                            ';
                            break;
                        }
                    }
                    break;
            }
        }
        $blocks_selected = implode("\n", $blocks_selected);
        $navibars->add_tab_content('<div id="block_group_selected_blocks" style="width: 49%; float: left; margin-right: 2%;">
                <div class="ui-accordion ui-widget ui-helper-reset">
                    <h3 class="ui-accordion-header ui-state-default ui-accordion-icons ui-accordion-header-active ui-state-active ui-corner-top">
                        <img src="img/icons/silk/bricks.png" style="vertical-align: middle;" /> ' . t(405, 'Selection') . '
                    </h3>
                    <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">' . $blocks_selected . '</div>
                </div>
                <div class="subcomment">
                    <img src="img/icons/silk/information.png" align="absmiddle" /> ' . t(72, "Drag any row to assign priorities") . '
                </div>
             </div>');
        // **** ADD specific BLOCKS ****
        $sql = '
             SELECT b.type, b.id, d.text as title
               FROM nv_blocks b
          LEFT JOIN nv_webdictionary d
                     ON b.id = d.node_id
                    AND d.node_type = "block"
                    AND d.subtype = "title"
                    AND d.lang = "' . $website->languages_list[0] . '"
                    AND d.website = ' . $website->id . '
              WHERE b.website = ' . $website->id . '
           ORDER BY b.id DESC';
        $DB->query($sql);
        $block_elements = $DB->result();
        $block_group_blocks = array();
        for ($bg = 0; $bg < count($theme->block_groups); $bg++) {
            if ($theme->block_groups[$bg]->id == $item->code) {
                $block_group_blocks = $theme->block_groups[$bg]->blocks;
            }
        }
        // blocks available in the accordion
        $navibars->add_tab_content('<div id="blocks_available_wrapper" style="float: left; width: 49%; ">
                <div id="blocks_available_accordion">
                    <h3><i class="fa fa-fw fa-cube"></i> ' . t(437, 'Block') . '</h3>
                    <div>
                    ' . implode("\n", array_map(function ($b) use($allowed_types) {
            global $theme;
            $classes = 'block_group_block ui-state-default';
            if (!empty($allowed_types) && !in_array($b->type, $allowed_types)) {
                $classes .= ' ui-state-disabled hidden';
            }
            $html = '<div class="' . $classes . '" data-block-id="' . $b->id . '" data-block-type="block">' . '<div class="actions">
                                                <a href="?fid=blocks&act=edit&id=' . $b->id . '"><img src="' . NAVIGATE_URL . '/img/icons/silk/pencil.png" /></a>
                                                <a href="#" onclick="navigate_blocks_selection_remove(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cancel.png" /></a>
                                            </div>' . '<div class="title">' . $b->title . '</div>' . '<div class="subcomment"><span style="float: right;">ID ' . $b->id . '</span><img src="img/icons/silk/brick.png" /> ' . $theme->t($b->type) . '</div>' . '</div>';
            return $html;
        }, $block_elements)) . '
                        <div class="navigate-block_group-accordion-info-link hidden"><i class="fa fa-eye-slash"></i>&nbsp;&nbsp;<a href="#">' . t(646, "Show all unselectable blocks") . '</a></div>
                    </div>
                    <h3><i class="fa fa-fw fa-cubes"></i> ' . t(543, 'Block type') . '</h3>
                    <div>
                    ' . implode("\n", array_map(function ($b) use($allowed_types) {
            $classes = 'block_group_block ui-state-default';
            if (!empty($allowed_types) && !in_array($b['id'], $allowed_types)) {
                $classes .= ' ui-state-disabled';
            }
            $html = '<div class="' . $classes . '" data-block-id="' . $b['id'] . '" data-block-type="block_type">' . '<div class="actions">
                                        <a href="#" data-block-group="' . $b['block_group'] . '" data-block-type-code="' . $b['code'] . '" data-block-type-title="(span)" onclick="navigate_blocks_block_type_title(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/text_horizontalrule.png" /><span class="hidden">' . $b['block_type_title'] . '</span></a>
                                        <a href="#" onclick="navigate_blocks_selection_remove(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cancel.png" /></a>
                                    </div>' . '<div class="title">' . $b['title'] . '</div>' . '<div class="subcomment">
                                        <span style="float: right;">' . $b['count'] . ' ' . ($b['count'] == 1 ? t(437, "Block") : t(23, "Blocks")) . '</span>
                                        <img src="img/icons/silk/brick_link.png" /> ID ' . $b['id'] . '</div>' . '</div>';
            return $html;
        }, $block_types)) . '
                    </div>
                    <h3><i class="fa fa-fw fa-plus-square-o"></i> ' . t(556, 'Block from group') . ' [' . $theme->t($item->code) . ']</h3>
                    <div>                    
                        ' . implode("\n", array_map(function ($b) use($item) {
            global $theme;
            $html = '<div class="block_group_block ui-state-default" data-block-id="' . $b->id . '" data-block-type="block_group_block" title="' . $theme->t(@$b->description) . '">' . '<div class="actions">
                                                    ' . (empty($b->properties) ? '' : '<a href="#" data-block-group="' . $item->code . '" data-block-group-block="' . $b->id . '" data-block-group-action="settings" onclick="navigate_blocks_group_block_settings(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cog.png" /></a>') . '
                                                    <a href="#" onclick="navigate_blocks_selection_remove(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cancel.png" /></a>
                                                </div>' . '<div class="title">' . $theme->t($b->title) . '</div>' . '<div class="subcomment">
                                                    <span style="float: right;">ID ' . $b->id . '</span>
                                                    <img src="img/icons/silk/bricks.png" />' . '</div>' . '</div>';
            return $html;
        }, $block_group_blocks)) . '
                    </div>
                    <h3><i class="fa fa-fw fa-puzzle-piece"></i> ' . t(327, 'Extensions') . '</h3>
                    <div>
                        ' . implode("\n", array_map(function ($b) use($allowed_types, $item) {
            $classes = 'block_group_block ui-state-default';
            $extension = new extension();
            $extension->load($b->_extension);
            $html = '<div class="' . $classes . '" data-block-id="' . $b->id . '" data-block-type="extension" data-block-extension="' . $b->_extension . '">' . '<div class="actions">
                                            ' . (empty($b->properties) ? '' : '<a href="#" data-block-group="' . $item->code . '" data-block-group-block="' . $b->id . '" data-block-group-action="settings" onclick="navigate_block_group_extension_block_settings(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cog.png" /></a>') . '
                                            <a href="#" onclick="navigate_blocks_selection_remove(this);"><img src="' . NAVIGATE_URL . '/img/icons/silk/cancel.png" /></a>
                                        </div>' . '<div class="title">' . $extension->t($b->title) . '</div>' . '<div class="subcomment"><span style="float: right;">ID ' . $b->id . '</span><img src="img/icons/silk/plugin.png" /> ' . $extension->title . '</div>' . '</div>';
            return $html;
        }, $extensions_blocks)) . '
                    </div>
                </div>
                <div class="subcomment">
                    <img src="img/icons/silk/information.png" align="absmiddle" /> ' . t(638, "Disabled blocks are not compatible with the current block group type") . '
                </div>
            </div>');
        $block_group_block_types_form = "";
        foreach ($website->languages_list as $lang) {
            $block_group_block_types_form .= ' 
                <div data-lang="' . $lang . '" class="navigate-form-row">
                    <label style="width: 48px; "><span title="' . language::name_by_code($lang) . '" class="navigate-form-row-language-info"><img align="absmiddle" src="img/icons/silk/comment.png">' . $lang . '</span></label>
                    <input type="text" style=" width: 340px;" name="block_type_title_value[' . $lang . ']" value="">
                </div>
            ';
        }
        $navibars->add_tab_content('
            <div id="navigate-block-groups-block-type-title" class="hidden">
                ' . $block_group_block_types_form . '
                <div class="subcomment" style="margin-left: 0;"><img src="img/icons/silk/information.png" /> ' . t(641, "It will only be shown if the template supports it") . '</div>
            </div>
        ');
        $layout->add_script('                       
            function navigate_blocks_block_type_title(el)
            {
                var title = $(el).find("span").text();
                
                try 
                {
                   title = jQuery.parseJSON(title);
                } 
                catch(e) 
                {
                    // not json; do nothing                    
                }                    
                
                $("#navigate-block-groups-block-type-title").find("input[type=text]").each(function()
                {                     
                    if(typeof(title)=="object")
                        $(this).val(title[$(this).parent().data("lang")]);
                    else
                        $(this).val(title);
                });
                
                $("#navigate-block-groups-block-type-title").removeClass("hidden");
                $("#navigate-block-groups-block-type-title").dialog({
                    title: navigate_t(67, "Title"),
                    modal: true,
                    width: 428,
                    buttons: [
                        {
                            text: navigate_t(190, "Ok"),
                            icons: {
                                primary: "ui-icon-check"
                            },
                            click: function()
                            {
                                var new_value = {};
                                
                                $("#navigate-block-groups-block-type-title")
                                    .find(\'input[type="text"]\').each(
                                        function()
                                        {
                                            new_value[$(this).parent().data("lang")] = $(this).val(); 
                                        }
                                    );
                                
                                $(el).find("span").text(JSON.stringify(new_value));
                                                            
                                blocks_selection_update();
                                
                                $( this ).dialog( "close" );
                            }
                        },
                        {
                            text: navigate_t(58, "Cancel"),
                            icons: {
                                primary: "ui-icon-close"
                            },
                            click: function()
                            {
                                $( this ).dialog( "close" );
                            }
                        }
                    ]
                });
            }
        ');
    }
    $navibars->add_tab(t(457, "Information"));
    $navibars->add_tab_content($naviforms->hidden('form-sent', 'true'));
    $navibars->add_tab_content($naviforms->hidden('id', $item->id));
    $navibars->add_tab_content_row(array('<label>ID</label>', '<span>' . (!empty($item->id) ? $item->id : t(52, '(new)')) . '</span>'));
    $navibars->add_tab_content_row(array('<label>' . t(67, 'Title') . '</label>', $naviforms->textfield('title', $item->title)));
    $blgroups = array();
    for ($blg = 0; $blg < count($theme->block_groups); $blg++) {
        $blgroups[$theme->block_groups[$blg]->id] = '';
        if (!empty($theme->block_groups[$blg]->description)) {
            $blgroups[$theme->block_groups[$blg]->id] = $theme->t($theme->block_groups[$blg]->description);
        }
    }
    if (!in_array($item->code, $blgroups)) {
        $blgroups[$item->code] = $item->code;
    }
    $navibars->add_tab_content_row(array('<label>' . t(237, 'Code') . '</label>', $naviforms->selectfield('code', array_keys($blgroups), array_keys($blgroups), $item->code, NULL, NULL, array_values($blgroups), "", true, true)));
    // DEPRECATED field, will be removed. Please use the Notes feature
    if (!empty($item->notes)) {
        $navibars->add_tab_content_row(array('<label>' . t(168, 'Notes') . '</label>', $naviforms->textarea('notes', $item->notes)));
    }
    if (!empty($item->id)) {
        $layout->navigate_notes_dialog('block_group', $item->id);
    }
    $layout->add_script('
        $.getScript("lib/packages/blocks/blocks.js?r=' . $current_version->revision . '", 
            function()
            {
                block_groups_onload();
            });
    ');
    return $navibars->generate();
}
Example #2
0
function nvweb_blocks($vars = array())
{
    global $website;
    global $DB;
    global $current;
    global $webgets;
    global $webuser;
    global $theme;
    $webget = 'blocks';
    nvweb_blocks_init();
    $out = '';
    $access = array();
    $access[] = 0;
    if (empty($current['webuser'])) {
        // 1: only signed in users, 2: only NON signed in users, 3: selected web user groups only
        $access[] = 2;
    } else {
        $access[] = 1;
        $access[] = 3;
    }
    // blocks type cache
    if (empty($webgets[$webget]['block_types'])) {
        $webgets[$webget]['block_types'] = block::types();
    }
    $block_types = $webgets[$webget]['block_types'];
    if ($vars['mode'] == 'single') {
        $order_mode = 'single';
    } else {
        // get the index for the block type requested
        $bti = array_multidimensional_search($block_types, array('code' => $vars['type']));
        // how must we process this block type
        $order_mode = @$block_types[$bti]['order'];
        if (empty($order_mode) || $order_mode == 'theme') {
            $order_mode = @$vars['mode'];
        }
    }
    // how many blocks of this type we have to show
    $howmany = @intval($block_types[$bti]['maximum']);
    if (empty($howmany) || $howmany > intval(@$vars['number']) && !empty($vars['number'])) {
        $howmany = intval(@$vars['number']) + 0;
    }
    // which zone of the block we have to return
    if (empty($vars['zone'])) {
        $vars['zone'] = 'block';
    }
    $categories = array();
    if (!empty($vars['categories'])) {
        $categories = explode(',', $vars['categories']);
        $categories = array_filter($categories);
    }
    if (isset($current['object']->category)) {
        $categories[] = $current['object']->category;
    } else {
        $categories[] = $current['category'];
    }
    $blocks = array();
    $categories_query = '';
    $exclusions_query = '';
    if (is_array($categories) && !empty($categories[0])) {
        foreach ($categories as $cq) {
            $categories_query .= " OR INSTR(CONCAT(',', categories, ','), '," . intval($cq) . ",') > 0 ";
            $exclusions_query .= " AND INSTR(CONCAT(',', exclusions, ','), '," . intval($cq) . ",') = 0 ";
        }
        $categories_query = ' AND (categories = "" ' . $categories_query . ') ';
    }
    switch ($order_mode) {
        case 'single':
            $query_type = '';
            if (!empty($vars['type'])) {
                $query_type = ' AND type = ' . protect($vars['type']);
            }
            $DB->query('
                SELECT id, type
                  FROM nv_blocks
                 WHERE enabled = 1
                   ' . $query_type . '
                   AND website = ' . $website->id . '
                   AND (date_published = 0 OR date_published < ' . core_time() . ')
                   AND (date_unpublish = 0 OR date_unpublish > ' . core_time() . ')
                   AND access IN(' . implode(',', $access) . ')
                   ' . $categories_query . '
                   ' . $exclusions_query . '
                   AND id = ' . protect($vars['id']));
            $row = $DB->first();
            if (!empty($row)) {
                $blocks[] = $row->id;
                $vars['type'] = $row->type;
            }
            break;
        case 'priority':
        case 'ordered':
            $DB->query('
                SELECT *
                  FROM nv_blocks
                 WHERE type = ' . protect($vars['type']) . '
                   AND enabled = 1
                   AND website = ' . $website->id . '
                   AND (date_published = 0 OR date_published < ' . core_time() . ')
                   AND (date_unpublish = 0 OR date_unpublish > ' . core_time() . ')
                   AND access IN(' . implode(',', $access) . ')
                   ' . $categories_query . '
                   ' . $exclusions_query . '
                ORDER BY position ASC
            ');
            $rows = $DB->result();
            foreach ($rows as $row) {
                if (!nvweb_block_enabled($row)) {
                    continue;
                }
                $row = (array) $row;
                $blocks[] = $row['id'];
            }
            break;
        default:
        case 'random':
            // "random" gets priority blocks first
            // retrieve fixed blocks
            $DB->query('
                SELECT *
                  FROM nv_blocks
                 WHERE type = ' . protect($vars['type']) . '
                   AND enabled = 1
                   AND website = ' . $website->id . '
                   AND (date_published = 0 OR date_published < ' . core_time() . ')
                   AND (date_unpublish = 0 OR date_unpublish > ' . core_time() . ')
                   AND access IN(' . implode(',', $access) . ')
                   AND fixed = 1
                   ' . $categories_query . '
                   ' . $exclusions_query . '
              ORDER BY position ASC
            ');
            $fixed_rows = $DB->result();
            $fixed_rows_ids = $DB->result('id');
            if (empty($fixed_rows_ids)) {
                $fixed_rows_ids = array(0);
            }
            // now retrieve the other blocks in random order
            $DB->query('
			    SELECT *
                  FROM nv_blocks
                 WHERE type = ' . protect($vars['type']) . '
                   AND enabled = 1
                   AND website = ' . $website->id . '
                   AND (date_published = 0 OR date_published < ' . core_time() . ')
                   AND (date_unpublish = 0 OR date_unpublish > ' . core_time() . ')
                   AND access IN(' . implode(',', $access) . ')
                   AND id NOT IN(' . implode(",", $fixed_rows_ids) . ')
                   ' . $categories_query . '
                   ' . $exclusions_query . '
                 ORDER BY RAND()
            ');
            $random_rows = $DB->result();
            // mix rows (fixed and random)
            // fixed position rows
            foreach ($fixed_rows as $fr) {
                if (!nvweb_block_enabled($fr)) {
                    continue;
                }
                $blocks[$fr->position] = $fr->id;
            }
            // random position rows
            $pos = 0;
            foreach ($random_rows as $rr) {
                if (!nvweb_block_enabled($rr)) {
                    continue;
                }
                // find next free position
                $free = false;
                while (!$free) {
                    $free = empty($blocks[$pos]);
                    if (!$free) {
                        $pos++;
                    }
                }
                $blocks[$pos] = $rr->id;
            }
            // sort array by key
            ksort($blocks);
            break;
    }
    // render the blocks found
    $shown = 0;
    $position = 1;
    $block_objects = array();
    foreach ($blocks as $id) {
        if ($howmany > 0 && $shown >= $howmany) {
            break;
        }
        $block = new block();
        $block->load($id);
        $bt = 'block';
        for ($bti = 0; $bti < count($block_types); $bti++) {
            if ($block->type == $block_types[$bti]['code']) {
                $bt = $block_types[$bti]['type'];
                break;
            }
        }
        // SKIP all blocks until a certain position
        if (isset($vars['position']) && $vars['position'] != $position) {
            $position++;
            continue;
        }
        // RENDER block zone
        switch ($vars['zone']) {
            case 'object':
                $block_objects[] = clone $block;
                break;
            case 'title':
                $out .= '<span class="block-' . $vars['type'] . '-title" zone="title" ng-block-id="' . $block->id . '">' . $block->dictionary[$current['lang']]['title'] . '</span>';
                break;
            case 'content':
            case 'block':
            default:
                if ($bt == 'theme') {
                    $fn = 'nvweb_' . $website->theme . '_blocks_render';
                    $out .= '<div class="block-' . $vars['type'] . '" ng-block-id="' . $block->id . '">';
                    $out .= $fn($block, $vars);
                    $out .= '</div>' . "\n";
                } else {
                    $out .= '<div class="block-' . $vars['type'] . '" ng-block-id="' . $block->id . '">';
                    $out .= nvweb_blocks_render($vars['type'], $block->trigger, $block->action, $vars['zone'], $block, $vars);
                    $out .= '</div>' . "\n";
                }
                break;
        }
        // the block requested at a CERTAIN POSITION was this, we've finished
        if (isset($vars['position']) && $vars['position'] == $position) {
            break;
        }
        $shown++;
        $position++;
    }
    if ($vars['zone'] == 'object') {
        $out = array($block_objects, count($block_objects));
    }
    return $out;
}
Example #3
0
 public function load_from_resultset($rs)
 {
     $main = $rs[0];
     $this->id = $main->id;
     $this->website = $main->website;
     $this->type = $main->type;
     $this->date_published = empty($main->date_published) ? '' : $main->date_published;
     $this->date_unpublish = empty($main->date_unpublish) ? '' : $main->date_unpublish;
     $this->date_modified = empty($main->date_modified) ? '' : $main->date_modified;
     $this->access = $main->access;
     $this->enabled = $main->enabled;
     $this->trigger = mb_unserialize($main->trigger);
     if (is_array($this->trigger['trigger-html'])) {
         foreach ($this->trigger['trigger-html'] as $language => $code) {
             $this->trigger['trigger-html'][$language] = htmlspecialchars_decode($code);
         }
     }
     if (is_array($this->trigger['trigger-content'])) {
         foreach ($this->trigger['trigger-content'] as $language => $code) {
             $this->trigger['trigger-content'][$language] = stripslashes($code);
         }
     }
     $this->action = mb_unserialize($main->action);
     $this->notes = $main->notes;
     $this->dictionary = webdictionary::load_element_strings('block', $this->id);
     // title
     $this->position = $main->position;
     $this->fixed = $main->fixed;
     $this->categories = array_filter(explode(',', $main->categories));
     $this->exclusions = array_filter(explode(',', $main->exclusions));
     $this->elements = json_decode($main->elements, true);
     // to get the array of groups first we remove the "g" character
     $groups = str_replace('g', '', $main->groups);
     $this->groups = explode(',', $groups);
     if (!is_array($this->groups)) {
         $this->groups = array($groups);
     }
     $block_classes = block::types();
     foreach ($block_classes as $bc) {
         if ($bc['code'] == $this->type) {
             $this->class = $bc['type'];
         }
     }
 }
Example #4
0
function nvweb_properties($vars = array())
{
    global $website;
    global $DB;
    global $current;
    global $cache;
    global $properties;
    global $webuser;
    $out = '';
    switch (@$vars['mode']) {
        case 'website':
            $wproperty = new property();
            $wproperty->load_from_theme($vars['property']);
            if (!empty($wproperty)) {
                $out = nvweb_properties_render($wproperty, $vars);
            }
            break;
        case 'webuser':
            $wuproperty = new property();
            $wuproperty->load_from_webuser($vars['property']);
            if (!empty($wuproperty)) {
                $out = nvweb_properties_render($wuproperty, $vars);
            }
            break;
        case 'element':
        case 'item':
            // deprecated, may be removed in a future version
            // if item ID is not given and the current object an element or a structure category?
            if (empty($vars['id']) && $current['type'] == 'structure') {
                // find the first embedded element for the current category
                // (because the template code has requested specifically to return the property from an element!)
                $itm = nvweb_content_items($current['object']->id, true, 1, true, 'priority');
                if (!empty($itm) && isset($itm[0])) {
                    $vars['id'] = $itm[0]->id;
                } else {
                    $vars['id'] = 0;
                }
            }
            if (!isset($properties['item-' . $vars['id']]) && !empty($vars['id'])) {
                // load item template
                if (empty($vars['template'])) {
                    $vars['template'] = $DB->query_single('template', 'nv_items', ' id = ' . protect($vars['id']));
                }
                // if template is not defined (embedded element), take its category template
                if (empty($vars['template'])) {
                    $vars['template'] = $DB->query_single('template', 'nv_structure', ' id = (
                            SELECT category
                            FROM nv_items
                            WHERE id = ' . intval($vars['id']) . '
                        )');
                }
                $properties['item-' . $vars['id']] = property::load_properties("item", $vars['template'], 'item', $vars['id']);
            } else {
                if (empty($vars['id'])) {
                    $vars['type'] = $current['object']->template;
                    if ($current['type'] == "item") {
                        $vars['id'] = $current['object']->id;
                    } else {
                        if ($current['type'] == "structure") {
                            // find the first embedded content associated with this structure entry
                            // (because the template code has requested specifically to return the property from an element!)
                            $itm = nvweb_content_items($current['object']->id, true, 1, true, 'priority');
                            if (!empty($itm) && isset($itm[0])) {
                                $vars['id'] = $itm[0]->id;
                            } else {
                                $vars['id'] = 0;
                            }
                        }
                    }
                    if (!isset($properties['item-' . $vars['id']])) {
                        $properties['item-' . $current['object']->id] = property::load_properties("item", $vars['type'], 'item', $vars['id']);
                    }
                }
            }
            $current_properties = $properties['item-' . $vars['id']];
            // now we find the property requested
            if (!is_array($current_properties)) {
                $current_properties = array();
            }
            foreach ($current_properties as $property) {
                if ($property->id == $vars['property'] || $property->name == $vars['property']) {
                    $out = nvweb_properties_render($property, $vars);
                    break;
                }
            }
            break;
        case 'block':
            if (!isset($properties['block-' . $vars['id']])) {
                // load item type
                if (empty($vars['type'])) {
                    $vars['type'] = $DB->query_single('type', 'nv_blocks', ' id = ' . protect($vars['id']));
                    if (empty($cache['block_types'])) {
                        $cache['block_types'] = block::types();
                    }
                    // we need to know if the block is defined in the active theme or in the database (numeric ID)
                    foreach ($cache['block_types'] as $bt) {
                        if ($bt['code'] == $vars['type']) {
                            $vars['type'] = $bt['id'];
                            break;
                        }
                    }
                }
                $properties['block-' . $vars['id']] = property::load_properties("block", $vars['type'], 'block', $vars['id']);
            }
            $current_properties = $properties['block-' . $vars['id']];
            // now we find the property requested
            if (!is_array($current_properties)) {
                $current_properties = array();
            }
            foreach ($current_properties as $property) {
                if ($property->id == $vars['property'] || $property->name == $vars['property']) {
                    $out = nvweb_properties_render($property, $vars);
                    break;
                }
            }
            break;
        case 'block_group_block':
            // find block_group block definition
            $block_group = null;
            // unknown
            $block_code = $vars['id'];
            $block_uid = $vars['uid'];
            if (empty($block_code)) {
                $block = block::block_group_block_by_property($vars['property']);
                $block_code = $block->type;
            } else {
                // find the block group block by its type
                $block = block::block_group_block($block_group, $block_code);
            }
            $properties = $block->properties;
            $current_properties = property::load_properties($block_code, $block->_block_group_id, 'block_group_block', $block_code, $block_uid);
            // now we find the property requested
            if (!is_array($current_properties)) {
                $current_properties = array();
            }
            foreach ($current_properties as $property) {
                if ($property->id == $vars['property'] || $property->name == $vars['property']) {
                    $out = nvweb_properties_render($property, $vars);
                    break;
                }
            }
            break;
        case 'structure':
            if (empty($vars['id'])) {
                if ($current['type'] == 'structure') {
                    $vars['id'] = $current['id'];
                } else {
                    $vars['id'] = $current['object']->category;
                }
            }
            if (!isset($properties['structure-' . $vars['id']])) {
                // load category template
                $category_template = $DB->query_single('template', 'nv_structure', ' id = ' . protect($vars['id']));
                if (!empty($category_template)) {
                    $properties['structure-' . $vars['id']] = property::load_properties("structure", $category_template, 'structure', $vars['id']);
                }
            }
            $current_properties = $properties['structure-' . $vars['id']];
            // now we find the property requested
            if (!is_array($current_properties)) {
                $current_properties = array();
            }
            foreach ($current_properties as $property) {
                if ($property->id == $vars['property'] || $property->name == $vars['property']) {
                    if ($vars['return'] == 'object') {
                        $out = $property;
                    } else {
                        $out = nvweb_properties_render($property, $vars);
                    }
                    break;
                }
            }
            break;
        case 'comment':
            if (!isset($properties['comment-' . $vars['id']])) {
                $properties['comment-' . $vars['id']] = property::load_properties("comment", $vars['template'], 'comment', $vars['id']);
            }
            $current_properties = $properties['comment-' . $vars['id']];
            // now we find the property requested
            if (!is_array($current_properties)) {
                $current_properties = array();
            }
            foreach ($current_properties as $property) {
                if ($property->id == $vars['property'] || $property->name == $vars['property']) {
                    if ($vars['return'] == 'object') {
                        $out = $property;
                    } else {
                        $out = nvweb_properties_render($property, $vars);
                    }
                    break;
                }
            }
            break;
        default:
            // find the property source by its name
            $current_properties = array();
            // get website theme property
            $current_properties[] = new property();
            $current_properties[0]->load_from_theme($vars['property']);
            if ($current['type'] == 'item') {
                if (!isset($properties['item-' . $current['object']->id])) {
                    $properties['item-' . $current['object']->id] = property::load_properties("item", $current['object']->template, 'item', $current['object']->id);
                }
                $current_properties = array_merge($current_properties, $properties['item-' . $current['object']->id]);
            } else {
                if ($current['type'] == 'structure') {
                    if (!isset($properties['structure-' . $current['object']->id])) {
                        $properties['structure-' . $current['object']->id] = property::load_properties("structure", $current['object']->template, 'structure', $current['object']->id);
                    }
                    $current_properties = array_merge($current_properties, $properties['structure-' . $current['object']->id]);
                    // the property could also be in the first item associated to this structure element
                    $structure_items = nvweb_content_items($current['object']->id, true, 1);
                    if (!empty($structure_items)) {
                        if (empty($structure_items[0]->template)) {
                            $structure_items[0]->template = $current['template'];
                        }
                        $properties['item-' . $structure_items[0]->id] = property::load_properties("item", $structure_items[0]->template, 'item', $structure_items[0]->id);
                    }
                    if (!empty($properties['item-' . $structure_items[0]->id])) {
                        $current_properties = array_merge($current_properties, $properties['item-' . $structure_items[0]->id]);
                    }
                } else {
                    if ($current['type'] == 'article') {
                        // TO DO
                    } else {
                        // unknown object type, maybe is an object managed by an extension?
                        if (!isset($properties[$current['type'] . '-' . $current['object']->id])) {
                            $properties[$current['type'] . '-' . $current['object']->id] = property::load_properties($current['type'], $current['object']->template, $current['type'], $current['object']->id);
                        }
                        $current_properties = array_merge($current_properties, $properties[$current['type'] . '-' . $current['object']->id]);
                    }
                }
            }
            // now we find the property requested
            if (!is_array($current_properties)) {
                $current_properties = array();
            }
            foreach ($current_properties as $property) {
                if ($property->id == $vars['property'] || $property->name == $vars['property']) {
                    $out = nvweb_properties_render($property, $vars);
                    break;
                }
            }
            break;
    }
    return $out;
}