Created
June 12, 2018 08:46
-
-
Save lensraster/a01067d5d4be8152ed40e2732105c6ba to your computer and use it in GitHub Desktop.
SAWA's PostController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use DB; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\Input; | |
use Illuminate\Support\Facades\Config; | |
use Illuminate\Support\Facades\Response; | |
use App\Project; | |
use App\AssetRequest; | |
use App\AssetRequestSocialMedia; | |
use App\Logic\Image\UploadRepository; | |
use App\Image; | |
use App\SavedPost; | |
use App\PostSize; | |
use Storage; | |
use App\Logic\Util; | |
use App\User; | |
class PostController extends Controller | |
{ | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function __construct(UploadRepository $uploadRepository) | |
{ | |
$this->middleware('auth')->except(['fill', 'add_image', 'delete_image', 'generate_design']); | |
$this->middleware('App\Http\Middleware\AuthOrPlayground')->only('fill', 'generate_design'); | |
$this->uploader = $uploadRepository; | |
} | |
private function getDefaultSizes() | |
{ | |
$temp = []; | |
foreach(config('templates.sizes') as $k=>$v) { | |
$v['id'] = $k; | |
$temp[] = $v; | |
} | |
return $temp; | |
} | |
private function getCustomSizes($show_delete, $user=false) | |
{ | |
if (!$user) { | |
$user = Auth::user(); | |
} | |
$organization = $user->org(); | |
$post_sizes = DB::table('post_sizes') | |
->where('organization_id', $organization->id); | |
if (!$show_delete) { | |
$post_sizes = $post_sizes->where('deleted', false); | |
} | |
return $post_sizes->get(); | |
} | |
public function index() | |
{ | |
return view('post/index'); | |
} | |
public function fill(Request $request, $request_id=null) { | |
if(!$request->is_playground) { | |
$user = Auth::user(); | |
} else { | |
$user = User::find(config('app.playground_user_id')); | |
} | |
$organization = $user->org(); | |
$brands = $organization->brands->where('is_complete', true)->toArray(); | |
$template_data = [ | |
'brands' => array_values($brands), | |
'post_sizes' => $this->getCustomSizes(false, $user), | |
'default_sizes' => $this->getDefaultSizes(), | |
'is_playground' => $request->is_playground | |
]; | |
if($request_id) { | |
$asset_request = AssetRequest::find($request_id); | |
if($asset_request) { | |
$template_data['request'] = $asset_request; | |
$template_data['request_json'] = Util::escapeJson($asset_request->social_media->frontend_format_json); | |
} else { | |
abort(404); | |
} | |
} | |
return view('post/fill', $template_data); | |
} | |
public function add_image(Request $request) { | |
$data = Input::all(); | |
$this->validate($request, Image::$rules); | |
$image_data = ['asset_request_id' => isset($data['asset_request_id']) ? $data['asset_request_id'] : null]; | |
if(!empty($data['unsplash_url'])) { | |
$image_data = array_merge($image_data, [ | |
'unsplash_url' => $data['unsplash_url'], | |
'unsplash_id' => $data['unsplash_id'] | |
]); | |
} else { | |
$image_data['file_uri'] = $this->uploader->uploadRequestFile($data['file'], Config::get('uploads.asset_request_images_dir')); | |
} | |
$image = Image::create($image_data); | |
return Response::json([ | |
'id' => $image->id, | |
'file_url' => $image->get_url() | |
], 200); | |
} | |
public function save_custom_size(Request $request) { | |
$user = Auth::user(); | |
$organization = $user->org(); | |
$data = Input::all(); | |
$post_size = new PostSize(); | |
$post_size = $post_size->fill($data); | |
$post_size->user_id = $user->id; | |
$post_size->organization_id = $organization->id; | |
$post_size->save(); | |
return Response::json($post_size->toArray(), 200); | |
} | |
public function delete_custom_size(Request $request, $id) | |
{ | |
$post_size = PostSize::find($id); | |
$post_size->deleted = true; | |
$post_size->save(); | |
return Response::json(['id'=>(int)$id], 200); | |
} | |
public function delete_image($id) | |
{ | |
$image = Image::find($id); | |
if($image) { | |
$image->delete(); | |
return Response::json([ | |
'status' => 'success' | |
]); | |
} else { | |
return Response::json([ | |
'error' => 'Image does not exist' | |
], 404); | |
} | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request, $id) | |
{ | |
$user = Auth::user(); | |
$data = Input::all(); | |
if ($id != 'new') { | |
$post = SavedPost::find($id); | |
} else { | |
$post = new SavedPost(); | |
} | |
$post->fill($data); | |
$post->saved_by()->associate($user); | |
$post->save(); | |
if ($id == 'new') { | |
$data_json = json_decode($post->data_json, true); | |
$data_json['id'] = $post->id; | |
$post->data_json = json_encode($data_json); | |
$post->save(); | |
} | |
return Response::json([ | |
'id' => $post->id | |
], 200); | |
} | |
public function saved(Request $request) | |
{ | |
$user = Auth::user(); | |
$posts = SavedPost::where('user_id', '=', $user->id)->get(); | |
$projects_ids = $user->org()->projects()->pluck('id')->toArray(); | |
$asset_requests = AssetRequest::whereIn('project_id', $projects_ids)->get(); | |
$brands = $user->org()->brands->where('is_complete', true)->toArray(); | |
$recent_designs = []; | |
foreach($asset_requests as $request) { | |
if($request->social_media && $request->social_media->frontend_format_json) { | |
$recent_designs[] = $request->social_media; | |
} | |
} | |
return view('post/saved', | |
[ | |
'brands' => array_values($brands), | |
'post_sizes' => $this->getCustomSizes(false), | |
'default_sizes' => $this->getDefaultSizes(), | |
'posts' => $posts, | |
'recent_designs' => $recent_designs | |
]); | |
} | |
public function generate_design(Request $request) | |
{ | |
if(!$request->is_playground) { | |
$user = Auth::user(); | |
} else { | |
$user = User::find(config('app.playground_user_id')); | |
} | |
$organization = $user->org(); | |
$data = Input::all(); | |
$post = json_decode($data['data_json'], true); | |
$request_id = isset($data['request_id']) ? $data['request_id'] : null; | |
// Destroy saved-for-later post data | |
if (isset($post['id'])) { | |
SavedPost::destroy($post['id']); | |
} | |
if($request_id) { | |
$asset_request = AssetRequest::find($data['request_id']); | |
$asset_request->project->name = $post['name']; | |
$asset_request->project->save(); | |
} else { | |
// create new project | |
$project = new Project(); | |
$project->name = $post['name']; | |
$project->organization_id = $organization->id; | |
$project->user_id = $user->id; | |
$project->save(); | |
// create new asset_request | |
$asset_request = new AssetRequest(); | |
$asset_request->project_id = $project->id; | |
$asset_request->type = 'social-media'; | |
$asset_request->status = 'new'; | |
$asset_request->submitted_by()->associate($user); | |
} | |
$asset_request->brand_id = $post['brandId']; | |
$asset_request->save(); | |
// associate saved images to asset_request | |
$images_ids = []; | |
foreach($post['imgData']['imgs'] as $img) { | |
if(!empty($img['id'])) { | |
$images_ids[] = $img['id']; | |
} | |
} | |
if($request_id) { | |
// Delete images that are deleted in UI | |
$stale_images = Image::whereNotIn('id', $images_ids)->where('asset_request_id', $asset_request->id)->get(); | |
foreach ($stale_images as $image) { | |
$image->delete(); | |
} | |
} | |
// Associate newly uploaded images with current asset request | |
Image::whereIn('id', $images_ids)->whereNull('asset_request_id') | |
->update(['asset_request_id' => $asset_request->id]); | |
if($request_id) { | |
$social_media_request = $asset_request->social_media; | |
} else { | |
// create new social_media_request | |
$social_media_request = new AssetRequestSocialMedia(); | |
$social_media_request->asset_request_id = $asset_request->id; | |
} | |
if (!is_numeric($post['size']['id'])) { | |
$social_media_request->platform = $post['size']['id']; | |
} else { | |
$social_media_request->platform = 'custom'; | |
$social_media_request->custom_size_id = $post['size']['id']; | |
} | |
$social_media_request->size_ids = "{$post['size']['id']}"; | |
$social_media_request->no_images = $post['imgData']['noUpload']; | |
$social_media_request->include_logo = $post['incLogo']; | |
$social_media_request->heading = $post['texts']['heading']; | |
$social_media_request->subheading = $post['texts']['subheading']; | |
$social_media_request->frontend_format_json = json_encode($post); | |
$social_media_request->save(); | |
return Response::json([ | |
'status' => 'OK', | |
'id' => $asset_request->id, | |
'template_id' => $asset_request->brand->template_id ?: $organization->template_id | |
], 200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment