Page 1 of 1

Get the firstname of the user logged in (in admin panel)

Posted: Sat Oct 13, 2018 1:00 am
by broopenc3
Hello,

as the title suggests, I am trying to get the firstname (or maybe the lastname and username also from the user name), so that I can add it to a custom query in order_history.

What I have made so far (maybe a little irrelevant from what I am asking):
in the file /catalog/model/checkout/order.php

Code: Select all

//line 345 aproximatly 
$name_for_added_by="admin";

$this->db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '" . (int)$notify . "', added_by = '".$name_for_added_by."', comment = '" . $this->db->escape($comment) . "', date_added = NOW()");
with this in mind I can see in my SQL that the field "added_by" in the table " . DB_PREFIX . "order_history is correctly populated with the word "admin";

Now what i am trying to do is set "$name_for_added_by" with the user that presses the button-history (to update the history);
What I found (from /admin/controller/common/header.php) is that I need "$user_info['firstname']". However I can not get something to work.

Any ideas? :) Thanx

Re: Get the firstname of the user logged in (in admin panel)

Posted: Sat Oct 13, 2018 2:00 am
by DigitCart
Hi,
In your controller file, use it:

Code: Select all

$user_name =  $this->user->getUserName();

//echo $user_name;

$this->load->model('user/user');
$user_info = $this->model_user_user->getUser($this->user->getId());

//print_r($user_info);

Re: Get the firstname of the user logged in (in admin panel)

Posted: Sat Oct 13, 2018 4:22 am
by straightlight
Better to use the user ID than the username on the database for tracking purposes.

Re: Get the firstname of the user logged in (in admin panel)

Posted: Sat Oct 13, 2018 3:52 pm
by broopenc3
DigitCart wrote:
Sat Oct 13, 2018 2:00 am
Hi,
In your controller file, use it:
Thanx for the answer! I managed to make it work in the header and any variabe I want from the user is accesible but I can not pass any variable in the 'public_html/catalog/model/checkout/order.php' file. So, final question: where is the controller file that I need to edit?

(ps: order history edit in opencart3 is different from opencart 1.5)

Re: Get the firstname of the user logged in (in admin panel)

Posted: Sat Oct 13, 2018 3:54 pm
by broopenc3
straightlight wrote:
Sat Oct 13, 2018 4:22 am
Better to use the user ID than the username on the database for tracking purposes.
You are right and thank you for the suggestion. However for the purposes I need and the current situation, the username is enough.

Re: Get the firstname of the user logged in (in admin panel)

Posted: Sat Oct 13, 2018 7:04 pm
by broopenc3
Update: I edited file "admin/controller/common/header.php"

Code: Select all

	public function index() {

		$user_name =  $this->user->getUserName();
		$this->load->model('user/user');
		$user_info = $this->model_user_user->getUser($this->user->getId());

		$GLOBALS["name_for_added_by"] = $user_name ; //either this
		$GLOBALS["name_for_added_by"] = $user_info['username']; //or this
but the variable still did not pass the value to the query in "catalog/model/checkout/order.php"

Code: Select all

$this->db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '" . (int)$notify . "', added_by = '".$GLOBALS["name_for_added_by"]."', comment = '" . $this->db->escape($comment) . "', date_added = NOW()");

Re: Get the firstname of the user logged in (in admin panel)

Posted: Sat Oct 13, 2018 9:42 pm
by straightlight
$GLOBALS is disabled by default in OC from the system folder for security reasons. A parameter needs to be passed with the username which is why I indicated above that the user ID would be more relative since a similar method already adds a user into the database as you demonstrate already. You just need to duplicate that method for your own database table target name.

Re: Get the firstname of the user logged in (in admin panel)

Posted: Mon Oct 15, 2018 8:59 pm
by broopenc3
straightlight wrote:
Sat Oct 13, 2018 9:42 pm
$GLOBALS is disabled by default in OC from the system folder for security reasons. A parameter needs to be passed with the username which is why I indicated above that the user ID would be more relative since a similar method already adds a user into the database as you demonstrate already. You just need to duplicate that method for your own database table target name.
allright got it... however how do i get the current user id?
neither: $this->session->data['user_id']
nor: $this->user->getId()
are working. :/

Re: Get the firstname of the user logged in (in admin panel)

Posted: Mon Oct 15, 2018 9:22 pm
by DigitCart
Hi,
admin and catalog are separate. if you want to get the user ID of admin in catalog (front-end), in your controller file use:

Code: Select all

$this->user = new Cart\User($this->registry);
$user_id = $this->user->getId(); // Returns null  or user ID

Re: Get the firstname of the user logged in (in admin panel)

Posted: Mon Oct 15, 2018 9:39 pm
by broopenc3
DigitCart wrote:
Mon Oct 15, 2018 9:22 pm
Hi,
admin and catalog are separate. if you want to get the user ID of admin in catalog (front-end), in your controller file use:

Code: Select all

$this->user = new Cart\User($this->registry);
$user_id = $this->user->getId(); // Returns null  or user ID
I tried in the controller/common/header.php with no results, unless you mean in system/engine/controller.php which throws an error.

I made a duplicate of model/user/user.php which loads in the order.php. Could I get the user_id there??? Like:

Code: Select all

<?php class ModelUserUser extends Model {
	public function getUserName() {
		$query = $this->db->query("SELECT *, (SELECT ug.name FROM `" . DB_PREFIX . "user_group` ug WHERE ug.user_group_id = u.user_group_id) AS user_group FROM `" . DB_PREFIX . "user` u WHERE u.user_id = '" . $user_id_HERE . "'");
		return $query->row;
	}
}

Re: Get the firstname of the user logged in (in admin panel)

Posted: Mon Oct 15, 2018 9:57 pm
by DigitCart
I tried in the controller/common/header.php
I just tested in header.php and it worked. make sure you've cleared caches.

Re: Get the firstname of the user logged in (in admin panel)

Posted: Tue Oct 16, 2018 2:36 pm
by broopenc3
DigitCart wrote:
Mon Oct 15, 2018 9:57 pm
I tried in the controller/common/header.php
I just tested in header.php and it worked. make sure you've cleared caches.
:/ I generaly have deactivated caching for admin menu. I cleared it once more but did not work. However I will look into it further just in case there is a problem.