2012-10-09 09:47:33 +00:00
< ? php
/**
* Implementation of an generic object in the document management system
*
* @ category DMS
2013-02-14 11:10:53 +00:00
* @ package SeedDMS_Core
2012-10-09 09:47:33 +00:00
* @ license GPL2
* @ author Uwe Steinmann < uwe @ steinmann . cx >
* @ copyright Copyright ( C ) 2010 - 2012 Uwe Steinmann
* @ version Release : @ package_version @
*/
/**
* Class to represent a generic object in the document management system
*
2013-02-14 11:10:53 +00:00
* This is the base class for generic objects in SeedDMS .
2012-10-09 09:47:33 +00:00
*
* @ category DMS
2013-02-14 11:10:53 +00:00
* @ package SeedDMS_Core
2012-10-09 09:47:33 +00:00
* @ author Uwe Steinmann < uwe @ steinmann . cx >
* @ copyright Copyright ( C ) 2010 - 2012 Uwe Steinmann
* @ version Release : @ package_version @
*/
2013-02-14 11:10:53 +00:00
class SeedDMS_Core_Object { /* {{{ */
2012-10-09 09:47:33 +00:00
/**
* @ var integer unique id of object
*/
2013-01-24 08:23:24 +00:00
protected $_id ;
2012-10-09 09:47:33 +00:00
/**
* @ var array list of attributes
*/
2013-01-24 08:23:24 +00:00
protected $_attributes ;
2012-10-09 09:47:33 +00:00
/**
2017-10-24 10:12:38 +00:00
* @ var SeedDMS_Core_DMS back reference to document management system
2012-10-09 09:47:33 +00:00
*/
2013-01-24 08:23:24 +00:00
public $_dms ;
2012-10-09 09:47:33 +00:00
2017-10-24 12:00:56 +00:00
/**
* SeedDMS_Core_Object constructor .
* @ param $id
*/
2016-03-22 14:08:36 +00:00
function __construct ( $id ) { /* {{{ */
2012-10-09 09:47:33 +00:00
$this -> _id = $id ;
$this -> _dms = null ;
} /* }}} */
2019-08-07 06:10:05 +00:00
/**
* Check if this object is of a given type .
*
* This method must be implemened in the child class
*
* @ param string $type type of object
*/
public function isType ( $type ) { return false ;}
2017-10-24 12:00:56 +00:00
/**
2012-10-09 09:47:33 +00:00
* Set dms this object belongs to .
*
* Each object needs a reference to the dms it belongs to . It will be
* set when the object is created .
* The dms has a references to the currently logged in user
* and the database connection .
*
2017-10-24 12:00:56 +00:00
* @ param SeedDMS_Core_DMS $dms reference to dms
2012-10-09 09:47:33 +00:00
*/
2019-08-07 06:10:05 +00:00
public function setDMS ( $dms ) { /* {{{ */
2012-10-09 09:47:33 +00:00
$this -> _dms = $dms ;
} /* }}} */
2019-08-07 06:10:05 +00:00
public function getDMS () { /* {{{ */
return $this -> _dms ;
} /* }}} */
2017-10-24 12:00:56 +00:00
/**
2012-10-09 09:47:33 +00:00
* Return the internal id of the document
*
* @ return integer id of document
*/
2019-08-07 06:10:05 +00:00
public function getID () { return $this -> _id ; }
2012-10-09 09:47:33 +00:00
/**
* Returns all attributes set for the object
*
2017-10-24 10:12:38 +00:00
* @ return array | bool
*/
2019-08-07 06:10:05 +00:00
public function getAttributes () { /* {{{ */
2012-10-09 09:47:33 +00:00
if ( ! $this -> _attributes ) {
$db = $this -> _dms -> getDB ();
switch ( get_class ( $this )) {
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'document' ) :
2017-02-10 07:04:19 +00:00
$queryStr = " SELECT a.* FROM `tblDocumentAttributes` a LEFT JOIN `tblAttributeDefinitions` b ON a.`attrdef`=b.`id` WHERE a.`document` = " . $this -> _id . " ORDER BY b.`name` " ;
2012-10-09 09:47:33 +00:00
break ;
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'documentcontent' ) :
2017-02-10 07:04:19 +00:00
$queryStr = " SELECT a.* FROM `tblDocumentContentAttributes` a LEFT JOIN `tblAttributeDefinitions` b ON a.`attrdef`=b.`id` WHERE a.`content` = " . $this -> _id . " ORDER BY b.`name` " ;
2012-10-09 09:47:33 +00:00
break ;
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'folder' ) :
2017-02-10 07:04:19 +00:00
$queryStr = " SELECT a.* FROM `tblFolderAttributes` a LEFT JOIN `tblAttributeDefinitions` b ON a.`attrdef`=b.`id` WHERE a.`folder` = " . $this -> _id . " ORDER BY b.`name` " ;
2012-10-09 09:47:33 +00:00
break ;
default :
return false ;
}
$resArr = $db -> getResultArray ( $queryStr );
if ( is_bool ( $resArr ) && ! $resArr ) return false ;
$this -> _attributes = array ();
foreach ( $resArr as $row ) {
$attrdef = $this -> _dms -> getAttributeDefinition ( $row [ 'attrdef' ]);
2013-02-14 11:10:53 +00:00
$attr = new SeedDMS_Core_Attribute ( $row [ " id " ], $this , $attrdef , $row [ " value " ]);
2012-10-09 09:47:33 +00:00
$attr -> setDMS ( $this -> _dms );
$this -> _attributes [ $attrdef -> getId ()] = $attr ;
}
}
return $this -> _attributes ;
} /* }}} */
2017-10-24 10:12:38 +00:00
/**
* Returns an attribute of the object for the given attribute definition
*
* @ param SeedDMS_Core_AttributeDefinition $attrdef
* @ return array | string value of attritbute or false . The value is an array
* if the attribute is defined as multi value
*/
2019-08-07 06:10:05 +00:00
public function getAttribute ( $attrdef ) { /* {{{ */
2015-04-15 12:11:21 +00:00
if ( ! $this -> _attributes ) {
$this -> getAttributes ();
}
if ( isset ( $this -> _attributes [ $attrdef -> getId ()])) {
return $this -> _attributes [ $attrdef -> getId ()];
} else {
return false ;
}
} /* }}} */
/**
* Returns an attribute value of the object for the given attribute definition
*
2017-10-24 10:12:38 +00:00
* @ param SeedDMS_Core_AttributeDefinition $attrdef
2015-04-15 12:11:21 +00:00
* @ return array | string value of attritbute or false . The value is an array
* if the attribute is defined as multi value
*/
2019-08-07 06:10:05 +00:00
public function getAttributeValue ( $attrdef ) { /* {{{ */
2012-10-09 09:47:33 +00:00
if ( ! $this -> _attributes ) {
$this -> getAttributes ();
}
2014-02-20 20:03:03 +00:00
if ( isset ( $this -> _attributes [ $attrdef -> getId ()])) {
$value = $this -> _attributes [ $attrdef -> getId ()] -> getValue ();
if ( $attrdef -> getMultipleValues ()) {
$sep = substr ( $value , 0 , 1 );
2016-11-14 10:05:57 +00:00
$vsep = $attrdef -> getValueSetSeparator ();
/* If the value doesn ' t start with the separator used in the value set ,
* then assume that the value was not saved with a leading separator .
* This can happen , if the value was previously a single value from
* the value set and later turned into a multi value attribute .
*/
if ( $sep == $vsep )
return ( explode ( $sep , substr ( $value , 1 )));
else
return ( array ( $value ));
2014-02-20 20:03:03 +00:00
} else {
return $value ;
}
} else
2012-10-09 09:47:33 +00:00
return false ;
} /* }}} */
2017-10-24 10:12:38 +00:00
/**
* Returns an attribute value of the object for the given attribute definition
*
* This is a short cut for getAttribute ( $attrdef ) -> getValueAsArray () but
* first checks if the object has an attribute for the given attribute
* definition .
*
* @ param SeedDMS_Core_AttributeDefinition $attrdef
* @ return array | bool
* even if the attribute is not defined as multi value
*/
2019-08-07 06:10:05 +00:00
public function getAttributeValueAsArray ( $attrdef ) { /* {{{ */
2015-04-15 12:11:21 +00:00
if ( ! $this -> _attributes ) {
$this -> getAttributes ();
}
if ( isset ( $this -> _attributes [ $attrdef -> getId ()])) {
return $this -> _attributes [ $attrdef -> getId ()] -> getValueAsArray ();
} else
return false ;
} /* }}} */
/**
* Returns an attribute value of the object for the given attribute definition
*
* This is a short cut for getAttribute ( $attrdef ) -> getValueAsString () but
* first checks if the object has an attribute for the given attribute
* definition .
*
2017-10-24 10:12:38 +00:00
* @ param SeedDMS_Core_AttributeDefinition $attrdef
2015-04-15 12:11:21 +00:00
* @ return string value of attritbute or false . The value is always a string
* even if the attribute is defined as multi value
*/
2019-08-07 06:10:05 +00:00
public function getAttributeValueAsString ( $attrdef ) { /* {{{ */
2015-04-15 12:11:21 +00:00
if ( ! $this -> _attributes ) {
$this -> getAttributes ();
}
if ( isset ( $this -> _attributes [ $attrdef -> getId ()])) {
return $this -> _attributes [ $attrdef -> getId ()] -> getValue ();
} else
return false ;
} /* }}} */
2012-10-09 09:47:33 +00:00
/**
* Set an attribute of the object for the given attribute definition
*
2017-10-24 10:12:38 +00:00
* @ param SeedDMS_Core_AttributeDefinition $attrdef definition of attribute
* @ param array | string $value value of attribute , for multiple values this
2014-02-20 20:03:03 +00:00
* must be an array
2012-10-09 09:47:33 +00:00
* @ return boolean true if operation was successful , otherwise false
*/
2019-08-07 06:10:05 +00:00
public function setAttributeValue ( $attrdef , $value ) { /* {{{ */
2012-10-09 09:47:33 +00:00
$db = $this -> _dms -> getDB ();
if ( ! $this -> _attributes ) {
$this -> getAttributes ();
}
2016-01-29 15:44:05 +00:00
switch ( $attrdef -> getType ()) {
case SeedDMS_Core_AttributeDefinition :: type_boolean :
$value = ( $value === true || $value != '' || $value == 1 ) ? 1 : 0 ;
break ;
}
2014-02-20 20:03:03 +00:00
if ( $attrdef -> getMultipleValues () && is_array ( $value )) {
$sep = substr ( $attrdef -> getValueSet (), 0 , 1 );
$value = $sep . implode ( $sep , $value );
}
2012-10-09 09:47:33 +00:00
if ( ! isset ( $this -> _attributes [ $attrdef -> getId ()])) {
switch ( get_class ( $this )) {
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'document' ) :
2017-02-11 14:19:36 +00:00
$tablename = 'tblDocumentAttributes' ;
2017-02-10 07:04:19 +00:00
$queryStr = " INSERT INTO `tblDocumentAttributes` (`document`, `attrdef`, `value`) VALUES ( " . $this -> _id . " , " . $attrdef -> getId () . " , " . $db -> qstr ( $value ) . " ) " ;
2012-10-09 09:47:33 +00:00
break ;
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'documentcontent' ) :
2017-02-11 14:19:36 +00:00
$tablename = 'tblDocumentContentAttributes' ;
2017-02-10 07:04:19 +00:00
$queryStr = " INSERT INTO `tblDocumentContentAttributes` (`content`, `attrdef`, `value`) VALUES ( " . $this -> _id . " , " . $attrdef -> getId () . " , " . $db -> qstr ( $value ) . " ) " ;
2012-10-09 09:47:33 +00:00
break ;
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'folder' ) :
2017-02-11 14:19:36 +00:00
$tablename = 'tblFolderAttributes' ;
2017-02-10 07:04:19 +00:00
$queryStr = " INSERT INTO `tblFolderAttributes` (`folder`, `attrdef`, `value`) VALUES ( " . $this -> _id . " , " . $attrdef -> getId () . " , " . $db -> qstr ( $value ) . " ) " ;
2012-10-09 09:47:33 +00:00
break ;
default :
return false ;
}
$res = $db -> getResult ( $queryStr );
if ( ! $res )
return false ;
2017-02-11 14:19:36 +00:00
$attr = new SeedDMS_Core_Attribute ( $db -> getInsertID ( $tablename ), $this , $attrdef , $value );
2012-10-09 09:47:33 +00:00
$attr -> setDMS ( $this -> _dms );
$this -> _attributes [ $attrdef -> getId ()] = $attr ;
return true ;
}
$this -> _attributes [ $attrdef -> getId ()] -> setValue ( $value );
return true ;
} /* }}} */
2013-05-28 07:01:04 +00:00
/**
* Remove an attribute of the object for the given attribute definition
2017-10-24 10:12:38 +00:00
* @ param SeedDMS_Core_AttributeDefinition $attrdef
2013-05-28 07:01:04 +00:00
* @ return boolean true if operation was successful , otherwise false
*/
2019-08-07 06:10:05 +00:00
public function removeAttribute ( $attrdef ) { /* {{{ */
2013-05-28 07:01:04 +00:00
$db = $this -> _dms -> getDB ();
if ( ! $this -> _attributes ) {
$this -> getAttributes ();
}
if ( isset ( $this -> _attributes [ $attrdef -> getId ()])) {
switch ( get_class ( $this )) {
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'document' ) :
2017-02-10 07:04:19 +00:00
$queryStr = " DELETE FROM `tblDocumentAttributes` WHERE `document`= " . $this -> _id . " AND `attrdef`= " . $attrdef -> getId ();
2013-05-28 07:01:04 +00:00
break ;
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'documentcontent' ) :
2017-02-10 07:04:19 +00:00
$queryStr = " DELETE FROM `tblDocumentContentAttributes` WHERE `content`= " . $this -> _id . " AND `attrdef`= " . $attrdef -> getId ();
2013-05-28 07:01:04 +00:00
break ;
2014-12-08 13:34:54 +00:00
case $this -> _dms -> getClassname ( 'folder' ) :
2017-02-10 07:04:19 +00:00
$queryStr = " DELETE FROM `tblFolderAttributes` WHERE `folder`= " . $this -> _id . " AND `attrdef`= " . $attrdef -> getId ();
2013-05-28 07:01:04 +00:00
break ;
default :
return false ;
}
$res = $db -> getResult ( $queryStr );
if ( ! $res )
return false ;
unset ( $this -> _attributes [ $attrdef -> getId ()]);
}
return true ;
} /* }}} */
2012-10-09 09:47:33 +00:00
} /* }}} */