Skip to content

Instantly share code, notes, and snippets.

@iedmrc
Created July 13, 2018 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iedmrc/2cc07f926e24daf2ae32c9aa93b99d97 to your computer and use it in GitHub Desktop.
Save iedmrc/2cc07f926e24daf2ae32c9aa93b99d97 to your computer and use it in GitHub Desktop.
A code snippet from a laravel project I wrote
/**
* Saves post to database
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function post(Request $request)
{
//Models
$posts = new Post();
$hashtags = new Hashtag();
$entries = new Fileentry();
//Check Authentication
if(!Auth::check()) return redirect('/');
$referer = $request->server('HTTP_REFERER');
$kafe_id=0;
//If sended in a kafe
if (strpos($referer,'kafe') != false)
{
$kafe_id= $request->kafe_hidden;
}
else $kafe_id=0;
//If user is not a subscriber of kafe, set kafe_id=0
if(!Auth::user()->kafes()->where('kafe_id',$kafe_id)->first()) $kafe_id=0;
//Chars number limited by user permission
if(Auth::user()->permissions==0)
{
$this->validate(
$request,
[
'post' => 'required_without_all:img,img_hidden,video|max:280',
'img'=>'mimes:jpeg,bmp,png|max:2048',
'video'=>'mimes:mp4,mov,quicktime,qt|max:10000',
'location'=>'max:32'
],
[ ],
[
'post' => trans('global_texts.gonderi'),
'img_hidden'=>trans('global_texts.gorsellerin'),
'img'=>'',
'video'=>'video',
'location'=>'Konum'
]);
}
elseif(Auth::user()->permissions==1 or Auth::user()->permissions==2 or Auth::user()->permissions==3)
{
$this->validate(
$request,
[
'post' => 'required_without_all:img,img_hidden,video|max:500',
'img'=>'mimes:jpeg,bmp,png|max:2048',
'video'=>'mimes:mp4,mov,quicktime,qt|max:10000',
'location'=>'max:32'
],
[ ],
[
'post' => trans('global_texts.gonderi'),
'img_hidden'=>trans('global_texts.gorsellerin'),
'img'=>'',
'video'=>'video',
'location'=>'Konum'
]);
}
$post = htmlspecialchars($request->post); //Get value of post textarea
$parsed_hashtags = getHashtags($post); //Parse hashtags
$parsed_mentions = getMentions($post); //Parse mentions
$db = convert_clickable_links($post); //Parse and convert normal and youtube links to <a> tag
$entry_id=0;
$video_entry_id=0;
$data= base64_decode(str_replace(' ','+',str_replace('data:image/jpeg;base64,', '', $request->img_hidden)));
if($data==false or $request->img_hidden=='' or $request->img_hidden==null)
{
//If image has sent with regular way
if($request->hasFile('img'))
{
$file = $request->file('img');
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put('/posts/' . $file->getFilename() . '.' . $extension, File::get($file));
$entry_id =
$entries->create(
[
'mime' => $file->getClientMimeType(),
'original_filename' => $file->getClientOriginalName(),
'filename' => $file->getFilename() . '.' . $extension
])->id;
}
}
else
{
$name=rand(1000000000,2147483647).'.jpg';
Storage::disk('local')->put('/posts/' . $name, $data);
$entry_id =
$entries->create(
[
'mime' => 'image/jpeg',
'original_filename' => $name,
'filename' => $name
])->id;
}
//If video sent
if($request->hasFile('video'))
{
$video_file = $request->file('video');
$extension = $video_file->getClientOriginalExtension();
Storage::disk('local')->put('/posts/' . $video_file->getFilename() . '.' . $extension, File::get($video_file));
$video_entry_id =
$entries->create(
[
'mime' => $video_file->getClientMimeType(),
'original_filename' => $video_file->getClientOriginalName(),
'filename' => $video_file->getFilename() . '.' . $extension
])->id;
}
//If post is a reply
if (isset($request->post_hidden) and $request->post_hidden!=0)
{
//Save post to table with mentions, links, entries and kafe id if set.
$username = Post::find($request->post_hidden)->user->username;
if(!in_array($username,$parsed_mentions)) return redirect()->back()->withErrors([trans('global_texts.gonderi_bahset')]);
$post_id =
$posts->create(
[
'user_id' => Auth::id(),
'post' => $db,
'mentions' => $parsed_mentions,
'reply_id'=>$request->post_hidden,
'kafe_id'=>$kafe_id,
'entry_id'=>$entry_id,
'video_entry_id'=>$video_entry_id,
'location'=>trim($request->location)
])->id;
$replied_user_id=$posts->find($request->post_hidden)->user_id;
$replied_user_name=User::find($replied_user_id)->username;
//Save notifications to table
foreach($parsed_mentions as $mention)
{
if($mention!=Auth::user()->username)
{
if ($mention == $replied_user_name)
{
Notifynder::category('user.reply')
->from(Auth::id())
->to($replied_user_id)
->url((string)$request->post_hidden)
->send();
} else
{
$to = User::where('username', $mention)->first()->id;
Notifynder::category('user.mention')
->from(Auth::id())
->to($to)
->url((string)$post_id)
->send();
}
}
}
//Save hashtags to table
foreach ($parsed_hashtags as $tags)
{
$hashtags->firstOrCreate(
[
'user_id' => Auth::id(),
'hashtag' => $tags,
'post_id' => $post_id
]);
}
}
//If post is not a reply
else
{
//Save post to table with mentions, links, entries and kafe id if set.
$post_id =
$posts->create(
[
'user_id' => Auth::id(),
'post' => $db,
'mentions' => $parsed_mentions,
'kafe_id'=>$kafe_id,
'entry_id'=>$entry_id,
'video_entry_id'=>$video_entry_id,
'location'=>trim($request->location)
])->id;
//Save notifications to table
foreach($parsed_mentions as $mention)
{
if($mention!=Auth::user()->username)
{
$to = User::where('username', $mention)->first()->id;
Notifynder::category('user.mention')
->from(Auth::id())
->to($to)
->url((string)$post_id)
->send();
}
}
//Save hashtags to table
foreach ($parsed_hashtags as $tags)
{
$hashtags->firstOrCreate(
[
'user_id' => Auth::id(),
'hashtag' => $tags,
'post_id' => $post_id
]);
}
}
return redirect()->back();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment