plugins/managePage/PageManageController.js
function deletePage(req, res, next) {
    var that = this, db = that.getDB(), DBActions = that.getDBActionsLib(),
        dbAction = DBActions.getAuthInstance(req, PAGE_SCHEMA, PAGE_PERMISSION_SCHEMA_ENTRY);
    var params = req.params;
    var pageId = params.id;
    if (!pageId) {
        that.setErrorMessage(req, "No page is selected");
        return next(null);
    }
    var redirect = req.params.page + "/" + req.params.plugin;
    dbAction.get("findByPageId", pageId, function (err, page) {
        if (err) {
            return next(err);
        }
        page && (page = page.toObject());
        if (page && (that.getAppProperty("DEFAULT_INDEX_PAGE") == page.friendlyURL)) {
            that.setErrorMessage(req, "Cannot delete index page.");
            that.setRedirect(req, redirect);
            return next(null);
        }
        PageManager.hasChildren(dbAction, pageId, function (err, pages) {
            if (pages.length > 0) {
                that.setRedirect(req, redirect);
                that.setErrorMessage(req, "Delete all child pages.");
                next(err);
            } else {
                var aboveSiblings;
                async.series([
                    function (n) {
                        dbAction.authorizedRemove(pageId, n);
                    },
                    function (n) {
                        PageManager.getAboveSiblings(dbAction, page, function (err, pages) {
                            if (pages) {
                                aboveSiblings = pages;
                            }
                            n(err, pages);
                        });
                    },
                    function (n) {
                        if (!aboveSiblings || aboveSiblings.length == 0) {
                            return n(null, true);
                        }

                        var dbAction1 = DBActions.getInstance(req, PAGE_SCHEMA);
                        var decrementPageOrder = function (page, n) {
                            var order = page.order;
                            --order;
                            dbAction1.update({
                                pageId: page.pageId,
                                order: order
                            }, n);
                        };

                        async.each(aboveSiblings, decrementPageOrder, n);
                    }
                ], function (err, result) {
                    if (!err) {
                        that.setRedirect(req, redirect);
                        that.setSuccessMessage(req, "Page deleted successfully.");
                    }
                    next(err);
                });
            }
        });
    })
}
function deletePage(req, res, next) {
    var that = this, db = that.getDB(), DBActions = that.getDBActionsLib(),
        dbAction = DBActions.getAuthInstance(req, PAGE_SCHEMA, PAGE_PERMISSION_SCHEMA_ENTRY);
    var params = req.params;
    var pageId = params.id;
    if (!pageId) {
        that.setErrorMessage(req, "No page is selected");
        return next(null);
    }
    var redirect = req.params.page + "/" + req.params.plugin;
    dbAction.get("findByPageId", pageId, function (err, page) {
        if (err) {
            return next(err);
        }
        page && (page = page.toObject());
        if (page && (that.getAppProperty("DEFAULT_INDEX_PAGE") == page.friendlyURL)) {
            that.setErrorMessage(req, "Cannot delete index page.");
            that.setRedirect(req, redirect);
            return next(null);
        }
        PageManager.hasChildren(dbAction, pageId, function (err, pages) {
            if (pages.length > 0) {
                that.setRedirect(req, redirect);
                that.setErrorMessage(req, "Delete all child pages.");
                next(err);
            } else {
                var aboveSiblings;
                async.series([
                    function (n) {
                        dbAction.authorizedRemove(pageId, n);
                    },
                    function (n) {
                        PageManager.getAboveSiblings(dbAction, page, function (err, pages) {
                            if (pages) {
                                aboveSiblings = pages;
                            }
                            n(err, pages);
                        });
                    },
                    function (n) {
                        if (!aboveSiblings || aboveSiblings.length == 0) {
                            return n(null, true);
                        }

                        var dbAction1 = DBActions.getInstance(req, PAGE_SCHEMA);
                        var decrementPageOrder = function (page, n) {
                            var order = page.order;
                            --order;
                            dbAction1.update({
                                pageId: page.pageId,
                                order: order
                            }, n);
                        };

                        async.each(aboveSiblings, decrementPageOrder, n);
                    }
                ], function (err, result) {
                    if (!err) {
                        that.setRedirect(req, redirect);
                        that.setSuccessMessage(req, "Page deleted successfully.");
                    }
                    next(err);
                });
            }
        });
    })
}
function deletePage ( req , res , next ) { var that = this , db = that . getDB ( ) , DBActions = that . getDBActionsLib ( ) , dbAction = DBActions . getAuthInstance ( req , PAGE_SCHEMA , PAGE_PERMISSION_SCHEMA_ENTRY ) ; var params = req . params ; var pageId = params . id ; if ( ! pageId ) { that . setErrorMessage ( req , "No page is selected" ) ; return next ( null ) ; } var redirect = req . params . page + "/" + req . params . plugin ; dbAction . get ( "findByPageId" , pageId , function ( err , page ) { if ( err ) { return next ( err ) ; } page && ( page = page . toObject ( ) ) ; if ( page && ( that . getAppProperty ( "DEFAULT_INDEX_PAGE" ) == page . friendlyURL ) ) { that . setErrorMessage ( req , "Cannot delete index page." ) ; that . setRedirect ( req , redirect ) ; return next ( null ) ; } PageManager . hasChildren ( dbAction , pageId , function ( err , pages ) { if ( pages . length > 0 ) { that . setRedirect ( req , redirect ) ; that . setErrorMessage ( req , "Delete all child pages." ) ; next ( err ) ; } else { var aboveSiblings ; async . series ( [ function ( n ) { dbAction . authorizedRemove ( pageId , n ) ; } , function ( n ) { PageManager . getAboveSiblings ( dbAction , page , function ( err , pages ) { if ( pages ) { aboveSiblings = pages ; } n ( err , pages ) ; } ) ; } , function ( n ) { if ( ! aboveSiblings || aboveSiblings . length == 0 ) { return n ( null , true ) ; } var dbAction1 = DBActions . getInstance ( req , PAGE_SCHEMA ) ; var decrementPageOrder = function ( page , n ) { var order = page . order ; -- order ; dbAction1 . update ( { pageId : page . pageId , order : order } , n ) ; } ; async . each ( aboveSiblings , decrementPageOrder , n ) ; } ] , function ( err , result ) { if ( ! err ) { that . setRedirect ( req , redirect ) ; that . setSuccessMessage ( req , "Page deleted successfully." ) ; } next ( err ) ; } ) ; } } ) ; } ) }
only single page is deleted at a time, ie if a page doesn't have any children
@param req
@param res
@param next
only single page is deleted at a time ie if a page doesn t have any children