330 lines
11 KiB
PHP
330 lines
11 KiB
PHP
<?php
|
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Uploads extends Admin_Controller {
|
|
|
|
var $template = array(
|
|
'table_open' => "<table class='table table-striped table-hover table-bordered table-condensed'>",
|
|
'thead_open' => '<thead class=\'bg-indigo\'>',
|
|
'thead_close' => '</thead>',
|
|
'heading_row_start' => '<tr>',
|
|
'heading_row_end' => '</tr>',
|
|
'heading_cell_start' => '<th>',
|
|
'heading_cell_end' => '</th>',
|
|
'tbody_open' => '<tbody>',
|
|
'tbody_close' => '</tbody>',
|
|
'row_start' => '<tr>',
|
|
'row_end' => '</tr>',
|
|
'cell_start' => '<td>',
|
|
'cell_end' => '</td>',
|
|
'row_alt_start' => '<tr>',
|
|
'row_alt_end' => '</tr>',
|
|
'cell_alt_start' => '<td>',
|
|
'cell_alt_end' => '</td>',
|
|
'table_close' => '</table>'
|
|
);
|
|
|
|
public function index() {
|
|
$this->load->helper('url');
|
|
$data = array();
|
|
$this->renderUploadPage('view_dash', $data);
|
|
// echo 'Ameye Olu';
|
|
}
|
|
|
|
protected function renderAdminPage($page_name, $data) {
|
|
$this->load->view('admin/view_admin_header', $data);
|
|
$this->load->view('admin/' . $page_name, $data);
|
|
$this->load->view('admin/view_admin_footer', $data);
|
|
}
|
|
|
|
public function getValueOfCardImages() {
|
|
return [
|
|
'category_id' => trim($this->input->get('card_category')),
|
|
'unique_id' => trim($this->input->get('unique_id')),
|
|
'from_date' => trim($this->input->get('from_date')),
|
|
'to_date' => trim($this->input->get('to_date')),
|
|
'catid' => trim($this->input->get('catid'))
|
|
];
|
|
}
|
|
|
|
public function setComboForCardImages($params) {
|
|
$this->load->model('combo_model');
|
|
|
|
$combo['card_category'] = $this->combo_model->getCardImageCategoryCombo(
|
|
'card_category',
|
|
$params['category_id']
|
|
);
|
|
$combo['catid'] = $this->combo_model->getCardImageCategoryCombo(
|
|
'catid',
|
|
$params['catid']
|
|
);
|
|
|
|
return $combo;
|
|
}
|
|
|
|
public function validateValueForCardImages($params) {
|
|
$this->load->library('form_validation');
|
|
$this->form_validation->set_data($params);
|
|
$this->setFormRuleForCardImages();
|
|
|
|
$errors = [];
|
|
if ($this->form_validation->run() === FALSE) {
|
|
$errors = $this->form_validation->error_array();
|
|
}
|
|
return $errors;
|
|
}
|
|
|
|
public function setFormRuleForCardImages() {
|
|
$date_pattern = 'regex_match[/\d{4}-\d{2}-\d{2}/]';
|
|
|
|
$this->form_validation->set_rules(
|
|
'card_category',
|
|
'Category Card',
|
|
'numberic'
|
|
);
|
|
$this->form_validation->set_rules(
|
|
'from_added',
|
|
'From Added',
|
|
$date_pattern
|
|
);
|
|
$this->form_validation->set_rules(
|
|
'to_added',
|
|
'To Added',
|
|
$date_pattern
|
|
);
|
|
}
|
|
|
|
public function load_pagination($all_record, $params, $action) {
|
|
$action_hidden = $this->input->get('action_hidden');
|
|
if ( isset( $params['category_id'] ) ) {
|
|
$category = $params['category_id'];
|
|
unset( $params['category_id'] );
|
|
$params['card_category'] = $category;
|
|
}
|
|
// pagination
|
|
$this->load->library('pagination');
|
|
$config["total_rows"] = count($all_record);
|
|
$config["base_url"] = base_url() . "/" . get_class($this) . "/" . $action;
|
|
$config["per_page"] = 10;
|
|
$config["uri_segment"] = 3;
|
|
$config["num_links"] = 5;
|
|
$config["suffix"] =
|
|
"?action_hidden=${action_hidden}&"
|
|
. http_build_query($params);
|
|
$config["first_url"] =
|
|
"/uploads/{$action}/0?action_hidden=${action_hidden}&"
|
|
. http_build_query($params);
|
|
$config['full_tag_open'] = "<ul class='pagination'>";
|
|
$config['full_tag_close'] = "</ul>";
|
|
$config['num_tag_open'] = '<li>';
|
|
$config['num_tag_close'] = '</li>';
|
|
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
|
|
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
|
|
$config['next_tag_open'] = "<li>";
|
|
$config['next_tagl_close'] = "</li>";
|
|
$config['prev_tag_open'] = "<li>";
|
|
$config['prev_tagl_close'] = "</li>";
|
|
$config['first_tag_open'] = "<li>";
|
|
$config['first_tagl_close'] = "</li>";
|
|
$config['last_tag_open'] = "<li>";
|
|
$config['last_tagl_close'] = "</li>";
|
|
|
|
$this->pagination->initialize($config);
|
|
$page = ( $this->uri->segment(3) ) ? $this->uri->segment(3) : 0;
|
|
$offset = is_numeric($page) ? $page : 0;
|
|
|
|
return [
|
|
'link' => $this->pagination->create_links(),
|
|
'offset' => $offset,
|
|
'limit' => $config["per_page"]
|
|
];
|
|
}
|
|
|
|
public function cardimages() {
|
|
global $savvyext;
|
|
|
|
$this->load->model('card_image_model');
|
|
|
|
$data['storage'] = $savvyext->cfgReadChar('system.storage_url');
|
|
$data['message'] = "";
|
|
|
|
if ($this->input->post()) {
|
|
$data = $this->cardimagesPost($data);
|
|
}
|
|
|
|
$params = $this->getValueOfCardImages();
|
|
$data = array_merge($data, $this->setComboForCardImages($params));
|
|
|
|
$errors = $this->validateValueForCardImages($params);
|
|
$params = array_filter($params, function($ele) {
|
|
return $ele !== "";
|
|
});
|
|
$params = array_diff_key($params, $errors);
|
|
$data = array_merge(
|
|
$data,
|
|
$params,
|
|
$this->load_pagination(
|
|
$this->card_image_model->get_card_image_records($params),
|
|
$params,
|
|
'cardimages'
|
|
)
|
|
);
|
|
$data['images'] =
|
|
array_map(
|
|
function($ele) {
|
|
|
|
$ele['file_size'] = $this->human_filesize($ele['file_size']);
|
|
return $ele;
|
|
|
|
},
|
|
$this->card_image_model->get_card_image_records(
|
|
$params,
|
|
$data['limit'],
|
|
$data['offset']
|
|
)
|
|
);
|
|
// add filter after update card images
|
|
$filter = $this->add_filter_after_submit();
|
|
if ( ($_SERVER['REQUEST_METHOD'] === 'POST') ) {
|
|
redirect( sprintf( '/uploads/cardimages?%s', $filter ) );
|
|
}
|
|
$this->renderUploadPage( 'cardimages', $data);
|
|
}
|
|
|
|
private function add_filter_after_submit( $http_query = '' ) {
|
|
$history = filter_var( $_SERVER['HTTP_REFERER'], FILTER_SANITIZE_URL );
|
|
$history = parse_url( $history );
|
|
|
|
if ( isset( $history['query'] ) ) {
|
|
parse_str( $history['query'], $query );
|
|
$http_query = http_build_query( $query );
|
|
}
|
|
|
|
return $http_query;
|
|
}
|
|
|
|
private function human_filesize($bytes, $decimals = 2) {
|
|
$sz = 'BKMGTP';
|
|
$factor = floor((strlen($bytes) - 1) / 3);
|
|
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
|
|
}
|
|
|
|
public function uploadInterface($data) {
|
|
return $this->cardimagesPost($data);
|
|
}
|
|
|
|
private function cardimagesPost($data) {
|
|
global $savvyext;
|
|
$bucket_name = '';
|
|
$catid = (int)$this->input->post('catid');
|
|
if ($catid<1) { $data['message'] = 'Please select category'; return $data; }
|
|
$q = "SELECT name FROM card_image_category WHERE id='${catid}'";
|
|
$r = $this->read_replica->query($q);
|
|
foreach ($r->result() as $row) {
|
|
$bucket_name = $row->name;
|
|
break;
|
|
}
|
|
if ($bucket_name=='') {
|
|
$data['message'] = 'Invalid category';
|
|
return $data;
|
|
}
|
|
// Process file
|
|
$imgData = $this->processFile('cardimg', array('jpg'=>1,'jpeg'=>1,'png'=>1,'gif'=>1));
|
|
if ($imgData==NULL || !is_array($imgData)) {
|
|
$data['message'] = 'Failed to process uploaded image! ('.$imgData.')';
|
|
return $data;
|
|
}
|
|
$this->load->library('googlestorage');
|
|
$this->googlestorage->Init(
|
|
$savvyext->cfgReadChar('google.storage_project_id'),
|
|
$savvyext->cfgReadChar('google.storage_auth_file')
|
|
);
|
|
$storage = $this->googlestorage;
|
|
// Get bucket
|
|
$buckets = $storage->ListBuckets();
|
|
if (array_key_exists($bucket_name, $buckets)) {
|
|
$bucket = $buckets[$bucket_name];
|
|
} else {
|
|
// Create bucket
|
|
$bucket = $storage->CreateBucket($bucket_name);
|
|
}
|
|
if ($bucket==NULL || !is_object($bucket)) {
|
|
$data['message'] = 'Cannot get or create bucket!';
|
|
return $data;
|
|
}
|
|
|
|
$total = count($imgData['name']);
|
|
|
|
// Loop through each file
|
|
for ($i = 0 ; $i < $total; $i++) {
|
|
$uniqueId = $storage->UniqueId();
|
|
$objectName = $uniqueId.".".$imgData['format'][$i];
|
|
$source = $imgData['tmp_name'][$i];
|
|
$object = $storage->UploadObject($bucket_name, $objectName, $source);
|
|
if ($object==NULL || !is_object($object)) {
|
|
$data['message'] = 'Failed to upload image!' . $imgData['name'][$i];
|
|
break;
|
|
} else {
|
|
$data['message'] = 'Image uploaded!';
|
|
// Save image data
|
|
$q = "INSERT INTO card_images (catid, uniqueid, name, file_size, format, dimensions) VALUES(";
|
|
$q.= "'${catid}','${uniqueId}','".$imgData['name'][$i]."','".$imgData['file_size'][$i]."','".$imgData['format'][$i]."','".$imgData['dimensions'][$i]."') RETURNING id";
|
|
//error_log($q);
|
|
$r = $this->db->query($q);
|
|
$f = $r->row_array();
|
|
$data["card_image_id"][$i] = $f["id"];
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function processFile($var, $formats) {
|
|
if (!isset($_FILES[$var])) return 'The file was not uploaded!';
|
|
if (is_array($_FILES[$var]['error'])) {
|
|
foreach ($_FILES[$var]['error'] as $key => $error) {
|
|
if ($error != UPLOAD_ERR_OK) {
|
|
return $error;
|
|
}
|
|
}
|
|
} else if (isset($_FILES[$var]['error']) && $_FILES[$var]['error'] != UPLOAD_ERR_OK) {
|
|
return $_FILES[$var]['error'];
|
|
}
|
|
$error = $_FILES[$var]['error'];
|
|
$name = $_FILES[$var]['name'];
|
|
$file_size = $_FILES[$var]['size'];
|
|
$tmp_name = $_FILES[$var]['tmp_name'];
|
|
|
|
// Count # of uploaded files in array
|
|
$total = count($_FILES[$var]['name']);
|
|
$result_formats = [];
|
|
$result_dimensions = [];
|
|
|
|
// Loop through each file
|
|
for ( $i=0 ; $i < $total ; $i++ ) {
|
|
$format = strtolower(pathinfo($_FILES[$var]['name'][$i], PATHINFO_EXTENSION));
|
|
|
|
if (!isset($formats[$format]) || $formats[$format]!=1) {
|
|
return 'Invalid file format: '.$format;
|
|
}
|
|
|
|
list($width, $height, $type, $attr) = getimagesize($tmp_name[$i]);
|
|
$dimensions = $width . 'x' . $height;
|
|
|
|
array_push($result_formats, $format);
|
|
array_push($result_dimensions, $dimensions);
|
|
}
|
|
|
|
$result = array(
|
|
'tmp_name' => $tmp_name,
|
|
'name' => $name,
|
|
'file_size' => $file_size,
|
|
'format' => $result_formats,
|
|
'dimensions' => $result_dimensions
|
|
);
|
|
|
|
return $result;
|
|
}
|
|
}
|