Skip to content

Joomla API

Tuan Pham Ngoc edited this page Aug 20, 2018 · 7 revisions

Path constants

  1. Absolute path to root folder của site: JPATH_ROOT
echo JPATH_ROOT; // Hiển thị D:\www\joomla
  1. Absolute path to administrator folder của site: JPATH_ADMINISTRATOR
echo JPATH_ADMINISTRATOR; // Hiển thị D:\www\joomla\administrator
  1. JPATH_BASE: Nếu constant này được sử dụng ở frontend code, nó sẽ có giá trị = JPATH_ROOT; Nếu constant này được sử dụng ở backend code, nó sẽ có giá trị = JPATH_ADMINISTRATOR

  2. Đường dẫn tuyệt đối đến root folder của component hiện tại: JPATH_COMPONENT. Nếu hằng số này được sử dụng ở fontend, nó sẽ có giá trị dạng D:\www\joomla\components\com_code, nếu ở backend, nó sẽ có dạng D:\www\joomla\administrator\components\com_code. Nhìn chung nên tránh sử dụng hằng số này vì nó không cố định (phụ thuộc vào code của component đang được chạy ở trang hiện tại). Nói chung nên sử dụng JPATH_ROOT . '/components/com_code' hoặc JPATH_ADMINISTRATOR . '/components/com_code'; thay vì JPATH_COMPONENT

Các constant này thường được sử dụng trong cách lềnh require_once, include_once, require, include để require 1 file php hoặc các lênh liên quan đến đường dẫn đến 1 thư mục trong component như JTable::addIncludePath()......

Method to get site's urls

  1. Lấy full url của site
echo JUri::root(); // Hiển thị http://localhost/joomla/
  1. Lấy relative url của site
echo JUri::root(true); // Hiển thị /joomla
  1. Lấy base url của site (phụ thuộc vào code chạy ở frontend hoặc backend)
echo JUri::base(); // Hiển thị http://localhost/joomla/ nếu code được viết ở frontend, http://localhost/joomla/administrator/ nếu code được viết ở backend 
  1. Lấy relative base url của site (phụ thuộc vào code chạy ở frontend hoặc backend)
echo JUri::base(true); // Hiển thị /joomla nếu code được viết ở frontend, /joomla/administrator nếu code được viết ở backend 
  1. Lấy url của trang hiện tại
echo  JUri::getInstance()->toString();

Thông thường các hàm liên quan đến url được sử dụng để lấy về url rồi nối với url link đến resource như thuộc tính src của thẻ img, link tới file css, js. Trong trường hợp này, nên sử dụng relative url thay vì absolute url

Add component's javascript, css file

  1. Add javascript file
$document = JFactory::getDocument();
$rootUri  = JUri::root(true);
$document->addScript($rootUri . '/media/com_code/assets/js/code.js');
  1. Add css file
$document = JFactory::getDocument();
$rootUri  = JUri::root(true);
$document->addScript($rootUri . '/media/com_code/assets/css/style.css');
  1. Add inline javascript code
$script = '
		var a = 1;
		var b= 2;
		var total  = a + b;
		alert(total);
	';
$document = JFactory::getDocument();
$document->addScriptDeclaration(
	$script
);
  1. Add inline css
$css = '
	.btn {background: red};
';
$document = JFactory::getDocument();
$document->addStyleDeclaration($css);

Joomla user object

  1. Get current logged in user object
$user = JFactory::getUser();
  1. Check if user logged in or not
$user = JFactory::getUser();

if ($user->id)
{
	echo 'User is not logged in';
}
else
{
	echo 'User is logged in';
}
  1. Get user information (id, username, name, email)
$user = JFactory::getUser();

echo 'ID:' . $user->id;
echo 'Username:' . $user->username;
echo 'Name:' . $user->name;
echo 'Email:' . $user->email;
  1. Get user groups
$user = JFactory::getUser();

print_r($user->get('groups'));
  1. Get user access levels
$user = JFactory::getUser();

print_r($user->getAuthorisedViewLevels());

// Use in query
$query = $db->getQuery(true)
	->select('*')
	->from('#__eb_events')
	->where('published = 1')
	->where($db->quoteName('access').' IN ('.implode(',', $user->getAuthorisedViewLevels()).')');

Send Emails

// From Name and From Email
$fromName  = 'Ossolution Team';
$fromEmail = 'services@joomdonation.com';

// Subject and email body
$subject  = 'This is a test subject';
$body     = 'This is a <strong>test</strong>. You receive this email from [FIRST_NAME] [LAST_NAME]';

// Sample key-value array use to replace tags in email message
$replaces = [
	'first_name' => 'Tuan',
	'last_name'  => 'Pham Ngoc',
];

// Create mailer object
$mailer = JFactory::getMailer();
$mailer->isHtml(true);

// [Optional] Set From Name and From Email if needed. If not set, from name and from email in global configuration will be used
$mailer->setSender([$fromEmail, $fromName]);

// [Optional] Replace tags in subject and message if needed
foreach ($replaces as $key => $value)
{
	$subject = str_ireplace('[' . $key . ']', $value, $subject);
	$body    = str_ireplace('[' . $key . ']', $value, $body);
}

// [Optional] Add attachment
$mailer->addAttachment(JPATH_ROOT . '/invoice.pdf');

// [Optional] Add BCC emails
$mailer->addBcc(['email1@domain.com', 'email2@domain.com']);

// [Optional] Add reply to
$mailer->addReplyTo('email@domain.com');

// Send email
$mailer->setSubject($subject)
	->setBody($body)
	->addRecipient('tuanpn@joomdonation.com')
	->Send();
Clone this wiki locally