Commit 131c22ad authored by Penny Leach's avatar Penny Leach
Browse files

work on artefact base class, some work on plugin classes

parent 2af94bee
......@@ -29,7 +29,15 @@ defined('INTERNAL') || die();
class PluginArtefactInternal extends PluginArtefact {
public static function get_artefact_types() {
return array('profile', 'file', 'folder', 'image');
return array('file', 'folder', 'image',
'firstname', 'lastname', 'studentid',
'preferredname', 'introduction',
'email', 'officialwebsite', 'personalwebsite',
'blog', 'address', 'town', 'city',
'country', 'homenumber', 'businessnumber',
'mobilenumber', 'faxnumber', 'icqnumber',
'msnnumber'
);
}
public static function get_plugin_name() {
......@@ -70,7 +78,76 @@ class PluginArtefactInternal extends PluginArtefact {
}
}
// @todo write ArtefactType$name classes for each of the
// types returned by get_artefact_types
class ArtefactTypeProfile extends ArtefactType {
public function commit() {
$this->commit_basic();
}
public function delete() {
$this->delete_basic();
}
public function render($format, $options) {
}
public function get_icon() {
}
public static function get_render_list() {
}
public static function can_render_to($format) {
}
public static function get_mandatory_fields() {
return array('firstname', 'lastname', 'studentid', 'email');
}
}
class ArtefactTypeCachedProfile extends ArtefactTypeProfile {
public function commit() {
$this->commit_basic();
$field = $this->get_artefact_type();
set_field('usr', $field, $this->title, 'id', $this->owner);
}
public function delete() {
$this->delete_basic();
$field = $this->get_artefact_type();
set_field('usr', $field, null, 'id', $this->owner);
}
}
class ArtefactTypeFirstname extends ArtefactTypeCachedProfile {}
class ArtefactTypeLastname extends ArtefactTypeCachedProfile {}
class ArtefactTypePreferredname extends ArtefactTypeCachedProfile {}
class ArtefactTypeEmail extends ArtefactTypeCachedProfile {}
class ArtefactTypeStudentid extends ArtefactTypeProfile {}
class ArtefactTypeIntroduction extends ArtefactTypeProfile {}
class ArtefactTypeOfficialwebsite extends ArtefactTypeProfile {}
class ArtefactTypePersonalwebsite extends ArtefactTypeProfile {}
class ArtefactTypeBlog extends ArtefactTypeProfile {}
class ArtefactTypeAddress extends ArtefactTypeProfile {}
class ArtefactTypeTown extends ArtefactTypeProfile {}
class ArtefactTypeCity extends ArtefactTypeProfile {}
class ArtefactTypeCountry extends ArtefactTypeProfile {}
class ArtefactTypeHomenumber extends ArtefactTypeProfile {}
class ArtefactTypeBusinessnumber extends ArtefactTypeProfile {}
class ArtefactTypeMobilenumber extends ArtefactTypeProfile {}
class ArtefactTypeFaxnumber extends ArtefactTypeProfile {}
class ArtefactTypeIcqnumber extends ArtefactTypeProfile {}
class ArtefactTypeMsnnumber extends ArtefactTypeProfile {}
class ArtefactTypeFolder extends ArtefactTypeProfile {}
class ArtefactTypeFile extends ArtefactTypeProfile {}
class ArtefactTypeImage extends ArtefactTypeFile {}
?>
......@@ -98,12 +98,13 @@ abstract class ArtefactType {
protected $dirty;
protected $parentdirty;
protected $id;
protected $type;
protected $artefacttype;
protected $owner;
protected $container;
protected $parent;
protected $ctime;
protected $mtime;
protected $vtime;
protected $atime;
protected $locked;
protected $title;
protected $description;
......@@ -132,7 +133,7 @@ abstract class ArtefactType {
}
}
foreach ((array)$data as $field => $value) {
if (property_exists($field)) {
if (property_exists($this, $field)) {
$this->{$field} = $value;
}
}
......@@ -140,6 +141,8 @@ abstract class ArtefactType {
else {
$this->ctime = time();
}
$this->atime = time();
$this->artefacttype = $this->get_artefact_type();
}
public function get_views_instances() {
......@@ -165,7 +168,7 @@ abstract class ArtefactType {
if ($children = $this->get_children_metadata()) {
$this->childreninstances = array();
foreach ($children as $child) {
$classname = $child->artefacttype;
$classname = generate_artefact_class_name($child->artefacttype);
$instance = new $classname($child->id, $child);
$this->childreninstances[] = $instance;
}
......@@ -202,7 +205,8 @@ abstract class ArtefactType {
if (!isset($this->parentinstance)) {
$this->parentinstance = false;
if ($parent = $this->get_parent_metadata()) {
$classname = $parent->artefacttype;
$classname = generate_artefact_class_name($parent->artefacttype);
// @todo this won't work.
$this->parentinstance = new $classname($parent->id, $parent);
}
}
......@@ -223,19 +227,20 @@ abstract class ArtefactType {
}
public function get($field) {
if (!property_exists($field)) {
if (!property_exists($this, $field)) {
throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
}
return $this->{$field};
}
public function set($field, $value) {
if (property_exists($field)) {
if (property_exists($this, $field)) {
$this->{$field} = $value;
$this->dirty = true;
if ($field == 'parent') {
$this->parentdirty = true;
}
$this->mtime = time();
return true;
}
throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
......@@ -246,11 +251,12 @@ abstract class ArtefactType {
* artefact cache as dirty if necessary.
*/
public function __destruct() {
try {
if (!empty($this->dirty)) {
$this->commit();
}
if (!empty($this->parentdirty)) {
if (!empty($this->parent) && !record_exists('artefact_parent_cache', 'aretfact', $this->id)) {
if (!empty($this->parent) && !record_exists('artefact_parent_cache', 'artefact', $this->id)) {
$apc = new StdClass;
$apc->artefact = $this->id;
$apc->parent = $this->parent;
......@@ -260,22 +266,65 @@ abstract class ArtefactType {
set_field_select('artefact_parent_cache', 'dirty', 1,
'artefact = ? OR parent = ?', array($this->id, $this->id));
}
}
catch (Exception $e) {
log_warn($e->GetMessage());
}
}
public function is_container() {
return false;
}
/**
* As commit is abstract, subclasses
* can use this as a helper to update
* the contents of the artefact table
*/
protected function commit_basic() {
$fordb = new StdClass;
foreach (get_object_vars($this) as $k => $v) {
$fordb->{$k} = $v;
if (in_array($k, array('mtime', 'ctime', 'atime')) && !empty($v)) {
$fordb->{$k} = db_format_timestamp($v);
}
}
if (empty($this->id)) {
$this->id = insert_record('artefact', $fordb, 'id', true);
}
else {
update_record('artefact', $fordb, 'id');
}
$this->dirty = false;
}
/**
* Saves any changes to the database
* for basic commits, use {@link commit_basic}
* @abstract
*/
public abstract function commit();
/**
* As delete is abstract, subclasses
* can use this to clear out the artefact
* table and set the parentdirty flag
*/
protected function delete_basic() {
delete_records('artefact', 'id', $this->id);
$this->dirty = false;
$this->parentdirty = true;
}
/**
* Deletes current instance
* you MUST set $this->parentdirty to true
* when delete is called.
* for basic delete, use {@link delete_basic}
* @abstract
*/
public abstract function delete();
......@@ -311,16 +360,43 @@ abstract class ArtefactType {
* returns array of formats can render to (constants)
* @abstract
*/
public static abstract function get_render_list($format);
public static abstract function get_render_list();
/**
* returns boolean for can render to given format
* @abstract
*/
public static abstract function can_render_to($format);
// ******************** HELPER FUNCTIONS ******************** //
protected function get_artefact_type() {
$classname = get_class($this);
$type = strtolower(substr($classname, strlen('ArtefactType')));
if (!record_exists('artefact_installed_type', 'name', $type)) {
throw new InvalidArgumentException("Classname $classname not a valid artefact type");
}
return $type;
}
}
// helper functions for artefacts in general
function artefact_check_plugin_sanity($pluginname) {
$classname = generate_class_name('artefact', $pluginname);
safe_require('artefact', $pluginname);
$types = call_static_method($classname, 'get_artefact_types');
foreach ($types as $type) {
$typeclassname = generate_artefact_class_name($type);
if (!class_exists($typeclassname)) {
throw new Exception("class $typeclassname for type $type in plugin $pluginname was missing");
}
}
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment