migrate to gtea from bistbucket
This commit is contained in:
40
public/restaurant/app/Traits/Authenticatable.php
Normal file
40
public/restaurant/app/Traits/Authenticatable.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Exception;
|
||||
|
||||
trait Authenticatable
|
||||
{
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getCurrentUserId(): int
|
||||
{
|
||||
if (app()->runningInConsole()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (! isset(request()->user()->id)) {
|
||||
throw new Exception('You are not authenticated to view this.');
|
||||
}
|
||||
|
||||
return (int) request()->user()->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getCurrentRestaurantId(): int
|
||||
{
|
||||
if (app()->runningInConsole()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (! isset(request()->user()->restaurant_id)) {
|
||||
throw new Exception('You are not authenticated to view this.');
|
||||
}
|
||||
|
||||
return (int) request()->user()->restaurant_id;
|
||||
}
|
||||
}
|
||||
24
public/restaurant/app/Traits/HasPermissionTrait.php
Normal file
24
public/restaurant/app/Traits/HasPermissionTrait.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
trait HasPermissionTrait
|
||||
{
|
||||
use ResponseTrait;
|
||||
|
||||
public function checkPermission($permission)
|
||||
{
|
||||
if (Auth::user()->hasPermissionTo($permission, 'web')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->responseError([], 'You have no access on this feature', 403);
|
||||
}
|
||||
|
||||
public function handlePermission($permission)
|
||||
{
|
||||
return $this->checkPermission($permission);
|
||||
}
|
||||
}
|
||||
78
public/restaurant/app/Traits/PaymentProcess.php
Normal file
78
public/restaurant/app/Traits/PaymentProcess.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Routing\Redirector;
|
||||
use Modules\Authentication\Models\SAASSetting;
|
||||
use Modules\Authentication\Models\Setting;
|
||||
|
||||
trait PaymentProcess
|
||||
{
|
||||
use Authenticatable;
|
||||
|
||||
public function responseFormatter($constant, $content = null, $errors = []): array
|
||||
{
|
||||
$constant = (array) $constant;
|
||||
$constant['content'] = $content;
|
||||
$constant['errors'] = $errors;
|
||||
|
||||
return $constant;
|
||||
}
|
||||
|
||||
public function errorProcessor($validator): array
|
||||
{
|
||||
$errors = [];
|
||||
foreach ($validator->errors()->getMessages() as $index => $error) {
|
||||
$errors[] = ['error_code' => $index, 'message' => $error[0]];
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
public function paymentResponse($payment_info, $payment_flag): Application|JsonResponse|Redirector|RedirectResponse|\Illuminate\Contracts\Foundation\Application
|
||||
{
|
||||
$getNewUser = (int) 0;
|
||||
$additionalData = json_decode($payment_info->additional_data, true);
|
||||
|
||||
if (isset($additionalData['new_customer_id']) && isset($additionalData['is_guest_in_order'])) {
|
||||
$getNewUser = (int) (($additionalData['new_customer_id'] != 0 && $additionalData['is_guest_in_order'] != 1) ? 1 : 0);
|
||||
}
|
||||
|
||||
$token_string = 'payment_method='.$payment_info->payment_method.'&&transaction_reference='.$payment_info->transaction_id;
|
||||
if (in_array($payment_info->payment_platform, ['web', 'app']) && $payment_info['external_redirect_link'] != null) {
|
||||
return redirect($payment_info['external_redirect_link'].'?flag='.$payment_flag.'&&token='.base64_encode($token_string).'&&new_user='.$getNewUser);
|
||||
}
|
||||
|
||||
return redirect()->route('payment-'.$payment_flag, ['token' => base64_encode($token_string), 'new_user' => $getNewUser]);
|
||||
}
|
||||
|
||||
public function paymentConfig($key, $settingsType, $payment): ?object
|
||||
{
|
||||
if (empty($key) || empty($settingsType) || empty($payment)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($payment->payment_type == 'subscription_payment') {
|
||||
try {
|
||||
$config = SAASSetting::where('name', $key)
|
||||
->where('type', $settingsType)->first();
|
||||
} catch (Exception $exception) {
|
||||
return new SAASSetting;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
$config = Setting::where('name', $key)
|
||||
->where('restaurant_id', $payment->restaurant_id)
|
||||
->where('type', $settingsType)->first();
|
||||
} catch (Exception $exception) {
|
||||
return new Setting;
|
||||
}
|
||||
}
|
||||
|
||||
return (isset($config)) ? $config : null;
|
||||
}
|
||||
}
|
||||
13
public/restaurant/app/Traits/RequestSanitizerTrait.php
Normal file
13
public/restaurant/app/Traits/RequestSanitizerTrait.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
trait RequestSanitizerTrait
|
||||
{
|
||||
protected function getUpdateRequest(Request $request)
|
||||
{
|
||||
return $request->except(['_method', 'id']);
|
||||
}
|
||||
}
|
||||
42
public/restaurant/app/Traits/ResponseTrait.php
Normal file
42
public/restaurant/app/Traits/ResponseTrait.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
trait ResponseTrait
|
||||
{
|
||||
/**
|
||||
* Success response.
|
||||
*
|
||||
* @param object|array $data
|
||||
*/
|
||||
public function responseSuccess($data, string $message = 'Successful'): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
'errors' => null,
|
||||
], Response::HTTP_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* Error response.
|
||||
*
|
||||
* @param array|object $errors
|
||||
*/
|
||||
public function responseError(
|
||||
$errors,
|
||||
string $message = 'Something went wrong.',
|
||||
int $responseCode = Response::HTTP_INTERNAL_SERVER_ERROR
|
||||
): JsonResponse {
|
||||
return response()->json([
|
||||
'status' => false,
|
||||
'message' => $message,
|
||||
'data' => null,
|
||||
'errors' => $errors,
|
||||
], $responseCode);
|
||||
}
|
||||
}
|
||||
48
public/restaurant/app/Traits/SlugAbleTrait.php
Normal file
48
public/restaurant/app/Traits/SlugAbleTrait.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
trait SlugAbleTrait
|
||||
{
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createUniqueSlug(string $title, string $tableName, string $columnName, string $separator = '-'): string
|
||||
{
|
||||
$id = 0;
|
||||
$slug = preg_replace('/\s+/', $separator, (trim(strtolower($title))));
|
||||
$slug = preg_replace('/\?+/', $separator, (trim(strtolower($slug))));
|
||||
$slug = preg_replace('/\#+/', $separator, (trim(strtolower($slug))));
|
||||
$slug = preg_replace('/\/+/', $separator, (trim(strtolower($slug))));
|
||||
|
||||
// // Replace all separator characters and whitespace by a single separator
|
||||
$slug = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $slug);
|
||||
|
||||
$allSlugs = $this->getRelatedSlugs($slug, $tableName, $columnName, $id);
|
||||
// If we haven't used it before then we are all good.
|
||||
if (! $allSlugs->contains("$columnName", $slug)) {
|
||||
return $slug;
|
||||
}
|
||||
// Just append numbers like a savage until we find not used.
|
||||
for ($i = 1; $i <= 1000000000; $i++) {
|
||||
$newSlug = $slug.$separator.$i;
|
||||
if (! $allSlugs->contains("$columnName", $newSlug)) {
|
||||
return $newSlug;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception('Can not create a unique slug');
|
||||
}
|
||||
|
||||
private function getRelatedSlugs(string $slug, string $tableName, string $columnName, int $id = 0): Collection
|
||||
{
|
||||
return DB::table($tableName)
|
||||
->select("$columnName")->where("$columnName", 'like', $slug.'%')
|
||||
->where('id', '<>', $id)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
36
public/restaurant/app/Traits/Trackable.php
Normal file
36
public/restaurant/app/Traits/Trackable.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Modules\Authentication\Models\UserLog;
|
||||
|
||||
trait Trackable
|
||||
{
|
||||
public function trackAction($action = 'create', $model = null, $model_id = null, $detail = null): void
|
||||
{
|
||||
try {
|
||||
if (! auth()->check()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
$user_id = $user->id;
|
||||
$ip_address = request()->ip();
|
||||
$url = request()->fullUrl();
|
||||
|
||||
$userLog = new UserLog;
|
||||
$userLog->user_id = $user_id;
|
||||
$userLog->institute_id = getUserRestaurantId();
|
||||
$userLog->user_id = $user_id;
|
||||
$userLog->ip_address = $ip_address;
|
||||
$userLog->action = $action;
|
||||
$userLog->detail = $detail;
|
||||
$userLog->model = $model;
|
||||
$userLog->url = $url;
|
||||
$userLog->model_id = $model_id;
|
||||
$userLog->save();
|
||||
} catch (\Throwable $th) {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user