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

284 lines
7.7 KiB
PHP

<?php
/** Container
* abstract superclass for containers that list and sort child elements
*
* @version 1.9.1
* @since 2007-05-01
* @author martin lenzelbauer
*
* @change 2007-06-20
* save container settings for each user:
* - added 2-column primary key (container, user) in the table
* - changed install()
* - changed updateSortSettings()
* - changed load()
*/
class Container extends CmsObject{
var $property; //the property that is compared
var $order; //the compare order (ASC or DESC)
var $objectsClass; //the class name of the objects that are held in the container
var $allowedProperties; //properties that can be compared
var $propertyLabels; //names for the properties
/** C'tor
*/
//-----------------------------------------------
function Container($id=0, $parent=NULL){
//-----------------------------------------------
parent::CmsObject($id, $parent);
$this->name = "[unbenannter Container]";
$this->property = "name";
$this->order = "ASC";
$this->objectsClass = "";
$this->allowedProperties = array("name", "lastUpdate");
$this->propertyLabels = array("Name", "Letzte Änderung");
}
/** @see CmsObject::load()
*/
//-----------------------------------------------
function load($path=array()){
//-----------------------------------------------
//load container settings
$query = sprintf("SELECT * FROM bruckm_container WHERE container = %s AND user = %s",
sqlstring(get_class($this)),
sqlstring($_SESSION['user']));
$result = dbQuery($query);
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
$this->property = $line['property'];
$this->order = $line['order'];
//load objects
$this->childObjects = array();
$query = sprintf("SELECT id FROM bruckm_index WHERE class = %s",
sqlstring($this->objectsClass));
$result = dbQuery($query);
while($line = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$this->childObjects[] = FlexiconFactory::instanceById($line['id'], $this);
}
if(sizeof($path) > 0 && $path[0] == $this->id){
array_shift($path);
foreach($this->childObjects as $i=>$child){
$this->childObjects[$i]->load($path);
}
}
$this->sort();
}
/** @see CmsObject::save()
*/
//-----------------------------------------------
function save(){
//-----------------------------------------------
foreach($this->childObjects as $i=>$child){
if($this->childObjects[$i]->getId() == 0){
$this->childObjects[$i]->save();
}
}
}
/** installs the container table
*/
//----------------------------------------------
function install(){
//----------------------------------------------
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_container (
container VARCHAR(100) NOT NULL,
user VARCHAR(100) NOT NULL,
property VARCHAR(50) NOT NULL,
`order` VARCHAR(4) NOT NULL,
PRIMARY KEY(container, user)
)");
dbQuery($query);
}
/** @see CmsObject::printContent();
*/
//-----------------------------------------------
function printContent(){
//-----------------------------------------------
$out = $this->doPrintErrors();
$out .= $this->doPrintMetadata();
foreach($this->childObjects as $i=>$child){
$out .= $this->doPrintInsertBar($i);
$out .= $this->childObjects[$i]->printChildContent();
}
$out .= $this->doPrintInsertBar(sizeof($this->childObjects));
return $out;
}
/** @see CmsObject:doPrintMetadata()
*/
//-----------------------------------------------
function doPrintMetadata(){
//-----------------------------------------------
$t = new Template(CMS_TEMPLATE_DIR."container.html");
$t->setVar("NAME", htmlspecialchars($this->name));
$p = "";
foreach($this->allowedProperties as $i=>$prop){
if($prop == $this->property){
$p .= "<option value=\"$prop\" selected=\"selected\">";
}
else{
$p .= "<option value=\"$prop\">";
}
$p .= htmlspecialchars($this->propertyLabels[$i])."</option>";
}
$t->setVar("PROPERTIES", $p);
if($this->order == "ASC"){
$t->setVar("ORDER_ASC", "selected=\"selected\"");
$t->setVar("ORDER_DESC", "");
}
else{
$t->setVar("ORDER_DESC", "selected=\"selected\"");
$t->setVar("ORDER_ASC", "");
}
return $t->toString();
}
/** @see CmsObject::handleAction()
*/
//---------------------------------------------
function handleAction($action, $position, $type){
//---------------------------------------------
if($action == "sort"){
$this->updateSortingSettings();
$this->sort();
}
}
// === ADDITIONAL METHODS ================================================================= //
/** sets the id
* @param id id
*/
//-------------------------------------------
function setId($id){
//-------------------------------------------
$this->id = $id;
}
/** updates the sorting property and order
*/
//-----------------------------------------------
function updateSortingSettings(){
//-----------------------------------------------
$this->property = $_POST['property'];
$this->order = $_POST['order'];
$query = sprintf("INSERT INTO bruckm_container (container, user, property, `order`) VALUES (%s, %s, %s, %s)
ON DUPLICATE KEY UPDATE property = %s, `order` = %s",
sqlstring(get_class($this)),
sqlstring($_SESSION['user']),
sqlstring($this->property),
sqlstring($this->order),
sqlstring($this->property),
sqlstring($this->order));
dbQuery($query);
}
/** sorts the objects
* @return nothing
*/
//-----------------------------------------------
function sort(){
//-----------------------------------------------
for($i=0; $i<sizeof($this->childObjects)-1; $i++){
$min = $this->findMin($i);
if($min != $i){
$this->childObjects = array_swap($this->childObjects, $i, $min);
}
}
}
/** finds index of the minimum child object
* @return index
*/
//-----------------------------------------------
function findMin($offset){
//-----------------------------------------------
$min = $offset;
for($i=$offset+1; $i<sizeof($this->childObjects); $i++){
if($this->compare($this->childObjects[$i], $this->childObjects[$min]) < 0){
$min = $i;
}
}
return $min;
}
/** compares two CmsObjects
* @param obj1 first object
* @param obj2 second object
* @return -1 if obj1 < obj2
* 0 if obj1 = obj2
* 1 if obj1 > obj2
*/
//---------------------------------------------
function compare($obj1, $obj2){
//---------------------------------------------
$result = 0;
switch($this->property){
case "name":
$result = strcasecmp($obj1->getName(), $obj2->getName());
break;
case "lastUpdate":
$d1 = strtotime($obj1->getLastUpdate());
$d2 = strtotime($obj2->getLastUpdate());
if($d1 < $d2) $result = -1;
else $result = 1;
break;
case "id":
$id1 = strtotime($obj1->getId());
$id2 = strtotime($obj2->getId());
if($id1 < $id2) $result = -1;
else $result = 1;
break;
case "creationDate":
$d1 = strtotime($obj1->getCreationDate());
$d2 = strtotime($obj2->getCreationDate());
if($d1 < $d2) $result = -1;
else $result = 1;
break;
default:
$result = $this->customCompare($obj1, $obj2);
break;
}
if($this->order == "DESC"){
$result = -$result;
}
return $result;
}
/** compares two CmsObjects with a non-default property
* @param obj1 first object
* @param obj2 second object
* @return -1 if obj1 < obj2
* 0 if obj1 = obj2
* 1 if obj1 > obj2
*/
//---------------------------------------------
function customCompare($obj1, $obj2){}
//---------------------------------------------
};
?>