Snippety

Snippet je malý znovupoužitelný kus programového kódu, který může např.:

Přístup k databázi

Příklad zobrazení požadovaných údajů přímo z databáze:

<?php
  $sql = "SELECT title FROM node ORDER BY node.vid DESC LIMIT 10";
  $result = db_query($sql);
  while ($node = db_fetch_object($result)) {
    $output .= $node->title ." ";
  }
  return $output;
?>

Pro zobrazení je možné využít např. vestavěný PHP filtr obsahu nebo bloku.

Drupal 5

Snippety pro Drupal 5:

Navigace v galerii obrázků

Popis

Doplnění textové navigace do galerie obrázků modulu Image.

Jako základ bylo použito řešení Add a << first < previous next > last >> Pager to Image Nodes Within a Gallery doplněné o lokalizaci.

Postup

  1. Do suboru template.php používaného vzhledu přidejte funkci:
    /**
     * Image Gallery Pager
     */
    function custom_pager($current_nid, $class = NULL) {
      $tid = reset(array_keys(taxonomy_node_get_terms($current_nid)));
      $result = db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = %s AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC, n.nid DESC'), $tid);
      while ($node = db_fetch_object($result)) {
        $nodes[++$i] = $node;
        if ($node->nid == $current_nid) $x = $i;
      }
      if($x > 1) {
        $output .= l(t('« first'), 'node/'. $nodes[1]->nid, array('title' => check_plain($nodes[1]->title), 'class' => $class), NULL, NULL, FALSE, TRUE);
        $output .= l(t('‹ previous'), 'node/'. $nodes[$x-1]->nid, array('title' => check_plain($nodes[$x-1]->title), 'class' => $class), NULL, NULL, FALSE, TRUE);
      }
      else {
        // not required
        // $output .= '<span class="">' . t(t('« first')) . ' </span>' . '<span class="">' . t('‹ previous') . ' </span>';
      }
      $output .= $x .' ('. $i .')';
      if($x < $i) {
        $output .= l(t('next ›'), 'node/'. $nodes[$x+1]->nid, array('title' => check_plain($nodes[$x+1]->title), 'class' => $class), NULL, NULL, FALSE, TRUE);
        $output .= l(t('last »'), 'node/'. $nodes[$i]->nid, array('title' => check_plain($nodes[$i]->title), 'class' => $class), NULL, NULL, FALSE, TRUE);
      }
      else {
        // not required
        // $output .= '<span class="">' . t(t('next ›')) . ' </span>' . '<span class="">' . t(t('last »')) . ' </span>';
      }
      return $output; 
    }
  2. Pokud v adresáři používaného vzhledu nemáte soubor node-image.tpl.php:
    • Zkopírujte např. soubor themes/garland/node.tpl.php do adresáře používaného vzhledu a přejmenujte jej na node-image.tpl.php.
  3. Do souboru node-image.tpl.php přidejte na vhodné místo (nejlépe před "obsah"):
    <!-- Image Gallery Pager -->
    <div class="pager">
      <?php if ($page != 0 && $terms) { print custom_pager($node->nid); } ?>
    </div>

 

Navigace v galerii obrázků s náhledy

Doplnění navigace s náhledy do galerie obrázků u modulu Image.

Jako základ bylo použito řešení Pager with thumbnails doplněné o lokalizaci, číslování a velikosti náhledů.

  1. Do suboru template.php používaného vzhledu přidejte funkci:
    /**
     * Image Gallery Pager with thumbnails
     */
    function custom_pager_thumbnails($current_nid, $class = NULL) {
      $tid = reset(array_keys(taxonomy_node_get_terms($current_nid)));
      $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, f.filepath FROM {node} n INNER JOIN {term_node} tn INNER JOIN {files} f ON n.nid = tn.nid AND n.nid = f.nid WHERE tn.tid = %s AND n.status = 1 AND f.filename = \'%s\' ORDER BY n.sticky DESC, n.created DESC, n.nid DESC'), $tid, 'thumbnail');
      while ($node = db_fetch_object($result)) {
        $nodes[++$i] = $node;
        if ($node->nid == $current_nid) $x = $i;
        // image size
        $image_info = image_get_info($nodes[$i]->filepath);
        $image_width[$i] = $image_info['width'];
        $image_height[$i] = $image_info['height'];
      }
      if ($i < 6) {
        while (++$j <= $i) {
          $output .= l('<img src="' . check_url(url('system/files/' . $nodes[$j]->filepath)) . '" alt="'. check_plain($nodes[$j]->title) . '" width="'. $image_width[$j] .'" height="'. $image_height[$j] .'" />', 'node/'. $nodes[$j]->nid, array('title' => check_plain($nodes[$j]->title), 'class' => $class), NULL, NULL, FALSE, TRUE);
        }
      }
      else {
        switch ($x) {
          case 1:
            // first image
            $y = array($i-1, $i, 1, 2, 3);
            break;
          case 2:
            // second image
            $y = array($i, 1, 2, 3, 4);
            break;
          case $i-1:
            // penultimate image
            $y = array($x-2, $x-1, $x, $i, 1);
            break;
          case $i:
            // last image
            $y = array($x-2, $x-1, $x, 1, 2);
            break;
          default:
            $y = array($x-2, $x-1, $x, $x+1, $x+2);
        }
        foreach($y as $index => $z) {
          if ($index != 2) {
            $output .= l('<img src="' . check_url(url('system/files/' . $nodes[$z]->filepath)) . '" alt="'. check_plain($nodes[$z]->title) . '" width="'. $image_width[$z] .'" height="'. $image_height[$z] .'" />', 'node/'. $nodes[$z]->nid, array('title' => '('. ($z) .') '. check_plain($nodes[$z]->title), 'class' => $class), NULL, NULL, FALSE, TRUE);
          }
          else {
            $output .= '<span class="active" title="('. ($z) .') '. check_plain($nodes[$z]->title) .'"><img src="' . check_url(url('system/files/' . $nodes[$z]->filepath)) . '" alt="'. check_plain($nodes[$z]->title) . '" width="'. $image_width[$z] .'" height="'. $image_height[$z] .'" /></span>';
          }
        }
      }
      return $output;
    }
  2. Pokud v adresáři používaného vzhledu nemáte soubor node-image.tpl.php:
    • Zkopírujte např. soubor themes/garland/node.tpl.php do adresáře používaného vzhledu a přejmenujte jej na node-image.tpl.php.
  3. Do souboru node-image.tpl.php přidejte na vhodné místo (nejlépe před "obsah"):
    <!-- Image Gallery Pager with thumbnails -->
    <div class="pager">
      <?php if ($page != 0 && $terms) { print custom_pager_thumbnails($node->nid); } ?>
    </div>
  4. Do souboru s kaskádovými styly používaného vzhledu (style.css) můžete vložit např.:
    /* Theme snippet: Image Gallery Pager with thumbnails */
    #content .pager {
      margin-top: 3px;
      margin-bottom: 3px;
    }
    #content .pager img {
      margin-bottom: 3px;
      vertical-align: middle;
    }
    #content .pager .active img {
      border: 5px solid #f93;
      margin-bottom: 0;
    }