File: //var/www/html/pmw24/app/application/controllers/admin/Jobnotifictaion.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Jobnotifictaion extends MY_Controller {
public function __construct() {
parent::__construct();
$this->redirect_guest();
$this->admin=$this->session->userdata('admin');
$this->load->model('admin/mjobnotification');
}
public function index() {
$this->_load_list_view();
}
private function _load_list_view() {
$data['admin'] = $this->admin;
$data['content'] = 'admin/jobnotifictaion/list';
$this->load->view('admin/layouts/index', $data);
}
public function all_content_list(){
$list = $this->mjobnotification->get_datatables();
$data = array();
$no = $_POST['start'];
$i=1;
foreach ($list as $person) {
$row = array();
$row[]=$i;
$condition = array("job_id"=>$person->job_id,"assign_status"=>1);
$assign_job = $this->mjobnotification->getRow('assign_job',$condition);
if(!empty($assign_job))
{
$mechanic_condition = array("admin_id"=>$assign_job['mechanic_id']);
$mechanic = $this->mjobnotification->getRow('admins',$mechanic_condition);
if(!empty($mechanic)){
$row[] = $person->car_no." (".$mechanic['first_name']." ".$mechanic['last_name'].")";
}
}else{
$row[] = $person->car_no;
}
$row[] = $person->service;
$row[] = $person->service_date;
if($person->job_status == 0){
$job_status = "Not Started";
}else if($person->job_status == 1){
$job_status = "In Progress";
}else if($person->job_status == 2){
$job_status = "Completed";
}
$row[] = $job_status;
$condition = array("job_id"=>$person->job_id);
$assign_job = $this->mjobnotification->getRowA("assign_job",$condition);
//$row[] = $assign_job['assign_status'];
if($assign_job['assign_status'] != ""){
if($assign_job['assign_status'] == 1)
{
$row[] = '<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Accepted
</div>';
}else if($assign_job['assign_status'] == 0){
$row[] = '<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Rejected
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="'.base_url().'admin/job/rejectlist/'.$person->job_id.'">RejectList</a></li>
<li><a href="'.base_url().'admin/job/reassign/'.$person->job_id.'">Re Assign</a></li>
</ul>
</div>';
}
}else{
$row[] = $assign_job['assign_status'];
}
$row[] = '<a class="cstm_view" id="view" style="padding-left:5px" href="javascript:void(0)" title="'.$person->job_id.'"><i class="glyphicon glyphicon-eye-open"></i></a>';
$admin = $this->admin;
$CI =& get_instance();
$CI->db->select('*');
$CI->db->from('role_menu');
$CI->db->join('menu', 'menu.menu_id = role_menu.menu_id', 'left');
$CI->db->where('role_id',$admin['role_id']);
$CI->db->where('parent_id',36);
$query=$CI->db->get();
$menus = $query->result_array();
$status_result = $this->searcharray('Status', menu_name, $menus);
$edit_result = $this->searcharray('Edit', menu_name, $menus);
if(!empty($status_result)){
$row[] = ($person->status==1?'<a class="cstm_view_status btn btn-success" id="active" href="javascript:void(0)" title="'.$person->job_id.'"><span class="glyphicon glyphicon-ok"></span></a>':'<a class="cstm_view_status btn btn-danger" id="inactive" href="javascript:void(0)" title="'.$person->job_id.'"><span class="glyphicon glyphicon-remove"></span></a>');
}
if(!empty($edit_result)){
$row[] = '<a href="'.base_url('admin/job/details/'.$person->job_id).'" title="Edit" class="btn btn-info"><span class="glyphicon glyphicon-pencil"></span></a></a>';
}
$data[] = $row;
$i++;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->mjobnotification->count_all(),
"recordsFiltered" => $this->mjobnotification->count_filtered(),
"data" => $data,
);
echo json_encode($output);
}
public function all_content_list_reject()
{
$job_id = $_POST['job_id'];
$list = $this->mjobnotification->get_datatables_reject($job_id);
$data = array();
$no = $_POST['start'];
$i=1;
foreach ($list as $person) {
$row = array();
$row[]=$i;
$row[] = $person->name;
$row[] = $person->remarks;
$data[] = $row;
$i++;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->mjobnotification->count_all_reject($job_id),
"recordsFiltered" => $this->mjobnotification->count_filtered_reject($job_id),
"data" => $data,
);
echo json_encode($output);
}
private function searcharray($value, $key, $array) {
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
return $val;
}
}
return null;
}
public function all_details(){
$job_id = $this->input->post('job_id');
$result = $this->mjobnotification->get_details($job_id);
$result['assign_job'] = $this->mjobnotification->getAsssignJob($job_id);
echo json_encode($result);
}
public function edit($job_id){
$data['cms']=$this->mjobnotification->get_details($job_id);
if(empty($data['cms'])){
$this->_load_list_view();
}else{
$this->_load_details_view($data);
}
}
public function reassign($job_id){
$mechanic_condition = array("role_id"=>14,"status"=>1);
$mechanics = $this->mjobnotification->getRows('admins',$mechanic_condition);
$rejected_mechanic_condition = array("job_id"=>$job_id);
$rejected_mechanics = $this->mjobnotification->getRows('assign_job',$rejected_mechanic_condition);
$new_rejected_mechanic = array();
foreach($rejected_mechanics as $key=>$rejected_mechanic)
{
$new_rejected_mechanic[] = $rejected_mechanic['mechanic_id'];
}
$new_mechanic = array();
foreach($mechanics as $mechanic)
{
$new_mechanic[] = $mechanic['admin_id'];
}
$reassign_mechanic_list = array_diff($new_mechanic,$new_rejected_mechanic);
$data['new_mechanic_list'] = array();
foreach($reassign_mechanic_list as $key_m=>$reassign_mechanic)
{
$reassign_mechanic_condition = array("admin_id"=>$reassign_mechanic);
$reassign_mechanic = $this->mjobnotification->getRow('admins',$reassign_mechanic_condition);
$data['new_mechanic_list'][$key_m] = $reassign_mechanic;
}
$data['job_id'] = $job_id;
$data['content']='admin/job/reassign';
$this->load->view('admin/layouts/index',$data);
}
public function savereassign()
{
$job_id = $this->input->post('job_id');
$data['job_id'] = $job_id;
$data['mechanic_id'] = $this->input->post('mechanic');
$data['assign_status'] = "";
$data['remarks'] = "";
$this->mjobnotification->insert('assign_job',$data);
$this->session->set_flashdata('success_msg','Job updated successfully');
redirect('admin/job/reassign/'.$job_id);
}
private function _load_details_view($parms){
$data['city_list'] = $this->mjob->get_city_list();
$data['parent_categories'] = $this->mjob->get_parent_category();
$data['sub_category'] = $this->mjob->get_sub_category_by_category_id($parms['cms']['cat_id']);
$data['parent_category_sub'] = $this->mjob->get_parent_category_by_parent_id($data['sub_category']['parent_id']);
$data['sub_categories'] = $this->mjob->get_sub_categories_by_parent_id($parms['cms']['cat_id']);
$data['client_list'] = $this->mjob->get_client_list();
$data['address_list'] = $this->mjob->get_client_address($parms['cms']['client_name']);
$data['phone_list'] = $this->mjob->get_client_phone($parms['cms']['client_name']);
$data['cms']=$parms['cms'];
$data['content'] = 'admin/job/detail';
$this->load->view('admin/layouts/index', $data);
}
public function update(){
if($this->input->post()){
$job_id = $this->input->post('job_id');
$this->form_validation->set_rules('job_date','Job Date','required');
$this->form_validation->set_rules('job_start_time','Job Start Time','required');
$this->form_validation->set_rules('category_id','Category','required');
$this->form_validation->set_rules('sub_category_id','Sub Category','required');
$this->form_validation->set_rules('client_name','Client Name','required');
$this->form_validation->set_rules('client_address','Client Address','required');
$this->form_validation->set_rules('job_end_time','Job End Time','required');
$this->form_validation->set_rules('client_name','Client Name','required');
$this->form_validation->set_rules('client_address','Client Address','required');
$this->form_validation->set_rules('city_id','Location','required');
if($this->form_validation->run()==FALSE){
$data['cms']=$this->mjob->get_details($job_id);
$this->_load_details_view($data);
}else{
$condition=array('job_id'=>$job_id);
$job_category_condition = array('category_id'=>$this->input->post('category_id'));
$job_category = $this->mjob->getRow('categories',$job_category_condition);
$job_sub_category_condition = array('category_id'=>$this->input->post('sub_category_id'));
$job_sub_category = $this->mjob->getRow('categories',$job_sub_category_condition);
$udata['job_title']= $job_category['category_name'];
$udata['job_description']= $job_category['category_name'];
$job_date = $this->input->post('job_date');
$date_array = explode('/',$job_date);
$newDate = $date_array[2]."-".$date_array[1]."-".$date_array[0];
$udata['job_date']=$newDate;
$udata['job_start_time']=$this->input->post('job_start_time');
$udata['job_end_time']=$this->input->post('job_end_time');
$udata['sub_cat_id'] = $this->input->post('sub_category_id');
$udata['cat_id'] = $this->input->post('category_id');
if($this->input->post('other_client_name') != '')
{
$udata['client_name'] = $this->input->post('other_client_name');
}else{
$udata['client_name'] = $this->input->post('client_name');
}
if($this->input->post('job_address') != '')
{
$udata['job_address'] = $this->input->post('job_address');
}else{
$udata['job_address'] = $this->input->post('client_address');
}
$client_phone_codition = array('client_name'=>$this->input->post('client_name'), 'client_address'=>$this->input->post('client_address'));
$client_phone = $this->mjob->getRow('client',$client_phone_codition);
$udata['client_phoneno'] = $client_phone['client_phone_no'];
$udata['type_of_work'] = $job_category['category_name'];
$udata['city_id'] = $this->input->post('city_id');
$udata['date_of_update'] = date('Y-m-d H:i:s');
//print_r($udata);exit;
$this->mjob->update($condition,$udata);
$push['notification_type'] = "job";
$push['job_category'] = $job_category['category_name'];
$push['job_sub_category'] = $job_sub_category['category_name'];
$push['job_date'] = $this->input->post('job_date');
$push['job_start_time'] = $this->input->post('job_start_time');
$push['job_end_time'] = $this->input->post('job_end_time');
$deviceList=$this->mjob->deviceList($this->input->post('category_id'),$this->input->post('city_id'));
if(!empty($deviceList)){
foreach($deviceList as $device){
if($device['device_type']==2 && $device['device_token'] != '' && $device['loggedin_status'] == 1){
$this->send_android_notification($device['device_token'],$push);
}else if($device['device_type']==1 && $device['device_token'] != '' && $device['loggedin_status'] == 1){
$this->send_ios_notification($device['device_token'],$push);
}
}
}
$this->session->set_flashdata('success_msg','Job updated successfully');
redirect('admin/job');
}
}else{
$this->_load_list_view();
}
}
public function add_content()
{
if($this->input->post()){
$this->form_validation->set_rules('car','Car','required');
$this->form_validation->set_rules('mechanic','Mechanic','required');
$this->form_validation->set_rules('service_date','Service Date','required');
if($this->form_validation->run()==FALSE){
$this->_load_add_view();
}else{
$service = $this->input->post('service');
$udata['car_id'] = $this->input->post('car');
$udata['service'] = implode(',',$service);
$udata['job_status'] = 0;
$udata['created_by'] = $this->admin['admin_id'];
$udata['service_date'] = $this->input->post('service_date');
$service_date = $this->input->post('service_date');
$next_date = date('d-m-Y', strtotime($service_date. ' + 45 days'));
$next_date_remider_before_seven = date('Y-m-d', strtotime($service_date. ' + 38 days'));
$next_service_date_before_three = date('Y-m-d', strtotime($service_date. ' + 42 days'));
$udata['next_service_date'] = $next_date;
$udata['next_service_date_reminder_before_seven'] = $next_date_remider_before_seven;
$udata['next_service_date_before_three'] = $next_service_date_before_three;
$udata['date_of_creation']=date('Y-m-d H:i:s');
$job_id = $this->mjob->add($udata);
if($job_id)
{
$data['job_id'] = $job_id;
$data['mechanic_id'] = $this->input->post('mechanic');
$data['assign_status'] = "";
$data['remarks'] = "";
$this->mjob->insert('assign_job',$data);
$car_condition = array("car_id"=>$this->input->post('car'));
$car = $this->mjob->getRow("car",$car_condition);
$service_condition = array("job_id"=>$job_id);
$job = $this->mjob->getRow("job",$service_condition);
$device_condition = array("user_id"=>$this->input->post('mechanic'));
$device = $this->mjob->getRow("devices",$device_condition);
$push['notification_type'] = "job";
$push['car'] = $car['car_no'];
$push['service'] = $job['service'];
$push['service_date'] = $this->input->post('service_date');
$this->send_android_notification($device['device_token'],$push);
$this->session->set_flashdata('success_msg','Job assign successfully');
redirect('admin/job/content');
}else{
$this->session->set_flashdata('error_msg','Something Went Wrong');
redirect('admin/job/content');
}
}
}else{
$this->_load_list_view();
}
}
private function image_upload(){
$img='imgInp';
if (!is_dir('./public/admin_assets/images/profilepics/')) {
mkdir('./public/admin_assets/images/profilepics/' ,0777, TRUE);
}
$config['upload_path'] = './public/admin_assets/images/profilepics/';
$config['allowed_types'] = 'gif|jpg|png';
//$config['min_width'] = '200';
//$config['min_height'] = '200';
//$config['max_size'] = '100';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($img)){
$message = array('result' => $this->upload->display_errors(),'status'=>0);
}else{
$data = array('upload_data' => $this->upload->data());
$message = array('result' => $data['upload_data']['file_name'],'status'=>1);
}
return $message;
}
public function add(){
$this->_load_add_view();
}
private function _load_add_view(){
$car_condition = array("status"=>1);
$data['cars'] = $this->mjob->getRows('car',$car_condition);
$mechanic_condition = array("role_id"=>14,"status"=>1);
$data['mechanics'] = $this->mjob->getRows('admins',$mechanic_condition);
$service_condition = array("status"=>1);
$data['services'] = $this->mjob->getRows('service',$service_condition);
$data['content']='admin/job/add';
$this->load->view('admin/layouts/index',$data);
}
public function getmechanicList()
{
$mechanic_condition = array("role_id"=>14,"status"=>1);
$mechanics = $this->mjob->getRows('admins',$mechanic_condition);
$date = $this->input->post('date');
$condition = array('available_date'=>$date);
$off_mechanics = $this->mjob->getRows('user_roaster',$condition);
$new_off_mechanic = array();
foreach($off_mechanics as $off_mechanic)
{
$new_off_mechanic[] = $off_mechanic['mechanic_id'];
}
$new_mechanic = array();
foreach($mechanics as $mechanic)
{
$new_mechanic[] = $mechanic['admin_id'];
}
$roasted_mechanic_list = array_diff($new_mechanic,$new_off_mechanic);
$result['new_mechanic_list'] = array();
foreach($roasted_mechanic_list as $key_m=>$roasted_mechanic)
{
$reassign_mechanic_condition = array("admin_id"=>$roasted_mechanic);
$reassign_mechanic = $this->mjob->getRow('admins',$reassign_mechanic_condition);
$result['new_mechanic_list'][$key_m] = $reassign_mechanic;
}
echo json_encode($result);
}
public function delete_content(){
$condition['job_id']=$this->input->post('job_id');
$this->mjob->delete($condition);
$response=array('status'=>1,'message'=>'Success');
echo header('Content-Type: application/json');
echo json_encode($response);
}
public function active()
{
$condition['user_id']=$this->input->post('user_id');
$udata['status'] = 1;
$this->musersetting->active($condition,$udata);
$response=array('status'=>1,'message'=>'Success');
echo json_encode($response);
}
public function inactive()
{
$condition['user_id']=$this->input->post('user_id');
$udata['status'] = 0;
$this->musersetting->active($condition,$udata);
$response=array('status'=>1,'message'=>'Success');
echo json_encode($response);
}
public function multiple_del()
{
$job_ids = explode(',',$this->input->post('job_ids'));
foreach($job_ids as $job_id)
{
$condition['job_id'] = $job_id;
$this->mjob->delete($condition);
$response=array('status'=>1,'message'=>'Success');
}
echo header('Content-Type: application/json');
echo json_encode($response);
}
public function get_sub_category()
{
$category_id = $this->input->post('category_id');
$result['sub_category'] = $this->musersetting->get_sub_category($category_id);
if(empty($result))
{
$result = array("category_name"=>"Select");
}
echo json_encode($result);
}
public function get_client_address()
{
$client_name = $this->input->post('client_name');
$result['client_addresss'] = $this->mjob->get_client_address($client_name);
echo json_encode($result);
}
public function client_job()
{
$client_name = $this->input->post('client_name');
$client_address = $this->input->post('client_address');
$result['job_list'] = $this->mjob->getJobInformationForclient($client_name,$client_address);
echo json_encode($result);
}
public function rejectlist($job_id)
{
$data['job_id'] = $job_id;
$data['content'] = 'admin/job/rejectlist';
$this->load->view('admin/layouts/index', $data);
}
public function send_special_notification()
{
$job_id = $this->input->post('job_id');
$comment = $this->input->post('comment');
$condition = array('job_id'=>$job_id);
$jobDetail = $this->mjob->getRow('job',$condition);
$category_id = $jobDetail['cat_id'];
$city_id = $jobDetail['city_id'];
$deviceList=$this->mjob->deviceList($category_id,$city_id);
$push['notification_type'] = "message";
$push['message'] = $comment;
if(!empty($deviceList)){
foreach($deviceList as $device){
if($device['device_type']==2 && $device['device_token'] != '' && $device['loggedin_status'] == 1){
$this->send_android_notification($device['device_token'],$push);
}else if($device['device_type']==1 && $device['device_token'] != '' && $device['loggedin_status'] == 1){
$this->send_ios_notification($device['device_token'],$push);
}
}
}
$result['notify_message'] = 'Notification Successfully Send';
echo json_encode($result);
}
public function send_ios_notification($devicetoken,$data){
$passphrase='123456';
$deviceToken = $devicetoken;
$ctx = stream_context_create();
// ck.pem is your certificate file ssl://gateway.sandbox.push.apple.com:2195
stream_context_set_option($ctx, 'ssl', 'local_cert', './public/WeCareStaffPem.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp){
//exit("Failed to connect: $err $errstr" . PHP_EOL);
return 2;
}else{
// Create the payload body
$body['aps'] = array(
'alert' => array(
'title' => 'Wecare App',
'body' => 'Wecare App',
),
'sound' => 'default',
'data'=>$data
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload; //
//$msg = chr(0) . pack('n', 32) . pack('H*',str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))) . pack('n', strlen($payload)) . $payload; //
//str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))
//$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))) . pack('n', strlen($payload)) . $payload
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
// Close the connection to the server
fclose($fp);
if (!$result){
//return 'Message not delivered' . PHP_EOL;
return 0;
}else{
//return 'Message successfully delivered' . PHP_EOL;
return 1;
}
}
}
public function send_android_notification($registration_ids, $data) {
$fields = array(
'registration_ids' => array($registration_ids),
'data'=> $data,
);
$headers = array(
'Authorization: key=AAAAAjykQAw:APA91bF2xwn1yqpfBkoxg_K_fTp5bOaI4M3zXL_yG63hmQpqvVFZlMiilFdf65DWdqAO5Xy3eBxJjFybwKMwDMnr16ls_YnMDpMAfwTbJUcBnpHK4supv42pZIoDEBHrdUwOFI9wmQYs', // FIREBASE_API_KEY_FOR_ANDROID_NOTIFICATION
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
// Disabling SSL Certificate support temporarly
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch );
if($result === false){
//die('Curl failed:' .curl_errno($ch));
return 0;
}
// Close connection
curl_close( $ch );
return $result;
}
}