<?php
/**
* tv2Xdb document class
*
* @package tv2-engine
* @author Emilis Dambauskas (hide@address.com)
* @copyright 20022003 Emilis Dambauskas under {@link http://opensource.org/licenses/artistic-license.php Artistic license}
* @version $Id: tv2Document.class.php,v 1.5 2003/07/10 05:09:07 lunaticlt Exp $
* @class tv2Document
*/
class tv2Document
{
/**
* document id
* @attribute public int $id
*/
var $id;
/**
* document type
* @attribute public string $type
*/
var $type;
/**
* document modify (change) time
* @attribute public int $ctime
*/
var $ctime;
/**
* document path
* @attribute public string $path
*/
var $path;
/**
* document name
* @attribute public string $name
*/
var $name;
/**
* document ident
* @attribute public string $ident
*/
var $ident;
/**
* document field array (numerically indexed)
* @attribute public array $fields
*/
var $fields;
/**
* document field values array (indexed by field name)
* @attribute public array $fv
*/
var $fv;
/**
* document permissions
* @attribute private array $permissions
*/
var $permissions;
// attributes not commented: version control is still experimental.
var $vcontext;
var $vinfo;
var $versions;
var $vchanged;
/**
* {@link tv2Xdb} object
* @attribute private object $xdb
*/
var $xdb;
/**
* Connects to tv2Xdb
*
* @constructor tv2Document
* @param optional boolean empty if set to TRUE tv2Xdb will not be connected.
* @use $xdb
*/
function tv2Document($empty = FALSE)
{
if (!$empty)
$this->xdb = &$GLOBALS['xdb'];
// documents are not versioned by default
$this->vcontext = NULL;
$this->vinfo = NULL;
$this->versions = array();
$this->vchanged = FALSE;
}
//----------------- MAPPABLE METHODS -----------------------------------------
/**
* Sends an error to the {@link $errors} object and returns FALSE
*
* @method private error
* @return boolean always FALSE
* @param string $msg error message
* @use $errors
*/
function error($msg)
{
$GLOBALS['errors']->add(get_class($this).': '.$msg);
return FALSE;
}
//----------------- DOCUMENT METHODS -----------------------------------------
/**
* create a new document
*
* @method public create
* @return boolean TRUE on success, FALSE on failure
* @param string ident document ident
* @param string type document type
*/
function create($ident, $type)
{
if (!$type)
return $this->error('Please supply a type for the document.');
if ($this->xdb->documentExists($ident))
return $this->error('Cannot create document '.$ident.': the document exists.');
$this->id = NULL;
$this->ident = $ident;
$this->type = $type;
$this->ctime = time();
$this->name = preg_replace('|^.*/([^/]+)$|', '\1', $this->ident);
$this->path = preg_replace('|/[^/]+$|', '', $this->ident);
$this->fields = array();
$this->fv = array();
$this->permissions = array();
return TRUE;
}
/**
* Opens an existing document from the tv2Xdb
*
* @method public open
* @return boolean TRUE on success, FALSE on failure
* @param mixed id document id/ident
* @param optional array vinfo version info (do not use: still experimental)
*/
function open($id, $vinfo = NULL)
{
if ($this = $this->xdb->readDocument($id, $vinfo))
return TRUE;
else
return $this;
}
/**
* Saves a document in the tv2Xdb
*
* @method public save
* @return boolean TRUE on success, FALSE on failure
*/
function save()
{
if ($this->xdb->writeDocument($this))
return TRUE;
else
return $this->error('Couldnot save document '.$this->ident.' due to a xdb error.');
}
/**
* Moves document in the tv2Xdb tree
*
* @method public move
* @return boolean TRUE on success, FALSE on failure
* @param string ident target ident
* @param optional boolean overwrite Should we overwrite if doc exists at new location? (default: FALSE)
*/
function move($ident, $overwrite = FALSE)
{
return $this->xdb->moveDocument($this, $ident, $overwrite);
}
/**
* Deletes a document from tv2Xdb
*
* @method public remove
* @return boolean TRUE on success, FALSE on failure
*/
function remove()
{
return $this->xdb->removeDocument($this);
}
/**
* Changes document type
*
* @method public changeType
* @param string type new type for the document
* @return boolean always TRUE
*/
function changeType($type)
{
$this->type = $type;
return TRUE;
}
//----------------- FIELD METHODS --------------------------------------------
/**
* Adds new field to the document
*
* @method public addFIeld
* @param string name field name
* @param optional string value field value
* @param optional string type field type
*/
function addField($name, $value = '', $type = '')
{
if (!$type)
$type = 'text';
$this->fields[] = array('name' => $name, 'type' => $type, 'value' => $value);
$this->fv[$name] = $value;
}
/**
* Changes field name type and value. If the field does not exist adds a new field.
*
* @method public changeField
* @return boolean TRUE on success, FALSE on failure
* @param string oldname field name
* @param string newname new name for the field (use same as oldname if you don't want to change it)
* @param optional string value new value for the field (pass NULL if you don't want to change it)
* @param optional string type new type for the field (pass NULL if you don't want to change it)
*/
function changeField($oldname, $newname, $value = NULL, $type = NULL)
{
if (!is_array($this->fields) || !isset($this->fv[$oldname]))
return $this->addField($newname, $value, $type);
foreach ($this->fields as $num => $field)
if ($field['name'] == $oldname)
{
if (!$newname)
return $this->removeField($oldname);
if ($newname != $oldname)
{
$this->fields[$num]['name'] = $newname;
unset($this->fv[$oldname]);
$this->fv[$newname] = $field['value'];
}
if ($value !== NULL)
{
$this->fields[$num]['value'] = $value;
$this->fv[$newname] = $value;
}
if ($type !== NULL)
$this->fields[$num]['type'] = $type;
return TRUE;
}
return $this->error('The field you are trying to change ['.$oldname.'] does not exist in document '.$this->ident.'.');
}
/**
* Deletes a field in a document
*
* @method public removeField
* @return boolean TRUE on success, FALSE on failure
* @param string name field name
*/
function removeField($name)
{
foreach ($this->fields as $n => $field)
{
if ($field['name'] == $name)
{
unset($this->fields[$n]);
unset($this->fv[$name]);
return TRUE;
}
}
return $this->error('The field you are trying to remove ['.$name.'] does not exist.');
}
/**
* Deletes all fields in a document
*
* @method public removeFields
*/
function removeFields()
{
$this->fields = array();
$this->fv = array();
}
/**
* Empties document fields
*
* @method public removeFieldValues
*/
function removeFieldValues()
{
foreach ($this->fields as $i=>$field)
{
$this->fields[$i]['value'] = '';
$this->fv[$field['name']] = '';
}
}
//----------------- PERMISSION METHODS ---------------------------------------
/**
* add new permission for the document
*
* @method public addPermission
* @return boolean TRUE on success, FALSE on failure
* @param string ug user 'u' or group 'g'?
* @param string name user/group name
* @param string permission o/w/r/x/d
*/
function addPermission($ug, $name, $permission)
{
if ($ug == 'u')
$k = 'users';
elseif ($ug == 'g')
$k = 'groups';
else
return $this->error('Unknown $ug type in addPermission()');
$this->permissions[$k][$name] = $permission;
return TRUE;
}
/**
* Replace a permission for a document
*
* @method public changePermission
* @return boolean TRUE on success, FALSE on failure
* @param string ug user 'u' or group 'g'?
* @param string name user/group name
* @param string permission o/w/r/x/d
*/
function changePermission($ug, $name, $permission)
{
return $this->addPermission($ug, $name, $permission);
}
/**
* Removes permissions for user or group on the document
*
* @method public removePermission
* @return boolean TRUE on success, FALSE on failure
* @param string ug user 'u' or group 'g'?
* @param string name user/group name
*/
function removePermission($ug, $name)
{
if ($ug == 'u')
$k = 'users';
elseif ($ug == 'g')
$k = 'groups';
else
return $this->error('Unknown $ug type in removePermission()');
unset($this->permissions[$k][$name]);
return TRUE;
}
/**
* Remove all permissions on the document
*
* @method public removePermissions
*/
function removePermissions()
{
$this->permissions = array();
}
//----------------- VERSION METHODS ------------------------------------------
/**
* Experimental. Sets version of the document
*
* @method public setVersion
* @return boolean always TRUE
* @param string lang document language
* @param string statuts document status
*/
function setVersion($lang, $status)
{
$this->vinfo = array('language'=>$lang, 'status'=>$status, 'vtime'=>time());
return TRUE;
}
/**
* Experimental. Changes version of the document
*
* @method public changeVersion
* @return boolean always TRUE
* @param string lang document language
* @param string statuts document status
*/
function changeVersion($lang, $status)
{
$this->vchanged = $this->vinfo;
$this->vinfo = array('language'=>$lang, 'status'=>$status, 'vtime'=>time());
return TRUE;
}
/**
* Experimental. Removes a version of the document.
*
* @method public removeVersion
* @return boolean TRUE on success, FALSE on failure
*/
function removeVersion()
{
return $this->xdb->removeDocument($this, $this->vinfo);
}
/**
* Experimental. Changes context in which versions status has meaning
*
* @method public changeContext
* @return boolean always TRUE
* @param optional string context context name
*/
function changeContext($context = NULL)
{
$this->vcontext = $context;
return TRUE;
}
/**
* Experimental. Start version control for the document.
*
* @method public startVersioning
* @return boolean always TRUE
* @param string context context
* @param string lang language
* @param string status status
*/
function startVersioning($context, $lang, $status)
{
$this->changeContext($context);
$this->setVersion($lang, $status);
return TRUE;
}
/**
* Experimental. Stop version control for the document.
*
* @method public stopVersioning
* @return boolean TRUE on success, FALSE on failure
*/
function stopVersioning()
{
return $this->changeContext(NULL);
}
}
?>