function xanth_db_install_page()
{
    //install a new visual element
    $element = new xVisualElement('page');
    $element->insert();
    //...and the default view mode
    $proc = '
$output = 
	\'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	<html>
	<head>
	<title>\'.$this->header[\'title\'].\'</title>
	<meta name="keywords" content="\'.$this->header[\'keywords\'].\'" />
	<meta name="description" content="\'.$this->header[\'description\'].\'" />
	<style type="text/css" media="all">@import "themes/default_theme/style.css";</style>
	</head>
	<body>
	<table id="page-table"><tr>
	<td id="left-sidebar">\'. $this->areas[\'sidebar left\'] . \'</td>
	<td id="content-area">\'. $this->areas[\'content\'] . \'</td>	 
	</tr></table>
	<div id="footer">\'. $this->areas[\'footer\'] .\'</div>
	</body>
	</html>\';
return $output;
';
    $view = new xViewMode(0, 'Default page view', 'page', TRUE, $proc);
    $view->insert();
}
function xanth_view_mode_admin_view_mode_edit($hook_primary_id, $hook_secondary_id, $arguments)
{
    if (!xUser::check_current_user_access('manage view_mode')) {
        return xSpecialPage::access_denied();
    }
    if (empty($arguments[0])) {
        new xPageContent('Edit View Mode', 'Wich view mode should edit?');
    }
    $form = new xForm('?p=admin/view_mode/edit//' . $arguments[0]);
    $view_mode = xViewMode::get($arguments[0]);
    $form->elements[] = new xFormElementHidden('view_mode_id', 'View Mode', $arguments[0], FALSE, new xInputValidatorInteger());
    $form->elements[] = new xFormElementTextField('view_mode_name', 'Name', '', $view_mode->name, FALSE, new xInputValidatorTextNoTags(32));
    //view elements option
    $velems = xVisualElement::find_all();
    $voptions = array();
    foreach ($velems as $velem) {
        $voptions[$velem->name] = $velem->name;
    }
    $form->elements[] = new xFormElementTextArea('view_mode_procedure', 'Procedure', '', $view_mode->display_procedure, FALSE, new xInputValidatorText(NULL));
    $form->elements[] = new xFormSubmit('submit', 'submit');
    $ret = $form->validate_input();
    if (isset($ret->valid_data['submit'])) {
        if (empty($ret->errors)) {
            $view_mode->name = $ret->valid_data['view_mode_name'];
            $view_mode->display_procedure = $ret->valid_data['view_mode_procedure'];
            $view_mode->update();
            return new xPageContent('Edit View Mode', 'View mode modified');
        } else {
            foreach ($ret->errors as $error) {
                xanth_log(LOG_LEVEL_USER_MESSAGE, $error);
            }
        }
    }
    return new xPageContent('Edit View Mode', $form->render());
}
function xanth_db_install_theme()
{
    //theme
    xanth_db_query("\r\n\t\tCREATE TABLE theme (\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\tPRIMARY KEY (name)\r\n\t\t)TYPE=InnoDB");
    //theme to elements
    xanth_db_query("\r\n\t\tCREATE TABLE theme_to_elements (\r\n\t\ttheme_name VARCHAR(32) NOT NULL,\r\n\t\tvisual_element VARCHAR(32) NOT NULL,\r\n\t\tview_mode INT UNSIGNED NOT NULL,\r\n\t\tUNIQUE (theme_name,visual_element),\r\n\t\tINDEX(theme_name),INDEX(visual_element),INDEX(view_mode),\r\n\t\tFOREIGN KEY (theme_name) REFERENCES theme(name) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY (visual_element) REFERENCES visual_element(name) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY (view_mode) REFERENCES view_mode(id) ON DELETE CASCADE\r\n\t\t)TYPE=InnoDB");
    //theme to elements
    xanth_db_query("\r\n\t\tCREATE TABLE theme_area (\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\tview_mode INT UNSIGNED,\r\n\t\tPRIMARY KEY (name),\r\n\t\tINDEX(view_mode),\r\n\t\tFOREIGN KEY (view_mode) REFERENCES view_mode(id) ON DELETE SET NULL\r\n\t\t)TYPE=InnoDB");
    //register new visual element
    $element = new xVisualElement('area');
    $element->insert();
    //...and the default view mode
    $proc = '
$output = \'\';
foreach($boxes as $box)
{
	$output .= "<div class=\\"box\\">$box</div>";
}
return $output;
';
    $view = new xViewMode(0, 'Default area view', 'area', TRUE, $proc);
    $view->insert();
    //content area view mode
    $proc = '
		return $page_content;
	';
    $content_view = new xViewMode(0, 'Content area view', 'area', FALSE, $proc);
    $content_view->insert();
    //footer area view mode
    $proc = '
		return \'Page created with \'. xPageElement::get_db_query_count() .\' queries in \'.xPageElement::get_execution_time().\' seconds\';
	';
    $foot_view = new xViewMode(0, 'Footer area view', 'area', FALSE, $proc);
    $foot_view->insert();
    //default theme areas
    $area = new xThemeArea('sidebar left');
    $area->insert();
    $area = new xThemeArea('content', $content_view->id);
    $area->insert();
    $area = new xThemeArea('footer', $foot_view->id);
    $area->insert();
    //access rule
    $access = new xAccessRule('manage theme', 'Theme');
    $access->insert();
}
function xanth_db_install_box()
{
    //box
    xanth_db_query("\r\n\t\tCREATE TABLE box (\r\n\t\tname VARCHAR(64) NOT NULL,\r\n\t\ttitle VARCHAR(255),\r\n\t\tcontent TEXT,\r\n\t\tcontent_format VARCHAR(64) NOT NULL,\r\n\t\tarea VARCHAR(32),\r\n\t\tis_user_defined TINYINT NOT NULL,\r\n\t\tPRIMARY KEY(name),\r\n\t\tINDEX(content_format),\r\n\t\tFOREIGN KEY(content_format) REFERENCES content_format(name)\r\n\t\t)TYPE=InnoDB");
    //install a new visual element
    $element = new xVisualElement('box');
    $element->insert();
    //...and the default view mode
    $proc = '
return \'<strong>\' . $this->title .\'</strong> <br />\' . $this->content;
';
    $view = new xViewMode(0, 'Default box view', 'box', TRUE, $proc);
    $view->insert();
    //another view mode for box
    $proc = '
return $this->content;
';
    $view = new xViewMode(0, 'Box view without title', 'box', FALSE, $proc);
    $view->insert();
    //install some access rule
    $access = new xAccessRule('manage box', 'Box');
    $access->insert();
}
function xanth_db_install_entry()
{
    //entry
    xanth_db_query("\r\n\t\tCREATE TABLE entry (\r\n\t\tid INT UNSIGNED NOT NULL AUTO_INCREMENT,\r\n\t\ttitle VARCHAR(256) NOT NULL,\r\n\t\ttype VARCHAR(32) NOT NULL,\r\n\t\tauthor VARCHAR(64) NOT NULL,\r\n\t\tcontent TEXT NOT NULL,\r\n\t\tcontent_format VARCHAR(64) NOT NULL,\r\n\t\tpublished TINYINT NOT NULL,\r\n\t\tdescription VARCHAR(512) NOT NULL,\r\n\t\tkeywords VARCHAR(128) NOT NULL,\r\n\t\tcreation_time TIMESTAMP NOT NULL,\r\n\t\tPRIMARY KEY (id),\r\n\t\tINDEX(type),\r\n\t\tINDEX(content_format),\r\n\t\tFOREIGN KEY(content_format) REFERENCES content_format(name) ON DELETE RESTRICT,\r\n\t\tFOREIGN KEY(type) REFERENCES entry_type(name) ON DELETE RESTRICT\r\n\t\t)TYPE=InnoDB");
    //category to entry
    xanth_db_query("\r\n\t\tCREATE TABLE categorytoentry (\r\n\t\tentryId INT UNSIGNED NOT NULL,\r\n\t\tcatId INT UNSIGNED NOT NULL,\r\n\t\tUNIQUE(entryId,catId),\r\n\t\tINDEX(entryId),\r\n\t\tINDEX(catId),\r\n\t\tFOREIGN KEY(entryId) REFERENCES entry(id) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY(catId) REFERENCES category(id) ON DELETE CASCADE\r\n\t\t)TYPE=InnoDB");
    //install a new visual element
    $element = new xVisualElement('entry');
    $element->insert();
    //...and the default view mode
    $proc = '
return \'<div class="title">\'.$this->title.\'</div><div class="body">\'.$this->content.\'</div>\';
';
    $view = new xViewMode(0, 'Default entry view', 'entry', TRUE, $proc);
    $view->insert();
    //install some access rule
    $access = new xAccessRule('view entry', 'Entry');
    $access->insert();
    $access = new xAccessRule('create entry', 'Entry');
    $access->insert();
    $access = new xAccessRule('edit entry', 'Entry');
    $access->insert();
}
function xanth_theme_admin_theme_edit($hook_primary_id, $hook_secondary_id, $arguments)
{
    if (!xUser::check_current_user_access('manage theme')) {
        return xSpecialPage::access_denied();
    }
    if (empty($arguments[0])) {
        new xPageContent('Edit Theme', 'Wich theme should edit?');
    }
    $form = new xForm('?p=admin/theme/edit//' . $arguments[0]);
    $form->elements[] = new xFormElementHidden('theme_name', 'Theme Name', $arguments[0], FALSE, new xInputValidatorTextNameId(32));
    //iterate every registered visual element
    $edit_theme = xTheme::get($arguments[0]);
    $visual_elements = xVisualElement::find_all();
    foreach ($visual_elements as $visual_element) {
        //now iterate every registered view mode
        $view_modes = xViewMode::find_by_element($visual_element->name);
        $options = array();
        $default = '';
        foreach ($view_modes as $view_mode) {
            $options[$view_mode->name] = $view_mode->id;
            if (isset($edit_theme->themed_elements[$visual_element->name])) {
                $default = $edit_theme->themed_elements[$visual_element->name];
            } elseif ($view_mode->default_for_element) {
                $default = $view_mode->id;
            }
        }
        $form->elements[] = new xFormElementHidden('visual_elements', 'Visual elements', $visual_element->name, TRUE, new xInputValidatorTextNameId(32));
        $form->elements[] = new xFormElementOptions('view_mode_' . $visual_element->name, 'View Mode for v.e. ' . $visual_element->name, '', $default, $options, FALSE, FALSE, new xInputValidatorInteger());
    }
    $form->elements[] = new xFormSubmit('submit', 'submit');
    $ret = $form->validate_input();
    if (isset($ret->valid_data['submit'])) {
        if (empty($ret->errors)) {
            //process form
            $themed_elements = array();
            foreach ($ret->valid_data['visual_elements'] as $vename) {
                $themed_elements[$vename] = $ret->valid_data['view_mode_' . $vename];
            }
            $theme = new xTheme($ret->valid_data['theme_name'], $themed_elements);
            $theme->update();
            return new xPageContent('Edit Theme', 'Theme Edited');
        } else {
            foreach ($ret->errors as $error) {
                xanth_log(LOG_LEVEL_USER_MESSAGE, $error);
            }
        }
    }
    return new xPageContent('Edit Theme', $form->render());
}