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_entry_type_admin_entry_type_add($hook_primary_id, $hook_secondary_id, $arguments)
{
    if (!xUser::check_current_user_access('manage entry type')) {
        return xSpecialPage::access_denied();
    }
    //create form
    $form = new xForm('?p=admin/entry_type/add');
    $form->elements[] = new xFormElementTextField('entry_type_name', 'Name', '', '', TRUE, new xInputValidatorTextNameId(32));
    //view modes
    $modes = xViewMode::find_by_element('entry');
    $options = array();
    $options['[theme default]'] = '0';
    foreach ($modes as $mode) {
        $options[$mode->name] = $mode->id;
    }
    $form->elements[] = new xFormElementOptions('entry_type_view_mode', 'View mode', '', '', $options, FALSE, FALSE, new xInputValidatorInteger());
    //submit buttom
    $form->elements[] = new xFormSubmit('submit', 'Add');
    $ret = $form->validate_input();
    if (isset($ret->valid_data['submit'])) {
        if (empty($ret->errors)) {
            $entry_type = new xEntryType($ret->valid_data['entry_type_name'], $ret->valid_data['entry_type_view_mode']);
            $entry_type->insert();
            return new xPageContent('Entry type created', 'Entry type created');
        } else {
            foreach ($ret->errors as $error) {
                xanth_log(LOG_LEVEL_USER_MESSAGE, $error);
            }
        }
    }
    return new xPageContent('Create entry', $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_category_admin_category_create($hook_primary_id, $hook_secondary_id, $arguments)
{
    if (!xUser::check_current_user_access('manage category')) {
        return xSpecialPage::access_denied();
    }
    //create form
    $form = new xForm('?p=admin/category/create');
    $form->elements[] = new xFormElementTextField('cat_title', 'Title', '', '', TRUE, new xInputValidatorTextNoTags(256));
    $form->elements[] = new xFormElementTextArea('cat_description', 'Description', '', '', TRUE, new xInputValidatorText(-1));
    //types
    $types = xEntryType::find_all();
    $options = array();
    foreach ($types as $type) {
        $options[$type->name] = $type->name;
    }
    $form->elements[] = new xFormElementOptions('entry_types', 'Select type', '', '', $options, TRUE, TRUE, new xInputValidatorTextNameId(32));
    //parent category
    $categories = xCategory::find_all();
    $options = array();
    $options['[no parent]'] = '0';
    foreach ($categories as $category) {
        $options[$category->title] = $category->id;
    }
    $form->elements[] = new xFormElementOptions('parent_category', 'Parent category', '', '', $options, FALSE, FALSE, new xInputValidatorInteger());
    //view modes
    $modes = xViewMode::find_by_element('category');
    $options = array();
    $options['[theme default]'] = '0';
    foreach ($modes as $mode) {
        $options[$mode->name] = $mode->id;
    }
    $form->elements[] = new xFormElementOptions('category_view_mode', 'View mode', '', '', $options, FALSE, FALSE, new xInputValidatorInteger());
    //submit buttom
    $form->elements[] = new xFormSubmit('submit', 'Create');
    $ret = $form->validate_input();
    if (isset($ret->valid_data['submit'])) {
        if (empty($ret->errors)) {
            $cat = new xCategory(NULL, $ret->valid_data['cat_title'], $ret->valid_data['entry_types'], $ret->valid_data['cat_description'], $ret->valid_data['category_view_mode'], $ret->valid_data['parent_category']);
            $cat->insert();
            return new xPageContent('Category created', 'Category created');
        } else {
            foreach ($ret->errors as $error) {
                xanth_log(LOG_LEVEL_USER_MESSAGE, $error);
            }
        }
    }
    return new xPageContent('Create category', $form->render());
}
 /**
  *
  */
 function render()
 {
     //retrieve view mode
     $type = xEntryType::get($this->type);
     if ($type->view_mode_id === NULL) {
         //apply theme default
         $theme = xTheme::get_default();
         return eval($theme->get_view_mode_procedure('entry'));
     } else {
         //apply specified view mode
         $view_mode = xViewMode::get($type->view_mode_id);
         return eval($view_mode->display_procedure);
     }
 }
function xanth_theme_admin_theme_area($hook_primary_id, $hook_secondary_id, $arguments)
{
    if (!xUser::check_current_user_access('manage theme')) {
        return xSpecialPage::access_denied();
    }
    $areas = xThemeArea::find_all();
    $output = "<table>\n";
    $output .= "<tr><th>Name</th><th>View Mode</th><th>Edit</th><th>Delete</th></tr>\n";
    foreach ($areas as $area) {
        $v = xViewMode::get($area->view_mode);
        if ($v === NULL) {
            $vname = 'Theme Default';
        } else {
            $vname = $v->name;
        }
        $output .= "<tr><td>" . $area->name . '</td><td>' . $vname . '</td><td>Edit</td><td>Delete</td></tr>';
    }
    $output .= "</table>\n";
    return new xPageContent('Admin area', $output);
}
 /**
  *
  */
 function render($boxes, $page_content)
 {
     if ($this->view_mode === NULL) {
         //apply theme default
         $theme = xTheme::get_default();
         return eval($theme->get_view_mode_procedure('area'));
     } else {
         //apply specified view mode
         $view_mode = xViewMode::get($this->view_mode);
         return eval($view_mode->display_procedure);
     }
 }