Skip to content

Commit

Permalink
Revert "Revert "Create new post only when actually saving""
Browse files Browse the repository at this point in the history
This reverts commit c06a43c.
  • Loading branch information
bobbyshaw committed Apr 18, 2015
1 parent 0bee191 commit 532b5e5
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 63 deletions.
13 changes: 9 additions & 4 deletions code/Controller/PostEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ public function get($postId)
public function post($postId)
{
$imageUrl = isset($_POST['image_url']) ? $_POST['image_url'] : null;
$subject = isset($_POST['subject']) ? $_POST['subject'] : null;
$body = isset($_POST['body']) ? $_POST['body'] : null;
$tagIds = isset($_POST['tag_ids']) ? $_POST['tag_ids'] : null;

$subject = isset($_POST['subject']) ? $_POST['subject'] : null;
$body = isset($_POST['body']) ? $_POST['body'] : null;
$tagIds = isset($_POST['tag_ids']) ? $_POST['tag_ids'] : null;
$isActive = isset($_POST['is_active']) ? $_POST['is_active'] : null;
$isNews = isset($_POST['is_news']) ? $_POST['is_news'] : null;
$isNews = isset($_POST['is_news']) ? $_POST['is_news'] : null;

if ($imageUrl) {
if (strpos($imageUrl, "javascript:") !== false || strpos($imageUrl, "data:") !== false) {
Expand All @@ -64,6 +65,10 @@ public function post($postId)
->set('image_url', $imageUrl)
->save();

if (!$isActive) {
$this->_redirect($post->getEditUrl());
}

$this->_redirect($post->getUrl());
}

Expand Down
52 changes: 43 additions & 9 deletions code/Controller/PostNew.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,50 @@ public function get()
{
$this->_preDispatch();

$data = array(
"subject" => "New Post",
"is_active" => false,
"is_news" => false,
"user_id" => $this->_getCurrentUser()->getId(),
);
$tags = $this->_getContainer()->Tag()->fetchAll();

$post = $this->_getContainer()->Post()->setData($data)->save();
$postId = $post->getId();
echo $this->_getTwig()->render('post_new.html.twig', array(
'session' => $this->_getSession(),
'local_config' => $this->_getContainer()->LocalConfig(),
'tags' => $tags,
));
}

public function post()
{
$imageUrl = isset($_POST['image_url']) ? $_POST['image_url'] : null;
$subject = isset($_POST['subject']) ? $_POST['subject'] : null;
$body = isset($_POST['body']) ? $_POST['body'] : null;
$tagIds = isset($_POST['tag_ids']) ? $_POST['tag_ids'] : null;
$isActive = isset($_POST['is_active']) ? (int) $_POST['is_active'] : null;
$isNews = isset($_POST['is_news']) ? $_POST['is_news'] : null;

if ($imageUrl) {
if (strpos($imageUrl, "javascript:") !== false || strpos($imageUrl, "data:") !== false) {
die("Looks like an injection attempt");
}
}

if (! $tagIds || empty($tagIds)) {
die("You have to pick at least one tag");
}

$post = $this->_getContainer()->Post()
->set('subject', $subject)
->set('body', $body)
->set('tag_ids', $tagIds)
->set('name', isset($profileData['name']) ? $profileData['name'] : null)
->set('is_active', $isActive)
->set('image_url', $imageUrl)
->set('is_news', (int)$isNews)
->set('user_id', $this->_getCurrentUser()->getId())
->save();

if (!$isActive) {
header("Location: " . $post->getEditUrl());
$this->_redirect($post->getEditUrl());
}

$this->_redirect("posts/$postId/edit");
$this->_redirect($post->getUrl());
}
}
6 changes: 6 additions & 0 deletions code/Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ public function getUrl()
return $url;
}

public function getEditUrl()
{
$url = implode("/", array($this->_localConfig->get('base_url'), "posts", $this->getId(), "edit"));
return $url;
}

public function getTweetUrl()
{
$text = $this->getSubject() . " " . $this->getUrl();
Expand Down
47 changes: 47 additions & 0 deletions template/fragment/post_form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<label for="is_active" >Published?</label>
<div class="input-wrapper">
<select id="is_active" name="is_active">
<option {{ post.getIsActive() ? "" : "selected" }} value=0>Draft</option>
<option {{ post.getIsActive() ? "selected " : "" }}value=1>Published</option>
</select>
</div>

<div class="input-wrapper">
<label for="is_news" >Post Type</label>
<select id="is_news" name="is_news">
<option {{ post.isNews() ? "" : "selected" }} value=0>What I'm working on</option>
<option {{ post.isNews() ? "selected " : "" }}value=1>News</option>
</select>
<div class="help">
<a href="http://magehero.com/posts/749/new-magehero-feature-news" target="_blank">What's the difference between <i>What I'm working on</i> and <i>News</i>?</a>
</div>
</div>

<div class="input-wrapper">
<label for="subject" >Subject</label>
<input id="subject" name="subject" class="input-field subject" value="{{ post.getSubject() }}" />
</div>

<div class="input-wrapper">
<label for="tag_ids" >Tags</label>
<select id="tag_ids" multiple name="tag_ids[]" class="fancy-select">
{% for tag in tags %}
<option {{ post.hasTagId(tag.getId()) ? "selected" : "" }} value="{{ tag.getId() }}">{{ tag.getTagText() }}</option>
{% endfor %}
</select>
</div>

<div class="input-wrapper">
<label for="image_url" >Image URL (Optional)</label>
<input id="image_url" name="image_url" class="input-field" value="{{ post.getImageUrl() }}" />
</div>

<div class="input-wrapper">
<label for="body" >Details (Optional)</label>
<textarea id="body" name="body" class="textarea body" style="display:none">{{ post.getBody() }}</textarea>
<div id="epiceditor"></div>
</div>

<div>
<input type="submit" value="Save">
</div>
54 changes: 4 additions & 50 deletions template/post_edit.html.twig
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
{% extends 'base.html.twig' %}

{% block content %}
<form action="/posts/{{ post.getId() }}/edit" method="POST" class="post-edit">
<label for="is_active" >Published?</label>
<div class="input-wrapper">
<select id="is_active" name="is_active">
<option {{ post.getIsActive() ? "" : "selected" }} value=0>Draft</option>
<option {{ post.getIsActive() ? "selected " : "" }}value=1>Published</option>
</select>
</div>

<div class="input-wrapper">
<label for="is_news" >Post Type</label>
<select id="is_news" name="is_news">
<option {{ post.isNews() ? "" : "selected" }} value=0>What I'm working on</option>
<option {{ post.isNews() ? "selected " : "" }}value=1>News</option>
</select>
<div class="help">
<a href="http://magehero.com/posts/749/new-magehero-feature-news" target="_blank">What's the difference between <i>What I'm working on</i> and <i>News</i>?</a>
</div>
</div>

<div class="input-wrapper">
<label for="subject" >Subject</label>
<input id="subject" name="subject" class="input-field subject" value="{{ post.getSubject() }}" />
</div>

<div class="input-wrapper">
<label for="tag_ids" >Tags</label>
<select id="tag_ids" multiple name="tag_ids[]" class="fancy-select">
{% for tag in tags %}
<option {{ post.hasTagId(tag.getId()) ? "selected" : "" }} value="{{ tag.getId() }}">{{ tag.getTagText() }}</option>
{% endfor %}
</select>
</div>

<div class="input-wrapper">
<label for="image_url" >Image URL (Optional)</label>
<input id="image_url" name="image_url" class="input-field" value="{{ post.getImageUrl() }}" />
</div>

<div class="input-wrapper">
<label for="body" >Details (Optional)</label>
<textarea id="body" name="body" class="textarea body" style="display:none">{{ post.getBody() }}</textarea>
<div id="epiceditor"></div>
</div>

<div>
<input type="submit" value="Save">
</div>
</form>
{% endblock %}
<form action="{{ post.getEditUrl() }}" method="POST" class="post-edit">
{% include 'fragment/post_form.html.twig' %}
</form>
{% endblock %}
7 changes: 7 additions & 0 deletions template/post_new.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'base.html.twig' %}

{% block content %}
<form action="/posts/new" method="POST" class="post-edit">
{% include 'fragment/post_form.html.twig' %}
</form>
{% endblock %}

0 comments on commit 532b5e5

Please sign in to comment.