From d9a4f93cfa23c0911ccbc46ac74f03da3377784b Mon Sep 17 00:00:00 2001 From: i92guboj Date: Mon, 19 Dec 2016 20:18:05 +0100 Subject: [PATCH] pic_filename behavior and silent migration from pic_id --- application/controllers/Items.php | 85 ++++++++++++++++++++++------ application/helpers/table_helper.php | 16 ++++-- 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/application/controllers/Items.php b/application/controllers/Items.php index 96cf176ac..9ddec2c24 100644 --- a/application/controllers/Items.php +++ b/application/controllers/Items.php @@ -63,21 +63,46 @@ class Items extends Secure_Controller foreach($items->result() as $item) { $data_rows[] = $this->xss_clean(get_item_data_row($item, $this)); + + // Guess whether file extension is not in the table field, + // if it isn't, then it's an old-format (formerly pic_id) field, + // so we guess the right filename and update the table + $ext = pathinfo($item->pic_filename, PATHINFO_EXTENSION); + if($ext == '') + { + $images = glob('./uploads/item_pics/' . $item->pic_filename . '.*'); + $new_pic_filename = pathinfo($images[0], PATHINFO_BASENAME); + $item_data = array('pic_filename' => $new_pic_filename); + $this->Item->save($item_data, $item->item_id); + } } echo json_encode(array('total' => $total_rows, 'rows' => $data_rows)); } - public function pic_thumb($pic_id) + public function pic_thumb($pic_filename) { $this->load->helper('file'); $this->load->library('image_lib'); - $base_path = './uploads/item_pics/' . $pic_id; - $images = glob($base_path . '.*'); + + $ext = pathinfo($pic_filename, PATHINFO_EXTENSION); + if($ext == '') + { + // if file extension is not found guess it (legacy) + $images = glob('./uploads/item_pics/' . $pic_filename . '.*'); + // update $ext, needed below + $ext = pathinfo($images[0], PATHINFO_EXTENSION); + } + else + { + $images = glob('./uploads/item_pics/' . $pic_filename); + } + + // make sure we pick only the file name, without extension + $base_path = './uploads/item_pics/' . pathinfo($pic_filename, PATHINFO_FILENAME); if(sizeof($images) > 0) { $image_path = $images[0]; - $ext = pathinfo($image_path, PATHINFO_EXTENSION); $thumb_path = $base_path . $this->image_lib->thumb_marker . '.' . $ext; if(sizeof($images) < 2) { @@ -87,8 +112,8 @@ class Items extends Secure_Controller $config['create_thumb'] = TRUE; $config['width'] = 52; $config['height'] = 32; - $this->image_lib->initialize($config); - $image = $this->image_lib->resize(); + $this->image_lib->initialize($config); + $image = $this->image_lib->resize(); $thumb_path = $this->image_lib->full_dst_path; } $this->output->set_content_type(get_mime_by_extension($thumb_path)); @@ -189,8 +214,24 @@ class Items extends Secure_Controller $data['suppliers'] = $suppliers; $data['selected_supplier'] = $item_info->supplier_id; - $data['logo_exists'] = $item_info->pic_id != ''; - $images = glob('./uploads/item_pics/' . $item_info->pic_id . '.*'); + $data['logo_exists'] = $item_info->pic_filename != ''; + $ext = pathinfo($item_info->pic_filename, PATHINFO_EXTENSION); + if($ext == '') + { + // if file extension is not found guess it (legacy) + $images = glob('./uploads/item_pics/' . $item_info->pic_filename . '.*'); + // try to update pic_filename in db, following 3 lines might not be necessary + // after what I put in search(), feel free to remove it if you also think it's + // redundant. + $new_pic_filename = pathinfo($images[0], PATHINFO_BASENAME); + $item_data = array('pic_filename' => $new_pic_filename); + $this->Item->save($item_data, $item_id); + } + else + { + // else just pick that file + $images = glob('./uploads/item_pics/' . $item_info->pic_filename); + } $data['image_path'] = sizeof($images) > 0 ? base_url($images[0]) : ''; $stock_locations = $this->Stock_location->get_undeleted_all()->result_array(); @@ -346,7 +387,7 @@ class Items extends Secure_Controller // XSS file image sanity check if($this->xss_clean($upload_data['raw_name'], TRUE) === TRUE) { - $item_data['pic_id'] = $upload_data['raw_name']; + $item_data['pic_filename'] = $upload_data['raw_name']; } } @@ -431,19 +472,16 @@ class Items extends Secure_Controller echo !$exists ? 'true' : 'false'; } - private function _handle_image_upload() + private function _handle_image_upload($pic_filename) { - $this->load->helper('directory'); - - $map = directory_map('./uploads/item_pics/', 1); - + /* Let files be uploaded with their original name */ + // load upload library $config = array('upload_path' => './uploads/item_pics/', 'allowed_types' => 'gif|jpg|png', 'max_size' => '100', 'max_width' => '640', - 'max_height' => '480', - 'file_name' => sizeof($map) + 1 + 'max_height' => '480' ); $this->load->library('upload', $config); $this->upload->do_upload('item_image'); @@ -453,7 +491,7 @@ class Items extends Secure_Controller public function remove_logo($item_id) { - $item_data = array('pic_id' => NULL); + $item_data = array('pic_filename' => NULL); $result = $this->Item->save($item_data, $item_id); echo json_encode(array('success' => $result)); @@ -597,6 +635,7 @@ class Items extends Secure_Controller // XSS file data sanity check $data = $this->xss_clean($data); + /* haven't touched this so old templates will work, or so I guess... */ if(sizeof($data) >= 23) { $item_data = array( @@ -620,6 +659,18 @@ class Items extends Secure_Controller 'custom9' => $data[22], 'custom10' => $data[23] ); + + /* we could do something like this, however, the effectiveness of + this is rather limited, since for now, you have to upload files manually + into that directory, so you really can do whatever you want, this probably + needs further discussion */ + + $pic_file = $data[26]; + /*if(strcmp('.htaccess', $pic_file)==0) { + $pic_file=''; + }*/ + $item_data['pic_filename']=$pic_file; + $item_number = $data[0]; $invalidated = FALSE; if($item_number != '') diff --git a/application/helpers/table_helper.php b/application/helpers/table_helper.php index 2e096a504..4c0e9229a 100644 --- a/application/helpers/table_helper.php +++ b/application/helpers/table_helper.php @@ -268,13 +268,21 @@ function get_item_data_row($item, $controller) $tax_percents = substr($tax_percents, 0, -2); $controller_name = strtolower(get_class($CI)); - $image = ''; - if ($item->pic_id != '') + if ($item->pic_filename != '') { - $images = glob('./uploads/item_pics/' . $item->pic_id . '.*'); + $ext = pathinfo($item->pic_filename, PATHINFO_EXTENSION); + if($ext == '') + { + // legacy + $images = glob('./uploads/item_pics/' . $item->pic_filename . '.*'); + } + else { + // preferred + $images = glob('./uploads/item_pics/' . $item->pic_filename); + } if (sizeof($images) > 0) { - $image .= ''; + $image .= ''; } }