Content classes
This is an introduction on how to use the core classes of Exponential
at a low level. With these classes you can define what elements an object
should be made up of. Content Class is the name of this definition.
Creating a Content Class
Before the system can be populated with actual content objects a class
is needed. A class consists primary of a name, an identifier and a list of attributes.
Classes like objects are versioned albeit in a minimalistic fashion. A class either
is defined which means there is only one working copy, or it is a temporary meaning that
it has just been created but is currently being worked on or it is modified which means that
someone is working on a temporary version and a defined version is already exists.
Note: To make objects of a new class available to anonymous users you must edit the Anonymous role,
as this role has specified access limitation by class by default.
Creating the class is done by calling the static create() function in eZContentClass,
it takes one parameter which is the ID of the user creating the class. Usually this is the current
user which is fetched with eZUser::currentUser(). Once the create() functions returns
we have an eZContentClass object which is a temporary version and has creator, modifier and dates
filled in. We then set the name and identifier ourselves.
include_once( "kernel/classes/ezcontentclass.php" );
// create new empty class
$user =& eZUser::currentUser();
$class = eZContentClass::create( $user->attribute( "id" ) );
// set the name and identifier
$class->setAttribute( "name", "new class1" );
$class->setAttribute( "identifier", "new_class1" );
The class is now ready to be stored, we call store() to store a temporary version.
$class->store();
To make the class more useful we must add some class attributes. Attributes are created
in the same was as classes but take two parameters, the ID of the class and the stringname of
the data type. For now we'll assume that a data type called ezstring exists.
As you see we also set the name and identifier of the attribute.
$classAttribute = eZContentClassAttribute::create( $class->attribute( "id" ),
"ezstring" );
$classAttribute->setAttribute( "name", "new attribute1" );
$classAttribute->setAttribute( "identifier", "new_attribute1" );
$classAttribute->store();
// Add to attribute list
$classAttributes = array();
$classAttributes[] =& $classAttribute;
Now we're happy with our new class and want to make it appear as a defined class.
We do that by removing the temporary version, setting the version to 0 and storing it.
// Remove this class and all it's attributes (note the true parameter)
$class->remove( true );
// Change version to defined, again for all attributes as well
// this also fetches a list of temporary attributes
$class->setVersion( 0, true );
// Alternatively
$class->setVersion( 0, $classAttributes );
// Finally store the class and all it's attributes
$class->store( $classAttributes );
Fetching the classes
Now that we've stored the class we can find it among the defined list. We do that by fetching
classes which has version equal to 0. We will then get an array with eZContentClass objects.
// fetch all defined classes
$classes =& eZContentClass::fetchList( 0 );
Once we got the class list we can iterate over it and fetch the attributes and then print
out the id of each attribute.
foreach( $classes as $class )
{
// We must fetch the attributes ourselves, they do not get fetched automatically
$classAttributes =& $class->fetchAttributes();
foreach( $classAttributes as $classAttribute )
{
print( $classAttribute->attribute( "id" ) );
}
}
Fetching a class directly can be done if the class ID is known. We then modify the class
name and store it. It's very important to update the modification date, that it's easy for
external clients to find out if an object has changed.
// Fetch class with ID 42 if it exists
$class =& eZContentClass::fetch( 42 );
if ( $class !== null )
{
print( $class->attribute( "name" ) );
$class->setAttribute( "name", "Some other name" );
// Change modification date
$class->setAttribute( "modified", eZDateTime::currentTimeStamp() );
$class->store();
}
|