/**
 * Adds an menu item set via POST
 *
 * @return array
 */
function addItem(&$reports)
{
    $menuset = checkChosenMenuset();
    $result['type'] = sanitize($_POST['type']);
    $result['show'] = getCheckboxState('show');
    $result['include_li'] = getCheckboxState('include_li');
    $result['id'] = 0;
    if (getCheckboxState('span')) {
        $result['span_id'] = sanitize($_POST['span_id']);
        $result['span_class'] = sanitize($_POST['span_class']);
    } else {
        $result['span_id'] = '';
        $result['span_class'] = '';
    }
    switch ($result['type']) {
        case 'all_items':
            query("INSERT INTO " . prefix('menu') . " (`title`,`link`,`type`,`show`,`menuset`,`sort_order`) " . "VALUES ('" . gettext('Home') . "', '" . WEBPATH . '/' . "','galleryindex','1'," . db_quote($menuset) . ",'000')", true);
            addAlbumsToDatabase($menuset);
            if (extensionEnabled('zenpage')) {
                query("INSERT INTO " . prefix('menu') . " (`title`,`link`,`type`,`show`,`menuset`,`sort_order`) " . "VALUES ('" . gettext('News index') . "', '" . getNewsIndexURL() . "', 'zenpagenewsindex', '1', " . db_quote($menuset) . ", '001')", true);
                addPagesToDatabase($menuset);
                addCategoriesToDatabase($menuset);
            }
            $reports[] = "<p class = 'messagebox fade-message'>" . gettext("Menu items for all Zenphoto objects added.") . " </p>";
            return NULL;
        case 'all_albums':
            addAlbumsToDatabase($menuset);
            $reports[] = "<p class = 'messagebox fade-message'>" . gettext("Menu items for all albums added.") . " </p>";
            return NULL;
        case 'all_zenpagepages':
            addPagesToDatabase($menuset);
            $reports[] = "<p class = 'messagebox fade-message'>" . gettext("Menu items for all Zenpage pages added.") . " </p>";
            return NULL;
        case 'all_zenpagecategorys':
            addCategoriesToDatabase($menuset);
            $reports[] = "<p class = 'messagebox fade-message'>" . gettext("Menu items for all Zenpage categories added.") . " </p>";
            return NULL;
        case 'album':
            $result['title'] = $result['link'] = sanitize($_POST['albumselect']);
            if (empty($result['link'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to select an album.") . " </p>";
                return $result;
            }
            $successmsg = sprintf(gettext("Album menu item <em>%s</em> added"), $result['link']);
            break;
        case 'galleryindex':
            $result['title'] = process_language_string_save("title", 2);
            $result['link'] = NULL;
            if (empty($result['title'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>title</strong>!") . " </p>";
                return $result;
            }
            $successmsg = sprintf(gettext("Gallery index menu item <em>%s</em> added"), $result['link']);
            break;
        case 'zenpagepage':
            $result['title'] = NULL;
            $result['link'] = sanitize($_POST['pageselect']);
            if (empty($result['link'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>link</strong>!") . " </p>";
                return $result;
            }
            $successmsg = sprintf(gettext("Zenpage page menu item <em>%s</em> added"), $result['link']);
            break;
        case 'zenpagenewsindex':
            $result['title'] = process_language_string_save("title", 2);
            $result['link'] = NULL;
            if (empty($result['title'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>title</strong>!") . " </p>";
                return $result;
            }
            $successmsg = sprintf(gettext("Zenpage news index menu item <em>%s</em> added"), $result['link']);
            break;
        case 'zenpagecategory':
            $result['title'] = NULL;
            $result['link'] = sanitize($_POST['categoryselect']);
            if (empty($result['link'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>link</strong>!") . " </p>";
                return $result;
            }
            $successmsg = sprintf(gettext("Zenpage news category menu item <em>%s</em> added"), $result['link']);
            break;
        case 'custompage':
            $result['title'] = process_language_string_save("title", 2);
            $result['link'] = sanitize($_POST['custompageselect']);
            if (empty($result['title'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>title</strong>!") . " </p>";
                return $result;
            }
            $successmsg = sprintf(gettext("Custom page menu item <em>%s</em> added"), $result['link']);
            break;
        case 'customlink':
            $result['title'] = process_language_string_save("title", 2);
            if (empty($result['title'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>title</strong>!") . " </p>";
                return $result;
            }
            $result['link'] = sanitize($_POST['link']);
            if (empty($result['link'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to provide a <strong>function</strong>!") . " </p>";
                return $result;
            }
            $successmsg = sprintf(gettext("Custom page menu item <em>%s</em> added"), $result['link']);
            break;
        case 'menulabel':
            $result['title'] = process_language_string_save("title", 2);
            $result['link'] = NULL;
            if (empty($result['title'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>title</strong>!") . " </p>";
                return $result;
            }
            $successmsg = gettext("Custom label added");
            break;
        case 'menufunction':
            $result['title'] = process_language_string_save("title", 2);
            if (empty($result['title'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>title</strong>!") . " </p>";
                return $result;
            }
            $result['link'] = sanitize($_POST['link'], 4);
            if (empty($result['link'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to provide a <strong>function</strong>!") . " </p>";
                return $result;
            }
            $successmsg = sprintf(gettext("Function menu item <em>%s</em> added"), $result['link']);
            break;
        case 'html':
            $result['title'] = process_language_string_save("title", 2);
            if (empty($result['title'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to give your menu item a <strong>title</strong>!") . " </p>";
                return $result;
            }
            $result['link'] = sanitize($_POST['link'], 4);
            if (empty($result['link'])) {
                $reports[] = "<p class = 'errorbox fade-message'>" . gettext("You forgot to provide a <strong>function</strong>!") . " </p>";
                return $result;
            }
            $successmsg = gettext("<em>HTML</em> added");
            break;
        default:
            break;
    }
    $count = db_count('menu', 'WHERE menuset=' . db_quote($menuset));
    $order = sprintf('%03u', $count);
    $sql = "INSERT INTO " . prefix('menu') . " ( `title`, `link`, `type`, `show`, `menuset`, `sort_order`, `include_li`, `span_id`, `span_class`) " . "VALUES (" . db_quote($result['title']) . ", " . db_quote($result['link']) . ", " . db_quote($result['type']) . ", " . $result['show'] . ", " . db_quote($menuset) . ", " . db_quote($order) . ", " . $result['include_li'] . ", " . db_quote($result['span_id']) . ", " . db_quote($result['span_class']) . ")";
    if (query($sql, true)) {
        $reports[] = "<p class = 'messagebox fade-message'>" . $successmsg . "</p>";
        //echo "<pre>"; print_r($result); echo "</pre>";
        $result['id'] = db_insert_id();
        return $result;
    } else {
        if (empty($result['link'])) {
            $reports[] = "<p class = 'errorbox fade-message'>" . sprintf(gettext('A <em>%1$s</em> item already exists in <em>%2$s </em>!'), $result['type'], $menuset) . "</p>";
        } else {
            $reports[] = "<p class = 'errorbox fade-message'>" . sprintf(gettext('A <em>%1$s</em> item with the link <em>%2$s</em> already exists in <em>%3$s </em>!'), $result['type'], $result['link'], $menuset) . "</p>";
        }
        return NULL;
    }
}
/**
 * Creates a menu set from the items passed. But only if the menu set does not already exist
 * @param array $menuitems items for the menuset
 * 		array elements:
 * 			'type'=>menuset type
 * 			'title'=>title for the menu item
 * 			'link'=>URL or other data for the item link
 * 			'show'=>set to 1:"visible" or 0:"hidden",
 * 			'nesting'=>nesting level of this item in the menu heirarchy
 *
 * @param string $menuset current menuset
 */
function createMenuIfNotExists($menuitems, $menuset = 'default')
{
    $count = db_count('menu', 'WHERE menuset=' . db_quote($menuset));
    if ($count == 0) {
        // there was not an existing menu set
        require_once dirname(__FILE__) . '/menu_manager/menu_manager-admin-functions.php';
        $success = 1;
        $orders = array();
        foreach ($menuitems as $key => $result) {
            if (array_key_exists('nesting', $result)) {
                $nesting = $result['nesting'];
            } else {
                $nesting = 0;
            }
            while ($nesting + 1 < count($orders)) {
                array_pop($orders);
            }
            while ($nesting + 1 > count($orders)) {
                array_push($orders, -1);
            }
            $result['id'] = 0;
            if (isset($result['include_li'])) {
                $includeli = $result['include_li'];
            } else {
                $includeli = 1;
            }
            $type = $result['type'];
            switch ($type) {
                case 'all_items':
                    $orders[$nesting]++;
                    query("INSERT INTO " . prefix('menu') . " (`title`,`link`,`type`,`show`,`menuset`,`sort_order`) " . "VALUES ('" . gettext('Home') . "', '" . WEBPATH . '/' . "','galleryindex','1'," . db_quote($menuset) . ',' . db_quote($orders), true);
                    $orders[$nesting] = addAlbumsToDatabase($menuset, $orders);
                    if (extensionEnabled('zenpage')) {
                        $orders[$nesting]++;
                        query("INSERT INTO " . prefix('menu') . " (title`,`link`,`type`,`show`,`menuset`,`sort_order`) " . "VALUES ('" . gettext('News index') . "', '" . getNewsIndexURL() . "','newsindex','1'," . db_quote($menuset) . ',' . db_quote(sprintf('%03u', $base + 1)), true);
                        $orders[$nesting] = addPagesToDatabase($menuset, $orders) + 1;
                        $orders[$nesting] = addCategoriesToDatabase($menuset, $orders);
                    }
                    $type = false;
                    break;
                case 'all_albums':
                    $orders[$nesting]++;
                    $orders[$nesting] = addAlbumsToDatabase($menuset, $orders);
                    $type = false;
                    break;
                case 'all_Pages':
                    $orders[$nesting]++;
                    $orders[$nesting] = addPagesToDatabase($menuset, $orders);
                    $type = false;
                    break;
                case 'all_categorys':
                    $orders[$nesting]++;
                    $orders[$nesting] = addCategoriesToDatabase($menuset, $orders);
                    $type = false;
                    break;
                case 'album':
                    $result['title'] = NULL;
                    if (empty($result['link'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty link.'), $key));
                    }
                    break;
                case 'galleryindex':
                    $result['link'] = NULL;
                    if (empty($result['title'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty title.'), $key));
                    }
                    break;
                case 'Page':
                    $result['title'] = NULL;
                    if (empty($result['link'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty link.'), $key));
                    }
                    break;
                case 'newsindex':
                    $result['link'] = NULL;
                    if (empty($result['title'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty title.'), $key));
                    }
                    break;
                case 'category':
                    $result['title'] = NULL;
                    if (empty($result['link'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty link.'), $key));
                    }
                    break;
                case 'custompage':
                    if (empty($result['title']) || empty($result['link'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty title or link.'), $key));
                    }
                    break;
                case 'customlink':
                    if (empty($result['title'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty title.'), $key));
                    } else {
                        if (empty($result['link'])) {
                            $result['link'] = seoFriendly(get_language_string($result['title']));
                        }
                    }
                    break;
                case 'menulabel':
                    if (empty($result['title'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty title.'), $key));
                    }
                    $result['link'] = sha1($result['title']);
                    break;
                case 'menufunction':
                    if (empty($result['title']) || empty($result['link'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty title or link.'), $key));
                    }
                    break;
                case 'html':
                    if (empty($result['title']) || empty($result['link'])) {
                        $success = -1;
                        debugLog(sprintf(gettext('createMenuIfNotExists item %s has an empty title or link.'), $key));
                    }
                    break;
                default:
                    $success = -1;
                    debugLog(sprintf(gettext('createMenuIfNotExists item %s has an invalid type.'), $key));
                    break;
            }
            if ($success > 0 && $type) {
                $orders[$nesting]++;
                $sort_order = '';
                for ($i = 0; $i < count($orders); $i++) {
                    $sort_order .= sprintf('%03u', $orders[$i]) . '-';
                }
                $sort_order = substr($sort_order, 0, -1);
                $sql = "INSERT INTO " . prefix('menu') . " (`title`,`link`,`type`,`show`,`menuset`,`sort_order`,`include_li`) " . "VALUES (" . db_quote($result['title']) . ", " . db_quote($result['link']) . "," . db_quote($result['type']) . "," . $result['show'] . "," . db_quote($menuset) . "," . db_quote($sort_order) . ",{$includeli})";
                if (!query($sql, false)) {
                    $success = -2;
                    debugLog(sprintf(gettext('createMenuIfNotExists item %1$s query (%2$s) failed: %3$s.'), $key, $sql, db_error()));
                }
            }
        }
    } else {
        $success = 0;
    }
    if ($success < 0) {
        zp_error(gettext('createMenuIfNotExists has posted processing errors to your debug log.'), E_USER_NOTICE);
    }
    return $success;
}