Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

generate thumbnails in frontside template


Reply


7 Posts   1962 Views

Avatar
dacar

Community Member, 168 Posts

27 January 2010 at 4:52am

Hi,

there is still - i think a very important - thing that i still haven't understand: In my holder page i refer to my detail objects

class ProjektCategory extends Page {
      
   static $allowed_children = array(
      'Projekt'
   );
   
   static $has_many = array(
         'Projekte' => 'Projekt',
         'Stammdaten' => 'Stammdaten'
      );

in the detail page i manage the main informations and some references to different tables (e.g.)

<?php

class Projekt extends Page {
      static $has_one = array(
         'ProjektCategory' => 'ProjektCategory',
         'ProjektImage' => 'Projekt_Thumbnail'
      );
      
      static $db = array(
      'Monat'    => 'Text',
      'Datum' => 'Date',
      'Kunde' => 'Int',
      'Bereich' => 'Text',
      'CopytextHome'    => 'HTMLText',
      'HeadlineHome'    => 'Text'
      );
   

   function getCMSFields() {
      $fields = parent::getCMSFields();      
      //Felder entfernen
      $fields->removeFieldFromTab('Root.Content.Main', 'RedirectToChild');
      $fields->removeByName("Kopfbilder");
      $fields->removeByName("Banner");
      $fields->addFieldToTab("Root.Content.Main", new ImageField("ProjektImage", "Vorschaubild", null, null, null, "assets/Projektbilder/"));
      //$fields->addFieldToTab('Root.Content.Main', new ImageField('ProjektImage'));
      $fields->addFieldToTab('Root.Content.Main', new TextField('Monat'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', new CalendarDateField('Datum'), 'Content');
      //$fields->addFieldToTab('Root.Content.Main', new DropdownField('PageColor', 'Page Color', self::$colors), $this->PageColor);
      //$fields->addFieldToTab('Root.Content.Main', new TextField('Bereich'), 'Content');
      
      $kunden = Dataobject::get("Stammdaten")->toDropdownMap("ID", "Firmenname");
      // DropdownField('field name', 'field titel', quelle, "the current value", the parent formular, "Empty Value Title");
      $fields->addFieldToTab("Root.Content.Main", new DropdownField(
      'Kunde',
      'Firmenname',
      $kunden,
      null,
      null,
      'Bitte eine Firma waehlen'
      ), 'Content');
      
      $bereich = Dataobject::get("SiteTree", "ClassName = 'ReferenzCategory'")->toDropdownMap("ID", "URLSegment");
      $fields->addFieldToTab("Root.Content.Main", new DropdownField(
      'Bereich',
      'Projektbereich',
      $bereich,
      null,
      null,
      'Bitte einen Bereich waehlen'
      ), 'Content');
      
      $fields->addFieldToTab('Root.Content.Main', new TextField('HeadlineHome'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', new TextareaField('CopytextHome'), 'Content');
      return $fields;
   }
   
}

'ProjektImage' is saved to the file table:

class Projekt_Thumbnail extends Image {
   
   public    function generateThumb($gd) {
      $gd = $gd->resizeByWidth(150);

      $gd->setQuality(100);
      return $gd;
      }
   }

in my .ss file i want to show the rendered informations:

<% control getJahresartikel %>   
<div class="absatz">
<div class="marginalspalte" style="margin-top:0px; font-weight:bold;">
   <img src="$ProjektImage.Thumb.URL" title="$FileFilename" alt="$FileFilename" class="" /></div>
<div class="contenspalte">
   $Monat<br />
Thema: $Title<br />
Kunde: $Firmenname<br />
Lesen sie <a href="$URLSegment" title="Lesen sie mehr in der Rubrik $SiteTreeURLSegment." class="$LinkingMode"><span>hier</span></a> weiter.
</div>
</div>
<% end_control %>

All data is passed to the template correctly, but the thumbnail will not be generated??
I have tried to ways to build the query

function getJahresartikel() {
   $jahr = $this->URLParams['URLSegment'];
   
   $sqlQuery = DB::query("SELECT p.`ID`, p.`Datum`, DATE_FORMAT(p.`Datum`, '%M') AS ProjektMonat, p.`Kunde`, p.`Bereich`, p.`Monat`, p.`ProjektImageID`, s.`ID`, s.`Title`, s.`URLSegment`, st.`ID`, st.`Firmenname`, f.`ID`, f.`Filename`, f.`Name`, f.`ClassName` FROM `Projekt` p LEFT JOIN `SiteTree` s ON p.`ID` = s.`ID` LEFT JOIN `File` f ON f.`ID` = p.`ProjektImageID` LEFT JOIN `Stammdaten` st ON st.`ID` = p.`Kunde` WHERE DATE_FORMAT(p.`Datum`, '%Y') = ".$jahr."");
      //debug::show($sqlQuery); die;

      $firmen = new DataObjectSet();
       foreach($sqlQuery as $row) {
         $firmen->push(new ArrayData($row));
      }          
      //var_dump($firmen);
      return $firmen;
   }

and

function getJahresartikel() {
   $jahr = $this->URLParams['URLSegment'];
   
   $sqlQuery = new SQLQuery();
      $sqlQuery->select = array(
      'Projekt.ID AS ProjektID',
      'Projekt.Datum AS ProjektDatum',
      'DATE_FORMAT(Projekt.Datum, "%M") AS ProjektMonat',
      'Projekt.Kunde AS ProjektKunde',
      'Projekt.Bereich AS ProjektBereich',
      'Projekt.Monat AS Monat',
      'Projekt.ProjektImageID AS ProjektImage',
      //
      'SiteTree.ID AS SiteTreeID',
      'SiteTree.Title AS SiteTreeTitle',
      'SiteTree.URLSegment AS SiteTreeURLSegment',
      //
      'Stammdaten.ID AS ID',
      'Stammdaten.Firmenname AS Firmenname',
      //
      'Stammdaten.ClassName AS ClassName',
      'Stammdaten.ClassName AS RecordClassName',
      
      'File.ID AS FileID',
      'File.Filename AS FileFilename'
      );
      $sqlQuery->from = array(
      "Projekt",
      "LEFT JOIN Stammdaten ON Stammdaten.ID = Projekt.Kunde LEFT JOIN SiteTree ON SiteTree.ID = Projekt.ID LEFT JOIN File ON File.ID = Projekt.ProjektImageID"
      );

      //debug::show($sqlQuery); die;
      $result = $sqlQuery->execute();
      $firmen = new DataObjectSet();
       foreach($result as $row) {
         $firmen->push(new ArrayData($row));
      }          
      //var_dump($firmen);
      return $firmen;
   }
   
}

Can somebody explain how to get into the right scope and how to generate the thumb?

Thanks, Carsten

Avatar
Willr

Forum Moderator, 5513 Posts

27 January 2010 at 11:48am

I dont think the template engine supports 3 levels like $ProjektImage.Thumb.URL. You might have to break that into a control

<% control ProjektImage.Thumb %>
$URL
<% end_control %>

You might want to try just $ProjektImage and make sure that works as well.

Avatar
dacar

Community Member, 168 Posts

28 January 2010 at 2:36am

Hi Willr,

but in the SilverStripe documentation they use those 3 levels, too: [url]http://doc.silverstripe.org/doku.php?id=recipes:imageupload[/url]?!

Avatar
dacar

Community Member, 168 Posts

28 January 2010 at 3:36am

Hi Willr,

i have just tested your suggested control

<% control ProjektImage.Thumb %>
$URL
<% end_control %>

Without success. if i only use $ProjektImage then i get back the ProjektImageID from "Projekte". Is it correct that this ID is passed to the function (public    function generateProjektBanner($gd) {)?

Thanks a lot for your help, Carsten.

Avatar
dacar

Community Member, 168 Posts

28 January 2010 at 4:41am

... i have just made a debug::show($gd) on an other part of the website where the thumbs are generated corretly:

GD::__set_state(array(
'gd' => NULL,
'width' => 354,
'height' => 354,
'quality' => 75,
'class' => 'GD',
'extension_instances' =>
array (
),
))

.

Then i have changed the working $LogoGesamtuebersicht.ThumbLogoColorDetail.URL to $LogoGesamtuebersicht. But surprise: it is not the id rendered in the template it is the original image!

In the "File" DB table the classname of the uploaded file is correct (Projekt_BannerImage). Please Help...

Avatar
dacar

Community Member, 168 Posts

29 January 2010 at 4:48am

Hi,

i have simply changed my function from DB::query to DataObject::get and now the thumb is generated. What is the difference?

function getJahresartikel() {
$jahr = $this->URLParams['URLSegment'];
$projektdesmonats = DataObject::get("Projekt", "", "Projekt.ID DESC", "LEFT JOIN Stammdaten ON Stammdaten.ID = Projekt.Kunde LEFT JOIN File ON File.ID = Projekt.ProjektImageID", "");
return $projektdesmonats;
}

But now there is an other problem, that let me step back to the first and main question of my thread.

Some data that i want to show in the template is saved to a different table (Stammdaten). With DataObject::get you can not use any joined data. So i have to use a nested control. But how can i pass any id from the first control to the function of the second control? Where is the scope? How to use controls if they are nested?

<% control getJahresartikel %>   
<img src="$ProjektImage.ProjektThumb.URL" title="$URLSegment" alt="$URLSegment" class="" />
$Monat<br />
Thema: $Title<br />
Kunde: <% control Top.Kunde %>$Firmenname<% end_control %><br />
<% end_control %>

The function is in ProjectCategory.php:

<?php

class ProjektCategory extends Page {
      
   static $allowed_children = array(
      'Projekt'
   );
   
   static $has_many = array(
         'Projekte' => 'Projekt',
         'Stammdaten' => 'Stammdaten',
         'ProjektImage' => 'Projekt_ProjektBannerImage'
      );

[...]

function getJahresartikel() {
      $jahr = $this->URLParams['URLSegment'];
      $projektdesmonats = DataObject::get("Projekt", "", "Projekt.ID DESC", "LEFT JOIN Stammdaten ON Stammdaten.ID = Projekt.Kunde LEFT JOIN File ON File.ID = Projekt.ProjektImageID", "");

      return $projektdesmonats;
   }
   
         
   public function Kunde() {
      // $ID = 1;
      // debug::show($ID);
      $firmenname = DataObject::get("Stammdaten", "Stammdaten.ID = ".$ID."", "", "", "");
      return $firmenname;
   }

Hope that someone can give a good hint. Greetings, Carsten.

Avatar
dacar

Community Member, 168 Posts

30 January 2010 at 1:17am

Unfortunately i still can't get it working :-(

-> I am inside my ProjectCategory.ss Template.
-> ProjektCategory extends Page.
-> in my template i use nested controls:

<% control getJahresartikel %>   
(...)
$Monat<br />
(...)
<% control Top.Kunde %>$Firmenname<% end_control %>
(...)
<% end_control %>

-> all data within the first control is rendered correctly.
-> class Projekt extends Page and has_one 'Stammdaten'

   public function Kunde() {
      $ID = ????;
      debug::show($this->Stammdaten());
      $firmenname = DataObject::get("Stammdaten", "Stammdaten.ID = ".$ID."", "", "", "");
      return $firmenname;
   }

How can i tell the function "Kunde" that the ID is the ID from the current Data looping in the outer control???
$this-> will allway be the SiteTree.ID and not the StammdatenID from getJahresartikel:

   function getJahresartikel() {
      $jahr = $this->URLParams['URLSegment'];
      $projektdesmonats = DataObject::get("Projekt", "", "Projekt.ID DESC", "LEFT JOIN Stammdaten ON Stammdaten.ID = Projekt.Kunde LEFT JOIN File ON File.ID = Projekt.ProjektImageID", "");
      //Debug::show(Referenzen); die;
      return $projektdesmonats;
   }

Please help. Carsten.