150 lines
4.6 KiB
PHP
150 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
class UserController extends AdminController
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
// helper('utility');
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
|
|
//if this get method return to user creation page
|
|
if($this->request->getMethod() == 'get')
|
|
return view('user');
|
|
//else do user insert operation
|
|
$userModel = new User();
|
|
|
|
$userData = [
|
|
'username' => $this->request->getPost('username'),
|
|
'firstname' => $this->request->getPost('firstname'),
|
|
'lastname' => $this->request->getPost('lastname'),
|
|
'email' => $this->request->getPost('email'),
|
|
'mobile_no' => $this->request->getPost('mobile_no'),
|
|
'password' => $this->request->getPost('password'),
|
|
'is_active' => true,
|
|
'updated_by' => get_session_userid(),
|
|
'created_by' => get_session_userid()
|
|
];
|
|
// print_r($userData);die();
|
|
// Inserting data into the 'users' table
|
|
$userModel->insert($userData);
|
|
|
|
// Get the ID of the inserted record
|
|
$insertedId = $userModel->insertID();
|
|
|
|
redirect('user/list');
|
|
|
|
}
|
|
|
|
public function edit()
|
|
{
|
|
|
|
$userModel = new User();
|
|
|
|
$id = $this->request->getPost('id');
|
|
|
|
$userData = [
|
|
'username' => $this->request->getPost('username'),
|
|
'firstname' => $this->request->getPost('firstname'),
|
|
'lastname' => $this->request->getPost('lastname'),
|
|
'email' => $this->request->getPost('email'),
|
|
'mobile_no' => $this->request->getPost('mobile_no'),
|
|
'password' => $this->request->getPost('password'),
|
|
'is_active' => $this->request->getPost('is_active'),
|
|
//'updated_by' => $this->request->getPost('updated_by'),
|
|
//'created_by' => $this->request->getPost('created_by')
|
|
];
|
|
|
|
// Update data in the 'users' table based on the $id
|
|
$is_updated = $userModel->update($id, $userData);
|
|
|
|
// Get the ID of the updated record
|
|
//$updatedId = $userModel->getPrimaryVal($id);
|
|
echo 'done';
|
|
redirect('user/list');
|
|
}
|
|
|
|
public function list()
|
|
{
|
|
$uri = service('uri');
|
|
$userid = $uri->getSegment(3);
|
|
|
|
$userModel = new User();
|
|
|
|
// Fetch all users from the 'users' table
|
|
$users = $userModel
|
|
->join('users3 as updater', 'updater.id = users3.updated_by', 'left')
|
|
->join('users3 as creator', 'creator.id = users3.created_by', 'left')
|
|
->select('users3.*, updater.username as updated_by_name,creator.username as created_by_name'); // Assuming
|
|
if(!empty($userid) && is_numeric($userid))
|
|
{
|
|
$result = $users->find($userid);
|
|
return view('user',['user' => $result]);
|
|
}
|
|
else
|
|
{
|
|
$result = $users->findAll();
|
|
// print_r($result);die();
|
|
return view('userlist',['users' => $result]);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public function search()
|
|
{
|
|
$userModel = new User();
|
|
|
|
// Get search inputs from the query string
|
|
$firstname = $this->request->getGet('firstname');
|
|
$lastname = $this->request->getGet('lastname');
|
|
$username = $this->request->getGet('username');
|
|
$mobile_no = $this->request->getGet('mobile_no');
|
|
$email = $this->request->getGet('email');
|
|
$created_at = $this->request->getGet('created_at');
|
|
|
|
|
|
// Set conditions for the search query
|
|
$conditions = [];
|
|
if (!empty($firstname)) {
|
|
$conditions['firstname LIKE'] = "%$firstname%";
|
|
}
|
|
if (!empty($lastname)) {
|
|
$conditions['lastname LIKE'] = "%$lastname%";
|
|
}
|
|
if (!empty($username)) {
|
|
$conditions['username LIKE'] = "%$username%";
|
|
}
|
|
if (!empty($mobile_no)) {
|
|
$conditions['mobile_no LIKE'] = "%$mobile_no%";
|
|
}
|
|
if (!empty($email)) {
|
|
$conditions['email LIKE'] = "%$email%";
|
|
}
|
|
if (!empty($created_at)) {
|
|
// Assuming created_before is in the format YYYY-MM-DD
|
|
$conditions['created_at'] = $created_at;
|
|
}
|
|
|
|
// Fetch users based on search conditions
|
|
$users = $userModel->where($conditions)->findAll();
|
|
|
|
return view('userlist',['users' => $users]);
|
|
|
|
}
|
|
}
|