예제 #1
0
 function edit()
 {
     // Init
     $data = array();
     $data['edit_mode'] = FALSE;
     $this->template->add_package(array('codemirror'));
     $data['breadcrumb'] = set_crumbs(array('content/snippets' => 'Snippets', current_url() => 'Snippet Edit'));
     $data['revision_id'] = $revision_id = $this->uri->segment(6);
     $this->load->model('snippets_model');
     $data['Snippet'] = $Snippet = new Snippets_model();
     $snippet_id = $this->uri->segment(5);
     // Edit mode
     if ($snippet_id) {
         $data['edit_mode'] = TRUE;
         $Snippet->get_by_id($snippet_id);
         // Check if snippet exists
         if (!$Snippet->exists()) {
             return show_404();
         }
         // Load a revision if a revision id was provided in the URL
         if (!empty($revision_id)) {
             $this->load->model('revisions_model');
             $Revision = new Revisions_model();
             $Revision->get_by_id($revision_id);
             if ($Revision->exists()) {
                 $revision_data = @unserialize($Revision->revision_data);
                 $Snippet->from_array($revision_data);
             } else {
                 return show_404();
             }
         }
     }
     $this->form_validation->set_rules('title', 'Title', 'trim|required');
     $this->form_validation->set_rules('short_name', 'Short Name', 'trim|required|alpha_dash|max_length[50]|is_unique[snippets.short_name.id.' . $snippet_id . ']');
     $this->form_validation->set_rules('snippet', 'Snippet', '');
     // Form validation
     if ($this->form_validation->run() == TRUE) {
         $Snippet->from_array($this->input->post());
         if ($data['edit_mode']) {
             $Snippet->id = $snippet_id;
         }
         $Snippet->save();
         $Snippet->add_revision();
         // Clear cache
         $this->load->library('cache');
         $this->cache->delete_all('snippets');
         $this->session->set_flashdata('message', '<p class="success">Snippet Saved.</p>');
         if ($this->input->post('save_exit')) {
             redirect(ADMIN_PATH . '/content/snippets/');
         } else {
             redirect(ADMIN_PATH . '/content/snippets/edit/' . $Snippet->id);
         }
     }
     $this->template->view('admin/snippets/edit', $data);
 }