HFP-2378 Add option to overwrite unset metadata fields
parent
e15f6d6678
commit
715fa6f803
|
@ -64,15 +64,16 @@ abstract class H5PMetadata {
|
||||||
',"authorComments":' . (isset($content->author_comments) ? json_encode($content->author_comments) : 'null') . '}';
|
',"authorComments":' . (isset($content->author_comments) ? json_encode($content->author_comments) : 'null') . '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make the metadata into an associative array keyed by the property names
|
* Make the metadata into an associative array keyed by the property names
|
||||||
* @param mixed $metadata Array or object containing metadata
|
* @param mixed $metadata Array or object containing metadata
|
||||||
* @param bool $include_title
|
* @param bool $include_title
|
||||||
|
* @param bool $include_missing For metadata fields not being set, skip 'em.
|
||||||
|
* Relevant for content upgrade
|
||||||
* @param array $types
|
* @param array $types
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function toDBArray($metadata, $include_title = true, &$types = array()) {
|
public static function toDBArray($metadata, $include_title = true, $include_missing = true, &$types = array()) {
|
||||||
$fields = array();
|
$fields = array();
|
||||||
|
|
||||||
if (!is_array($metadata)) {
|
if (!is_array($metadata)) {
|
||||||
|
@ -81,12 +82,21 @@ abstract class H5PMetadata {
|
||||||
|
|
||||||
foreach (self::$fields as $key => $config) {
|
foreach (self::$fields as $key => $config) {
|
||||||
|
|
||||||
|
// Ignore title?
|
||||||
if ($key === 'title' && !$include_title) {
|
if ($key === 'title' && !$include_title) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array_key_exists($key, $metadata)) {
|
$exists = array_key_exists($key, $metadata);
|
||||||
$value = $metadata[$key];
|
|
||||||
|
// Don't include missing fields
|
||||||
|
if (!$include_missing && !exists) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $exists ? $metadata[$key] : null;
|
||||||
|
|
||||||
|
// lowerCamelCase to snake_case
|
||||||
$db_field_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key));
|
$db_field_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key));
|
||||||
|
|
||||||
switch ($config['type']) {
|
switch ($config['type']) {
|
||||||
|
@ -98,7 +108,7 @@ abstract class H5PMetadata {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'int':
|
case 'int':
|
||||||
$value = ($value !== null) ? intval($value): null;
|
$value = ($value !== null) ? intval($value) : null;
|
||||||
$types[] = '%d';
|
$types[] = '%d';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -110,7 +120,6 @@ abstract class H5PMetadata {
|
||||||
|
|
||||||
$fields[$db_field_name] = $value;
|
$fields[$db_field_name] = $value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue