StartTypo33D-GraphicAustraliaProjectsPrivateBlog




Typo3 » Tutorials » Development » Create your own Frontend Plugin

Create your own Frontend Plugin

PHP code

The class you have to edit you can find in the path 'typo3conf/ext/image_gallery/pi1'. Open the file with class.tx_imagegallery_pi1.php with your favorite PHP editor and change the class that it looks like:

 

class tx_imagegallery_pi1 extends tslib_pibase {
  var $prefixId = "tx_imagegallery_pi1";
  var $scriptRelPath = "pi1/class.tx_imagegallery_pi1.php";
  var $extKey = "image_gallery";
 
  var $conf;
  var $templateCode;
  var $columns;
 
  /**
  * Image gallery Frontend Plugin
  */
  function main($content,$conf) {
    $this->conf=$conf;
    $this->pi_setPiVarDefaults();
    $this->pi_loadLL();
 
    /**
    * Get the necessary database rows
    */
 
    // 001: Database query
    $query = "SELECT title,image " .
             "FROM tx_imagegallery_images " .
             "WHERE pid=" . $GLOBALS["TSFE"]->id .
    $this->cObj->enableFields("tx_imagegallery_images");

    // 002: Execute query
    $res = mysql(TYPO3_db, $query);
 
 
    /*
    * Generate output
    */
 
    // 003: Get the template
    $this->templateCode =
      $this->cObj->fileResource($this->conf["templateFile"]);
 
    // 004: Get the parts out of the template
    $t = array();
    $t["total"] = $this->cObj->getSubpart($this->templateCode,
      "###GALLERY_TABLE###");
    $t["row"] = $this->cObj->getSubpart($t["total"],
      "###GALLERY_TABLE_ROW###");
    $t["item"] = $this->cObj->getSubpart($t["row"],
      "###GALLERY_TABLE_ITEM###");
 
    // 005: Some values
    $this->columns = $this->conf["columns"];
    $row = mysql_fetch_array($res);
    $content_table = "";
 
    // 006: For all database rows from the query
    while ($row) {
      // 007: Clear content_row
      $content_row = "";
 
      // 008: Collects items for one row
      for ($i = 0; $i < $this->columns; $i++) {
        // 009: Initialise $markerArray
        $markerArray = array();
 
        if ($row) {
          // 010: Create one item
          $img = $this->conf["image."];
          $img["file"] = "uploads/tx_imagegallery/" .
            $row["image"];
          $markerArray["###GALLERY_IMAGE###"] =
            $this->cObj->IMAGE($img);
        } else {
          // 011: Create one dummy icon
          $markerArray["###GALLERY_IMAGE###"] = " ";
        }
 
        // 012: Add item to $content_row
        $content_row .=
          $this->cObj->substituteMarkerArrayCached($t["item"],
          $markerArray, array(), array());
 
        // 013: Fetch new row from the query
        $row = mysql_fetch_array($res);
      }
 
      // 014: Create one row
      $subpartArray = array();
      $subpartArray["###CONTENT_ROW###"] = $content_row;
      $content_table .=
        $this->cObj->substituteMarkerArrayCached($t["row"],
        array(), $subpartArray, array());
    }
 
    // 015: Create the whole table
    $subpartArray = array();
    $subpartArray["###CONTENT###"] = $content_table;
    $content .=
      $this->cObj->substituteMarkerArrayCached($t["total"],
      array(), $subpartArray, array());
 
    // 016: Return the created table
    return $this->pi_wrapInBaseClass($content);
  }
}

 

After inserting the code into the PHP file the extension is ready to use.

 

At least some explanations to the code:

 

To get the correct results with the SQL query you have to use some things from Typo3. First the variable

 

$GLOBALS["TSFE"]->id

 

gives you the pid of the current page, so that you get only the records that are related to this page. And second the function

 

$this->cObj->enableFields("tx_imagegallery_images");

 

This function gives you back a part of the WHERE clause, which deselects the hidden or deleted records.

 

To read some values of the TypoScript configuration of your extension use the variable

 

$this->conf["templateFile"];

 

In this case the value of the key templateFile is read.

 

To handle the template for your extension you use mainly three functions.

 

The function

 

$this->cObj->fileResource("filename");

 

reads the content of the specified filename. If the filename is a image (.jpg, .jpeg, .png, .gif) it gives back a string (<a href="filename"><img></a>) to show that image.

 

With the function

 

$this->cObj->getSubpart($template, "###MARKER###");

 

you can get a part out of the template. In case the marker is '###GALLERY_TABLE_ITEM###' you get everything between the HTML comments <!-- ###GALLERY_TABLE_ITEM### -->, but excluding the comments.

 

To substitute the markers with the content from the database you have to use the function

 

$this->cObj->substituteMarkerArrayCached($template,
  $subpartArray, $markerArray, $wrappedSubpartArray);

 

In the variable $template put the template where you want to substitute the markers. The variable $subpartArray is used to substitute a part between two HTML comments within the template including the comments. Use it like this:

 

$subpartArray["###CONTENT###"] = "This is the content.";

 

The variable $markerArray substitutes a single marker with your Text, use it like this:

 

$markerArray["###NAME###"] = "Karsten Hachmeister";

 

And the variable $wrappedSubpartArray substitutes two corresponding marker, but it don't delete the content between them, use it like this:

 

$wrappedSubpartArray["###EMAIL_LINK###"] =
  array('<a href="mailto:name@domain.com">', '</a>');

 

If you have further questions to this tutorial, please ask them in the forum.

 

For a working example look here.