File: /var/www/vhost/disk-apps/teamdemo.sports-crowd.com/app/Http/Controllers/OtherUsersController.php
<?php
namespace App\Http\Controllers;
use App\AcademyLocation;
use App\AcademyLocationUser;
use App\AcademyParameter;
use App\Parameter;
use App\Rol;
use App\Services\AcademyLocationsService;
use App\Tag;
use App\User;
use App\UserTag;
use Datatables;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class OtherUsersController extends Controller
{
private $__ROLESTOIGNORE = [];
public function __construct()
{
$this->__ROLESTOIGNORE = [$this->__SUPERADMIN_ROL, $this->__MESSENGER_ROL, $this->__CLIENT_ROL];
}
public function index()
{
$rols = Rol::select('id', 'name')->whereNotIn('id', ($this->__ROLESTOIGNORE))->where('active', true)->orderBy('name', 'ASC')->get();
$academyLocationsService = new AcademyLocationsService;
$showFranchises = $academyLocationsService->showFranchises();
return view('users.other-users', compact('rols', 'showFranchises'));
}
public function filter(Request $request)
{
DB::statement("SET sql_mode = ''");
if ($request['rol'] == 'Todos') {
$obj = $obj = DB::table('users')
->select(
'users.id',
'users.first_name',
'users.last_name',
'users.email',
'rols.id as rolId',
'rols.name as rol',
DB::raw('GROUP_CONCAT(DISTINCT(tags.name)) AS segmentation'),
'users.created_at',
'users.active',
DB::raw('GROUP_CONCAT(DISTINCT(academy_locations.name) SEPARATOR ", ") AS locations')
)
->join('rols', 'rols.id', '=', 'users.rol_id')
->leftjoin('user_tags', 'users.id', '=', 'user_tags.user_id')
->leftjoin('tags', function ($join) {
$join->on('tags.id', '=', 'user_tags.tag_id')->where('tags.active', 1);
})
->leftjoin('academy_location_users', 'users.id', '=', 'academy_location_users.user_id')
->leftjoin('academy_locations', 'academy_locations.id', '=', 'academy_location_users.academy_location_id')
->whereNotIn('rols.id', ($this->__ROLESTOIGNORE))
->whereNull('users.deleted_at');
} else {
$obj = $obj = DB::table('users')
->select(
'users.id',
'users.first_name',
'users.last_name',
'users.email',
'rols.id as rolId',
'rols.name as rol',
DB::raw('GROUP_CONCAT(DISTINCT(tags.name)) AS segmentation'),
'users.created_at',
'users.active',
DB::raw('GROUP_CONCAT(DISTINCT(academy_locations.name) SEPARATOR ", ") AS locations')
)
->join('rols', 'rols.id', '=', 'users.rol_id')
->leftjoin('user_tags', 'users.id', '=', 'user_tags.user_id')
->leftjoin('tags', function ($join) {
$join->on('tags.id', '=', 'user_tags.tag_id')->where('tags.active', 1);
})
->leftjoin('academy_location_users', 'users.id', '=', 'academy_location_users.user_id')
->leftjoin('academy_locations', 'academy_locations.id', '=', 'academy_location_users.academy_location_id')
->whereIn('rols.id', $request['rol'])
->whereNull('users.deleted_at');
}
$academyLocationsService = new AcademyLocationsService;
if (!$academyLocationsService->validateAuthorizedLocations()) {
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
$obj->whereIn('academy_locations.id', $authorizedLocations);
}
$obj = $obj->groupBy('users.id');
DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");
return Datatables::of($obj)
->addColumn('actions', function ($obj) {
$actions = '<i class="fa fa-pencil iconMini" onClick="clickEditOtherUsers(' . $obj->id . ')" data-id="' . $obj->id . '" title="Editar"></i>
<i class="fa fa-trash iconMini" onClick="clickDeleteOtherUsers(' . $obj->id . ')" data-id="' . $obj->id . '" title="Borrar"></i>';
return $actions;
})
->editColumn('active', function ($obj) {
$active = '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> No <input type="checkbox" onChange="chkOtherUsers(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo"';
if ($obj->active == 1) {
$active .= ' checked=""';
}
$active .= ' /> <span></span>Si </label></div> </label> </div>';
return $active;
})
->editColumn('created_at', function ($obj) {
return \Carbon\Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
})
->editColumn('locations', function ($obj) {
if ($obj->locations) {
$locations = explode(',', $obj->locations);
$data = '';
foreach ($locations as $location) {
$data .= ('<li>' . $location . '</li>');
}
return $data;
} else {
return '<li>TODAS</li>';
}
})
->rawColumns(['actions', 'active', 'locations'])
->make(true);
}
public function activate(Request $request)
{
$id = $request['id'];
$state = $request['state'];
$user = User::find($id);
$user->active = $state;
$user->update();
return array('r' => true, 'm' => trans('messages.controller_other_users_tag2'));
}
public function indexAdd(Request $request)
{
$rols = Rol::select('id', 'name')->whereNotIn('id', ($this->__ROLESTOIGNORE))->where('active', true);
$academyLocationsService = new AcademyLocationsService;
if (!$academyLocationsService->validateAuthorizedLocations()) {
$rols = $rols->where('id', '>=', $request->user()->rol_id);
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
$locations = AcademyLocation::select('id', 'name')->where('active', 1)->whereIn('id', $authorizedLocations)->orderBy('name', 'ASC')->get();
$isSuperAdminFrachise = false;
} else {
$locations = AcademyLocation::select('id', 'name')->where('active', 1)->orderBy('name', 'ASC')->get();
$isSuperAdminFrachise = true;
}
$rols = $rols->orderBy('name', 'ASC')->get();
$tags = Tag::select('id', 'name')->where('active', true)->get();
$academyLocationsService = new AcademyLocationsService;
$showFranchises = $academyLocationsService->showFranchises();
$enableFranchises = AcademyParameter::where('key', 'enable_franchises')->first()->value;
return view('users.addOtherUsers', compact('rols', 'tags', 'locations', 'showFranchises', 'isSuperAdminFrachise', 'enableFranchises'));
}
public function create(Request $request, $checkDeletedMail = false)
{
$parameters = Parameter::select('id', 'db_city', 'db_name')->first();
$data = array(
'rol' => $request["rol_id"],
'accountInfo' => array(
'name' => $request["first_name"],
'lastName' => $request["last_name"],
'cellPhone' => $request["phone"],
'email' => $request["email"],
'password' => $request["password"],
'document' => $request["document"]
),
);
$r = User::where("document", $request["document"])->first();
if ($checkDeletedMail)
$email = User::where("email", "=", $request["email"])->withTrashed()->first();
else
$email = User::where("email", "=", $request["email"])->first();
// Valido si existe un usaurio con el mismo documento
if ($r) {
return array('r' => false, 'm' => trans('messages.controller_deliveryMan_tag2'));
}
// Valido si existe un usuario con el mismo email.
if ($email) {
if ($email->email == $request["email"]) {
if ($email->deleted_at != null) {
return array('r' => true, 'm' => trans('messages.controller_deliveryMan_tag7'), 'id' => $email->id);
} else {
return array('r' => false, 'm' => trans('messages.controller_deliveryMan_tag3'));
}
}
}
$userController = new UserController();
$remoteUser = $userController->signupFromAdminSale($data);
if ($remoteUser->status != "success") {
return array('r' => false);
}
$tags = $request["tags"];
if ($tags != null) {
foreach ($tags as $tagId) {
UserTag::updateOrCreate(
['tag_id' => $tagId, 'user_id' => $remoteUser->user->id],
['tag_id' => $tagId, 'user_id' => $remoteUser->user->id]
);
}
}
$academyLocationsService = new AcademyLocationsService;
$locations = $request["locations"];
if ($locations != null) {
foreach ($locations as $locationId) {
AcademyLocationUser::updateOrCreate(
['academy_location_id' => $locationId, 'user_id' => $remoteUser->user->id],
['academy_location_id' => $locationId, 'user_id' => $remoteUser->user->id]
);
}
} else if (!$academyLocationsService->validateAuthorizedLocations()) {
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
foreach ($authorizedLocations as $locationId) {
AcademyLocationUser::updateOrCreate(
['academy_location_id' => $locationId, 'user_id' => $remoteUser->user->id],
['academy_location_id' => $locationId, 'user_id' => $remoteUser->user->id]
);
}
}
$this->registerLog(Auth::user()->id, 'Crear usuario', json_encode($request->all()), "Create", $this->getModule($request));
return array('r' => true, 'm' => trans('messages.controller_other_users_tag0'), 'd' => $remoteUser->user->id);
}
public function indexEdit(Request $request, $id)
{
$user = User::find($id);
$rols = Rol::select('id', 'name')->whereNotIn('id', ($this->__ROLESTOIGNORE))->where('active', true);
$academyLocationsService = new AcademyLocationsService;
if (!$academyLocationsService->validateAuthorizedLocations()) {
$rols = $rols
->where('id', '>=', $request->user()->rol_id)
->OrWhere('id', '=', $this->__LOGISTICS_ROL);
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
$locations = AcademyLocation::select('id', 'name')->where('active', 1)->whereIn('id', $authorizedLocations)->orderBy('name', 'ASC')->get();
} else {
$locations = AcademyLocation::select('id', 'name')->where('active', 1)->orderBy('name', 'ASC')->get();
}
$rols = $rols->orderBy('name', 'ASC')->get();
$tags = Tag::select('id', 'name')->where('active', true)->get();
$assignedTags = UserTag::where('user_id', $id)->pluck('tag_id')->toArray();
$assignedLocations = AcademyLocationUser::where('user_id', $id)->pluck('academy_location_id')->toArray();
$academyLocationsService = new AcademyLocationsService;
$showFranchises = $academyLocationsService->showFranchises();
$enableFranchises = AcademyParameter::where('key', 'enable_franchises')->first()->value;
return view('users.editOtherUsers', compact('user', 'rols', 'tags', 'assignedTags', 'locations', 'assignedLocations', 'showFranchises', 'enableFranchises'));
}
public function update(Request $request)
{
$user = User::find($request["id"]);
$user->first_name = $request["first_name"];
$user->last_name = $request["last_name"];
$user->phone = $request["phone"];
$user->email = $request["email"];
if ($request["password"]) {
$user->password = Hash::make($request["password"]);
}
$user->document = $request["document"];
$user->rol_id = $request["rol_id"];
$user->update();
UserTag::where('user_id', $user->id)->delete();
$tags = $request["tags"];
if ($tags != null) {
foreach ($tags as $tagId) {
DB::transaction(function () use ($tagId, $user) {
UserTag::updateOrCreate(
['tag_id' => $tagId, 'user_id' => $user->id],
['tag_id' => $tagId, 'user_id' => $user->id]
);
});
}
}
AcademyLocationUser::where('user_id', $user->id)->delete();
$academyLocationsService = new AcademyLocationsService;
$locations = $request["locations"];
if ($locations != null) {
foreach ($locations as $locationId) {
DB::transaction(function () use ($locationId, $user) {
AcademyLocationUser::updateOrCreate(
['academy_location_id' => $locationId, 'user_id' => $user->id],
['academy_location_id' => $locationId, 'user_id' => $user->id]
);
});
}
} else if (!$academyLocationsService->validateAuthorizedLocations()) {
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
foreach ($authorizedLocations as $locationId) {
DB::transaction(function () use ($locationId, $user) {
AcademyLocationUser::updateOrCreate(
['academy_location_id' => $locationId, 'user_id' => $user->id],
['academy_location_id' => $locationId, 'user_id' => $user->id]
);
});
}
}
$this->registerLog(Auth::user()->id, 'Actualizar usuario', json_encode($request->all()), "Update", $this->getModule($request));
return array('r' => true, 'm' => trans('messages.controller_other_users_tag2'));
}
public function delete(Request $request)
{
$userId = $request['id'];
$logObj = User::find($userId);
$controller = new UserController();
$controller->deleteUser($userId);
// Log
$this->registerLog(Auth::user()->id, 'Eliminar usuario', json_encode($logObj), "Delete", $this->getModule($request));
return array('r' => true, 'm' => trans('messages.controller_other_users_tag1'));
}
}