<?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
* KBModuleGroup
*/
class KBModuleGroup extends CDpObject
{
var $id = 0;
var $base_id;
var $name;
var $visible;
var $width = 0;
var $rank;
/*
* constructor
*/
function KBModuleGroup ()
{
$primaryKey = 'id';
$this->CDpObject (KB_TABLE_GROUPS, $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 ()
{
return $this->name;
}
/*
* setName ()
*/
function setName ($name)
{
$this->name = $name;
}
/*
* isVisible ()
*/
function isVisible ()
{
return $this->visible;
}
/*
* setVisible ()
*/
function setVisible ($visible = true)
{
$this->visible = (boolean) $visible;
}
/*
* setInvisible ()
*/
function setInvisible ()
{
$this->setVisible (false);
}
/*
* 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->setVisible ($this->visible);
$this->setWidth ($this->width == 'auto' ? 0 : $this->width);
// rank
$this->increaseRanksFromPoint ($this->rank);
// execute
$this->storeRecord ();
// update
} else {
// stored object
$obj = new KBModuleGroup ();
$obj->load ($this->id);
// set
$obj->setName ($this->name);
$obj->setVisible ($this->visible);
$obj->setWidth ($this->width == 'auto' ? 0 : $this->width);
// 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)
{
$groups = $this->getGroupsByBase (KB_BASE);
// iterate over groups
foreach ($groups as $group)
{
if ($group->getRank () >= $ref)
$group->increaseRank ();
}
}
/*
* increaseRank ()
*/
function increaseRank ()
{
$sql = "UPDATE " . KB_TABLE_GROUPS . " SET rank = '" . ((int) $this->rank + 1) . "'";
$sql .= " WHERE id = " . $this->id;
// execute
db_exec ($sql);
}
/*
* decreaseRanksFromPoint ()
*/
function decreaseRanksFromPoint ($ref)
{
$groups = $this->getGroupsByBase (KB_BASE);
// iterate over groups
foreach ($groups as $group)
{
if ($group->getRank () > $ref)
$group->decreaseRank ();
}
}
/*
* decreaseRank ()
*/
function decreaseRank ()
{
$sql = "UPDATE " . KB_TABLE_GROUPS . " SET rank = '" . ((int) $this->rank - 1) . "'";
$sql .= " WHERE id = " . $this->id;
// execute
db_exec ($sql);
}
/*
* delete ()
*/
function delete ($recursive = false)
{
// recursive
$elements = KBModuleElement::getElementsByGroup ($this->id);
foreach ($elements as $element)
$element->delete (true);
// recursive
$labels = KBModuleLabel::getLabelsByGroup ($this->id);
foreach ($labels as $label)
$label->delete (true);
// stored object
$obj = new KBModuleGroup ();
$obj->load ($this->id);
// rank
if (!$recursive)
$this->decreaseRanksFromPoint ($obj->getRank ());
// sql
$sql = "DELETE FROM " . KB_TABLE_GROUPS . " WHERE id = " . $this->id;
// execute
return !db_exec ($sql) ? db_error () : null;
}
/*
* getGroupsByBase ()
*/
function getGroupsByBase ($base_id)
{
$r = array ();
// sql
$sql = "SELECT * FROM " . KB_TABLE_GROUPS;
$sql .= " WHERE base_id = '" . (int) $base_id . "'";
$sql .= " ORDER BY rank";
// load
$hash = db_loadList ($sql);
// bind
foreach ($hash as $row)
{
$obj = new KBModuleGroup ();
bindHashToObject ($row, $obj);
$r [] = $obj;
}
// return
return $r;
}
/*
* getVisibleGroupsByBase ()
*/
function getVisibleGroupsByBase ($base_id)
{
$r = array ();
// get
$groups = KBModuleGroup::getGroupsByBase ($base_id);
// filter
foreach ($groups as $group)
{
if ($group->isVisible ())
$r [] = $group;
}
// return
return $r;
}
}
?>