mirror of
https://github.com/Karaka-Management/oms-Tasks.git
synced 2026-01-21 03:48:42 +00:00
Implement create and get
This commit is contained in:
parent
2d08306678
commit
473dafdbcf
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
namespace Modules\Tasks;
|
||||
|
||||
use Model\Message\FormValidation;
|
||||
use Model\Message\Redirect;
|
||||
use Model\Message\Reload;
|
||||
use Modules\Tasks\Models\Task;
|
||||
|
|
@ -167,6 +168,21 @@ class Controller extends ModuleAbstract implements WebInterface
|
|||
return $view;
|
||||
}
|
||||
|
||||
private function validateTaskCreate(RequestAbstract $request) : array
|
||||
{
|
||||
$val = [];
|
||||
if (
|
||||
($val['title'] = empty($request->getData('title')))
|
||||
|| ($val['description'] = empty($request->getData('description')))
|
||||
|| ($val['due'] = !((bool)strtotime($request->getData('due'))))
|
||||
|| ($val['forward'] = !(is_numeric($request->getData('forward') ?? 0)))
|
||||
) {
|
||||
return $val;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
|
|
@ -179,6 +195,12 @@ class Controller extends ModuleAbstract implements WebInterface
|
|||
*/
|
||||
public function apiTaskCreate(RequestAbstract $request, ResponseAbstract $response, $data = null)
|
||||
{
|
||||
if (!empty($val = $this->validateTaskCreate($request))) {
|
||||
$response->set('task_create', new FormValidation($val));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$task = new Task();
|
||||
$task->setTitle($request->getData('title') ?? '');
|
||||
$task->setDescription($request->getData('description') ?? '');
|
||||
|
|
@ -198,7 +220,7 @@ class Controller extends ModuleAbstract implements WebInterface
|
|||
$task->addElement($element);
|
||||
|
||||
TaskMapper::create($task);
|
||||
$response->set($request->__toString(), new Redirect(UriFactory::build('{/base}/{/lang}/{/app}/task/single?id=' . $task->getId())));
|
||||
$response->set($request->__toString(), $task->jsonSerialize());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ use phpOMS\Datatypes\Exception\InvalidEnumValue;
|
|||
* @link http://orange-management.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Task
|
||||
class Task implements \JsonSerializable
|
||||
{
|
||||
|
||||
/**
|
||||
|
|
@ -119,7 +119,7 @@ class Task
|
|||
*/
|
||||
protected $schedule = null;
|
||||
|
||||
protected $media = null;
|
||||
protected $media = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
|
@ -197,6 +197,7 @@ class Task
|
|||
public function setCreatedBy(int $id)
|
||||
{
|
||||
$this->createdBy = $id;
|
||||
$this->schedule->setCreatedBy($id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -415,4 +416,28 @@ class Task
|
|||
return $this->schedule;
|
||||
}
|
||||
|
||||
private function toArray() : array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'description' => $this->description,
|
||||
'status' => $this->status,
|
||||
'type' => $this->type,
|
||||
'due' => $this->due->format('Y-m-d H:i:s'),
|
||||
'done' => (!isset($this->done) ? null : $this->done->format('Y-m-d H:i:s')),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
* which is a value of any type other than a resource.
|
||||
* @since 5.4.0
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return json_encode($this->toArray());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
namespace Modules\Tasks\Models;
|
||||
|
||||
use Modules\Calendar\Models\ScheduleMapper;
|
||||
use Modules\Media\Models\MediaMapper;
|
||||
use Modules\Tasks\Models\TaskElementMapper;
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Query\Builder;
|
||||
|
|
@ -68,7 +69,7 @@ class TaskMapper extends DataMapperAbstract
|
|||
'dst' => 'task_element_task',
|
||||
'src' => null,
|
||||
],
|
||||
'media' => [
|
||||
'media' => [ // todo: maybe make this a has one and then link to collection instead of single media files!
|
||||
'mapper' => MediaMapper::class,
|
||||
'table' => 'task_media',
|
||||
'dst' => 'task_media_dst',
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ echo $this->getData('nav')->render(); ?>
|
|||
<header><h1><?= $this->getText('Task'); ?></h1></header>
|
||||
|
||||
<div class="inner">
|
||||
<form id="fTask" method="POST" action="<?= \phpOMS\Uri\UriFactory::build('/{/lang}/api/task/create?csrf={$CSRF}'); ?>">
|
||||
<form id="fTask" method="POST" action="<?= \phpOMS\Uri\UriFactory::build('/{/lang}/api/task?csrf={$CSRF}'); ?>">
|
||||
<table class="layout wf-100">
|
||||
<tbody>
|
||||
<tr><td colspan="2"><label for="iReceiver"><?= $this->getText('To'); ?></label>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user