示例#1
0
function nvweb_content($vars = array())
{
    global $website;
    global $DB;
    global $current;
    global $template;
    global $structure;
    $out = '';
    switch (@$vars['mode']) {
        case 'title':
            if ($current['type'] == 'structure') {
                $rs = nvweb_content_items($current['object']->id, true, 1);
                $texts = webdictionary::load_element_strings('item', $rs[0]->id);
                $out = $texts[$current['lang']]['title'];
            } else {
                $texts = webdictionary::load_element_strings($current['type'], $current['object']->id);
                $out = $texts[$current['lang']]['title'];
            }
            if (!empty($vars['function'])) {
                eval('$out = ' . $vars['function'] . '("' . $out . '");');
            }
            break;
        case 'date':
        case 'date_post':
            $ts = $current['object']->date_to_display;
            // if no date, return nothing
            if (!empty($ts)) {
                $out = nvweb_content_date_format(@$vars['format'], $ts);
            }
            break;
        case 'date_created':
            $ts = $current['object']->date_created;
            $out = $vars['format'];
            $out = nvweb_content_date_format($out, $ts);
            break;
        case 'comments':
            // display published comments number for the current item
            $out = nvweb_content_comments_count();
            break;
        case 'views':
            $out = $current['object']->views;
            break;
        case 'summary':
            $length = 300;
            $allowed_tags = array();
            if (!empty($vars['length'])) {
                $length = intval($vars['length']);
            }
            $texts = webdictionary::load_element_strings('item', $current['object']->id);
            $text = $texts[$current['lang']]['main'];
            if (!empty($vars['allowed_tags'])) {
                $allowed_tags = explode(',', $vars['allowed_tags']);
            }
            $out = core_string_cut($text, 300, '…', $allowed_tags);
            break;
        case 'author':
            if (!empty($current['object']->author)) {
                $nu = new user();
                $nu->load($current['object']->author);
                $out = $nu->username;
                unset($nu);
            }
            if (empty($out)) {
                $out = $website->name;
            }
            break;
        case 'structure':
            // force loading structure data
            nvweb_menu();
            $structure_id = 0;
            if ($current['type'] == 'item') {
                $structure_id = $current['object']->category;
            } else {
                if ($current['type'] == 'structure') {
                    $structure_id = $current['object']->id;
                }
            }
            switch ($vars['return']) {
                case 'path':
                    $out = $structure['routes'][$structure_id];
                    break;
                case 'title':
                    $out = $structure['dictionary'][$structure_id];
                    break;
                case 'action':
                    $out = nvweb_menu_action($structure_id);
                    break;
                default:
            }
            break;
        case 'tags':
            $tags = array();
            $search_url = nvweb_source_url('theme', 'search');
            if (!empty($search_url)) {
                $search_url .= '?q=';
            } else {
                $search_url = NVWEB_ABSOLUTE . '/nvtags?q=';
            }
            $ids = array();
            if (empty($vars['separator'])) {
                $vars['separator'] = ' ';
            }
            $class = 'item-tag';
            if (!empty($vars['class'])) {
                $class = $vars['class'];
            }
            if (!empty($vars['id'])) {
                $itm = new item();
                $itm->load($vars['id']);
                $enabled = nvweb_object_enabled($itm);
                if ($enabled) {
                    $texts = webdictionary::load_element_strings('item', $itm->id);
                    $itags = explode(',', $texts[$current['lang']]['tags']);
                    if (!empty($itags)) {
                        for ($i = 0; $i < count($itags); $i++) {
                            if (empty($itags[$i])) {
                                continue;
                            }
                            $tags[$i] = '<a class="' . $class . '" href="' . $search_url . $itags[$i] . '">' . $itags[$i] . '</a>';
                        }
                    }
                }
            } else {
                if ($current['type'] == 'item') {
                    // check publishing is enabled
                    $enabled = nvweb_object_enabled($current['object']);
                    if ($enabled) {
                        $texts = webdictionary::load_element_strings('item', $current['object']->id);
                        $itags = explode(',', $texts[$current['lang']]['tags']);
                        if (!empty($itags)) {
                            for ($i = 0; $i < count($itags); $i++) {
                                if (empty($itags[$i])) {
                                    continue;
                                }
                                $tags[$i] = '<a class="' . $class . '" href="' . $search_url . $itags[$i] . '">' . $itags[$i] . '</a>';
                            }
                        }
                    }
                } else {
                    if ($current['type'] == 'structure') {
                        $rs = nvweb_content_items($current['object']->id);
                        foreach ($rs as $category_item) {
                            $enabled = nvweb_object_enabled($category_item);
                            if ($enabled) {
                                $texts = webdictionary::load_element_strings('item', $current['object']->id);
                                $itags = explode(',', $texts[$current['lang']]['tags']);
                                if (!empty($itags)) {
                                    for ($i = 0; $i < count($itags); $i++) {
                                        $tags[$i] = '<a class="' . $class . '" href="' . $search_url . $itags[$i] . '">' . $itags[$i] . '</a>';
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $out = implode($vars['separator'], $tags);
            break;
        case 'section':
        case 'body':
        default:
            if (empty($vars['section'])) {
                $vars['section'] = 'main';
            }
            $section = "section-" . $vars['section'];
            if ($current['type'] == 'item') {
                // check publishing is enabled
                $enabled = nvweb_object_enabled($current['object']);
                $texts = NULL;
                // retrieve last saved text (is a preview request from navigate)
                if ($_REQUEST['preview'] == 'true' && $current['navigate_session'] == 1) {
                    $texts = webdictionary_history::load_element_strings('item', $current['object']->id, 'latest');
                } else {
                    if ($enabled) {
                        $texts = webdictionary::load_element_strings('item', $current['object']->id);
                    }
                }
                // have we found any content?
                if (!empty($texts)) {
                    foreach ($template->sections as $tsection) {
                        if ($tsection['id'] == $vars['section'] || $tsection['code'] == $vars['section']) {
                            switch ($tsection['editor']) {
                                case 'raw':
                                    $out = nl2br($texts[$current['lang']][$section]);
                                    break;
                                case 'html':
                                case 'tinymce':
                                default:
                                    $out = $texts[$current['lang']][$section];
                                    break;
                            }
                            break;
                        }
                    }
                }
            } else {
                if ($current['type'] == 'structure') {
                    $rs = nvweb_content_items($current['object']->id);
                    foreach ($rs as $category_item) {
                        $enabled = nvweb_object_enabled($category_item);
                        if (!$enabled) {
                            continue;
                        } else {
                            $texts = webdictionary::load_element_strings('item', $category_item->id);
                            foreach ($template->sections as $tsection) {
                                if ($tsection['id'] == $vars['section'] || $tsection['code'] == $vars['section']) {
                                    switch ($tsection['editor']) {
                                        case 'raw':
                                            $texts[$current['lang']][$section] = nl2br($texts[$current['lang']][$section]);
                                            break;
                                        case 'html':
                                        case 'tinymce':
                                        default:
                                            // we don't need to change a thing
                                            // $texts[$current['lang']][$section] = $texts[$current['lang']][$section];
                                            break;
                                    }
                                    break;
                                }
                            }
                            $out .= '<div id="navigate-content-' . $category_item->id . '-' . $section . '">' . $texts[$current['lang']][$section] . '</div>';
                        }
                    }
                }
            }
            break;
    }
    return $out;
}
示例#2
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;
}
示例#3
0
function nvweb_list_parse_tag($tag, $item, $source = 'item', $item_relative_position, $item_absolute_position, $total)
{
    global $current;
    global $website;
    global $structure;
    global $DB;
    $out = '';
    switch ($tag['attributes']['source']) {
        // special condition, return direct query result values
        case 'query':
            $out = $item->_query->{$tag}['attributes']['value'];
            break;
            // special: return element position in list
        // special: return element position in list
        case 'position':
            $position = $item_relative_position;
            if ($tag['attributes']['absolute'] == 'true') {
                $position = $item_absolute_position;
            }
            switch ($tag['attributes']['type']) {
                case 'alphabetic':
                    $out = number2alphabet($position);
                    break;
                case 'numeric':
                default:
                    $out = $position + 1;
                    // first element is 1, but in list is zero
                    break;
            }
            break;
            // NOTE: the following refers to structure information of an ITEM, useless if the source are categories!
        // NOTE: the following refers to structure information of an ITEM, useless if the source are categories!
        case 'structure':
        case 'category':
            nvweb_menu_load_dictionary();
            // load menu translations if not already done
            nvweb_menu_load_routes();
            // load menu paths if not already done
            switch ($tag['attributes']['value']) {
                case 'title':
                    if ($source == 'structure' || $source == 'category') {
                        $out = $structure['dictionary'][$item->id];
                    } else {
                        $out = $structure['dictionary'][$item->category];
                    }
                    if (!empty($tag['attributes']['length'])) {
                        $out = core_string_cut($out, $tag['attributes']['length'], '&hellip;');
                    }
                    break;
                case 'slug':
                    if ($source == 'structure' || $source == 'category') {
                        $out = $structure['dictionary'][$item->id];
                    } else {
                        $out = $structure['dictionary'][$item->category];
                    }
                    // remove spaces, special chars, etc.
                    $out = core_string_clean($out);
                    $out = slug($out);
                    break;
                case 'property':
                    $id = $item->id;
                    if ($source != 'structure' && $source != 'category') {
                        $id = $item->category;
                    }
                    $nvweb_properties_parameters = array_replace($tag['attributes'], array('mode' => !isset($tag['attributes']['mode']) ? 'structure' : $tag['attributes']['mode'], 'id' => $id, 'property' => !empty($tag['attributes']['property']) ? $tag['attributes']['property'] : $tag['attributes']['name']));
                    $out = nvweb_properties($nvweb_properties_parameters);
                    break;
                case 'url':
                case 'path':
                    if ($source == 'structure' || $source == 'category') {
                        $out = $structure['routes'][$item->id];
                    } else {
                        $out = $structure['routes'][$item->category];
                    }
                    $out = nvweb_prepare_link($out);
                    break;
                case 'id':
                    if ($source == 'structure' || $source == 'category') {
                        $out = $item->id;
                    } else {
                        // source = 'item'?
                        $out = $item->category;
                    }
                    break;
                default:
                    break;
            }
            break;
            // ITEM comments
        // ITEM comments
        case 'comment':
        case 'comments':
            switch ($tag['attributes']['value']) {
                case 'id':
                    $out = $item->id;
                    break;
                case 'avatar':
                    $size = '48';
                    $extra = '';
                    if (!empty($tag['attributes']['size'])) {
                        $size = intval($tag['attributes']['size']);
                    }
                    if (!empty($tag['attributes']['border'])) {
                        $extra .= '&border=' . $tag['attributes']['border'];
                    }
                    if (!empty($item->avatar)) {
                        $out = '<img class="' . $tag['attributes']['class'] . '" src="' . NVWEB_OBJECT . '?type=image' . $extra . '&id=' . $item->avatar . '" width="' . $size . 'px" height="' . $size . 'px"/>';
                    } else {
                        if (!empty($tag['attributes']['default'])) {
                            // the comment creator has not an avatar, but the template wants to show a default one
                            // 3 cases:
                            //  numerical   ->  ID of the avatar image file in Navigate CMS
                            //  absolute path (http://www...)
                            //  relative path (/img/avatar.png) -> path to the avatar file included in the THEME used
                            if (is_numeric($tag['attributes']['default'])) {
                                $out = '<img class="' . $tag['attributes']['class'] . '" src="' . NVWEB_OBJECT . '?type=image' . $extra . '&id=' . $tag['attributes']['default'] . '" width="' . $size . 'px" height="' . $size . 'px"/>';
                            } else {
                                if (strpos($tag['attributes']['default'], 'http://') === 0) {
                                    $out = '<img class="' . $tag['attributes']['class'] . '" src="' . $tag['attributes']['default'] . '" width="' . $size . 'px" height="' . $size . 'px"/>';
                                } else {
                                    if ($tag['attributes']['default'] == 'none') {
                                        $out = '';
                                    } else {
                                        $out = '<img class="' . $tag['attributes']['class'] . '"src="' . NAVIGATE_URL . '/themes/' . $website->theme . '/' . $tag['attributes']['default'] . '" width="' . $size . 'px" height="' . $size . 'px"/>';
                                    }
                                }
                            }
                        } else {
                            $gravatar_hash = "";
                            $gravatar_default = 'blank';
                            if (!empty($tag['attributes']['gravatar_default'])) {
                                $gravatar_default = $tag['attributes']['gravatar_default'];
                            }
                            if (!empty($item->email)) {
                                $gravatar_hash = md5(strtolower(trim($item->email)));
                            } else {
                                if (!empty($item->user)) {
                                    $email = $DB->query_single('email', 'nv_webusers', 'id = ' . protect($item->user));
                                    if (!empty($email)) {
                                        $gravatar_hash = md5(strtolower(trim($item->email)));
                                    }
                                }
                            }
                            if (!empty($gravatar_hash) && $gravatar_default != 'none') {
                                // gravatar real url: https://www.gravatar.com/avatar/
                                // we use libravatar to get more userbase
                                $gravatar_url = 'https://seccdn.libravatar.org/avatar/' . $gravatar_hash . '?s=' . $size . '&d=' . $gravatar_default;
                                $out = '<img class="' . $tag['attributes']['class'] . '" src="' . $gravatar_url . '" width="' . $size . 'px" height="' . $size . 'px"/>';
                            } else {
                                $out = '<img class="' . $tag['attributes']['class'] . '" src="data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" width="' . $size . 'px" height="' . $size . 'px"/>';
                            }
                        }
                    }
                    if ($tag['attributes']['linked'] == 'true' && !empty($out)) {
                        if (!empty($item->url)) {
                            $comment_link = $item->url;
                        } else {
                            if (!empty($item->user)) {
                                $wu = new webuser();
                                $wu->load($item->user);
                                $comment_link = $wu->social_website;
                            }
                        }
                        if (!empty($comment_link)) {
                            $out = '<a href="' . $comment_link . '" target="_blank">' . $out . '</a>';
                        }
                    }
                    break;
                case 'username':
                    $out = !empty($item->username) ? $item->username : $item->name;
                    if ($tag['attributes']['linked'] == 'true' && !empty($out)) {
                        if (!empty($item->url)) {
                            $comment_link = $item->url;
                        } else {
                            if (!empty($item->user)) {
                                $wu = new webuser();
                                $wu->load($item->user);
                                $comment_link = $wu->social_website;
                            }
                        }
                        if (!empty($comment_link)) {
                            $out = '<a href="' . $comment_link . '" target="_blank">' . $out . '</a>';
                        }
                    }
                    break;
                case 'website':
                    if (!empty($item->url)) {
                        $out = $item->url;
                    } else {
                        if (!empty($item->user)) {
                            $wu = new webuser();
                            $wu->load($item->user);
                            $out = $wu->social_website;
                        }
                    }
                    if (empty($out)) {
                        $out = '#';
                    }
                    break;
                case 'message':
                    if (!empty($tag['attributes']['length'])) {
                        $out = core_string_cut($item->message, $tag['attributes']['length'], '&hellip;');
                    } else {
                        $out = nl2br($item->message);
                    }
                    break;
                case 'date':
                    // Navigate CMS 1.6.6 compatibility
                    if (empty($tag['attributes']['format']) && !empty($tag['attributes']['date_format'])) {
                        $tag['attributes']['format'] = $tag['attributes']['date_format'];
                    }
                    if (!empty($tag['attributes']['format'])) {
                        // custom date format
                        $out = nvweb_content_date_format($tag['attributes']['format'], $item->date_created);
                    } else {
                        $out = date($website->date_format . ' H:i', $item->date_created);
                    }
                    break;
                case 'item_url':
                    $out = nvweb_source_url('item', $item->item, $current['lang']);
                    break;
                case 'item_title':
                    $out = $item->item_title;
                    break;
                case 'reply_to':
                    $out = $item->reply_to;
                    break;
                case 'depth':
                    $c = new comment();
                    $c->load_from_resultset(array($item));
                    $out = $c->depth();
                    break;
                case 'property':
                    $c = new comment();
                    $c->load_from_resultset(array($item));
                    // pass all nvlist tag parameters to properties nvweb, but some attribute/values take preference
                    $nvweb_properties_parameters = array_replace($tag['attributes'], array('mode' => 'comment', 'id' => $c->id, 'template' => $c->element_template(), 'property' => !empty($tag['attributes']['property']) ? $tag['attributes']['property'] : $tag['attributes']['name']));
                    $out = nvweb_properties($nvweb_properties_parameters);
                    break;
            }
            break;
        case 'block':
            switch ($tag['attributes']['value']) {
                case 'id':
                    $out = $item->id;
                    break;
                    // only for blocks in a block group!
                // only for blocks in a block group!
                case 'uid':
                    $out = $item->uid;
                    break;
                case 'block':
                    // generate the full block code
                    if ($item->type == "extension") {
                        if (function_exists('nvweb_' . $item->extension . '_' . $item->id)) {
                            // load extension block property values
                            $item->properties = property::load_properties(NULL, $item->id, "extension_block", NULL, $item->uid);
                            $out = call_user_func('nvweb_' . $item->extension . '_' . $item->id, $item);
                        }
                    } else {
                        $out = nvweb_blocks_render($item->type, $item->trigger, $item->action, NULL, NULL, $tag['attributes']);
                    }
                    break;
                    // not for extension_blocks
                // not for extension_blocks
                case 'title':
                    $out = $item->dictionary[$current['lang']]['title'];
                    if (!empty($tag['attributes']['length'])) {
                        $out = core_string_cut($out, $tag['attributes']['length'], '&hellip;');
                    }
                    break;
                case 'content':
                    if ($item->type == "extension") {
                        if (function_exists('nvweb_' . $item->extension . '_' . $item->id)) {
                            // load extension block property values
                            $item->properties = property::load_properties(NULL, $item->id, "extension_block", NULL, $item->uid);
                            $out = call_user_func('nvweb_' . $item->extension . '_' . $item->id, $item);
                        }
                    } else {
                        $out = nvweb_blocks_render($item->type, $item->trigger, $item->action, 'content', $item, $tag['attributes']);
                    }
                    break;
                    // not for extension_blocks
                // not for extension_blocks
                case 'url':
                case 'path':
                    $out = nvweb_blocks_render_action($item->action, '', $current['lang'], true);
                    if (empty($out)) {
                        $out = '#';
                    } else {
                        $out = nvweb_prepare_link($out);
                    }
                    break;
                    // not for extension_blocks
                // not for extension_blocks
                case 'target':
                    if ($item->action['action-type'][$current['lang']] == 'web-n') {
                        $out = '_blank';
                    } else {
                        $out = '_self';
                    }
                    break;
                    // not for extension_blocks (only for standard blocks and block group blocks)
                // not for extension_blocks (only for standard blocks and block group blocks)
                case 'property':
                    $properties_mode = 'block';
                    if (!is_numeric($item->id)) {
                        $properties_mode = 'block_group_block';
                    }
                    $nvweb_properties_parameters = array_replace($tag['attributes'], array('mode' => !isset($tag['attributes']['mode']) ? $properties_mode : $tag['attributes']['mode'], 'id' => $item->id, 'property' => !empty($tag['attributes']['property']) ? $tag['attributes']['property'] : $tag['attributes']['name'], 'uid' => @$item->uid));
                    $out = nvweb_properties($nvweb_properties_parameters);
                    break;
                    // not for extension_blocks
                // not for extension_blocks
                case 'poll_answers':
                    $out = nvweb_blocks_render_poll($item);
                    break;
                default:
                    break;
            }
            break;
        case 'block_link':
            switch ($tag['attributes']['value']) {
                case 'id':
                    $out = $item->id;
                    break;
                case 'title':
                    $out = $item->title;
                    if (!empty($tag['attributes']['length'])) {
                        $out = core_string_cut($out, $tag['attributes']['length'], '&hellip;');
                    }
                    break;
                case 'url':
                case 'path':
                    $out = $item->link;
                    if (empty($out)) {
                        $out = '#';
                    } else {
                        $out = nvweb_prepare_link($out);
                    }
                    break;
                case 'target':
                    if ($item->new_window == 1) {
                        $out = '_blank';
                    } else {
                        $out = '_self';
                    }
                    break;
                case 'icon':
                    $out = @$item->icon;
                    break;
                default:
                    break;
            }
            break;
        case 'block_type':
            switch ($tag['attributes']['value']) {
                case 'title':
                    $title_obj = json_decode($item->title, true);
                    if (empty($title_obj)) {
                        // not json
                        $out = $item->title;
                    } else {
                        $out = $title_obj[$current['lang']];
                    }
                    break;
            }
            break;
        case 'gallery':
            switch ($tag['attributes']['value']) {
                case 'url':
                case 'path':
                    $out = NVWEB_OBJECT . '?wid=' . $website->id . '&id=' . $item['file'] . '&amp;disposition=inline';
                    break;
                case 'thumbnail':
                case 'thumbnail_url':
                    $thumbnail_url = NVWEB_OBJECT . '?wid=' . $website->id . '&id=' . $item['file'] . '&amp;disposition=inline&amp;width=' . $tag['attributes']['width'] . '&amp;height=' . $tag['attributes']['height'] . '&amp;border=' . $tag['attributes']['border'];
                    if ($tag['attributes']['value'] == 'thumbnail_url' || @$tag['attributes']['return'] == 'url') {
                        $out = $thumbnail_url;
                    } else {
                        $out = '<img src="' . $thumbnail_url . '" alt="' . $item[$current['lang']] . '" title="' . $item[$current['lang']] . '" />';
                    }
                    break;
                case 'title':
                    $f = new file();
                    $f->load($item['file']);
                    $out = $f->title[$current['lang']];
                    break;
                case 'alt':
                case 'description':
                    $f = new file();
                    $f->load($item['file']);
                    $out = $f->description[$current['lang']];
                    break;
                default:
                    $out = '<a href="' . NVWEB_OBJECT . '?wid=' . $website->id . '&id=' . $item['file'] . '&amp;disposition=inline">
                                <img src="' . NVWEB_OBJECT . '?wid=' . $website->id . '&id=' . $item['file'] . '&amp;disposition=inline&amp;width=' . $tag['attributes']['width'] . '&amp;height=' . $tag['attributes']['height'] . '&amp;border=' . $tag['attributes']['border'] . '"
									 alt="' . $item[$current['lang']] . '" title="' . $item[$current['lang']] . '" />
                            </a>';
                    break;
            }
            break;
        case 'item':
            // useful also for source="structure" (but some are nonsense: title, comments, etc)
        // useful also for source="structure" (but some are nonsense: title, comments, etc)
        default:
            switch ($tag['attributes']['value']) {
                case 'id':
                    $out = $item->id;
                    break;
                case 'slug':
                    $lang = $current['lang'];
                    if (!empty($tag['attributes']['lang'])) {
                        $lang = $tag['attributes']['lang'];
                    }
                    $out = $item->dictionary[$lang]['title'];
                    // remove spaces, special chars, etc.
                    $out = core_string_clean($out);
                    $out = slug($out);
                    break;
                case 'title':
                    $lang = $current['lang'];
                    if (!empty($tag['attributes']['lang'])) {
                        $lang = $tag['attributes']['lang'];
                    }
                    $out = $item->dictionary[$lang]['title'];
                    if (!empty($tag['attributes']['length'])) {
                        $out = core_string_cut($out, $tag['attributes']['length'], '&hellip;', $tag['attributes']['length']);
                    }
                    break;
                case 'author':
                    if (!empty($item->author)) {
                        $nu = new user();
                        $nu->load($item->author);
                        $out = $nu->username;
                        unset($nu);
                    }
                    if (empty($out)) {
                        $out = $website->name;
                    }
                    break;
                case 'date':
                case 'date_post':
                    if (!empty($tag['attributes']['format'])) {
                        // custom date format
                        $out = nvweb_content_date_format($tag['attributes']['format'], $item->date_to_display);
                    } else {
                        $out = date($website->date_format, $item->date_to_display);
                    }
                    break;
                case 'content':
                case 'section':
                    if ($source == 'structure' && $tag['attributes']['source'] == 'item') {
                        $items = nvweb_content_items($item->id, true, 1, false, 'priority');
                        // we force finding the first non-embedded item ordered by priority
                        if (empty($items)) {
                            $items = nvweb_content_items($item->id, true, 1, true, 'priority');
                        }
                        // find the first embedded item ordered by priority
                        $item = $items[0];
                    }
                    $section = $tag['attributes']['section'];
                    if (empty($section)) {
                        $section = 'main';
                    }
                    $out = $item->dictionary[$current['lang']]['section-' . $section];
                    if (!empty($tag['attributes']['length'])) {
                        $allowed_tags = '';
                        if (!empty($tag['attributes']['allowed_tags'])) {
                            $allowed_tags = explode(',', $tag['attributes']['allowed_tags']);
                        }
                        $out = core_string_cut($out, $tag['attributes']['length'], '&hellip;', $allowed_tags);
                    }
                    break;
                case 'comments':
                    $out = nvweb_content_comments_count($item->id);
                    break;
                case 'gallery':
                    $params = array('item' => $item->id);
                    $params = array_merge($params, $tag['attributes']);
                    $out = nvweb_gallery($params);
                    break;
                case 'image':
                case 'photo':
                    $photo = @array_shift(array_keys($item->galleries[0]));
                    if (empty($photo)) {
                        $out = NVWEB_OBJECT . '?type=transparent';
                    } else {
                        $out = NVWEB_OBJECT . '?wid=' . $website->id . '&id=' . $photo . '&amp;disposition=inline&amp;width=' . $tag['attributes']['width'] . '&amp;height=' . $tag['attributes']['height'] . '&amp;border=' . $tag['attributes']['border'];
                    }
                    break;
                case 'url':
                case 'path':
                    // rss -> full url
                    // item -> relative url
                    // embedded item -> category url
                    if ($item->embedding == 1 && $item->association == 'category') {
                        nvweb_menu_load_routes();
                        // load menu paths if not already done
                        $out = nvweb_prepare_link($structure['routes'][$item->category]);
                    } else {
                        $path = $item->paths[$current['lang']];
                        if (empty($path)) {
                            $path = '/node/' . $item->id;
                        }
                        $out = nvweb_prepare_link($path);
                    }
                    break;
                case 'tags':
                    // pass all nvlist tag parameters to the content nvweb, but some attribute/values take preference
                    $nvweb_parameters = array_replace($tag['attributes'], array('mode' => 'tags', 'id' => $item->id));
                    $out = nvweb_content($nvweb_parameters);
                    break;
                case 'score':
                    $out = nvweb_votes_calc($item, $tag['attributes']['round'], $tag['attributes']['half'], $tag['attributes']['min'], $tag['attributes']['max']);
                    break;
                case 'votes':
                    $out = intval($item->votes);
                    break;
                case 'views':
                    $out = intval($item->views);
                    break;
                case 'property':
                    if ($source == 'structure' && $tag['attributes']['source'] == 'item') {
                        $items = nvweb_content_items($item->id, true, 1, false, 'priority');
                        // we force finding the first non-embedded item ordered by priority
                        if (empty($items)) {
                            $items = nvweb_content_items($item->id, true, 1, true, 'priority');
                        }
                        // find the first embedded item ordered by priority
                        $item = $items[0];
                        $source = "item";
                    }
                    // pass all nvlist tag parameters to properties nvweb, but some attribute/values take preference
                    $nvweb_properties_parameters = array_replace($tag['attributes'], array('mode' => $source == 'structure' || $source == 'category' ? 'structure' : 'item', 'id' => $item->id, 'template' => $item->template, 'property' => !empty($tag['attributes']['property']) ? $tag['attributes']['property'] : $tag['attributes']['name']));
                    $out = nvweb_properties($nvweb_properties_parameters);
                    break;
                default:
                    // maybe a special tag not related to a source? (unimplemented)
            }
            break;
    }
    return $out;
}
示例#4
0
function nvweb_conditional($vars = array())
{
    global $website;
    global $DB;
    global $current;
    global $cache;
    global $structure;
    global $webgets;
    global $webuser;
    $out = array();
    $webget = 'conditional';
    $categories = array();
    $item = new item();
    if ($current['type'] == 'item') {
        $item->load($current['object']->id);
        $item_type = 'element';
    } else {
        $item_type = 'structure';
        if (isset($vars['scope']) && $vars['scope'] == 'element') {
            // the current path belongs to a structure category, but the template is asking for an element value,
            // so we try to find the first element assigned to the current category
            $categories = array();
            if (!empty($current['object']->id)) {
                $categories = array($current['object']->id);
            }
            if (isset($vars['categories'])) {
                $categories = explode(',', $vars['categories']);
                $categories = array_filter($categories);
                // remove empty elements
            }
            $permission = !empty($_SESSION['APP_USER#' . APP_UNIQUE]) ? 1 : 0;
            // public access / webuser based / webuser groups based
            $access = 2;
            $access_extra = '';
            if (!empty($current['webuser'])) {
                $access = 1;
                if (!empty($webuser->groups)) {
                    $access_groups = array();
                    foreach ($webuser->groups as $wg) {
                        if (empty($wg)) {
                            continue;
                        }
                        $access_groups[] = 's.groups LIKE "%g' . $wg . '%"';
                    }
                    if (!empty($access_groups)) {
                        $access_extra = ' OR (s.access = 3 AND (' . implode(' OR ', $access_groups) . '))';
                    }
                }
            }
            // get order type: PARAMETER > NV TAG PROPERTY > DEFAULT (priority given in CMS)
            $order = @$_REQUEST['order'];
            if (empty($order)) {
                $order = @$vars['order'];
            }
            if (empty($order)) {
                // default order: latest
                $order = 'latest';
            }
            $orderby = nvweb_list_get_orderby($order);
            $rs = NULL;
            $access_extra_items = str_replace('s.', 'i.', $access_extra);
            if (empty($categories)) {
                // force executing the query; search in all categories
                $categories = nvweb_menu_get_children(array(0));
            }
            // default source for retrieving items
            $DB->query('
                SELECT SQL_CALC_FOUND_ROWS i.id, i.permission, i.date_published, i.date_unpublish,
                       i.date_to_display, COALESCE(NULLIF(i.date_to_display, 0), i.date_created) as pdate,
                       d.text as title, i.position as position
                  FROM nv_items i, nv_structure s, nv_webdictionary d
                 WHERE i.category IN(' . implode(",", $categories) . ')
                   AND i.website = ' . $website->id . '
                   AND i.permission <= ' . $permission . '
                   AND (i.date_published = 0 OR i.date_published < ' . core_time() . ')
                   AND (i.date_unpublish = 0 OR i.date_unpublish > ' . core_time() . ')
                   AND s.id = i.category
                   AND (s.date_published = 0 OR s.date_published < ' . core_time() . ')
                   AND (s.date_unpublish = 0 OR s.date_unpublish > ' . core_time() . ')
                   AND s.permission <= ' . $permission . '
                   AND (s.access = 0 OR s.access = ' . $access . $access_extra . ')
                   AND (i.access = 0 OR i.access = ' . $access . $access_extra_items . ')
                   AND d.website = i.website
                   AND d.node_type = "item"
                   AND d.subtype = "title"
                   AND d.node_id = i.id
                   AND d.lang = ' . protect($current['lang']) . '
                 ' . $orderby . '
                 LIMIT 1
                 OFFSET 0');
            $rs = $DB->result();
            // now we have the element against which the condition will be checked
            $i = 0;
            $item->load($rs[$i]->id);
            $item_type = 'element';
        } else {
            if (!isset($vars['scope']) || $vars['scope'] == 'structure') {
                $item = $current['object'];
                $item_type = 'structure';
            }
        }
    }
    // get the template
    $item_html = $vars['_template'];
    // now, parse the conditional tags (with html source code inside)
    switch ($vars['by']) {
        case 'property':
            $property_value = NULL;
            $property_name = $vars['property_name'];
            if (empty($vars['property_name'])) {
                $property_name = $vars['property_id'];
            }
            if ($vars['property_scope'] == "element") {
                $property_value = $item->property($property_name);
            } else {
                if ($vars['property_scope'] == "structure") {
                    $property = nvweb_properties(array('mode' => 'structure', 'property' => $property_name, 'return' => 'object'));
                    if (!empty($property)) {
                        $property_value = $property->value;
                    }
                } else {
                    if ($vars['property_scope'] == "website") {
                        $property_value = $website->theme_options->{$property_name};
                    } else {
                        // no scope defined, so we have to check ELEMENT > STRUCTURE > WEBSITE (the first with a property with the given name)
                        // element
                        $property_value = $item->property($property_name);
                        if (!$item->property_exists($property_name) && $item_type == 'structure') {
                            // get the first embedded element and check find the property
                            $ci = nvweb_content_items(array($item->id), true, 1, true, 'priority');
                            $item = new item();
                            if (isset($ci[0])) {
                                $item->load($ci[0]->id);
                                $property_value = $item->property($property_name);
                            }
                        }
                        if (!$item->property_exists($property_name)) {
                            // structure
                            $property = nvweb_properties(array('mode' => 'structure', 'property' => $property_name, 'return' => 'object'));
                            if (!empty($property)) {
                                $property_value = $property->value;
                            } else {
                                // website
                                if (isset($website->theme_options->{$property_name})) {
                                    $property_value = $website->theme_options->{$property_name};
                                } else {
                                    $property_value = '';
                                }
                            }
                        }
                    }
                }
            }
            // if the property is multilanguage, get the value for the current language
            if (is_array($property_value)) {
                $property_value = $property_value[$current['lang']];
            }
            // check the given condition
            if (isset($vars['empty']) || isset($vars['property_empty'])) {
                if (@$vars['empty'] == 'true' || @$vars['property_empty'] == 'true') {
                    if (empty($property_value)) {
                        $out = $item_html;
                    } else {
                        $out = '';
                    }
                } else {
                    if (@$vars['empty'] == 'false' || @$vars['property_empty'] == 'false') {
                        if (!empty($property_value)) {
                            $out = $item_html;
                        } else {
                            $out = '';
                        }
                    }
                }
            } else {
                if (isset($vars['property_value'])) {
                    $condition_value = $vars['property_value'];
                    switch ($vars['property_compare']) {
                        case '>':
                        case 'gt':
                            $condition = $property_value > $condition_value;
                            break;
                        case '<':
                        case 'lt':
                            $condition = $property_value < $condition_value;
                            break;
                        case '>=':
                        case '=>':
                        case 'gte':
                            $condition = $property_value >= $condition_value;
                            break;
                        case '<=':
                        case '=<':
                        case 'lte':
                            $condition = $property_value <= $condition_value;
                            break;
                        case 'in':
                            $condition_values = explode(",", $condition_value);
                            $condition = in_array($property_value, $condition_values);
                            break;
                        case 'nin':
                            $condition_values = explode(",", $condition_value);
                            $condition = !in_array($property_value, $condition_values);
                            break;
                        case '!=':
                        case 'neq':
                            if (is_numeric($property_value)) {
                                if ($condition_value == 'true' || $condition_value === true) {
                                    $condition_value = '1';
                                } else {
                                    if ($condition_value == 'false' || $condition_value === false) {
                                        $condition_value = '0';
                                    }
                                }
                            }
                            $condition = $property_value != $condition_value;
                            break;
                        case '=':
                        case '==':
                        case 'eq':
                        default:
                            if (is_numeric($property_value)) {
                                if ($condition_value == 'true' || $condition_value === true) {
                                    $condition_value = '1';
                                } else {
                                    if ($condition_value == 'false' || $condition_value === false) {
                                        $condition_value = '0';
                                    }
                                }
                            }
                            $condition = $property_value == $condition_value;
                            break;
                    }
                    if ($condition) {
                        $out = $item_html;
                    } else {
                        $out = '';
                    }
                }
            }
            break;
        case 'template':
        case 'templates':
            $templates = array();
            if (isset($vars['templates'])) {
                $templates = explode(",", $vars['templates']);
            } else {
                if (isset($vars['template'])) {
                    $templates = array($vars['template']);
                }
            }
            if (in_array($item->template, $templates)) {
                $out = $item_html;
            } else {
                $out = '';
            }
            break;
        case 'section':
            $section_empty = empty($item->dictionary[$current['lang']]['section-' . $vars['section']]);
            if ($vars['empty'] == 'true' && $section_empty || $vars['empty'] == 'false' && !$section_empty) {
                $out = $item_html;
            } else {
                $out = '';
            }
            break;
        case 'access':
            $access = 0;
            switch ($vars['access']) {
                case 3:
                case 'webuser_groups':
                    $access = 3;
                    break;
                case 2:
                case 'not_signed_in':
                    $access = 2;
                    break;
                case 1:
                case 'signed_in':
                    $access = 1;
                    break;
                case 0:
                case 'everyone':
                default:
                    $access = 0;
                    break;
            }
            if ($item->access == $access) {
                $out = $item_html;
            } else {
                $out = '';
            }
            break;
        case 'webuser':
            if ($vars['signed_in'] == 'true' && !empty($webuser->id)) {
                $out = $item_html;
            } else {
                if ($vars['signed_in'] == 'false' && empty($webuser->id)) {
                    $out = $item_html;
                } else {
                    $out = '';
                }
            }
            break;
        case 'languages':
            if (count($website->languages_published) >= $vars['min']) {
                $out = $item_html;
            } else {
                if (count($website->languages_published) <= $vars['max']) {
                    $out = $item_html;
                }
            }
            break;
        case 'language':
            if ($current['lang'] == $vars['lang']) {
                $out = $item_html;
            }
            break;
        case 'gallery':
            if ($vars['empty'] == 'true') {
                if (empty($item->galleries[0])) {
                    $out = $item_html;
                }
            } else {
                if ($vars['empty'] == 'false') {
                    if (!empty($item->galleries[0])) {
                        $out = $item_html;
                    }
                } else {
                    if (isset($vars['min']) && count($item->galleries[0]) >= intval($vars['min'])) {
                        $out = $item_html;
                    } else {
                        if (isset($vars['max']) && count($item->galleries[0]) <= intval($vars['max'])) {
                            $out = $item_html;
                        }
                    }
                }
            }
            break;
        case 'tags':
            if ($vars['empty'] == 'true') {
                if (empty($item->dictionary[$current['lang']]['tags'])) {
                    $out = $item_html;
                }
            } else {
                if ($vars['empty'] == 'false') {
                    if (!empty($item->dictionary[$current['lang']]['tags'])) {
                        $out = $item_html;
                    }
                }
            }
            break;
        case 'comments':
            $DB->query('
                SELECT COUNT(*) as total
				  FROM nv_comments
				 WHERE website = ' . protect($website->id) . '
				   AND item = ' . protect($item->id) . '
				   AND status = 0
            ');
            $rs = $DB->result();
            $comments_count = $rs[0]->total + 0;
            if (isset($vars['allowed'])) {
                if ($vars['allowed'] == 'true' || $vars['allowed'] == '1' || empty($vars['allowed'])) {
                    // comments allowed to everybody (2) or to registered users only (1)
                    if ($item->comments_enabled_to == 2 || $item->comments_enabled_to == 1 && !empty($webuser->id)) {
                        $out = $item_html;
                    }
                } else {
                    if ($vars['allowed'] == 'false') {
                        // comments not allowed for anyone or for webusers but there is no webuser active right now
                        if ($item->comments_enabled_to == 0 || $item->comments_enabled_to == 1 && empty($webuser->id)) {
                            $out = $item_html;
                        }
                    }
                }
            } else {
                if (isset($vars['min']) && $comments_count >= intval($vars['min'])) {
                    $out = $item_html;
                } else {
                    if (isset($vars['max']) && $comments_count <= intval($vars['max'])) {
                        $out = $item_html;
                    }
                }
            }
            break;
        default:
            // unknown nvlist_conditional, discard
            $out = '';
    }
    // return the new html code after applying the condition
    return $out;
}