Current File : /home/giriqfky/public_html/Barga/application/modules/notifications/models//Notifications_model.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Notifications_model extends CI_Model
{
var $table = TABLE_NOTIFICATIONS;
var $table_app_users = TABLE_APP_USERS;
var $table_app_registrations = TABLE_APP_REGISTRATIONS;
var $table_modules = TABLE_MODULES;
public function __construct()
{
parent::__construct();
}
public function getAppUsers($app_user_type, $user_mobile, $user_profile_name)
{
$this->db->select('au.app_user_id,au.package_id,au.app_user_type,au.service_provider_type,au.user_mobile,au.user_email,au.user_profile_name,au.profile_image');
$this->db->from($this->table_app_users. ' au');
if(!empty($app_user_type)) $this->db->where('au.app_user_type', $app_user_type);
if(!empty($user_mobile)) $this->db->where('au.user_mobile', $user_mobile);
if(!empty($user_profile_name)) $this->db->like('au.user_profile_name', $user_profile_name);
$this->db->order_by('au.user_profile_name', 'ASC');
return $this->db->get()->result();
}
public function saveNotification($module_id = 0, $app_user_ids =0, $notify_message='', $notify_message_title='', $push_notify_img='', $notify_type = 1, $notify_alert_type = 1,$additional_data = array())
{
$notification_ids = array();
if(!is_array($app_user_ids)) $app_user_ids = explode(",", $app_user_ids);
if(is_array($app_user_ids) && count($app_user_ids) > 0) {
foreach($app_user_ids as $app_user_id) {
$data = array(
'module_id' => $module_id,
'app_user_id' => $app_user_id,
'notification_title' => $notify_message_title,
'notification_message' => $notify_message,
'push_image_url' => $push_notify_img,
'doc' => date('Y-m-d H:i:s'),
'notification_type' => (int)$notify_type,
'notification_alert_type' => (int)$notify_alert_type,
'additional_data' => !empty($additional_data) ? json_encode($additional_data) : ""
);
$this->db->insert($this->table, $data);
$notification_ids[] = $this->db->insert_id();
}
}
return $notification_ids;
}
public function getMobileNumbers($app_user_ids)
{
$mobile_numbers = array();
$this->db->select('user_mobile');
$this->db->where_in('app_user_id', $app_user_ids);
$app_users = $this->db->get($this->table_app_users)->result();
foreach($app_users as $au) {
$mobile_numbers[] = $au->user_mobile;
}
return $mobile_numbers;
}
public function saveSingleNotification($member_id, $notify_message, $image_url, $notify_type = 1, $notify_alert_type = 1,$additional_data = array())
{
$data = array('member_id' => $member_id, 'notification_message' => $notify_message, 'push_image_url' => $image_url, 'doc' => date('Y-m-d H:i:s'), 'notification_type' => (int)$notify_type, 'notification_alert_type' => (int)$notify_alert_type);
if(!empty($additional_data))
$data['additional_data'] = json_encode($additional_data);
$this->db->insert($this->table, $data);
$notification_id = $this->db->insert_id();
return $notification_id;
}
public function getAppUserEmails($app_user_ids)
{
$app_user_emails = array();
$this->db->select('user_email');
$this->db->where('user_status', 1);
$this->db->where_in('app_user_id', $app_user_ids);
$user_data = $this->db->get($this->table_app_users)->result();
foreach($user_data as $ud) {
$app_user_emails[] = $ud->user_email;
}
return $app_user_emails;
}
public function getDeviceToken($device_type, $app_user_ids)
{
$device_tokens = array();
$this->db->select('device_token');
$this->db->where('device_type', $device_type);
$this->db->where('device_status', 1);
$this->db->where_in('app_user_id', $app_user_ids);
$device_data = $this->db->get($this->table_app_registrations)->result();
foreach($device_data as $dd) {
$device_tokens[] = $dd->device_token;
}
return $device_tokens;
}
public function getNotificationList($params = array())
{
$this->db->select('n.*, au.user_profile_name,au.app_user_type,md.module_name');
$this->db->from($this->table. " n");
$this->db->join($this->table_app_users. ' au', 'au.app_user_id = n.app_user_id');
$this->db->join($this->table_modules. ' md', 'md.module_id = n.module_id', 'LEFT');
if(!empty($params['search']['notification_message'])){
$this->db->like('n.notification_message',$params['search']['notification_message']);
}
if(!empty($params['search']['notification_alert_type'])) {
$this->db->where('n.notification_alert_type',$params['search']['notification_alert_type']);
}
if(!empty($params['search']['app_user_type'])) {
$this->db->where('au.app_user_type',$params['search']['app_user_type']);
}
if(!empty($params['search']['date_from'])) {
$date_from = getDateFromDatepicker($params['search']['date_from']);
$this->db->where("n.doc >= '".date($date_from)."'");
}
if(!empty($params['search']['date_to'])) {
$date_to = getDateFromDatepicker($params['search']['date_to']);
$this->db->where("n.doc <= '".date($date_to)."23:59:59'");
}
$this->db->where('n.notification_id > ', 0);
$this->db->order_by('n.notification_id','DESC');
//set start and limit
if(array_key_exists("start",$params) && array_key_exists("limit",$params)) {
$this->db->limit($params['limit'],$params['start']);
} elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)) {
$this->db->limit($params['limit']);
}
return $this->db->get()->result();
}
public function saveBulkNotification($data = array())
{
$notification_ids = array();
if(!empty($data)){
$this->db->insert($this->table, $data);
$notification_ids[] = $this->db->insert_id();
//debug($this->db->last_query());
}
return $notification_ids;
}
public function getCronNotification($select = "*",$where = array())
{
if(!empty($where)){
$this->db->select($select);
$this->db->from($this->table. " n");
$this->db->where($where);
$this->db->where('n.doc >=',date('Y-m-d 00:00:00'));
$this->db->where('n.doc <=',date('Y-m-d 23:59:59'));
$this->db->limit(10);
return $this->db->get()->result();
}
}
public function updateNotification($where = array(),$data= array())
{
$this->db->where($where);
$this->db->update(TABLE_NOTIFICATIONS,$data);
}
public function getNotificationsinbox($select = "*",$where = array(),$limit = array())
{
$this->db->select($select);
$this->db->from(TABLE_NOTIFICATIONS.' n');
if(!empty($where))
$this->db->where($where);
if(!empty($limit)) $this->db->limit($limit['per_page'], $limit['limit_start']);
$this->db->order_by('n.doc','DESC');
$res = $this->db->get();
return $res->result();
}
public function removenotification($where = array())
{
if(!empty($where)){
$this->db->where($where);
$this->db->delete(TABLE_NOTIFICATIONS);
}
}
}