/var/www/vhosts/ihelp.ro/httpdocs/src/Controller/Admin
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Controller/Admin/UsersController.php (8244B)
paginate = [
'limit' => ADMIN_PAGE_LIMIT,
'contain' => ['Roles', 'Profiles'],
'sortableFields' => ['Users.id', 'Profiles.first_name', 'Profiles.phone', 'Users.email', 'Users.is_active', 'Users.created']
];
$users = $this->paginate($this->Users);
$this->set(compact('users'));
}
/**
* Add method
*/
public function add()
{
$user = $this->Users->newEmptyEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData(), ['associated' => ['Profiles']]);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$roles = $this->Users->Roles->find('list', ['limit' => 200]);
$this->set(compact('user', 'roles'));
}
/**
* Edit method
*/
public function edit($id = null)
{
if (!empty($id) && is_numeric($id)) {
$user = $this->Users->get($id, [
'contain' => ['Profiles', 'UserAddresses'],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
} else {
$this->Flash->error(__('Oops. Please, try again.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
$roles = $this->Users->Roles->find('list', ['limit' => 200]);
$this->set(compact('user', 'roles'));
}
/**
* View method
*/
public function view($id = null)
{
if (!empty($id) && is_numeric($id)) {
$this->paginate = [
'limit' => ADMIN_PAGE_LIMIT
];
$this->loadModel('Orders');
$orders = $this->paginate($this->Orders->find()
->contain(['UserAddresses', 'OrderStatuses', 'PaymentMethods'])
->where(['Orders.user_id' => $id]));
} else {
$this->Flash->error(__('Oops. Please, try again.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
$userId = $id;
$this->set(compact('orders', 'userId'));
}
/**
* View method
*/
public function viewProducts($id = null)
{
if (!empty($id) && is_numeric($id)) {
$this->paginate = [
'limit' => ADMIN_PAGE_LIMIT
];
$this->loadModel('Products');
$products = $this->paginate($this->Products->find()
->contain(['Causes'])
->where(['Products.user_id' => $id]));
} else {
$this->Flash->error(__('Oops. Please, try again.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
$userId = $id;
$this->set(compact('products', 'userId'));
}
/**
* View method
*/
public function viewCauses($id = null)
{
if (!empty($id) && is_numeric($id)) {
$this->paginate = [
'limit' => ADMIN_PAGE_LIMIT
];
$this->loadModel('Causes');
$causes = $this->paginate($this->Causes->find()
->where(['Causes.user_id' => $id]));
} else {
$this->Flash->error(__('Oops. Please, try again.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
$userId = $id;
$this->set(compact('causes', 'userId'));
}
/**
* View method
*/
public function viewAddresses($id = null)
{
if (!empty($id) && is_numeric($id)) {
$this->paginate = [
'limit' => ADMIN_PAGE_LIMIT
];
$this->loadModel('UserAddresses');
$userAddresses = $this->paginate($this->UserAddresses->find()
->contain(['Cities'])
->where(['UserAddresses.user_id' => $id]));
} else {
$this->Flash->error(__('Oops. Please, try again.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
$userId = $id;
$this->set(compact('userAddresses', 'userId'));
}
/**
* View method
*/
public function viewProductFavorites($id = null)
{
if (!empty($id) && is_numeric($id)) {
$this->paginate = [
'limit' => ADMIN_PAGE_LIMIT
];
$this->loadModel('ProductFavorites');
$productFavorites = $this->paginate($this->ProductFavorites->find()
->contain(['Products'])
->where(['ProductFavorites.user_id' => $id]));
} else {
$this->Flash->error(__('Oops. Please, try again.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
$userId = $id;
$this->set(compact('productFavorites', 'userId'));
}
/**
* Disable/Activate method
*/
public function delete($id = null)
{
if (!empty($id) && is_numeric($id)) {
$this->request->allowMethod(['post', 'get']);
$user = $this->Users->get($id);
$user->is_active = ($user->is_active === true) ? false : true;
if ($this->Users->save($user)) {
$this->Flash->success(__('Userul a fost modificat.'));
} else {
$this->Flash->error(__('Utilizatorul nu a putut fi modificat. Va rugam, incercati mai tarziu.'));
}
} else {
$this->Flash->error(__('Oops. Please, try again.'));
}
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
}
/**
* Login method
*/
public function login()
{
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
// regardless of POST or GET, redirect if user is logged in
if ($result->isValid()) {
$roleId = $this->Authentication->getIdentityData('role_id');
if ($roleId == ADMIN) {
// redirect to /articles after login success
$redirect = $this->request->getQuery('redirect', [
'prefix' => 'Admin',
'controller' => 'Dashboard',
'action' => 'index',
]);
return $this->redirect($redirect);
} else {
$this->logout();
}
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid email or password'));
}
}
/**
* Logout method
*/
public function logout()
{
$result = $this->Authentication->getResult();
// regardless of POST or GET, redirect if user is logged in
if ($result->isValid()) {
$this->Authentication->logout();
$this->Flash->success(__('LogOut'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'login']);
}
}
}