From 9095d90842ddfa5fe711bd08e056da50c7e01796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Tue, 5 Jul 2016 18:30:29 +0200 Subject: [PATCH] Added tasks to activity on dashboard - Creating and updating tasks creates an activity now - Each has its own event - The activity is shown on the dashboard --- app/Events/TaskWasCreated.php | 28 +++++++++++++++++ app/Events/TaskWasUpdated.php | 28 +++++++++++++++++ app/Http/Controllers/TaskController.php | 11 ++++++- app/Http/routes.php | 4 +++ app/Listeners/ActivityListener.php | 28 +++++++++++++++++ app/Models/Activity.php | 9 ++++++ app/Models/Task.php | 10 ++++++ app/Providers/EventServiceProvider.php | 8 +++++ ...5_140904_add_task_id_to_activity_table.php | 31 +++++++++++++++++++ resources/lang/de/texts.php | 2 ++ resources/lang/en/texts.php | 2 ++ 11 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 app/Events/TaskWasCreated.php create mode 100644 app/Events/TaskWasUpdated.php create mode 100644 database/migrations/2016_07_05_140904_add_task_id_to_activity_table.php diff --git a/app/Events/TaskWasCreated.php b/app/Events/TaskWasCreated.php new file mode 100644 index 0000000000..af1fc5c27d --- /dev/null +++ b/app/Events/TaskWasCreated.php @@ -0,0 +1,28 @@ +task = $task; + } +} diff --git a/app/Events/TaskWasUpdated.php b/app/Events/TaskWasUpdated.php new file mode 100644 index 0000000000..608f57ea7c --- /dev/null +++ b/app/Events/TaskWasUpdated.php @@ -0,0 +1,28 @@ +task = $task; + } +} diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index fb0248be40..9ed66a7b95 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -1,5 +1,7 @@ taskRepo->save($publicId, Input::all()); - Session::flash('message', trans($publicId ? 'texts.updated_task' : 'texts.created_task')); + + if($publicId) { + Session::flash('message', trans('texts.updated_task')); + event(new TaskWasUpdated($task)); + } else { + Session::flash('message', trans('texts.created_task')); + event(new TaskWasCreated($task)); + } if (in_array($action, ['invoice', 'add_to_invoice'])) { return self::bulk(); diff --git a/app/Http/routes.php b/app/Http/routes.php index 9c8f3d9ef9..cf02f29e22 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -459,6 +459,10 @@ if (!defined('CONTACT_EMAIL')) { define('ACTIVITY_TYPE_ARCHIVE_EXPENSE', 35); define('ACTIVITY_TYPE_DELETE_EXPENSE', 36); define('ACTIVITY_TYPE_RESTORE_EXPENSE', 37); + + // tasks + define('ACTIVITY_TYPE_CREATE_TASK', 42); + define('ACTIVITY_TYPE_UPDATE_TASK', 43); define('DEFAULT_INVOICE_NUMBER', '0001'); define('RECENTLY_VIEWED_LIMIT', 8); diff --git a/app/Listeners/ActivityListener.php b/app/Listeners/ActivityListener.php index 9745dfaa89..191325960b 100644 --- a/app/Listeners/ActivityListener.php +++ b/app/Listeners/ActivityListener.php @@ -1,5 +1,7 @@ fromDeleted ? $payment->getCompletedAmount() : 0 ); } + + /** + * Creates an activity when a task was created + * + * @param TaskWasCreated $event + */ + public function createdTask(TaskWasCreated $event) + { + $this->activityRepo->create( + $event->task, + ACTIVITY_TYPE_CREATE_TASK + ); + } + + /** + * Creates an activity when a task was updated + * + * @param TaskWasUpdated $event + */ + public function updatedTask(TaskWasUpdated $event) + { + $this->activityRepo->create( + $event->task, + ACTIVITY_TYPE_UPDATE_TASK + ); + } } diff --git a/app/Models/Activity.php b/app/Models/Activity.php index f36e14eaeb..8db76b87c2 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -78,6 +78,11 @@ class Activity extends Eloquent return $this->belongsTo('App\Models\Payment')->withTrashed(); } + public function task() + { + return $this->belongsTo('App\Models\Task')->withTrashed(); + } + /** * @return mixed */ @@ -93,6 +98,9 @@ class Activity extends Eloquent $credit = $this->credit; $isSystem = $this->is_system; + /** @var Task $task */ + $task = $this->task; + $data = [ 'client' => link_to($client->getRoute(), $client->getDisplayName()), 'user' => $isSystem ? '' . trans('texts.system') . '' : $user->getDisplayName(), @@ -103,6 +111,7 @@ class Activity extends Eloquent 'payment_amount' => $payment ? $account->formatMoney($payment->amount, $payment) : null, 'adjustment' => $this->adjustment ? $account->formatMoney($this->adjustment, $this) : asdf, 'credit' => $credit ? $account->formatMoney($credit->amount, $client) : null, + 'task' => $task ? link_to($task->getRoute(), substr($task->description, 0, 30).'...') : null, ]; return trans("texts.activity_{$activityTypeId}", $data); diff --git a/app/Models/Task.php b/app/Models/Task.php index b8b75000fa..852c8956ba 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -131,4 +131,14 @@ class Task extends EntityModel { return round($this->getDuration() / (60 * 60), 2); } + + /** + * Gets the route to the tasks edit action + * + * @return string + */ + public function getRoute() + { + return "/tasks/{$this->public_id}/edit"; + } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 765742e4a9..0e847484ad 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -151,6 +151,14 @@ class EventServiceProvider extends ServiceProvider { 'App\Events\UserSettingsChanged' => [ 'App\Listeners\HandleUserSettingsChanged', ], + + // Task events + 'App\Events\TaskWasCreated' => [ + 'App\Listeners\ActivityListener@createdTask', + ], + 'App\Events\TaskWasUpdated' => [ + 'App\Listeners\ActivityListener@updatedTask', + ], ]; /** diff --git a/database/migrations/2016_07_05_140904_add_task_id_to_activity_table.php b/database/migrations/2016_07_05_140904_add_task_id_to_activity_table.php new file mode 100644 index 0000000000..9f3f03ab7f --- /dev/null +++ b/database/migrations/2016_07_05_140904_add_task_id_to_activity_table.php @@ -0,0 +1,31 @@ +integer('task_id')->after('invitation_id')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('activities', function (Blueprint $table) { + $table->dropColumn('task_id'); + }); + } +} diff --git a/resources/lang/de/texts.php b/resources/lang/de/texts.php index 655902c0e9..6cfbb74461 100644 --- a/resources/lang/de/texts.php +++ b/resources/lang/de/texts.php @@ -775,6 +775,8 @@ $LANG = array( 'activity_35' => ':user created :vendor', 'activity_36' => ':user created :vendor', 'activity_37' => ':user created :vendor', + 'activity_42' => ':user erstellte Aufgabe ":task"', + 'activity_43' => ':user aktualisierte Aufgabe ":task"', 'payment' => 'Zahlung', 'system' => 'System', 'signature' => 'Email-Signatur', diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 09c2dcd69b..a348b4b1c1 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -774,6 +774,8 @@ $LANG = array( 'activity_35' => ':user created :vendor', 'activity_36' => ':user created :vendor', 'activity_37' => ':user created :vendor', + 'activity_42' => ':user created task ":task"', + 'activity_43' => ':user updated task ":task"', 'payment' => 'Payment', 'system' => 'System', 'signature' => 'Email Signature',