/**
*
*/
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_entry_type()
{
    //entry type
    xanth_db_query("\r\n\t\tCREATE TABLE entry_type (\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\tview_mode_id INT UNSIGNED,\r\n\t\tPRIMARY KEY (name),\r\n\t\tINDEX(view_mode_id),\r\n\t\tFOREIGN KEY (view_mode_id) REFERENCES view_mode(id) ON DELETE SET NULL\r\n\t\t)TYPE=InnoDB");
    $access = new xAccessRule('manage entry type', 'Entry Type');
    $access->insert();
    //some default tipes
    $type = new xEntryType('StaticEntry');
    $type->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_entry_admin_entry_create($hook_primary_id, $hook_secondary_id, $arguments)
{
    if (!xUser::check_current_user_access('create entry')) {
        return xSpecialPage::access_denied();
    }
    //create form
    $form = new xForm('?p=admin/entry/create');
    //types
    $types = xEntryType::find_all();
    $options = array();
    foreach ($types as $type) {
        $options[$type->name] = $type->name;
    }
    $form->elements[] = new xFormElementOptions('entry_type', 'Select type', '', '', $options, FALSE, TRUE, new xInputValidatorTextNameId(32));
    //title
    $form->elements[] = new xFormElementTextField('content_title', 'Title', '', '', TRUE, new xInputValidatorTextNoTags(256));
    //body
    $form->elements[] = new xFormElementTextArea('content_body', 'Body', '', '', TRUE, new xInputValidatorText(256));
    //content formats
    $content_formats = xContentFormat::find_all();
    $content_formats_radio_group = new xFormRadioGroup(array(), 'Content format');
    foreach ($content_formats as $content_format) {
        $content_formats_radio_group->elements[] = new xFormElementRadio('content_format', $content_format->name, $content_format->description, $content_format->name, FALSE, TRUE, new xInputValidatorText(64));
    }
    $content_formats_radio_group->elements[0]->checked = TRUE;
    $form->elements[] = $content_formats_radio_group;
    //categories
    $categories = xCategory::find_all();
    $options = array();
    foreach ($categories as $category) {
        $options[$category->title] = $category->id;
    }
    $form->elements[] = new xFormElementOptions('entry_categories', 'Categories', '', '', $options, TRUE, FALSE, new xInputValidatorInteger());
    //parameters
    $parameters = new xFormGroup(array(), 'Parameters');
    $parameters->elements[] = new xFormElementCheckbox('param_published', 'Published', '', '1', TRUE, FALSE, new xInputValidatorInteger());
    $form->elements[] = $parameters;
    //metadata
    $metadata = new xFormGroup(array(), 'Metadata');
    $metadata->elements[] = new xFormElementTextField('meta_description', 'Description', '', '', FALSE, new xInputValidatorTextNoTags(512));
    $metadata->elements[] = new xFormElementTextField('meta_keywords', 'Keywords', '', '', FALSE, new xInputValidatorTextNoTags(128));
    $form->elements[] = $metadata;
    //submit buttom
    $form->elements[] = new xFormSubmit('submit', 'Create');
    $ret = $form->validate_input();
    if (isset($ret->valid_data['submit'])) {
        if (empty($ret->errors)) {
            //no error,lets create the entry
            $author = xUser::get_current_username() !== NULL ? xUser::get_current_username() : 'anonymous';
            //translate categories
            $cat_ids = $ret->valid_data['entry_categories'];
            $categories = array();
            if (!empty($cat_ids)) {
                foreach ($cat_ids as $cat_id) {
                    $categories[] = new xCategory($cat_id);
                }
            }
            $entry = new xEntry(NULL, $ret->valid_data['content_title'], $ret->valid_data['entry_type'], $author, $ret->valid_data['content_body'], $ret->valid_data['content_format'], $ret->valid_data['param_published'], $ret->valid_data['meta_description'], $ret->valid_data['meta_keywords'], $categories);
            $entry->insert();
            return new xPageContent('Entry created', 'Entry created, <a href="?p=entry//' . $entry->id . '">view it</a>');
        } else {
            foreach ($ret->errors as $error) {
                xanth_log(LOG_LEVEL_USER_MESSAGE, $error);
            }
        }
    }
    return new xPageContent('Create entry', $form->render());
}