Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
www
/
app
/
Libraries
/
Filename :
Helpers.php
back
Copy
<?php namespace App\Libraries; use Validator; use App\Models\SPWalletTransaction; use App\Models\User; use DB; use Config; use Carbon\Carbon; class Helpers { public function __construct() { //do some stuff } static function successResponse($array, $response, $message) { $array['ResponseCode'] = $response; $array['ResponseMsg'] = $message; return response()->json($array); } static function responseMessage($response, $message) { return response()->json([ "ResponseCode" => $response, "ResponseMsg"=> $message ]); } public static function validatorErrorResponse($request, $rules) { $validator = Validator::make($request->all(), $rules); if($validator->fails()){ $data['errors'] = $validator->errors(); $result = Helpers::successResponse($data, 422, 'Please validate request.'); return array('flag' => 1, 'response' => $result); } return array('flag' => 0); } static function sendPushIOS($device_udid_array, $title, $message, $type, $id="") { $pem_file = Config::get('constants.app.ios-pem-file-path'); $pem_secret = Config::get('constants.app.ios-passphrase'); $apns_topic = 'com.CoinsForCollege'; $tAlert = $message; $tBadge = 0; $tSound = 'default'; $tPayload = array( "title" => $title, "body" => $message, "type" => $type, "id" => $id, ); $tBody['aps'] = array ('alert' => $tAlert,'badge' => $tBadge,'sound' => $tSound); $tBody ['payload'] = $tPayload; $tBody = json_encode($tBody); foreach ($device_udid_array as $key => $device_token) { if(strlen($device_token) == 64){ if(!empty($device_token)){ $url = "https://api.push.apple.com/3/device/$device_token"; //$url = "https://api.sandbox.push.apple.com/3/device/$device_token"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_POSTFIELDS, $tBody); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic")); curl_setopt($ch, CURLOPT_SSLCERT, $pem_file); curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret); $response = curl_exec($ch); //print_r($response);exit; $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); } } } } static function sendPushAndroid($device_udid_array, $title, $message, $type, $id="") { // echo print_r($device_udid_array).'-'.$title.'-'.$message;exit; $url = 'https://fcm.googleapis.com/fcm/send'; $fields = array( 'registration_ids' => $device_udid_array, /* 'to' => $device_udid_array,*/ 'data' => array( "title" => $title, "body" => $message, "type" => $type, "id" => $id, ), /*'notification' => array( "title" => 'Coins', "text" => $message ),*/ ); $headers = array( 'Authorization: key=' . Config::get('constants.app.android-auth-key'), 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($ch); if ($result === FALSE) { // die('Curl failed: ' . curl_error($ch)); } curl_close($ch); //return $result; } static function storeNotifications($from_user_id, $to_user_ids, $title, $message, $table_name, $field_id) { return DB::table('notifications')->insert([ 'from_user_id' => $from_user_id, 'to_user_ids' => implode(",", $to_user_ids), 'title' => $title, 'message' => $message, 'table_name' => $table_name, 'field_id' => $field_id, 'created_at' => date("Y-m-d H:i:s") ]); } public static function dateDiffForHumans($date) { $fromDate = Carbon::parse($date)->addDays(1); $toDate = Carbon::now(); $days = $toDate->diffInDays($fromDate); if($days == 1){ return 'tomorrow'; } else if($days == 0){ return 'today'; } else if($days > 0){ return $days.' days'; } else { return $fromDate->diffForHumans(['options' => Carbon::ONE_DAY_WORDS]); } } }