implementing crm with laravel tasks module

Implementing CRM System With Laravel Part 9: Tasks Module

When we do some stuff with contacts like meetings and phone calls we need to record those events, events like this are called tasks in the context of the CRM system and we will implement it in this part.

 

 

Series Topics:

 

Generating the views:

php artisan crud:view tasks --fields="name#string;priority#string;status#integer;type_id#integer;start_date#string;end_date#string;complete_date#string;contact_type#string;contact_id#integer;description#string;assigned_user_id#integer" --view-path="pages" --route-group=admin --form-helper=html

Generating the controller:

php artisan crud:controller TasksController --crud-name=tasks --model-name=Task --model-namespace="Models\\" --view-path="pages" --route-group=admin

 

Tasks Controller

Open app/Http/Controllers/TasksController.php and update it as below:

<?php

namespace App\Http\Controllers;

use App\Helpers\MailerFactory;
use App\Models\ContactStatus;
use App\Models\Document;
use App\Models\Task;
use App\Models\TaskDocument;
use App\Models\TaskStatus;
use App\Models\TaskType;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class TasksController extends Controller
{

    protected $mailer;

    public function __construct(MailerFactory $mailer)
    {
        $this->middleware('admin:index-list_tasks|create-create_task|show-view_task|edit-edit_task|destroy-delete_task|getAssignTask-assign_task|getUpdateStatus-update_task_status', ['except' => ['store', 'update', 'postAssignTask', 'postUpdateStatus']]);

        $this->mailer = $mailer;
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\View\View
     */
    public function index(Request $request)
    {
        $keyword = $request->get('search');
        $perPage = 25;

        if (!empty($keyword)) {
            $query = Task::where('name', 'like', "%$keyword%");
        } else {
            $query = Task::latest();
        }

        if(\request('assigned_user_id') != null) {
            $query->where('assigned_user_id', \request('assigned_user_id'));
        }

        // if not admin user show tasks if assigned to or created by that user
        if(Auth::user()->is_admin == 0) {

            $query->where(function ($query) {
                $query->where('assigned_user_id', Auth::user()->id)
                    ->orWhere('created_by_id', Auth::user()->id);
            });

        }

        $tasks = $query->paginate($perPage);

        return view('pages.tasks.index', compact('tasks'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        $data = $this->getFormData();

        list($users, $statuses, $task_types, $contact_statuses, $documents) = $data;

        return view('pages.tasks.create', compact('users', 'documents', 'statuses', 'task_types', 'contact_statuses'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        $this->do_validate($request);

        $requestData = $request->all();

        if(isset($requestData['documents'])) {

            $documents = $requestData['documents'];

            unset($requestData['documents']);

            $documents = array_filter($documents, function ($value) {
                return !empty($value);
            });
        }

        $requestData['created_by_id'] = Auth::user()->id;

        if(empty($requestData['contact_type'])) {

            $requestData['contact_id'] = null;
        }
        
        $task = Task::create($requestData);


        // insert documents
        if($task && $task->id) {

            if(isset($documents)) {

                $this->insertDocuments($documents, $task->id);
            }
        }


        // send notifications email
        if(getSetting("enable_email_notification") == 1 && isset($requestData['assigned_user_id'])) {

            $this->mailer->sendAssignTaskEmail("Task assigned to you", User::find($requestData['assigned_user_id']), $task);
        }

        return redirect('admin/tasks')->with('flash_message', 'Task added!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        $task = Task::findOrFail($id);

        return view('pages.tasks.show', compact('task'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function edit($id)
    {
        $data = $this->getFormData($id);

        list($users, $statuses, $task_types, $contact_statuses, $documents, $task, $selected_documents) = $data;

        return view('pages.tasks.edit', compact('task', 'users', 'documents', 'statuses', 'task_types', 'selected_documents', 'contact_statuses'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(Request $request, $id)
    {
        $this->do_validate($request);

        $requestData = $request->all();

        if(isset($requestData['documents'])) {

            $documents = $requestData['documents'];

            unset($requestData['documents']);

            $documents = array_filter($documents, function ($value) {
                return !empty($value);
            });
        }

        if(empty($requestData['contact_type'])) {

            $requestData['contact_id'] = null;
        }

        $requestData['modified_by_id'] = Auth::user()->id;
        
        $task = Task::findOrFail($id);

        $old_assign_user_id = $task->assigned_user_id;

        $old_task_status = $task->status;

        $task->update($requestData);


        // delete documents if exist
        TaskDocument::where('task_id', $id)->delete();

        // insert documents
        if(isset($documents)) {

            $this->insertDocuments($documents, $id);
        }


        // send notifications emails

        if(getSetting("enable_email_notification") == 1) {

            if (isset($requestData['assigned_user_id']) && $old_assign_user_id != $requestData['assigned_user_id']) {

                $this->mailer->sendAssignTaskEmail("Task assigned to you", User::find($requestData['assigned_user_id']), $task);
            }

            // if status get update then send a notification to both the super admin and the assigned user
            if($old_task_status != $requestData['status']) {

                $super_admin = User::where('is_admin', 1)->first();

                if($super_admin->id == $task->assigned_user_id) {
                    $this->mailer->sendUpdateTaskStatusEmail("Task status update", User::find($task->assigned_user_id), $task);
                } else {
                    $this->mailer->sendUpdateTaskStatusEmail("Task status update", User::find($task->assigned_user_id), $task);

                    $this->mailer->sendUpdateTaskStatusEmail("Task status update", $super_admin, $task);
                }

            }
        }

        return redirect('admin/tasks')->with('flash_message', 'Task updated!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy($id)
    {
        $task = Task::find($id);

        Task::destroy($id);

        if(getSetting("enable_email_notification") == 1) {
            $this->mailer->sendDeleteTaskEmail("Task deleted", User::find($task->assigned_user_id), $task);
        }

        return redirect('admin/tasks')->with('flash_message', 'Task deleted!');
    }


    public function getAssignTask($id)
    {
        $task = Task::find($id);

        $users = User::where('id', '!=', $task->assigned_user_id)->get();

        return view('pages.tasks.assign', compact('users', 'task'));
    }


    public function postAssignTask(Request $request, $id)
    {
        $this->validate($request, [
            'assigned_user_id' => 'required'
        ]);

        $task = Task::find($id);

        $task->update(['assigned_user_id' => $request->assigned_user_id]);

        if(getSetting("enable_email_notification") == 1) {
            $this->mailer->sendAssignTaskEmail("Task assigned to you", User::find($request->assigned_user_id), $task);
        }

        return redirect('admin/tasks')->with('flash_message', 'Task assigned!');
    }


    public function getUpdateStatus($id)
    {
        $task = Task::find($id);

        $statuses = TaskStatus::all();

        return view('pages.tasks.update_status', compact('task', 'statuses'));
    }

    public function postUpdateStatus(Request $request, $id)
    {
        $this->validate($request, [
            'status' => 'required'
        ]);

        $task = Task::find($id);

        $task->update(['status' => $request->status]);


        if(getSetting("enable_email_notification") == 1 && !empty($task->assigned_user_id)) {

            $super_admin = User::where('is_admin', 1)->first();

            if($super_admin->id == $task->assigned_user_id) {

                $this->mailer->sendUpdateTaskStatusEmail("Task status update", User::find($task->assigned_user_id), $task);
            } else {
                $this->mailer->sendUpdateTaskStatusEmail("Task status update", User::find($task->assigned_user_id), $task);

                $this->mailer->sendUpdateTaskStatusEmail("Task status update", $super_admin, $task);
            }
        }

        return redirect('admin/tasks')->with('flash_message', 'Task status updated!');
    }


    /**
     * insert documents
     *
     *
     * @param $documents
     * @param $task_id
     */
    protected function insertDocuments($documents, $task_id)
    {
        foreach ($documents as $document) {

            $taskDocument = new TaskDocument();

            $taskDocument->document_id = $document;

            $taskDocument->task_id = $task_id;

            $taskDocument->save();
        }
    }


    /**
     * do_validate
     *
     *
     * @param $request
     */
    protected function do_validate($request)
    {
        $this->validate($request, [
            'name' => 'required',
            'start_date' => 'nullable|date',
            'end_date'   => 'nullable|date|after_or_equal:start_date'
        ]);
    }


    /**
     * get form data for the tasks form
     *
     *
     *
     * @param null $id
     * @return array
     */
    protected function getFormData($id = null)
    {
        $users = User::where('is_active', 1)->get();

        $statuses = TaskStatus::all();

        $task_types = TaskType::all();

        $contact_statuses = ContactStatus::all();

        if(Auth::user()->is_admin == 1) {
            $documents = Document::where('status', 1)->get();
        } else {
            $super_admin = User::where('is_admin', 1)->first();

            $documents = Document::where('status', 1)->where(function ($query) use ($super_admin) {
                $query->where('created_by_id', Auth::user()->id)
                    ->orWhere('assigned_user_id', Auth::user()->id)
                    ->orWhere('created_by_id', $super_admin->id)
                    ->orWhere('assigned_user_id', $super_admin->id);
            })->get();
        }

        if($id == null) {

            return [$users, $statuses, $task_types, $contact_statuses, $documents];
        }

        $task = Task::findOrFail($id);

        $selected_documents = $task->documents()->pluck('document_id')->toArray();

        return [$users, $statuses, $task_types, $contact_statuses, $documents, $task, $selected_documents];
    }
}

The code structure shown above is pretty similar to the previous modules. First in the constructor i have added the admin middleware and the actions and their permission names using the format [action-permission] separated by “|”. I have injected the Mailer into the constructor to be used later when sending emails.

The index() function displays the tasks listing and checks on whether this is the super admin with “Auth::user()->is_admin==0”, if the user is not the super admin is displays the tasks that he is assigned to or created by. If this is the super admin we don’t add this restriction.

The create() and edit() functions show the create and update forms respectively. I have added another function getFormData() which retrieves the form data like the documents and users to be used in form dropdown.  The getFormData() function retrieves a lot of items like the users, task statuses, tasks types, contact statuses and documents.

The store() and update() functions do the create or update process respectively.In the store() function we insert the data as usual then we check if the “enable_email_notification” is on and send an email to the user to notify him that he assigned to this task.

In the update() function we update the task and then also we check if the “enable_email_notification” is on then we send an email to the assigned user if this is a new user and send other email to notify him about the task status in case the task have been updated status.

The destroy() delete the task as usual, the getAssignTask() and postAssignTask() functions do assigns a task to a user and send an email to notify the user. The getUpdateStatus() and postUpdateStatus() functions update the task status and also notifies the user of this update.

Tasks Views

Make the following updates to the below view files:

resources/views/pages/tasks/index.blade.php

@extends('layout.app')

@section('title', ' | List Tasks')

@section('content')

    <section class="content-header">
        <h1>
            Tasks
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li class="active">Tasks</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">

                        @include('includes.flash_message')

                        @if(user_can("create_task"))
                            <a href="{{ url('/admin/tasks/create') }}" class="btn btn-success btn-sm pull-right" title="Add New task">
                                <i class="fa fa-plus" aria-hidden="true"></i> Add New
                            </a>
                        @endif

                        <form method="GET" action="{{ url('/admin/tasks') }}" accept-charset="UTF-8" class="form-inline my-2 my-lg-0" role="search">
                            <div class="input-group">
                                <input type="text" class="form-control" name="search" placeholder="Search..." value="{{ request('search') }}">
                                <span class="input-group-btn">
                                    <button class="btn btn-secondary" type="submit">
                                        <i class="fa fa-search"></i>
                                    </button>
                                </span>
                            </div>
                        </form>

                        <br/>
                        <br/>
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                <tr>
                                    @if(\Auth::user()->is_admin == 1)
                                        <th>#</th>
                                    @endif
                                    <th>Name</th>
                                    <th>Priority</th>
                                    <th>Status</th>
                                    @if(\Auth::user()->is_admin == 1)
                                        <th>Created by</th>
                                    @endif
                                    <th>Assigned to</th>
                                    <th>Created at</th>
                                    <th>Actions</th>
                                </tr>
                                </thead>
                                <tbody>
                                    @foreach($tasks as $item)
                                        <tr>
                                            @if(\Auth::user()->is_admin == 1)
                                                <td>{{ $item->id }}</td>
                                            @endif
                                            <td>{{ $item->name }}</td>
                                            <td>{{ $item->priority }}</td>
                                            <td>{{ $item->getStatus->name }}</td>
                                            @if(\Auth::user()->is_admin == 1)
                                                <td>{{ $item->createdBy->name }}</td>
                                            @endif
                                            <td>{{ $item->assignedTo != null ? $item->assignedTo->name : "not set" }}</td>
                                            <td>{{ $item->created_at }}</td>
                                            <td>
                                                @if(user_can('view_task'))
                                                    <a href="{{ url('/admin/tasks/' . $item->id) }}" title="View task"><button class="btn btn-info btn-sm"><i class="fa fa-eye" aria-hidden="true"></i> View</button></a>
                                                @endif

                                                @if(user_can('edit_task'))
                                                    <a href="{{ url('/admin/tasks/' . $item->id . '/edit') }}" title="Edit task"><button class="btn btn-primary btn-sm"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button></a>
                                                @endif

                                                @if(user_can('assign_task'))
                                                    <a href="{{ url('/admin/tasks/' . $item->id . '/assign') }}" title="Assign task"><button class="btn btn-primary btn-sm"><i class="fa fa-envelope-o" aria-hidden="true"></i> Assign</button></a>
                                                @endif

                                                @if(user_can('update_task_status'))
                                                    <a href="{{ url('/admin/tasks/' . $item->id . '/update-status') }}" title="Update task status"><button class="btn btn-primary btn-sm"><i class="fa fa-star" aria-hidden="true"></i> Update status</button></a>
                                                @endif

                                                @if(user_can('delete_task'))
                                                    <form method="POST" action="{{ url('/admin/tasks' . '/' . $item->id) }}" accept-charset="UTF-8" style="display:inline">
                                                        {{ method_field('DELETE') }}
                                                        {{ csrf_field() }}
                                                        <button type="submit" class="btn btn-danger btn-sm" title="Delete task" onclick="return confirm('Confirm delete?')"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</button>
                                                    </form>
                                                @endif
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                            <div class="pagination-wrapper"> {!! $tasks->appends(['search' => Request::get('search')])->render() !!} </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

resources/views/pages/tasks/create.blade.php

@extends('layout.app')

@section('title', ' | Create task')

@section('content')

    <section class="content-header">
        <h1>
            Create New task
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin/') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="{{ url('/admin/tasks') }}">Tasks</a></li>
            <li class="active">Create</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <a href="{{ url('/admin/tasks') }}" title="Back"><button class="btn btn-warning btn-sm"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
                        <br />
                        <br />

                        @if ($errors->any())
                            <ul class="alert alert-danger">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        @endif

                        <form method="POST" action="{{ url('/admin/tasks') }}" accept-charset="UTF-8" enctype="multipart/form-data">
                            {{ csrf_field() }}

                            @include ('pages.tasks.form', ['formMode' => 'create'])

                        </form>

                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

@section('scripts')

    <script type="text/javascript" src="{{ url('theme/views/tasks/form.js') }}"></script>

@stop

resources/views/pages/tasks/edit.blade.php

@extends('layout.app')

@section('title', ' | Edit task')

@section('content')


    <section class="content-header">
        <h1>
            Edit task #{{ $task->id }}
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin/') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="{{ url('/admin/tasks') }}">Tasks</a></li>
            <li class="active">Edit</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <a href="{{ url('/admin/tasks') }}" title="Back"><button class="btn btn-warning btn-sm"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
                        <br />
                        <br />

                        @if ($errors->any())
                            <ul class="alert alert-danger">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        @endif

                        <form method="POST" action="{{ url('/admin/tasks/' . $task->id) }}" accept-charset="UTF-8" enctype="multipart/form-data">
                            {{ method_field('PATCH') }}
                            {{ csrf_field() }}

                            @include ('pages.tasks.form', ['formMode' => 'edit'])

                        </form>

                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection


@section('scripts')

    <script type="text/javascript" src="{{ url('theme/views/tasks/form.js') }}"></script>

@stop

resources/views/pages/tasks/form.blade.php

<div class="row">
    <div class="col-md-8">
        <div class="panel panel-default">
            <div class="panel-heading">Basic details</div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
                            <label for="name" class="control-label">{{ 'Name' }}</label>
                            <input class="form-control" name="name" type="text" id="name" value="{{ isset($task->name) ? $task->name : ''}}" >
                            {!! $errors->first('name', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group {{ $errors->has('contact_type') ? 'has-error' : ''}}">
                            <label for="contact_type" class="control-label">{{ 'Contact type' }}</label>
                            <select class="form-control" name="contact_type" id="contact_type">
                                <option value="">Select type</option>
                                @foreach($contact_statuses as $contact_status)
                                    <option value="{{ $contact_status->name }}" {{ isset($task->contact_type) && $task->contact_type == $contact_status->name ? 'selected' : ''}}>{{ $contact_status->name }}</option>
                                @endforeach
                            </select>
                            {!! $errors->first('contact_type', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group {{ $errors->has('contact_id') ? 'has-error' : ''}}">
                            <label for="contact_id" class="control-label">{{ 'Contact name' }}</label>
                            <select name="contact_id" id="contact_id" class="form-control" data-selected-value="{{ isset($task->contact_id) ? $task->contact_id : '' }}"></select>
                            {!! $errors->first('contact_id', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group {{ $errors->has('type_id') ? 'has-error' : ''}}">
                            <label for="type_id" class="control-label">{{ 'Task type' }}</label>
                            <select name="type_id" id="type_id" class="form-control">
                                @foreach($task_types as $type)
                                    <option value="{{ $type->id }}" {{ isset($task->type_id) && $task->type_id == $type->id ? 'selected' : ''}}>{{ $type->name }}</option>
                                @endforeach
                            </select>
                            {!! $errors->first('type_id', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group {{ $errors->has('status') ? 'has-error' : ''}}">
                            <label for="status" class="control-label">{{ 'Contact status' }}</label>
                            <select name="status" id="status" class="form-control">
                                @foreach($statuses as $status)
                                    <option value="{{ $status->id }}" {{ isset($task->status) && $task->status == $status->id ? 'selected' : ''}}>{{ $status->name }}</option>
                                @endforeach
                            </select>
                            {!! $errors->first('status', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group {{ $errors->has('priority') ? 'has-error' : ''}}">
                            <label for="priority" class="control-label">{{ 'Priority' }}</label>
                            <select class="form-control" name="priority" id="priority">
                                <option value="">Select priority</option>
                                @foreach(array('Low', 'Normal', 'High', 'Urgent') as $value)
                                    <option value="{{ $value }}" {{ isset($task->priority) && $task->priority == $value ? 'selected' : ''}}>{{ $value }}</option>
                                @endforeach
                            </select>
                            {!! $errors->first('priority', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group {{ $errors->has('description') ? 'has-error' : ''}}">
                            <label for="description" class="control-label">{{ 'Description' }}</label>
                            <textarea class="form-control" name="description" type="text" id="description">{{ isset($task->description) ? $task->description : ''}}</textarea>
                            {!! $errors->first('description', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="panel panel-default">
            <div class="panel-heading">Task dates</div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-12">
                        <div class="form-group {{ $errors->has('start_date') ? 'has-error' : ''}}">
                            <label for="start_date" class="control-label">{{ 'Start date' }}</label>
                            <input class="form-control" name="start_date" type="text" id="start_date" value="{{ isset($task->start_date) ? $task->start_date : ''}}" >
                            {!! $errors->first('start_date', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group {{ $errors->has('end_date') ? 'has-error' : ''}}">
                            <label for="end_date" class="control-label">{{ 'End date' }}</label>
                            <input class="form-control" name="end_date" type="text" id="end_date" value="{{ isset($task->end_date) ? $task->end_date : ''}}" >
                            {!! $errors->first('end_date', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group {{ $errors->has('complete_date') ? 'has-error' : ''}}">
                            <label for="complete_date" class="control-label">{{ 'Completed date' }}</label>
                            <input class="form-control" name="complete_date" type="text" id="complete_date" value="{{ isset($task->complete_date) ? $task->complete_date : ''}}" >
                            {!! $errors->first('complete_date', '<p class="help-block">:message</p>') !!}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="form-group">
    <label for="documents" class="control-label">{{ 'Documents' }} <i class="fa fa-link"></i></label>
    <select name="documents[]" id="documents" multiple class="form-control">
        @foreach($documents as $document)
            <option value="{{ $document->id }}" {{ isset($selected_documents) && in_array($document->id, $selected_documents)?"selected":"" }}>{{ $document->name }}</option>
        @endforeach
    </select>
</div>

@if(\Auth::user()->is_admin == 1)
    <div class="form-group {{ $errors->has('assigned_user_id') ? 'has-error' : ''}}">
        <label for="assigned_user_id" class="control-label">{{ 'Assigned User' }}</label>
        <select name="assigned_user_id" id="assigned_user_id" class="form-control">
            @foreach($users as $user)
                <option value="{{ $user->id }}" {{ isset($task->assigned_user_id) && $task->assigned_user_id == $user->id?"selected":"" }}>{{ $user->name }}</option>
            @endforeach
        </select>

        {!! $errors->first('assigned_user_id', '<p class="help-block">:message</p>') !!}
    </div>
@endif


<div class="form-group">
    <input class="btn btn-primary" type="submit" value="{{ $formMode === 'edit' ? 'Update' : 'Create' }}">
</div>

<input type="hidden" id="getContactsAjaxUrl" value="{{ url('/admin/api/contacts/get-contacts-by-status') }}" />

resources/views/pages/tasks/show.blade.php

@extends('layout.app')

@section('title', ' | Show task')

@section('content')

    <section class="content-header">
        <h1>
            task #{{ $task->id }}
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin/') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="{{ url('/admin/tasks') }}">Tasks</a></li>
            <li class="active">Show</li>
        </ol>
    </section>


    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">

                        <a href="{{ url('/admin/tasks') }}" title="Back"><button class="btn btn-warning btn-sm"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>

                        @if(user_can('edit_task'))
                            <a href="{{ url('/admin/tasks/' . $task->id . '/edit') }}" title="Edit task"><button class="btn btn-primary btn-sm"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button></a>
                        @endif

                        @if(user_can('delete_task'))
                            <form method="POST" action="{{ url('admin/tasks' . '/' . $task->id) }}" accept-charset="UTF-8" style="display:inline">
                                {{ method_field('DELETE') }}
                                {{ csrf_field() }}
                                <button type="submit" class="btn btn-danger btn-sm" title="Delete task" onclick="return confirm('Confirm delete?')"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</button>
                            </form>
                        @endif

                        <br/>
                        <br/>

                        <div class="table-responsive">
                            <table class="table">
                                <tbody>
                                    @if(\Auth::user()->is_admin == 1)
                                        <tr>
                                            <th>ID</th><td>{{ $task->id }}</td>
                                        </tr>
                                    @endif
                                    <tr><th> Name </th><td> {{ $task->name }} </td></tr>
                                    <tr><th> Priority </th><td> {{ $task->priority }} </td></tr>
                                    <tr><th> Status </th><td> {{ $task->getStatus->name }} </td></tr>
                                    <tr>
                                        <th>Type</th><td> {{ $task->type->name }}</td>
                                    </tr>
                                    <tr><th>Start date</th> <td>{{ $task->start_date }}</td></tr>
                                    <tr><th>End date</th> <td>{{ $task->end_date }}</td></tr>
                                    <tr><th>Complete date</th> <td>{{ $task->complete_date }}</td></tr>
                                    <tr><th>Contact</th> <td> {!! !empty($task->contact_type)&&!empty($task->contact_id)?'<a href="' . url('/admin/contacts/' . $task->contact_id) . '">'.$task->contact->getName() . " (<i class=\"btn bg-maroon\">".$task->contact_type."</i>)".'</a>':"" !!}</td></tr>
                                    <tr>
                                        <th>Description</th> <td>{!! $task->description !!}</td>
                                    </tr>
                                    @if(\Auth::user()->is_admin == 1)
                                        <tr><th> Created by </th><td>{{ $task->createdBy->name }}</td></tr>
                                        <tr><th> Modified by </th><td>{{ isset($task->modifiedBy->name)?$task->modifiedBy->name:"" }}</td></tr>
                                    @endif

                                    <tr><th> Assigned to </th><td>{{ $task->assignedTo != null ?$task->assignedTo->name : "not set" }}</td></tr>
                                    <tr><th> Created at </th><td>{{ $task->created_at }}</td></tr>
                                    <tr><th> Modified at </th><td>{{ $task->updated_at }}</td></tr>
                                    @if($task->documents->count() > 0)
                                        <tr><th>Documents </th> <td>@foreach($task->documents as $document) <a href="{{ url('uploads/documents/' . $document->file) }}">{{ $document->name }}</a> - @endforeach</td></tr>
                                    @endif
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

resources/views/pages/tasks/update_status.blade.php

@extends('layout.app')

@section('title', ' | Update task status')

@section('content')

    <section class="content-header">
        <h1>
            Update task status
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin/') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="{{ url('/admin/tasks') }}">Tasks</a></li>
            <li class="active">Update task status</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <a href="{{ url('/admin/tasks') }}" title="Back"><button class="btn btn-warning btn-sm"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
                        <br />
                        <br />

                        @if ($errors->any())
                            <ul class="alert alert-danger">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        @endif

                        <form method="POST" action="{{ url('/admin/tasks/' . $task->id . '/update-status') }}" accept-charset="UTF-8" enctype="multipart/form-data">
                            {{ csrf_field() }}

                            {{ method_field("put") }}

                            <div class="form-group {{ $errors->has('assigned_user_id') ? 'has-error' : ''}}">
                                <label for="status" class="control-label">{{ 'Status' }}</label>
                                <select name="status" id="status" class="form-control">
                                    @foreach($statuses as $status)
                                        @if($status->id != $task->status)
                                            <option value="{{ $status->id }}">{{ $status->name }}</option>
                                        @endif
                                    @endforeach
                                </select>

                                {!! $errors->first('status', '<p class="help-block">:message</p>') !!}
                            </div>

                            <div class="form-group">
                                <input class="btn btn-primary" type="submit" value="Update">
                            </div>

                        </form>

                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

resources/views/pages/tasks/assign.blade.php

@extends('layout.app')

@section('title', ' | Assign task')

@section('content')

    <section class="content-header">
        <h1>
            Assign task
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin/') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="{{ url('/admin/tasks') }}">Tasks</a></li>
            <li class="active">Assign</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <a href="{{ url('/admin/tasks') }}" title="Back"><button class="btn btn-warning btn-sm"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
                        <br />
                        <br />

                        @if ($errors->any())
                            <ul class="alert alert-danger">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        @endif

                        <form method="POST" action="{{ url('/admin/tasks/' . $task->id . '/assign') }}" accept-charset="UTF-8" enctype="multipart/form-data">
                            {{ csrf_field() }}

                            {{ method_field("put") }}

                            <div class="form-group {{ $errors->has('assigned_user_id') ? 'has-error' : ''}}">
                                <label for="assigned_user_id" class="control-label">{{ 'Assigned User' }}</label>
                                <select name="assigned_user_id" id="assigned_user_id" class="form-control">
                                    @foreach($users as $user)
                                        <option value="{{ $user->id }}">{{ $user->name }}</option>
                                    @endforeach
                                </select>

                                {!! $errors->first('assigned_user_id', '<p class="help-block">:message</p>') !!}
                            </div>

                            <div class="form-group">
                                <input class="btn btn-primary" type="submit" value="Assign">
                            </div>

                        </form>

                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

I have made a lot of updates to the above files. At first i have adjusted the permissions, this can be seen in the index view file using the user_can() function. Also i have updated the form UI and updated the inputs such as the dropdowns for contact type and contact id.

The contact name dropdown will be filled by ajax when the user select a contact type, we will see the task javascript file below.

 

Tasks Javascript Code

public/theme/views/tasks/form.js

/**
 * Contacts Form Api
 */

$(function () {

    $("#documents").select2();

    $("#start_date, #end_date, #complete_date").datepicker();
    
    $("#contact_type").on("change", function (e) {

        $("#contact_id").html("");

        if($(this).val() == "")
            return;

        var selected_contact_id = $("#contact_id").attr("data-selected-value")!=""?$("#contact_id").attr("data-selected-value"):"";

        getContacts($(this).val(), selected_contact_id);
    });

    if($("#contact_type").val() != "") {

        $("#contact_type").trigger("change");
    }
});


/**
 * get Contacts
 *
 *
 * @param status
 * @param selected_contact_id
 */
var getContacts = function (status, selected_contact_id)
{
    $.ajax({
        url: $("#getContactsAjaxUrl").val(),
        data: {status: status},
        method: "GET",
        dataType: "json",
        success: function (response) {
            if(response.length > 0) {
                for(var i=0; i<response.length; i++) {
                    $("#contact_id").append('<option value="'+ response[i].id +'" ' + (selected_contact_id == response[i].id?'selected':'') + '>'+ response[i].first_name + (response[i].middle_name != null?' ' + response[i].middle_name + ' ':' ') + (response[i].last_name!=null? response[i].last_name:'') + '</option>');
                }
            }
        }
    });
};

In the above code we initialized the “#documents” selector with select2 plugin, then we initialized the datepickers. then i added a function to be triggered when the contact name dropdown field get changed which is getContacts(), this function make an ajax request to the contacts controller to retrieve the contacts by the status.

 

Update the contacts controller

Now update the contacts controller and add this function at the end of the controller:

   
/**
     * get Contacts By Status
     *
     *
     * @param Request $request
     * @return array
     */
    public function getContactsByStatus(Request $request)
    {
        if(!$request->status)
            return [];


        $contacts = Contact::where('contact_status.name', $request->status)
            ->join('contact_status', 'contact_status.id', '=', 'contact.status');

        if(Auth::user()->is_admin == 1) {

            return $contacts->get();
        }

        return $contacts->where(function ($query) {
            $query->where('assigned_user_id', Auth::user()->id)
                  ->orWhere('created_by_id', Auth::user()->id);
        })->get();
    }

Modify app/Helpers/MailerFactory.php add these functions at the end of the class

/**
     * send Assign Task Email
     *
     *
     * @param $subject
     * @param $user
     * @param $task
     */
    public function sendAssignTaskEmail($subject, $user, $task)
    {
        try {
            $this->mailer->send("emails.assign_task", ['user' => $user, 'task' => $task, 'subject' => $subject], function($message) use ($subject, $user) {

                $message->from($this->fromAddress, $this->fromName)
                    ->to($user->email)->subject($subject);

            });
        } catch (\Exception $ex) {
            die("Mailer Factory error: " . $ex->getMessage());
        }
    }


    /**
     * send Update Task Status Email
     *
     *
     * @param $subject
     * @param $user
     * @param $contact
     */
    public function sendUpdateTaskStatusEmail($subject, $user, $task)
    {
        try {
            $this->mailer->send("emails.update_task_status", ['user' => $user, 'task' => $task, 'subject' => $subject], function($message) use ($subject, $user) {

                $message->from($this->fromAddress, $this->fromName)
                    ->to($user->email)->subject($subject);

            });
        } catch (\Exception $ex) {
            die("Mailer Factory error: " . $ex->getMessage());
        }
    }


    /**
     * send Delete Task Email
     *
     *
     * @param $subject
     * @param $user
     * @param $task
     */
    public function sendDeleteTaskEmail($subject, $user, $task)
    {
        try {
            $this->mailer->send("emails.delete_task", ['user' => $user, 'task' => $task, 'subject' => $subject], function($message) use ($subject, $user) {

                $message->from($this->fromAddress, $this->fromName)
                    ->to($user->email)->subject($subject);

            });
        } catch (\Exception $ex) {
            die("Mailer Factory error: " . $ex->getMessage());
        }
    }

resources/views/emails/assign_task.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ $subject }}</title>
</head>
<body>
<p>
    Hello {{ $user->name }},
</p>

<p>
    You have been assigned a task, go and check them here <a href="{{ url('admin/tasks/' . $task->id) }}"> {{ url('admin/tasks/' . $task->id) }} </a>
</p>
</body>
</html>

resources/views/emails/update_task_status.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ $subject }}</title>
</head>
<body>
<p>
    Hello {{ $user->name }},
</p>

<p>
    Task "{{ $task->name }}" have been updated to status "{{ $task->getStatus->name }}" you can view it here <a href="{{ url('admin/tasks/' . $task->id) }}"> {{ url('admin/tasks/' . $task->id) }} </a>
</p>
</body>
</html>

resources/views/emails/delete_task.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ $subject }}</title>
</head>
<body>
<p>
    Hello {{ $user->name }},
</p>

<p>
    The task {{ $task->name }} have been deleted.
</p>
</body>
</html>

 

Updating routes

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () {

    Route::get('/', function () {
        return view('pages.home.index');
    });

    Route::resource('/users', 'UsersController');

    Route::get('/my-profile', 'UsersController@getProfile');

    Route::get('/my-profile/edit', 'UsersController@getEditProfile');

    Route::patch('/my-profile/edit', 'UsersController@postEditProfile');

    Route::resource('/permissions', 'PermissionsController');

    Route::resource('/roles', 'RolesController');

    Route::get('/users/role/{id}', 'UsersController@getRole');

    Route::put('/users/role/{id}', 'UsersController@updateRole');

    Route::resource('/documents', 'DocumentsController');

    Route::get('/documents/{id}/assign', 'DocumentsController@getAssignDocument');

    Route::put('/documents/{id}/assign', 'DocumentsController@postAssignDocument');

    Route::resource('/contacts', 'ContactsController');

    Route::get('/contacts/{id}/assign', 'ContactsController@getAssignContact');

    Route::put('/contacts/{id}/assign', 'ContactsController@postAssignContact');

    Route::get('/api/contacts/get-contacts-by-status', 'ContactsController@getContactsByStatus');

    Route::resource('/tasks', 'TasksController');

    Route::get('/tasks/{id}/assign', 'TasksController@getAssignTask');

    Route::put('/tasks/{id}/assign', 'TasksController@postAssignTask');

    Route::get('/tasks/{id}/update-status', 'TasksController@getUpdateStatus');

    Route::put('/tasks/{id}/update-status', 'TasksController@postUpdateStatus');

    Route::get('/forbidden', function () {
        return view('pages.forbidden.forbidden_area');
    });
});

Route::get('/', function () {
   return redirect()->to('/admin');
});

Auth::routes();

 

Updating the sidebar

Now let’s update the sidebar with tasks module

resources/views/layout/sidebar.blade.php

<!-- Left side column. contains the logo and sidebar -->
<aside class="main-sidebar">
    <!-- sidebar: style can be found in sidebar.less -->
    <section class="sidebar">
        <!-- Sidebar user panel -->
        <div class="user-panel">
            <div class="pull-left image">
                @if(\Auth::user()->image != null)
                    <img src="{{ url('uploads/users/' . \Auth::user()->image) }}" class="img-circle" alt="User Image">
                @else
                    <img src="{{ url('theme/dist/img/image_placeholder.png') }}" class="img-circle" alt="User Image">
                @endif
            </div>
            <div class="pull-left info">
                <p>{{ \Auth::user()->name }}</p>
            </div>
        </div>
        <!-- sidebar menu: : style can be found in sidebar.less -->
        <ul class="sidebar-menu" data-widget="tree">
            <li class="header">MAIN NAVIGATION</li>
            <li class="{{ Request::segment(2) == ""?"active":"" }}">
                <a href="{{ url('/admin') }}">
                    <i class="fa fa-dashboard"></i> <span>Dashboard</span>
                </a>
            </li>

            @if(user_can('list_contacts'))
                <li class="treeview {{ Request::segment(2) == 'contacts'? 'active':'' }}">
                    <a href="#">
                        <i class="fa fa-address-card"></i> <span>Accounts</span>
                        <span class="pull-right-container">
                            <i class="fa fa-angle-left pull-right"></i>
                        </span>
                    </a>
                    <ul class="treeview-menu">
                        <li class="{{ Request::segment(2) == "contacts" && request('status_name') == null?"active":"" }}">
                            <a href="{{ url('/admin/contacts') }}"><i class="fa fa-list"></i> All contacts</a>
                        </li>
                        <li class="{{ Request::segment(2) == "contacts" && request('status_name') == 'Lead'?"active":"" }}">
                            <a href="{{ url('/admin/contacts?status_name=Lead') }}"><i class="fa fa-leaf"></i> Leads</a>
                        </li>
                        <li class="{{ Request::segment(2) == "contacts" && request('status_name') == 'Opportunity'?"active":"" }}">
                            <a href="{{ url('/admin/contacts?status_name=Opportunity') }}"><i class="fa fa-flag"></i> Opportunities</a>
                        </li>
                        <li class="{{ Request::segment(2) == "contacts" && request('status_name') == 'Customer'?"active":"" }}">
                            <a href="{{ url('/admin/contacts?status_name=Customer') }}"><i class="fa fa-user-circle"></i> Customers</a>
                        </li>
                        <li class="{{ Request::segment(2) == "contacts" && request('status_name') == 'Close'?"active":"" }}">
                            <a href="{{ url('/admin/contacts?status_name=Close') }}"><i class="fa fa-ban"></i> Close</a>
                        </li>
                    </ul>
                </li>
            @endif

            @if(user_can('list_documents'))
                <li class="{{ Request::segment(2) == "documents"?"active":"" }}">
                    <a href="{{ url('/admin/documents') }}">
                        <i class="fa fa-file-word-o"></i> <span>Documents</span>
                    </a>
                </li>
            @endif

            @if(user_can('list_tasks'))
                <li class="{{ Request::segment(2) == "tasks"?"active":"" }}">
                    <a href="{{ url('/admin/tasks') }}">
                        <i class="fa fa-tasks"></i> <span>Tasks</span>
                    </a>
                </li>
            @endif

            @if(\Auth::user()->is_admin == 1)
                <li class="{{ in_array(Request::segment(2), ['users', 'permissions', 'roles'])?"active":"" }} treeview">
                    <a href="#">
                        <i class="fa fa-users"></i> <span>User Managment</span>
                        <span class="pull-right-container">
                            <i class="fa fa-angle-left pull-right"></i>
                        </span>
                    </a>
                    <ul class="treeview-menu">
                        <li class="{{ Request::segment(2) == "users"?"active":"" }}">
                            <a href="{{ url('/admin/users') }}"><i class="fa fa-user-o"></i> Users</a>
                        </li>

                        <li class="{{ Request::segment(2) == "permissions"?"active":"" }}">
                            <a href="{{ url('/admin/permissions') }}"><i class="fa fa-ban"></i> Permissions</a>
                        </li>
                        <li class="{{ Request::segment(2) == "roles"?"active":"" }}">
                            <a href="{{ url('/admin/roles') }}"><i class="fa fa-list"></i> Roles</a>
                        </li>
                    </ul>
                </li>
            @endif
        </ul>
    </section>
    <!-- /.sidebar -->
</aside>

Updating the user profile

To finish this part we will conclude by updating the view user profile page to include the tasks assigned to the currently logged in user:

resources/views/pages/users/profile/view.blade.php

@extends('layout.app')

@section('title', ' | My Profile')

@section('content')

    <section class="content-header">
        <h1>
            My Profile
        </h1>
    </section>


    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">

                        @include('includes.flash_message')

                        @if(user_can('edit_profile'))
                            <a href="{{ url('/admin/my-profile/edit') }}" title="Edit profile"><button class="btn btn-primary btn-sm"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button></a>
                        @endif
                        <br/>
                        <br/>

                        <div class="table-responsive">
                            <table class="table">
                                <tbody>

                                @if(!empty($user->image))
                                    <tr>
                                        <td>
                                            <img src="{{ url('uploads/users/' . $user->image) }}" class="pull-right" width="200" height="200" />
                                        </td>
                                    </tr>
                                @endif

                                <tr><th> Name </th><td> {{ $user->name }} </td>
                                </tr><tr><th> Email </th><td> {{ $user->email }} </td></tr>
                                <tr><th> Position Title </th><td> {{ $user->position_title }} </td></tr>
                                <tr><th> Phone </th><td> {{ $user->phone }} </td></tr>

                                </tbody>
                            </table>

                            <hr/>

                            @if(user_can('list_documents'))
                                <h3>Documents assigned</h3>
                                @if($user->documents->count() > 0)
                                    <table class="table">
                                        <tr>
                                            <th>Name</th>
                                            <th>View</th>
                                        </tr>
                                        <tbody>
                                        @foreach($user->documents as $document)
                                            <tr>
                                                <td>{{ $document->name }}</td>
                                                <td>
                                                    @if(user_can("view_document"))
                                                        <a href="{{ url('/admin/documents/' . $document->id) }}"><i class="fa fa-camera"></i></a>
                                                    @endif
                                                </td>
                                            </tr>
                                        @endforeach
                                        </tbody>
                                    </table>
                                @else
                                    <p>No documents assigned</p>
                                @endif
                            @endif

                            @if(user_can('list_contacts'))
                                <h3>Contacts assigned</h3>
                                <div class="box-body">
                                    <div class="box-group" id="accordion">
                                        <div class="panel box box-primary">
                                            <div class="box-header with-border">
                                                <h4 class="box-title">
                                                    <a data-toggle="collapse" data-parent="#accordion" href="#contacts">
                                                        All contacts
                                                    </a>
                                                </h4>
                                            </div>
                                            <div id="contacts" class="panel-collapse collapse in">
                                                <div class="box-body">
                                                    @if($user->contacts->count() > 0)
                                                        <table class="table">
                                                            <tr>
                                                                <th>Name</th>
                                                                <th>View</th>
                                                            </tr>
                                                            <tbody>
                                                            @foreach($user->contacts as $contact)
                                                                <tr>
                                                                    <td>{{ $contact->getName() }}</td>
                                                                    <td>
                                                                        @if(user_can("view_contact"))
                                                                            <a href="{{ url('/admin/contacts/' . $contact->id) }}" class="btn btn-info btn-sm"><i class="fa fa-camera"></i></a>
                                                                        @endif
                                                                    </td>
                                                                </tr>
                                                            @endforeach
                                                            </tbody>
                                                        </table>
                                                    @else
                                                        <p>No contacts assigned</p>
                                                    @endif
                                                </div>
                                            </div>
                                        </div>
                                        <div class="panel box box-primary">
                                            <div class="box-header with-border">
                                                <h4 class="box-title">
                                                    <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#my_leads">
                                                        My leads
                                                    </a>
                                                </h4>
                                            </div>
                                            <div id="my_leads" class="panel-collapse collapse">
                                                <div class="box-body">
                                                    @if($user->leads->count() > 0)
                                                        <table class="table">
                                                            <tr>
                                                                <th>Name</th>
                                                                <th>View</th>
                                                            </tr>
                                                            <tbody>
                                                            @foreach($user->leads as $contact)
                                                                <tr>
                                                                    <td>{{ $contact->getName() }}</td>
                                                                    <td>
                                                                        @if(user_can("view_contact"))
                                                                            <a href="{{ url('/admin/contacts/' . $contact->id) }}" class="btn btn-info btn-sm"><i class="fa fa-camera"></i></a>
                                                                        @endif
                                                                    </td>
                                                                </tr>
                                                            @endforeach
                                                            </tbody>
                                                        </table>
                                                    @else
                                                        <p>No leads assigned</p>
                                                    @endif
                                                </div>
                                            </div>
                                        </div>
                                        <div class="panel box box-primary">
                                            <div class="box-header with-border">
                                                <h4 class="box-title">
                                                    <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#my_opportunities">
                                                        My opportunities
                                                    </a>
                                                </h4>
                                            </div>
                                            <div id="my_opportunities" class="panel-collapse collapse">
                                                <div class="box-body">
                                                    @if($user->opportunities->count() > 0)
                                                        <table class="table">
                                                            <tr>
                                                                <th>Name</th>
                                                                <th>View</th>
                                                            </tr>
                                                            <tbody>
                                                            @foreach($user->opportunities as $contact)
                                                                <tr>
                                                                    <td>{{ $contact->getName() }}</td>
                                                                    <td>
                                                                        @if(user_can("view_contact"))
                                                                            <a href="{{ url('/admin/contacts/' . $contact->id) }}" class="btn btn-info btn-sm"><i class="fa fa-camera"></i></a>
                                                                        @endif
                                                                    </td>
                                                                </tr>
                                                            @endforeach
                                                            </tbody>
                                                        </table>
                                                    @else
                                                        <p>No opportunities assigned</p>
                                                    @endif
                                                </div>
                                            </div>
                                        </div>
                                        <div class="panel box box-primary">
                                            <div class="box-header with-border">
                                                <h4 class="box-title">
                                                    <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#my_customers">
                                                        My customers
                                                    </a>
                                                </h4>
                                            </div>
                                            <div id="my_customers" class="panel-collapse collapse">
                                                <div class="box-body">
                                                    @if($user->customers->count() > 0)
                                                        <table class="table">
                                                            <tr>
                                                                <th>Name</th>
                                                                <th>View</th>
                                                            </tr>
                                                            <tbody>
                                                            @foreach($user->customers as $contact)
                                                                <tr>
                                                                    <td>{{ $contact->getName() }}</td>
                                                                    <td>
                                                                        @if(user_can("view_contact"))
                                                                            <a href="{{ url('/admin/contacts/' . $contact->id) }}" class="btn btn-info btn-sm"><i class="fa fa-camera"></i></a>
                                                                        @endif
                                                                    </td>
                                                                </tr>
                                                            @endforeach
                                                            </tbody>
                                                        </table>
                                                    @else
                                                        <p>No customers assigned</p>
                                                    @endif
                                                </div>
                                            </div>
                                        </div>
                                        <div class="panel box box-primary">
                                            <div class="box-header with-border">
                                                <h4 class="box-title">
                                                    <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#my_archives">
                                                        My archives / Close
                                                    </a>
                                                </h4>
                                            </div>
                                            <div id="my_archives" class="panel-collapse collapse">
                                                <div class="box-body">
                                                    @if($user->archives->count() > 0)
                                                        <table class="table">
                                                            <tr>
                                                                <th>Name</th>
                                                                <th>View</th>
                                                            </tr>
                                                            <tbody>
                                                            @foreach($user->archives as $contact)
                                                                <tr>
                                                                    <td>{{ $contact->getName() }}</td>
                                                                    <td>
                                                                        @if(user_can("view_contact"))
                                                                            <a href="{{ url('/admin/contacts/' . $contact->id) }}" class="btn btn-info btn-sm"><i class="fa fa-camera"></i></a>
                                                                        @endif
                                                                    </td>
                                                                </tr>
                                                            @endforeach
                                                            </tbody>
                                                        </table>
                                                    @else
                                                        <p>No archives assigned</p>
                                                    @endif
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            @endif

                            @if(user_can('list_tasks'))
                                <h3>Tasks assigned</h3>
                                @if($user->tasks->count() > 0)
                                    <table class="table">
                                        <tr>
                                            <th>Name</th>
                                            <th>View</th>
                                        </tr>
                                        <tbody>
                                        @foreach($user->tasks as $task)
                                            <tr>
                                                <td>{{ $task->name }}</td>
                                                <td>
                                                    @if(user_can("view_task"))
                                                        <a href="{{ url('/admin/tasks/' . $task->id) }}"><i class="fa fa-camera"></i></a>
                                                    @endif
                                                </td>
                                            </tr>
                                        @endforeach
                                        </tbody>
                                    </table>
                                @else
                                    <p>No tasks assigned</p>
                                @endif
                            @endif

                        </div>

                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

 

 

Continue to Part 10: Mailbox module

 

0 0 vote
Article Rating
Share this: