Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.37 KB | None | 0 0
  1. <?php
  2.  
  3. class pages extends Admin_Controller {
  4.  
  5.     public function __construct() {
  6.         parent::__construct();
  7.         $this->template->set_breadcrumb('Manage Pages', site_url('admin/pages'));
  8.         $this->data['allEditors'] = $this->user_model->get_many_by('group', 2);
  9.     }
  10.  
  11.     public function index() {
  12.  
  13.         switch ($this->input->post('submit')) {
  14.             case 'New':
  15.                 redirect('admin/pages/add');
  16.                 break;
  17.             case'Edit':
  18.                 if ($this->input->post('pageid')) {
  19.                     redirect('admin/pages/edit/' . $this->input->post('pageid'));
  20.                 }
  21.                 else {
  22.                     $this->session->set_flashdata('information', '<p class="red"><strong>Error - You must select a page to edit</strong></p>');
  23.                     redirect('admin/pages');
  24.                 }
  25.                 break;
  26.             case'Delete':
  27.                 if ($this->input->post('pageid')) {
  28.                     redirect('admin/pages/delete/' . $this->input->post('pageid'));
  29.                 }
  30.                 else {
  31.                     $this->session->set_flashdata('information', '<p class="red"><strong>Error - You must select a page to delete</strong></p>');
  32.                     redirect('admin/pages');
  33.                 }
  34.             default :
  35.                 $this->template->write_view('content', 'desktop/views/' . $this->data['viewbase'] . '/pages/index', $this->data);
  36.                 $this->template->render();
  37.                 break;
  38.         }
  39.     }
  40.  
  41.     /**
  42.      * VIEW METHODS
  43.      */
  44.     public function add() {
  45.         if (!isAdmin()) {
  46.             $this->session->set_flashdata('information', '<p class="red">You do not have permission to perform this action</p>');
  47.             redirect('/admin/pages');
  48.         }
  49.         $this->template->set_breadcrumb('Create Page', site_url('admin/pages/add'));
  50.         $this->template->write_view('content', 'desktop/views/admin/pages/add', $this->data);
  51.         $this->template->render();
  52.     }
  53.  
  54.     public function edit() {
  55.         $this->data['editPage'] = $this->page_model->get($this->uri->segment('4'));
  56.         $this->template->set_breadcrumb('Edit Page: ' . $this->data['editPage']->title, site_url('admin/pages/edit/' . $this->data['editPage']->id));
  57.         $this->template->write_view('content', 'desktop/views/' . $this->data['viewbase'] . '/pages/edit', $this->data);
  58.         $this->template->render();
  59.     }
  60.  
  61.     public function delete() {
  62.         if (!isAdmin()) {
  63.             $this->session->set_flashdata('information', '<p class="red">You do not have permission to perform this action</p>');
  64.             redirect('/admin/pages');
  65.         }
  66.         $this->data['deletePage'] = $this->page_model->get($this->uri->segment('4'));
  67.         $this->template->set_breadcrumb('Delete Page: ' . $this->data['deletePage']->title, site_url('admin/pages/delete'));
  68.         $this->template->write_view('content', 'desktop/views/admin/pages/delete', $this->data);
  69.         $this->template->render();
  70.     }
  71.  
  72.     /**
  73.      * ACTION METHODS
  74.      */
  75.     public function create() {
  76.         if (!isAdmin()) {
  77.             $this->session->set_flashdata('information', '<p class="red">You do not have permission to perform this action</p>');
  78.             redirect('/admin/pages');
  79.         }
  80.  
  81.         $rules = array(
  82.             array('field' => 'title', 'label' => 'Page Title', 'rules' => 'trim|required|xss_clean|callback_title_check'),
  83.             array('field' => 'content', 'label' => 'Page Content', 'rules' => 'trim|xss_clean'),
  84.         );
  85.  
  86.         $this->form_validation->set_rules($rules);
  87.  
  88.         if ($this->form_validation->run()) {
  89.  
  90.             $newPage = array('title' => $this->input->post('title'), 'slug' => strtolower(url_title($this->input->post('title'), 'underscore')), 'content' => $this->input->post('content'), 'date' => date('Y-m-d H:i:s'), 'lastupdate' => date('Y-m-d H:i:s'), 'lastupdateby' => $this->session->userdata('id'));
  91.             if ($this->input->post('index')) {
  92.                 foreach ($this->page_model->get_many_by(array('idx' => '1')) as $indexPage) {
  93.                     $this->page_model->update_by('id', $indexPage->id, array('idx' => 0));
  94.                 }
  95.                 $newPage['idx'] = 1;
  96.             }
  97.  
  98.             if (sizeof($this->page_model->get_all()) == 0) {
  99.                 $newPage['idx'] = '1';
  100.             }
  101.  
  102.             $newPageID = $this->page_model->insert($newPage);
  103.  
  104.  
  105.             foreach ($this->input->post('editors') as $editor) {
  106.                 $newCrossReference = array('page' => $newPageID, 'user' => $editor);
  107.                 if (!$this->xref_model->insert($newCrossReference)) {
  108.                     exit(mysql_error());
  109.                 }
  110.             }
  111.  
  112.             $this->session->set_flashdata('information', '<p class="green">The page was created successfully</p>');
  113.             redirect(site_url('admin/pages'));
  114.         }
  115.         else {
  116.             $this->message->set('error', explode("\n", trim(strip_tags(validation_errors()))));
  117.             $this->template->write_view('content', 'desktop/views/admin/pages/add', $this->data);
  118.             $this->template->render();
  119.         }
  120.     }
  121.  
  122.     public function update() {
  123.         $rules = array(
  124.             array('field' => 'title', 'label' => 'Page Title', 'rules' => 'trim|required|xss_clean|callback_update_title_check'),
  125.         );
  126.  
  127.         $this->form_validation->set_rules($rules);
  128.  
  129.         if ($this->form_validation->run()) {
  130.             if (isAdmin()) {
  131.                 $allEditors = array();
  132.                 foreach ($this->data['allEditors'] as $editor) {
  133.                     $allEditors[] = $editor->id;
  134.                 }
  135.                 $editorsPosted = $this->input->post('editors');
  136.                 $editorsNotPosted = array_diff($allEditors, $editorsPosted);
  137.  
  138.                 if (sizeof($editorsNotPosted) > 0) {
  139.                     foreach ($editorsNotPosted as $editorNotPosted) {
  140.                         $this->xref_model->delete_by(array('user' => $editorNotPosted, 'page' => $this->input->post('pageid')));
  141.                     }
  142.                 }
  143.  
  144.                 if (sizeof($editorsPosted) > 0) {
  145.                     foreach ($editorsPosted as $editorPosted) {
  146.                         $this->xref_model->insert(array('user' => $editorPosted, 'page' => $this->input->post('pageid')));
  147.                     }
  148.                 }
  149.             }
  150.  
  151.             $updatedPage = array('title' => $this->input->post('title'), 'slug' => url_title(strtolower($this->input->post('title')), 'underscore'), 'content' => $this->input->post('content'), 'lastupdate' => date('Y-m-d H:i:s'), 'lastupdateby' => $this->session->userdata('id'));
  152.  
  153.             if (isAdmin()) {
  154.                 if ($this->input->post('index')) {
  155.                     foreach ($this->page_model->get_many_by(array('idx' => '1')) as $indexPage) {
  156.                         $this->page_model->update_by('id', $indexPage->id, array('idx' => 0));
  157.                     }
  158.                     $updatedPage['idx'] = 1;
  159.                 }
  160.             }
  161.  
  162.             if (($this->input->post('published')) | ($this->page_model->get($this->input->post('pageid'))->idx == '1') | ($this->input->post('index'))) {
  163.                 $updatedPage['published'] = 1;
  164.             }
  165.             else {
  166.                 $updatedPage['published'] = 0;
  167.             }
  168.  
  169.             $this->page_model->update_by('id', $this->input->post('pageid'), $updatedPage);
  170.             $this->session->set_flashdata('information', '<p class="green">Your Changes To ' . $this->input->post('title') . ' were saved</p>');
  171.             redirect(site_url('admin/pages'));
  172.         }
  173.         else {
  174.  
  175.             $this->data['editPage'] = $this->page_model->get($this->input->post('pageid'));
  176.             $this->template->set_breadcrumb('Edit Page: ' . $this->data['editPage']->title, site_url('admin/pages/edit/' . $this->data['editPage']->id));
  177.             $this->message->set('error', explode("\n", trim(strip_tags(validation_errors()))));
  178.             $this->template->write_view('content', 'desktop/views/' . $this->data['viewbase'] . '/pages/edit', $this->data);
  179.             $this->template->render();
  180.         }
  181.     }
  182.  
  183.     public function drop() {
  184.         if (!isAdmin()) {
  185.             $this->session->set_flashdata('information', '<p class="red">You do not have permission to perform this action</p>');
  186.             redirect('/admin/pages');
  187.         }
  188.         if ($this->input->post('submit') == 'Yes') {
  189.             $this->page_model->delete_by('id', $this->input->post('pageid'));
  190.             $this->session->set_flashdata('information', '<p class="green">The page was successfully deleted</p>');
  191.         }
  192.  
  193.         redirect('admin/pages');
  194.     }
  195.  
  196.     public function title_check($title) {
  197.         if ($this->page_model->get_by('title', $title)) {
  198.             $this->form_validation->set_message('title_check', 'The page title "' . $title . '" is already in use.');
  199.             return FALSE;
  200.         }
  201.         else {
  202.             return TRUE;
  203.         }
  204.     }
  205.  
  206.     public function update_title_check($title) {
  207.         if ((sizeof($this->page_model->get_many_by('title', $title)) - 1) >= 1) {
  208.             $this->form_validation->set_message('update_title_check', 'The page title "' . $title . '" is already in use.');
  209.             return FALSE;
  210.         }
  211.         else {
  212.             return TRUE;
  213.         }
  214.     }
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement