How to create a password protected gallery in MODx


Before I launch into this, a bit of a background.

I had a client who wanted to be able to upload images to their website in galleries, some of which were to have password protection placed on them. That is, those galleries could not be viewed without a password being entered.

This isn’t being done from scratch, I’m using the Gallery plugin for image gallery functionality and the PageLocker plugin for password protection.However, by default, these two plugins are completely separate and won’t work with each other.

 

Pagelocker allows a password to be set on any resource. It does this through the password being set as a template variable on that resource (For a more thorough explanation of how the PageLocker plugin works, check the installation instructions).

The issue is that the Gallery plugin displays the list of albums and the album content on the same page. This means there are no individual resources to set passwords on.

[wp_ad_camp_2]

The solution I thought of was to display each gallery on a separate resource. This requires a snippet which creates a resource for each gallery, when one is added. Finally onto the code!

Create a new Snippet. I called mine ‘CheckNewGallery’. In that snippet, put the following PHP:

[wp_ad_camp_2]

A couple of values need to be changed for it to work for your installation, these are at the very top of the code. The first is $GalleryParentID. All resources created will be under one parent (It would make sense if this was the page holding all the galleries. For me, it was 72. The screenshot below shows the resource tree, 72 contains my list of galleries and the 3 resources below have been automatically created.

The second is $pageLockerTemplateID. This is the ID of the PageLocker template. This will make sense if you read the PageLocker installation guide. When run, this snippet checks a couple of things. It checks if there are more galleries than resources for galleries (in which case 1 or more resources are created for these galleries). If there are less, resources need to be deleted. This is where I ran into another issue – I found no way to get this to run when a gallery is created or deleted. The best thing I could think of is to put ‘[[!CheckNewGallery]]’ on the gallery page. This must be refreshed whenever a gallery is added or removed for the gallery resources to be created (or deleted).

The gallery resources are now created when a gallery is added, the only issue is linking to these resources. The Gallery row chunk needs to be changed. By default this is called ‘galAlbumRowTpl’. The only part that needs changing is the anchor tag so it links to the gallery resource rather than back to the same page as it would by default. The existing Anchor tag should read:

Which should be changed to:

That does of course mean another snippet needs adding. Called getGalleryLink, into which, paste the following PHP:

 

And that’s it! Sorry for the long guide, it’s a bit of a hacked together work around. But it solved a problem that I had and although I’m aware it isn’t the best I’ve not seen anything better. I hope it helps you and if you have any suggestions or a better way of doing this then please let me know!

UPDATE 22/7: For some reason the php tags aren’t coming up correctly, please make sure <!–?php is actually <?php when you copy this code!

Leave a comment

Your email address will not be published. Required fields are marked *