How to get current loggedin username in vtiger tpl file - smarty

How i can get the current loggedin username in the header.tpl file of vtiger CRM. It should be for use inside {php} {/php} tags.
Header.tpl
{php} if($CURRENT_USER == 'admin') { echo "yes"; } else { echo "no"; } {/php}

$current_user_model isn't a variable that the .tpl has in it's scope by default.
What you should do is go to the .php file for the view that you're using and add something like this:
$username = Users_Record_Model::getCurrentUserModel() ->get('user_name');
$viewer->assign('USERNAME', $username);
This passes the php variable $username to your tpl with the name $USERNAME, so now anywhere you need it in the .tpl simply use {$USERNAME}

You have to first assign a variable in your PHP file. Like if you want in Edit view you should define it in Edit.php file of that module or Vtiger Module. Like this using Viewer.
$viewer->assign('CURRENT_USER_MODEL', Users_Record_Model::getCurrentUserModel());
Now you can access this variable and current username using this code from TPL file
{$CURRENT_USER_MODEL->get('user_name')}

Not sure if that would help, but if you need to check what user is logged in, you can get their ID via
{$smarty.session.authenticated_user_id}
If you need the name, though, you would need to use elax's solution.

Related

How to check, which route is session()->get('url.intended')?

Need to write some logic based on url.intended for example if the url is route /create show different login form and different if it's /move.
if(Route::current()->getName() == '/create'){
// do something
}
You can also pass it from your controller to your blade view as a parameter, not the cleanest way, but it works.
Cheers!
You can get the current route name and based on that do stuff;
use \Illuminate\Support\Facades\Route;
$routeName = Route::currentRouteName();
if($routeName == 'user.create') {
// whatever is needed.
}

Laravel pass data between views

I have a view with a few variables that I want to pass to a new page and prepopulate a form with them.
$data->title
$data->catid
$data->category
$data->groupName
The "vanila way I could make a url like something.com?var1=something&var2=something etc.
But how can I do this in laravel?
Considering you can redirect to a page with GET parameter. For example, "something.com?var1=something&var2=something", You can actually pass this data to another view in Laravel. Let's assume you want to pass var1 and var2 variables to another view.
You will redirect user to the URL containing two variables as GET parameters, You will also execute Controller function that will handle that route. Let's assume that following is your web.php or routes.php file.
Route::get('firstpage","SomeController#view1");
Route::get("secondpage","SomeController#view2");
Now, in view2 method of the SomeController, You can check if those variables exist or not. Here is an example of var1 and var2.
public function view2()
{
if(isset($_GET['var1'])){
$var1 = $_GET['var1'];
}
// Similar for var2
}
This is how you can pass variables between views.
The best way IMHO is using sessions.
session()->put('data', $data);
And to retrieve it and to check if the key exists:
if (session()->has('data')) {
$data = session('data'); //array
}
If you use the $data in session() only once you can pull it session()->pull('data'); //array
A word of caution if you use database sessions. You may have to change the field 'payload' from TEXT to MEDIUMTEXT if $data holds larger amounts of data.
The use of sessions is not search engine (SEO) friendly. If that matters, the easiest way to make the url is $str = http_build_query($data); and to build the url \URL()->route('your.route').'?'.$str;
To access the data from the URL: $title = \Request::query('title');

Checking folder already existed and if not create a new folder by id in laravel

i want to store my image to a specific folder and the folder will named by page id. For example if Page id=1, the folder location should be public/pages/page_id/image_here.
If the folder are not existed yet the system will generate and named with their page id.
$id=1;
$directoryPath=public_path('pages/' . $id);
if(File::isDirectory($directoryPath)){
//Perform storing
} else {
Storage::makeDirectory($directoryPath);
//Perform storing
}
But i having error that "mkdir(): Invalid argument".
Can i know why it happen?
And after my research some people say the folder name should based with id+token so intruder cannot search image based on id, is there possible to achieve?
I had the same problem, but when I use File instead of Storage it works!
$id=1;
$directoryPath=public_path('pages/'.$id);
//check if the directory exists
if(!File::isDirectory($directoryPath)){
//make the directory because it doesn't exists
File::makeDirectory($directoryPath);
}
//Perform store of the file
Hope this works!
When you use Storage facade, it uses local disk as default which is configured to work with storage_path().
So, if you want to create directory in public directory, use File::makeDirectory which is just simple wrapper for mkdir() with additional options or use mkdir() directly:
File::makeDirectory($directoryPath);
mkdir($directoryPath);
For basic Laravel file system the syntax is :
At very top under namespace write:
use File;
Then in your method use:
$id=1;
$directoryPath=public_path('pages/' . $id);
if(File::isDirectory($directoryPath)){
//Perform storing
} else {
File::makeDirectory($directoryPath, 0777, true, true);
//Perform storing
}

codeigniter output cache doesn't work?

I'm using $this->output->cache(n) to cache webpage, but i cannot figure out how does it work.. I didn't find any cache files under system/cache folder...and also after I edit the page and show it again, the content changes, so it seems that the page is not really cached. Can anyone give a help? (i'm using phil's template lib)
my code:
function show(){
$this->output->cache(5);
$this->load->model('page_model');
$var = $this->uri->segment(3, 0); //get About page
$row = $this->page_model->getPage($var);
$this->template->title('about')
->set_layout('default')
->set_partial('styles', 'css')
->set('data', $row->body)
->build('about');
}
THANKS!
Two things, as outlined in the documentation:
Warning: Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.
Perhaps not using the "native" views is an issue?
Additionally:
Note: Before the cache files can be written you must set the file permissions on your application/cache folder such that it is writable.
Are you sure your application/cache directory has the correct permissions?
Debug this file and check it's actually writing the cache:
system/core/Output.php
// Do we need to write a cache file? Only if the controller does not have its
// own _output() method and we are not dealing with a cache file, which we
// can determine by the existence of the $CI object above
if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
{
$this->_write_cache($output);
}
This works well for me:
function _output($content) {
// Load the base template with output content available as $content
$data['content'] = &$content;
$this->output->cache(600);
$output = $this->load->view('base', $data, true);
$this->output->_write_cache($output);
echo $output;
}
Are you sure your application/cache directory has the correct permissions?
you you to directories application/cache in cpanel , permissions become 777 is OK

accessing lang variables from controller + codeigniter

How can I access my lang variables in controller?
function index()
{
$seo_title = $this->lang->line('blablabla');
$data['page_title'] = $seo_title;
$this->load->view('contact/index.php',$data);
}
in my lang file blablabla has string in, and it works well when I call from view file. but when I want to call from controller, it doesnt return anything :(
any idea about problem? appreciate!!
You need to load the language file you want to use before you grab lines from it:
$this->lang->load('filename', 'language');

Resources