Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit Item Permalink Issue #13 #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion application/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
$route['admin'] = "admin/dashboard";
$route['admin/items/page/:num'] = 'admin/items';
$route['admin/write/page'] = 'admin/write/index';
$route['admin/write/edit/:num'] = 'admin/write/index';
$route['admin/edit/(:num)'] = 'admin/write/edit/$1';
$route['p/:any'] = "p";

/* End of file routes.php */
Expand Down
198 changes: 115 additions & 83 deletions application/controllers/admin/write.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,111 +42,143 @@

class Write extends MY_Auth_Controller {

/**
* Builds the form validation constructors for editing and creating new
* item_models.
*/
function __construct()
{
parent::__construct();

$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
$this->form_validation->set_rules('content', 'Content', 'trim');
$this->form_validation->set_rules('tags', 'Tags', 'trim|xss_clean');
$this->form_validation->set_rules('timestamp', 'Date', 'trim|xss_clean');
$this->form_validation->set_rules('name', 'Slug', 'trim|required|alpha_dash');

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
}

function index()
/**
* Edit an existing item
*
* @param int $item_id Primary key of item_model
*
* @return void
*/
public function edit($item_id)
{
if ($this->uri->segment(3) == 'edit') {
if ($this->input->post('referer')) {
$data->referer = $this->input->post('referer');
} else {
$data->referer = $_SERVER['HTTP_REFERER'];
}
$item = $this->item_model->get_edit_item_by_id($item_id);

$data->editing = TRUE;
$data = new stdClass();
$data->page_name = 'Edit';
$data->referer = $this->getReferer();
$data->editing = TRUE;
$data->tag_string = '';

// Get item
$data->item = $this->item_model->get_edit_item_by_id($this->uri->segment(4));
if (!empty($item->item_tags)) {
$tags = array();

if (isset($data->item->item_tags[0])) {
foreach ($data->item->item_tags as $tag) {
$tags[] = $tag->name;
}
$data->tag_string = implode(', ', $tags);
foreach ($item->item_tags as $tag) {
$tags[] = $tag->name;
}

$new_post->item_data = $data->item->item_data;
$data->tag_string = implode(', ', $tags);
}

if ($this->form_validation->run()) {
$new_post = $this->createItemFromPost();
if ($this->input->post('timestamp') == 'make_current') {
$new_post->item_date = time();
} elseif ($this->input->post('timestamp') == 'make_current_publish') {
$new_post->item_status = 'publish';
$new_post->item_date = time();
}

$this->item_model->update_item($new_post, $item);

header('Location: '.$this->input->post('referer'));
}


$data->item = $item;
$this->load->view('admin/_header', $data);
$this->load->view('admin/edit', $data);
$this->load->view('admin/_footer');
}

/**
* Create a new item_model
*
* @return void
*/
public function index()
{
$data = new stdClass();
$data->page_name = 'Write';

if ($_POST) {
$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
$this->form_validation->set_rules('date', 'Date', 'trim|xss_clean');
$this->form_validation->set_rules('content', 'Content', 'trim');
$this->form_validation->set_rules('tags', 'Tags', 'trim|xss_clean');

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

if ($this->form_validation->run() == FALSE) {
$this->load->view('admin/_header', $data);
$this->load->view('admin/write', $data);
} else {
// Prepare data
if (!isset($data->editing)) {
$new_post->item_data = array();
}

if ($this->input->post('tags', TRUE)) {
$tags = explode(',', $this->input->post('tags', TRUE));

foreach ($tags as $key => $value) {
$tags[$key] = trim($value);
}

if (!empty($tags)) {
$new_post->item_data['tags'] = $tags;
}
} else {
$new_post->item_data['tags'] = array();
}

$new_post->item_title = $this->input->post('title', TRUE);

if (!$this->input->post('content')) {
$new_post->item_content = '';
} else {
$new_post->item_content = $this->input->post('content');
}

if ($this->input->post('save_edit') == 'true') {
// Save edits
if ($this->input->post('timestamp') == 'make_current') {
$new_post->item_date = time();
} elseif ($this->input->post('timestamp') == 'make_current_publish') {
$new_post->item_status = 'publish';
$new_post->item_date = time();
}

$this->item_model->update_item($new_post, $data->item);

header('Location: '.$this->input->post('referer'));
} else {
// Add new item
$new_post->item_name = url_title($this->input->post('title', TRUE));
$new_post->item_date = time();

if ($this->input->post('draft') == 'true') {
$new_post->item_status = 'draft';
}

$this->item_model->add_blog_post($new_post);

header('Location: '.$this->config->item('base_url').'admin/items');
}
if ($this->form_validation->run()) {
// Prepare data
$new_post = $this->createItemFromPost();
$new_post->item_date = time();


if ($this->input->post('draft')) {
$new_post->item_status = 'draft';
}
} else {
$this->load->view('admin/_header', $data);
$this->load->view('admin/write', $data);

$this->item_model->add_blog_post($new_post);
redirect('admin/items', 'location');
}

$this->load->view('admin/_header', $data);
$this->load->view('admin/write', $data);
$this->load->view('admin/_footer');
}

/**
* Retrieve the HTTP_REFERER for the form. This
*
* @return [type] [description]
*/
private function getReferer() {
if ($this->input->post('referer')) {
return $this->input->post('referer');
} else if (isset($_SERVER['HTTP_REFERER'])) {
return $_SERVER['HTTP_REFERER'];
}
}

/**
* Creates a stdClass Object with data from the $_POST input. The title is
* sluggified and the tags are stored as an array.
*
* @return stdClass A new "item" record with values taken from $_POST input
*/
private function createItemFromPost()
{
$new_post = new stdClass();
$new_post->item_title = $this->input->post('title', TRUE);
$new_post->item_content = $this->input->post('content');
$new_post->item_name = url_title($this->input->post('title', TRUE), '-', TRUE);
$new_post->item_data = array(
'tags' => array()
);

if ($this->input->post('tags', TRUE)) {
$tags = explode(',', $this->input->post('tags', TRUE));
$tags = array_filter($tags);
foreach ($tags as $key => $value) {
$tags[$key] = trim($value);
}

$new_post->item_data['tags'] = $tags;
}


return $new_post;
}
}

/* End of file write.php */
Expand Down
6 changes: 3 additions & 3 deletions application/views/admin/_activity_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<li id="item_<?php echo $item->ID?>" class="item <?php echo $item->item_status?>">
<ul class="item_tools">
<li class="expand"><a href="#expand">Expand</a></li>
<li><a href="<?php echo $this->config->item('base_url')?>admin/write/edit/<?php echo $item->ID?>">Edit</a></li>
<li><a href="<?php echo site_url("admin/edit/" . $item->ID); ?>">Edit</a></li>
<li class="unpublish_this"><a href="#unpublish">Unpublish</a></li>
<li class="publish_this"><a href="#publish">Publish</a></li>
<li class="item_delete"><a class="confirm_first" href="<?php echo $this->config->item('base_url')?>admin/items/delete/<?php echo $item->ID?>">x</a></li>
<li class="item_delete"><a class="confirm_first" href="<?php echo site_url("admin/items/delete/" . $item->ID); ?>">x</a></li>
</ul>

<p class="icon" style="background-image: url(<?php echo $item->get_feed_icon()?>)"><?php echo $item->get_feed_domain()?> &#8212; <?php echo $item->get_human_date()?></p>
Expand Down Expand Up @@ -40,7 +40,7 @@
<ul class="tags">
<li class="title">Tags:</li>
<?php foreach ($item->get_tags() as $tag): ?>
<li><a href="<?php echo $this->config->item('base_url')?>admin/items/tag/<?php echo $tag->slug?>"><?php echo $tag->name?></a></li>
<li><a href="<?php echo site_url("admin/items/tag/" . $tag->slug); ?>"><?php echo $tag->name?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Expand Down
77 changes: 77 additions & 0 deletions application/views/admin/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<div id="main_content">
<p id="breadcrumb"><a href="<?php echo $referer?>">Back to Items</a> &rsaquo; Editing <span class="highlight"><?php echo $item->item_title?></span></p>


<?php echo validation_errors();?>

<form action="" method="post" class="generic">
<div class="row">
<label class="title" for="title_input">Title</label>
<input id="title_input" type="text" class="text_input" name="title" value="<?php echo set_value('title', $item->item_title); ?>" />
</div>

<div class="row">
<label for="title_input">Slug</label>
<?php echo site_url('items/view/' . $item->ID); ?>/
<input id="title_input" type="text" name="name" value="<?php echo set_value('name', $item->item_name); ?>" />
</div>

<div class="row">
<label class="title" for="content_input">Content</label>
<div class="wmd-panel">
<div id="wmd-button-bar"></div>
<textarea id="wmd-input" class="wmd-input text_input" name="content"><?php echo set_value('content', $item->item_content); ?></textarea>
</div>
</div>

<div class="row">
<label class="title" for="wmd-preview">Preview</label>
<div id="wmd-preview" class="wmd-panel wmd-preview"></div>
</div>

<div class="row">
<label class="title" for="tag_input">Tags</label>
<input id="tag_input" type="text" class="text_input" name="tags" value="<?php echo set_value('tags', $tag_string); ?>" />
</div>

<span class="input_explain">Separate with commas e.g. these, are, my, tags</span>

<div class="row">
<label class="title" for="feed_url_input">Timestamp / Publish Options</label>
<span class="option_container">
<span class="option"><input type="radio" name="timestamp" value="no_change" id="radio_no_change" <?php echo set_radio('timestamp', 'no_change', TRUE); ?> /> <label for="radio_no_change">No Change</label></span>
<span class="option"><input type="radio" name="timestamp" value="make_current" id="radio_make_current" <?php echo set_radio('timestamp', 'make_current'); ?> /> <label for="radio_make_current">Make Current Time</label></span>
<?php if ($item->item_status == 'draft'): ?>
<span class="option"><input type="radio" name="timestamp" value="make_current_publish" id="radio_make_current_publish" <?php echo set_radio('timestamp', 'make_current_publish'); ?> /> <label for="radio_make_current_publish">Make Current Time and Publish Now</label></span>
<?php endif; ?>
</span>
</div>

<div class="buttons">
<input type="hidden" value="false" name="draft" />
<div class="clear"></div>

<input type="hidden" name="referer" value="<?php echo $referer?>" />
<input type="hidden" name="save_edit" value="true" />

<button type="submit" class="positive"><img src="<?php echo base_url("public/images/system/icons/silk/accept.png"); ?>" alt="" />Save Changes</button>
</div>

</form>

</div>

<div id="side_content">
<p class="tip"><strong>Shorthand</strong><br />The blog post content area supports the <a href="http://daringfireball.net/projects/markdown/syntax" rel="external">Markdown</a> method of shorthand markup.</p>
</div>

<script type="text/javascript" src="<?php echo base_url("public/scripts/pagedown/Markdown.Converter.js"); ?>"></script>
<script type="text/javascript" src="<?php echo base_url("public/scripts/pagedown/Markdown.Sanitizer.js"); ?>"></script>
<script type="text/javascript" src="<?php echo base_url("public/scripts/pagedown/Markdown.Editor.js"); ?>"></script>
<script type="text/javascript">
(function () {
var converter1 = Markdown.getSanitizingConverter(),
editor1 = new Markdown.Editor(converter1);
editor1.run();
})();
</script>
Loading