Commit 9491e6f5 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Move blogpost attachment database stuff into blogpost class

parent cc621740
......@@ -479,6 +479,24 @@ class ArtefactTypeBlogPost extends ArtefactType {
return $smarty->fetch('artefact:blog:render/blogpost_renderfull.tpl');
}
public function attachment_id_list() {
if (!$list = get_column('artefact_blog_blogpost_file', 'file', 'blogpost', $this->get('id'))) {
$list = array();
}
return $list;
}
public function attach_file($artefactid) {
$data = new StdClass;
$data->blogpost = $this->get('id');
$data->file = $artefactid;
insert_record('artefact_blog_blogpost_file', $data);
}
public function detach_file($artefactid) {
delete_records('artefact_blog_blogpost_file', 'blogpost', $this->get('id'), 'file', $artefactid);
}
protected function count_attachments() {
return count_records('artefact_blog_blogpost_file', 'blogpost', $this->get('id'));
......
......@@ -59,6 +59,8 @@ if (!empty($uploads)) {
}
// Get blogpost instance
$postobj = new ArtefactTypeBlogPost($blogpost, null);
$postobj->set('title', $title);
$postobj->set('description', $body);
......@@ -75,16 +77,17 @@ $blogpost = $postobj->get('id');
// Delete old attachments in the db that no longer appear in the list
// of artefacts
if (!$old = get_column('artefact_blog_blogpost_file', 'file', 'blogpost', $blogpost)) {
$old = array();
}
$old = $postobj->attachment_id_list();
foreach ($old as $o) {
if (!in_array($o, $artefacts)) {
delete_records('artefact_blog_blogpost_file', 'blogpost', $blogpost, 'file', $o);
$postobj->detach_file($o);
}
}
......@@ -94,10 +97,7 @@ foreach ($old as $o) {
foreach ($artefacts as $a) {
if (!in_array($a, $old)) {
$data = new StdClass;
$data->blogpost = $blogpost;
$data->file = $a;
insert_record('artefact_blog_blogpost_file', $data);
$postobj->attach_file($a);
}
}
......
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