2023-05-25 15:04:43 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use DB;
|
|
|
|
|
use Mail;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Mail\NotifyAdmin;
|
|
|
|
|
use App\Mail\NotifyUser;
|
2023-06-02 04:17:05 -04:00
|
|
|
use App\Mail\PinDisabled;
|
2023-05-25 15:04:43 -04:00
|
|
|
use App\Models\ExpiredPin;
|
|
|
|
|
use App\Models\PBXPin;
|
|
|
|
|
use GuzzleHttp\RetryMiddleware;
|
|
|
|
|
|
|
|
|
|
class PBXController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function pbx (Request $request) {
|
|
|
|
|
$pins = DB::table('pbx_pin')->orderBy('name')->get();
|
|
|
|
|
$curr_user = $request->session()->get('credentials');
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'pins' => $pins,
|
|
|
|
|
'curr_user' => $curr_user[0]['name']
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return view('Admin.pbx', compact('data'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function pbxSearch (Request $request) {
|
|
|
|
|
$data = $request->all();
|
|
|
|
|
$criteria = $data['search'];
|
|
|
|
|
|
2023-05-25 16:57:39 -04:00
|
|
|
$pins = PBXPin::where('name', 'LIKE', '%'.$criteria.'%')
|
2023-05-25 15:04:43 -04:00
|
|
|
->orWhere('pin', '=', $criteria)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
$curr_user = $request->session()->get('credentials');
|
|
|
|
|
$data = [
|
|
|
|
|
'pins' => $pins,
|
|
|
|
|
'curr_user' => $curr_user[0]['name']
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return view('Admin.pbx', compact('data'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createPin (Request $request) {
|
|
|
|
|
$data = $request->all();
|
|
|
|
|
$curr_user = $request->session()->get('credentials');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$pins = DB::table('pbx_pin')->orderBy("name")->get();
|
|
|
|
|
$user = DB::table('users')->where('email', '=', $data['add-pbx'])->get();
|
|
|
|
|
|
|
|
|
|
if (!$user->isEmpty()) {
|
|
|
|
|
$user_check = DB::table('pbx_pin')->where('email', '=', $data['add-pbx'])->get();
|
|
|
|
|
|
|
|
|
|
if($user_check->isEmpty()){
|
|
|
|
|
$check = 0;
|
|
|
|
|
while ($check == 0) {
|
|
|
|
|
$pinGenerator = rand(10000,99999);
|
|
|
|
|
$check_curr = DB::table('pbx_pin')->where('pin', '=', $pinGenerator)->get();
|
|
|
|
|
$check_old = DB::table('expired_pins')->where('pin', '=', $pinGenerator)->get();
|
|
|
|
|
|
|
|
|
|
if ($check_curr->isEmpty() && $check_old->isEmpty()){
|
|
|
|
|
$check = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pbx = new PBXPin();
|
2023-05-25 16:57:39 -04:00
|
|
|
|
|
|
|
|
$pbx->name = $user[0]->name;
|
|
|
|
|
$pbx->email = $user[0]->email;
|
2023-05-25 15:04:43 -04:00
|
|
|
$pbx->updated_by = $curr_user[0]['name'];
|
|
|
|
|
$pbx->pin = $pinGenerator;
|
2023-05-25 16:57:39 -04:00
|
|
|
|
2023-05-25 15:04:43 -04:00
|
|
|
$pbx->save();
|
2023-06-02 04:17:05 -04:00
|
|
|
|
|
|
|
|
$mailData = $pbx;
|
|
|
|
|
|
|
|
|
|
Mail::to('amirah.ali@gov.tt')->send(new NotifyUser($mailData));
|
|
|
|
|
Mail::to('amirah.ali@gov.tt')->send(new NotifyAdmin($mailData));
|
|
|
|
|
|
2023-05-25 16:57:39 -04:00
|
|
|
return redirect()->route('pbx');
|
2023-05-25 15:04:43 -04:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// User already has PBX pin assigned
|
|
|
|
|
$data = [
|
|
|
|
|
'pins' => $pins,
|
|
|
|
|
'error' => '1',
|
|
|
|
|
'curr_user' => $curr_user[0]['name']
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return view('Admin.pbx', compact('data'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// User is not found
|
|
|
|
|
$pins = DB::table('pbx_pin')->orderBy("name")->get();
|
|
|
|
|
$data = [
|
|
|
|
|
'pins' => $pins,
|
|
|
|
|
'error' => '2',
|
|
|
|
|
'curr_user' => $curr_user[0]['name']
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return view('Admin.pbx', compact('data'));
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
|
Log::channel('error')->info('Error:'.$ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unauthorizedAccess () {
|
|
|
|
|
return view('Admin.unauthorized');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function disable (Request $request, $id) {
|
|
|
|
|
try {
|
2023-05-25 16:57:39 -04:00
|
|
|
$pin = DB::table('pbx_pin')->where('id', $id)->first();
|
2023-05-25 15:04:43 -04:00
|
|
|
$curr_user = $request->session()->get('credentials');
|
2023-05-25 16:57:39 -04:00
|
|
|
|
|
|
|
|
$old_pin = new ExpiredPin();
|
|
|
|
|
|
|
|
|
|
$old_pin->pin = $pin->pin;
|
|
|
|
|
$old_pin->name = $pin->name;
|
|
|
|
|
$old_pin->email = $pin->email;
|
|
|
|
|
$old_pin->updated_by = $curr_user[0]['name'];
|
|
|
|
|
|
|
|
|
|
$old_pin->save();
|
|
|
|
|
$pin = DB::table('pbx_pin')->where('id', $id)->delete();
|
2023-06-02 04:17:05 -04:00
|
|
|
|
|
|
|
|
$mailData = $old_pin;
|
|
|
|
|
Mail::to('amirah.ali@gov.tt')->send(new PinDisabled($mailData));
|
2023-05-25 15:04:43 -04:00
|
|
|
|
|
|
|
|
return redirect()->route('pbx');
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $ex) {
|
|
|
|
|
Log::channel('error')->info('Error:'.$ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|