diff --git a/app/controllers/ClientController.php b/app/controllers/ClientController.php index 57dd8a359026..8b5083d7115b 100755 --- a/app/controllers/ClientController.php +++ b/app/controllers/ClientController.php @@ -62,42 +62,7 @@ class ClientController extends \BaseController { */ public function store() { - $rules = array( - 'name' => 'required', - 'email' => 'email' - ); - $validator = Validator::make(Input::all(), $rules); - - if ($validator->fails()) { - return Redirect::to('clients/create') - ->withErrors($validator) - ->withInput(Input::except('password')); - } else { - $client = new Client; - $client->account_id = Auth::user()->account_id; - $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'); - $client->save(); - - $contact = new Contact; - $contact->email = Input::get('email'); - $contact->first_name = Input::get('first_name'); - $contact->last_name = Input::get('last_name'); - $contact->phone = Input::get('phone'); - $client->contacts()->save($contact); - - $url = 'clients/' . $client->id; - processedRequest($url); - - Session::flash('message', 'Successfully created client'); - return Redirect::to($url); - } + return $this->save(); } /** @@ -120,7 +85,7 @@ class ClientController extends \BaseController { */ public function edit($id) { - $client = Client::find($id); + $client = Client::with('contacts')->find($id); $data = array('client' => $client, 'method' => 'PUT', 'url' => 'clients/' . $id, 'title' => 'Edit'); return View::make('clients.edit', $data); } @@ -132,6 +97,11 @@ class ClientController extends \BaseController { * @return Response */ public function update($id) + { + return $this->save($id); + } + + private function save($id = null) { $rules = array( 'name' => 'required' @@ -143,7 +113,12 @@ class ClientController extends \BaseController { ->withErrors($validator) ->withInput(Input::except('password')); } else { - $client = Client::find($id); + if ($id) { + $client = Client::find($id); + } else { + $client = new Client; + } + $client->name = Input::get('name'); $client->work_phone = Input::get('work_phone'); $client->address1 = Input::get('address1'); @@ -154,12 +129,36 @@ class ClientController extends \BaseController { $client->postal_code = Input::get('postal_code'); $client->save(); - $contact = $client->contacts[0]; - $contact->email = Input::get('email'); - $contact->first_name = Input::get('first_name'); - $contact->last_name = Input::get('last_name'); - $contact->phone = Input::get('phone'); - $contact->save(); + $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(); + } + } Session::flash('message', 'Successfully updated client'); return Redirect::to('clients'); diff --git a/app/controllers/InvoiceController.php b/app/controllers/InvoiceController.php index e8cfd505a567..f0edded2ee80 100755 --- a/app/controllers/InvoiceController.php +++ b/app/controllers/InvoiceController.php @@ -172,11 +172,23 @@ class InvoiceController extends \BaseController { } } - /** - * Show the form for creating a new resource. - * - * @return Response - */ + + public function edit($id) + { + $invoice = Invoice::with('client', 'invoice_items')->find($id); + + $data = array( + 'invoice' => $invoice, + 'method' => 'PUT', + 'url' => 'invoices/' . $id, + 'title' => 'Edit', + 'account' => Auth::user()->account, + 'products' => Product::getProducts()->get(), + 'client' => $invoice->client, + 'clients' => Client::where('account_id','=',Auth::user()->account_id)->orderBy('name')->get()); + return View::make('invoices.edit', $data); + } + public function create($clientId = 0) { $client = null; @@ -193,7 +205,7 @@ class InvoiceController extends \BaseController { 'items' => json_decode(Input::old('items')), 'account' => Auth::user()->account, 'products' => Product::getProducts()->get(), - 'clients' => Client::where('account_id','=',Auth::user()->account_id)->get()); + 'clients' => Client::where('account_id','=',Auth::user()->account_id)->orderBy('name')->get()); return View::make('invoices.edit', $data); } @@ -335,28 +347,6 @@ class InvoiceController extends \BaseController { return View::make('invoices.show')->with('invoice', $invoice); } - /** - * Show the form for editing the specified resource. - * - * @param int $id - * @return Response - */ - public function edit($id) - { - $invoice = Invoice::with('client', 'invoice_items')->find($id); - - $data = array( - 'invoice' => $invoice, - 'method' => 'PUT', - 'url' => 'invoices/' . $id, - 'title' => 'Edit', - 'account' => Auth::user()->account, - 'products' => Product::getProducts()->get(), - 'client' => $invoice->client, - 'clients' => Client::where('account_id','=',Auth::user()->account_id)->get()); - return View::make('invoices.edit', $data); - } - /** * Update the specified resource in storage. * diff --git a/app/views/clients/edit.blade.php b/app/views/clients/edit.blade.php index a02daaeab031..5a98d5f86e28 100755 --- a/app/views/clients/edit.blade.php +++ b/app/views/clients/edit.blade.php @@ -10,15 +10,15 @@ - @if ($client) - {{ Former::populate($client); }} - @endif - {{ Former::open($url)->addClass('col-md-9 col-md-offset-1 main_form')->method($method)->rules(array( 'name' => 'required', 'email' => 'email' )); }} + @if ($client) + {{ Former::populate($client) }} + @endif + {{ Former::legend('Organization') }} {{ Former::text('name') }} @@ -26,19 +26,72 @@ {{ Former::textarea('notes') }} {{ Former::legend('Contacts') }} - {{ Former::text('first_name') }} - {{ Former::text('last_name') }} - {{ Former::text('email') }} - {{ Former::text('phone') }} +
+ {{ Former::hidden('id')->data_bind("value: id, valueUpdate: 'afterkeydown'") }} + {{ Former::text('first_name')->data_bind("value: first_name, valueUpdate: 'afterkeydown'") }} + {{ Former::text('last_name')->data_bind("value: last_name, valueUpdate: 'afterkeydown'") }} + {{ Former::text('email')->data_bind("value: email, valueUpdate: 'afterkeydown'") }} + {{ Former::text('phone')->data_bind("value: phone, valueUpdate: 'afterkeydown'") }} +
+
+ + {{ link_to('#', 'Add contact', array('onclick'=>'return addContact()')) }} + + + {{ link_to('#', 'Remove contact', array('data-bind'=>'click: $parent.removeContact')) }} + +
+
+
+ +
+ {{ Former::legend('Address') }} - {{ Former::text('address1')->label('Street') }} - {{ Former::text('address2')->label('Apt/Floor') }} + {{ Former::text('address1') }} + {{ Former::text('address2') }} {{ Former::text('city') }} {{ Former::text('state') }} {{ Former::text('postal_code') }} + {{ Former::hidden('data')->data_bind("value: ko.toJSON(model)") }} + {{ Former::actions( Button::lg_primary_submit('Save') ) }} {{ Former::close() }} + + @stop \ No newline at end of file diff --git a/app/views/header.blade.php b/app/views/header.blade.php index ab77cec6fbeb..6c9bb45b1345 100755 --- a/app/views/header.blade.php +++ b/app/views/header.blade.php @@ -38,6 +38,7 @@ + diff --git a/public/js/knockout.mapping-latest.js b/public/js/knockout.mapping-latest.js new file mode 100755 index 000000000000..8d1d51260c9e --- /dev/null +++ b/public/js/knockout.mapping-latest.js @@ -0,0 +1,22 @@ +/// Knockout Mapping plugin v2.4.1 +/// (c) 2013 Steven Sanderson, Roy Jacobs - http://knockoutjs.com/ +/// License: MIT (http://www.opensource.org/licenses/mit-license.php) +(function(e){"function"===typeof require&&"object"===typeof exports&&"object"===typeof module?e(require("knockout"),exports):"function"===typeof define&&define.amd?define(["knockout","exports"],e):e(ko,ko.mapping={})})(function(e,f){function y(b,c){var a,d;for(d in c)if(c.hasOwnProperty(d)&&c[d])if(a=f.getType(b[d]),d&&b[d]&&"array"!==a&&"string"!==a)y(b[d],c[d]);else if("array"===f.getType(b[d])&&"array"===f.getType(c[d])){a=b;for(var e=d,l=b[d],n=c[d],t={},g=l.length-1;0<=g;--g)t[l[g]]=l[g];for(g= +n.length-1;0<=g;--g)t[n[g]]=n[g];l=[];n=void 0;for(n in t)l.push(t[n]);a[e]=l}else b[d]=c[d]}function E(b,c){var a={};y(a,b);y(a,c);return a}function z(b,c){for(var a=E({},b),e=L.length-1;0<=e;e--){var f=L[e];a[f]&&(a[""]instanceof Object||(a[""]={}),a[""][f]=a[f],delete a[f])}c&&(a.ignore=h(c.ignore,a.ignore),a.include=h(c.include,a.include),a.copy=h(c.copy,a.copy),a.observe=h(c.observe,a.observe));a.ignore=h(a.ignore,j.ignore);a.include=h(a.include,j.include);a.copy=h(a.copy,j.copy);a.observe=h(a.observe, +j.observe);a.mappedProperties=a.mappedProperties||{};a.copiedProperties=a.copiedProperties||{};return a}function h(b,c){"array"!==f.getType(b)&&(b="undefined"===f.getType(b)?[]:[b]);"array"!==f.getType(c)&&(c="undefined"===f.getType(c)?[]:[c]);return e.utils.arrayGetDistinctValues(b.concat(c))}function F(b,c,a,d,k,l,n){var t="array"===f.getType(e.utils.unwrapObservable(c));l=l||"";if(f.isMapped(b)){var g=e.utils.unwrapObservable(b)[p];a=E(g,a)}var j=n||k,h=function(){return a[d]&&a[d].create instanceof +Function},x=function(b){var f=G,g=e.dependentObservable;e.dependentObservable=function(a,b,c){c=c||{};a&&"object"==typeof a&&(c=a);var d=c.deferEvaluation,M=!1;c.deferEvaluation=!0;a=new H(a,b,c);if(!d){var g=a,d=e.dependentObservable;e.dependentObservable=H;a=e.isWriteableObservable(g);e.dependentObservable=d;d=H({read:function(){M||(e.utils.arrayRemoveItem(f,g),M=!0);return g.apply(g,arguments)},write:a&&function(a){return g(a)},deferEvaluation:!0});d.__DO=g;a=d;f.push(a)}return a};e.dependentObservable.fn= +H.fn;e.computed=e.dependentObservable;b=e.utils.unwrapObservable(k)instanceof Array?a[d].create({data:b||c,parent:j,skip:N}):a[d].create({data:b||c,parent:j});e.dependentObservable=g;e.computed=e.dependentObservable;return b},u=function(){return a[d]&&a[d].update instanceof Function},v=function(b,f){var g={data:f||c,parent:j,target:e.utils.unwrapObservable(b)};e.isWriteableObservable(b)&&(g.observable=b);return a[d].update(g)};if(n=I.get(c))return n;d=d||"";if(t){var t=[],s=!1,m=function(a){return a}; +a[d]&&a[d].key&&(m=a[d].key,s=!0);e.isObservable(b)||(b=e.observableArray([]),b.mappedRemove=function(a){var c="function"==typeof a?a:function(b){return b===m(a)};return b.remove(function(a){return c(m(a))})},b.mappedRemoveAll=function(a){var c=C(a,m);return b.remove(function(a){return-1!=e.utils.arrayIndexOf(c,m(a))})},b.mappedDestroy=function(a){var c="function"==typeof a?a:function(b){return b===m(a)};return b.destroy(function(a){return c(m(a))})},b.mappedDestroyAll=function(a){var c=C(a,m);return b.destroy(function(a){return-1!= +e.utils.arrayIndexOf(c,m(a))})},b.mappedIndexOf=function(a){var c=C(b(),m);a=m(a);return e.utils.arrayIndexOf(c,a)},b.mappedGet=function(a){return b()[b.mappedIndexOf(a)]},b.mappedCreate=function(a){if(-1!==b.mappedIndexOf(a))throw Error("There already is an object with the key that you specified.");var c=h()?x(a):a;u()&&(a=v(c,a),e.isWriteableObservable(c)?c(a):c=a);b.push(c);return c});n=C(e.utils.unwrapObservable(b),m).sort();g=C(c,m);s&&g.sort();s=e.utils.compareArrays(n,g);n={};var J,A=e.utils.unwrapObservable(c), +y={},z=!0,g=0;for(J=A.length;g