function delete_submit(Pieform $form, $values) { $blogpost = new ArtefactTypeBlogPost((int) $values['delete']); $blogpost->check_permission(); if ($blogpost->get('locked')) { $form->reply(PIEFORM_ERR, get_string('submittedforassessment', 'view')); } $blogpost->delete(); $form->reply(PIEFORM_OK, array('message' => get_string('blogpostdeleted', 'artefact.blog'), 'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blogpost->get('parent'), 'id' => $values['delete'])); }
function editpost_submit(Pieform $form, $values) { global $USER, $SESSION, $blogpost, $blog; require_once 'embeddedimage.php'; db_begin(); $postobj = new ArtefactTypeBlogPost($blogpost, null); $postobj->set('title', $values['title']); $postobj->set('description', $values['description']); $postobj->set('tags', $values['tags']); if (get_config('licensemetadata')) { $postobj->set('license', $values['license']); $postobj->set('licensor', $values['licensor']); $postobj->set('licensorurl', $values['licensorurl']); } $postobj->set('published', !$values['draft']); $postobj->set('allowcomments', (int) $values['allowcomments']); if (!$blogpost) { $postobj->set('parent', $blog); $blogobj = new ArtefactTypeBlog($blog); if ($blogobj->get('institution')) { $postobj->set('institution', $blogobj->get('institution')); } else { if ($blogobj->get('group')) { $postobj->set('group', $blogobj->get('group')); } else { $postobj->set('owner', $USER->id); } } } $postobj->commit(); $blogpost = $postobj->get('id'); // Need to wait until post is saved in case we are a new blogpost before we can sort out embedded images as we need an id $postobj->set('description', EmbeddedImage::prepare_embedded_images($values['description'], 'blogpost', $postobj->get('id'))); // Attachments $old = $postobj->attachment_id_list(); // $new = is_array($values['filebrowser']['selected']) ? $values['filebrowser']['selected'] : array(); $new = is_array($values['filebrowser']) ? $values['filebrowser'] : array(); // only allow the attaching of files that exist and are editable by user foreach ($new as $key => $fileid) { $file = artefact_instance_from_id($fileid); if (!$file instanceof ArtefactTypeFile || !$USER->can_publish_artefact($file)) { unset($new[$key]); } } if (!empty($new) || !empty($old)) { foreach ($old as $o) { if (!in_array($o, $new)) { try { $postobj->detach($o); } catch (ArtefactNotFoundException $e) { } } } foreach ($new as $n) { if (!in_array($n, $old)) { try { $postobj->attach($n); } catch (ArtefactNotFoundException $e) { } } } } db_commit(); $result = array('error' => false, 'message' => get_string('blogpostsaved', 'artefact.blog'), 'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog); if ($form->submitted_by_js()) { // Redirect back to the blog page from within the iframe $SESSION->add_ok_msg($result['message']); $form->json_reply(PIEFORM_OK, $result, false); } $form->reply(PIEFORM_OK, $result); }
if (!($title && $description)) { jsonreply(array('fail' => 'Journal posts must have a title and entry (description).')); } if (!get_record('artefact', 'id', $blog, 'owner', $USER->get('id'))) { // Blog security is also checked closer to when blogs are added, this // check ensures that malicious users do not even see the screen for // adding a post to a blog that is not theirs throw new AccessDeniedException(get_string('youarenottheownerofthisblog', 'artefact.blog')); } // Should we create a new post or attach the new file to an existing post $postids = get_records_sql_array("\n SELECT a.id\n FROM {artefact} a\n WHERE a.title = ? AND a.description = ?\n\tAND a.owner = ? ", array($title, $description, $USER->get('id'))); if ($postids) { $blogpost = $postids[0]->id; $postobj = new ArtefactTypeBlogPost($blogpost); $postobj->check_permission(); if ($postobj->get('locked')) { throw new AccessDeniedException(get_string('submittedforassessment', 'view')); } } else { $postobj = new ArtefactTypeBlogPost($blogpost, null); $postobj->set('title', $title); $postobj->set('description', $description); $postobj->set('tags', $tags); $postobj->set('published', !$draft); $postobj->set('allowcomments', (int) $allowcomments); $postobj->set('parent', $blog); $postobj->set('owner', $USER->id); $postobj->commit(); } } // Check to see if we're creating a journal entry
/** * This function updates an existing blog post. * * @param User * @param array */ public static function edit_post(User $user, array $values) { $artefact = new ArtefactTypeBlogPost($values['id']); if ($user->get('id') != $artefact->get('owner')) { return false; } $artefact->set('title', $values['title']); $artefact->set('description', $values['description']); $artefact->set('published', $values['published']); $artefact->set('tags', $values['tags']); if (get_config('licensemetadata')) { $artefact->set('license', $values['license']); $artefact->set('licensor', $values['licensor']); $artefact->set('licensorurl', $values['licensorurl']); } $artefact->commit(); return true; }
/** * Creates a blogpost from the given entry * * @param SimpleXMLElement $entry The entry to create the blogpost from * @param PluginImportLeap $importer The importer * @param int $blogid The blog in which to put the post * @return array A list of artefact IDs created, to be used with the artefact mapping. * There will either be one (the blogpost ID), or two. If there is two, the * second one will be the ID of the file created to hold the out-of-line * content associated with the blogpost */ private static function create_blogpost(SimpleXMLElement $entry, PluginImportLeap $importer, $blogid) { $createdartefacts = array(); $blogpost = new ArtefactTypeBlogPost(); $blogpost->set('title', (string) $entry->title); // If the entry has out of line content, we import that separately as a // file and set the content to refer to it if (isset($entry->content['src']) && isset($entry->content['type'])) { $file = LeapImportFile::create_file($entry, $importer); $createdartefacts[] = $file->get('id'); $content = '<a href="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $file->get('id') . '"' . ' title="' . hsc($file->get('title')) . '">'; if (is_image_mime_type($file->get('filetype'))) { $content .= '<img src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $file->get('id') . '&maxwidth=500&maxheight=500"' . ' alt="' . hsc($file->get('title')) . '">'; } $content .= '</a>'; $blogpost->set('description', $content); } else { $description = PluginImportLeap::get_entry_content($entry, $importer); $type = isset($entry->content['type']) ? (string) $entry->content['type'] : 'text'; if ($type == 'text') { $description = format_whitespace($description); } $blogpost->set('description', $description); } if ($published = strtotime((string) $entry->published)) { $blogpost->set('ctime', $published); } if ($updated = strtotime((string) $entry->updated)) { $blogpost->set('mtime', $updated); } $draftpost = count($entry->xpath('a:category[(' . $importer->curie_xpath('@scheme', PluginImportLeap::NS_CATEGORIES, 'readiness#') . ') and @term="Unready"]')) == 1; $blogpost->set('published', $draftpost ? 0 : 1); $blogpost->set('owner', $importer->get('usr')); $blogpost->set('parent', $blogid); $blogpost->set('tags', PluginImportLeap::get_entry_tags($entry)); $blogpost->commit(); array_unshift($createdartefacts, $blogpost->get('id')); return $createdartefacts; }
function editpost_submit(Pieform $form, $values) { global $USER, $SESSION, $blogpost, $blog; db_begin(); $postobj = new ArtefactTypeBlogPost($blogpost, null); $postobj->set('title', $values['title']); $postobj->set('description', $values['description']); $postobj->set('tags', $values['tags']); $postobj->set('published', !$values['draft']); if (!$blogpost) { $postobj->set('parent', $blog); $postobj->set('owner', $USER->id); } $postobj->commit(); $blogpost = $postobj->get('id'); // Attachments $old = $postobj->attachment_id_list(); // $new = is_array($values['filebrowser']['selected']) ? $values['filebrowser']['selected'] : array(); $new = is_array($values['filebrowser']) ? $values['filebrowser'] : array(); if (!empty($new) || !empty($old)) { foreach ($old as $o) { if (!in_array($o, $new)) { $postobj->detach($o); } } foreach ($new as $n) { if (!in_array($n, $old)) { $postobj->attach($n); } } } db_commit(); $result = array('error' => false, 'message' => get_string('blogpostsaved', 'artefact.blog'), 'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog); if ($form->submitted_by_js()) { // Redirect back to the blog page from within the iframe $SESSION->add_ok_msg($result['message']); $form->json_reply(PIEFORM_OK, $result, false); } $form->reply(PIEFORM_OK, $result); }
/** * Creates a blogpost from the given entry * * @param SimpleXMLElement $entry The entry to create the blogpost from * @param PluginImportLeap $importer The importer * @param int $blogid The blog in which to put the post * @return array A list of artefact IDs created, to be used with the artefact mapping. * There will either be one (the blogpost ID), or two. If there is two, the * second one will be the ID of the file created to hold the out-of-line * content associated with the blogpost */ private static function create_blogpost(SimpleXMLElement $entry, PluginImportLeap $importer, $blogid) { $config = self::get_blogpost_entry_data($entry, $importer, $blogid); $createdartefacts = array(); $blogpost = new ArtefactTypeBlogPost(); $blogpost->set('title', $config['content']['title']); // If the entry has out of line content, we import that separately as a // file and set the content to refer to it if ($config['isfile']) { $file = LeapImportFile::create_file($entry, $importer); $createdartefacts[] = $file->get('id'); $content = '<a href="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $file->get('id') . '"' . ' title="' . hsc($file->get('title')) . '">'; if (is_image_mime_type($file->get('filetype'))) { $content .= '<img src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $file->get('id') . '&maxwidth=500&maxheight=500"' . ' alt="' . hsc($file->get('title')) . '">'; } $content .= '</a>'; $blogpost->set('description', $content); } else { $blogpost->set('description', $config['content']['description']); } if ($config['content']['ctime']) { $blogpost->set('ctime', $config['content']['ctime']); } if ($config['content']['mtime']) { $blogpost->set('mtime', $config['content']['mtime']); } $draftpost = PluginImportLeap::is_correct_category_scheme($entry, $importer, 'readiness', 'Unready'); $blogpost->set('published', $config['content']['published']); $blogpost->set('owner', $config['owner']); $blogpost->set('parent', $blogid); $blogpost->set('tags', $config['content']['tags']); $blogpost->commit(); array_unshift($createdartefacts, $blogpost->get('id')); return $createdartefacts; }