Download link to the original file?

Permalink Browser Info Environment
Hi Jordan,

would it be possible to tweak a custom template in a way that a download link to the original image file could be provided?
(A client wants to have a gallery to browse photos and then be able to dowload a chosen photo as full size image file). I'm not sure, if that could be easily implemented...?

Cheers,
Michael

Type: Discussion
Status: New
okapi
View Replies:
jordanlev replied on at Permalink Reply
jordanlev
Yes. All of the images in the gallery are links to the full-size images -- the javascript intercepts those links and shows them as a lightbox, but if you just disable the javascript then the links will function as normal.

Here's how to do that:
Create a custom template for the deluxe image gallery block and in its view.php file delete this chunk of javascript code (lines #43 through #58):
<script type="text/javascript">
$(document).ready(function(){
   $('a[rel="<?php echo $rel; ?>"]').fancybox({
      'transitionIn' : '<?php echo $lightboxTransitionEffect; ?>',
      'transitionOut' : '<?php echo $lightboxTransitionEffect; ?>',
      'titleShow' : <?php echo $lightboxTitlePosition == 'none' ? 'false' : 'true'; ?>,
      'titlePosition' : '<?php echo $lightboxTitlePosition; ?>',
      'onStart': function(currentArray,currentIndex,currentOpts) {
         var obj = currentArray[ currentIndex ];
         if ($(obj).next().length) {
            this.title = $(obj).next().html();
         }
      }
   });
});



You'll probably want to make sure that you're linking to the full-size image and not a resized one -- to do this just leave the 'Zoomed Width' and 'Zoomed Height' fields empty (or set to 0) when you add/edit the block.

Finally, you might want to force the images to be downloaded as opposed to displayed inline in the browser window. To make this happen, you'll need to make a modification to the controller and the view: in the controller, find this line (should be line #41):
$image = array();

...and just BELOW that add this new line:
$image['fID'] = $file['fID'];


And now in the view, change line #17 from this:
<a href="<?php echo $img['full_src']; ?>" rel="<?php echo $rel; ?>">

...to this:
<a href="<?php echo View::url('/download_file', $img['fID'], Page::getCurrentPage()->getCollectionID()); ?>" rel="<?php echo $rel; ?>">

(that call to Page::getCurrentPage()->getCollectionID() is so that concrete5's built-in downloading functionality can track which page the download occurred on -- see the 'statistics' tab in the properties window of a file in the file manager to see what I'm talking about).

-Jordan
okapi replied on at Permalink Reply
okapi
Jordan, you're the man!
Thank you for that!

Unfortunately i still have A LOT to learn... for me it works - almost! Maybe i have screwed up something...

What i actually want to have, is the default behaviour with the lightbox enlargement when thumbnails are clicked, but with an additional download link to the original (big) image, just below the thumbnail titles.

Let me sum up, what i have done so far:

i created this directory structure:
blocks/deluxe_image_gallery/templates/my_custom_template/


in that directory i copied controller.php, view.css and view.php and edited these files.

According to your advice, in controller.php, below

$image = array();


i added

$image['fID'] = $file['fID'];



And to have the download link to the original file below the title of the thumbnails, in view.php i inserted

<div class="deluxe_image_gallery_download"><a href="<?php echo View::url('/download_file', $img['fID'], Page::getCurrentPage()->getCollectionID()); ?>">Download Original</a></div>


(on line 37)

Unfortunately the output of the link "Download Original" is just

http://my_website/index.php/download_file/


The last part of the url is not rendered... obviously i have missed something...?

Michael
jordanlev replied on at Permalink Reply
jordanlev
You may need to move the modified controller.php file up 2 directories (to blocks/deluxe_image_gallery/controller.php). Because I don't think controllers are included in custom templates really, but I'm pretty sure you can just override them altogether by putting them in the block directory where I showed you.

And perhaps you still have a lot to learn but honestly it seems to me like you have a pretty good handle on this stuff -- most people wouldn't be able to take the instructions I gave and "translate" them to this slightly different task you're trying to perform!

-Jordan
okapi replied on at Permalink Reply
okapi
Thank you for your kind words - i would love to understand more.
Yes, that's what i was wondering about, maybe controller.php should not go to a custom templates directory but only to blocks.
So i finally put them all together in the blocks directory
UPDATE: and now it suddenly works! Wow.

Thank you for your precious help!

Cheers,
Michael
jordanlev replied on at Permalink Reply
jordanlev
Yeah, I don't quite understand all the rules of overriding things with custom templates -- seem to be a lot of exceptions. If moving the controller up 2 levels didn't fix the problem, then I think you're going to have to remove it entirely and just edit the controller in the package block. If I ever do another update to this addon I'll add this to the controller so if you upgrade it you won't lost that particular change.

-Jordan
okapi replied on at Permalink Reply
okapi
It works nicely now, thanks to your support. Clearing the cache finally helped to get it to work ;)

What i have learned:

one can create multiple custom templates for an add-on, while the view.php and css.php files should always go to:

blocks/name_of_the_add-on/templates/name_of_the_custom_template/


But there is only one controller.php file - if customized - which should go to:

blocks/name_of_the_add-on/


As far as i understand now, the controller.php file provides the functionality, while the custom templates (with the view.php and css.php files) are responsible for how that functionality is executed and displayed. All files can be customized, which is absolutely great, and the custom templates - if there are more than one - can be chosen individually for each block using the related add-on.

Thank you,
Michael
jordanlev replied on at Permalink Reply
jordanlev
Exactly! This is known as "separation of business logic and display logic", which is a very powerful concept in programming. The point is that you put functionality that gathers and prepares data in one place and then the code responsible for displaying that data is in another place, and the purpose of separating things this way is so you can have different "views" on the same data (like a custom template in Concrete5). If you've heard of the "MVC" patter or framework, that's basically what it's all about -- the "V" is for View, the "C" is for Controller, and the "M" is for Model. Model is short for "data model", which basically means the raw data that you would store in a database, and the MVC pattern is a three-way separation (view is still responsible for displaying information, the model is responsible for storing data, and the controller is responsible for shuttling data back and forth between the two and should ideally handle any logic or functions that need to happen outside of the database).

So you've learned one of the most important concepts in programming today -- congratulations!
(And also taught me something along the way -- that you need to clear the cache when overriding a block controller, and that you can only have one controller override for a block even if you have several different custom templates).

-Jordan
okapi replied on at Permalink Reply
okapi
Cool.
Actually that MVC concept seems to be an interesting model for human life in general:
we collect information, we learn how to handle it, we develop skills, and finally we use our knowledge in an appropriate way for work and for our social relationships.
If these three fields are well balanced, maybe this is a good life. ;)

Michael

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.