<?php
/**
 * i-doit
 *
 * my-doit Task-Area.
 *
 * @package    i-doit
 * @author     Dennis Stücken <dstuecken@synetics.de>
 * @version    0.9 Thu Jun 29 11:12:33 CEST 2006
 * @copyright  synetics GmbH
 * @license    http://www.i-doit.com/license
 */

// Prepare dao's.
$l_user_dao = isys_component_dao_user::instance($g_comp_database);
$l_user_id = $l_user_dao->get_current_user_id();
$l_workflow_dao = new isys_workflow_dao_action($g_comp_database);
$l_ar_tasks = [];

// Get number of mytask entries from registry.
$l_max_entries = isys_tenantsettings::get('cmdb.registry.mytask_entries', 10);

$blacklistCondition = '';

if (defined('C__WORKFLOW_TYPE__CHECKLIST') && isys_tenantsettings::get('workflows.my-doit.hide-checklists', 0)) {
    $blacklistCondition = 'AND isys_workflow__isys_workflow_type__id != ' . C__WORKFLOW_TYPE__CHECKLIST . ' ';
}

if (defined('C__WORKFLOW__ACTION__TYPE__ACCEPT')) {
    $statuses = filter_defined_constants(['C__TASK__STATUS__ACCEPT', 'C__TASK__STATUS__OPEN']);

    $l_tasks[0] = $l_workflow_dao->get_workflows(
        null,
        null,
        null,
        C__WORKFLOW__ACTION__TYPE__ACCEPT,
        (!empty($statuses) ? ' AND (isys_workflow__status IN (' . implode(', ', $statuses) . ')) ' : '') . $blacklistCondition,
        ($l_max_entries == 0 ? 10 : $l_max_entries),
        null,
        null,
        'startdate DESC',
        $l_user_id,
        null
    );
}

if (defined('C__WORKFLOW__ACTION__TYPE__NEW')) {
    $l_tasks[2] = $l_workflow_dao->get_workflows(
        null,
        null,
        null,
        C__WORKFLOW__ACTION__TYPE__NEW,
        $blacklistCondition,
        $l_max_entries,
        null,
        null,
        'startdate DESC',
        $l_user_id,
        null
    );
}

if (defined('C__WORKFLOW__ACTION__TYPE__ASSIGN')) {
    $statuses = filter_defined_constants(['C__TASK__STATUS__INIT', 'C__TASK__STATUS__ASSIGNMENT']);

    $l_tasks[1] = $l_workflow_dao->get_workflows(
        null,
        null,
        null,
        C__WORKFLOW__ACTION__TYPE__ASSIGN,
        (!empty($statuses) ? ' AND (isys_workflow__status IN (' . implode(', ', $statuses) . ')) ' : '') . $blacklistCondition,
        $l_max_entries,
        null,
        null,
        'startdate DESC',
        $l_user_id,
        null
    );
}
// Iterate through menu entries currently : my- and assigned-tasks.
$l_added = [];
for ($i = 0; $i <= 2; $i++) {
    // Fixing a potential bug if a results are missing.
    if (!($l_tasks[$i] instanceof isys_component_dao_result)) {
        continue;
    }

    while ($l_row = $l_tasks[$i]->get_row()) {
        if (!defined('C__TASK__STATUS__END') || $l_row['isys_workflow__status'] < C__TASK__STATUS__END) {
            if (!array_key_exists($l_row['isys_workflow__id'], $l_added)) {
                if (empty($l_row['startdate'])) {
                    $l_tmp = $l_workflow_dao->get_workflows($l_row['isys_workflow__id']);
                    $l_rtmp = $l_tmp->get_row();

                    $l_row['startdate'] = $l_rtmp['startdate'];
                }

                $l_ar_tasks[$i][] = [
                    'title' => $l_row['isys_workflow__title'],
                    'date'  => isys_application::instance()->container->get('locales')->fmt_datetime($l_row['startdate']),
                    'link'  => isys_helper_link::create_url([
                        C__GET__MODULE_ID                    => defined_or_default('C__MODULE__WORKFLOW'),
                        C__CMDB__GET__VIEWMODE               => defined_or_default('C__WF__VIEW__DETAIL__GENERIC'),
                        C__CMDB__GET__TREEMODE               => defined_or_default('C__WF__VIEW__TREE'),
                        defined_or_default('C__WF__GET__ID') => $l_row['isys_workflow__id']
                    ])
                ];
            }

            $l_added[$l_row["isys_workflow__id"]] = true;
        }
    }
}

isys_application::instance()->container->get('template')
    ->assign('g_tasks__accepted', $l_ar_tasks[0])
    ->assign('g_tasks__created', $l_ar_tasks[2])
    ->assign('g_tasks__assigned', $l_ar_tasks[1]);

unset($l_user_dao, $l_user_id, $l_task_dao, $l_tasks);
