diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index c4297d2..0d90711 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -6,134 +6,24 @@ use Illuminate\Http\Request; use App\Company; use Image; use Illuminate\Support\Facades\Storage; +use Validator; class CompanyController extends Controller { - /** - * Display a listing of the resource. - * - * @return \Illuminate\Http\Response - */ public function index() { // return view ('home'); } - /** - * Show the form for creating a new resource. - * - * @return \Illuminate\Http\Response - */ - public function create() - { - // - } - - /** - * Store a newly created resource in storage. - * - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response - */ public function store(Request $request) { - // - //$company = new Company; - // if($request->hasFile('avatar')) - // { - // $avatar = $request->file('avatar'); - // $filename = time() . '.' . $avatar->getClientOriginalExtension(); - // Image::make($avatar)->resize(300,300)->save(public_path('/uploads/company_profile/'.$filename)); - // } - // else - // { - // return response()->json(['message'=>'Document not found']); - // } $input = $request->all(); - //validating uploaded files - // $this->validate(request(),[ - // 'company_profile' => 'required|image|mimes:jpg,jpeg,bmp,png', - // 'reg_cerf' => 'required|mimes:jpg,jpeg,bmp,png,gif,svg,pdf' - // ]); - - if($file = $request->file('company_profile')) //company profile picture - { - $company_profile = $request->file('company_profile'); - $filename = $company_profile->getClientOriginalName(); - //$destinationPath = config('app.fileDestinationPath').'/'.$filename; - - $file = $request->file('company_profile'); - $ext = $file->getClientOriginalExtension(); - - Storage::putFileAs( - 'company_profile',/*folder name*/ $file, $filename. '.' .$ext - ); - - // $name = $file->getClientOriginalName(); - // $img_file = $request->file('company_profile'); - // Image::make($img_file)->resize(300,300)->save(public_path('\company_profile')); - // //return $img_file; - // // $file->move('uploads/company_profile/', $img_file); - - // $input['company_profile'] = $name; - } - - if($request->hasFile('reg_cert')) //Registration Certificate upload - { - $reg_cert = $request->file('reg_cert'); - $filename = $reg_cert->getClientOriginalName(); - //$destinationPath = config('app.fileDestinationPath').'/'.$filename; - - $file = $request->file('reg_cert'); - $ext = $file->getClientOriginalExtension(); - - Storage::putFileAs( - 'reg_cert',/*folder name*/ $file, $filename. '.' .$ext - ); - - // $uploaded = Storage::put($destinationPath, file_get_contents($reg_cert->getRealPath())); - // $name = $file->getClientOriginalName(); - // $img_file = $request->file('reg_cert'); - // Image::make($img_file)->save(public_path('uploads\reg_cert')); - - // $input['company_profile'] = $name; - // $request->file('req_cert'); - // //$request->image->store('\uploads\reg_cert'); - // Storage::putFile('uploads\reg_cert', $request->file('reg_cert')); - } - // else{ - // return "failed"; - // } Company::create($input); - //Company::create(request()->all()); - //dd(request()->all()); return redirect('/'); } - /** - * Display the specified resource. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function show($id) - { - // - } - - /** - * Show the form for editing the specified resource. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function edit($id) - { - // - } - /** * Update the specified resource in storage. * @@ -146,37 +36,8 @@ class CompanyController extends Controller $company = Company::find($id); if (!$company) { - return response()->json(['message'=>'Document not found'],200); + return response()->json(['message'=>'company not found'],200); } - //updating company profile picture - if($file = $request->file('company_profile')) //company profile picture - { - $company_profile = $request->file('company_profile'); - $filename = $company_profile->getClientOriginalName(); - //$destinationPath = config('app.fileDestinationPath').'/'.$filename; - - $file = $request->file('company_profile'); - $ext = $file->getClientOriginalExtension(); - - Storage::putFileAs( - 'company_profile',/*folder name*/ $file, $filename. '.' .$ext - ); - } - - //updating registration certificate - if($request->hasFile('reg_cert')) //Registration Certificate upload - { - $reg_cert = $request->file('reg_cert'); - $filename = $reg_cert->getClientOriginalName(); - //$destinationPath = config('app.fileDestinationPath').'/'.$filename; - - $file = $request->file('reg_cert'); - $ext = $file->getClientOriginalExtension(); - - Storage::putFileAs( - 'reg_cert',/*folder name*/ $file, $filename. '.' .$ext - ); - } $company->company_name=$request->input('company_name'); $company->registration_no=$request->input('registration_no'); @@ -189,34 +50,89 @@ class CompanyController extends Controller $company->state=$request->input('state'); $company->country=$request->input('country'); $company->contact_person=$request->input('contact_person'); + $company->save(); - return response()->json(['warehouse'=>$company],200); - // $product = Company::findOrFail($productId); - // $product->update($request->all()); - // $product->company_name = 'imagepath'; - // $product->save(); - - //return response()->json($product, 200); + return response()->json(['company'=>$company],200); } - /** - * Remove the specified resource from storage. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function destroy($id) + public function companyProfile(Request $request, $id) { - // - } + $company = Company::find($id); + if (!$company) + { + return response()->json(['message'=>'Document not found'],200); + } - public function uploadImage($request) + $validator = Validator::make($request->all(), [ + 'company_profile' => 'image|mimes:jpg,jpeg,bmp,png' + ]); + + if($validator->fails()) + { + return "failed"; + } + + if($file = $request->file('company_profile')) //company profile picture + { + $company_profile = $request->file('company_profile'); + $filename = $company_profile->getClientOriginalName(); + //$filename = $company_profile->uniqid('img_'); + $unique_name = 'comp_prof_' . md5($filename); + //$destinationPath = config('app.fileDestinationPath').'/'.$filename; + + $file = $request->file('company_profile'); + $ext = $file->getClientOriginalExtension(); + $input['company_profile'] = $filename; + Storage::putFileAs( + 'company_profile',/*folder name*/ $file, $unique_name. '.' .$ext + ); + $company->company_profile = $unique_name. '.' .$ext; + // $name = $file->getClientOriginalName(); + // $img_file = $request->file('company_profile'); + // Image::make($img_file)->resize(300,300)->save(public_path('\company_profile')); + // //return $img_file; + // // $file->move('uploads/company_profile/', $img_file); + + // $input['company_profile'] = $name; + } //end if + $company->save(); + return response()->json(['company'=>$company],200); + }//end of companyProfile() + + public function regCert(Request $request, $id) { - // if($request->hasFile('avatar')) - // { - // $avatar = $request->file('avatar'); - // $filename = time() . '.' . $avatar->getClientOriginalExtension(); - // Image::make($avatar)->resize(300,300)->save(public_path('/uploads/company_profile/'.$filename)); - // } - } -} \ No newline at end of file + $company = Company::find($id); //find the company first + if (!$company) + { + return response()->json(['message'=>'Document not found'],200); + } + + //validating file types + $validator = Validator::make($request->all(), [ + 'reg_cert' => 'mimes:jpg,jpeg,bmp,png,gif,svg,pdf' + ]); + + if($validator->fails()){ + return "failed"; + } + + if($request->hasFile('reg_cert')) //Registration Certificate upload + { + $reg_cert = $request->file('reg_cert'); + $filename = $reg_cert->getClientOriginalName(); + $unique_name = 'reg_cert_' . md5($filename); + //$destinationPath = config('app.fileDestinationPath').'/'.$filename; + + $file = $request->file('reg_cert'); + $ext = $file->getClientOriginalExtension(); + $input['reg_cert'] = $filename; + Storage::putFileAs( + 'reg_cert',/*folder name*/ $file, $unique_name. '.' .$ext + ); + $company->reg_cert = $unique_name. '.' .$ext; + }//end if + $company->save(); + return response()->json(['company'=>$company],200); + }//end regCert() + +}//end of class \ No newline at end of file diff --git a/database/migrations/2018_05_08_081508_add_nullable_to_companies.php b/database/migrations/2018_05_08_081508_add_nullable_to_companies.php new file mode 100644 index 0000000..28ac581 --- /dev/null +++ b/database/migrations/2018_05_08_081508_add_nullable_to_companies.php @@ -0,0 +1,45 @@ +string('company_profile')->nullable()->change(); //need to check about storage type + $table->string('reg_cert')->nullable()->change(); + $table->string('company_name')->nullable()->change(); + $table->string('registration_no')->nullable()->change(); + $table->string('tax_no')->nullable()->change(); + $table->integer('tel_no')->nullable()->change(); + $table->integer('fax')->nullable()->change(); + $table->string('address')->nullable()->change(); + $table->string('city')->nullable()->change(); + $table->string('postcode')->nullable()->change(); + $table->string('state')->nullable()->change(); + $table->string('country')->nullable()->change(); + $table->string('contact_person')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('companies', function (Blueprint $table) { + // + }); + } +} diff --git a/public/uploads/company_profile/hello.jpg b/public/uploads/company_profile/hello.jpg deleted file mode 100644 index 470ac4c..0000000 Binary files a/public/uploads/company_profile/hello.jpg and /dev/null differ diff --git a/public/uploads/reg_cert/reg_cert.jpg b/public/uploads/reg_cert/reg_cert.jpg deleted file mode 100644 index 470ac4c..0000000 Binary files a/public/uploads/reg_cert/reg_cert.jpg and /dev/null differ diff --git a/routes/api.php b/routes/api.php index 890896e..b8f714c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -30,7 +30,8 @@ Route::group(['middleware' => ['jwt.auth']], function() { Route::post('/company', 'CompanyController@store'); //store company information Route::put('/company/{company}', 'CompanyController@update'); //update company information -//Route::post('/company', 'CompanyController@uploadImage'); +Route::post('/company/company_profile/{companyID}', 'CompanyController@companyProfile');//upload company profile picture +Route::post('/company/reg_cert/{companyID}', 'CompanyController@regCert'); //upload registration certificaate // Route::put('company/{company}', function(Request $request, $productId) { // $product = Company::findOrFail($productId);