ORIENT/plugins/october/demo/components/Todo.php

46 lines
1.2 KiB
PHP
Raw Normal View History

2014-05-14 13:24:20 +00:00
<?php namespace October\Demo\Components;
use Cms\Classes\ComponentBase;
2015-08-07 09:28:59 +00:00
use ApplicationException;
2014-05-14 13:24:20 +00:00
class Todo extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'Todo List',
'description' => 'Implements a simple to-do list.'
];
}
public function defineProperties()
{
return [
'max' => [
'description' => 'The most amount of todo items allowed',
'title' => 'Max items',
'default' => 10,
'type' => 'string',
'validationPattern' => '^[0-9]+$',
'validationMessage' => 'The Max Items value is required and should be integer.'
]
];
}
public function onAddItem()
{
$items = post('items', []);
2014-10-18 10:49:29 +00:00
if (count($items) >= $this->property('max')) {
2015-08-07 09:28:59 +00:00
throw new ApplicationException(sprintf('Sorry only %s items are allowed.', $this->property('max')));
2014-10-18 10:49:29 +00:00
}
2014-05-14 13:24:20 +00:00
2014-10-18 10:49:29 +00:00
if (($newItem = post('newItem')) != '') {
2014-05-14 13:24:20 +00:00
$items[] = $newItem;
2014-10-18 10:49:29 +00:00
}
2014-05-14 13:24:20 +00:00
$this->page['items'] = $items;
}
2014-10-18 10:49:29 +00:00
}