}
            }
        }
    }
    $action = 'view';
}
echo '<script src="' . APP_ROOT_PATH . '/js/notebook_page_general.js"></script>' . "\n";
if ($action == 'view') {
    if ($USER->canActOnTarget($ACTIONS['edit'], $notebook_page)) {
        echo '<div id="actions">' . $notebook_page->renderAsButtonEdit() . '</div>' . "\n";
    }
    echo $notebook_page->renderAsView();
    echo '<script src="' . APP_ROOT_PATH . '/js/plant_image_viewer.js"></script>' . "\n";
} else {
    if ($action == 'edit' || $action == 'create') {
        if ($USER->canActOnTarget($ACTIONS['edit'], $notebook_page)) {
            echo $notebook_page->renderAsEdit();
        }
        echo '<script src="' . APP_ROOT_PATH . '/js/plant_image_viewer.js"></script>' . "\n";
        echo '<script src="' . APP_ROOT_PATH . '/js/ordering_controls.js"></script>' . "\n";
        echo '<script src="' . APP_ROOT_PATH . '/js/notebook_page_edit.js"></script>' . "\n";
        //echo 'TODO: implement edit and create actions';
    } else {
        if ($action == 'delete') {
            $notebook = $notebook_page->getNotebook();
            $notebook_page->doDelete();
            util_redirectToAppPage('app_code/notebook.php?action=view&notebook_id=' . $notebook->notebook_id, 'info', util_lang('msg_notebook_page_deleted'));
        }
    }
}
require_once '../foot.php';
    if (!$ap) {
        $action = 'list';
    }
} elseif (!$ap || !$ap->matchesDb) {
    $action = 'list';
}
# 3. confirm that the user is allowed to take that action on that object (if not, redirect them to the home page with an appropriate warning)
if ($action == 'list' && !$USER->canActOnTarget($ACTIONS[$action], $ap)) {
    //        util_prePrintR($USER);
    //        exit;
    util_redirectToAppHome('failure', util_lang('no_permission'));
} elseif ($action != 'list' && !$USER->canActOnTarget($ACTIONS[$action], $ap)) {
    if ($action == 'view') {
        util_redirectToAppPage('app_code/authoritative_plant.php?action=list', 'failure', util_lang('no_permission'));
    }
    util_redirectToAppPage('app_code/authoritative_plant.php?action=view&authoritative_plant_id=' . $_REQUEST['authoritative_plant_id'], 'failure', util_lang('no_permission'));
}
if ($action != 'delete') {
    require_once '../app_head.php';
}
# 4. branch behavior based on the action
#      update - update the object with the data coming in, then show the object (w/ 'saved' message)
#      verify/publish - set the appropriate flag (true or false, depending on data coming in), then show the object (w/ 'saved' message)
#      *list* - not a standard action; show a list (tree) of all metadata to which the user has view access
#      view - show the object
#      create/edit - show a form with the object's current values ($action is 'update' on form submit)
#      delete - delete the metadata_structure, then go to list w/ 'deleted' message
if ($action == 'update') {
    //        echo 'TODO: implement update, verify, and publish actions';
    //        util_prePrintR($_REQUEST);
    /////////////////////
        $notebook = Notebook::createNewNotebookForUser($USER->user_id, $DB);
    } else {
        $notebook = Notebook::getOneFromDb(['notebook_id' => $_REQUEST['notebook_id']], $DB);
        if (!$notebook->matchesDb) {
            //            util_redirectToAppHome('failure',util_lang('no_notebook_found'));
            util_redirectToAppPage('app_code/notebook.php?action=list', 'failure', util_lang('no_notebook_found'));
        }
    }
}
# 3. confirm that the user is allowed to take that action on that object (if not, redirect them to the home page with an appropriate warning)
if (!$USER->canActOnTarget($ACTIONS[$action], $notebook)) {
    //        util_redirectToAppHome('failure',util_lang('no_permission'));
    if ($action == 'edit') {
        util_redirectToAppPage('app_code/notebook.php?action=view&notebook_id=' . $notebook->notebook_id, 'failure', util_lang('no_permission'));
    }
    util_redirectToAppPage('app_code/notebook.php?action=list', 'failure', util_lang('no_permission'));
}
if ($action != 'delete') {
    require_once '../app_head.php';
}
# 4. branch behavior based on the action
#      update - update the object with the data coming in, then show the object (w/ 'saved' message)
#      verify/publish - set the appropriate flag (true or false, depending on data coming in), then show the object (w/ 'saved' message)
#      view - show the object
#      create/edit - show a form with the object's current values ($action is 'update' on form submit)
#      delete - delete the notebook, then go to home page w/ 'deleted' message
if ($action == 'update' || $action == 'verify' || $action == 'publish') {
    //        echo 'TO BE IMPLEMENTED:: implement update action';
    $changed = false;
    if ($notebook->name != $_REQUEST['name']) {
        $changed = true;
    //echo util_lang('add_metadata_structure');
    ?>
<!--</a>--><?php 
    //        }
    echo '<script src="' . APP_ROOT_PATH . '/js/metadata_edit.js"></script>' . "\n";
    echo '<script src="' . APP_ROOT_PATH . '/js/ordering_controls.js"></script>' . "\n";
}
if ($action == 'view') {
    if ($USER->canActOnTarget($ACTIONS['edit'], $mds)) {
        echo '<div id="actions">' . $mds->renderAsButtonEdit() . '</div>' . "\n";
    }
    echo $mds->renderAsView();
} else {
    if ($action == 'edit' || $action == 'create') {
        //echo 'TO BE IMPLEMENTED: edit and create actions';
        echo $mds->renderAsEdit();
        echo '<script src="' . APP_ROOT_PATH . '/js/ordering_controls.js"></script>' . "\n";
        echo '<script src="' . APP_ROOT_PATH . '/js/metadata_edit.js"></script>' . "\n";
    } else {
        if ($action == 'delete') {
            $parent_mds = $mds->getParent();
            $mds->doDelete();
            if ($parent_mds) {
                util_redirectToAppPage('app_code/metadata_structure.php?action=view&metadata_structure_id=' . $parent_mds->metadata_structure_id, 'info', util_lang('msg_metadata_structure_deleted'));
            } else {
                util_redirectToAppPage('app_code/metadata_structure.php?action=list', 'info', util_lang('msg_metadata_structure_deleted'));
            }
        }
    }
}
require_once '../foot.php';