diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index 5e1c19f934f2..fef5820e2573 100755 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -1,7 +1,18 @@ accountRepo = $accountRepo; + } + public function getStarted() { if (Auth::check()) @@ -45,6 +56,13 @@ class AccountController extends \BaseController { return Redirect::to('invoices/create'); } + public function getSearchData() + { + $data = $this->accountRepo->getSearchData(); + + return Response::json($data); + } + public function showSection($section = ACCOUNT_DETAILS) { if ($section == ACCOUNT_DETAILS) diff --git a/app/handlers/InvoiceEventHandler.php b/app/handlers/InvoiceEventHandler.php index bbce7793b174..c7952f77efea 100755 --- a/app/handlers/InvoiceEventHandler.php +++ b/app/handlers/InvoiceEventHandler.php @@ -25,12 +25,12 @@ class InvoiceEventHandler public function onViewed($invoice) { - + $this->sendNotifications($invoice, 'viewed'); } public function onPaid($invoice) { - + $this->sendNotifications($invoice, 'paid'); } private function sendNotifications($invoice, $type) diff --git a/app/ninja/repositories/AccountRepository.php b/app/ninja/repositories/AccountRepository.php new file mode 100644 index 000000000000..e3d808ffa64b --- /dev/null +++ b/app/ninja/repositories/AccountRepository.php @@ -0,0 +1,43 @@ +where('clients.deleted_at', '=', null) + ->select(\DB::raw("'Clients' as type, clients.public_id, clients.name")); + + $contacts = \DB::table('clients') + ->join('contacts', 'contacts.client_id', '=', 'clients.id') + ->where('clients.deleted_at', '=', null) + ->select(\DB::raw("'Contacts' as type, clients.public_id, CONCAT(contacts.first_name, ' ', contacts.last_name) as name")); + + $invoices = \DB::table('clients') + ->join('invoices', 'invoices.client_id', '=', 'clients.id') + ->where('clients.deleted_at', '=', null) + ->where('invoices.deleted_at', '=', null) + ->select(\DB::raw("'Invoices' as type, invoices.public_id, CONCAT(invoices.invoice_number, ': ', clients.name) as name")); + + + $data = []; + + foreach ($clients->union($contacts)->union($invoices)->get() as $row) + { + if (!isset($data[$row->type])) + { + $data[$row->type] = []; + } + + $data[$row->type][] = [ + 'value' => $row->name, + 'public_id' => $row->public_id + ]; + } + + return $data; + } +} \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index e85da0b2b53c..27f11eed579a 100755 --- a/app/routes.php +++ b/app/routes.php @@ -58,7 +58,8 @@ Route::filter('auth', function() Route::group(array('before' => 'auth'), function() { Route::get('home', function() { return View::make('header'); }); - Route::get('account/{section?}', 'AccountController@showSection'); + Route::get('account/getSearchData', array('as' => 'getSearchData', 'uses' => 'AccountController@getSearchData')); + Route::get('account/{section?}', 'AccountController@showSection'); Route::post('account/{section?}', 'AccountController@doSection'); Route::post('user/setTheme', 'UserController@setTheme'); diff --git a/app/views/header.blade.php b/app/views/header.blade.php index eb0b42887167..8fb262269b5e 100755 --- a/app/views/header.blade.php +++ b/app/views/header.blade.php @@ -28,6 +28,10 @@ + + + + @@ -485,7 +489,30 @@ } } - $(function() { + $(function() + { + $('#search').focus(function(){ + if (!window.hasOwnProperty('searchData')) { + $.get('{{ URL::route('getSearchData') }}', function(data) { + window.searchData = true; + var datasets = []; + for (var type in data) + { + if (!data.hasOwnProperty(type)) continue; + datasets.push({ + name: type, + header: ' ' + type + '', + local: data[type] + }); + } + $('#search').typeahead(datasets).on('typeahead:selected', function(element, datum, name) { + var type = name == 'Contacts' ? 'clients' : name.toLowerCase(); + window.location = '{{ URL::to('/') }}' + '/' + type + '/' + datum.public_id; + }).focus().typeahead('setQuery', $('#search').val()); + }); + } + }); + if (isStorageSupported()) { @if (Auth::check() && !Auth::user()->registered) diff --git a/public/js/script.js b/public/js/script.js index 403d8cea46b7..a235c8367ab4 100755 --- a/public/js/script.js +++ b/public/js/script.js @@ -641,7 +641,6 @@ function wordWrapText(value, width) return lines.slice(0, 6).join("\n"); } - var CONSTS = {}; CONSTS.INVOICE_STATUS_DRAFT = 1; CONSTS.INVOICE_STATUS_SENT = 2;