1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/controllers/ClientController.php

196 lines
4.4 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
class ClientController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//$clients = Client::orderBy('name')->get();
//return View::make('clients.index')->with('clients', $clients);
return View::make('clients.index');
}
public function getDatatable()
{
return Datatable::collection(Client::with('contacts')->where('account_id','=',Auth::user()->account_id)->get())
->addColumn('name', function($model)
{
//return $model->name;
return link_to('clients/' . $model->id, $model->name);
})
->addColumn('contact', function($model)
{
2013-11-26 22:45:10 +01:00
return $model->contacts[0]->getFullName();
2013-11-26 13:45:07 +01:00
})
->addColumn('last_login', function($model)
{
return $model->contacts[0]->lastLogin();
})
->addColumn('email', function($model)
{
//return $model->contacts[0]->email;
return HTML::mailto($model->contacts[0]->email, $model->contacts[0]->email);
})
->addColumn('phone', function($model)
{
return $model->contacts[0]->phone;
})
->orderColumns('name')
->make();
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
2013-11-28 22:10:01 +01:00
$data = array(
'client' => null,
'method' => 'POST',
'url' => 'clients',
'title' => 'New',
'countries' => Country::orderBy('name')->get());
2013-11-26 13:45:07 +01:00
return View::make('clients.edit', $data);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
2013-11-28 20:06:38 +01:00
return $this->save();
2013-11-26 13:45:07 +01:00
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$client = Client::find($id);
return View::make('clients.show')->with('client', $client);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
2013-11-28 20:06:38 +01:00
$client = Client::with('contacts')->find($id);
2013-11-28 22:10:01 +01:00
$data = array(
'client' => $client,
'method' => 'PUT',
'url' => 'clients/' . $id,
'title' => 'Edit',
'countries' => Country::orderBy('name')->get());
2013-11-26 13:45:07 +01:00
return View::make('clients.edit', $data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
2013-11-28 20:06:38 +01:00
{
return $this->save($id);
}
private function save($id = null)
2013-11-26 13:45:07 +01:00
{
$rules = array(
'name' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('clients/' . $id . '/edit')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
2013-11-28 20:06:38 +01:00
if ($id) {
$client = Client::find($id);
} else {
$client = new Client;
}
2013-11-28 17:40:13 +01:00
$client->name = Input::get('name');
$client->work_phone = Input::get('work_phone');
$client->address1 = Input::get('address1');
$client->address2 = Input::get('address2');
$client->city = Input::get('city');
$client->state = Input::get('state');
$client->notes = Input::get('notes');
$client->postal_code = Input::get('postal_code');
2013-11-28 22:10:01 +01:00
$client->country_id = Input::get('country_id');
2013-11-26 13:45:07 +01:00
$client->save();
2013-11-28 20:06:38 +01:00
$data = json_decode(Input::get('data'));
$contactIds = [];
foreach ($data->contacts as $contact)
{
if (isset($contact->id) && $contact->id)
{
$record = Contact::find($contact->id);
}
else
{
$record = new Contact;
}
$record->email = $contact->email;
$record->first_name = $contact->first_name;
$record->last_name = $contact->last_name;
$record->phone = $contact->phone;
$client->contacts()->save($record);
$contactIds[] = $record->id;
}
foreach ($client->contacts as $contact)
{
if (!in_array($contact->id, $contactIds))
{
$contact->forceDelete();
}
}
2013-11-28 17:40:13 +01:00
2013-11-26 13:45:07 +01:00
Session::flash('message', 'Successfully updated client');
2013-11-28 22:10:01 +01:00
return Redirect::to('clients/' . $client->id);
2013-11-26 13:45:07 +01:00
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$client = Client::find($id);
$client->delete();
// redirect
Session::flash('message', 'Successfully deleted the client');
return Redirect::to('clients');
}
}