コード例 #1
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;
}