function smarty_function_init_objects($params, &$smarty)
{
    global $site, $leht, $template;
    $content_template =& $leht->content_template;
    $objects_arr = array();
    ##############
    # default values
    extract($params);
    if (!isset($parent_system_alias) && !isset($parent)) {
        $parent_id = $leht->id;
    } elseif (isset($parent_system_alias)) {
        //$parent_id = $site->alias(array('key' => $parent_system_alias));
        $parent_id = $site->alias(array('key' => $parent_system_alias, 'keel' => $site->keel));
    } elseif (isset($parent)) {
        $parent_id = $parent;
    }
    // if parent_id not found
    if (!$parent_id) {
        $parent_id = $leht->id;
    }
    if (!isset($name)) {
        $name = "objects";
    }
    $classes = trim($classes);
    switch ($on_create) {
        case "publish":
            $publish = 1;
            break;
        case "hide":
            $publish = 0;
            break;
        default:
            $publish = 0;
    }
    ###############
    # action-buttons
    # by default show all
    if (!isset($buttons)) {
        $buttons = array("new", "edit", "hide", "move", "delete");
    } else {
        $buttons = split(",", $buttons);
    }
    # for language compatibility, replace with search string existing db field name
    $order = preg_replace('#\\btitle\\b#i', "pealkiri", $order);
    $order = preg_replace('#\\bdate\\b#i', "aeg", $order);
    ##################
    # classes
    if ($classes) {
        ######### translate classes: change class values for language compability
        $transl_class_arr = array();
        foreach (split(",", $classes) as $class) {
            if (trim($class) != '') {
                $transl_class_arr[] = translate_ee($class);
                # translate it to estonian
            }
        }
        #		echo printr($transl_class_arr);
        $classes = join(",", $transl_class_arr);
        ######## gather tyyp ID values => to array
        $tyyp_id_arr = array();
        $sql = "SELECT tyyp_id, klass FROM tyyp";
        $sth = new SQL($sql);
        $site->debug->msg($sth->debug->get_msgs());
        while ($tmp = $sth->fetch()) {
            # if ID found in classes array, then add it:
            if (in_array($tmp['klass'], $transl_class_arr)) {
                $tyyp_id_arr[] = $tmp['tyyp_id'];
            }
        }
        #		echo printr($tyyp_id_arr);
        # tyyp_idlist ID numeric values for buttons:
        $tyyp_idlist = join(",", $tyyp_id_arr);
    }
    # if classes parameter provided
    # / classes
    ##################
    ##############
    # alamlist
    $alamlistSQL = new AlamlistSQL(array(parent => $parent_id, klass => $classes, asukoht => $position, order => $order));
    if ($select) {
        $alamlistSQL->add_select($select);
    }
    if ($where) {
        $alamlistSQL->add_where($where);
    }
    if ($group) {
        $alamlistSQL->add_group($site->db->prepare('group by ' . $group));
    }
    $alamlist = new Alamlist(array('alamlistSQL' => $alamlistSQL, start => $start, limit => $limit));
    $alamlist->debug->print_msg();
    # if parameter "limit" is provided then "counttotal" element is needed (shows total rows)
    if (isset($limit)) {
        $alamlist_count = new Alamlist(array(parent => $parent_id, klass => $classes, asukoht => $position, on_counter => 1));
    }
    ##############
    # load variables
    $new_button = $alamlist->get_edit_buttons(array(tyyp_idlist => $tyyp_idlist, publish => $publish));
    while ($obj = $alamlist->next()) {
        ################
        # object parameters
        $obj->id = $obj->objekt_id;
        # kui link
        if ($obj->all[klass] == "link") {
            # load sisu, et saada vļæ½ļæ½rtused "url" ja "on_uusaken"
            $obj->load_sisu();
            $objektUrl = $obj->all['url'];
            // replace index.php?id=xxx or ?id=xxx style local url with its alias
            if (!$site->in_editor && $site->CONF['use_aliases'] && $site->CONF['replace_links_with_alias']) {
                $objektUrl = convert_local_link_to_alias($objektUrl);
            }
            $objektUrl && $obj->all['on_uusaken'] ? $obj->href = $objektUrl . '" target="_blank' : ($obj->href = $objektUrl);
        } else {
            $obj->get_object_href();
        }
        $obj->is_selected = $leht->parents->on_parent($obj->objekt_id);
        $obj->title = $obj->pealkiri;
        $obj->buttons = $obj->get_edit_buttons(array(nupud => $buttons, tyyp_idlist => $tyyp_idlist, publish => $publish));
        $obj->fdate = $obj->all[aeg];
        $obj->last_modified = date('Y', $obj->all['last_modified']) > 1970 ? date('d.m.Y H:i', $obj->all['last_modified']) : '';
        ## crap data
        $obj->flast_modified = $obj->all['last_modified'];
        $obj->author = $obj->all[author];
        $obj->class = translate_en($obj->all[klass]);
        # translate it to english
        $obj->details_link = $obj->href;
        $obj->details_title = $site->sys_sona(array(sona => "loe edasi", tyyp => "kujundus"));
        $obj->printgif = '<a href="' . $obj->href . '&op=print" onClick="avaprintaken(this.href, 600, 400, \'print\'); return false;" target=_blank><img src="' . $site->img_path . '/print_it.gif" border=0 width=19 height=18></a>';
        $obj->printlink = $site->self . '?id=' . $obj->objekt_id . '&op=print';
        $obj->created_user_id = $obj->all['created_user_id'];
        $obj->created_user_name = $obj->all['created_user_name'];
        $obj->changed_user_id = $obj->all['changed_user_id'];
        $obj->changed_user_name = $obj->all['changed_user_name'];
        $obj->created_time = $site->db->MySQL_ee($obj->all['created_time']);
        $obj->fcreated_time = $obj->all['created_time'];
        $obj->changed_time = $site->db->MySQL_ee($obj->all['changed_time']);
        $obj->fchanged_time = $obj->all['changed_time'];
        $obj->last_commented_time = $site->db->MySQL_ee($obj->all['last_commented_time']);
        $obj->comment_count = $obj->all['comment_count'];
        ###############
        # push array, in case we don't have "peida menuus" turned on (section objects)
        if (!$obj->all[is_hided_in_menu] || $site->in_editor) {
            array_push($objects_arr, $obj);
        }
    }
    $count = sizeof($objects_arr);
    $counttotal = isset($limit) ? $alamlist_count->rows : $count;
    ##############
    # assign to template variables
    $smarty->assign(array($name => $objects_arr, $name . '_newbutton' => $new_button, $name . '_counttotal' => $counttotal, $name . '_count' => $count));
}
function smarty_function_init_section($params, &$smarty)
{
    global $site, $leht, $template;
    $content_template =& $leht->content_template;
    $section = array();
    ##############
    # default values
    extract($params);
    if (!isset($level) && !isset($parent)) {
        $parent_id = $leht->id;
    } elseif (isset($level) && !isset($parent)) {
        $level = 0 - $level;
        /* put '-' at the beginning */
        $tmp = $leht->parents->get($level);
        $parent_id = $tmp->objekt_id;
    } elseif (isset($parent)) {
        $parent_id = $parent;
    }
    if (!isset($name)) {
        $name = "section";
    }
    switch ($on_create) {
        case "publish":
            $publish = 1;
            break;
        case "hide":
            $publish = 0;
            break;
        default:
            $publish = 0;
    }
    if (!$parent_id) {
        $smarty->assign(array($name => $section, $name . '_newbutton' => '', $name . '_counttotal' => 0, $name . '_rows' => 0, $name . '_count' => 0));
        return;
    }
    ###############
    # action-buttons
    # by default show all
    if (!isset($buttons)) {
        $buttons = array("new", "edit", "hide", "move", "delete");
    } else {
        $buttons = split(",", $buttons);
    }
    if (!isset($classes)) {
        $classes = "section";
    }
    # for language compatibility, replace with search string existing db field name
    $order = preg_replace('#\\btitle\\b#i', "pealkiri", $order);
    $order = preg_replace('#\\bdate\\b#i', "aeg", $order);
    ################
    # position
    # default values for position
    if (!isset($position)) {
        $position = 0;
    }
    ##############
    # alamlist
    # change class values for language compability:
    $tyyp_idlist = $classes;
    $classes = str_replace("section", translate_ee("section"), $classes);
    # tyyp_id = 1
    $classes = str_replace("link", translate_ee("link"), $classes);
    # tyyp_id = 3
    # strip out all spaces
    $classes = preg_replace("/(\\s)*/", "", $classes);
    # for buttons:
    $tyyp_idlist = str_replace("section", "1", $tyyp_idlist);
    # tyyp_id = 1
    $tyyp_idlist = str_replace("link", "3", $tyyp_idlist);
    # tyyp_id = 3
    $alamlist = new Alamlist(array(parent => $parent_id, klass => $classes ? $classes : "rubriik", asukoht => $position, order => $order, start => $start, limit => $limit));
    #$alamlist->debug->print_msg();
    # if parameter "limit" is provided then "counttotal" element is needed (shows total rows)
    if (isset($limit)) {
        $alamlist_count = new Alamlist(array(parent => $parent_id, klass => $classes ? $classes : "rubriik", asukoht => $position, on_counter => 1));
    }
    ##############
    # load variables
    $new_button = $alamlist->get_edit_buttons(array(tyyp_idlist => $tyyp_idlist ? $tyyp_idlist : "1", publish => $publish));
    while ($obj = $alamlist->next()) {
        ################
        # object parameters
        $obj->id = $obj->objekt_id;
        # kui rubriik:
        if ($obj->all[klass] == "rubriik") {
            $obj->get_object_href();
        } elseif ($obj->all[klass] == 'link') {
            # load sisu, et saada vļæ½ļæ½rtused "url" ja "on_uusaken"
            $obj->load_sisu();
            $objektUrl = $obj->all['url'];
            // replace index.php?id=xxx or ?id=xxx style local url with its alias
            if (!$site->in_editor && $site->CONF['use_aliases'] && $site->CONF['replace_links_with_alias']) {
                $objektUrl = convert_local_link_to_alias($objektUrl);
            }
            $objektUrl && $obj->all['on_uusaken'] ? $obj->href = $objektUrl . '" target="_blank' : ($obj->href = $objektUrl);
        }
        $obj->is_selected = $leht->parents->on_parent($obj->objekt_id);
        $obj->title .= $obj->pealkiri;
        $obj->buttons = $obj->get_edit_buttons(array(nupud => $buttons, tyyp_idlist => $tyyp_idlist ? $tyyp_idlist : "1", publish => $publish));
        $obj->last_modified = date('Y', $obj->all['last_modified']) > 1970 ? date('d.m.Y H:i', $obj->all['last_modified']) : '';
        ## crap data
        $obj->flast_modified = $obj->all['last_modified'];
        $obj->class = translate_en($obj->all[klass]);
        # translate it to english
        $obj->created_user_id = $obj->all['created_user_id'];
        $obj->created_user_name = $obj->all['created_user_name'];
        $obj->changed_user_id = $obj->all['changed_user_id'];
        $obj->changed_user_name = $obj->all['changed_user_name'];
        $obj->created_time = $site->db->MySQL_ee($obj->all['created_time']);
        $obj->fcreated_time = $obj->all['created_time'];
        $obj->changed_time = $site->db->MySQL_ee($obj->all['changed_time']);
        $obj->fchanged_time = $obj->all['changed_time'];
        $obj->last_commented_time = $site->db->MySQL_ee($obj->all['last_commented_time']);
        $obj->comment_count = $obj->all['comment_count'];
        ###############
        # push array
        # kui objektil featuur "Peida menļæ½ļæ½s" sisselļæ½litatud (NB! erinev tingimus kui avaldatus)
        # ja pole admin siis mitte lisada objekti massiivi
        if (!$obj->all[is_hided_in_menu] || $site->in_editor) {
            array_push($section, $obj);
        }
    }
    $count = sizeof($section);
    $counttotal = isset($limit) ? $alamlist_count->rows : $count;
    ##############
    # assign to template variables
    ## This is how we __should__ have assigned the
    ## variables !!
    ##
    $smarty->assign(array($name => $section, $name . '_newbutton' => $new_button, $name . '_counttotal' => $counttotal, $name . '_rows' => $counttotal, $name . '_count' => $count));
}
function smarty_function_init_object($params, &$smarty)
{
    global $site, $leht, $template, $class_path;
    $content_template =& $leht->content_template;
    ##############
    # default values
    extract($params);
    if (!isset($id)) {
        $id = $leht->id;
    }
    if (!isset($name)) {
        $name = "object";
    }
    ###############
    # action-buttons
    # by default show all
    if (!isset($buttons)) {
        $buttons = array("new", "edit", "hide", "move", "delete");
    } else {
        $buttons = split(",", $buttons);
    }
    ##############
    # luua objekt & load sisu
    $obj = new Objekt(array(objekt_id => $id));
    $obj->load_sisu();
    ################
    # object GENERAL parameters
    $obj->id = $obj->objekt_id;
    $obj->class = translate_en($obj->all[klass]);
    # translate it to english
    # kui link
    if ($obj->all[klass] == "link") {
        $objektUrl = $obj->all['url'];
        // replace index.php?id=xxx or ?id=xxx style local url with its alias
        if (!$site->in_editor && $site->CONF['use_aliases'] && $site->CONF['replace_links_with_alias']) {
            $objektUrl = convert_local_link_to_alias($objektUrl);
        }
        $objektUrl && $obj->all['on_uusaken'] ? $obj->href = $objektUrl . '" target="_blank' : ($obj->href = $objektUrl);
    } else {
        $obj->get_object_href();
    }
    $obj->title = $obj->pealkiri;
    $obj->buttons = $obj->get_edit_buttons(array(nupud => $buttons, tyyp_idlist => $obj->all['tyyp_id'], publish => $publish));
    $obj->fdate = $obj->all[aeg];
    $obj->last_modified = date('Y', $obj->all['last_modified']) > 1970 ? date('d.m.Y H:i', $obj->all['last_modified']) : '';
    ## crap data
    $obj->flast_modified = $obj->all['last_modified'];
    $obj->details_link = $obj->href;
    $obj->details_title = $site->sys_sona(array(sona => "loe edasi", tyyp => "kujundus"));
    $obj->printgif = '<a href="' . $obj->href . '&op=print" onClick="avaprintaken(this.href, 600, 400, \'print\'); return false;" target=_blank><img src="' . $site->img_path . '/print_it.gif" border=0 width=19 height=18></a>';
    $obj->printlink = $site->self . '?id=' . $obj->objekt_id . '&op=print';
    $obj->created_user_id = $obj->all['created_user_id'];
    $obj->created_user_name = $obj->all['created_user_name'];
    $obj->changed_user_id = $obj->all['changed_user_id'];
    $obj->changed_user_name = $obj->all['changed_user_name'];
    $obj->created_time = $site->db->MySQL_ee($obj->all['created_time']);
    $obj->fcreated_time = $obj->all['created_time'];
    $obj->changed_time = $site->db->MySQL_ee($obj->all['changed_time']);
    $obj->fchanged_time = $obj->all['changed_time'];
    $obj->last_commented_time = $site->db->MySQL_ee($obj->all['last_commented_time']);
    $obj->comment_count = $obj->all['comment_count'];
    ################
    # ALL values, set as attributes
    foreach ($obj->all as $fieldname => $value) {
        $obj->{$fieldname} = $value;
    }
    ###############
    # profile values, set as attributes
    $profile_def = $site->get_profile(array(id => $obj->all['profile_id']));
    if ($profile_def[profile_id]) {
        include_once $class_path . 'profile.class.php';
        $obj_profile = new Profile(array("id" => $obj->all['profile_id']));
        #### 1. set profile fields as object attributes
        $obj_profile->set_obj_general_fields(array("obj" => &$obj, "get_object_fields" => $get_object_fields));
        ###################
        # get selectlist values - 1 extra sql per function; sql is fast
        if (is_array($obj_profile->selectlist)) {
            $obj_profile->selectlist = array_unique($obj_profile->selectlist);
            #printr($obj_profile->selectlist);
        }
        # go on if object values needs changing:
        if (sizeof($obj_profile->selectlist) > 0) {
            #### 2. save array "->asset_names"  human readable NAME-s:
            $obj_profile->get_asset_names(array("selectlist" => $obj_profile->selectlist));
            #printr($obj_profile->asset_names);
            #printr($obj_profile->change_fields);
            ### 3. save object rest of attributes
            #print "<br>muuta ID: ".$obj->id;
            $obj_profile->set_obj_selectlist_fields(array("obj" => &$obj, "change_fields" => $obj_profile->change_fields));
        }
        # if any selectvalue exist & need to change
        # / get selectlist values
        ###################
    }
    ################
    # object CLASS specific parameters
    ########## ARTICLE
    if ($obj->class == 'article') {
        //$obj = init_article(array("id"=>$obj->id), &$smarty);
        if (!function_exists('smarty_function_init_article')) {
            require_once $smarty->_get_plugin_filepath('function', 'init_article');
        }
        smarty_function_init_article(array("id" => $obj->id, 'name' => $name), $smarty);
        return;
    } elseif ($obj->class == 'document') {
        if (!function_exists('smarty_function_init_document')) {
            require_once $smarty->_get_plugin_filepath('function', 'init_document');
        }
        $obj = smarty_function_init_document(array("id" => $obj->id, 'name' => $name), $smarty);
        return;
    } elseif ($obj->class == 'image') {
        if (!function_exists('smarty_function_init_picture')) {
            require_once $smarty->_get_plugin_filepath('function', 'init_picture');
        }
        $obj = smarty_function_init_picture(array("id" => $obj->id, 'name' => $name), $smarty);
        return;
    } elseif ($obj->class == 'section') {
        $obj->show_toolicons = $obj->all['on_printlink'];
        $obj->is_mailinglist = $obj->all['on_meilinglist'];
        $obj->show_subarticles = $obj->all['on_alamartiklid'];
        $obj->hide_in_menu = $obj->all['on_peida_vmenyy'];
        $obj->show_date = $obj->all['on_kp_nahtav'];
    } elseif ($obj->class == 'poll') {
        $obj->is_open = $obj->all['on_avatud'];
        $obj->expires = $obj->all['expires'] ? $site->db->MySQL_ee($obj->all['expires']) : '';
        $obj->fexpires = $obj->all['expires'] ? $obj->all['expires'] : '';
        $obj->is_expired = $obj->all['expires'] && (strtotime($obj->all['expires']) > 0 && strtotime($obj->all['expires']) < time()) ? 1 : 0;
        #printr(strtotime($obj->all['expires']));
        ######### CHECK voting
        # 1) IP-based gallup
        if ($site->CONF[gallup_ip_check] == 1) {
            $sql = $site->db->prepare("SELECT COUNT(gi_id) FROM gallup_ip WHERE objekt_id=? AND ip LIKE ?", $obj->id, $_SERVER["REMOTE_ADDR"]);
            $sth = new SQL($sql);
            $count = $sth->fetchsingle();
        } else {
            if ($site->CONF[gallup_ip_check] == 2 && $site->cookie["gallup"][$obj->id] == 1) {
                $count = 1;
            } else {
                if ($site->CONF[gallup_ip_check] == 3) {
                    $sql = $site->db->prepare("SELECT COUNT(gi_id) FROM gallup_ip WHERE objekt_id=? AND user_id=?", $obj->id, $site->user->user_id);
                    $sth = new SQL($sql);
                    # count=1: not logged in users are not allowed to vote:
                    $count = $site->user->user_id ? $sth->fetchsingle() : 1;
                } else {
                    $count = 0;
                }
            }
        }
        ######### / CHECK voting
        ### is_voted: if user is voted this poll or not, 1/0
        $obj->is_voted = $count;
        # not voted
        ### answers
        $sql = $site->db->prepare("SELECT * FROM gallup_vastus WHERE objekt_id=?", $obj->id);
        $sth = new SQL($sql);
        $site->debug->msg($sth->debug->get_msgs());
        $obj->answers = array();
        $obj->answers_count = 0;
        while ($vastus = $sth->fetch()) {
            unset($tmp);
            $tmp = new stdClass();
            $tmp->id = $vastus[gv_id];
            $tmp->answer = $vastus[vastus];
            $tmp->title = $vastus[vastus];
            $tmp->count = $vastus[count];
            $obj->answers[$vastus[gv_id]] = $tmp;
            $obj->answers_count += $vastus[count];
        }
        ### / answers
        ### voters (if not anonymous poll)
        if (!$obj->is_anonymous) {
            $sql = $site->db->prepare("SELECT gallup_ip.*, users.firstname, users.lastname\r\n\t\t\t\tFROM gallup_ip\r\n\t\t\t\t\tLEFT JOIN users ON users.user_id = gallup_ip.user_id\r\n\t\t\t\tWHERE objekt_id=?", $obj->id);
            $sth = new SQL($sql);
            $site->debug->msg($sth->debug->get_msgs());
            $obj->voters = array();
            while ($vastus = $sth->fetch()) {
                unset($tmp);
                $tmp->id = $vastus[gi_id];
                $tmp->answer_id = $vastus[gv_id];
                $tmp->ip = $vastus[ip];
                $tmp->user_id = $vastus[user_id];
                $tmp->user_firstname = $vastus[firstname];
                $tmp->user_lastname = $vastus[lastname];
                $tmp->time = $site->db->MySQL_ee($vastus[vote_time]);
                $tmp->ftime = $vastus[vote_time];
                $obj->voters[$vastus[gi_id]] = $tmp;
            }
        }
        # if not anonymous poll
        ### / voters
    } elseif ($obj->class == 'album') {
        // add album config atributes
        $conf = new CONFIG($obj->all['ttyyp_params']);
        $obj->description = $conf->get('desc');
        $obj->thumbnail_size = $conf->get('tn_size');
        # in pixels
        $obj->image_size = $conf->get('pic_size');
        # in pixels
        $obj->folder_id = $conf->get('folder_id');
        # source folder ID
        $obj->folder_path = $conf->get('path');
        # source folder path, eg "public/images"
    }
    ########## / ALBUM
    ##############
    # assign to template variables
    $smarty->assign($name, $obj);
}