Add support for creating projects inline

This commit is contained in:
Hillel Coren 2017-03-01 22:03:15 +02:00
parent ae7acc8602
commit e78194ec2f
4 changed files with 63 additions and 18 deletions

View File

@ -96,7 +96,7 @@ class TaskController extends BaseController
*/ */
public function store(CreateTaskRequest $request) public function store(CreateTaskRequest $request)
{ {
return $this->save(); return $this->save($request);
} }
/** /**
@ -202,7 +202,7 @@ class TaskController extends BaseController
{ {
$task = $request->entity(); $task = $request->entity();
return $this->save($task->public_id); return $this->save($request, $task->public_id);
} }
/** /**
@ -222,7 +222,7 @@ class TaskController extends BaseController
* *
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
*/ */
private function save($publicId = null) private function save($request, $publicId = null)
{ {
$action = Input::get('action'); $action = Input::get('action');
@ -230,7 +230,7 @@ class TaskController extends BaseController
return self::bulk(); return self::bulk();
} }
$task = $this->taskRepo->save($publicId, Input::all()); $task = $this->taskRepo->save($publicId, $request->input());
if ($publicId) { if ($publicId) {
Session::flash('message', trans('texts.updated_task')); Session::flash('message', trans('texts.updated_task'));

View File

@ -5,4 +5,22 @@ namespace App\Http\Requests;
class TaskRequest extends EntityRequest class TaskRequest extends EntityRequest
{ {
protected $entityType = ENTITY_TASK; protected $entityType = ENTITY_TASK;
public function sanitize()
{
$input = $this->all();
// check if we're creating a new project
if ($this->input('project_id') == '-1' && $this->user()->can('create', ENTITY_PROJECT)) {
$project = app('App\Services\ProjectService')->save([
'name' => $this->input('project_name'),
'client_id' => $this->input('client')
]);
$input['project_id'] = $project->public_id;
}
$this->replace($input);
return $this->all();
}
} }

View File

@ -2392,6 +2392,7 @@ $LANG = array(
'credit_created_by' => 'Credit created by payment :transaction_reference', 'credit_created_by' => 'Credit created by payment :transaction_reference',
'created_payment_and_credit' => 'Successfully created payment and credit', 'created_payment_and_credit' => 'Successfully created payment and credit',
'created_payment_and_credit_emailed_client' => 'Successfully created payment and credit, and emailed client', 'created_payment_and_credit_emailed_client' => 'Successfully created payment and credit, and emailed client',
'create_project' => 'Create project',
); );

View File

@ -58,7 +58,9 @@
@endif @endif
@else @else
{!! Former::select('client')->addOption('', '')->addGroupClass('client-select') !!} {!! Former::select('client')->addOption('', '')->addGroupClass('client-select') !!}
{!! Former::select('project_id')->addOption('', '')->addGroupClass('project-select') {!! Former::select('project_id')
->addOption('', '')
->addGroupClass('project-select')
->label(trans('texts.project')) !!} ->label(trans('texts.project')) !!}
@endif @endif
@ -552,6 +554,7 @@
$projectCombobox = $('select#project_id'); $projectCombobox = $('select#project_id');
$projectCombobox.find('option').remove().end().combobox('refresh'); $projectCombobox.find('option').remove().end().combobox('refresh');
$projectCombobox.append(new Option('', '')); $projectCombobox.append(new Option('', ''));
$projectCombobox.append(new Option("{{ trans('texts.create_project')}}: $name", '-1'));
var list = clientId ? (projectsForClientMap.hasOwnProperty(clientId) ? projectsForClientMap[clientId] : []).concat(projectsForAllClients) : projects; var list = clientId ? (projectsForClientMap.hasOwnProperty(clientId) ? projectsForClientMap[clientId] : []).concat(projectsForAllClients) : projects;
for (var i=0; i<list.length; i++) { for (var i=0; i<list.length; i++) {
var project = list[i]; var project = list[i];
@ -561,23 +564,46 @@
}); });
var $projectSelect = $('select#project_id').on('change', function(e) { var $projectSelect = $('select#project_id').on('change', function(e) {
$clientCombobox = $('select#client'); $clientCombobox = $('select#client');
var projectId = $('input[name=project_id]').val(); var projectId = $('input[name=project_id]').val();
if (projectId) { if (projectId == '-1') {
var project = projectMap[projectId]; $('input[name=project_name]').val(projectName);
if (project.client) { } else if (projectId) {
var client = clientMap[project.client.public_id]; // when selecting a project make sure the client is loaded
if (client) { var project = projectMap[projectId];
project.client = client; if (project && project.client) {
setComboboxValue($('.client-select'), client.public_id, getClientDisplayName(client)); var client = clientMap[project.client.public_id];
if (client) {
project.client = client;
setComboboxValue($('.client-select'), client.public_id, getClientDisplayName(client));
}
} }
} else {
$clientSelect.trigger('change');
} }
} else {
$clientSelect.trigger('change');
}
}); });
$projectSelect.combobox(); var projectName = '';
$projectSelect.combobox({
highlighter: function (item) {
if (item.indexOf("{{ trans('texts.create_project') }}") == 0) {
projectName = this.query;
return "{{ trans('texts.create_project') }}: " + this.query;
} else {
var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>';
})
}
},
template: '<div class="combobox-container"> <input type="hidden" /> <div class="input-group"> <input type="text" name="project_name" autocomplete="off" /> <span class="input-group-addon dropdown-toggle" data-dropdown="dropdown"> <span class="caret" /> <i class="fa fa-times"></i> </span> </div> </div> ',
matcher: function (item) {
if (item.indexOf("{{ trans('texts.create_project') }}") == 0) {
return this.query.length;
}
return ~item.toLowerCase().indexOf(this.query.toLowerCase());
}
});
if (projectId) { if (projectId) {
var project = projectMap[projectId]; var project = projectMap[projectId];