Welcome Guest, Not a member yet? Register   Sign In
Making things DRY. Some feedback please.
#2

[eluser]Bramme[/eluser]
Code:
function edit($newsID = NULL)
{
    $this->auth->restrict();
    
    if (!isset($newsID))
    {
        redirect('/admin/news/overview');
    }
    
    $this->load->library('validation');
    
    //rules    
    $this->validation->set_rules($rules);
    
    //fields    
    $this->validation->set_fields($fields);
    $this->validation->set_error_delimiters('<li>', '</li>');
    
    // get values from db
    $this->db->where('newsID', $newsID);
    $qry = $this->db->get('news');
    $row = $qry->row();
    foreach ($fields as $field => $name)
    {
        $standard_values[$field] = $row->$field;
    }
    $standard_values['type'] = $row->type;
    
    if ($this->validation->run() == TRUE)
    {
        foreach ($fields as $field => $name)
        {
            $insert[$field] = $this->input->post($field);
        }
        
        $timestamp = time();
        $insert['datetime'] = unix_to_human($timestamp, TRUE, 'eu');
        $insert['type'] = $this->input->post('type');
        
        if ($this->input->post('chk') == 'yes')
        {
            if ($_FILES['thumb_url']['error'] != 4)
            {
                $upload_config['upload_path'] = './_assets/images/thumbs/';
                $upload_config['allowed_types'] = 'gif|jpg|png';
                $upload_config['max_size'] = '100';
                $upload_config['max_width'] = '150';
                $upload_config['max_height'] = '150';
                
                $this->load->library('upload', $upload_config);
                
                if ($this->upload->do_upload('thumb_url'))
                {
                    $info = $this->upload->data();
                    $insert['thumb_url'] =     '/_assets/images/thumbs/'.$info['file_name'];
                } else {
                    echo $this->upload->display_errors();
                    die();    
                }
            }
            else
            {
                $insert['thumb_url'] = '/_assets/images/thumbs/m.gif';
            }
        }
        
        $this->db->where('newsID', $newsID);
        if ( ! $this->db->update('news', $insert))
        {
            send_notice('error', 'Something went wrong with the query', 'self');
        }
        else
        {
            send_notice('success', 'Item successfully edited', 'self');
        }
    }
    else
    {
        
        if ( ! empty($this->validation->error_string))
        {
            send_notice('validation_error', $this->validation->error_string);
            
            $standard_values['poster'] = $this->input->post('poster');
            $standard_values['type'] = $this->input->post('type');
            $standard_values['title'] = $this->input->post('title');
            $standard_values['content'] = $this->input->post('content');
        }
    }
    
    $data['form'] = $this->table_editor->create_form($standard_values, TRUE);
    $data['action'] = '/admin/news/edit/'.$newsID;
    $this->template->add_js('_assets/js/jquery.js');
    $this->template->add_js('_assets/js/news_edit.js');
    $this->template->write_view('region_content', '_admin/news/new', $data);
    $this->template->render();
}
That's the second method.


Messages In This Thread
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 01:00 AM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 01:00 AM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 02:13 AM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 04:39 AM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 05:00 AM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 07:20 AM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 11:51 AM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 11:58 AM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 12:08 PM
Making things DRY. Some feedback please. - by El Forum - 08-08-2008, 12:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB