Example #1
0
/**
* Upload files @ingroup pages
*
* read more at http://www.streber-pm.org/3658
*/
function filesUpload()
{
    global $PH;
    $new_file = File::getUploaded();
    if (!$new_file) {
        $PH->abortWarning("Nothing uploaded");
    }
    ### first try single project-id ###
    $project = NULL;
    if ($ids = getPassedIds('prj', 'projects_*')) {
        if (!($project = Project::getVisibleById($ids[0]))) {
            $PH->abortWarning("ERROR: could not get Project");
            return;
        }
    }
    ### try to get task ###
    $task_id = 0;
    if ($task_ids = getPassedIds('parent_task', 'tasks_*')) {
        if (count($task_ids) > 1) {
            $PH->messages[] = __("only expected one task. Used the first one.");
        }
        $task_id = $task_ids[0];
    } else {
        if ($task_ids = getPassedIds('', 'folders_*')) {
            if (count($task_ids) > 1) {
                $PH->messages[] = __("only expected one task. Used the first one.");
            }
            $task_id = $task_ids[0];
        }
    }
    if ($task_id) {
        if ($task = Task::getEditableById($task_id)) {
            $new_file->parent_item = $task->id;
        } else {
            $PH->abortWarning(__('Could not edit task'), ERROR_RIGHTS);
        }
    }
    if (!$project) {
        if (!$task_id) {
            $PH->abortWarning("Could not get project", ERROR_NOTE);
        }
        if (!($task = Task::getVisibleById($task_id))) {
            $PH->abortWarning("Invalid task id?", ERROR_BUG);
        }
        if (!($project = Project::getVisibleById($task->project))) {
            $PH->abortWarning("Invalid project for task?", ERROR_BUG);
        }
    }
    ### build new object ###
    $new_file->project = $project->id;
    $PH->show('fileEdit', array('file' => $new_file->id), $new_file);
}