<?php
/*
* Free IT Foundation
* Free Technology Serving Knowledge
* http://www.free-it-foundation.org
*
* This file is part of Knowledge Box.
*
* Knowledge Box is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knowledge Box is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Knowledge Box. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* class
* KBModuleElement
*/
class KBModuleElement extends CDpObject
{
var $id = 0;
var $base_id;
var $type;
var $resource_id;
var $width = 0;
var $rank;
var $name = null;
/*
* constructor
*/
function KBModuleElement ()
{
$primaryKey = 'id';
$this->CDpObject (KB_TABLE_ELEMENTS, $primaryKey);
}
/*
* getId ()
*/
function getId ()
{
return $this->id;
}
/*
* setId ()
*/
function setId ($id)
{
$this->id = (int) $id;
}
/*
* getBaseId ()
*/
function getBaseId ()
{
return $this->base_id;
}
/*
* setBaseId ()
*/
function setBaseId ($base_id)
{
$this->base_id = (int) $base_id;
}
/*
* getName ()
*/
function getName ()
{
// name undefined
if (is_null ($this->name))
{
// entry name
if ($this->type == KB_ELEMENT_ENTRY)
{
$name = '#fieldTypeName#';
// keywords aggregation
} elseif ($this->type == KB_ELEMENT_AGGREGATION) {
$name = '#elementTypeKeywords#';
// relations aggregation
} elseif ($this->type == KB_ELEMENT_COLLECTION) {
$name = '#elementTypeRelations#';
// get from db
} else {
if ($this->type == KB_ELEMENT_FIELD)
$resource = new KBModuleField ();
elseif ($this->type == KB_ELEMENT_KEYWORDS)
$resource = new KBModuleGroup ();
elseif ($this->type == KB_ELEMENT_RELATIONS)
$resource = new KBModuleBase ();
// record
$resource->load ($this->resource_id);
// assign
$name = $resource->getName ();
}
// set
$this->setName ($name);
}
return $this->name;
}
/*
* setName ()
*/
function setName ($name)
{
$this->name = $name;
}
/*
* isType ()
*/
function isType ($type)
{
return $this->type == $type;
}
/*
* getType ()
*/
function getType ()
{
return $this->type;
}
/*
* setType ()
*/
function setType ($type)
{
$this->type = (int) $type;
}
/*
* getResourceId ()
*/
function getResourceId ()
{
return $this->resource_id;
}
/*
* setResourceId ()
*/
function setResourceId ($resource_id)
{
$this->resource_id = (int) $resource_id;
}
/*
* isAutomaticWidth ()
*/
function isAutomaticWidth ()
{
return $this->width == 0;
}
/*
* getWidth ()
*/
function getWidth ()
{
return $this->width;
}
/*
* setWidth ()
*/
function setWidth ($width)
{
$this->width = (int) $width;
}
/*
* getRank ()
*/
function getRank ()
{
return $this->rank;
}
/*
* setRank ()
*/
function setRank ($rank)
{
$this->rank = (int) $rank;
}
/*
* store ()
*/
function store ()
{
// insert
if ((int) $this->id == 0)
{
// set
$this->setBaseId (KB_BASE);
$this->setWidth ($this->width == 'auto' ? 0 : $this->width);
// resource id
if ($this->type == KB_ELEMENT_FIELD)
$this->setResourceId ($_POST ['resource_field']);
elseif ($this->type == KB_ELEMENT_ENTRY)
$this->setResourceId (0);
elseif ($this->type == KB_ELEMENT_KEYWORDS)
$this->setResourceId ($_POST ['resource_group']);
elseif ($this->type == KB_ELEMENT_RELATIONS)
$this->setResourceId ($_POST ['resource_relations']);
// rank
$this->increaseRanksFromPoint ($this->rank);
// execute
$this->storeRecord ();
// update
} else {
// stored object
$obj = new KBModuleElement ();
$obj->load ($this->id);
// set
$obj->setType ($this->type);
$obj->setWidth ($this->width == 'auto' ? 0 : $this->width);
// resource id
if ($this->type == KB_ELEMENT_FIELD)
$obj->setResourceId ($_POST ['resource_field']);
elseif ($this->type == KB_ELEMENT_ENTRY)
$obj->setResourceId (0);
elseif ($this->type == KB_ELEMENT_KEYWORDS)
$obj->setResourceId ($_POST ['resource_group']);
elseif ($this->type == KB_ELEMENT_RELATIONS)
$obj->setResourceId ($_POST ['resource_relations']);
// rank
if ($this->rank != -1)
{
// decrease greater rank
$this->decreaseRanksFromPoint ($obj->getRank ());
// set
$obj->setRank ($this->rank);
// increase greater rank
$this->increaseRanksFromPoint ((int) $obj->getRank ());
}
// execute
$obj->storeRecord ();
}
}
/*
* storeRecord ()
*/
function storeRecord ()
{
parent::store ();
}
/*
* increaseRanksFromPoint ()
*/
function increaseRanksFromPoint ($ref)
{
$elements = $this->getElementsByBase (KB_BASE);
// iterate over elements
foreach ($elements as $element)
{
if ($element->getRank () >= $ref)
$element->increaseRank ();
}
}
/*
* increaseRank ()
*/
function increaseRank ()
{
$sql = "UPDATE " . KB_TABLE_ELEMENTS . " SET rank = '" . ((int) $this->rank + 1) . "'";
$sql .= " WHERE id = " . $this->id;
// execute
db_exec ($sql);
}
/*
* decreaseRanksFromPoint ()
*/
function decreaseRanksFromPoint ($ref)
{
$elements = $this->getElementsByBase (KB_BASE);
// iterate over elements
foreach ($elements as $element)
{
if ($element->getRank () > $ref)
$element->decreaseRank ();
}
}
/*
* decreaseRank ()
*/
function decreaseRank ()
{
$sql = "UPDATE " . KB_TABLE_ELEMENTS . " SET rank = '" . ((int) $this->rank - 1) . "'";
$sql .= " WHERE id = " . $this->id;
// execute
db_exec ($sql);
}
/*
* ensureRanks ()
*/
function ensureRanks ()
{
$elements = $this->getElementsByBase (KB_BASE);
// iterate over elements
for ($i = 0; $i < count ($elements); $i++)
{
$element = $elements [$i];
// natural order
$sql = "UPDATE " . KB_TABLE_ELEMENTS . " SET rank = '" . $i . "'";
$sql .= " WHERE id = " . $this->id;
// execute
db_exec ($sql);
}
}
/*
* delete ()
*/
function delete ($recursive = false)
{
// stored object
$obj = new KBModuleElement ();
$obj->load ($this->id);
// rank
if (!$recursive)
$this->decreaseRanksFromPoint ($obj->getRank ());
// sql
$sql = "DELETE FROM " . KB_TABLE_ELEMENTS . " WHERE id = " . $this->id;
// rank
if ($recursive)
$this->ensureRanks ();
// execute
return !db_exec ($sql) ? db_error () : null;
}
/*
* getElementsByBase ()
*/
function getElementsByBase ($base_id)
{
$r = array ();
// sql
$sql = "SELECT * FROM " . KB_TABLE_ELEMENTS;
$sql .= " WHERE base_id = '" . (int) $base_id . "'";
$sql .= " ORDER BY rank";
// load
$hash = db_loadList ($sql);
// bind
foreach ($hash as $row)
{
$obj = new KBModuleElement ();
bindHashToObject ($row, $obj);
$r [] = $obj;
}
// return
return $r;
}
/*
* getElementsByGroup ()
*/
function getElementsByGroup ($group_id)
{
$r = array ();
// sql
$sql = "SELECT * FROM " . KB_TABLE_ELEMENTS;
$sql .= " WHERE type = '" . KB_ELEMENT_KEYWORDS . "'";
$sql .= " AND resource_id = '" . (int) $group_id . "'";
// load
$hash = db_loadList ($sql);
// bind
foreach ($hash as $row)
{
$obj = new KBModuleElement ();
bindHashToObject ($row, $obj);
$r [] = $obj;
}
// return
return $r;
}
/*
* getElementsByField ()
*/
function getElementsByField ($field_id)
{
$r = array ();
// sql
$sql = "SELECT * FROM " . KB_TABLE_ELEMENTS;
$sql .= " WHERE type = '" . KB_ELEMENT_FIELD . "'";
$sql .= " AND resource_id = '" . (int) $field_id . "'";
// load
$hash = db_loadList ($sql);
// bind
foreach ($hash as $row)
{
$obj = new KBModuleElement ();
bindHashToObject ($row, $obj);
$r [] = $obj;
}
// return
return $r;
}
/*
* getElementsByBaseAsRelations ()
*/
function getElementsByBaseAsRelations ($base_id)
{
$r = array ();
// sql
$sql = "SELECT * FROM " . KB_TABLE_ELEMENTS;
$sql .= " WHERE type = '" . KB_ELEMENT_RELATIONS . "'";
$sql .= " AND resource_id = '" . (int) $base_id . "'";
// load
$hash = db_loadList ($sql);
// bind
foreach ($hash as $row)
{
$obj = new KBModuleElement ();
bindHashToObject ($row, $obj);
$r [] = $obj;
}
// return
return $r;
}
/*
* hasElementsInBase ()
*/
function hasElementsInBase ($base_id)
{
return count (KBModuleElement::getElementsByBase ($base_id)) > 0;
}
}
?>