File: /home/mmickelson/spilicensing/app/Http/Controllers/LicenseController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\License;
use App\LicenseType;
use App\Product;
use App\Purchaser;
use App\Subproduct;
use App\Vendor;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Session;
class LicenseController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$license = License::orderBy('created_at', 'desc')->paginate(15);
$batchNum = License::max('batch');
return view('license.index', compact('license', 'batchNum'));
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$licensetype = LicenseType::lists('licenseType', 'id');
$product = Product::lists('productName', 'id');
$subproduct = Subproduct::lists('subproductName', 'id');
$vendor = Vendor::lists('vendorName', 'id');
return view('license.create', compact('licensetype', 'product', 'subproduct', 'vendor'));
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(Request $request)
{
$batch = (int)License::max('batch');
// make sure a null value is entered - is this really necessary?
$request['expiration'] = $request['expiration']?:null;
for ($i=0; $i < $request['numLicenses']; $i++) {
License::create(array_merge($request->all(), ['licenseKey' => $this->generateLicenseKey(), 'batch' => $batch+1]));
}
Session::flash('flash_message', 'License added!');
return redirect('license');
}
/**
* Display the specified resource.
*
* @param int $id
*
* @return Response
*/
public function show($id)
{
$license = License::findOrFail($id);
return view('license.show', compact('license'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$license = License::findOrFail($id);
$licensetype = LicenseType::lists('licenseType', 'id');
$product = Product::lists('productName', 'id');
$subproduct = Subproduct::lists('subproductName', 'id');
$vendor = Vendor::lists('vendorName', 'id');
$purchaser = Purchaser::lists('contactName', 'id');
return view('license.edit', compact('license', 'licensetype', 'product', 'subproduct', 'vendor', 'purchaser'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
*
* @return Response
*/
public function update($id, Request $request)
{
$license = License::findOrFail($id);
$license->update($request->all());
Session::flash('flash_message', 'License updated!');
return redirect('license');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
License::destroy($id);
Session::flash('flash_message', 'License deleted!');
return redirect('license');
}
/**
* Generates a license key
*/
public function generateLicenseKey() {
$tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$segment_chars = 5;
$num_segments = 4;
$key_string = '';
for ($i = 0; $i < $num_segments; $i++) {
$segment = '';
for ($j = 0; $j < $segment_chars; $j++) {
$segment .= $tokens[rand(0, 35)];
}
$key_string .= $segment;
if ($i <= $num_segments-1) {
$key_string .= '-';
}
}
$key_string .= strtoupper(substr(md5($key_string), 0, 5));
return $key_string;
}
}