diff --git a/app/Bus/Commands/Metric/CreateMetricCommand.php b/app/Bus/Commands/Metric/CreateMetricCommand.php index dc1ed90d..0a1cc82f 100644 --- a/app/Bus/Commands/Metric/CreateMetricCommand.php +++ b/app/Bus/Commands/Metric/CreateMetricCommand.php @@ -111,7 +111,7 @@ final class CreateMetricCommand 'display_chart' => 'nullable|int', 'places' => 'nullable|int|between:0,4', 'default_view' => 'required|int|between:0,3', - 'threshold' => 'nullable|numeric|between:0,10', + 'threshold' => 'required|int', 'order' => 'nullable|int', 'visible' => 'required|int|between:0,2', ]; diff --git a/app/Bus/Commands/Metric/UpdateMetricCommand.php b/app/Bus/Commands/Metric/UpdateMetricCommand.php index 55939afb..9ef77011 100644 --- a/app/Bus/Commands/Metric/UpdateMetricCommand.php +++ b/app/Bus/Commands/Metric/UpdateMetricCommand.php @@ -114,7 +114,7 @@ final class UpdateMetricCommand 'display_chart' => 'nullable|int', 'places' => 'nullable|numeric|between:0,4', 'default_view' => 'nullable|numeric|between:0,4', - 'threshold' => 'nullable|numeric|between:0,10', + 'threshold' => 'nullable|int', 'order' => 'nullable|int', 'visible' => 'required|int|between:0,2', ]; diff --git a/app/Http/Controllers/Dashboard/MetricController.php b/app/Http/Controllers/Dashboard/MetricController.php index d87fb436..94f8379d 100644 --- a/app/Http/Controllers/Dashboard/MetricController.php +++ b/app/Http/Controllers/Dashboard/MetricController.php @@ -45,7 +45,8 @@ class MetricController extends Controller public function showAddMetric() { return View::make('dashboard.metrics.add') - ->withPageTitle(trans('dashboard.metrics.add.title').' - '.trans('dashboard.dashboard')); + ->withPageTitle(trans('dashboard.metrics.add.title').' - '.trans('dashboard.dashboard')) + ->withAcceptableThresholds(Metric::ACCEPTABLE_THRESHOLDS); } /** @@ -131,7 +132,8 @@ class MetricController extends Controller { return View::make('dashboard.metrics.edit') ->withPageTitle(trans('dashboard.metrics.edit.title').' - '.trans('dashboard.dashboard')) - ->withMetric($metric); + ->withMetric($metric) + ->withAcceptableThresholds(Metric::ACCEPTABLE_THRESHOLDS); } /** diff --git a/app/Models/Metric.php b/app/Models/Metric.php index 5ddeafc5..2e23f940 100644 --- a/app/Models/Metric.php +++ b/app/Models/Metric.php @@ -12,10 +12,12 @@ namespace CachetHQ\Cachet\Models; use AltThree\Validator\ValidatingTrait; +use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Models\Traits\SortableTrait; use CachetHQ\Cachet\Presenters\MetricPresenter; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\MessageBag; use McCool\LaravelAutoPresenter\HasPresenter; class Metric extends Model implements HasPresenter @@ -57,6 +59,13 @@ class Metric extends Model implements HasPresenter */ const VISIBLE_HIDDEN = 2; + /** + * Array of acceptable threshold minutes. + * + * @var int[] + */ + const ACCEPTABLE_THRESHOLDS = [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]; + /** * The model's attributes. * @@ -200,6 +209,26 @@ class Metric extends Model implements HasPresenter return $this->display_chart; } + /** + * Validate the model before save. + * + * @throws \AltThree\Validator\ValidationException + * + * @return void + */ + public function validate() + { + $messages = []; + + if (60 % $this->threshold === 0) { + $messages[] = 'Threshold must be divisible by 60.'; + } + + if ($messages) { + throw new ValidationException(new MessageBag($messages)); + } + } + /** * Get the presenter class. * diff --git a/resources/views/dashboard/metrics/add.blade.php b/resources/views/dashboard/metrics/add.blade.php index 9cdf4ee6..4f5a0ca1 100644 --- a/resources/views/dashboard/metrics/add.blade.php +++ b/resources/views/dashboard/metrics/add.blade.php @@ -57,7 +57,11 @@