HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Audit/AuditController.php
<?php

/**
 * Short description for the file.
 *
 * @author      Tim Van Samang <timvansamang@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace KommaApp\Audit;

use App\Helpers\KommaHelpers;
use Illuminate\Support\MessageBag;
use KommaApp\Audit\Models\Audit;
use KommaApp\Audit\Models\Document;
use KommaApp\Audit\Models\DocumentGroup;
use KommaApp\Core\CoreController;
use KommaApp\Files\FileService;

class AuditController extends CoreController
{
    /**
     * @var AuditService
     */
    private $auditService;

    private $endUser;

    /**
     * AuditController constructor.
     * @param AuditService $auditService
     */
    public function __construct(AuditService $auditService)
    {
        $this->middleware('auth:endUser');

        $this->auditService = $auditService;
        $this->endUser = \Auth::endUser()->get();
    }

    /**
     * @param $audit
     * @return mixed
     */
    public function index($audit)
    {

        switch ($this->endUser->role) {
            case 'member':
                if (!$this->auditService->isUserAllowed($this->endUser, $audit)) return \App::abort(401, \Lang::get('not_allowed'));
                return \View::make('audit/member.audit')->with(['audit' => $audit, 'member' => \Auth::endUser()->get()]);
            case 'auditor':
                if (!$this->auditService->isUserAllowed($this->endUser, $audit)) return \App::abort(401, \Lang::get('not_allowed'));
                return \View::make('audit/auditor.audit')->with(['audit' => $audit, 'member' => \Auth::endUser()->get()]);
        }

        return \App::abort(401, \Lang::get('not_allowed'));
    }


    public function processAudit(Audit $audit)
    {
        if (\Input::has('save')) {
            Return \Redirect::route('audit.detail', [$audit->id]);

        }

        switch (\Input::get('reference')) {
            case 'member':
                return $this->processAuditForMember($audit);
                break;
            case 'auditor':
                return $this->processAuditForAuditor($audit);
        }
    }

    public function processAuditForMember(Audit $audit)
    {
        //Check if all the documentGroups have an document
        if ($errors = $this->auditService->checkDocumentGroup($audit)) return \Redirect::back()->withErrors($errors);
        $this->auditService->changeAuditStatus($audit, Audit::$judged);

        //Go back to the audit, with a message
        Return \Redirect::route('audit.detail', [$audit->id]);

    }

    public function processAuditForAuditor(Audit $audit)
    {

        if (\Input::has('approve')) {
            if ($errors = $this->auditService->checkAuditForApproval($audit)) return \Redirect::back()->withErrors($errors);

            //close this audit
            $this->auditService->changeAuditStatus($audit, Audit::$closed);
            //Create the next audit
            $this->auditService->createNewAudit($audit->member, $audit->year + 1);

            \Session::flash('confirmation', 'closed');
            //Go back to the dashboard
            return \Redirect::route('dashboard.index');
        }
        if (\Input::has('reject')) {
            if ($errors = $this->auditService->checkAuditForRejection($audit)) return \Redirect::back()->withErrors($errors);
            $this->auditService->changeAuditStatus($audit, Audit::$open);
            \Session::flash('confirmation', 'rejected');
            //Go back to the dashboard
            return \Redirect::route('dashboard.index');
        }
        if (\Input::has('practice')) {

            // Close this audit
            $this->auditService->changeAuditStatus($audit, Audit::$reviewedInPractice);
            // Create the next audit
            $this->auditService->createNewAudit($audit->member, $audit->year + 1);

            \Session::flash('confirmation', 'reviewedInPractice');
            // Go back to the dashboard
            return \Redirect::route('dashboard.index');
        }

    }


    public function uploadDocumentGroup(DocumentGroup $documentGroup)
    {
        //Check if it has an applicable field
        if (!\Input::has('applicable')) return KommaHelpers::ajaxAbort(401, 'No applicable given');

        $this->auditService->updateDocumentGroup($documentGroup, \Input::all());

        return \Response::json($documentGroup);
    }


    /**
     * This will upload an posted file (ajax)
     *
     * @param FileService $fileService
     * @param DocumentGroup $documentGroup
     * @return mixed
     */
    public function uploadDocument(FileService $fileService, DocumentGroup $documentGroup)
    {
        //Todo foutmeldingen

        //set the path to upload the file
        $fileService->path = storage_path('uploads/audits/' . $documentGroup->audit_id . '/' . $documentGroup->id);

        //Upload the file (as array)
        $filesData = $fileService->uploadFiles([\Input::file('file')]);

        //Bind the uploaded files to the documentGroup
        $documents = $this->auditService->bindFilesToDocumentGroup($documentGroup, $filesData);

        //Return the documents as Json
        return \Response::json($documents);

    }

    /**
     * Delete a document from the documentGroup
     *
     * @param FileService $fileService
     * @param DocumentGroup $documentGroup
     * @param Document $document
     * @return mixed
     * @throws \Exception
     */
    public function deleteDocument(FileService $fileService, DocumentGroup $documentGroup, Document $document)
    {

        //Check if the Document is from this documentGroup
        if (!$this->auditService->isDocumentPartOfDocumentGroup($document, $documentGroup)) return KommaHelpers::ajaxAbort(403, 'This file does not belong to the documentGroup');

        if (!$fileService->deleteFile(storage_path($document->full_path))) return KommaHelpers::ajaxAbort(403);

        $document->delete();
        return \Response::json($documentGroup->documents->all());


    }

    public function claimAudit(Audit $audit)
    {
        //check if the role is an auditor
        if (!$this->endUser->role == 'auditor') return \App::abort(403);

        //check if there is no auditor to this audit
        if ($audit->auditor_id != null) return \App::abort(403, 'This auditor is already claimed');

        //Bind the audit to the auditor
        $this->auditService->bindAuditorToAudit($this->endUser, $audit);


        //todo where to redirect?
        return \Redirect::route('dashboard.index');
    }

    /**
     * Functions for auditor
     */
    public function updateDocument(DocumentGroup $documentGroup, Document $document)
    {
        //Check if the Document is from this documentGroup
        if (!$this->auditService->isDocumentPartOfDocumentGroup($document, $documentGroup)) return KommaHelpers::ajaxAbort(403, 'This file does not belong to the documentGroup');

        $this->auditService->updateDocument($document, \Input::all());

        return \Response::json($document);

    }

    public function updateAllDocuments(DocumentGroup $documentGroup)
    {
        $this->auditService->updateDocuments($documentGroup->documents, \Input::all());

        return \Response::json($documentGroup->documents);
    }

}