Skip to content

Instantly share code, notes, and snippets.

@ianrodrigues
Created March 15, 2017 13:35
Show Gist options
  • Save ianrodrigues/7de02491cc744ba3a9b8b2dff3bcd674 to your computer and use it in GitHub Desktop.
Save ianrodrigues/7de02491cc744ba3a9b8b2dff3bcd674 to your computer and use it in GitHub Desktop.
<?php
class JobController extends BaseController {
public function index()
{
$search = Input::get('search');
$filter_client = Input::get('client');
$filter_type = Input::get('type');
$user = Auth::user();
$query = Job::orderBy('status')->orderBy('name')->orderBy('date_prompt', 'DESC');
$query_filter = $query;
$jobs = $query_filter->get();
$filter_types = array();
$filter_clients = array();
foreach ($jobs as $job) {
if ($job->jobtype_id) {
$type = $job->jobtype;
if(!in_array($type, $filter_types))
array_push($filter_types, $type);
}
if ($job->client_id) {
$client = $job->client;
if(!in_array($client, $filter_clients))
array_push($filter_clients, $client);
}
}
function so($a, $b) { return (strcmp ($a['name'],$b['name']));}
uasort($filter_types, 'so');
uasort($filter_clients, 'so');
if ($search) {
if(is_numeric($search) && !str_contains($search, ".")) {
$query->where('jobs.id', (int) $search);
}
else {
$query->where('name', 'ILIKE', '%'.$search.'%');
}
}
if ($filter_client)
$query->where('client_id', $filter_client);
if ($filter_type)
$query->where('jobtype_id', $filter_type);
$count = count($query->get());
if($count == 1)
return Redirect::to('job/'.$query->get()[0]->id);
$this->data['status'] = Config::get('view.job_status');
$this->data['filter_types'] = $filter_types;
$this->data['filter_clients'] = $filter_clients;
$this->data['job_total'] = $count;
//$this->data['recents'] = Job::mostRecentlyModified()->limit(3)->get();
$this->data['list'] = $query->orderBy('name')->orderBy('jobtype_id')->orderBy('client_id')->orderBy('status')->paginate(LIMIT_PAG);
return $this->render($this->data);
}
public function show($id)
{
$job = Job::find($id);
$search = Input::get('search');
$job->date_finish = ($job->date_finish)? $this->toViewDate($job->date_finish) : '';
$services = Service::fromJob($id);
if ($search) {
if(is_numeric($search) && !str_contains($search, ".")) {
$services->where('id', (int) $search);
}
else {
$services->where('name', 'ILIKE', '%'.$search.'%');
}
}
$services = $services->orderBy('name')->get();
if ($search && count($services) == 1)
return Redirect::to('service/'.$services[0]->id);
$this->data['roles'] = Auth::user()->let($job)->getRoles();
$this->data['reg'] = $job;
$this->data['services'] = $services;
$this->data['users'] = User::where('active', true)->orderBy('name')->get();
$this->data['attachtypes'] = AttachType::orderBy('name')->get();
$this->data['status'] = Config::get('view.job.status');
$this->data['task_status'] = Config::get('view.task.status');
$this->data['streaming'] = $job->streamings()->orderBy('created_at', 'desc')->get();
$this->data['attachs'] = $job->attachs()->get();
$this->data['job_access'] = Config::get('view.job.profiles');
$this->data['job_users'] = $job->users();
$this->data['statistics'] = $job->getStatistics();
$this->data['activity'] = $job->getActivity(30);
$this->data['requeriments'] = $job->requeriments()->where('status', '<', Requeriment::getStatusParamByKey('done','value'))->orderBy('status')->get();
$this->data['requeriments_total'] = count($job->requeriments()->where('status', '<', Requeriment::getStatusParamByKey('done', 'value'))->get());
$this->data['requeriments_status'] = Config::get('view.requeriment.status');
$this->data['requeriments_types'] = Config::get('view.requeriment.types');
$this->data['supports'] = $job->supports()->where('status', '<', Task::getStatusParamByKey('done', 'value'))->orderBy('status')->get();
$this->data['supports_total'] = count($job->supports()->where('status', '<', Task::getStatusParamByKey('done', 'value'))->get());
$this->data['supports_status'] = Config::get('view.support.status');
$this->useComponents(array('bootstrap-select', 'tag-it', 'rater', 'chart', 'editor', 'maskmonkey', 'datetimepicker'));
return $this->render($this->data);
}
public function create()
{
$this->data['jobtypes'] = Jobtype::orderBy('name')->get();
$this->data['clients'] = Client::orderBy('name')->get();
$this->data['users'] = User::orderBy('name')->get();
$this->useComponents(array('datetimepicker','editor', 'maskmonkey'));
return $this->render($this->data);
}
public function store()
{
try {
$name = Input::get('name');
$jobtype = Input::get('jobtype');
$client = Input::get('client');
$date_prompt = $this->toDBDateTime(Input::get('date_prompt'));
$url = Input::get('url');
$repository = Input::get('repository');
$description = Input::get('description');
$start_parts = explode('/', Input::get('date_prompt'));
$finish_parts = explode('/', Input::get('date_finish'));
$reg = new Job;
$reg->name = $name;
$reg->jobtype_id = ($jobtype)? $jobtype : null;
$reg->client_id = ($client)? $client : null;
$reg->status = Job::getStatusParamByKey('todo', 'value');
$reg->date_prompt = ($date_prompt)? $date_prompt : null;
$reg->url = ($url)? $url : null;
$reg->repository = ($repository)? $repository : null;
$reg->description = ($description)? htmlentities($description) : '';
$reg->created_at = time();
$reg->created_by = Auth::user()->id;
$reg->save();
$this->data['reg'] = $reg->toArray();
$this->message = lang::get('job.success_create');
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
Session::flash('message', array(($this->success=='YES')? 'success' : 'danger', $this->message));
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function edit($id)
{
$job = Job::find($id);
$this->data['reg'] = $job;
$this->data['jobtypes'] = Jobtype::orderBy('name')->get();
$this->data['clients'] = Client::orderBy('name')->get();
$this->data['status'] = Config::get('view.job_status');
$this->data['date_prompt'] = $this->toViewDate($job->date_prompt);;
$this->data['date_finish'] = $this->toViewDate($job->date_finish);;
$this->useComponents(array('datetimepicker','editor' , 'maskmonkey'));
return $this->render($this->data);
}
public function update($id)
{
$success = "NO";
$message = "";
$data = array();
try {
$reg = Job::find($id);
$name = Input::get('name');
$jobtype = Input::get('jobtype');
$client = Input::get('client');
$url = Input::get('url');
$repository = Input::get('repository');
$description = Input::get('description');
$reg->name = $name;
$reg->jobtype_id = ($jobtype)? $jobtype : null;
$reg->client_id = ($client)? $client : null;
$reg->url = ($url)? $url : '';
$reg->repository = ($repository)? $repository : null;
$reg->description = ($description)? htmlentities($description) : '';
$reg->updated_at = time();
$reg->updated_by = Auth::user()->id;
$reg->save();
$reg->addStreaming(Lang::get('system.streaming_info_updated'), '', '', $reg->status, $reg->toJson());
$message = lang::get('job.success_update');
$success = "YES";
} catch (Exception $e) {
$message = $e->getMessage();
}
Session::flash('message', array(($success=="YES")? 'success' : 'danger', $message));
return Response::json(array('success' => $success, 'message' => $message, 'data' => $data));
}
public function updateStatus($id)
{
try {
$reg = Job::find($id);
switch(Input::get('status')) {
case Job::getStatusParamByKey('todo', 'value'):
$reg->restart();
break;
case Job::getStatusParamByKey('doing', 'value'):
($reg->isTodo())? $reg->start() : $reg->restart();
break;
case Job::getStatusParamByKey('done', 'value'):
$reg->done();
$this->data['reg']['date_finish'] = $this->toViewDate($reg->date_finish);
break;
case Job::getStatusParamByKey('paused', 'value'):
($reg->isPending())? $reg->resolve() : $reg->pause();
break;
case Job::getStatusParamByKey('discarded', 'value'):
$reg->pending(Input::get('status_description'));
break;
}
$this->message = Lang::get('system.status_updated_success');
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
Session::flash('message', array(($this->success=="YES")? 'success' : 'danger', $this->message));
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function updateResponsible($id)
{
try {
$reg = Job::find($id);
$reg->updateResponsible(Input::get('responsible'));
$this->message = Lang::get('system.success_update_responsible');
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
Session::flash('message', array(($this->success=="YES")? 'success' : 'danger', $this->message));
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function comment($id)
{
try {
$reg = Job::find($id);
if (!Input::get('text'))
throw new Exception (Lang::get('system.comment_forgetful'));
else
$reg->addStreaming(Lang::get('system.user_comment'), Input::get('text'), 'info');
$this->message = Lang::get('system.comment_created_success');
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function destroy($id)
{
$success = 'NO';
$message = '';
$data = array();
try {
if (!Auth::user()->isAdmin())
throw new Exception(Lang::get('job.cannot_delete'));
$reg = Job::find($id);
$reg->delete();
$message = Lang::get('job.success_delete');
$success = 'YES';
} catch (Exception $e) {
$message = $e->getMessage();
}
Session::flash('message', array(($success=='YES')? 'success' : 'danger', $message));
return Response::json(array('success' => $success, 'message' => $message, 'data' => $data));
}
public function upload($id)
{
try {
$reg = Job::find($id);
$this->data['reg']['attach_id'] = $reg->attach($this->processUpload('file', '/attachs/', rand().'_'.time()));
$reg->addStreaming(Lang::get('system.streaming_attach_added'));
$this->message = Lang::get('system.upload_success');
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function deletefile($id)
{
try {
$attach_id = Input::get('id');
$job = Job::find($id);
if (!$job)
throw new Exception(Lang::get('task.not_found'));
$job->deleteAttach($attach_id);
$this->message = Lang::get('system.file_deleted_success');
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
Session::flash('message', array(($this->success=='YES')? 'success' : 'danger', $this->message));
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function deleteAllAttachs($id)
{
try {
$job_attachs = JobAttach::where('job_id', $id)->get();
foreach ($job_attachs as $job_attach)
$job_attach->delete();
$this->message = Lang::get('system.attachs_deleted_success');
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
Session::flash('message', array(($this->success=='YES')? 'success' : 'danger', $this->message));
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
private function can($operation)
{
$user = Auth::user();
switch($operation) {
case 'seeFinanceInfo':
return $user->canSeeFinanceInfo();
break;
default:
return false;
}
}
public function modalPopulate()
{
try {
$task = Job::find(Input::get('job_id'));
if($task->date_prompt)
$task->date_prompt = date('d/m/Y H:i', strtotime($task->date_prompt));
$this->data['reg'] = $task->toArray();
$this->message = Lang::get('job.success_update');;
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function updateDatePrompt($id)
{
try {
if (!Input::get('date_prompt'))
throw new Exception (sprintf(Lang::get('system.empty_field'), Lang::get('job.date_prompt')));
$reg = Job::find($id);
$reg->date_prompt = $this->toDBDateTime(Input::get('date_prompt'));
$reg->save();
$this->data['reg']['date_prompt'] = date('d/m/Y H:i', strtotime($reg->date_prompt));
$reg->addStreaming(sprintf(Lang::get('job.streaming_update_date_prompt'), date('d/m/Y H:i', strtotime($reg->date_prompt))));
$this->message = Lang::get('job.success_update_date_prompt');
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
Session::flash('message', array(($this->success=='YES')? 'success' : 'danger', $this->message));
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function services($id)
{
$list = array();
try {
$list = Service::fromJob($id)->orderBy('status')->orderBy('created_at', 'desc')->get();
$this->data['list'] = $list;
$this->success = 'YES';
} catch (Exception $e) {
$message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function addUser($id)
{
$job = Job::find($id);
$user = User::find(Input::get('user_id'));
$access = Input::get('access');
try {
if (!$job)
throw new Exception(Lang::get('job.not_found'));
if (!$user)
throw new Exception(Lang::get('user.not_found'));
$job->addUser($user->id, $access);
$job->addStreaming(sprintf(Lang::get('job.streaming_user_added'), url('user/'.$user->id), $user->name, JobUser::getAccessConfig($access)['text']), '', null, null, $job->toJson());
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function removeUser($id)
{
$job = Job::find($id);
$user = User::find(Input::get('user_id'));
try {
if (!$job)
throw new Exception(Lang::get('job.not_found'));
if (!$user)
throw new Exception(Lang::get('user.not_found'));
$job->removeUser($user->id);
$job->addStreaming(sprintf(Lang::get('job.streaming_user_removed'), url('user/'.$user->id), $user->name), '', null, null, $job->toJson());
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function supports($id)
{
$job = Job::find($id);
try {
if (!$job)
throw new Exception(Lang::get('job.not_found'));
$this->data = json_decode($job->supports()->orderBy('status')->paginate(15)->toJson());
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function getSprints($id)
{
try {
if (!is_numeric($id))
throw new Exception(Lang::get('job.not_found'));
$job = Job::find($id);
if (!$job)
throw new Exception(Lang::get('job.not_found'));
$sprints = Sprint::fromJob($id)->orderBy('date_start');
$sprints->forward();
$this->data = json_decode($sprints->get()->toJson());
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function getServices($id)
{
try {
if (!is_numeric($id))
throw new Exception(Lang::get('job.not_found'));
$job = Job::find($id);
$undone = (Input::get('undone'))? true : false;
if (!$job)
throw new Exception(Lang::get('job.not_found'));
$services = Service::fromJob($id)->orderBy('date_begin');
//if ($undone)
$services->undone();
$this->data = json_decode($services->get()->toJson());
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
public function getUsers($id)
{
try {
if (!is_numeric($id))
throw new Exception(Lang::get('job.not_found'));
$job = Job::find($id);
if (!$job)
throw new Exception(Lang::get('job.not_found'));
$this->data = $job->users();
$this->success = 'YES';
} catch (Exception $e) {
$this->message = $e->getMessage();
}
return Response::json(array('success' => $this->success, 'message' => $this->message, 'data' => $this->data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment