407 lines
11 KiB
PHP
407 lines
11 KiB
PHP
<?php
|
|
|
|
/** Person
|
|
*
|
|
* @version 2.1.0
|
|
* @date 2007-05-01
|
|
* @author martin lenzelbauer
|
|
*
|
|
* @change 2007-06-15
|
|
* fixed PHP4 bug in getMenu() and getTemplate()
|
|
*
|
|
* @change 2007-06-27
|
|
* added hack in printClassContent() to force IE to reload person thumb
|
|
*
|
|
* @change 2008-09-23
|
|
* updated loading & publishing routines to keep memory low in PHP5
|
|
*/
|
|
class Person extends Page{
|
|
|
|
var $job; //job
|
|
var $mail; //e-mail address
|
|
var $thumb; //name of the thumbnail file (without directory)
|
|
|
|
//thumbnail settings
|
|
var $thumbWidth = 50;
|
|
var $thumbHeight = 50;
|
|
var $thumbStretch = false;
|
|
|
|
|
|
/** C'tor
|
|
*/
|
|
//------------------------------------------------
|
|
function Person($id, $parent){
|
|
//------------------------------------------------
|
|
parent::Page($id, $parent);
|
|
$this->name = "[unbenannte Person]";
|
|
$this->job = "";
|
|
$this->mail = "";
|
|
$this->thumb = "";
|
|
$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_person WHERE id = %d", $this->classId);
|
|
$result = dbQuery($query);
|
|
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
|
$this->job = $line['job'];
|
|
$this->mail = $line['mail'];
|
|
if($line['thumb']){
|
|
if(file_exists(PERSON_THUMB_DIR.$this->thumb)){
|
|
$this->thumb = $line['thumb'];
|
|
}
|
|
else{
|
|
logError(4, "Person thumbnail doesn't exist anymore: $line[thumb]", __FILE__, __LINE__);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/** @see Page::doSave()
|
|
*/
|
|
//----------------------------------------------
|
|
function doSave(){
|
|
//----------------------------------------------
|
|
$query = sprintf("UPDATE bruckm_person SET job = %s, mail = %s, thumb = %s WHERE id = %d",
|
|
sqlstring($this->job),
|
|
sqlstring($this->mail),
|
|
sqlstring($this->thumb),
|
|
sqlnum($this->classId));
|
|
dbQuery($query);
|
|
parent::doSave();
|
|
}
|
|
|
|
|
|
/** @see Page::doCreate()
|
|
*/
|
|
//----------------------------------------------
|
|
function doCreate(){
|
|
//----------------------------------------------
|
|
$query = sprintf("INSERT INTO bruckm_person (job) VALUES (%s)",
|
|
sqlstring($this->job));
|
|
dbQuery($query);
|
|
$this->classId = mysql_insert_id();
|
|
parent::doCreate();
|
|
$this->childObjects[] = FlexiconFactory::instanceByClass("PersonPage", $this);
|
|
parent::doSave();
|
|
}
|
|
|
|
|
|
/** @see Page::doDelete()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doDelete(){
|
|
//-----------------------------------------------
|
|
parent::doDelete();
|
|
$query = sprintf("DELETE FROM bruckm_person WHERE id = %d LIMIT 1", $this->classId);
|
|
dbQuery($query);
|
|
if(!empty($this->thumb) && file_exists(PERSON_THUMB_DIR.$this->thumb)){
|
|
@unlink(PERSON_THUMB_DIR.$this->thumb);
|
|
}
|
|
}
|
|
|
|
|
|
/** @see Page::install()
|
|
*/
|
|
//-----------------------------------------------
|
|
function install(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_person (
|
|
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
job VARCHAR(100) NOT NULL DEFAULT '',
|
|
mail VARCHAR(100) NOT NULL DEFAULT '',
|
|
thumb VARCHAR(10) NOT NULL DEFAULT '',
|
|
PRIMARY KEY (id)
|
|
)");
|
|
dbQuery($query);
|
|
}
|
|
|
|
|
|
/** @see CmsObject::update()
|
|
*/
|
|
//-----------------------------------------------
|
|
function update(){
|
|
//-----------------------------------------------
|
|
parent::update();
|
|
if($_POST['personname'] != "[unbenannte Person]"){
|
|
$this->name= $_POST['personname'];
|
|
}
|
|
$this->childObjects[0]->setName($this->name);
|
|
$this->job = $_POST['job'];
|
|
$this->mail = $_POST['mail'];
|
|
if(isset($_FILES['thumb']) && $_FILES['thumb']['error'] == UPLOAD_ERR_OK){
|
|
$this->thumb = $this->uploadPreviewImage($_FILES['thumb']);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/** @see CmsObject::doPrintClassContent()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doPrintClassContent(){
|
|
//-----------------------------------------------
|
|
$t = new Template(CMS_TEMPLATE_DIR."person.html");
|
|
$t->setVar("NAME", htmlspecialchars($this->name));
|
|
$t->setVar("JOB", htmlspecialchars($this->job));
|
|
$t->setVar("MAIL", $this->mail);
|
|
if(empty($this->thumb) || !file_exists(PERSON_THUMB_DIR.$this->thumb)){
|
|
$t->removeBlock("THUMB");
|
|
}
|
|
else{
|
|
$t->setVar("THUMB", PERSON_THUMB_DIR.$this->thumb."?random=".time());
|
|
}
|
|
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 den Namen der Person ein!");
|
|
$ok = false;
|
|
}
|
|
/*if(empty($this->job)){
|
|
$this->addError("Bitte geben Sie die Aufgabe der Person ein!");
|
|
$ok = false;
|
|
}*/
|
|
/*if(empty($this->thumb)){
|
|
$this->addError("Bitte laden Sie ein Bild hoch!");
|
|
$ok = false;
|
|
}*/
|
|
if(sizeof($this->childObjects) == 0){
|
|
$this->addError("Bitte legen Sie zuerst eine Seite für die Person an!");
|
|
$ok = false;
|
|
}
|
|
else if(!$this->childObjects[0]->canBePublished()){
|
|
$this->addError("Die Personenseite ist fehlerhaft!");
|
|
$this->addError($this->childObjects[0]->getErrors());
|
|
$ok = false;
|
|
}
|
|
return $ok;
|
|
}
|
|
|
|
|
|
/** @see Element::getCssClass()
|
|
*/
|
|
//------------------------------------------------
|
|
function getCssClass(){
|
|
//------------------------------------------------
|
|
return "itemPerson";
|
|
}
|
|
|
|
|
|
// === ADDITIONAL METHODS ========================================================= //
|
|
|
|
|
|
/** uploads a preview image file
|
|
* @param file $_FILES file date
|
|
*/
|
|
//------------------------------------------------
|
|
function uploadPreviewImage($file){
|
|
//------------------------------------------------
|
|
|
|
//remove old image
|
|
if(!empty($this->thumb) && file_exists(PERSON_THUMB_DIR.$this->thumb)){
|
|
@unlink(PERSON_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, PERSON_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, PERSON_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, PERSON_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(PERSON_THUMB_DIR.$path, 0777);
|
|
}
|
|
|
|
//copy image in original size
|
|
else{
|
|
if(@move_uploaded_file($file['tmp_name'], PERSON_THUMB_DIR.$path)){
|
|
@chmod(PERSON_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;
|
|
}
|
|
|
|
|
|
/** 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 PersonList
|
|
* @param alt flag for alternating template
|
|
* @return string
|
|
*/
|
|
//----------------------------------------
|
|
function publishForPersonList($alt){
|
|
//----------------------------------------
|
|
$t = new Template(TEMPLATE_DIR."subparts/person$alt.html");
|
|
$t->setVar("ID", $this->id);
|
|
$t->setVar("NAME", htmlspecialchars($this->name));
|
|
$t->setVar("JOB", $this->job);
|
|
if(!empty($this->thumb) && file_exists(PERSON_THUMB_DIR.$this->thumb)){
|
|
$t->setVar("THUMB", $this->thumb);
|
|
}
|
|
else{
|
|
$t->setVar("THUMB", "default$alt.jpg");
|
|
}
|
|
if($this->mail){
|
|
$t->setVar("MAIL", $this->mail);
|
|
}
|
|
else{
|
|
$t->removeBlock("MAIL");
|
|
}
|
|
return $t->toString();
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
?>
|