$l = OC_L10N::get('collaboration');
// Fetch member list
$details = array(array());
$i = 0;
foreach ($_POST as $key => $value) {
    if (strpos($key, 'mem_name') === 0) {
        $id = substr($key, 8);
        $details[$i]['member'] = $value;
        $details[$i]['role'] = $_POST['mem_role' . $id];
        $details[$i]['email'] = $_POST['mem_email' . $id];
        $details[$i]['mobile'] = $_POST['mem_mobile' . $id];
        $i++;
    }
}
$tpl = new OCP\Template('collaboration', 'display_message', 'user');
$tpl->assign('title', $l->t('Loading...'));
$tpl->assign('msg', $l->t('Updating project \'%s\'. Please be patient.', array($_POST['title'])));
$tpl->printPage();
$redirect = '';
$post_id = false;
if (!isset($_POST['pid'])) {
    $post_id = OC_Collaboration_Project::createProject($_POST['title'], $_POST['description'], OC_User::getUser(), $_POST['deadline'], $details);
    $redirect = 'submit_new_project';
} else {
    $post_id = OC_Collaboration_Project::updateProject($_POST['pid'], $_POST['title'], $_POST['description'], $_POST['deadline'], $details, OC_User::getUser(), isset($_POST['project_completed']));
    $redirect = 'submit_change_project';
}
if ($post_id != false && isset($_POST['send_mail'])) {
    OC_Collaboration_Mail::sendProjectAssignmentMail($_POST['title'], $details);
}
print_unescaped('<META HTTP-EQUIV="Refresh" Content="0; URL=' . \OCP\Util::linkToRoute('collaboration_route', array('rel_path' => $redirect)) . '?post=' . $post_id . '&title=' . $_POST['title'] . '">');
            $tid = OC_Collaboration_Task::createTask($_POST['title'], $_POST['description'], OC_User::getUser(), $_POST['pid'], $_POST['priority'], $_POST['deadline_time'], 'Unassigned', NULL);
        }
        if ($tid != false && isset($_POST['send_mail'])) {
            OC_Collaboration_Mail::sendTaskCreationMail($_POST['title'], $_POST['description'], $_POST['pid'], $_POST['member'], $_POST['deadline_time']);
        }
        $eve->assign('title', $l->t('Loading...'));
        $eve->assign('permission_granted', 'true');
        $eve->assign('task', $tid);
        $eve->printPage();
    } else {
        if (!isset($_POST['status'])) {
            $_POST['status'] = NULL;
        }
        if (!isset($_POST['member'])) {
            $_POST['member'] = NULL;
        }
        if (!isset($_POST['reason'])) {
            $_POST['reason'] = NULL;
        }
        $status = OC_Collaboration_Task::updateTask($_POST['tid'], $_POST['title'], $_POST['description'], OC_User::getUser(), $_POST['pid'], $_POST['priority'], $_POST['deadline_time'], $_POST['status'], $_POST['member'], $_POST['reason']);
        if ($status != false && isset($_POST['send_mail'])) {
            OC_Collaboration_Mail::sendTaskCreationMail($_POST['title'], $_POST['description'], $_POST['pid'], $_POST['member'], $_POST['deadline_time']);
        }
        $eve = new OCP\Template('collaboration', 'event_edit_form', 'user');
        $eve->assign('title', $l->t('Loading...'));
        $eve->assign('permission_granted', 'true');
        $eve->assign('task', $_POST['tid']);
        $eve->printPage();
        //		print_unescaped('<META HTTP-EQUIV="Refresh" Content="0; URL=' . \OCP\Util::linkToRoute('collaboration_route', array('rel_path' => 'submit_change_task')) . '?task=' . $_POST['tid'] . '&title=' . $_POST['title'] . '">');
    }
}