Ich habe einmal den kompletten Code unten aufgeführt. Zuerst das Input-, dann das Output-Modul:
Code: Alles auswählen
?><?php
/**
* Module-Input: Slideshow
*
* @author Murat Purc <murat@purc.de>
* @copyright © Murat Purc 2008
* @package Contenido
* @subpackage Module_Slideshow
*/
if (!class_exists('ModSlideshow')) {
cInclude('classes', 'class.upload.php');
/**
* Contenido module input class for Slideshow
*
* @author Murat Purc <murat@purc.de>
* @copyright © Murat Purc 2008
* @package Contenido
* @subpackage Module_Slideshow
*/
class ModSlideshow {
/**
* Default slideshow speed
* @var int
*/
const DEFAULT_SPEED = 4000;
/**
* Selected slideshow order
* @var string
*/
private $_sSelectedOrder;
/**
* Assoziative order array
* @var array
*/
private $_aOrder;
/**
* Selected folder containing images for the slideshow
* @var string
*/
private $_sSelectedDirname;
/**
* Client id
* @var int
*/
private $_client;
/**
* Slideshow identifier
* @var string
*/
public $sElementId;
/**
* Speed in milliseconds (time between two slides)
* @var int
*/
public $iSpeed;
/**
* Horizontal align
* @var string
*/
public $sCenterHorizontalChk;
/**
* Max width of slideshow images
* @var int
*/
public $maxWidth;
/**
* Max height of slideshow images
* @var int
*/
public $maxHeight;
/**
* Max cache time of resized images in minutes
* @var int
*/
public $maxCachetime;
/**
* Constructor sets some properties
*
* @param int $clientId Client id
* @param array $aSettings Module settings
*/
public function __construct($clientId, $aSettings) {
$this->_client = (int) $clientId;
$this->sElementId = trim($aSettings['elementId']);
$this->_sSelectedDirname = $aSettings['selectedDirname'];
$this->iSpeed = (int) $aSettings['speed'];
$this->sCenterHorizontalChk = ($aSettings['centerHorizontalChk'] != '') ? ' checked="checked"' : '';
$this->maxWidth = (int) $aSettings['maxWidth'];
$this->maxHeight = (int) $aSettings['maxHeight'];
$this->maxCachetime = (int) $aSettings['maxCachetime'];
$this->_sSelectedOrder = $aSettings['selectedOrder'];
$this->_aOrder = array(
'RAND()' => mi18n("Zufällig"),
'filename:ASC' => mi18n("Dateiname aufsteigend"),
'filename:DESC' => mi18n("Dateiname absteigend"),
'size:ASC' => mi18n("Dateigröße aufsteigend"),
'size:DESC' => mi18n("Dateigröße absteigend"),
'filetype:ASC' => mi18n("Dateityp aufsteigend"),
'filetype:DESC' => mi18n("Dateityp absteigend"),
'created:ASC' => mi18n("Erstellungsdatum aufsteigend"),
'created:esc' => mi18n("Erstellungsdatum absteigend"),
'idupl:ASC' => mi18n("Id aufsteigend"),
'idupl:DESC' => mi18n("Id absteigend")
);
if ($this->sElementId == '') {
$this->sElementId = 'slideshow';
}
if ($this->iSpeed <= 0) {
$this->iSpeed = self::DEFAULT_SPEED;
}
if ($this->maxWidth <= 0) {
$this->maxWidth = '';
}
if ($this->maxHeight <= 0) {
$this->maxHeight = '';
}
if ($this->maxCachetime <= 0) {
$this->maxCachetime = '';
}
}
/**
* Generates and returns option items of the dirname select box.
*
* @return string Composed option items.
*/
public function generateDirSelectOptions(){
$oUploadColl = new UploadCollection();
$oUploadColl->flexSelect('dirname', '', 'idclient=' . $this->_client, 'dirname');
$opt = '<option value="">' . mi18n("-- Ordner auswählen --") . '</option>' . "\n";
while ($oUploadItem = $oUploadColl->next()) {
$dirname = $oUploadItem->get('dirname');
$sel = ($dirname == $this->_sSelectedDirname) ? ' selected="selected"' : '';
$opt .= '<option value="' . $dirname . '"' . $sel . '>' . $dirname . '</option>' . "\n";
}
return $opt;
}
/**
* Generates and returns option items of the order select box.
*
* @return string Composed option items.
*/
public function generateOrderSelectOptions(){
$opt = '<option value="">' . mi18n("-- Reihenfolge auswählen --") . '</option>' . "\n";
foreach ($this->_aOrder as $key => $value) {
$sel = ($key == $this->_sSelectedOrder) ? ' selected="selected"' : '';
$opt .= '<option value="' . $key . '"' . $sel . '>' . $value . '</option>' . "\n";
}
return $opt;
}
}
} // if (!class_exists('ModSlideshow'))
$oModSlideshow = new ModSlideshow($client, array(
'elementId' => "CMS_VALUE[7]",
'selectedDirname' => "CMS_VALUE[0]",
'speed' => "CMS_VALUE[1]",
'centerHorizontalChk' => "CMS_VALUE[2]",
'maxWidth' => "CMS_VALUE[3]",
'maxHeight' => "CMS_VALUE[4]",
'maxCachetime' => "CMS_VALUE[5]",
'selectedOrder' => "CMS_VALUE[6]"
));
?>
<!-- module slideshow -->
<table cellspacing="0" cellpadding="3" border="0">
<tr>
<td valign="top" class="text_medium"><?php print mi18n("ID der Slideshow") ?></td>
<td>
<input type="text" class="text_medium" name="CMS_VAR[7]" value="<?php print $oModSlideshow->sElementId ?>" /><br />
<small><?php print mi18n("(Bei mehrfachem Vorkommen der Slideshow innerhalb einer Seite, muss die ID eindeutig sein, z. B. slideshow, slideshow2, slideshow3, usw.)") ?></small><br />
<br />
</td>
</tr>
<tr>
<td valign="top" class="text_medium"><?php print mi18n("Ordner für Slideshow wählen:") ?></td>
<td>
<select name="CMS_VAR[0]" class="text_medium">
<?php print $oModSlideshow->generateDirSelectOptions() ?>
</select><br />
<small><?php print mi18n("(Der ausgewählte Ordner sollte nur Bilder enthalten)") ?></small><br />
<br />
</td>
</tr>
<tr>
<td valign="top" class="text_medium"><?php print mi18n("Reihenfolge der Bilder in der Slideshow:") ?></td>
<td>
<select name="CMS_VAR[6]" class="text_medium">
<?php print $oModSlideshow->generateOrderSelectOptions() ?>
</select><br />
<br />
</td>
</tr>
<tr>
<td valign="top" class="text_medium"><?php print mi18n("Slideshowgeschwindigkeit in Millisekunden:") ?></td>
<td class="text_medium">
<input type="text" class="text_medium" name="CMS_VAR[1]" value="<?php print $oModSlideshow->iSpeed ?>" /> <?php print mi18n("ms") ?><br />
<small><?php print mi18n("(z. B. 4000 für 4 Sekunden)") ?></small><br />
<br />
</td>
</tr>
<tr>
<td class="text_medium" colspan="2">
<input type="checkbox" class="text_medium" name="CMS_VAR[2]" value="vcenter"<?php print $oModSlideshow->sCenterHorizontalChk ?> /> <?php print mi18n("Bilder horizontal zentrieren") ?><br />
<small><?php print mi18n("(Bei unterschiedlich großen Bildern alle Bilder horizontal zentrieren.<br /> Die vertikale Zentrierung kann über CSS definiert werden.)") ?></small><br />
<br />
</td>
</tr>
<tr>
<td valign="top" class="text_medium"><?php print mi18n("Maximale Breite der Bilder:") ?></td>
<td class="text_medium">
<input type="text" class="text_medium" name="CMS_VAR[3]" value="<?php print $oModSlideshow->maxWidth ?>" /> <?php print mi18n("Pixel") ?><br />
</td>
</tr>
<tr>
<td valign="top" class="text_medium"><?php print mi18n("Maximale Höhe der Bilder:") ?></td>
<td class="text_medium">
<input type="text" class="text_medium" name="CMS_VAR[4]" value="<?php print $oModSlideshow->maxHeight ?>" /> <?php print mi18n("Pixel") ?><br />
<small><?php print mi18n("(Sind Werte für maximale Breite und/oder Höhe angegeben,<br />werden große Bilder heruntergerechnet.)") ?></small><br />
<br />
</td>
</tr>
<tr>
<td valign="top" class="text_medium"><?php print mi18n("Maximale Cachedauer der Bilder:") ?></td>
<td class="text_medium">
<input type="text" class="text_medium" name="CMS_VAR[5]" value="<?php print $oModSlideshow->maxCachetime ?>" /> <?php print mi18n("Minuten") ?><br />
<small><?php print mi18n("(Gilt nur für zu große Bilder, die heruntergerechnet wurden.)") ?></small>
</td>
</tr>
</table>
<!-- /module slideshow -->
<?php
unset($oModSlideshow);
Code: Alles auswählen
<?php
/**
* Module-Output: Slideshow
*
* @author Murat Purc <murat@purc.de>
* @copyright © Murat Purc 2008
* @package Contenido
* @subpackage Module_Slideshow
*/
if (!class_exists('ModSlideshow')) {
cInclude('classes', 'class.upload.php');
cInclude('frontend', 'includes/class.uploadmeta.php');
cInclude('includes', 'functions.api.images.php');
/**
* Contenido module output class for Slideshow
*
* @author Murat Purc <murat@purc.de>
* @copyright © Murat Purc 2008
* @package Contenido
* @subpackage Module_Slideshow
*/
class ModSlideshow {
/**
* Default slideshow speed
* @var int
*/
const DEFAULT_SPEED = 4000;
/**
* Default cache time of resized images in minutes
* @var int
*/
const DEFAULT_CACHETIME = 10;
/**
* Default image width
* @var string
*/
const DEFAULT_WIDTH = '100%';
/**
* Default image height
* @var string
*/
const DEFAULT_HEIGHT = '100%';
/**
* Supported filetypes for the slideshow
* @var string
*/
const FILE_TYPES = "'jpg','jpeg','png','gif'";
/**
* To store occured errors
* @var string
*/
private $_sError = '';
/**
* Max calculated image width
* @var int
*/
public $iMaxWidth = 0;
/**
* Max calculated image height
* @var int
*/
public $iMaxHeight = 0;
/**
* Language id
* @var int
*/
private $_lang;
/**
* Assoziative order array
* @var array
*/
private $_aOrder;
/**
* Client HTML path
* @var string
*/
private $_sHtmlPath;
/**
* Client upload directory
* @var string
*/
private $_sUploadDir;
/**
* Absolute path to client upload directory
* @var string
*/
private $_sAbsUploadPath;
/**
* Slideshow id
* @var string
*/
private $_sElementId;
/**
* Selected slideshow directory
* @var string
*/
private $_sSelectedDirname;
/**
* Slideshow speed in miliseconds
* @var int
*/
private $_iSpeed;
/**
* Max allowed image width
* @var int
*/
private $_maxAllowedWidth;
/**
* Max allowed image height
* @var int
*/
private $_maxAllowedHeight;
/**
* Max cache time for resized images in minutes
* @var int
*/
private $_iMaxCachetime;
/**
* Flag to center images horizontaly
* @var bool
*/
private $_bCenterImagesHorizontal = false;
/**
* Slideshow order
* @var string
*/
private $_sSelectedOrder;
/**
* Constructor sets some properties...
*
* @param array $aClientCfg Client configuration
* @param int $iLangId Language id
* @param array $aSettings Module settings
*/
public function __construct($aClientCfg, $iLangId, $aSettings) {
$this->_lang = $iLangId;
$this->_sHtmlPath = $aClientCfg['path']['htmlpath'];
$this->_sUploadDir = $aClientCfg['upl']['frontendpath'];
$this->_sAbsUploadPath = $aClientCfg['upl']['path'];
$this->_aOrder = array(
'RAND()' => mi18n("Zufällig"),
'filename:ASC' => mi18n("Dateiname aufsteigend"),
'filename:DESC' => mi18n("Dateiname absteigend"),
'size:ASC' => mi18n("Dateigröße aufsteigend"),
'size:DESC' => mi18n("Dateigröße absteigend"),
'filetype:ASC' => mi18n("Dateityp aufsteigend"),
'filetype:DESC' => mi18n("Dateityp absteigend"),
'created:ASC' => mi18n("Erstellungsdatum aufsteigend"),
'created:esc' => mi18n("Erstellungsdatum absteigend"),
'idupl:ASC' => mi18n("Id aufsteigend"),
'idupl:DESC' => mi18n("Id absteigend")
);
// slideshow id
$this->_sElementId = trim($aSettings['elementId']);
if ($this->_sElementId == '') {
$this->_sElementId = 'slideshow';
}
// directory including images 4 the slideshow
$this->_sSelectedDirname = trim($aSettings['selectedDirname']);
if ($this->_sSelectedDirname == '') {
$this->_sError = mi18n("Slideshow: Es ist kein Verzeichnis gewählt!");
return;
} elseif (!is_dir($this->_sUploadDir.$this->_sSelectedDirname)) {
$this->_sError = mi18n("Slideshow: Das gewählte Verzeichnis existiert nicht");
return;
}
// slideshowspeed
$this->_iSpeed = (int) $aSettings['speed'];
if ($this->_iSpeed <= 0) {
$this->_iSpeed = self::DEFAULT_SPEED;
}
// flag 2 center all images horizontally
$this->_bCenterImagesHorizontal = ($aSettings['centerHorizontalChk'] != '') ? true : false;
// max allowed width of images. bigger ones will be resized
$this->_maxAllowedWidth = (int) $aSettings['maxWidth'];
if ($this->_maxAllowedWidth <= 0) {
$this->_maxAllowedWidth = '';
}
// max allowed height of images. bigger ones will also be resized
$this->_maxAllowedHeight = (int) $aSettings['maxHeight'];
if ( $this->_maxAllowedHeight <= 0) {
$this->_maxAllowedHeight = '';
}
// max cachetime in minutes 4 resized images
$this->_iMaxCachetime = (int) $aSettings['maxCachetime'];
if ($this->_iMaxCachetime <= 0) {
$this->_iMaxCachetime = self::DEFAULT_CACHETIME;
} elseif ($this->_maxAllowedWidth == '' && $this->_maxAllowedHeight == '') {
$this->_iMaxCachetime = self::DEFAULT_CACHETIME;
}
// selected order type
$this->_sSelectedOrder = $aSettings['selectedOrder'];
if (!isset($this->_sSelectedOrder[$this->_aOrder])) {
$this->_sSelectedOrder = 'filename:ASC';
}
}
/**
* Generates the slideshow or a message on any occured error.
*/
public function generateOutput(){
if ($this->_sError !== '') {
print $this->_sError;
return;
}
$oTemplate = new Template();
$oUploadColl = new UploadCollection();
// order settings
if (strpos($this->_sSelectedOrder, ':') > 0) {
list($sort, $sortdir) = explode(':', $this->_sSelectedOrder);
$sOrder = $sort . ' ' . $sortdir;
} else {
$sOrder = $this->_sSelectedOrder;
}
// where statement with selected dir and supported filetypes
$sWhere = 'dirname="' . $this->_sSelectedDirname . '" AND filetype IN(' . self::FILE_TYPES . ')';
// run the statement
$oUploadColl->select($sWhere, '', $sOrder);
$aImages = array();
// iterate thru upload collection 2 store data in array
while ($oUploadItem = $oUploadColl->next()) {
// some checks
$sImageFile = $this->_sAbsUploadPath . $this->_sSelectedDirname . $oUploadItem->get('filename');
if (!is_file($sImageFile) || !is_readable($sImageFile)) {
continue;
}
if (is_numeric($this->_maxAllowedWidth) && is_numeric($this->_maxAllowedHeight)) {
// bigger images have 2 be resized
$sImageFile = cApiImgScale(
$sImageFile, $this->_maxAllowedWidth, $this->_maxAllowedHeight, false, false, $this->_iMaxCachetime
);
if (!$sImageFile) {
continue;
}
$sImageFile = str_replace($this->_sHtmlPath, '', $sImageFile);
} else {
// use original image file
$sImageFile = $this->_sUploadDir . $this->_sSelectedDirname . $oUploadItem->get('filename');
}
// get'n store image dimensions
$size = $this->_getImageSize($sImageFile);
$attr = (is_array($size)) ? ' ' . $size[3] : '';
// add new images array item
$aImages[$oUploadItem->get('idupl')] = array(
'size' => $size,
'src' => $sImageFile,
'alt' => '',
'attr' => $attr
);
}
if (count($aImages) == 0) {
print mi18n("Slideshow: Es wurden keine Bilder im angegebenen Verzeichnis gefunden!");
return;
}
// now get description by language
$sWhere = '';
foreach ($aImages as $idUpl => $item) {
$sWhere .= ' idupl=' . $idUpl . ' OR ';
}
$sWhere = substr($sWhere, 0, -3) . ' AND idlang=' . $this->_lang;
$oUploadMetaColl = new UploadMetaCollection();
$oUploadMetaColl->select($sWhere, '', '');
// iterate upload meta collection 2 store description in images array
while ($oItem = $oUploadMetaColl->next()) {
$aImages[$oItem->get('idupl')]['alt'] = htmlspecialchars($oItem->get('description'));
}
// max width and height of slideshow box
$sStyleDimensions = (is_numeric($this->iMaxWidth)) ? 'width:' . $this->iMaxWidth . 'px;' : 'width:' . self::DEFAULT_WIDTH . ';';
$sStyleDimensions .= (is_numeric($this->iMaxHeight)) ? 'height:' . $this->iMaxHeight . 'px;' : 'height:' . self::DEFAULT_HEIGHT . ';';
$sStyleDimensions = sprintf(' style="%s"', $sStyleDimensions);
$oTemplate->set('s', 'SLIDESHOW.ID', $this->_sElementId);
$oTemplate->set('s', 'SLIDESHOW.ATTRIBUTES', $sStyleDimensions);
// loop images array an fill template
foreach ($aImages as $image) {
if ($this->_bCenterImagesHorizontal == true) {
// comopse css definition 2 center a image horizontally
$sCenterCss = $this->_css2centerImageHorizontal($image['size']);
if ($sCenterCss != '') {
$image["attr"] .= ' style="'.$sCenterCss.'"';
}
}
$oTemplate->set('d', 'SLIDE.ATTRIBUTES', $sStyleDimensions);
$oTemplate->set('d', 'IMG.SRC', $image['src']);
$oTemplate->set('d', 'IMG.ALT', $image['alt']);
$oTemplate->set('d', 'IMG.ATTRIBUTES', $image['attr']);
$oTemplate->next();
}
$oTemplate->set('s', 'SLIDESHOW.SPEED', $this->_iSpeed);
$oTemplate->set('s', 'NOSCRIPT', mi18n("JavaScript Code zum Erstellen der Bilder-Slideshow"));
$oTemplate->generate('templates/slideshow.html', 0, 0);
}
/**
* Returns value of getimagesize function, and also stores maximum width/height of
* existing images.
*
* @param string Image file to ger size array for
* @return array Returnvalue of getimagesize() function
* @access private
*/
private function _getImageSize($file){
$size = @getimagesize($file);
if (is_array($size)) {
if ($this->iMaxWidth < $size[0]) {
$this->iMaxWidth = $size[0];
}
if ($this->iMaxHeight < $size[1]) {
$this->iMaxHeight = $size[1];
}
}
return $size;
}
/**
* Composes css definition 2 center a image horizontally and returns it back.
*
* @param mixed Array including image size imnformations (result of getimagesize())
* @return string Composed css definition
* @access private
*/
private function _css2centerImageHorizontal($size){
if (!is_array($size) || $this->iMaxHeight == 0 || ($size[1] == $this->iMaxHeight)) {
return '';
}
$css = "margin-top:".(($this->iMaxHeight - $size[1]) / 2)."px";
return $css;
}
}
} // if (!class_exists('ModSlideshow'))
// create slideshow module instance, pass client config and actual language id
$oModSlideshow = new ModSlideshow($cfgClient[$client], $lang, array(
'elementId' => "CMS_VALUE[7]",
'selectedDirname' => "CMS_VALUE[0]",
'speed' => "CMS_VALUE[1]",
'centerHorizontalChk' => "CMS_VALUE[2]",
'maxWidth' => "CMS_VALUE[3]",
'maxHeight' => "CMS_VALUE[4]",
'maxCachetime' => "CMS_VALUE[5]",
'selectedOrder' => "CMS_VALUE[6]"
));
// generate the slideshow
$oModSlideshow->generateOutput();
// save memory
unset($oModSlideshow);
?>