From 78a48c3b285e76482d5cc86dd2a6ac1c92167294 Mon Sep 17 00:00:00 2001 From: Logan Bailey Date: Mon, 25 Jun 2012 21:01:34 -0700 Subject: [PATCH 1/3] Updates admin item creation and modification Breaks the admin write and edit code into two separate templates and functions. --- application/config/routes.php | 2 +- application/controllers/admin/write.php | 197 ++++++++++++++---------- application/views/admin/edit.php | 71 +++++++++ application/views/admin/write.php | 43 ++---- 4 files changed, 195 insertions(+), 118 deletions(-) create mode 100644 application/views/admin/edit.php diff --git a/application/config/routes.php b/application/config/routes.php index 84af9f4..7f97ce8 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -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 */ diff --git a/application/controllers/admin/write.php b/application/controllers/admin/write.php index 982d2dc..74aceb0 100644 --- a/application/controllers/admin/write.php +++ b/application/controllers/admin/write.php @@ -42,111 +42,140 @@ 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_error_delimiters('
', '
'); } - function index() - { - if ($this->uri->segment(3) == 'edit') { - if ($this->input->post('referer')) { - $data->referer = $this->input->post('referer'); - } else { - $data->referer = $_SERVER['HTTP_REFERER']; + /** + * Edit an existing item + * + * @param int $item_id Primary key of item_model + * + * @return void + */ + public function edit($item_id) { + $item = $this->item_model->get_edit_item_by_id($item_id); + + $data = new stdClass(); + $data->page_name = 'Edit'; + $data->referer = $this->getReferer(); + $data->editing = TRUE; + $data->tag_string = ''; + + if (!empty($item->item_tags)) { + $tags = array(); + + foreach ($item->item_tags as $tag) { + $tags[] = $tag->name; } - $data->editing = TRUE; - - // Get item - $data->item = $this->item_model->get_edit_item_by_id($this->uri->segment(4)); + $data->tag_string = implode(', ', $tags); + } - if (isset($data->item->item_tags[0])) { - foreach ($data->item->item_tags as $tag) { - $tags[] = $tag->name; - } - $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(); } - $new_post->item_data = $data->item->item_data; + $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('
', '
'); - - 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 */ diff --git a/application/views/admin/edit.php b/application/views/admin/edit.php new file mode 100644 index 0000000..21c84e5 --- /dev/null +++ b/application/views/admin/edit.php @@ -0,0 +1,71 @@ +
+ + + + + +
+
+ + +
+ +
+ +
+
+ +
+
+ +
+ +
+
+ +
+ + +
+ + Separate with commas e.g. these, are, my, tags + +
+ + + /> + /> + item_status == 'draft'): ?> + /> + + +
+ +
+ +
+ + + + + +
+ +
+ +
+ +
+

Shorthand
The blog post content area supports the Markdown method of shorthand markup.

+
+ + + + + \ No newline at end of file diff --git a/application/views/admin/write.php b/application/views/admin/write.php index f8c90b9..d4db31b 100644 --- a/application/views/admin/write.php +++ b/application/views/admin/write.php @@ -1,21 +1,18 @@
- - -
- +
- +
@@ -26,39 +23,19 @@
- +
Separate with commas e.g. these, are, my, tags - item_feed_id)): ?> -
- - - input->post('timestamp') == 'no_change'): ?> checked="checked" type="radio" name="timestamp" value="no_change" id="radio_no_change" /> - input->post('timestamp') == 'make_current'): ?> checked="checked" type="radio" name="timestamp" value="make_current" id="radio_make_current" /> - item_status == 'draft'): ?> - input->post('timestamp') == 'make_current_publish'): ?> checked="checked" type="radio" name="timestamp" value="make_current_publish" id="radio_make_current_publish" /> - - -
- -
- - - - - - - + - Save as Draft - + Save as Draft
@@ -69,13 +46,13 @@

Shorthand
The blog post content area supports the Markdown method of shorthand markup.

- - - + + + From 6b57e45091a6ff675205339f6eb44d9dc8198e49 Mon Sep 17 00:00:00 2001 From: Logan Bailey Date: Mon, 25 Jun 2012 21:36:59 -0700 Subject: [PATCH 2/3] CI 2.0 url constructors for items pages --- application/views/admin/_activity_list.php | 6 +++--- application/views/admin/edit.php | 8 ++++---- application/views/admin/write.php | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/application/views/admin/_activity_list.php b/application/views/admin/_activity_list.php index 6375d21..6946521 100644 --- a/application/views/admin/_activity_list.php +++ b/application/views/admin/_activity_list.php @@ -3,10 +3,10 @@
  • get_feed_domain()?> — get_human_date()?>

    @@ -40,7 +40,7 @@ diff --git a/application/views/admin/edit.php b/application/views/admin/edit.php index 21c84e5..08fc266 100644 --- a/application/views/admin/edit.php +++ b/application/views/admin/edit.php @@ -48,7 +48,7 @@ - + @@ -59,9 +59,9 @@

    Shorthand
    The blog post content area supports the Markdown method of shorthand markup.

    - - - + + + - - + + +