2021-07-16 02:35:54 -04:00
< ? php
namespace App\Http\Controllers ;
use App\Models\Meme ;
use App\Models\User ;
use App\Models\Address ;
use Illuminate\Http\Request ;
use chillerlan\QRCode\ { QRCode , QROptions };
2021-08-06 16:06:07 -04:00
use MoneroIntegrations\MoneroPhp\walletRPC ;
2021-08-06 17:01:16 -04:00
use Artesaos\SEOTools\Facades\SEOTools ;
2021-07-16 02:35:54 -04:00
class MemeController extends Controller
{
/**
* Create a new controller instance .
*
* @ return void
*/
public function __construct ()
{
// $this->middleware('auth');
}
/**
* Display a listing of the resource .
*
* @ return \Illuminate\Http\Response
*/
public function index ()
{
2021-08-06 17:01:16 -04:00
SEOTools :: setTitle ( 'Home' );
2021-07-16 02:35:54 -04:00
$data = [
'memes' => Meme :: with ([ 'user' , 'tips' ]) -> orderByDesc ( 'created_at' ) -> paginate ( 20 ),
];
return view ( 'homepage' , [ 'data' => $data ]);
}
public function leaderboard ()
{
2021-08-06 17:01:16 -04:00
SEOTools :: setTitle ( 'Leaderboard' );
2021-07-16 02:35:54 -04:00
$users = User :: with ( 'tips' ) -> has ( 'memes' ) -> get ();
$users = $users -> sortByDesc ( function ( $user ) {
return $user -> tips_total ;
});
$data = [
'users' => $users ,
];
return view ( 'leaderboard' , [ 'data' => $data ]);
}
/**
* Show the form for creating a new resource .
*
* @ return \Illuminate\Http\Response
*/
public function create ()
{
2021-08-06 17:01:16 -04:00
SEOTools :: setTitle ( 'Submit' );
2021-07-16 02:35:54 -04:00
return view ( 'meme-create' );
}
/**
* Store a newly created resource in storage .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\Response
*/
public function store ( Request $request )
{
2021-07-26 22:39:11 -04:00
$validatedData = $request -> validate ([
'title' => [ 'required' , 'string' , 'max:255' ],
'caption' => [ 'string' , 'max:255' , 'nullable' ],
'image' => [ 'required' , 'image' ],
]);
2021-07-16 02:35:54 -04:00
$user = \Auth :: user ();
2021-08-06 16:06:07 -04:00
Meme :: create ([
2021-07-16 02:35:54 -04:00
'user_id' => $user -> id ,
'title' => $request -> input ( 'title' ),
2021-07-26 22:39:11 -04:00
'caption' => $request -> input ( 'caption' ),
2021-07-16 02:35:54 -04:00
'image' => $request -> file ( 'image' ),
]);
}
/**
* Display the specified resource .
*
* @ param \App\Models\Meme $meme
* @ return \Illuminate\Http\Response
*/
public function show ( $id )
{
2021-08-06 16:06:07 -04:00
$meme = Meme :: where ( 'id' , $id ) -> with ([ 'user' , 'tips' ]) -> firstOrFail ();
2021-07-16 02:35:54 -04:00
2021-08-06 17:01:16 -04:00
SEOTools :: setTitle ( $meme -> title );
SEOTools :: addImages ( $meme -> image_url );
2021-07-16 02:35:54 -04:00
$share = \Share :: page ( url () -> current (), $meme -> title , [ 'class' => 'fa-lg' , 'target' => '_blank' ])
-> facebook ()
-> twitter ()
-> reddit ()
-> telegram ()
-> linkedin ()
-> whatsapp ();
$data = [
'meme' => $meme ,
2021-08-06 16:06:07 -04:00
'qr' => ( new QRCode ) -> render ( $meme -> address ),
2021-07-16 02:35:54 -04:00
'share' => preg_replace ( " /<a(.*?)>/ " , " <a $ 1 target= \" _blank \" > " , $share ),
];
return view ( 'meme' , [ 'data' => $data ]);
}
/**
* Show the form for editing the specified resource .
*
* @ param \App\Models\Meme $meme
* @ return \Illuminate\Http\Response
*/
public function edit ( Meme $meme )
{
//
}
/**
* Update the specified resource in storage .
*
* @ param \Illuminate\Http\Request $request
* @ param \App\Models\Meme $meme
* @ return \Illuminate\Http\Response
*/
public function update ( Request $request , Meme $meme )
{
//
}
2021-07-26 22:39:11 -04:00
public function approve ( $id )
{
if ( \Auth :: user () -> is_admin === 1 ) {
2021-08-06 16:06:07 -04:00
try {
$walletRPC = new walletRPC ( config ( 'app.xmr_daemon_ip' ), config ( 'app.xmr_network_port' )); // Change to match your wallet (monero-wallet-rpc) IP address and port; 18083 is the customary port for mainnet, 28083 for testnet, 38083 for stagenet
$open_wallet = $walletRPC -> open_wallet ( config ( 'app.xmr_wallet_name' ), '' );
$account = $walletRPC -> create_account ();
$meme = Meme :: withoutGlobalScope ( 'approved' ) -> find ( $id );
$meme -> is_approved = 1 ;
$meme -> account_index = $account [ 'account_index' ];
$meme -> address = $account [ 'address' ];
$meme -> save ();
return redirect () -> away ( url () -> previous ());
} catch ( \Exception $e ) {
}
2021-07-26 22:39:11 -04:00
}
}
2021-07-16 02:35:54 -04:00
/**
* Remove the specified resource from storage .
*
* @ param \App\Models\Meme $meme
* @ return \Illuminate\Http\Response
*/
2021-07-26 22:39:11 -04:00
public function destroy ( $id )
2021-07-16 02:35:54 -04:00
{
2021-07-26 22:39:11 -04:00
if ( \Auth :: user () -> is_admin === 1 ) {
Meme :: withoutGlobalScope ( 'approved' ) -> where ( 'id' , $id ) -> delete ();
return redirect () -> away ( url () -> previous ());
}
2021-07-16 02:35:54 -04:00
}
}