Advertisement
Guest User

Add / Edit

a guest
Dec 3rd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.88 KB | None | 0 0
  1.  
  2. /*=================================================================================
  3.         ADD ARTICLE
  4. ==================================================================================*/
  5.     public function add(){
  6.         $this->load->helper('application_helper');
  7.         //Validation Rules
  8.         $this->form_validation->set_rules('title','title','required');
  9.         $this->form_validation->set_rules('body','Body','trim|required');
  10.         $this->form_validation->set_rules('is_published','Publish','required');
  11.         $this->form_validation->set_rules('category','Category','required');
  12.        
  13.         $data['categories'] = $this->Article_model->get_categories();
  14.        
  15.         $data['users'] = $this->User_model->get_users();
  16.        
  17.         $data['groups'] = $this->User_model->get_groups();
  18.        
  19.         if( $this->form_validation->run() == FALSE ){
  20.             // if the form has not been submitted, show the add article form
  21.             $data['main_content'] = 'admin/articles/add';  // the form
  22.             $this->load->view('admin/layouts/main', $data); // template that contains this form
  23.         }
  24.        
  25.         else {  // Collect the values when the form is submitted
  26.  
  27.             // POST FEATURED IMAGE DATA
  28.             $config['upload_path'] = './uploads/';
  29.             $config['allowed_types'] = 'gif|jpg|png';
  30.             $config['max_size']    = '1000';
  31.             $config['max_width']  = '1300';
  32.             $config['max_height']  = '1300';        
  33.  
  34.             $this->load->library('upload', $config);
  35.  
  36.             // Check
  37.             if( !$this->upload->do_upload('post_image') ) {
  38.                 //echo  $this->upload->display_errors() ;
  39.                 echo '<br><h1>IMAGE COULD NOT BE UPLOADED</h1><br>';
  40.                 //exit() ;
  41.                 $image = '';
  42.             }
  43.             else {
  44.                 $data_upload_files = $this->upload->data();
  45.                 echo '<h1>IMAGE WAS SUCCESSFULLY UPLOADED CHECK DATABASE</h1>';
  46.                 $image = $data_upload_files['file_name'];    
  47.                 echo 'The name of the file is:  ' . $image ;
  48.             }
  49.  
  50.             // Check if the seoUrl helper was successful
  51.             $seo_url =  seoUrl( $this->input->post('slug') ) ;
  52.             if ( empty( $seo_url )  )  {
  53.                 $seo_url =  seoUrl( $this->input->post('title') ) ;
  54.             }
  55.  
  56.             //Create Articles Data Array
  57.             $data = array(
  58.                     'title'         => $this->input->post('title'),
  59.                     'body'          => $this->input->post('body'),
  60.                     'slug'          => $seo_url,
  61.                     'post_image'    => $image,
  62.                     'category_id'   => $this->input->post('category'),
  63.                     'user_id'       => $this->input->post('user'),
  64.                     'access'           => $this->input->post('access'),
  65.                     'is_published'  => $this->input->post('is_published'),
  66.                     'in_menu'          => $this->input->post('in_menu'),
  67.                     'order'          => $this->input->post('order')
  68.             );
  69.  
  70.  
  71.             echo "<br><br>the name of this Image FIle is: $image <br><br>" ;
  72.            
  73.             // Insert to Articles table, this is defined on the model
  74.             //print_r($data) ;
  75.             //exit() ;
  76.             $this->Article_model->insert($data);
  77.            
  78.             print_r($data) ;
  79.             exit() ;
  80.  
  81.             //Create Message
  82.             $this->session->set_flashdata('article_saved', 'Your article has been saved');
  83.            
  84.            
  85.            
  86.             //Redirect to pages
  87.             redirect('admin/articles');
  88.         }
  89.     }
  90.  
  91.  
  92.  
  93.  
  94. /*=================================================================================
  95.         EDIT ARTICLE
  96. ==================================================================================*/
  97.     public function edit($id){
  98.         //Validation Rules
  99.         $this->form_validation->set_rules('title','Title');
  100.         $this->form_validation->set_rules('body','Body','trim|required');
  101.         $this->form_validation->set_rules('is_published','Publish','required');
  102.         $this->form_validation->set_rules('category','Category','required');
  103.    
  104.         $data['categories'] = $this->Article_model->get_categories();
  105.         $data['users'] = $this->User_model->get_users();
  106.         $data['article'] = $this->Article_model->get_article($id);
  107.         $data['groups'] = $this->User_model->get_groups();
  108.  
  109.  
  110.         if ($this->form_validation->run() == FALSE){
  111.             //Views
  112.             $data['main_content'] = 'admin/articles/edit';
  113.             $this->load->view('admin/layouts/main', $data);
  114.  
  115.         }
  116.         else {
  117.  
  118.             // POST FEATURED IMAGE DATA
  119.             $config['upload_path'] = './uploads/';
  120.             $config['allowed_types'] = 'gif|jpg|png';
  121.             $config['max_size']    = '1300';
  122.             $config['max_width']  = '1300';
  123.             $config['max_height']  = '1300';        
  124.  
  125.             $this->load->library('upload', $config);
  126.             echo 'after load library upload';
  127.  
  128.  
  129.        
  130.             if( ! $this->upload->do_upload('post_image')  )  {
  131.                 echo  $this->upload->display_errors() ;
  132.                 echo '<br><h1>IMAGE COULD NOT BE UPLOADED</h1><br>';
  133.                 $image = '';
  134.             }
  135.  
  136.             else {
  137.                 $this->upload->do_upload();
  138.                 $data_upload_files = array('upload_data' => $this->upload->data() );
  139.                 $image_data = $this->upload->data() ;
  140.                 echo 'Data Upload Files: ' . print_r($data_upload_files)  . ' <br>' ;
  141.                 echo '<h1>IMAGE WAS SUCCESSFULLY UPLOADED CHECK DATABASE</h1>';
  142.                 $image = $image_data['file_name'];    
  143.  
  144.             }
  145.  
  146.  
  147.             //Create Articles Data Array
  148.             $data = array(
  149.                     'title'         => $this->input->post('title'),
  150.                     'body'          => $this->input->post('body'),
  151.                     'slug'          => $this->input->post('slug'),
  152.                     'post_image'    => $image,
  153.                     'category_id'   => $this->input->post('category'),
  154.                     'user_id'       => $this->input->post('user'),
  155.                     'access'        => $this->input->post('access'),
  156.                     'is_published'  => $this->input->post('is_published'),
  157.                     'in_menu'       => $this->input->post('in_menu'),
  158.                     'order'         => $this->input->post('order')
  159.             );
  160.                
  161.             //Articles Table Insert
  162.        
  163.             $this->Article_model->update($data, $id);
  164.  
  165.             //Create Message
  166.             $this->session->set_flashdata('article_saved', 'Your article has been saved');
  167.                
  168.             //Redirect to pages
  169.             redirect('admin/articles');
  170.         }
  171.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement