Item file order and implications for display

Hi folks,

We have altered the show.php file in our theme for a couple of reasons.

1. to display pdfs using pdf.js and pdf.js only
2. to show TIFs in the Docsviewer plugin

The full show.php is available here
but the important sections are as follows:

<!-- prevent pdf files from showing both in the item view and in the plugins /viewers (lines 8 - 11), fix to prevent images being thumbnailed as a knock-on (line 12) -->
<?php if ((get_theme_option('Item FileGallery') == 0) && metadata('item', 'has files')): ?>
<?php echo set_loop_records('files', $item->Files);
          foreach (loop('files') as $file):
           if ($file->getExtension() !='pdf'):
             echo file_markup($file, array('imageSize' => 'fullsize'));
           endif;
          endforeach; ?>
<?php endif; ?>

<?php echo all_element_texts('item'); ?>

<!-- disply pdfs in pdf.js, which should be installed in /var/www/html/ (lines 23 - 24), if the file is not a pdf it can be displayed in plugins as normal (lines 26 -28) -->
<?php if (!$item) $item = get_current_record('item');
    $files = $item->Files;
   foreach($files as $file)
    if ($file->getExtension() =='pdf'){
     echo '<iframe width=100% height=800 src="http://deevy.nuim.ie/pdf.js/web/viewer.html?file=http://deevy.nuim.ie/files/original/'.metadata($file,'filename').'"></iframe>';
   }
    if($file->getExtension() !='pdf'){
    fire_plugin_hook('public_items_show', array('view' => $this, 'item' => $item));
    }
  ?>

This works some of the time but not all of the time. Whether it works or not depends on the order files are attached to the items in.

If the pdf is listed second things work as they should, see here for example: http://deevy.nuim.ie/items/show/88

However if the pdf is the first file listed things go awry.

In this example the file order is pdf followed by jpeg, see screengrab here. The pdf is dispayed in both pdf.js and the docs viewer: http://deevy.nuim.ie/items/show/84

There aren't a lot of items where this is an issue, and editing the item inside Omeka and dragging to change the file order isn't a big deal in this case. However for sharing or reusing the code things could be problematic.

Should item order change things?

*update*
we fixed the problem on our test server by adding a 'return' to our if conditional.

<!-- disply pdfs in pdf.js, which should be installed in /var/www/ (lines 23 - 24), if the file is not a pdf it can be displayed in plugins as normal (lines 26 -28) -->
  <?php if (!$item) $item = get_current_record('item');
    $files = $item->Files;
   foreach($files as $file)
    if ($file->getExtension() =='pdf'){
     echo '<iframe width=100% height=800 src="http://xdeevy.nuim.ie/pdf.js/web/viewer.html?file=http://xdeevy.nuim.ie/files/original/'.metadata($file,'filename').'"></iframe>';
    return;
    }
    else if($file->getExtension() !='pdf'){
    fire_plugin_hook('public_items_show', array('view' => $this, 'item' => $item));
    }
  ?>

This solves our immediate problem in this case but we still wonder how item files order changed things, and would appreciate it if anyone could shed light on this for us.

I'd have to do a lot more digging, but here's what I suspect is happening. The weird thing is that I would have expected to always see the pdf in DocsViewer no matter what.

That's because when you do fire_plugin_hook to bring in the DocsViewer, the $item is passed in, and DocsViewer tries to display something for all the files on that item. So, since that line always get fired eventually, I would have thought that the pdf would always get into the DocsViewer.

My first guess, then, is that there are colliding loops. DocsView also does a

foreach($files as $file) {
//stuff
}

Maybe the variables are colliding in a funny way? Like I said, that's just my first guess.

To test it, I suppose you could change the name of $file to something else in your loop, and see if it behaves better.

I think you're right that the pdf will show if DocsView is fired / called. We tried

if($file->getExtension() =='TIF'){

instead of

if($file->getExtension() !='pdf'){

and the pdf would display anyway. My colleague Ranju is working on this so I will forward your test suggestion on.

You probably want a break, not a return. With return, nothing after it in your show.php, like the page's footer, will be printed.

Oops! thanks for that John, we literally didn't notice anything past the pdf displaying.