Sample Image Template

From TechWiki

Jump to: navigation, search

Here is a sample Drupal template page in PHP for an image layout. The nominal source file is page-image.tpl.php:

<?php
 
  global $base_url;
 
  $wsfAddress = "http://npidev.structureddynamics.com/ws/";
  $imagesDatasetUri = "http://npidev.structureddynamics.com/datasets/images/";
 
  $topicConcepts = array();
  $relatesToEntities = array();
  $page = 0;
  $itemsPerPage = 30;
 
  if(isset($_GET["page"]))
  {
    $page = $_GET["page"];
  }
 
  $indicators = "[";
 
  if(isset($node->field_images_topic_uri))
  {
    foreach($node->field_images_topic_uri as $field)
    {
      if($field["value"] !== NULL && $field["value"] != "")
      {
        $start = strrpos($field["value"], "(") + 1;
        $end = strrpos($field["value"], ")");
        $uri = substr($field["value"], $start, ($end - $start));  
 
        array_push($topicConcepts, $uri);
      }
    }
  }
 
  if(isset($node->field_images_relates_to_uri))
  {
    foreach($node->field_images_relates_to_uri as $field)
    {
      if($field["value"] !== NULL && $field["value"] != "")
      {
        $start = strrpos($field["value"], "(") + 1;
        $end = strrpos($field["value"], ")");
        $uri = substr($field["value"], $start, ($end - $start));  
 
        array_push($relatesToEntities, $uri);
      }
    }
  }
 
  include_once($base_url . "/" . drupal_get_path("module", "conStruct") . "/framework/WebServiceQuerier.php");
  include_once($base_url . "/" . drupal_get_path("module", "conStruct") . "/framework/ProcessorXML.php");
 
  $wsq;
 
  if(count($topicConcepts) > 0 && count($relatesToEntities) > 0)
  {    
    $filters = "";
 
    foreach($topicConcepts as $tc)
    {
      $filters .= urlencode("http://xmlns.com/foaf/0.1/topic")."::".urlencode($tc) . ";";
    }
 
    foreach($relatesToEntities as $re)
    {
      $filters .= urlencode("http://purl.org/ontology/npi#relatesTo")."::".urlencode($re) . ";";
    }    
 
    $filters = trim($filters, ";");    
 
    $wsq = new WebServiceQuerier($wsfAddress . "search/", "post", "text/xml", 
                                 'attributes='.$filters.
                                 '&items='.$itemsPerPage.
                                 '&page='.($itemsPerPage * $page).
                                 '&include_aggregates=true'.
                                 '&datasets='.urlencode($imagesDatasetUri)); 
  }
  elseif(count($topicConcepts) > 0)
  {
    $filters = "";
 
    foreach($topicConcepts as $tc)
    {
      $filters .= urlencode("http://xmlns.com/foaf/0.1/topic")."::".urlencode($tc) . ";";
    }
 
    $filters = trim($filters, ";");
 
    $wsq = new WebServiceQuerier($wsfAddress . "search/", "post", "text/xml", 
                                 'attributes='.$filters.
                                 '&items='.$itemsPerPage.
                                 '&page='.($itemsPerPage * $page).
                                 '&include_aggregates=true'.
                                 '&datasets='.urlencode($imagesDatasetUri)); 
  }  
  elseif(count($relatesToEntities) > 0)
  {
    $filters = "";
 
    foreach($relatesToEntities as $re)
    {
      $filters .= urlencode("http://purl.org/ontology/npi#relatesTo")."::".urlencode($re) . ";";
    }
 
    $filters = trim($filters, ";");    
 
    $wsq = new WebServiceQuerier($wsfAddress . "search/", "post", "text/xml", 
                                 'attributes='.$filters.
                                 '&items='.$itemsPerPage.
                                 '&page='.($itemsPerPage * $page).
                                 '&include_aggregates=true'.
                                 '&datasets='.urlencode($imagesDatasetUri)); 
  }  
 
  $images = array();
 
  if ($wsq->getStatus() != 200)
  {
    $wsq->displayError();
  }
  else
  {
    $xml = new ProcessorXML();
    $xml->loadXML($wsq->getResultset());
 
    $nbImages = 0;
 
    $aggregates = $xml->getSubjectsByType("aggr:Aggregate");
 
    foreach ($aggregates as $aggregate)
    {
      $aggregetionType = $xml->getPredicatesByType($aggregate, "aggr:property");
      $aggregetionTypeObj = $xml->getObjects($aggregetionType->item(0));
 
      switch ($xml->getURI($aggregetionTypeObj->item(0)))
      {
        case "http://rdfs.org/ns/void#Dataset":
          $aggregateObject = $xml->getPredicatesByType($aggregate, "aggr:object");
          $aggregateObjectObj = $xml->getObjects($aggregateObject->item(0));
 
          $aggregateCount = $xml->getPredicatesByType($aggregate, "aggr:count");
          $aggregateCountObj = $xml->getObjects($aggregateCount->item(0));
 
          $nbImages = $xml->getContent($aggregateCountObj->item(0));
        break;
      }
    }  
 
    $subjects = $xml->getSubjects();
 
    $recordsUriList = "";
    $recordsDatasetUriList = "";
 
    foreach ($subjects as $subject)
    {
      $recordType = $xml->getType($subject, FALSE);
 
      if($recordType == "http://xmlns.com/foaf/0.1/Image")
      {                                                  
        $recordUri = $xml->getURI($subject);
        $images[$recordUri] = array();
 
        $predicates = $xml->getPredicates($subject);
 
        foreach ($predicates as $predicate)
        {
          $object = $xml->getObjects($predicate)->item(0);
 
          switch($xml->getType($predicate, FALSE))
          {
            case "http://xmlns.com/foaf/0.1/topic":
              $images[$recordUri]["topic"] = $xml->getURI($object);
            break;
            case "http://purl.org/ontology/iron#prefLabel":
              $images[$recordUri]["prefLabel"] = $xml->getContent($object);
            break;
            case "http://purl.org/ontology/iron#description":
              $images[$recordUri]["description"] = $xml->getContent($object);
            break;
            case "http://purl.org/ontology/npi#relatesTo":
              $images[$recordUri]["relatesTo"] = $xml->getURI($object);
            break;
            case "http://purl.org/ontology/npi#imageUrl":
              $images[$recordUri]["imageUrl"] = $xml->getContent($object);
            break;
            case "http://purl.org/ontology/npi#thumbnailUrl":
              $images[$recordUri]["thumbnailUrl"] = $xml->getContent($object);
            break;
            case "http://purl.org/ontology/npi#credit":
              $images[$recordUri]["credit"] = $xml->getContent($object);
            break;
            case "http://purl.org/ontology/npi#creditUrl":
              $images[$recordUri]["creditUrl"] = $xml->getContent($object);
            break;
          }
        }
      }                                                     
    }    
  }    
 
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="<?php print $language->language; ?>" xml:lang="<?php print $language->language; ?>">
 
  <head>
    <title><?php print $head_title; ?></title>
    <?php print $styles; ?>
    <!--[if IE 7]>
      <link rel="stylesheet" href="<?php print $base_path . $directory; ?>/ie7-fixes.css" type="text/css">
    <![endif]-->
    <!--[if lte IE 6]>
      <link rel="stylesheet" href="<?php print $base_path . $directory; ?>/ie6-fixes.css" type="text/css">
    <![endif]-->
    <?php print $scripts; ?>
    <?php print $head; ?>
 
 
<?php
    if(count($images) > 0)
    {
?>    
 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" language="javascript"></script>
    <script src="<?php print base_path() . path_to_theme() ?>/js/jquery.ad-gallery.js" language="javascript"></script>
 
    <link rel="stylesheet" type="text/css" href="<?php print base_path() . path_to_theme() ?>/css/jquery.ad-gallery.css?f" />
 
  <script type="text/javascript">
 
  var nbImages = <?php echo $nbImages; ?> ;
  var currentPage = <?php echo $page; ?> ;
  var nbItemsPerPage = <?php echo $itemsPerPage; ?> ;
 
  $(function() {
<?php
 
    $nb = 0;
    foreach($images as $image)
    {
      if(isset($image["prefLabel"]) && $image["prefLabel"] != "")
      {
        echo '$(\'img.image'.$nb.'\').data(\'ad-title\', \''.str_replace("'", "\'", $image["prefLabel"]).'\');';
      }
 
      if(isset($image["description"]) && $image["description"] != "")
      {
        echo '$(\'img.image'.$nb.'\').data(\'ad-desc\', \''.str_replace("'", "\'", $image["description"]).'\');';
      }
 
      $nb++;
    }
 
?>    
 
    var galleries = $('.ad-gallery').adGallery({
      loader_image: '<?php print base_path() . path_to_theme() ?>/images/loader.gif',
      slideshow: {enable: false}
    });
 
 
    $('#switch-effect').change(
      function() {
        galleries[0].settings.effect = $(this).val();
        return false;
      }
    );
    $('#toggle-slideshow').click(
      function() {
        galleries[0].slideshow.toggle();
        return false;
      }
    );
    $('#toggle-description').click(
      function() {
        if(!galleries[0].settings.description_wrapper) {
          galleries[0].settings.description_wrapper = $('#descriptions');
        } else {
          galleries[0].settings.description_wrapper = false;
        }
        return false;
      }
    );
  });
  </script>
 
  <style type="text/css">
 
    #gallery {
      padding: 30px;
      /*background: #e1eef5;*/
    }
 
    #descriptions {
      position: relative;
      height: 50px;
      background: #EEE;
      margin-top: 10px;
      width: 640px;
      padding: 10px;
      overflow: hidden;
    }
 
    #descriptions .ad-image-description {
      position: absolute;
    }
 
    #descriptions .ad-image-description .ad-description-title {
      display: block;
    }
 
  </style>    
<?php
    }
?>    
 
  </head>
 
  <body class="<?php print $body_classes; ?>">
    <div id="page" class="clearfix">
 
      <div id="header">
        <div id="header-wrapper" class="clearfix">
 
          <div id="header-top" class="clearfix">
            <?php if ($primary_links): ?>
          <div id="primary-menu">
            <?php print $primary_links_tree; ?>
          </div><!-- /primary_menu -->
          <?php endif; ?>
          </div><!-- /header-top --> 
 
          <?php if ($search_box): ?>
          <div id="search-box">
            <?php print $search_box; ?>
          </div><!-- /search-box -->
          <?php endif; ?>
 
          <div id="header-first">
            <?php if ($logo): ?> 
            <div id="logo">
              <a href="<?php print $base_path ?>" title="<?php print t('Home') ?>"><img src="<?php print $logo ?>" alt="<?php print t('Home') ?>" /></a>
            </div>
            <?php endif; ?>
          </div><!-- /header-first -->
 
          <div id="header-middle">
            <?php if ($header_middle): ?>
            <?php print $header_middle; ?>
            <?php endif; ?>
            <?php if ($site_name): ?>
		    <div id="title"> 
              <a href="<?php print $base_path ?>" title="<?php print t('Home'); ?>"><?php print $site_name; ?></a>
			</div>
            <?php endif; ?>
            <?php if ($site_slogan): ?>
            <div id="slogan">
			  <?php print $site_slogan; ?>
			</div>
            <?php endif; ?>	
            <?php if ($mission): ?>
            <div id="mission"> 
              <?php print $mission; ?>
            </div>
            <?php endif; ?>			
          </div><!-- /header-middle -->
 
          <div id="header-last">
            <?php if ($header_last): ?>
            <?php print $header_last; ?>
            <?php endif; ?>
          </div><!-- /header-last -->
 
        </div><!-- /header-wrapper -->
 
        <div id="header-bottom" class="clearfix">
        </div><!-- /header-bottom -->
 
		</div><!-- /header -->
 
      <div id="preface">
        <?php if ($preface_first || $preface_middle || $preface_last): ?>
        <div id="preface-wrapper" class="<?php print $prefaces; ?> clearfix">
 
          <?php if ($preface_first): ?>
          <div id="preface-first">
            <?php print $preface_first; ?>
          </div><!-- /preface-first -->
          <?php endif; ?>
 
          <?php if ($preface_middle): ?>
          <div id="preface-middle">
            <?php print $preface_middle; ?>
          </div><!-- /preface-middle -->
          <?php endif; ?>
 
          <?php if ($preface_last): ?>
          <div id="preface-last">
            <?php print $preface_last; ?>
          </div><!-- /preface-last -->
          <?php endif; ?>
        </div><!-- /preface-wrapper -->
        <?php endif; ?>
      </div><!-- /preface -->
 
	  <div id="tri-bar" class="clearfix">
	    <div id="tri-bar-1">&nbsp;</div>
		<div id="tri-bar-2">&nbsp;</div>
	  </div><!-- /tri-bar -->
 
      <div id="main">
        <div id="main-wrapper" class="clearfix">
 
          <?php if ($breadcrumb): ?>
          <div id="breadcrumb">
            <?php print $breadcrumb; ?>
          </div><!-- /breadcrumb -->
          <?php endif; ?>
 
          <?php if ($sidebar_first): ?>
          <div id="sidebar-first">
            <?php print $sidebar_first; ?>
          </div><!-- /sidebar-first -->
          <?php endif; ?>
          <div id="content-wrapper">
 
            <?php if ($messages): ?>
              <?php print $messages; ?>
            <?php endif; ?>
 
            <?php if ($content_top): ?>
            <div id="content-top">
              <?php print $content_top; ?>
            </div><!-- /content-top -->
            <?php endif; ?>
 
            <div id="content">
              <?php if ($tabs): ?>
              <div id="content-tabs">
                <?php print $tabs; ?>
              </div>
              <?php endif; ?>
 
              <div id="content-inner">
              <?php if ($help): ?>
                <div id="help">
                  <?php print $help; ?>
                </div>
              <?php endif; ?>
 
                <?php /* if ($title): ?>
                <h1 class="title"><?php print $title; ?></h1>
                <?php endif;*/ ?>
 
                <?php
                  if(stripos($_GET['q'], "/edit") !== FALSE)
                  {
                ?>
 
                <div id="content-content">
                  <?php print $content; ?>
                </div>
 
                <?php
                  }
                ?>
 
 
      </div><!-- /content-inner -->
            </div><!-- /content -->
 
            <?php
              if(stripos($_GET['q'], "/edit") === FALSE && count($images) > 0)
              {
            ?>
 
              <div id="gallery" class="ad-gallery">
                <div class="ad-image-wrapper">
                </div>
                <div class="ad-controls">
                </div>
                <div class="ad-nav">
                  <div class="ad-thumbs">
                    <ul class="ad-thumb-list" id="ad-thumb-list">                      
 
<?php
                      if($nbImages > $itemsPerPage && $page > 0)
                      {
                        echo '<li style="height: 61px;position:relative;background:none;width:61px;">
                                <img src="'. base_path() . path_to_theme() .'/images/previous.png" id="previousButtonImage"
                                     style="position:absolute;top: 10px;cursor:pointer;"
                                     title="Previous page of images...">
                              </li>';
                      }
 
 
                      $nb = 0;
                      foreach($images as $image)
                      {
                        echo '<li>
                                <a href="'.$image["imageUrl"].'">
                                  <img src="'.$image["thumbnailUrl"].'" class="image'.$nb.'">
                                </a>
                              </li>';
 
                        $nb++;
                      }  
 
                      if($nbImages > $itemsPerPage && ((($page + 1) * $itemsPerPage) < $nbImages))
                      {
                        echo '<li style="height: 61px;position:relative;background:none;width:61px;">
                                <img src="'. base_path() . path_to_theme() .'/images/next.png" id="nextButtonImage"
                                     style="position:absolute;top: 10px;cursor:pointer;"
                                     title="Next page of images...">
                              </li>';
                      }
 
?>                                        
                    </ul>
                  </div>
                </div>
              </div>  
 
              <script type="text/javascript">
                $("#ad-thumb-list").attr("width", "9000");
 
                $('#nextButtonImage').click(function() {
                  window.location.href = "?page=" + (currentPage + 1);
                });
 
                $('#previousButtonImage').click(function() {
                  window.location.href = "?page=" + (currentPage - 1);
                });
 
              </script>
 
          <?php
            }
          ?>
 
            <?php
              if(stripos($_GET['q'], "/edit") === FALSE && count($images) <= 0)
              {
                echo "No images available";
              }
            ?>
 
 
 
            <?php if ($content_bottom): ?>
            <div id="content-bottom">
              <?php print $content_bottom; ?>
            </div><!-- /content-bottom -->
            <?php endif; ?>
 
 
		</div><!-- /content-wrapper -->
 
                <?php if ($sidebar_last): ?>
                <div id="sidebar-last">
                  <?php print $sidebar_last; ?>
                </div><!-- /sidebar_last -->
                <?php endif; ?>
 
 
          <?php if ($postscript_first || $postscript_middle || $postscript_last): ?>
          <div id="postscript-wrapper" class="<?php print $postscripts; ?> clearfix">
            <?php if ($postscript_first): ?>
            <div id="postscript-first" class="column">
              <?php print $postscript_first; ?>
            </div><!-- /postscript-first -->
            <?php endif; ?>
 
            <?php if ($postscript_middle): ?>
            <div id="postscript-middle" class="column">
              <?php print $postscript_middle; ?>
            </div><!-- /postscript-middle -->
            <?php endif; ?>
 
            <?php if ($postscript_last): ?>
            <div id="postscript-last" class="column">
              <?php print $postscript_last; ?>
            </div><!-- /postscript-last -->
            <?php endif; ?>
          </div><!-- /postscript-wrapper -->
          <?php endif; ?>
 
          <?php print $feed_icons; ?>   
 
        </div><!-- /main-wrapper -->
      </div><!-- /main -->
      <?php if ($footer_top || $footer || $footer_message): ?>
        <div id="footer" class="clearfix">
          <?php if ($footer_top): ?>
          <?php print $footer_top; ?>
          <?php endif; ?>
          <?php if ($footer): ?>
          <?php print $footer; ?>
          <?php endif; ?>
          <?php if ($footer_message): ?>
          <?php print $footer_message; ?>
          <?php endif; ?>
        </div><!-- /footer -->
	  <?php endif; ?>
	  </div><!-- /page -->
    <?php print $closure; ?>
  </body>
</html>
Personal tools