<?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
* KBModuleEntry
*/
class KBModuleEntry extends CDpObject
{
var $id = 0;
var $base_id;
var $name;
var $visible;
/*
* constructor
*/
function KBModuleEntry ()
{
$primaryKey = 'id';
$this->CDpObject (KB_TABLE_ENTRIES, $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);
}
/*
* store ()
*/
function store ()
{
// insert
if ((int) $this->id == 0)
{
// set
$this->setBaseId (KB_BASE);
$this->setVisible ($this->visible);
// execute
$this->storeRecord ();
// keywords
$this->storeKeywords ();
// fields
$this->storeFields ();
// update
} else {
// stored object
$obj = new KBModuleEntry ();
$obj->load ($this->id);
// set
$obj->setName ($this->name);
$obj->setVisible ($this->visible);
// execute
$obj->storeRecord ();
// keywords
$obj->storeKeywords ();
// fields
$obj->storeFields (true);
}
}
/*
* storeKeywords ()
*/
function storeKeywords ()
{
// groups
$groups = KBModuleGroup::getGroupsByBase (KB_BASE);
// cleanup relations
$cleanup = KBModuleRelation::getRelationsByEntry ($this->getId ());
foreach ($cleanup as $clean)
$clean->delete ();
// insert relations
foreach ($groups as $group)
{
// post
$relations = $_POST ['keywords_' . $group->getId ()];
// only on selected groups
if (is_array ($relations))
{
// iterate
foreach ($relations as $key)
{
// instanciate
$relation = new KBModuleRelation ();
// set
$relation->setEntryId ($this->getId ());
$relation->setKeywordId ((int) $key);
// store
$relation->store ();
}
}
}
}
/*
* storeFields ()
*/
function storeFields ($isUpdate = false)
{
// fields
$fields = KBModuleField::getFieldsByBase (KB_BASE);
// insert substances
foreach ($fields as $field)
{
// not on labels
if ($field->isType (KB_FIELD_LABEL))
continue;
// instanciate
$substance = new KBModuleSubstance ();
// set
$substance->setEntryId ($this->getId ());
$substance->setFieldId ($field->getId ());
// text
if ($field->getType () == KB_FIELD_TEXT) {
$substance->setTextValue ($_POST ['field_' . $field->getId ()]);
// html
} elseif ($field->getType () == KB_FIELD_HTML) {
$substance->setTextValue ($_POST ['field_' . $field->getId ()]);
// file & image
} elseif ($field->getType () == KB_FIELD_FILE || $field->getType () == KB_FIELD_IMAGE) {
$tmpName = $_FILES ['field_' . $field->getId ()]['tmp_name'];
$mimeType = $_FILES ['field_' . $field->getId ()]['type'];
$oriName = $_FILES ['field_' . $field->getId ()]['name'];
// file exists
if (strlen (trim ($tmpName)) > 0 && file_exists ($tmpName) && filesize ($tmpName) > 0)
{
$stream = fopen ($tmpName, 'rb');
$bytes = addslashes (fread ($stream, filesize ($tmpName)));
// set
$substance->setTextValue ($oriName);
$substance->setBinaryValue ($bytes);
$substance->setMimeType ($mimeType);
// information
if ($field->getType () == KB_FIELD_FILE)
$substance->setInformation ($_POST ['field_' . $field->getId () . '_information']);
// file is not submitted
// but we are in update case. empty file means no update.
} elseif ($isUpdate) {
// want to remove?
if ((boolean) $_POST ['field_' . $field->getId () . '_remove'])
{
// reset fields
$substance->setTextValue ('');
$substance->setBinaryValue ('');
$substance->setMimeType ('');
// keep binary, substance is just updated
} else {
// get old record
$oldSubstance = KBModuleSubstance::getSubstance ($this->getId (), $field->getId ());
// old substance exists (if not, the field has been added after this entry)
if ((int) $oldSubstance->getEntryId () > 0)
{
// set
$substance->setTextValue ($oldSubstance->getTextValue ());
$substance->setBinaryValue (addslashes ($oldSubstance->getBinaryBytes ()));
$substance->setMimeType ($oldSubstance->getMimeType ());
// information
if ($field->getType () == KB_FIELD_FILE)
$substance->setInformation ($_POST ['field_' . $field->getId () . '_information']);
// old substance not exists
} else {
// empty fields
$substance->setTextValue ('');
$substance->setBinaryValue ('');
$substance->setMimeType ('');
$substance->setInformation ('');
}
}
}
// link
} elseif ($field->getType () == KB_FIELD_LINK) {
$substance->setTextValue ($_POST ['field_' . $field->getId ()]);
// information
if (strlen ($substance->getTextValue ()) > 0)
$substance->setInformation ($_POST ['field_' . $field->getId () . '_information']);
// opens in a new window
$substance->setMimeType ((int) $_POST ['field_' . $field->getId () . '_target']);
// email
} elseif ($field->getType () == KB_FIELD_EMAIL) {
$substance->setTextValue ($_POST ['field_' . $field->getId ()]);
// date
} elseif ($field->getType () == KB_FIELD_DATE) {
$day = (int) $_POST ['field_' . $field->getId () . '_day'];
$month = (int) $_POST ['field_' . $field->getId () . '_month'];
$year = (int) $_POST ['field_' . $field->getId () . '_year'];
$textValue = str_pad ((string) $year, 4, '0', STR_PAD_LEFT) .
str_pad ((string) $month, 2, '0', STR_PAD_LEFT) .
str_pad ((string) $day, 2, '0', STR_PAD_LEFT);
$substance->setTextValue ($textValue == '00000000' ? '' : $textValue);
// password
} elseif ($field->getType () == KB_FIELD_PASSWORD) {
$pwd = trim ($_POST ['field_' . $field->getId ()]);
// on update
if ($isUpdate) {
// password redefined
if (strlen ($pwd) > 0)
$substance->setTextValue (sha1 ($pwd));
// keep previous password
else
{
// get old record
$oldSubstance = KBModuleSubstance::getSubstance ($this->getId (), $field->getId ());
// set
$substance->setTextValue ($oldSubstance->getTextValue ());
}
// on insert
} else {
// set
if (strlen ($pwd) > 0)
$substance->setTextValue (sha1 ($pwd));
// empty
else
$substance->setTextValue ('');
}
}
// commit
$substance->store ();
}
}
/*
* storeRecord ()
*/
function storeRecord ()
{
parent::store ();
}
/*
* delete ()
*/
function delete ()
{
// recursive
$relations = KBModuleRelation::getRelationsByEntry ($this->id);
foreach ($relations as $relation)
$relation->delete ();
// recursive
$substances = KBModuleSubstance::getSubstancesByEntry ($this->id);
foreach ($substances as $substance)
$substance->delete ();
// sql
$sql = "DELETE FROM " . KB_TABLE_ENTRIES . " WHERE id = " . $this->id;
// execute
return !db_exec ($sql) ? db_error () : null;
}
/*
* getEntriesByBase ()
*/
function getEntriesByBase ($base_id, $limit = null, $visibleOnly = false)
{
$r = array ();
// sql
$sql = "SELECT * FROM " . KB_TABLE_ENTRIES;
$sql .= " WHERE base_id = '" . (int) $base_id . "'";
if ($visibleOnly)
$sql .= " AND visible = 1";
$sql .= " ORDER BY id DESC";
if (!is_null ($limit) && $limit > 0)
$sql .= " LIMIT 0, " . (int) $limit;
// load
$hash = db_loadList ($sql);
// bind
foreach ($hash as $row)
{
$obj = new KBModuleEntry ();
bindHashToObject ($row, $obj);
$r [] = $obj;
}
// return
return $r;
}
/*
* getEntriesByBaseOrdered ()
*/
function getEntriesByBaseOrdered ($base_id)
{
$r = array ();
// sql
$sql = "SELECT * FROM " . KB_TABLE_ENTRIES;
$sql .= " WHERE base_id = '" . (int) $base_id . "'";
$sql .= " ORDER BY name";
// load
$hash = db_loadList ($sql);
// bind
foreach ($hash as $row)
{
$obj = new KBModuleEntry ();
bindHashToObject ($row, $obj);
$r [] = $obj;
}
// return
return $r;
}
/*
* getVisibleEntriesByBase ()
*/
function getVisibleEntriesByBase ($base_id, $limit = null)
{
return KBModuleEntry::getEntriesByBase ($base_id, $limit, true);
}
/*
* hasEntriesInBase ()
*/
function hasEntriesInBase ($base_id, $contributor = false)
{
if ($contributor)
return count (KBModuleEntry::getEntriesByBase ($base_id)) > 0;
else
return count (KBModuleEntry::getVisibleEntriesByBase ($base_id)) > 0;
}
}
?>