Example #1
0
function pages_html_menu($elements)
{
    $menu = '';
    global $page_owner;
    if (is_array($elements)) {
        $current_page = pages_current_page();
        $current_uri = !empty($current_page->uri) ? $current_page->uri : '';
        foreach ($elements as $p => $li) {
            if (is_array($li)) {
                $parent = $li[0];
                $childs = isset($li[1]) ? $li[1] : null;
                // workaround
                if (!$parent) {
                    // this never should happen
                    trigger_error(__FUNCTION__ . ": invalid menu element", E_USER_NOTICE);
                    continue;
                }
                // set owner
                if (!isset($owner)) {
                    $owner = $parent->owner;
                }
                if ($parent->uri == $current_uri) {
                    $parent_link = pages_html_a(pages_url($parent->uri, 'pages::page', $parent->owner), $parent->name, array('class' => 'selected'));
                } else {
                    $parent_link = pages_html_a(pages_url($parent->uri, 'pages::page', $parent->owner), $parent->name);
                }
                if (is_array($childs)) {
                    $submenu = '';
                    foreach ($childs as $child) {
                        if ($child->uri == $current_uri) {
                            $_li = pages_html_a(pages_url($child->uri, 'pages::page', $child->owner), $child->name, array('class' => 'selected'));
                        } else {
                            $_li = pages_html_a(pages_url($child->uri, 'pages::page', $child->owner), $child->name);
                        }
                        $submenu .= pages_html_wrap('li', $_li, array('class' => 'menu-item'));
                    }
                    $menu .= pages_html_wrap('li', $parent_link . pages_html_wrap('ul', $submenu), array('class' => 'menu-parent'));
                } else {
                    $menu .= pages_html_wrap('li', $parent_link, array('class' => 'menu-item'));
                }
            } else {
            }
        }
    }
    // workaround
    if (!isset($owner)) {
        $owner = empty($page_owner) ? page_owner() : $page_owner;
    }
    if (empty($menu)) {
        $main_link = pages_html_a(pages_url(__gettext('Main'), 'pages::page', $owner), __gettext('Main'));
        $menu .= pages_html_wrap('li', $main_link, array('class' => 'menu-item'));
    }
    if (!empty($menu)) {
        $menu = pages_html_wrap('ul', $menu);
    }
    return $menu;
}
function pages_edit_page($page_name, $owner = -1)
{
    global $CFG, $PAGE;
    $PAGE->pages->editing = true;
    if (pages_enabled() && !empty($page_name) && permissions_check('pages::edit', $owner)) {
        if ($owner == -1 && pages_is_frontpage($page_name)) {
            $is_frontpage = true;
        } else {
            $is_frontpage = false;
        }
        if (!($page = get_record('pages', 'uri', $page_name, 'owner', $owner))) {
            // try to edit legacy content
            if ($owner == -1) {
                $page = pages_get_legacy($page_name);
            }
        } else {
            pages_current_page($page);
        }
        $page = pages_input_override($page, $owner, false);
        if (isset($page->ident)) {
            $page->ident = intval($page->ident);
        }
        //if frontpage force some value
        if ($is_frontpage) {
            $page->name = $page_name;
            $page->parent = -1;
            //hidden
            $page->access = 'PUBLIC';
            //force public
        }
        $title_enc = htmlspecialchars($page->title, ENT_QUOTES, 'utf-8');
        //$page->content = htmlspecialchars(stripslashes($page->content), ENT_COMPAT, 'utf-8');
        $page->content = trim($page->content);
        $page->name = htmlspecialchars($page->name, ENT_QUOTES, 'utf-8');
        $input_title = pages_html_wrap('label', __gettext('Title:'), array('for' => 'page-title'));
        $input_title .= pages_html_input('text', array('id' => 'page-title', 'name' => 'page-title', 'value' => $title_enc, 'maxlength' => 127, 'style' => 'width:100%;'));
        $input_content = pages_html_wrap('label', __gettext('Content:'), array('for' => 'page-content'));
        $input_content .= pages_html_wrap('textarea', htmlspecialchars($page->content, ENT_NOQUOTES, 'utf-8'), array('id' => 'page-content', 'name' => 'page-content', 'rows' => 20, 'cols' => '79', 'style' => 'width:100%;'));
        if ($is_frontpage) {
            $input_menu = pages_html_input('hidden', array('name' => 'page-name', 'value' => stripslashes($page->name)));
            $input_default = null;
            $input_parent = pages_html_input('hidden', array('name' => 'menu-parent', 'value' => -1));
            $input_weight = null;
            $input_access = pages_html_input('hidden', array('name' => 'page-access', 'value' => 'PUBLIC'));
        } else {
            $input_menu = pages_html_wrap('label', __gettext('Menu title:'), array('for' => 'page-name'));
            $input_menu .= pages_html_input('text', array('id' => 'page-name', 'name' => 'page-name', 'value' => stripslashes($page->name), 'maxlength' => 127));
            if (!isset($page->ident) && $page_name == __gettext('Main')) {
                $page_default = 'checked';
            } else {
                $page_default = pages_is_default(isset($page->ident) ? $page->ident : 0, $owner) ? 'checked' : 'dummy';
            }
            if ($page_default == 'dummy') {
                $input_default = pages_html_wrap('label', __gettext('Make default:'), array('for' => 'page-default'));
                $input_default .= pages_html_input('checkbox', array('id' => 'page-default', 'name' => 'page-default', $page_default => $page_default));
            } else {
                // hidden checked input
                $input_default = pages_html_input('hidden', array('name' => 'page-default', 'value' => true));
            }
            $input_parent = pages_html_wrap('label', __gettext('Parent element:'), array('for' => 'menu-parent'));
            if (isset($page->ident)) {
                $menu_parents = get_records_select('pages', 'parent=? AND ident<>? AND owner=?', array(0, $page->ident, $owner));
            } else {
                $menu_parents = get_records_select('pages', 'parent=? AND owner=?', array(0, $owner));
            }
            $menu_parents_opts = array();
            $menu_parents_opts[] = (object) array('label' => __gettext('Top menu'), 'value' => 0);
            if (is_array($menu_parents)) {
                foreach ($menu_parents as $m) {
                    $opt = new StdClass();
                    $opt->value = $m->ident;
                    $opt->label = '- ' . $m->name;
                    if ($m->ident == $page->parent) {
                        $opt->selected = true;
                    }
                    $menu_parents_opts[] = $opt;
                }
            }
            $input_parent .= pages_html_select('menu-parent', $menu_parents_opts, array('id' => 'menu-parent'));
            $input_weight = pages_html_wrap('label', __gettext('Weight:'), array('for' => 'menu-weight'));
            $weights = array();
            for ($i = -10; $i <= 10; $i++) {
                $w = (object) array('label' => " {$i}", 'value' => $i);
                if ($page->weight == $i) {
                    $w->selected = true;
                }
                $weights[] = $w;
            }
            $input_weight .= pages_html_select('menu-weight', $weights, array('id' => 'menu-weight'));
            // access level
            $input_access = pages_html_wrap('label', __gettext('Access:'), array('for' => 'page-access'));
            $input_access .= run('display:access_level_select', array('page-access', $page->access));
        }
        // help
        $form_help = __gettext('You can use {{page}} keyword to link to others pages.');
        $form_help .= '<br />&raquo; ';
        $form_help .= __gettext('<em>{{page:About_me}}</em> will link to your page with menu title "About me". e.g. {{page:Main}}');
        $form_help .= '<br />&raquo; ';
        $form_help .= __gettext('<em>{{page:user:Title}}</em> will link to user\'s page "Title". e.g. {{page:news:Main}}');
        $form_help .= '<br />&raquo; ';
        $form_help .= __gettext('<em>{{page:content:Title}}</em> will link to site main pages "Title". e.g. {{page:content:About}}');
        $form_help = pages_html_wrap('p', $form_help);
        $input_buttons = pages_html_input('submit', array('id' => 'page-save', 'name' => 'submit', 'value' => __gettext('Save page')));
        $input_buttons .= pages_html_input('submit', array('id' => 'page-preview', 'name' => 'submit', 'value' => __gettext('Preview')));
        if ($page->name != 'New page' && !$is_frontpage) {
            $input_buttons .= pages_html_input('submit', array('id' => 'page-delete', 'name' => 'submit', 'value' => __gettext('Delete'), 'onclick' => 'return confirm_delete()'));
            $confirm_delete = __gettext('Are you sure that you want to delete this page?');
            $input_buttons .= pages_html_wrap('script', "\n               <!--\n                function confirm_delete() {\n                    return confirm('{$confirm_delete}');\n                }\n                -->\n                ", array('type' => 'text/javascript'));
        }
        // hidden action
        $input_buttons .= pages_html_input('hidden', array('name' => 'action', 'value' => 'pages:edit'));
        $content = pages_html_wrap('div', $input_title, array('class' => 'form-item'));
        $content .= pages_html_wrap('div', $input_content, array('class' => 'form-item'));
        $content .= pages_html_wrap('div', $form_help, array('class' => 'form-item'));
        $content .= pages_html_wrap('div', $input_menu . $input_default, array('class' => 'form-item'));
        $content .= pages_html_wrap('div', $input_parent . $input_weight, array('class' => 'form-item'));
        $content .= pages_html_wrap('div', $input_access, array('class' => 'form-item'));
        $content .= pages_html_wrap('div', $input_buttons, array('class' => 'form-item form-button'));
        $content = pages_html_wrap('form', $content, array('id' => 'page-form', 'method' => 'post', 'action' => $_SERVER['REQUEST_URI']));
        // check for preview action
        if (optional_param('submit') == __gettext('Preview')) {
            $content = pages_preview_page($page) . $content;
        }
        // override content
        $page->content = $content;
    } else {
        $page = pages_page_denied();
    }
    if (empty($page)) {
        $page = pages_page_not_found();
    }
    return $page;
}