<?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
* KBModuleField
*/
class KBModuleField extends CDpObject
{
var $id = 0;
var $base_id;
var $type;
var $name;
var $helper;
var $visible;
var $mandatory;
var $rank;
/*
* constructor
*/
function KBModuleField ()
{
$primaryKey = 'id';
$this->CDpObject (KB_TABLE_FIELDS, $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;
}
/*
* getNameJS ()
*/
function getNameJS ()
{
$name = str_replace ('"', '\"', $this->name);
$name = str_replace ('&', '&', $name);
$name = str_replace ('>', '>', $name);
$name = str_replace ('<', '<', $name);
return $name;
}
/*
* setName ()
*/
function setName ($name)
{
$this->name = $name;
}
/*
* getHelper ()
*/
function getHelper ()
{
return $this->helper;
}
/*
* setHelper ()
*/
function setHelper ($helper)
{
$this->helper = $helper;
}
/*
* isType ()
*/
function isType ($type)
{
return $this->type == $type;
}
/*
* getType ()
*/
function getType ()
{
return $this->type;
}
/*
* setType ()
*/
function setType ($type)
{
$this->type = (int) $type;
}
/*
* isSortable ()
*/
function isSortable ()
{
$sort = true;
switch ($this->type)
{
case KB_FIELD_HTML:
case KB_FIELD_PASSWORD:
case KB_FIELD_IMAGE: $sort = false;
}
return $sort;
}
/*
* isVisible ()
*/
function isVisible ()
{
return $this->visible;
}
/*
* setVisible ()
*/
function setVisible ($visible = true)
{
$this->visible = (boolean) $visible;
}
/*
* setInvisible ()
*/
function setInvisible ()
{
$this->setVisible (false);
}
/*
* isMandatory ()
*/
function isMandatory ()
{
return $this->mandatory;
}
/*
* setMandatory ()
*/
function setMandatory ($mandatory = true)
{
$this->mandatory = (boolean) $mandatory;
}
/*
* setOptional ()
*/
function setOptional ()
{
$this->setMandatory (false);
}
/*
* 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);
// label
if ($this->type == KB_FIELD_LABEL)
{
$this->setVisible (true);
$this->setMandatory (false);
$this->setHelper ('');
// normal field
} else {
$this->setVisible ($this->visible);
$this->setMandatory ($this->mandatory);
}
// rank
$this->increaseRanksFromPoint ($this->rank);
// execute
$this->storeRecord ();
// update
} else {
// stored object
$obj = new KBModuleField ();
$obj->load ($this->id);
// set
$obj->setName ($this->name);
$obj->setHelper ($this->helper);
$obj->setVisible ($this->visible);
$obj->setMandatory ($this->mandatory);
// 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)
{
$fields = $this->getFieldsByBase (KB_BASE);
// iterate over fields
foreach ($fields as $field)
{
if ($field->getRank () >= $ref)
$field->increaseRank ();
}
}
/*
* increaseRank ()
*/
function increaseRank ()
{
$sql = "UPDATE " . KB_TABLE_FIELDS . " SET rank = '" . ((int) $this->rank + 1) . "'";
$sql .= " WHERE id = " . $this->id;
// execute
db_exec ($sql);
}
/*
* decreaseRanksFromPoint ()
*/
function decreaseRanksFromPoint ($ref)
{
$fields = $this->getFieldsByBase (KB_BASE);
// iterate over fields
foreach ($fields as $field)
{
if ($field->getRank () > $ref)
$field->decreaseRank ();
}
}
/*
* decreaseRank ()
*/
function decreaseRank ()
{
$sql = "UPDATE " . KB_TABLE_FIELDS . " SET rank = '" . ((int) $this->rank - 1) . "'";
$sql .= " WHERE id = " . $this->id;
// execute
db_exec ($sql);
}
/*
* delete ()
*/
function delete ($recursive = false)
{
// recursive
$substances = KBModuleSubstance::getSubstancesByField ($this->id);
foreach ($substances as $substance)
$substance->delete ();
// recursive
$elements = KBModuleElement::getElementsByField ($this->id);
foreach ($elements as $element)
$element->delete (true);
// get config
$reset = false;
$configs = KBModuleConfig::getConfigsByBase (KB_BASE_UNDEFINED);
// recursive reset
foreach ($configs as $config)
{
// all kind of authentication
if ($config->getProperty () == KB_CONFIG_PROPERTY_AUTHENTICATION_USERNAME
|| $config->getProperty () == KB_CONFIG_PROPERTY_AUTHENTICATION_PASSWORD
|| $config->getProperty () == KB_CONFIG_PROPERTY_AUTHENTICATION_RECOVERY)
{
// field is positive id
if ($config->getIntValue () == $this->id)
{
$reset = true;
// reset
$config->setIntValue (KB_CONFIG_DEFAULT_AUTHENTICATION_UNDEFINED);
$config->store ();
}
}
}
// an authentication config has been resetted, then reset all
if ($reset)
{
foreach ($configs as $config)
{
// resource
if ($config->getProperty () == KB_CONFIG_PROPERTY_AUTHENTICATION_USERNAME
|| $config->getProperty () == KB_CONFIG_PROPERTY_AUTHENTICATION_PASSWORD
|| $config->getProperty () == KB_CONFIG_PROPERTY_AUTHENTICATION_RECOVERY)
{
$config->setIntValue (KB_CONFIG_DEFAULT_AUTHENTICATION_UNDEFINED);
$config->store ();
}
}
}
// stored object
$obj = new KBModuleField ();
$obj->load ($this->id);
// rank
if (!$recursive)
$this->decreaseRanksFromPoint ($obj->getRank ());
// sql
$sql = "DELETE FROM " . KB_TABLE_FIELDS . " WHERE id = " . $this->id;
// execute
return !db_exec ($sql) ? db_error () : null;
}
/*
* getFieldsByBase ()
*/
function getFieldsByBase ($base_id)
{
$r = array ();
// sql
$sql = "SELECT * FROM " . KB_TABLE_FIELDS;
$sql .= " WHERE base_id = '" . (int) $base_id . "'";
$sql .= " ORDER BY rank";
// load
$hash = db_loadList ($sql);
// bind
foreach ($hash as $row)
{
$obj = new KBModuleField ();
bindHashToObject ($row, $obj);
$r [] = $obj;
}
// return
return $r;
}
/*
* getVisibleFieldsByBase ()
*/
function getVisibleFieldsByBase ($base_id)
{
$r = array ();
// get
$fields = KBModuleField::getFieldsByBase ($base_id);
// filter
foreach ($fields as $field)
{
if ($field->isVisible ())
$r [] = $field;
}
// return
return $r;
}
}
?>