Permalink
Browse files

Replace endless switches with something a little easier on the eye (a…

…nd fix missing approved time conversion)
  • Loading branch information...
flack committed Dec 5, 2018
1 parent d8cf6d7 commit 440e2c521e47d2efc35a200fc0221fdebc1be9a9
Showing with 60 additions and 112 deletions.
  1. +60 −112 lib/midcom/helper/metadata.php
@@ -103,6 +103,13 @@ class midcom_helper_metadata
*/
private $_datamanager;
private $field_config = [
'readonly' => ['creator', 'created', 'revisor', 'revised', 'locker', 'locked', 'revision', 'size', 'deleted', 'exported', 'imported'],
'timebased' => ['created', 'revised', 'published', 'locked', 'approved', 'schedulestart', 'scheduleend', 'exported', 'imported'],
'person' => ['creator', 'revisor', 'locker', 'approver'],
'other' => ['authors', 'owner', 'hidden', 'navnoentry', 'score', 'revision', 'size', 'deleted']
];
/**
* This will construct a new metadata object for an existing content object.
*
@@ -144,7 +151,7 @@ public function get($key)
}
if (!isset($this->_cache[$key])) {
$this->_retrieve_value($key);
$this->_cache[$key] = $this->_retrieve_value($key);
}
return $this->_cache[$key];
@@ -165,7 +172,7 @@ public function __isset($key)
}
if (!isset($this->_cache[$key])) {
$this->_retrieve_value($key);
$this->_cache[$key] = $this->_retrieve_value($key);
}
return isset($this->_cache[$key]);
@@ -291,55 +298,32 @@ private function _set_property($key, $value)
if (is_object($value)) {
$classname = get_class($value);
debug_add("Can not set metadata '{$key}' property with '{$classname}' object as value", MIDCOM_LOG_WARN);
return false;
}
switch ($key) {
// Read-only properties
case 'creator':
case 'created':
case 'revisor':
case 'revised':
case 'locker':
case 'locked':
case 'revision':
case 'size':
case 'deleted':
case 'exported':
case 'imported':
midcom_connection::set_error(MGD_ERR_ACCESS_DENIED);
return false;
if (in_array($key, $this->field_config['readonly'])) {
midcom_connection::set_error(MGD_ERR_ACCESS_DENIED);
return false;
}
// Writable properties
case 'published':
case 'schedulestart':
case 'scheduleend':
if (!is_numeric($value) || $value == 0) {
$value = null;
} else {
$value = new midgard_datetime(gmstrftime('%Y-%m-%d %T', $value));
}
$this->__metadata->$key = $value;
return true;
case 'approver':
case 'approved':
// Prevent lock changes from creating new revisions
$this->__object->_use_rcs = false;
// Fall through
case 'authors':
case 'owner':
case 'hidden':
case 'navnoentry':
case 'score':
$this->__metadata->$key = $value;
return true;
if (in_array($key, ['approver', 'approved'])) {
// Prevent lock changes from creating new revisions
$this->__object->_use_rcs = false;
}
if (in_array($key, $this->field_config['timebased'])) {
if (!is_numeric($value) || $value == 0) {
$value = null;
} else {
$value = new midgard_datetime(gmstrftime('%Y-%m-%d %T', $value));
}
} elseif (!in_array($key, $this->field_config['other']) && $key !== 'approver') {
// Fall-back for non-core properties
default:
return $this->__object->set_parameter('midcom.helper.metadata', $key, $value);
return $this->__object->set_parameter('midcom.helper.metadata', $key, $value);
}
$this->__metadata->$key = $value;
return true;
}
/**
@@ -383,80 +367,44 @@ private function on_update($key = false)
*/
private function _retrieve_value($key)
{
switch ($key) {
// Time-based properties
case 'created':
case 'revised':
case 'published':
case 'locked':
case 'approved':
case 'schedulestart':
case 'scheduleend':
case 'exported':
case 'imported':
if (isset($this->__metadata->$key)) {
//This is ugly, but seems the only possible way...
if ((string) $this->__metadata->$key === "0001-01-01T00:00:00+00:00") {
$value = 0;
if (in_array($key, $this->field_config['timebased'])) {
// This is ugly, but seems the only possible way...
if ( isset($this->__metadata->$key)
&& (string) $this->__metadata->$key !== "0001-01-01T00:00:00+00:00") {
return (int) $this->__metadata->$key->format('U');
}
return 0;
}
if (in_array($key, $this->field_config['person'])) {
if (!$this->__metadata->$key) {
// Fall back to "Midgard root user" if person is not found
static $root_user_guid = null;
if (!$root_user_guid) {
$mc = new midgard_collector('midgard_person', 'id', 1);
$mc->set_key_property('guid');
$mc->execute();
$guids = $mc->list_keys();
if (empty($guids)) {
$root_user_guid = 'f6b665f1984503790ed91f39b11b5392';
} else {
$value = (int) $this->__metadata->$key->format('U');
$root_user_guid = key($guids);
}
} else {
$value = 0;
}
break;
// Person properties
case 'creator':
case 'revisor':
case 'locker':
case 'approver':
$value = $this->__metadata->$key;
if (!$value) {
// Fall back to "Midgard root user" if person is not found
static $root_user_guid = null;
if (!$root_user_guid) {
$mc = new midgard_collector('midgard_person', 'id', 1);
$mc->set_key_property('guid');
$mc->execute();
$guids = $mc->list_keys();
if (empty($guids)) {
$root_user_guid = 'f6b665f1984503790ed91f39b11b5392';
} else {
$root_user_guid = key($guids);
}
}
$value = $root_user_guid;
}
break;
// Other midgard_metadata properties
case 'revision':
case 'hidden':
case 'navnoentry':
case 'size':
case 'deleted':
case 'score':
case 'authors':
case 'owner':
$value = $this->__metadata->$key;
break;
return $root_user_guid;
}
return $this->__metadata->$key;
}
if (!in_array($key, $this->field_config['other'])) {
// Fall-back for non-core properties
default:
$dm = $this->get_datamanager();
if (!$dm->get_schema()->has_field($key)) {
// Fall back to the parameter reader for non-core MidCOM metadata params
$value = $this->__object->get_parameter('midcom.helper.metadata', $key);
} else {
$content = $dm->get_content_csv();
$value = $content[$key];
}
break;
$dm = $this->get_datamanager();
if (!$dm->get_schema()->has_field($key)) {
// Fall back to the parameter reader for non-core MidCOM metadata params
return $this->__object->get_parameter('midcom.helper.metadata', $key);
}
return $dm->get_content_csv()[$key];
}
$this->_cache[$key] = $value;
return $this->__metadata->$key;
}
/* ------- CONVENIENCE METADATA INTERFACE --------- */

0 comments on commit 440e2c5

Please sign in to comment.