public static function save_properties_from_array($object_type, $object_id, $template, $properties_assoc = array(), $ws = null, $node_uid = "")
    {
        global $DB;
        global $website;
        global $theme;
        if (empty($ws)) {
            $ws = $website;
        }
        $dictionary = array();
        $property_object_type = $object_type;
        // object_type: item, structure, block, block_group_block
        if ($object_type == 'block_group_block') {
            // we have to identify the block subtype: block, block_type, block_group_block or extension_block
            // so we can get its properties definition
            if (!is_numeric($object_id)) {
                // assume there can only be one block group of the same type
                $block_group_id = $DB->query_single('MAX(id)', 'nv_block_groups', ' code = ' . protect($template) . ' AND website = ' . $ws->id);
                $object_id = $block_group_id;
                if (empty($block_group_id)) {
                    $object_id = 0;
                }
            }
            if (!empty($node_uid)) {
                $bg = new block_group();
                $bg->load($object_id);
                for ($b = 0; $b < count($bg->blocks); $b++) {
                    if ($bg->blocks[$b]['uid'] == $node_uid) {
                        $block_id = $bg->blocks[$b]['id'];
                        if ($bg->blocks[$b]['type'] == 'extension') {
                            // an extension block
                            $property_object_type = 'block_group-extension-block';
                            // load the extension, if installed in this instance
                            $extension = new extension();
                            $extension->load($bg->blocks[$b]['extension']);
                            // find the property declaration in the extension definition
                            if (isset($extension->definition->blocks)) {
                                for ($eb = 0; $eb < count($extension->definition->blocks); $eb++) {
                                    if ($extension->definition->blocks[$eb]->id == $block_id) {
                                        $block = $extension->definition->blocks[$eb];
                                        break;
                                    }
                                }
                            } else {
                                // ignore this property, extension is not installed or it does not have the requested block definition
                                continue;
                            }
                        } else {
                            // a block group block
                            $property_object_type = 'block_group_block';
                            $block = block::block_group_block($template, $block_id);
                        }
                        // note: standard blocks don't "embed" their properties in a block group,
                        // they have their own separate values
                        break;
                    }
                }
            } else {
                // compatibility with < Navigate 2.1 themes (to be removed)
                $properties_names = array_keys($properties_assoc);
                $block = block::block_group_block_by_property($properties_names[0]);
            }
            if (!isset($block->properties) || empty($block->properties)) {
                return false;
            }
            $properties = $block->properties;
        } else {
            $properties = property::elements($template, $object_type);
        }
        if (!is_array($properties)) {
            $properties = array();
        }
        foreach ($properties as $property) {
            if (!isset($properties_assoc[$property->name]) && !isset($properties_assoc[$property->id])) {
                continue;
            }
            $values_dict = array();
            $value = '';
            // we try to find the property value by "property name", if empty then we try to find it via "property id"
            if (isset($properties_assoc[$property->name])) {
                $value = $properties_assoc[$property->name];
            }
            if (empty($value)) {
                $value = $properties_assoc[$property->id];
            }
            // multilanguage property?
            if (in_array($property->type, array('text', 'textarea', 'link', 'rich_textarea')) || @$property->multilanguage == 'true' || @$property->multilanguage === true) {
                if (isset($properties_assoc[$property->name])) {
                    $values_dict = $properties_assoc[$property->name];
                }
                if (empty($values_dict)) {
                    $values_dict = $properties_assoc[$property->id];
                }
                $value = '[dictionary]';
            }
            if ($property->type == 'coordinates') {
                if (is_array($value)) {
                    $value = $value['latitude'] . '#' . $value['longitude'];
                }
                // if it isn't an array, then we suppose it already has the right format
            }
            // property->type "decimal"; we don't need to reconvert, it should already be in the right format
            if ($property->type == 'webuser_groups' && !empty($value)) {
                $value = 'g' . implode(',g', $value);
            }
            // boolean (checkbox): if not checked,  form does not send the value
            if ($property->type == 'boolean' && empty($value)) {
                $value = 0;
            }
            if (is_null($value)) {
                $value = "";
            }
            // should not be needed because of value_or_default, but doing this here fixes some warnings
            // remove the old property value row
            $DB->execute('
				DELETE FROM nv_properties_items
                	  WHERE property_id = ' . protect($property->id) . '
                        AND element = ' . protect($property_object_type) . '
                        AND node_id = ' . protect($object_id) . (empty($node_uid) ? '' : ' AND node_uid = ' . protect($node_uid)) . '
                        AND website = ' . $ws->id);
            // now we insert a new row
            $DB->execute('
			    INSERT INTO nv_properties_items
				    (id, website, property_id, element, node_id, node_uid, name, value)
				VALUES
				    (   0,
						:website,
						:property_id,
						:type,
						:object_id,
						:node_uid,
						:name,
						:value
                    )', array(':website' => $ws->id, ':property_id' => $property->id, ':type' => $property_object_type, ':object_id' => value_or_default($object_id, 0), ':node_uid' => value_or_default($node_uid, ""), ':name' => $property->name, ':value' => value_or_default($value, "")));
            // $error = $DB->get_last_error();
            // set the dictionary for the multilanguage properties
            $default_language = '';
            if (isset($property->multilanguage) && ($property->multilanguage === 'false' || $property->multilanguage === false)) {
                $default_language = $ws->languages_list[0];
            }
            if (in_array($property->type, array('text', 'textarea', 'rich_textarea', 'link')) || @$property->multilanguage == 'true' || @$property->multilanguage === true) {
                foreach ($ws->languages_list as $lang) {
                    if (!empty($default_language)) {
                        // property is NOT multilanguage, use the first value for all languages
                        $dictionary[$lang]['property-' . $property->id . '-' . $lang] = $values_dict[$default_language];
                    } else {
                        $dictionary[$lang]['property-' . $property->id . '-' . $lang] = $values_dict[$lang];
                    }
                }
            }
        }
        if (!empty($dictionary)) {
            webdictionary::save_element_strings('property-' . $property_object_type, $object_id, $dictionary, $ws->id, $node_uid);
        }
        return true;
    }
示例#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;
}