Skip to content

Commit

Permalink
metadata point field saving wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hinanaya committed Jul 16, 2024
1 parent a5c232d commit b54a8cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
8 changes: 8 additions & 0 deletions html/media/addedit_metadata.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,12 @@
<ob-field-playlist data-edit data-single class="metadata_name_field"></ob-field-playlist>
<button class="copy_to_all" data-field="metadata_name_field" onclick="OB.Media.copyField(this);" title="Copy To All"><i class="far fa-copy"></i></button>
</div>
</div>

<div id="media_metadata_template_coordinates" class="hidden">
<div class="fieldrow">
<label></label>
<ob-field-coordinates data-edit class="metadata_name_field"></ob-field-coordinates>
<button class="copy_to_all" data-field="metadata_name_field" onclick="OB.Media.copyField(this);" title="Copy To All"><i class="far fa-copy"></i></button>
</div>
</div>
2 changes: 2 additions & 0 deletions html/media/metadata_addedit.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<ob-option value="tags">Tags</ob-option>
<ob-option value="media">Media</ob-option>
<ob-option value="playlist">Playlist</ob-option>
<ob-option value="coordinates">Coordinates</ob-option>
</ob-field-select>
</div>

Expand Down Expand Up @@ -70,6 +71,7 @@
<ob-tag-input data-suggestions="false" class="hidden metadata_default metadata_default_tags"></ob-tag-input>
<ob-field-media data-edit data-single class="hidden metadata_default metadata_default_media"></ob-field-media>
<ob-field-playlist data-edit data-single class="hidden metadata_default metadata_default_playlist"></ob-field-playlist>
<ob-field-coordinates data-edit class="hidden metadata_default metadata_default_coordinates"></ob-field-coordinates>
</div>

<div class="fieldrow">
Expand Down
35 changes: 34 additions & 1 deletion models/mediametadata_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function validate($data, $id)
}

//T The field type is not valid.
if (array_search($data['type'], ['select','bool','text','textarea','formatted','integer','date','time','datetime','tags','hidden','media','playlist']) === false) {
if (array_search($data['type'], ['select','bool','text','textarea','formatted','integer','date','time','datetime','tags','hidden','media','playlist','coordinates']) === false) {
return [false,'The field type is not valid.'];
}

Expand Down Expand Up @@ -254,6 +254,35 @@ public function save($data, $id)
} elseif ($save['type'] === 'playlist') {
$this->db->query('ALTER TABLE ' . $this->db->format_backticks('media') . ' ADD ' . $this->db->format_backticks('metadata_' . $data['name']) . ' INT UNSIGNED NULL DEFAULT NULL');
$this->db->query('ALTER TABLE ' . $this->db->format_backticks('media') . ' ADD CONSTRAINT ' . $this->db->format_backticks('fk_media_metadata_' . $data['name']) . ' FOREIGN KEY (' . $this->db->format_backticks('metadata_' . $data['name']) . ') REFERENCES ' . $this->db->format_backticks('playlists') . ' (' . $this->db->format_backticks('id') . ') ON UPDATE CASCADE ON DELETE SET NULL');
} elseif ($save['type'] === 'coordinates') {
$this->db->query('ALTER TABLE ' . $this->db->format_backticks('media') . ' ADD ' . $this->db->format_backticks('metadata_' . $data['name']) . ' POINT NULL DEFAULT NULL');
// TODO: Use triggers probably to make thsi actually convert to POINT(x y) format.
// Assumes field is named 'point' in example for testing.
// Need to replace before_insert_metadata_point, NEW.metadata_point, etc with actual field name.
/*$this->db->query("
DELIMITER //
CREATE TRIGGER before_insert_metadata_point
BEFORE INSERT ON media
FOR EACH ROW
BEGIN
DECLARE x DECIMAL(10, 6);
DECLARE y DECIMAL(10, 6);
-- Split the coordinate_string into x and y
SET x = CAST(SUBSTRING_INDEX(NEW.metadata_point, ',', 1) AS DECIMAL(10, 6));
SET y = CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(NEW.metadata_point, ',', -1), ' ', -1) AS DECIMAL(10, 6));
-- Set the coordinate_point column
SET NEW.metadata_point = ST_GeomFromText(CONCAT('POINT(', x, ' ', y, ')'));
END//
DELIMITER ;
");*/
if ($err = $this->db->error()) {
var_dump($err);
die();
}
}

return $id;
Expand Down Expand Up @@ -281,6 +310,10 @@ public function delete($id)
$this->db->query('ALTER TABLE ' . $this->db->format_backticks('media') . ' DROP FOREIGN KEY ' . $this->db->format_backticks('fk_media_metadata_' . $field['name']));
}

if ($field['type'] === 'coordinates') {
$this->db->query('DROP TRIGGER before_insert_metadata_' . $field['name']);
}

$this->db->query('ALTER TABLE ' . $this->db->format_backticks('media') . ' DROP COLUMN ' . $this->db->format_backticks('metadata_' . $field['name']));

return true;
Expand Down

0 comments on commit b54a8cf

Please sign in to comment.