tractorcow / silverstripe-dynamiccache

Simple on the fly caching of dynamic content for Silverstripe
39 stars 27 forks source link

Compatibitly with Frontend Admin #25

Closed cjsewell closed 8 years ago

cjsewell commented 9 years ago

This is not a bug, more of a help request. I have created a module to allow admins to edit CMS pages on the front end (Frontend Admin).

The issue with DynamicCache is that when an admin logs in, they see the cached page, and not the page with the additional javascript that Frontend Admin adds. If I flush while logged in, then guests see the cached page with Frontend Admin

I have created an extension, which checks to see if you are logged in using Session, then if the DB is not connected, it connects it, then if you have access to LeftAndMain it disables DynamicCache:

<?php

if (class_exists("DynamicCacheExtension")) {

    class FrontEndAdminDynamicCacheExtension extends DynamicCacheExtension {

        public function updateEnabled(&$enabled) {
            global $databaseConfig;
            if (Session::get("loggedInAs")) {
                if (!DB::getConn() && $databaseConfig) {
                    DB::connect($databaseConfig);
                }
                $enabled = !Injector::inst()->create('LeftAndMain')->canView();
            } else {
                $enabled = true;
            }
        }

    }

}

While this seems to work, I would like your input if it is the best approach?

Thanks

jonom commented 8 years ago

Hey @cjsewell I know this is a very late reply but I would remove theelse $enabled = true part as this could interfere with other extensions and behaviours from disabling caching.

cjsewell commented 8 years ago

Hey, no worries I know how it can be.

Thanks for the feedback. This is the final version and seems to do the trick FrontEndAdminDynamicCacheExtension.php