/** * Pull together news variables for the save form * * @param ElggNews $post * @param ElggAnnotation $revision * @return array */ function news_prepare_form_vars($post = null, $revision = null) { // input names => defaults $values = array('title' => null, 'description' => null, 'status' => 'published', 'access_id' => ACCESS_DEFAULT, 'comments_on' => 'On', 'excerpt' => null, 'tags' => null, 'container_guid' => null, 'guid' => null, 'draft_warning' => ''); if ($post) { foreach (array_keys($values) as $field) { if (isset($post->{$field})) { $values[$field] = $post->{$field}; } } if ($post->status == 'draft') { $values['access_id'] = $post->future_access; } } if (elgg_is_sticky_form('news')) { $sticky_values = elgg_get_sticky_values('news'); foreach ($sticky_values as $key => $value) { $values[$key] = $value; } } elgg_clear_sticky_form('news'); if (!$post) { return $values; } // load the revision annotation if requested if ($revision instanceof ElggAnnotation && $revision->entity_guid == $post->getGUID()) { $values['revision'] = $revision; $values['description'] = $revision->value; } // display a notice if there's an autosaved annotation // and we're not editing it. $auto_save_annotations = $post->getAnnotations(array('annotation_name' => 'news_auto_save', 'limit' => 1)); if ($auto_save_annotations) { $auto_save = $auto_save_annotations[0]; } else { $auto_save = false; } /* @var ElggAnnotation|false $auto_save */ if ($auto_save && $revision && $auto_save->id != $revision->id) { $values['draft_warning'] = elgg_echo('news:messages:warning:draft'); } return $values; }
// because get_input() doesn't use the default if the input is '' if (empty($excerpt)) { $excerpt = $description; } // store errors to pass along $error = false; if ($title && $description) { if ($guid) { $entity = get_entity($guid); if (elgg_instanceof($entity, 'object', 'news') && $entity->canEdit()) { $news = $entity; } else { $error = elgg_echo('news:error:post_not_found'); } } else { $news = new ElggNews(); $news->subtype = 'news'; // force draft and private for autosaves. $news->status = 'unsaved_draft'; $news->access_id = ACCESS_PRIVATE; $news->title = $title; $news->description = $description; $news->excerpt = elgg_get_excerpt($excerpt); // mark this as a brand new post so we can work out the // river / revision logic in the real save action. $news->new_post = true; if (!$news->save()) { $error = elgg_echo('news:error:cannot_save'); } } // create draft annotation
$user = elgg_get_logged_in_user_entity(); // edit or create a new entity $guid = get_input('guid'); if ($guid) { $entity = get_entity($guid); if (elgg_instanceof($entity, 'object', 'news') && $entity->canEdit()) { $news = $entity; } else { register_error(elgg_echo('news:error:post_not_found')); forward(get_input('forward', REFERER)); } // save some data for revisions once we save the new edit $revision_text = $news->description; $new_post = $news->new_post; } else { $news = new ElggNews(); $news->subtype = 'news'; $new_post = true; } // set the previous status for the hooks to update the time_created and river entries $old_status = $news->status; // set defaults and required values. $values = array('title' => '', 'description' => '', 'status' => 'draft', 'access_id' => ACCESS_DEFAULT, 'comments_on' => 'On', 'excerpt' => '', 'tags' => '', 'container_guid' => (int) get_input('container_guid')); // fail if a required entity isn't set $required = array('title', 'description'); // load from POST and do sanity and access checking foreach ($values as $name => $default) { if ($name === 'title') { $value = htmlspecialchars(get_input('title', $default, false), ENT_QUOTES, 'UTF-8'); } else { $value = get_input($name, $default);
$title = get_input('title'); $description = get_input('description'); $tags = string_to_tag_array(get_input('tags')); $container_guid = (int) get_input('container_guid'); $access_id = (int) get_input('access_id'); $top_photo = get_input("top_photo"); $featured_photo = get_input("featured_photo"); elgg_make_sticky_form('news'); if ($guid) { $news = get_entity($guid); if (!$news || !$news instanceof ElggNews || !$news->canEdit()) { register_error(elgg_echo('InvalidParameterException:NoEntityFound')); forward(REFERER); } } else { $news = new ElggNews(); } if (!$title) { register_error(elgg_echo('news:title:missing')); forward(REFERER); } $news->title = $title; $news->description = $description; $news->container_guid = $container_guid; $news->access_id = $access_id; $news->tags = $tags; $news->save(); if ($guid && $top_photo == "remove") { $fh = new ElggFile(); $fh->owner_guid = $news->guid; $fh->setFilename("top_photo.jpg");