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); }
function edit() { // Init $data = array(); $data['edit_mode'] = $edit_mode = FALSE; $data['breadcrumb'] = set_crumbs(array('content/entries' => 'Entries', current_url() => 'Entry Edit')); $data['content_type_id'] = $content_type_id = $this->uri->segment(5); $data['entry_id'] = $entry_id = $this->uri->segment(6); $data['revision_id'] = $revision_id = $this->uri->segment(7); // Get entry $this->load->model('users/users_model'); $this->load->model('entries_model'); $this->load->model('content_types_model'); // Used for content types dropdown $Content_types_model = new Content_types_model(); // If user not a super admin check if user's group is allowed access if ($this->Group_session->type != SUPER_ADMIN) { $this->content_types_model->group_start()->where('restrict_admin_access', 0)->or_where_related('admin_groups', 'group_id', $this->Group_session->id)->group_end(); $this->entries_model->group_start()->where('restrict_admin_access', 0)->or_where_related('content_types/admin_groups', 'group_id', $this->Group_session->id)->group_end(); // Only get Content Types user has access to for dropdown $Content_types_model->group_start()->where('restrict_admin_access', 0)->or_where_related('admin_groups', 'group_id', $this->Group_session->id)->group_end(); } $data['Entry'] = $Entry = $this->entries_model->get_by_id($entry_id); $data['Content_type'] = $Content_type = $this->content_types_model->get_by_id($content_type_id); // Load content fields library $config['Entry'] = $Entry; $config['content_type_id'] = $content_type_id; $this->load->add_package_path(APPPATH . 'modules/content/content_fields'); $Content_fields = $this->load->library('content_fields'); $Content_fields->initialize($config); // Check if versioning is enabled and whether a revision is loaded if ($Content_type->enable_versioning && is_numeric($revision_id)) { $Revision = new Revisions_model(); $Revision->get_by_id($revision_id); if ($Revision->exists()) { $revision_data = @unserialize($Revision->revision_data); // Update Entry and content fields from revision // Entries data gets queiried in the content_fields library initialize if (is_array($revision_data)) { $Entry->from_array($revision_data); $Content_fields->from_array($revision_data); } } } // Get content types for the setting's // content type dropdown $Change_content_types = $Content_types_model->where('id !=', $content_type_id)->order_by('title')->get(); $data['change_content_types'] = array('' => ''); foreach ($Change_content_types as $Change_content_type) { $entries_count = $Change_content_type->entries->count(); // Only add the content type to the Add Entry dropdown if it has not reached the // limit of entries allowed. An empty entries_allowed is unlimited if ($Change_content_type->entries_allowed == '' || $entries_count < $Change_content_type->entries_allowed || $entries_count == 0 && $Change_content_type->entries_allowed > 0) { $data['change_content_types'][$Change_content_type->id] = $Change_content_type->title; } } // Get Admins and Super Admins for the setting's // author dropdown $Users = $this->users_model->where_in_related('groups', 'type', array(SUPER_ADMIN, ADMINISTRATOR))->order_by('first_name')->get(); $data['authors'] = array('' => ''); foreach ($Users as $User) { $data['authors'][$User->id] = $User->full_name(); } // Validate that the content type exists if (!$Content_type->exists()) { return show_404(); } if ($Entry->exists()) { // Check that url content_type_id and entry content_type_id match if ($Entry->content_type_id != $content_type_id && !$revision_id) { return show_404(); } $data['edit_mode'] = $edit_mode = TRUE; } // Build categories tree if content type has a category group assigned if ($Content_type->category_group_id) { $this->load->library('categories_library'); $config['id'] = $Content_type->category_group_id; $config['admin_entries_categories'] = TRUE; $config['populate'] = option_array_value($Entry->categories->get(), 'id', 'id'); $data['categories_tree'] = $this->categories_library->list_categories($config); $Entry->categories->get(); } // Form Validation Rules if ($edit_mode) { $this->form_validation->set_rules('slug', 'URL', 'trim|max_length[255]|callback_unique_slug_check[' . addslashes($Entry->slug) . ']'); } else { $this->form_validation->set_rules('slug', 'URL', 'trim|max_length[255]|callback_unique_slug_check'); } $this->form_validation->set_rules('meta_title', 'Meta Title', 'trim'); $this->form_validation->set_rules('meta_description', 'Meta Description', 'trim'); $this->form_validation->set_rules('meta_keywords', 'Meta Keywords', 'trim'); $this->form_validation->set_rules('status', 'Status', 'trim|required'); $this->form_validation->set_rules('title', 'Entry Title', 'trim|required|max_length[100]'); $this->form_validation->set_rules('created_date', 'Date Created', 'trim|required'); // Validate url title if content type has a dynamic route if ($Content_type->dynamic_route != '') { $this->form_validation->set_rules('url_title', 'URL Title', 'trim|required|alpha_dash|max_length[100]|is_unique[entries.url_title' . ($edit_mode ? '.id.' . $Entry->id : '') . ']'); } // Get content fields html $field_validation = $Content_fields->validate(); // Validation and process form if ($this->form_validation->run() == TRUE && $field_validation) { // Populate from post and prep for insert / update $Entry->from_array($this->input->post()); $Entry->modified_date = date('Y-m-d H:i:s'); $Entry->created_date = date('Y-m-d H:i:s', strtotime($this->input->post('created_date'))); $Entry->slug = $this->input->post('slug') != '' ? $this->input->post('slug') : NULL; $Entry->url_title = $this->input->post('url_title') != '' ? $this->input->post('url_title') : NULL; $Entry->meta_title = $this->input->post('meta_title') != '' ? $this->input->post('meta_title') : NULL; $Entry->meta_description = $this->input->post('meta_description') != '' ? $this->input->post('meta_description') : NULL; $Entry->meta_keywords = $this->input->post('meta_keywords') != '' ? $this->input->post('meta_keywords') : NULL; $Entry->content_type_id = $content_type_id; $Entry->author_id = $this->input->post('author_id') != '' ? $this->input->post('author_id') : NULL; // Ensure the id wasn't overwritten by an id in the post if ($edit_mode) { $Entry->id = $entry_id; } $Entry->save(); // Save field data to entries_data $Content_fields->from_array($this->input->post()); $Content_fields->save(); // Add Revision if versioning enabled if ($Content_type->enable_versioning) { // Delete old revsions so that not to exceed max revisions setting $Revision = new Revisions_model(); $Revision->where_related('revision_resource_types', 'key_name', 'ENTRY')->where('resource_id', $entry_id)->order_by('id', 'desc')->limit(25, $Content_type->max_revisions - 1)->get()->delete_all(); // Serialize and save post data to entry revisions table $User = $this->secure->get_user_session(); $Revision = new Revisions_model(); $Revision->resource_id = $Entry->id; $Revision->revision_resource_type_id = Revision_resource_types_model::ENTRY; $Revision->content_type_id = $Entry->content_type_id; $Revision->author_id = $User->id; $Revision->author_name = $User->first_name . ' ' . $User->last_name; $Revision->revision_date = date('Y-m-d H:i:s'); $Revision->revision_data = serialize($this->input->post()); $Revision->save(); } // Assign entry to selected categories if ($Content_type->category_group_id) { $this->load->model('categories_entries_model'); $Categories_entries = new Categories_entries_model(); $categories_post = is_array($this->input->post('categories')) ? $this->input->post('categories') : array(); // Delete all of the entries categories that are not in the posted array $Categories_entries->where('entry_id', $Entry->id); if (!empty($categories_post)) { $Categories_entries->where_not_in('category_id', $categories_post); } $Categories_entries->get()->delete_all(); // Check if categories posted already exist in the relationship table // If not add them foreach ($categories_post as $category_id) { $Categories_entries = new Categories_entries_model(); $Categories_entries->where('category_id', $category_id)->where('entry_id', $Entry->id)->get(); if (!$Categories_entries->exists()) { $Categories_entries->category_id = $category_id; $Categories_entries->entry_id = $entry_id; $Categories_entries->save(); } } } // Clear cache so updates will show on next page load $this->load->library('cache'); $this->cache->delete_all('entries'); // Clear navigation cache so updates will show on next page load $this->load->library('navigations/navigations_library'); $this->navigations_library->clear_cache(); // Set a success message $this->session->set_flashdata('message', '<p class="success">Changes Saved.</p>'); // Deteremine where to redirect user if ($this->input->post('save_exit')) { redirect(ADMIN_PATH . "/content/entries"); } else { redirect(ADMIN_PATH . "/content/entries/edit/" . $Entry->content_type_id . "/" . $Entry->id); } } $_SESSION['KCFINDER'] = array(); $_SESSION['KCFINDER']['disabled'] = false; $_SESSION['isLoggedIn'] = true; // Field form needs to be built after running form_validation->run() $data['Fields'] = $Content_fields->form(); $this->template->view('admin/entries/edit', $data); }
function edit() { // Init $data = array(); $data['breadcrumb'] = set_crumbs(array('content/types' => 'Content Types', current_url() => 'Content Type Edit')); $data['revision_id'] = $revision_id = $this->uri->segment(6); $this->load->model('content_types_model'); $this->load->model('content_types_admin_groups_model'); $this->load->model('users/groups_model'); $this->load->model('category_groups_model'); // Load codemirror $this->template->add_package(array('codemirror', 'zclip')); $content_type_id = $this->uri->segment(5); $data['Content_type'] = $Content_type = $this->content_types_model->get_by_id($content_type_id); $data['theme_layouts'] = $this->template->get_theme_layouts($this->settings->theme, TRUE); $data['Admin_groups'] = $Admin_groups = $this->groups_model->where('type', ADMINISTRATOR)->order_by('name')->get(); // Get all groups except super admin for group access $Groups = new Groups_model(); $data['Groups'] = $Groups->where('type !=', SUPER_ADMIN)->order_by('name')->get(); // Get all category groups for dropdown $data['category_groups'] = option_array_value($this->category_groups_model->order_by('title')->get(), 'id', 'title', array('' => '')); // Check if layout exists if (!$data['Content_type']->exists()) { return show_404(); } // Get selected restrict to groups for repopulation $data['restrict_to'] = @unserialize($data['Content_type']->restrict_to); if (!is_array($data['restrict_to'])) { $data['restrict_to'] = (array) $data['restrict_to']; } // 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); $Content_type->from_array($revision_data); } else { return show_404(); } } // Get admin groups currently assigned to this conten type $data['current_admin_groups'] = option_array_value($Content_type->admin_groups->get(), 'id', 'group_id'); $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[content_types.short_name.id.' . $content_type_id . ']'); $this->form_validation->set_rules('enable_dynamic_routing', 'Enable Dynamic Routing', 'trim'); $this->form_validation->set_rules('enable_versioning', 'Enable Versioning', 'trim|required'); $this->form_validation->set_rules('max_revisions', 'Max Revisions', 'trim|required|integer|less_than[26]'); $this->form_validation->set_rules('category_group_id', 'Category Group', 'trim'); $this->form_validation->set_rules('restrict_admin_access', 'Restrict Admin Access', 'trim|required'); $this->form_validation->set_rules('selected_admin_groups[]', 'Administrative Access', 'trim'); $this->form_validation->set_rules('access', 'Access', 'trim|required'); $this->form_validation->set_rules('restrict_to[]', 'Group Access', 'trim'); $this->form_validation->set_rules('entries_allowed', 'Number of Entries Allowed', 'trim|integer'); $this->form_validation->set_rules('layout', 'layout', ''); // Add dynamic route validation if enable dynamic routing checkbox selected if ($this->input->post('enable_dynamic_routing') == 1) { $this->form_validation->set_rules('dynamic_route', 'Dynamic Route', 'trim|required|max_length[255]|callback_unique_dynamic_route[' . $Content_type->dynamic_route . ']'); } // Form validation if ($this->form_validation->run() == TRUE) { // Detect if the category group changed. // If it has changed delete entry category relations // of the content type if ($Content_type->category_group_id != $this->input->post('category_group_id')) { $Content_type->entries->get(); foreach ($Content_type->entries as $Entry) { $Entry->categories->get(); $Entry->delete($Entry->categories->all, 'categories'); } } $Content_type->from_array($this->input->post()); $Content_type->id = $content_type_id; $Content_type->dynamic_route = $this->input->post('dynamic_route') != '' && $this->input->post('enable_dynamic_routing') ? $this->input->post('dynamic_route') : NULL; $Content_type->restrict_to = $this->input->post('access') == 2 ? serialize($this->input->post('restrict_to')) : NULL; $Content_type->category_group_id = $this->input->post('category_group_id') != '' ? $this->input->post('category_group_id') : NULL; $Content_type->layout = trim($this->input->post('layout')) != '' ? $this->input->post('layout') : NULL; $Content_type->page_head = trim($this->input->post('page_head')) != '' ? $this->input->post('page_head') : NULL; $Content_type->theme_layout = $this->input->post('theme_layout') != '' ? $this->input->post('theme_layout') : NULL; $Content_type->entries_allowed = $this->input->post('entries_allowed') != '' ? $this->input->post('entries_allowed') : NULL; $Content_type->save(); $Content_type->add_revision(); // Assign admin groups to this content type // if restrict admin access is enabled if ($this->content_types_model->restrict_admin_access && is_array($this->input->post('selected_admin_groups'))) { $selected_admin_groups = $this->input->post('selected_admin_groups'); foreach ($Admin_groups as $Admin_group) { $Content_types_admin_groups = new Content_types_admin_groups_model(); $Content_types_admin_groups->where('content_type_id', $Content_type->id)->where('group_id', $Admin_group->id)->get(); if (in_array($Admin_group->id, $selected_admin_groups)) { // Admin group was selected so Update or Insert to database $Content_types_admin_groups->content_type_id = $Content_type->id; $Content_types_admin_groups->group_id = $Admin_group->id; $Content_types_admin_groups->save(); } else { // Admin group was NOT selected so delete it $Content_types_admin_groups->delete_all(); } } } else { // Restrict admin access was disabled so remove any assigned groups from database $Content_types_admin_groups = new Content_types_admin_groups_model(); $Content_types_admin_groups->where('content_type_id', $this->content_types_model->id)->get(); $Content_types_admin_groups->delete_all(); } // Clear cache $this->load->library('cache'); $this->cache->delete_all('entries'); $this->cache->delete_all('content_types'); $this->session->set_flashdata('message', '<p class="success">Content Type Saved.</p>'); if ($this->input->post('save_exit')) { redirect(ADMIN_PATH . '/content/types/'); } else { redirect(ADMIN_PATH . '/content/types/edit/' . $this->content_types_model->id); } } $data['Fields'] = $this->content_types_model->content_fields->order_by('sort')->get(); $this->template->view('admin/types/edit', $data); }