You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

462 lines
19 KiB

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Event_file_attachment_model extends CI_Model {
var $column_order = array(null, 'attachment_type'); //set column field database for datatable orderable
var $column_search = array('description'); //set column field database for datatable searchable just firstname , lastname , address are searchable
var $order = array('attachment_type' => 'asc'); // default order
public function __construct() {
parent::__construct();
}
public function disableOlderFileAttachment ($event_id, $mime_type, $attachment_type) {
$max_num_event_file_attachments = 2;
// Eg. image
$file_type = explode('/', $mime_type)[0];
$existing_file_attachments = $this->db->where([
"event_id" => $event_id,
"attachment_type" => $attachment_type,
"status" => '1',
])
->like('mime_type', $file_type)
->order_by('date_added', 'DESC')
->get('event_file_attachment');
$last_active_file_attachment = $existing_file_attachments->num_rows() > 0
? $existing_file_attachments->row()
: null;
// Exit the function if existing_file_attachments has not reached the max
if ($existing_file_attachments->num_rows() < $max_num_event_file_attachments) return $last_active_file_attachment;
// Get the oldest active file attachment with same event_id, attachment_type, and mime_type
$oldest_active_file_attachment = $this->db->where([
"event_id" => $event_id,
"attachment_type" => $attachment_type,
"status" => '1',
])
->like('mime_type', $file_type)
->order_by('date_added', 'ASC')
->get('event_file_attachment')->row();
// Disable status
$this->db->set('status', '0')
->where(['attachment_id' => $oldest_active_file_attachment->id])
->update('event_file_attachment');
return $last_active_file_attachment;
}
/*private function _get_datatables_query($data_source, $event_id){
$this->db->select("
attachment_id,
event_id,
description,
file_name,
file_size,
mime_type,
attachment_type")
->select("(CASE
WHEN added_by IS NULL THEN 'Unknown'
ELSE (SELECT CONCAT(first_name,' ',last_name) as added_by FROM user WHERE user_id = added_by)
END) AS added_by", FALSE)
// ->select("(SELECT CONCAT(efas.setup_id,',',efas.autoplay_vaudio,',', efas.preview_default_image) as file_setup
// FROM event_file_attachment_setup efas
// WHERE efas.attachment_id = attachment_id
// ) AS file_setup", FALSE)
// ->select("(SELECT
// GROUP_CONCAT(CONCAT('{\"setup_id\":\"',efas.setup_id,
// '\", \"autoplay_vaudio\":\"',efas.autoplay_vaudio,
// '\", \"preview_default_image\":\"',efas.preview_default_image,'\"}'
// ) ORDER BY efas.attachment_id SEPARATOR ',') as file_setup
// FROM event_file_attachment_setup efas
// WHERE efas.attachment_id = attachment_id
// ) AS file_setup", FALSE)
->select("DATE_FORMAT(date_added, '%d/%m/%Y %Hh%i') AS date_added", FALSE)
->from("event_file_attachment")
->where("event_id", $event_id)
// ->where("status IN(1,2)");
->where("status", 1);
$i = 0;
foreach ($this->column_search as $item) {// loop column
if ($data_source['search']['value']) { // if datatable send POST for search
if ($i === 0) { // first loop
$this->db->like($item, $data_source['search']['value']);
} else {
$this->db->or_like($item, $data_source['search']['value']);
}
}
$i++;
}
if(isset($data_source['order'])) { // here order processing
if(isset($data_source['order']['0']['dir']) && !empty($data_source['order']['0']['dir'])) {
$this->db->order_by($this->column_order[$data_source['order']['0']['column']], $data_source['order']['0']['dir']);
}
} else if(isset($this->order)) {
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
public function get_datatables($data_source, $event_id){
$this->_get_datatables_query($data_source, $event_id);
if($data_source['length'] != -1)
$this->db->limit($data_source['length'], $data_source['start']);
$query = $this->db->get();
return $query->result();
}
public function count_filtered($data_source, $event_id){
$this->_get_datatables_query($data_source, $event_id);
$query = $this->db->get();
return $query->num_rows();
}
public function count_all($data_source, $event_id){
$this->_get_datatables_query($data_source, $event_id);
return $this->db->count_all_results();
}
public function delete_event_attachment($attachment_id=0){
if($attachment_id){
$this->db->where("attachment_id", $attachment_id);
$this->db->update("event_file_attachment", array("status" => 3));
return $this->db->affected_rows();
} else {
return false;
}
}
*/
public function _reset_attachment($event_id){
$this->db->where("status" , 2);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 0));
//set deleted image( status = 3) to 0 status
$this->update_event_attachment_status("event_id",$event_id,"event_file_attachment",["status" => 0], "status", 3);
}
public function attachment_type_action($event_id){
//update new attachments
$this->event_file_attachment_model->update_attachment_status_by_type($event_id);
//set deleted image( status = 3) to 0 status
$this->update_event_attachment_status("event_id",$event_id,"event_file_attachment",["status" => 0], "status", 3);
}
public function _exclude_from_update($event_id,$attachment_type,$mime_type){
$result = $this->db->query("
SELECT attachment_id
FROM event_file_attachment
WHERE event_id = ?
AND attachment_type = ?
AND INSTR(`mime_type`, ?) > 0
ORDER BY attachment_id DESC
LIMIT 1
", array($event_id, $attachment_type, $mime_type));
if ($result->num_rows() > 0) {
$ret = $result->row();
return $ret->attachment_id;
}
}
public function update_attachment_status_by_type($event_id){
//update attachment 1
if( $this->event_file_attachment_model->get_attachment_status_by_type($event_id, 1,'image') ){
$this->db->where("attachment_type",1);
$this->db->where("INSTR(`mime_type`, 'image') > 0");
$this->db->where("status" , 2);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 1));
$this->db->where("attachment_id !=",$this->_exclude_from_update($event_id,1,'image'));
$this->db->where("attachment_type",1);
$this->db->where("INSTR(`mime_type`, 'image') > 0");
$this->db->where("status" , 1);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 3));
}
//update attachment 2 = image
if( $this->event_file_attachment_model->get_attachment_status_by_type($event_id, 2,'image') ){
$this->db->where("attachment_type",2);
$this->db->where("INSTR(`mime_type`, 'image') > 0");
$this->db->where("status" , 2);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 1));
$this->db->where("attachment_id !=",$this->_exclude_from_update($event_id,2,'image'));
$this->db->where("attachment_type",2);
$this->db->where("INSTR(`mime_type`, 'image') > 0");
$this->db->where("status" , 1);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 3));
}
//update attachment 2 = audio
if( $this->event_file_attachment_model->get_attachment_status_by_type($event_id, 2, 'audio') ){
$this->db->where("attachment_type",2);
$this->db->where("INSTR(`mime_type`, 'audio') > 0");
$this->db->where("status" , 2);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 1));
$this->db->where("attachment_id !=",$this->_exclude_from_update($event_id,2,'audio'));
$this->db->where("attachment_type",2);
$this->db->where("INSTR(`mime_type`, 'audio') > 0");
$this->db->where("status" , 1);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 3));
}
//update attachment 2 = video
if( $this->event_file_attachment_model->get_attachment_status_by_type($event_id, 2, 'video') ){
$this->db->where("attachment_type",2);
$this->db->where("INSTR(`mime_type`, 'video') > 0");
$this->db->where("status" , 2);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 1));
$this->db->where("attachment_id !=",$this->_exclude_from_update($event_id,2,'video'));
$this->db->where("attachment_type",2);
$this->db->where("INSTR(`mime_type`, 'video') > 0");
$this->db->where("status" , 1);
$this->db->where("event_id", $event_id);
$this->db->update("event_file_attachment", array("status" => 3));
}
}
public function get_attachment_status_by_type($event_id,$attachment_type,$mime_type){
$and = "";
if($mime_type != false) $and ="AND INSTR(`mime_type`, '".$mime_type."') > 0 ";
$result = $this->db->query("
SELECT attachment_id,status
FROM event_file_attachment
WHERE event_id = ?
AND status = 1
AND attachment_type = ?
$and ", array($event_id, $attachment_type));
if ($result->num_rows() > 0) return true;
else return false;
}
public function get_status_type($event_id, $event_file_attachment,$status,$attachment_type,$mime_type){
$and = "";
if($mime_type != false) $and ="AND INSTR(`mime_type`, '".$mime_type."') > 0 ";
$result = $this->db->query("
SELECT status
FROM $event_file_attachment
WHERE event_id = ?
AND status = ?
AND attachment_type = ?
$and ", array($event_id, $status, $attachment_type));
return ( ($result->num_rows() > 0) ? 2 : 1 );
}
private function _get_datatables_query($data_source,$event_id){
$fields = "attachment_id,
event_id,
description,
display,
file_name,
file_size,
mime_type,
attachment_type,
status,
(CASE
WHEN added_by IS NULL THEN 'Unknown'
ELSE (SELECT CONCAT(first_name,' ',last_name) as added_by FROM user WHERE user_id = added_by)
END) AS added_by,
DATE_FORMAT(date_added, '%d/%m/%Y %Hh%i') AS date_added";
$_query = ""; $_search = ""; $_ordey_by = ""; $data = array(); $select = "";
$_query = "
SELECT ".$fields."
FROM event_file_attachment
WHERE event_id = ".$this->db->escape($event_id)."
AND attachment_type = 1
AND status =". $this->db->escape($this->get_status_type($event_id, "event_file_attachment" , 2,1,false))."
UNION
SELECT ".$fields."
FROM event_file_attachment
WHERE event_id = ".$this->db->escape($event_id)."
AND attachment_type = 2
AND INSTR(`mime_type`, 'image') > 0
AND status =". $this->db->escape($this->get_status_type($event_id, "event_file_attachment" , 2,2,'image'))."
UNION
SELECT ".$fields."
FROM event_file_attachment
WHERE event_id = ".$this->db->escape($event_id)."
AND attachment_type = 2
AND INSTR(`mime_type`, 'audio') > 0
AND status =". $this->db->escape($this->get_status_type($event_id, "event_file_attachment" , 2,2,'audio'))."
UNION
SELECT ".$fields."
FROM event_file_attachment
WHERE event_id = ".$this->db->escape($event_id)."
AND attachment_type = 2
AND INSTR(`mime_type`, 'video') > 0
AND status =". $this->db->escape($this->get_status_type($event_id, "event_file_attachment" , 2,2,'video'))."
";
$_ordey_by .= " ORDER BY ".key($this->order)." ".$this->order[key($this->order)];
return array("query"=>$_query.$_search.$_ordey_by, "data" =>$data);
}
public function get_datatables($data_source,$event_id){
$_query = $this->_get_datatables_query($data_source,$event_id);
if($data_source['length'] != -1){
$_query["query"] .= " LIMIT ".$data_source['start'].", ".$data_source['length'];
}
return $this->db->query($_query["query"], $_query["data"])->result();
}
public function count_filtered($data_source,$event_id){
$_query = $this->_get_datatables_query($data_source,$event_id);
return $this->db->query($_query["query"], $_query["data"])->num_rows();
}
public function count_all($data_source,$event_id){
$_query = $this->_get_datatables_query($data_source,$event_id);
return $this->db->query($_query["query"], $_query["data"])->num_rows();
}
public function update_event_attachment_status($field_id,$id,$table,$data,$field_status,$status_where){
$this->db->where($field_id, $id);
$this->db->where($field_status, $status_where);
$this->db->update($table, $data);
return $this->db->affected_rows();
}
public function disable_other_attachments($event_id, $new_file_mime, $attachment_type, $action = 'add'){
if ($action == 'add') {
if (preg_match("[audio]", $new_file_mime)) {
$this->db->not_like('mime_type ', "image");
} elseif (preg_match("[image]", $new_file_mime)) {
$this->db->not_like('mime_type ', "audio");
}
} else {
if (preg_match("[audio]", $new_file_mime)) {
$this->db->not_like('mime_type ', "image");
} elseif (preg_match("[image]", $new_file_mime)) {
$this->db->not_like('mime_type ', "audio");
}
}
$this->db->where('event_id', $event_id);
$this->db->where('attachment_type', $attachment_type);
$this->db->update(
'event_file_attachment',
array(
'status' => 0
)
);
return $this->db->affected_rows();
}
public function get_events_list_export($data_source,$event_id){
$this->_get_datatables_query($data_source, $event_id);
$query = $this->db->get();
$res = $query->result();
return $res;
}
public function add_event_attachment($data, $audio_video=array()){
$this->db->insert("event_file_attachment", $data);
$attachment_id = $this->db->insert_id();
if( $attachment_id ){
//if there is audio or video setup, add an entry to table: event_file_attachment_setup
if(sizeof($audio_video) > 0){
$audio_video["attachment_id"] = $attachment_id;
$this->db->insert("event_file_attachment_setup", $audio_video);
return $this->db->insert_id();
}
return $attachment_id;
}
return false;
}
public function update_event_attachment($attachment_id=0, $data=array(), $audio_video=array()){
if($attachment_id && sizeof($data) > 0){
$this->db->where("attachment_id", $attachment_id);
$this->db->update("event_file_attachment", $data);
$updated = $this->db->affected_rows();
//we need to update the file attachment setup
$this->update_file_attachment_setup($attachment_id, $audio_video);
return $updated;
} else {
return false;
}
}
private function update_file_attachment_setup($attachment_id, $audio_video =array()){
$this->db->select("*");
$this->db->where("attachment_id", $attachment_id);
$result = $this->db->get("event_file_attachment_setup");
if($result->num_rows() > 0) {
if( sizeof($audio_video) >0 ) {
$this->db->where("attachment_id", $attachment_id);
$this->db->update("event_file_attachment_setup", $audio_video);
} else {
//delete file attachment setup entry
$this->db->where("attachment_id", $attachment_id);
$this->db->delete("event_file_attachment_setup");
}
} else if( sizeof($audio_video) > 0 ) {
$audio_video["attachment_id"] = $attachment_id;
$this->db->insert("event_file_attachment_setup", $audio_video);
}
}
public function delete_event_attachment($event_id,$attachment_type,$mime_type,$attachment_id){
if($event_id && $attachment_type && $mime_type){
$this->db->where("attachment_id", $attachment_id);
/*$this->db->where("event_id", $event_id);
$this->db->where("attachment_type", $attachment_type);
$this->db->where("INSTR(`mime_type`, '".$mime_type."') > 0");*/
$this->db->update("event_file_attachment", array("status" => 0));
return $this->db->affected_rows();
} else {
return false;
}
}
public function list_event_attachment($event_id=0, $attachment_type){
if($event_id){
return ($this->db->query("SELECT
efa.attachment_id, efa.file_name, efa.mime_type,
efa.description, efa.attachment_type
FROM event_file_attachment efa
-- LEFT JOIN event_file_attachment_setup efas ON efas.attachment_id = efa.attachment_id
WHERE efa.event_id = ?
AND efa.attachment_type = ?
-- AND efa.status NOT IN(0)
AND efa.status NOT IN(0,2)
ORDER BY efa.mime_type ASC", array($event_id, $attachment_type)))->result();
}
return array();
}
}