diff --git a/app/components/form/FilterTasks.php b/app/components/form/FilterTasks.php
new file mode 100644
index 0000000..ad16113
--- /dev/null
+++ b/app/components/form/FilterTasks.php
@@ -0,0 +1,76 @@
+taskRepository = $taskRepository;
+ }
+
+ public function render()
+ {
+ $template = $this->template;
+ $template->setFile(__DIR__ . '/templates/FilterTasks.latte');
+ $template->render();
+ }
+
+ /**
+ * @param int $idTaskGroup
+ */
+ public function setTaskGroupId($idTaskGroup)
+ {
+ $this->idTaskGroup = $idTaskGroup;
+ }
+
+ /**
+ * @param array $filter
+ */
+ public function setFilter(array $filter)
+ {
+ $this->filter = $filter;
+ }
+
+ /**
+ * @return Form
+ */
+ protected function createComponentFilterTasksForm()
+ {
+ $form = new Form();
+ $form->addText('name', 'Name');
+ $form->addSubmit('submit', 'Filter');
+ $form->onSuccess[] = array($this, 'filterTasksFormSuccess');
+
+ $form->setDefaults($this->filter);
+
+ return $form;
+ }
+
+ /**
+ * @param Form $form
+ * @param $values
+ */
+ public function filterTasksFormSuccess(Form $form, $values)
+ {
+ $this->presenter->redirect('this', array('filter' => (array) $values));
+ }
+}
diff --git a/app/components/form/InsertTask.php b/app/components/form/InsertTask.php
index 9997c70..594ceaf 100644
--- a/app/components/form/InsertTask.php
+++ b/app/components/form/InsertTask.php
@@ -5,6 +5,7 @@
use App\Model\Entity\TaskGroup;
use App\Model\Repository\TaskRepository;
use App\Model\Repository\TaskGroupRepository;
+use App\Presenters\TaskPresenter;
use Nette\Application\UI\Control;
use Nette\Application\UI\Form;
@@ -53,9 +54,11 @@ protected function createComponentInsertTaskForm()
->setRequired('Please fill task name');
$form->addText('date', 'Date')
->setRequired('Please fill task date');
- $form->addHidden('idTaskGroup', $this->idTaskGroup);
+ $form->addSelect('taskGroup', 'Task group', $this->taskGroupRepository->getSelect())
+ ->setRequired('Please select task group')
+ ->setDefaultValue($this->idTaskGroup);
$form->addSubmit('submit', 'Add');
- $form->onSuccess[] = array($this, 'insertTaskFromSuccess');
+ $form->onSuccess[] = array($this, 'insertTaskFormSuccess');
return $form;
}
@@ -63,16 +66,24 @@ protected function createComponentInsertTaskForm()
* @param Form $form
* @param $values
*/
- public function insertTaskFromSuccess(Form $form, $values)
+ public function insertTaskFormSuccess(Form $form, $values)
{
- $taskGroup = $this->taskGroupRepository->getById($values->idTaskGroup);
+ $taskGroup = $this->taskGroupRepository->getById($values->taskGroup);
$taskEntity = new Task();
$taskEntity->setName($values->name);
$taskEntity->setDate($values->date);
$taskEntity->setTaskGroup($taskGroup);
$this->taskRepository->insert($taskEntity);
- $this->presenter->flashMessage('Task was created', 'success');
- $this->redirect('this');
+
+ if($this->presenter->isAjax()){
+ $form->setValues(array('taskGroup' => $this->idTaskGroup), TRUE);
+ $this->redrawControl();
+ }
+
+ /** @var TaskPresenter $presenter */
+ $presenter = $this->presenter;
+ $presenter->flashMessage('Task was created', 'success');
+ $presenter->redrawOrRedirect(array('flashes', 'tasks'));
}
}
diff --git a/app/components/form/templates/FilterTasks.latte b/app/components/form/templates/FilterTasks.latte
new file mode 100644
index 0000000..a9f01cb
--- /dev/null
+++ b/app/components/form/templates/FilterTasks.latte
@@ -0,0 +1,16 @@
+{snippet}
+ {form filterTasksForm}
+
+
+
+ {label name /}
+ {input name, class => 'form-control'}
+
+
+
+
+ {input submit, class => 'btn btn-primary'}
+
+
+ {/form}
+{/snippet}
\ No newline at end of file
diff --git a/app/components/form/templates/InsertTask.latte b/app/components/form/templates/InsertTask.latte
index e03d57b..a499fda 100644
--- a/app/components/form/templates/InsertTask.latte
+++ b/app/components/form/templates/InsertTask.latte
@@ -1,20 +1,28 @@
-{form insertTaskForm}
-
-
-
- {label name /}
- {input name, class => 'form-control'}
-
-
-
-
- {label date /}
- {input date, class => 'form-control datepicker'}
-
-
-
-
- {input submit, class => 'btn btn-primary'}
-
-
-{/form}
\ No newline at end of file
+{snippet}
+ {form insertTaskForm}
+
+
+
+ {label name /}
+ {input name, class => 'form-control'}
+
+
+
+
+ {label taskGroup /}
+ {input taskGroup, class => 'form-control'}
+
+
+
+
+ {label date /}
+ {input date, class => 'form-control datepicker'}
+
+
+
+
+ {input submit, class => 'btn btn-primary ajax'}
+
+
+ {/form}
+{/snippet}
\ No newline at end of file
diff --git a/app/config/config.neon b/app/config/config.neon
index 8251824..e056023 100644
--- a/app/config/config.neon
+++ b/app/config/config.neon
@@ -21,6 +21,7 @@ services:
- App\Model\Repository\TaskRepository
- App\Factories\Modal\IInsertTaskGroupFactory
- App\Factories\Form\IInsertTaskFactory
+ - App\Factories\Form\IFilterTasksFactory
extensions:
console: Kdyby\Console\DI\ConsoleExtension
diff --git a/app/factories/form/IFilterTasksFactory.php b/app/factories/form/IFilterTasksFactory.php
new file mode 100644
index 0000000..c0f203c
--- /dev/null
+++ b/app/factories/form/IFilterTasksFactory.php
@@ -0,0 +1,12 @@
+entityManager->persist($taskGroup);
$this->entityManager->flush();
}
+
+ /**
+ * @param array $criteria
+ * @param string $value
+ * @param string $key
+ * @param array $orderBy
+ * @return array
+ */
+ public function getSelect($criteria = array(), $value = 'name',
+ $orderBy = array('name' => 'ASC'), $key = 'id')
+ {
+ return $this->taskGroup->findPairs($criteria, $value, $orderBy, $key);
+ }
}
diff --git a/app/model/repository/TaskRepository.php b/app/model/repository/TaskRepository.php
index 7980e47..374c9c1 100644
--- a/app/model/repository/TaskRepository.php
+++ b/app/model/repository/TaskRepository.php
@@ -23,14 +23,25 @@ public function getById($id)
{
return $this->task->find($id);
}
-
- /**
- * @param number $idTaskGroup
- * @return Entity\Task[]
- */
- public function getByTaskGroup($idTaskGroup)
+
+ /**
+ * @param number $idTaskGroup
+ * @param array|null $orderBy
+ * @return Entity\Task[]
+ */
+ public function getByTaskGroup($idTaskGroup, $orderBy = NULL)
+ {
+ return $this->task->findBy(array('taskGroup' => $idTaskGroup), $orderBy);
+ }
+
+ /**
+ * @param array $criteria
+ * @param array|null $orderBy
+ * @return Entity\Task[]
+ */
+ public function getBy(array $criteria, $orderBy = NULL)
{
- return $this->task->findBy(array('taskGroup' => $idTaskGroup));
+ return $this->task->findBy($criteria, $orderBy);
}
/**
diff --git a/app/presenter/TaskPresenter.php b/app/presenter/TaskPresenter.php
index 5e2bbf9..0beab30 100644
--- a/app/presenter/TaskPresenter.php
+++ b/app/presenter/TaskPresenter.php
@@ -15,25 +15,48 @@ class TaskPresenter extends BasePresenter
public $insertTaskGroupFactory;
/** @var \App\Factories\Form\IInsertTaskFactory @inject */
public $insertTaskFactory;
- /** @var number */
- protected $idTaskGroup;
+ /** @var \App\Factories\Form\IFilterTasksFactory @inject */
+ public $filterTasksFactory;
+ /**
+ * @var number
+ * @persistent
+ */
+ public $idTaskGroup;
+ /**
+ * @var array
+ * @persistent
+ */
+ public $filter = array();
public function renderDefault()
{
$this->template->taskGroups = $this->getTaskGroups();
}
+ /**
+ * @param int $id
+ */
+ public function handleCompleteTask($id)
+ {
+ $task = $this->taskRepository->getById($id);
+ if($task){
+ $task->setCompleted(TRUE);
+ $this->taskRepository->updateEntity($task);
+ $this->flashMessage('Task was completed', 'success');
+ } else {
+ $this->flashMessage('Task was not found', 'error');
+ }
+
+ $this->redrawOrRedirect(array('tasks', 'flashes'));
+ }
+
/**
* @param int $id
*/
public function handleDeleteTaskGroup($id)
{
$this->taskGroupRepository->delete($id);
- if ($this->isAjax()) {
- $this->redrawControl('taskGroups');
- } else {
- $this->redirect('this');
- }
+ $this->redrawOrRedirect('taskGroups');
}
/**
@@ -64,6 +87,17 @@ protected function createComponentInsertTaskForm()
return $control;
}
+ /**
+ * @return \App\Components\Form\FilterTasks
+ */
+ protected function createComponentFilterTasksForm()
+ {
+ $control = $this->filterTasksFactory->create();
+ $control->setTaskGroupId($this->idTaskGroup);
+ $control->setFilter($this->filter);
+ return $control;
+ }
+
/**
* @return array
*/
@@ -87,7 +121,12 @@ protected function getTaskGroups()
protected function getTasks($idTaskGroup)
{
$result = array();
- $tasks = $this->taskRepository->getByTaskGroup($idTaskGroup);
+ $criteria = array_merge(
+ $this->prepareTasksFilter(),
+ array('taskGroup' => $idTaskGroup)
+ );
+
+ $tasks = $this->taskRepository->getBy($criteria, array('date' => 'DESC'));
foreach ($tasks as $task) {
$item = array();
$item['id'] = $task->getId();
@@ -98,4 +137,36 @@ protected function getTasks($idTaskGroup)
}
return $result;
}
+
+
+ /**
+ * @param string|array $snippets
+ * @param string $destination
+ */
+ public function redrawOrRedirect($snippets, $destination = 'this')
+ {
+ if($this->isAjax()){
+ if(!is_array($snippets)){
+ $snippets = array($snippets);
+ }
+ foreach($snippets as $snippet){
+ $this->redrawControl($snippet);
+ }
+ } else {
+ $this->redirect($destination);
+ }
+ }
+
+
+ /**
+ * @return array
+ */
+ private function prepareTasksFilter()
+ {
+ $filter = array();
+ if(array_key_exists('name', $this->filter)){
+ $filter['name LIKE'] = "%{$this->filter['name']}%";
+ }
+ return $filter;
+ }
}
diff --git a/app/templates/@layout.latte b/app/templates/@layout.latte
index 17d3257..e93de87 100644
--- a/app/templates/@layout.latte
+++ b/app/templates/@layout.latte
@@ -10,6 +10,7 @@
+
{block head}{/block}
@@ -38,11 +39,13 @@
- {foreach $flashes as $flash}
-
- {$flash->message}
-
- {/foreach}
+ {snippet flashes}
+ {foreach $flashes as $flash}
+
+ {$flash->message} ({(new DateTime)|date:'Y/m/d H:i:s'})
+
+ {/foreach}
+ {/snippet}
{include content}
@@ -53,6 +56,7 @@
+
diff --git a/app/templates/Task/taskGroup.latte b/app/templates/Task/taskGroup.latte
index 7e265b7..dd82c03 100644
--- a/app/templates/Task/taskGroup.latte
+++ b/app/templates/Task/taskGroup.latte
@@ -1,17 +1,19 @@
{block content}
{control insertTaskForm}
+
+ {control filterTasksForm}
{snippet tasks}
-
+
{/snippet}
diff --git a/www/js/main.js b/www/js/main.js
index 55ccd4c..0f5e778 100755
--- a/www/js/main.js
+++ b/www/js/main.js
@@ -1,7 +1,48 @@
$(document).ready(function(){
$.nette.init();
- $('.datepicker').datepicker({
- orientation: 'left top'
+ function bindICheck(){
+ var input = $('input');
+ input.iCheck('destroy');
+ input.iCheck({
+ handle: 'checkbox',
+ checkboxClass: 'icheckbox_flat-blue'
+ });
+ }
+
+ function bindHandleCheckbox() {
+ $('input[type=checkbox].handle').on('ifChecked', function (e) {
+ var link = $(this).data('link');
+ var bound = $(this).data('handle-bound');
+ if(!bound){
+ $.nette.ajax(link);
+ $(this).data('handle-bound', 1);
+ }
+ });
+ }
+
+ function bindDatepicker() {
+ var datepicker = $('.datepicker');
+ datepicker.datepicker('destroy');
+ datepicker.datepicker({
+ orientation: 'left bottom'
+ });
+ }
+
+ function initBinds(){
+ bindICheck();
+ bindHandleCheckbox();
+ bindDatepicker();
+ }
+
+ /* init */
+ initBinds();
+
+ /* after ajax */
+ $.nette.ext({
+ load: function () {
+ initBinds();
+ }
});
+
});
\ No newline at end of file