return;
}
if (!isAdmin() and !(isCustom() and haveRight(6))) {
    return;
}
?>
<script src="/ckeditor/ckeditor.js"></script>
<?php 
if (isset($_POST['newpage']) or isset($_POST['savepage'])) {
    $page_name = isset($_POST['page_name']) ? mysqli_real_escape_string($con, $_POST['page_name']) : '';
    $page_content = isset($_POST['page_content']) ? mysqli_real_escape_string($con, $_POST['page_content']) : '';
    $page_slug = isset($_POST['page_slug']) ? mysqli_real_escape_string($con, $_POST['page_slug']) : '';
    $page_site = isset($_POST['page_site']) ? mysqli_real_escape_string($con, $_POST['page_site']) : 0;
    $page_modified = date('Y-m-d H:i:s');
    $page_author = CUSER;
    $page_slug = prettyslug($page_slug);
    if ($page_name == '' or $page_slug == '') {
        err("Užpildykite visus reikalaujamus laukelius", 'red');
    } elseif (isset($_POST['newpage']) and countData('pages', "page_slug = '{$page_slug}' AND page_site = {$page_site}") > 0) {
        err("Tokia svetainės ir santrumpos kombinacija jau yra", 'red');
    } elseif (isset($_POST['savepage']) and countData('pages', "page_slug = '{$page_slug}' AND page_site = {$page_site} AND page_id != " . $_GET['page']) > 0) {
        err("Tokią svetainės ir santrumpos kombinaciją jau turi kitas puslapis", 'red');
    } else {
        if (isset($_POST['newpage'])) {
            $id = insertRow('pages', 'page_type, page_name, page_content, page_modified, page_author, page_slug, page_site', "0, '{$page_name}', '{$page_content}', '{$page_modified}', {$page_author}, '{$page_slug}', {$page_site}", false);
            if ($id) {
                redirect(0, '?p=' . page() . '&page=' . $id);
            } else {
                err('Puslapis nesukurtas', 'red');
            }
        } elseif (isset($_POST['savepage']) and isset($_GET['page'])) {
        $tablehead['columns'][$rg] = 'left fc-dates';
        $sortby[$rg] = 'need_expires';
    }
    $parent = '';
    getSort('need_id');
    $where = 'SELECT need_id, need_name, cat_name, user_fname, user_lname, user_orgname, need_expires, need_desc, need_regdate, a.deleted AS deleted FROM (SELECT need_id, need_name, cat_name, need_type, need_needy, need_expires, need_desc, need_regdate, needs.deleted AS deleted FROM needs LEFT JOIN cats ON needs.need_cat = cats.cat_id WHERE needs.need_type=' . subpage() . ' AND needs.need_full=0) a LEFT JOIN needy ON a.need_needy = needy.user_id WHERE a.need_type=' . subpage() . ' ' . $parent . ' ORDER BY ' . $getsort . $getorder;
    formatTable(listData(false, false, pageNum(), $where), $tablefields, $tablehead, $sortby, true, 'nomargin');
    pagination(countData(false, false, $where));
}
// customs end
if (!isAdmin() and !isManager() and !isGridManager()) {
    return;
}
if (isAdmin() and isset($_GET['regenerate'])) {
    foreach (listData('cats', '1') as $key => $data) {
        updateField('cats', 'cat_slug', prettyslug($data['cat_name']), 'cat_id', $data['cat_id']);
    }
    redirect(0, '/index.php?p=need&subp=1');
}
if (!subpage()) {
    return;
}
if (isAction('salinti') and getParam()) {
    if (isManager() and countData(false, false, "SELECT * FROM needs INNER JOIN needy ON needs.need_needy = needy.user_id WHERE needs.need_id = " . getParam() . " AND needy.user_parent = " . CUSER) == 0) {
        err('Neturite teisės keisti šio poreikio duomenis', 'red');
        return;
    }
    if (isGridManager()) {
        $children = array();
        foreach (listData('users', 'user_acctype = 1 AND user_active = 1 AND user_parent = ' . CUSER) as $child) {
            $children[] = 'needy.user_parent = ' . $child['user_id'];