<?php
class ProjectsController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$projects = Project::all();
return View::make('projects.home')->with('projects', $projects);
}
public function fetchProject($url = null)
{
$session = curl_init('http://simpledeveloper.com/');
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
return $json;
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return View::make('projects.add');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$rules = array(
'title' => 'required|min:5',
'link' => 'required|url',
'language' => 'required'
);
$data = Input::all();
$validator = Validator::make($data, $rules);
if($validator->fails())
{
return Redirect::to('projects/create')
->withErrors($validator)
->withInput(Input::all());
}else
{
$project = Project::firstOrCreate(
array(
'user_id' => Auth::id(),
'title' => Input::get('title'),
'link' => Input::get('link'),
'language' => Input::get('language')
));
if($project->save())
{
return Redirect::to('projects');
}else{
return Redirect::back()->with('errors', 'Something horrible happened');
}
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$project = Project::find($id);
return View::make('projects.project')->with('project', $project);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$project = Project::find($id);
return View::make('projects.edit')->with('project', $project);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$project = Project::find($id);
$rules = array(
'title' => 'required|min:5',
'link' => 'required|url',
'language' => 'required'
);
$data = Input::all();
$validator = Validator::make($data, $rules);
if($validator->fails())
{
return Redirect::to('projects/' . $id . '/edit')
->withErrors($validator)
->withInput(Input::all());
}else
{
$project->user_id = Auth::id();
$project->title = Input::get('title');
$project->link = Input::get('link');
$project->language = Input::get('language');
if($project->save())
{
Session::flash('message', 'You have successfully updated '. $project->title);
return Redirect::to('projects');
}else{
return Redirect::back();
}
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$project = Project::find($id);
$project->delete();
Session::flash('message', 'Successfully deleted the project!');
return Redirect::to('projects');
}
}