Files
bm/public_html/public/cms/modules/pressinfo.class.php
2025-09-24 13:26:28 +02:00

428 lines
11 KiB
PHP

<?php
/** Press Info
*
* @version 1.0.0
* @since 2009-03-19
* @author Martin Lenzelbauer
*/
class PressInfo extends Page{
var $thumb; //name of the thumbnail file (without directory)
var $pdf;
//thumbnail settings
var $thumbWidth = 50;
var $thumbHeight = 50;
var $thumbStretch = false;
/** C'tor
*/
//------------------------------------------------
function PressInfo($id, $parent){
//------------------------------------------------
parent::Page($id, $parent);
$this->name = "[unbenannte Presseinfo]";
$this->thumb = "";
$this->pdf = "";
$this->visible = 1;
$this->buttonDelete = true;
$this->buttonMove = true;
}
/** @see CmsObject::load()
*/
//-----------------------------------------------
function load($path=array()){
//-----------------------------------------------
parent::load($path);
if(!$this->classId){
return;
}
$query = sprintf("SELECT * FROM bruckm_pressinfo WHERE id = %d", $this->classId);
$result = dbQuery($query);
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
if($line['pdf']){
if(file_exists(PRESS_THUMB_DIR.$this->pdf)){
$this->pdf = $line['pdf'];
}
else{
logError(4, "PressInfo PDF doesn't exist anymore: $line[pdf]", __FILE__, __LINE__);
}
}
if($line['thumb']){
if(file_exists(PRESS_THUMB_DIR.$this->thumb)){
$this->thumb = $line['thumb'];
}
else{
logError(4, "PressInfo thumbnail doesn't exist anymore: $line[thumb]", __FILE__, __LINE__);
}
}
}
/** @see Page::doSave()
*/
//----------------------------------------------
function doSave(){
//----------------------------------------------
$query = sprintf("UPDATE bruckm_pressinfo SET thumb = %s, pdf = %s WHERE id = %d",
sqlstring($this->thumb),
sqlstring($this->pdf),
sqlnum($this->classId));
dbQuery($query);
parent::doSave();
}
/** @see Page::doCreate()
*/
//----------------------------------------------
function doCreate(){
//----------------------------------------------
$query = sprintf("INSERT INTO bruckm_pressinfo (thumb, pdf) VALUES ('', '')");
dbQuery($query);
$this->classId = mysql_insert_id();
parent::doCreate();
$this->childObjects[] = FlexiconFactory::instanceByClass("PressPage", $this);
parent::doSave();
}
/** @see Page::doDelete()
*/
//-----------------------------------------------
function doDelete(){
//-----------------------------------------------
parent::doDelete();
$query = sprintf("DELETE FROM bruckm_pressinfo WHERE id = %d LIMIT 1", $this->classId);
dbQuery($query);
if(!empty($this->thumb) && file_exists(PRESS_THUMB_DIR.$this->thumb)){
@unlink(PRESS_THUMB_DIR.$this->thumb);
}
if(!empty($this->pdf) && file_exists(PRESS_THUMB_DIR.$this->pdf)){
@unlink(PRESS_THUMB_DIR.$this->pdf);
}
}
/** @see Page::install()
*/
//-----------------------------------------------
function install(){
//-----------------------------------------------
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_pressinfo (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
thumb VARCHAR(10) NOT NULL DEFAULT '',
pdf VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY (id)
)");
dbQuery($query);
}
/** @see CmsObject::update()
*/
//-----------------------------------------------
function update(){
//-----------------------------------------------
parent::update();
if($_POST['pressname'] != "[unbenannte Presseinfo]"){
$this->name= $_POST['pressname'];
}
$this->childObjects[0]->setName($this->name);
if(isset($_FILES['thumb']) && $_FILES['thumb']['error'] == UPLOAD_ERR_OK){
$this->thumb = $this->uploadPreviewImage($_FILES['thumb']);
}
if(isset($_FILES['pdf']) && $_FILES['pdf']['error'] == UPLOAD_ERR_OK){
$this->pdf = $this->uploadFile($_FILES['pdf']);
}
}
/** @see CmsObject::doPrintClassContent()
*/
//-----------------------------------------------
function doPrintClassContent(){
//-----------------------------------------------
$t = new Template(CMS_TEMPLATE_DIR."pressinfo.html");
$t->setVar("NAME", htmlspecialchars($this->name));
if(empty($this->thumb) || !file_exists(PRESS_THUMB_DIR.$this->thumb)){
$t->removeBlock("THUMB");
}
else{
$t->setVar("THUMB", PRESS_THUMB_DIR.$this->thumb."?random=".time());
}
if(empty($this->pdf) || !file_exists(PRESS_THUMB_DIR.$this->pdf)){
$t->removeBlock("PDF");
}
else{
$t->setVar("PDF", PRESS_THUMB_DIR.$this->pdf);
}
return $t->toString();
}
/** @see CmsObject::preview()
*/
//---------------------------------------------
function preview(){
//---------------------------------------------
$child = clone($this->childObjects[0]);
$child->load();
return $child->preview();
}
/** @see CmsObject::publish()
*/
//---------------------------------------------
function publish(){
//---------------------------------------------
$load = !$this->childObjects[0]->isLoaded();
if($load){
$this->childObjects[0]->load();
}
$this->childObjects[0]->publish();
if($load){
$this->childObjects[0]->unload();
}
}
/** @see Page::canBePublished()
*/
//----------------------------------------------
function canBePublished(){
//----------------------------------------------
$ok = true;
if(empty($this->name)){
$this->addError("Bitte geben Sie einen Titel für die Presseinfo ein!");
$ok = false;
}
if(sizeof($this->childObjects) == 0){
$this->addError("Bitte legen Sie zuerst eine Seite für die Presseinfo an!");
$ok = false;
}
else if(!$this->childObjects[0]->canBePublished()){
$this->addError("Die Presseseite ist fehlerhaft!");
$this->addError($this->childObjects[0]->getErrors());
$ok = false;
}
return $ok;
}
/** @see Element::getCssClass()
*/
//------------------------------------------------
function getCssClass(){
//------------------------------------------------
return "itemPressInfo";
}
// === ADDITIONAL METHODS ========================================================= //
/** uploads a preview image file
* @param file $_FILES file date
*/
//------------------------------------------------
function uploadPreviewImage($file){
//------------------------------------------------
//remove old image
if(!empty($this->thumb) && file_exists(PRESS_THUMB_DIR.$this->thumb)){
@unlink(PRESS_THUMB_DIR.$this->thumb);
}
//create target path
$imgSize = getImageSize($file['tmp_name']);
switch($imgSize[2]){
case 1: //gif
$path = $this->id.".gif";
break;
case 2: //jpg
$path = $this->id.".jpg";
break;
case 3: //png
$path = $this->id.".png";
break;
default:
logError(3, "Unsupported mime type: $file[name] ($imgSize[2])", __FILE__, __LINE__);
$this->addError("Das Bild verwendet ein nicht unterstütztes Dateiformat oder es ist beschädigt!");
return "";
}
//resize image
if($imgSize[0] > $this->thumbWidth || $imgSize[1] > $this->thumbHeight){
switch($imgSize[2]){
case 1: //gif
$original = imageCreateFromGif($file['tmp_name']);
break;
case 2: //jpg
$original = imageCreateFromJpeg($file['tmp_name']);
break;
case 3: //png
$original = imageCreateFromPng($file['tmp_name']);
break;
}
$origWidth = $imgSize[0];
$origHeight = $imgSize[1];
$width = $this->thumbWidth;
$height = $this->thumbHeight;
$origX = 0;
$origY = 0;
//clip image
if(!$this->thumbStretch){
if($origWidth > $origHeight){
$origX = ($origWidth - $origHeight)/2;
$origWidth = $origHeight;
}
else{
$origY = ($origHeight - $origWidth)/2;
$origHeight = $origWidth;
}
}
$small = imageCreateTrueColor($width, $height);
imageCopyResampled($small, $original, 0, 0, $origX, $origY, $width, $height, $origWidth, $origHeight);
switch($imgSize[2]){
case 1:
if(!@imageGif($small, PRESS_THUMB_DIR.$path)){
logError(4, "Could not create gif image: $path", __FILE__, __LINE__);
$this->addError("Das Bild konnte nicht erzeugt werden!");
return "";
}
break;
case 2:
if(!@imageJpeg($small, PRESS_THUMB_DIR.$path, 100)){
logError(4, "Could not create jpg image: $path", __FILE__, __LINE__);
$this->addError("Das Bild konnte nicht erzeugt werden!");
return "";
}
break;
case 3:
if(!@imagePng($small, PRESS_THUMB_DIR.$path)){
logError(4, "Could not create png image: $path", __FILE__, __LINE__);
$this->addError("Das Bild konnte nicht erzeugt werden!");
return "";
}
break;
}
imageDestroy($small);
imageDestroy($original);
@chmod(PRESS_THUMB_DIR.$path, 0777);
}
//copy image in original size
else{
if(@move_uploaded_file($file['tmp_name'], PRESS_THUMB_DIR.$path)){
@chmod(PRESS_THUMB_DIR.$path, 0777);
}
else{
logError(4, "Could not copy image: $file[tmp_name] => $path)", __FILE__, __LINE__);
$this->addError("Das Bild konnte nicht erzeugt werden!");
return "";
}
}
return $path;
}
/** uploads the pdf file
*/
//------------------------------------------
function uploadFile($file) {
//------------------------------------------
//remove old pdf
if(!empty($this->pdf) && file_exists(PRESS_THUMB_DIR.$this->pdf)){
@unlink(PRESS_THUMB_DIR.$this->pdf);
}
$path = $this->id . ".pdf";
move_uploaded_file($file["tmp_name"], PRESS_THUMB_DIR . $path);
return $path;
}
/** returns the html template that is selected in the PersonList
* @return template
*/
//------------------------------------------
function getTemplate(){
//------------------------------------------
if(!$this->parentObj){
$this->parentObj = FlexiconFactory::instanceById($this->parentId);
}
if(!$this->parentObj->isLoaded()){
$this->parentObj->load();
}
return $this->parentObj->getTemplate();
}
/** returns the Menu object
* @return Menu
*/
//------------------------------------------
function getMenu(){
//------------------------------------------
if(!$this->parentObj){
$this->parentObj = FlexiconFactory::instanceById($this->parentId);
}
if(!$this->parentObj->isLoaded()){
$this->parentObj->load();
}
return $this->parentObj->getMenu();
}
/** prints as list item in PressList
* @param alt flag for alternating template
* @return string
*/
//----------------------------------------
function publishForPressList($alt){
//----------------------------------------
$t = new Template(TEMPLATE_DIR."subparts/pressinfo$alt.html");
$t->setVar("ID", $this->id);
$t->setVar("NAME", htmlspecialchars($this->name));
if(!empty($this->thumb) && file_exists(PRESS_THUMB_DIR.$this->thumb)){
$t->setVar("THUMB", $this->thumb);
}
else{
$t->setVar("THUMB", "default$alt.jpg");
}
return $t->toString();
}
/** provides a pdf version of the page as download
*/
//-------------------------------------------
function downloadPdf(){
//-------------------------------------------
// there exists and uploaded pdf file
if ($this->pdf) {
header("Location: " . PRESS_THUMB_DIR . $this->pdf);
return;
}
// generate a pdf file on the fly
$load = !$this->childObjects[0]->isLoaded();
if($load){
$this->childObjects[0]->load();
}
$this->childObjects[0]->downloadPdf($this->name . ".pdf");
}
};
?>