<?php

if (!defined('W2P_BASE_DIR')) {
    die('You should not access this file directly.');
}
// @todo    refactor to use a core controller
$del = (int) w2PgetParam($_POST, 'del', 0);
$notify_owner = w2PgetParam($_POST, 'task_log_notify_owner', 'off');
// TODO: This is a dirty hack.
$_POST['task_log_task_end_date'] = $_POST['task_end_date'];
$_POST['task_log_percent_complete'] = $_POST['task_percent_complete'];
$obj = new CTask_Log();
if (!$obj->bind($_POST)) {
    $AppUI->setMsg($obj->getError(), UI_MSG_ERROR);
    $AppUI->redirect('m=tasks&a=view&task_id=' . $obj->task_log_task);
}
$action = $del ? 'deleted' : 'stored';
$result = $del ? $obj->delete() : $obj->store();
if (count($obj->getError())) {
    $AppUI->setMsg($result, UI_MSG_ERROR, true);
    $AppUI->holdObject($obj);
    $AppUI->redirect('m=tasks&a=view&task_id=' . $obj->task_log_task);
}
if ($result) {
    $AppUI->setMsg('Task Log ' . $action, UI_MSG_OK, true);
    $task = new CTask();
    $task->load($obj->task_log_task);
    if ('on' == $notify_owner) {
        if ($msg = $task->notifyOwner()) {
            $AppUI->setMsg($msg, UI_MSG_ERROR);
        }