by
KK » Fri Nov 20, 2015 4:27 am
Ewan,
I think I have a better solution for you -
Obviously I can insert a thumbnail field now, but I would have to go back through the 300 images and generate the thumbnail. (Or someone else would have to do it, and she isn't happy!)
I totally agree. So let's create an automated script that will go through the 300 pages and create the missing thumbnails for us
For that, we'll adapt the technique we used in the 'CSV importer' (
viewtopic.php?f=5&t=8803).
Define a new thumbnail region in the template.
If you edit any existing page in admin-panel, this thumbnail will show up empty.
Simply hitting 'save' on the page, sadly, will not create this thumbnail as it is associated with your existing main image. Couch will recreate all thumbnails associated with an image only when it senses that the main image itself has changed.
So, as a test, if we set the main image as blank, save, set the image back to the original image and save a second time you'll see that the thumbnail will get created.
Of course we are not going to do this manually for 300 pages
We'll use the script we are going to create next.
Create a new template (say named 'import.php') place the following code within it and register it by accessing it as super-admin.
- Code: Select all
<?php require_once ('couch/cms.php'); ?>
<cms:template title='Import' ></cms:template>
<cms:set mystart="<cms:gpc 'import' method='get' />" />
<cms:if mystart >
<cms:pages masterpage='gallery.php' paginate='1' limit='2'>
<cms:if k_paginated_top >
<cms:if k_paginate_link_next >
<script language="JavaScript" type="text/javascript">
var myVar;
myVar = window.setTimeout( 'location.href="<cms:show k_paginate_link_next />";', 1000 );
</script>
<button onclick="clearTimeout(myVar);">Stop</button>
<cms:else />
<h1>Done!</h1>
</cms:if>
<h3><cms:show k_current_page /> / <cms:show k_total_pages /> pages (Total <cms:show k_total_records /> records. Showing <cms:show k_paginate_limit /> records per page)</hr>
</cms:if>
<h3><cms:show k_page_title /></h3>
<!-- MAIN PROCESSING START -->
<!-- 1. save existing main-image name in a variable -->
<cms:set my_orig_image = gg_image />
<!-- 2. set main-image to blank -->
<cms:db_persist
_masterpage=k_template_name
_mode='edit'
_page_id=k_page_id
gg_image=''
>
<cms:if k_error>
<strong style="color:red;">ERROR:</strong> <cms:show k_error/>
</cms:if>
</cms:db_persist>
<!-- 3. set main-image back to original data.. this will recreate the new thumbnail associated with it -->
<cms:db_persist
_masterpage=k_template_name
_mode='edit'
_page_id=k_page_id
gg_image=my_orig_image
>
<cms:if k_error>
<strong style="color:red;">ERROR:</strong> <cms:show k_error/>
</cms:if>
</cms:db_persist>
<!-- MAIN PROCESSING END -->
<cms:if k_paginated_bottom >
<cms:paginator />
</cms:if>
</cms:pages>
<cms:else/>
<button onclick='location.href="<cms:add_querystring k_page_link 'import=1' />"'>Start!</button>
</cms:if>
<?php COUCH::invoke(); ?>
You'll have to make slight changes to the code above to make it work with your template that needs the thumbnail. Change the 'masterpage' parameter of <cms:pages> to point to your template. Also the code above is assuming that the main-image's name is 'gg_image' (a norm with gallery templates). Replace the name with the name of your type 'image' main image that needs the associated thumbnail (this name has been used at *three* locations - make sure you change all).
Explaining what the code does - the shim code simply loads 2 pages at a time, does the processing and moves on to the next 2 pages (as explained in CSV importer
viewtopic.php?f=5&t=8803).
The main processing is what interests us.
It simply saves each existing page twice (using <cms:db_persist>) - once by setting the main-image to blank and then again by setting the image back to its original value which creates the thumbnail.
N.B. It might be a good idea to take a database backup of your site before embarking on any operation that involves large number of pages.Once you have used this importer, remove it from your system by deleting the template.
Incidentally this technique can come in useful for almost any kind of batch operations involving huge number of cloned pages in Couch.
Hope it helps. Do let me know.