File: /home/dmstechonline/crm.chaitanyahospitalvirar.com/application/models/Systemnotification_model.php
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Systemnotification_model extends MY_Model
{
public $current_session;
public function __construct()
{
parent::__construct();
}
/**
* This funtion takes id as a parameter and will fetch the record.
* If id is not provided, then it will fetch all the records form the table.
* @param int $id
* @return mixed
*/
public function get($id = null)
{
$userdata = $this->customlib->getUserData();
$role_id = $userdata["role_id"];
$sql = "SELECT * from send_notification JOIN (SELECT send_notification_id, GROUP_CONCAT(role_id) as roles FROM notification_roles group by send_notification_id) as notification_roles on notification_roles.send_notification_id = send_notification.id ";
if ($id != null) {
$sql .= "where send_notification.id =" . $id;
}
$query = $this->db->query($sql);
if ($id != null) {
return $query->row_array();
} else {
return $query->result_array();
}
}
public function getRole($arr)
{
$query = $this->db->where_in("id", $arr)->get("roles");
return $query->result_array();
}
public function getUnreadStaffNotification($staffid = null, $role_id = null)
{
$sql = "select send_notification.* from send_notification INNER JOIN notification_roles on notification_roles.send_notification_id = send_notification.id left JOIN read_notification on read_notification.staff_id=" . $this->db->escape($staffid) . " and read_notification.notification_id = send_notification.id WHERE send_notification.created_id !=" . $this->db->escape($staffid) . " and send_notification.visible_staff='yes' and read_notification.id IS NULL and notification_roles.role_id=" . $this->db->escape($role_id) . " order by send_notification.id desc";
$query = $this->db->query($sql);
return $query->result();
}
/**
* This function will delete the record based on the id
* @param $id
*/
public function remove($id)
{
$this->db->trans_start(); # Starting Transaction
$this->db->trans_strict(false); # See Note 01. If you wish can remove as well
//=======================Code Start===========================
$this->db->where('id', $id);
$this->db->delete('send_notification');
$message = DELETE_RECORD_CONSTANT . " On Send Notification id " . $id;
$action = "Delete";
$record_id = $id;
$this->log($message, $record_id, $action);
//======================Code End==============================
$this->db->trans_complete(); # Completing transaction
/* Optional */
if ($this->db->trans_status() === false) {
# Something went wrong.
$this->db->trans_rollback();
return false;
} else {
return $record_id;
}
}
/**
* This function will take the post data passed from the controller
* If id is present, then it will do an update
* else an insert. One function doing both add and edit.
* @param $data
*/
public function add($data)
{
$this->db->trans_start(); # Starting Transaction
$this->db->trans_strict(false); # See Note 01. If you wish can remove as well
//=======================Code Start===========================
if (isset($data['id'])) {
$this->db->where('id', $data['id']);
$this->db->update('send_notification', $data);
$message = UPDATE_RECORD_CONSTANT . " On Send Notification id " . $data['id'];
$action = "Update";
$record_id = $data['id'];
$this->log($message, $record_id, $action);
} else {
$this->db->insert('send_notification', $data);
$insert_id = $this->db->insert_id();
$message = INSERT_RECORD_CONSTANT . " On Send Notification id " . $insert_id;
$action = "Insert";
$record_id = $insert_id;
$this->log($message, $record_id, $action);
}
//======================Code End==============================
$this->db->trans_complete(); # Completing transaction
/* Optional */
if ($this->db->trans_status() === false) {
# Something went wrong.
$this->db->trans_rollback();
return false;
} else {
return $record_id;
}
}
public function insertBatch($data, $staff_roles, $delete_array = array())
{
$this->db->trans_start();
$this->db->trans_strict(false);
if (isset($data['id'])) {
$insert_id = $data['id'];
$this->db->where('id', $data['id']);
$this->db->update('send_notification', $data);
} else {
$this->db->insert('send_notification', $data);
$insert_id = $this->db->insert_id();
}
if (!empty($staff_roles)) {
foreach ($staff_roles as $stf_role_key => $stf_role_value) {
$staff_roles[$stf_role_key]['send_notification_id'] = $insert_id;
}
$this->db->insert_batch('notification_roles', $staff_roles);
}
if (!empty($delete_array)) {
$this->db->where('send_notification_id', $insert_id);
$this->db->where_in('role_id', $delete_array);
$this->db->delete('notification_roles');
}
$this->db->trans_complete();
if ($this->db->trans_status() === false) {
$this->db->trans_rollback();
return false;
} else {
$this->db->trans_commit();
return true;
}
}
}