Skip to content

Instantly share code, notes, and snippets.

@ambercouch
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambercouch/3272090372b574700214 to your computer and use it in GitHub Desktop.
Save ambercouch/3272090372b574700214 to your computer and use it in GitHub Desktop.
profile controller
<?php
function profiles($action = FALSE, $id = FALSE) {
$data = $this->data;
//set the file upload path is the method as the admin controler might upload files for different things
$this->original_path = realpath(APPPATH . '../uploads/profiles');
$this->thumbs_path = realpath(APPPATH . '../uploads/profiles/thumbs');
$view_content = __FUNCTION__;
if ($action != FALSE) {
//create the view filename
$view_content = $view_content . '-' . $action;
}
$data['page_title'] = "Profiltyper";
$data['page_parent'] = "Redigering";
$data['scripts'] = 'crud';
//Switch the $query based on the action
switch ($action) {
case 'del':
if ($query = $this->profiletype_model->delete_profileType($id)) {
redirect(base_url('admin/' . __FUNCTION__));
} else {
//delete error
redirect(base_url('admin/' . __FUNCTION__));
}
break;
case 'edit':
//Check if this request does not have POST data
if ($_SERVER['REQUEST_METHOD'] != "POST") {
//Request has NO Post data
if ($query = $this->profiletype_model->get_profiles($id)) {
$data['query'] = $query;
$data['id'] = $id;
}
} else {
if ($_FILES['userfile']['name'] != '') {
//Request HAS Post data
//The form has been submited
//configure the upload library
$config['upload_path'] = $this->original_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = '1600';
$config['max_height'] = '1600';
//load the upload library
$this->load->library('upload', $config);
//Try to upload the file
if (!$this->upload->do_upload()) {
echo 'upload';
echo $this->upload->display_errors();
} else {
//If the uploads succeeds.
//Get the upload date to use in teh resize.
$image_data = $this->upload->data();
//configure the image_lib library.
//$image_data['full_path'] and $image_data['file_name'] will only work if the uploaded suceeded.
//$config['new_image'] must be a full path to the image inc file name.
$config['image_library'] = 'gd2';
$config['source_image'] = $image_data['full_path'];
$config['new_image'] = $this->thumbs_path . '/' . $image_data['file_name'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 106;
$config['height'] = 106;
//load the image_lib library
$this->load->library('image_lib', $config);
//Try to resize and save the file.
if (!$this->image_lib->resize()) {
//If fail display errors.
echo 'resize error';
echo $this->image_lib->display_errors();
}
}
}
//save the profile data to the db
if ($query = $this->profiletype_model->update_profile($id)) {
redirect(base_url('admin/profiles/edit/' . __FUNCTION__));
} else {
//update error
redirect(base_url('admin/' . __FUNCTION__));
}//If the resize succeeds.
//save the profile data to the db
if ($query = $this->profiletype_model->update_profile($id)) {
redirect(base_url('admin/profiles/edit/' . __FUNCTION__));
} else {
//update error
redirect(base_url('admin/' . __FUNCTION__));
}
}
break;
case 'add':
if ($_SERVER['REQUEST_METHOD'] != "POST") {
$query = $this->doorwidth_model->get_door();
$data['door_widths'] = $query;
$query = $this->profilecolour_model->get_profileColour();
$data['door_colour'] = $query;
// load the add view (already taken care of)
} else {
if ($this->input->post('type') != '') {
if ($_FILES['userfile']['name'] != '') {
//Request HAS Post data
//The form has been submited
//configure the upload library
$config['upload_path'] = $this->original_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = '1600';
$config['max_height'] = '1600';
//load the upload library
$this->load->library('upload', $config);
//Try to upload the file
if (!$this->upload->do_upload()) {
echo 'upload';
echo $this->upload->display_errors();
die();
} else {
//If the uploads succeeds.
//Get the upload date to use in teh resize.
$image_data = $this->upload->data();
//configure the image_lib library.
//$image_data['full_path'] and $image_data['file_name'] will only work if the uploaded suceeded.
//$config['new_image'] must be a full path to the image inc file name.
$config['image_library'] = 'gd2';
$config['source_image'] = $image_data['full_path'];
$config['new_image'] = $this->thumbs_path . '/' . $image_data['file_name'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 106;
$config['height'] = 106;
//load the image_lib library
$this->load->library('image_lib', $config);
//Try to resize and save the file.
if (!$this->image_lib->resize()) {
//If fail display errors.
echo 'resize error';
echo $this->image_lib->display_errors();
die();
}
}
} else {
echo 'no image';
die();
}
if ($query = $this->profiletype_model->add_profileType($id)) {
redirect(base_url('admin/' . __FUNCTION__));
} else {
//add error
redirect(base_url('admin/' . __FUNCTION__));
}
} else {
//no colour error
redirect(base_url('admin/' . __FUNCTION__));
}
}
break;
default:
if ($query = $this->profiletype_model->get_profiles()) {
$data['query'] = $query;
}
break;
}//End $action Switch
$this->load->view('templates/admin/shamcey/header', $data);
$this->load->view('admin/shamcey/' . $view_content, $data);
$this->load->view('templates/admin/shamcey/footer', $data);
}
<?php
public function update_profile($id) {
//update profiletype
$prices = $this->input->post('price');
//if there is a file uploaded
if ($_FILES['userfile']['name'] != '') {
//get the upload data
$img_data = $this->upload->data();
//get resize_data from the image_lib
$resize_data = $this->image_lib;
//get the uploaded file name
$img_name = $img_data['file_name'];
//make uploaded file name thumb
$img_thumb = $img_data['raw_name'] . $resize_data->thumb_marker . $img_data['file_ext'];
}
$dealer_prices = array();
if ($this->input->post('dealer_price')) {
$dealer_prices = $this->input->post('dealer_price');
}
$type_price = $prices[1];
$data = array(
'type' => $this->input->post('type'),
'info' => $this->input->post('info'),
'price' => $type_price
);
//only enter the image names if they exist
if (isset($img_name)) {
$data['img'] = $img_name;
$data['imgthumb'] = $img_thumb;
}
$q = $this->db->where('id', $id);
$q = $this->db->update('profileType', $data);
// remove old profile colors
$q = $this->db->where('profileType_id', $id);
$q = $this->db->delete('profile_typeColour');
//insert profile colours
foreach ($this->input->post('colour') as $colour) {
$data = array(
'profileType_id' => $id,
'profileColour_id' => $colour
);
$this->db->insert('profile_typeColour', $data);
}
//echo '<pre>';print_r($prices);
//insert profile price
foreach ($prices as $key => $price) {
$priceVariable = round($price / $type_price, 4);
$data = array(
'priceVariable' => $priceVariable
);
$this->db->where('profileType_id', $id);
$this->db->where('doorWidth_id', $key);
$this->db->update('doorPrice', $data);
}
//insert profile Dealer price
if (!empty($dealer_prices)) {
foreach ($dealer_prices as $key => $price) {
$dealer_price = round($price / $type_price, 4);
$data = array(
'dealer_price' => $dealer_price
);
$this->db->where('profileType_id', $id);
$this->db->where('doorWidth_id', $key);
$this->db->update('doorPrice', $data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment