diff --git a/app/Http/Requests/TaskRequest.php b/app/Http/Requests/TaskRequest.php index c7d087d5c7..50afe37c7c 100644 --- a/app/Http/Requests/TaskRequest.php +++ b/app/Http/Requests/TaskRequest.php @@ -13,6 +13,17 @@ class TaskRequest extends EntityRequest { $input = $this->all(); + // check if we're creating a new client + if ($this->client_id == '-1') { + $client = [ + 'name' => trim($this->client_name), + ]; + if (Client::validate($client) === true) { + $client = app('App\Ninja\Repositories\ClientRepository')->save($client); + $input['client_id'] = $this->client_id = $client->public_id; + } + } + // check if we're creating a new project if ($this->project_id == '-1') { $project = [ diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 85ff821f12..880a687ae3 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2458,6 +2458,7 @@ $LANG = array( 'discard_changes' => 'Discard Changes', 'tasks_not_enabled' => 'Tasks are not enabled.', 'started_task' => 'Successfully started task', + 'create_client' => 'Create Client', ); diff --git a/resources/views/partials/entity_combobox.blade.php b/resources/views/partials/entity_combobox.blade.php index 67cd397388..85a8dd3ea9 100644 --- a/resources/views/partials/entity_combobox.blade.php +++ b/resources/views/partials/entity_combobox.blade.php @@ -2,6 +2,7 @@ var {{ $entityType }}Name = ''; ${{ $entityType }}Select.combobox({ highlighter: function (item) { + console.log(item); if (item.indexOf("{{ trans("texts.create_{$entityType}") }}") == 0) { {{ $entityType }}Name = this.query; return "{{ trans("texts.create_{$entityType}") }}: " + this.query; diff --git a/resources/views/tasks/time_tracker.blade.php b/resources/views/tasks/time_tracker.blade.php index 005a37d0eb..f0aad2dcc6 100644 --- a/resources/views/tasks/time_tracker.blade.php +++ b/resources/views/tasks/time_tracker.blade.php @@ -242,26 +242,58 @@ var projectMap = {}; var projectsForClientMap = {}; + function refreshClientList() { + var $clientSelect = $('select#client_id'); + $clientSelect.find('option').remove().end().combobox('refresh'); + $clientSelect.append(new Option('', '')); + + @if (Auth::user()->can('create', ENTITY_CLIENT)) + $clientSelect.append(new Option("{{ trans('texts.create_client')}}: $name", '-1')); + @endif + + for (var i=0; ican('create', ENTITY_PROJECT)) if (clientId) { $projectCombobox.append(new Option("{{ trans('texts.create_project')}}: $name", '-1')); } @endif - var list = (clientId && ! forceClear) ? (projectsForClientMap.hasOwnProperty(clientId) ? projectsForClientMap[clientId] : []) : projects; + if (clientId && ! forceClear) { + var list = projectsForClientMap.hasOwnProperty(clientId) ? projectsForClientMap[clientId] : []; + console.log('client list: ' + clientId); + } else { + var list = projects; + } for (var i=0; i ENTITY_CLIENT]) @include('partials/entity_combobox', ['entityType' => ENTITY_PROJECT]) + refreshClientList(); $clientSelect.trigger('change'); window.model = new ViewModel(); @@ -363,7 +384,6 @@ var taskId = localStorage.getItem('last:time_tracker_task'); var task = model.taskById(taskId); if (task) { - console.log(task); setTimeout(function() { model.selectTask(task); }, 1); diff --git a/resources/views/tasks/time_tracker_knockout.blade.php b/resources/views/tasks/time_tracker_knockout.blade.php index f411c02457..d796f14aa5 100644 --- a/resources/views/tasks/time_tracker_knockout.blade.php +++ b/resources/views/tasks/time_tracker_knockout.blade.php @@ -23,7 +23,7 @@ } self.onFilterClick = function() { - console.log('filter...'); + console.log('filter clicked...'); } self.onRefreshClick = function() { @@ -425,6 +425,16 @@ success: function(response) { console.log(response); if (isSelected) { + var isNew = !self.public_id(); + self.update(response); + model.formChanged(false); + var clientId = $('input[name=client_id]').val(); + if (clientId) { + var client = response.client; + clients.push(client); + addClientToMaps(client); + refreshClientList(); + } var projectId = $('input[name=project_id]').val(); if (projectId == -1) { var project = response.project; @@ -433,9 +443,6 @@ addProjectToMaps(project); refreshProjectList(); } - var isNew = !self.public_id(); - self.update(response); - model.formChanged(false); if (isNew) { toastr.success("{{ trans('texts.created_task') }}"); } else { @@ -687,7 +694,7 @@ function ClientModel(data) { var self = this; - self.public_id = ko.observable(0); + self.public_id = ko.observable(-1); self.name = ko.observable(''); self.contacts = ko.observableArray();