files = array(7); $this->hyperlinks = array(7); for($i=0; $i<7; $i++){ $this->files[$i] = ""; $this->hyperlinks[$i] = ""; $this->captions[$i] = ""; } $this->displayWidth = 540; $this->imgDir = IMG_DIR; $this->setSize(); } /** @see BuildingBlock::install() */ //----------------------------------------------- function install(){ //----------------------------------------------- $query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_alternatingimageblock ( id INT not null AUTO_INCREMENT, files TEXT not null, hyperlinks TEXT not null, captions TEXT not null, PRIMARY KEY(id) )"); $result = dbQuery($query); } /** @see BuildingBlock::load() */ //------------------------------------------------- function load(){ //------------------------------------------------- $query = sprintf("SELECT * FROM bruckm_alternatingimageblock WHERE id = %d", $this->id); $result = dbQuery($query); $line = mysqli_fetch_array($result, MYSQLI_ASSOC); $this->files = explode("\t", $line['files']); $this->hyperlinks = explode("\t", $line['hyperlinks']); $this->captions = explode("\t", $line['captions']); } /** @see BuildingBlock::doSave() */ //----------------------------------------------- function doSave(){ //----------------------------------------------- $query = sprintf("UPDATE bruckm_alternatingimageblock SET files = %s, hyperlinks = %s, captions = %s WHERE id = %d", sqlstring(implode("\t", $this->files)), sqlstring(implode("\t", $this->hyperlinks)), sqlstring(implode("\t", $this->captions)), $this->id); dbQuery($query); } /** @see BuildingBlock::doCreate() */ //----------------------------------------------- function doCreate(){ //----------------------------------------------- $query = sprintf("INSERT INTO bruckm_alternatingimageblock (files, hyperlinks, captions) VALUES (%s, %s, %s)", sqlstring(""), sqlstring(""), sqlstring("")); dbQuery($query); return mysql_insert_id(); } /** @see BuildingBlock::delete() */ //------------------------------------------------- function delete(){ //------------------------------------------------- $query = sprintf("DELETE FROM bruckm_alternatingimageblock WHERE id = %d", $this->id); dbQuery($query); foreach($this->files as $file){ @unlink($this->imgDir.$file); } } /** @see BuildingBlock::update() */ //----------------------------------------------- function update(){ //----------------------------------------------- for($i=0; $i<7; $i++){ if(isset($_FILES["imageblock_file".$this->id."_".$i]) && $_FILES["imageblock_file".$this->id."_".$i]['error'] == UPLOAD_ERR_OK){ if($this->files[$i]){ @unlink($this->imgDir.$this->files[$i]); } $this->files[$i] = $this->findName($this->imgDir, $_FILES["imageblock_file".$this->id."_".$i]['name']); $this->uploadImage($_FILES["imageblock_file".$this->id."_".$i], $this->imgDir.$this->files[$i]); } $this->hyperlinks[$i] = trim($_POST["imageblock_hyperlink".$this->id."_".$i]); $this->captions[$i] = trim($_POST["imageblock_caption".$this->id."_".$i]); } } /** @see BuildingBlock::printContent() */ //----------------------------------------------- function printContent($position){ //----------------------------------------------- $t = new Template(CMS_TEMPLATE_DIR."alternatingimageblock.html"); $t->setVar("ID", $this->id); $t->setVar("POSITION", $position); for($i=0; $i<7; $i++){ if($this->hyperlinks[$i]){ $t->setVar("HYPERLINK_$i", htmlspecialchars($this->hyperlinks[$i])); } else{ $t->setVar("HYPERLINK_$i", ""); } $t->setVar("CAPTION_$i", htmlspecialchars($this->captions[$i])); if($this->files[$i] && file_exists($this->imgDir.$this->files[$i])){ $t->setVar("PATH_$i", $this->imgDir.$this->files[$i]); $imgSize = getImageSize($this->imgDir.$this->files[$i]); $width = $imgSize[0]; $height = $imgSize[1]; if($width > $this->displayWidth){ $height = round($height * ($this->displayWidth/$width)); $width = $this->displayWidth; } $t->setVar("WIDTH_$i", $width); $t->setVar("HEIGHT_$i", $height); } else{ $t->removeBlock("IMAGE_$i"); } } return $t->toString(); } /** @see BuildingBlock::publish() */ //---------------------------------------------- function publish(){ //---------------------------------------------- //select day of week $day = (date("w") + 6) % 7; if(empty($this->files[$day])){ $day = -1; $i = 0; //if no image exists for this day -> search for any other image while($day < 0){ if(!empty($this->files[$i])){ $day = $i; } $i++; } if($day < 0){ logError(3, "no images uploaded (id: ".$this->id.")", __FILE__, __LINE__); return ""; } } if($this->captions[$day]){ $alt = htmlspecialchars($this->captions[$day]); } else{ $alt = "Bild des Tages"; } $img = "files[$day]."\" alt=\"$alt\" />"; if(empty($this->hyperlinks[$day])){ return $img."
"; } else{ return "hyperlinks[$day]."\" target=\"_self\">$img
\n"; } } // === ADDITIONAL METHODS ======================================================== // /** uploads an image file * @param file $_FILES file data */ //------------------------------------------------ function uploadImage($file, $path){ //------------------------------------------------ //resize image $imgSize = getImageSize($file['tmp_name']); if($imgSize[0] > $this->maxWidth || $imgSize[1] > $this->maxHeight){ 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 = $origWidth; $height = $origHeight; if($width > $this->maxWidth){ $fact = $this->maxWidth/$origWidth; $width *= $fact; $height *= $fact; } if($height > $this->maxHeight){ $fact = $this->maxHeight/$height; $width *= $fact; $height *= $fact; } $small = imageCreateTrueColor($width,$height); imageCopyResampled($small, $original, 0, 0, 0, 0, $width, $height, $origWidth, $origHeight); switch($imgSize[2]){ case 1: if(!@imageGif($small, $path)){ logError(4, "Could not create gif image: $path", __FILE__, __LINE__); return ""; } break; case 2: if(!@imageJpeg($small, $path, 100)){ logError(4, "Could not create jpg image: $path", __FILE__, __LINE__); return ""; } break; case 3: if(!@imagePng($small, $path)){ logError(4, "Could not create png image: $path", __FILE__, __LINE__); return ""; } break; } imageDestroy($small); imageDestroy($original); @chmod($path, 0777); } else{ move_uploaded_file($file['tmp_name'], $path); //copy($file['tmp_name'], $path); @chmod($path, 0777); } } /** sets the image and thumb size */ //----------------------------------------------- function setSize($maxW=580, $maxH=1000){ //----------------------------------------------- $this->maxWidth = $maxW; $this->maxHeight = $maxH; } /** finds a path that does not exist * @param dir target directory * @param name name of the source image * @return filename in the target directory that does not exist */ //----------------------------------------------- function findName($dir, $name){ //----------------------------------------------- $name = strtolower($name); $name = str_replace("ä", "ae", $name); $name = str_replace("ö", "oe", $name); $name = str_replace("ü", "ue", $name); $name = str_replace("ß", "sz", $name); $name = ereg_replace("[^A-Za-z0-9\.-_]", "", $name); if(!file_exists($dir.$name)){ return $name; } //extract file extension $dot = strrpos($name, "."); $ext = substr($name, $dot+1); $name = substr($name, 0, $dot); //add 3-digit number $count = 1; while(file_exists(sprintf("%s%s%03d.%s", $dir, $name, $count, $ext))){ $count++; } return sprintf("%s%03d.%s", $name, $count, $ext); } }; ?>