Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pub_date and unpub_date handling #7

Open
ttyiav opened this issue May 30, 2017 · 2 comments
Open

pub_date and unpub_date handling #7

ttyiav opened this issue May 30, 2017 · 2 comments

Comments

@ttyiav
Copy link

ttyiav commented May 30, 2017

Hello,
Tried to implement the auto-unpublish with CacheMaster enabled, but unfortunately it does not work as expected :(
Here is my bug report and my local corrections.

Related commits for reference:
9c45274
0ea93d8

  1. When $modx->cacheManager->refresh(...) is called on OnBeforeDocFormSave event, MODX takes the values of pub_date and unpub_date from the database, not from event's $resource object.
    Since we are in OnBeforeDocFormSave handler, the new values (pub or unpub) are not yet saved to the database.
    Therefore, $modx->cacheManager->refresh will update the auto_publish.cache.php not for current resource, but for other resources with non-zero pub_date and unpub_date values.
    If the submitted (but not saved yet) resource is the only with the unpub_date, the auto_publish.cache.php will not be updated.

  2. With the current approach, the auto_publish.cache.php not updating when the user unset either pub_date or unpub_date, which was previously set. Inside OnBeforeDocFormSave unset values are empty and the condition is not satisfied.

My solution:

  • bind CacheMaster to OnDocFormSave event.
  • relocate $modx->cacheManager->refresh(...) without surrounding condition from OnBeforeFormSave to the first switch($event):
/* Bail out if we're not doing this object */
switch($event) {
    case 'OnDocFormSave':
        if (!$doResources) {
            return;
        }
        
        $ctx = $resource->get('context_key');
        
        // If pub_date or unpub_date are set, update the auto_publish cache 
        // if ($resource->get('pub_date') || $resource->get('unpub_date')) {
        $providers = array(
            'auto_publish' => array('contexts' => $ctx),
        );
        $providers_result = array();
        $modx->cacheManager->refresh($providers, $providers_result); // Cache refreshes from database
            
        if ($doDebug) {
            my_debug($providers_result);
        }
            
        //}
        return;        
        
        break;
    case 'OnDocFormPrerender':
    case 'OnBeforeDocFormSave':
        if (!$doResources) {
            return;
        }
        break;


@BobRay
Copy link
Owner

BobRay commented May 31, 2017

Thanks! That sounds like a good solution. Have you tested it to make sure the rest of the actions work as they should?

@ttyiav
Copy link
Author

ttyiav commented Jun 13, 2017

Hello,
Sorry for answering with delay.
Actually, only one line of this modification was authored by me. It is "case 'OnDocFormSave':". All other lines were copy-pasted from your original code.
I think it is possible to assume that multiple calls to getOption are safe for execution inside OnDocFormSave handler.

Yes, I tested my modification with following scenarios (only with unpub_date).

  • set the unpub_date (one resource)
  • clear the unpub_date (one resource)
  • set the unpub_date (multiple resources with unpub_date set)
  • clear the unpub_date (multiple resources with unpub_date set)

My modification is already working on production website. I use CacheMaster in doResource+uncheckEmptyCache mode only.

The only improvement I could imagine is to actually detect the change of pub_date and unpub_date values on OnBeforeDocFormSave (before) and somehow pass the change to OnDocFormSave (after).
In this case the auto_publish.cache.php will be regenerated only then absolutely needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants