title = "Users" url = "/setup/users" layout = "default" is_hidden = 0 == addJs('assets/js/setup/users1.js'); $this->addJs('assets/js/dataTablesSetup.js'); $accessLevels = Db::select('select * from custom_access_level'); $this['accessLevels'] = $accessLevels; } function onGetAllBrands(){ return Db::select('select * from custom_brands'); } function onGetAllRestaurants(){ return Db::select('select * from custom_restaurants WHERE custom_restaurants.isDeactivated="0" ORDER BY code '); } function onGetAlluserRestaurantAccess(){ return Db::select('select restaurantID from custom_userRestaurantAccess where userID="'.$_POST['id'].'" '); } function onAddUser() { //get the values sent from ajax $name = $_POST['name']; $email = $_POST['email']; $accessLevel = $_POST['accessLevel']; $password = $_POST['password']; $doNotSendEmail = $_POST['doNotSendEmailAdd']; $hourRate = $_POST['hourRate']; if(!empty($_POST['brandAccess'])){ $brandAccessNotSelected = true; $brandAccess = $_POST['brandAccess']; }else{ $brandAccessNotSelected = false; } if(!empty($_POST['restaurantAccess'])){ $restaurantAccessNotSelected = true; $restaurantAccess = $_POST['restaurantAccess']; }else{ $restaurantAccessNotSelected = false; } //encrypt the password $password = Hash::make($password); //wrap the mysql call into a try and catch so that we can catch any errors //here we insert the new user and then retrieve its id so that we can setup the restaurant access for the user try { //$id = Db::connection('hssData')->table('users')->insertGetId("INSERT INTO users(name, email, accessLevelCode, password) VALUES(?, ?, ?, ?)",[$name,$email,$accessLevel, $password]); $id = Db::table('custom_users')->insertGetId(['name' => $name, 'email' => $email, 'accessLevelCode' => $accessLevel, 'hourlyRate' => $hourRate, 'password' => $password, 'doNotSendEmail' => $doNotSendEmail]); //return 'ok'; } catch (\PDOException $e) { return 'There was an error with adding the User to the database. Please make sure that the email entered is not already in use by another user.'; } //first check if a brand has been selected for a user //if not then we will not try to add the brands in the DB if($brandAccessNotSelected){ //setup the restaurant access foreach ($brandAccess as $brand) { //for each brand load all its stores $brandRestaurants = Db::select('select id from custom_restaurants where brandCode="'.$brand.'" '); foreach ($brandRestaurants as $brandRestaurant){ try { Db::table('custom_userRestaurantAccess')->insert(['restaurantID' => $brandRestaurant->id, 'userID' => $id]); } catch (\PDOException $e) { return 'There was an error will adding the Brand Access'; } } } } //check if the user has selected a restaurant //if not then we will not try to add any restaurants in the DB if($restaurantAccessNotSelected){ //loop through all restaurant id's foreach ($restaurantAccess as $res) { //check if the restaurant was already added by the brand //if yes then we do not add it again $restaurantExists = Db::table('custom_userRestaurantAccess') ->where('restaurantID', '=', $res) ->where('userID', '=', $id) ->first(); //if true then it means this is null if(is_null($restaurantExists)){ try { Db::table('custom_userRestaurantAccess')->insert(['restaurantID' => $res, 'userID' => $id]); } catch (\PDOException $e) { return 'There was an error will adding the Restaurant Access'; } } } } return 'ok'; } function onEditRestaurant() { //get the values sent from ajax $id = $_POST['id']; $name = $_POST['name']; $email = $_POST['email']; $accessLevel = $_POST['accessLevel']; $password = $_POST['password']; $hourlyRate = $_POST['hourlyRate']; if (isset($_POST["restaurantAccess"])){ $restaurantAccess = $_POST['restaurantAccess']; }else{ $restaurantAccess = []; } $archiveUser = $_POST['userArchiveEdit']; $doNotSendEmail = $_POST['doNotSendEmailEdit']; $updatePassword =false; if(!empty($password)){ $updatePassword =true; //encrypt the password $password = Hash::make($password); } //wrap the mysql call into a try and catch so that we can catch any errors try { if($updatePassword){ Db::insert("UPDATE custom_users SET name=:name, email=:email, accessLevelCode=:accessLevel, archiveUser=:archiveUser, hourlyRate=:hourlyRate, password=:password WHERE id=:id",[$name,$email,$accessLevel,$archiveUser,$hourlyRate,$password,$id]); }else{ //Db::connection('hssData')->insert('UPDATE users SET name=:name, email=:email, accessLevelCode=:accessLevel WHERE id="'.$id.'" '); Db::insert("UPDATE custom_users SET name=:name, email=:email, accessLevelCode=:accessLevel, archiveUser=:archiveUser, hourlyRate=:hourlyRate, doNotSendEmail=:doNotSendEmail WHERE id=:id",[$name,$email,$accessLevel,$archiveUser, $hourlyRate, $doNotSendEmail,$id]); } if($accessLevel !== 'dev' && $accessLevel !== 'ops' && $accessLevel !== 'store' && $accessLevel !== 'admin'){ Db::select('DELETE FROM custom_userRestaurantAccess WHERE userID="'.$id.'"'); } } catch (\PDOException $e) { Log::info('EEEEEERRRRROOOOOOORRRR '); return 'There was an error while updating the user\'s details'; } Db::table('custom_userRestaurantAccess')->where('userID', $id)->delete(); if($accessLevel == "store" || $accessLevel == "ops" || $accessLevel == "dev" || $accessLevel == 'admin'){ foreach ($restaurantAccess as $res) { try { Db::table('custom_userRestaurantAccess')->insert(['restaurantID' => $res, 'userID' => $id]); } catch (\PDOException $e) { return 'There was an error will adding the Restaurant Access'; } } } return 'ok'; } function onDeleteUser(){ $id = $_POST['userID']; $res = Db::select('select COUNT(*) AS count from custom_tickets WHERE responsible_user_id="'.$id.'"'); $hours = Db::select('select COUNT(*) AS count from custom_technician_ticket_hours WHERE user_id="'.$id.'"'); if( $res[0]->count == 0 && $hours[0]->count == 0 ){ //first delete all restaurants linked to the account and then delete the account // Db::select('DELETE FROM custom_userRestaurantAccess WHERE userID="'.$id.'"'); Db::select('DELETE FROM custom_users WHERE id="'.$id.'"'); Db::select('DELETE FROM custom_userRestaurantAccess WHERE userID="'.$id.'"'); return ['#loadUsers' => $this->renderPartial('loadUsers.htm', [ ])]; }else{ return 'error'; } } ?> ==

Users

{% partial 'loadUsers' %}