Turning the Google Calendar OAuth 2.0 PHP integration into an installable Joomla 5 component is totally doable. This is a walk-through of how to create a Joomla 5 component that integrates with Google Calendar API, based on the PHP code we discussed earlier.
✅ Joomla 5 Component Structure for Google Calendar Integration
In Joomla, components are structured extensions with MVC architecture, and the installable package includes:
- Component folder (site/admin files)
manifest.xml
file- PHP scripts (controllers, views, models)
- Helper classes (like for Google API)
- Templates for output
✅ Step 1: Basic Folder Structure
Here's a simplified directory structure for your Joomla 5 component:
/com_gcalendar
├── admin
│ ├── src
│ │ ├── Controller
│ │ ├── Model
│ │ ├── View
│ │ ├── Service
│ │ └── Helper
│ └── tmpl
├── site
│ ├── src
│ │ ├── Controller
│ │ ├── Model
│ │ ├── View
│ │ ├── Service
│ │ └── Helper
│ └── tmpl
├── language
│ └── en-GB
│ └── en-GB.com_gcalendar.ini
└── com_gcalendar.xml (manifest file)
➡️ The admin
folder handles backend management.
➡️ The site
folder is where you display calendar events or forms on the public website.
✅ Step 2: Install Google API Library in Joomla
You can:
- Use Composer inside your component folder:
cd com_gcalendar composer require google/apiclient:^2.0
- Or manually include the required Google API libraries inside
libraries
or yourHelper
folder.
For Joomla best practice, it’s recommended to install the library via Composer in your Joomla project root or your component and autoload it in your component.
✅ Step 3: Create the manifest.xml
This file defines your Joomla component and the files it uses.
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="5.0" method="upgrade">
<name>com_gcalendar</name>
<author>Your Name</author>
<creationDate>2025-03-17</creationDate>
<version>1.0.0</version>
<description>Google Calendar Integration for Joomla 5</description>
<files folder="site">
<folder>src</folder>
<folder>tmpl</folder>
</files>
<administration>
<files folder="admin">
<folder>src</folder>
<folder>tmpl</folder>
</files>
</administration>
</extension>
✅ Step 4: Joomla MVC Basics (How to Handle OAuth and Events)
➤ Controller Example (site/src/Controller/CalendarController.php
)
<?php
namespace Joomla\Component\Gcalendar\Site\Controller;
\defined('_JEXEC') or die;
use Joomla\CMS\MVC\Controller\BaseController;
class CalendarController extends BaseController
{
public function authorize()
{
$app = \Joomla\CMS\Factory::getApplication();
$client = $this->getGoogleClient();
$authUrl = $client->createAuthUrl();
$app->redirect($authUrl);
}
public function callback()
{
$app = \Joomla\CMS\Factory::getApplication();
$input = $app->input;
$code = $input->get('code');
if (!$code) {
$app->enqueueMessage('Authorization code missing', 'error');
$this->setRedirect('index.php');
return;
}
$client = $this->getGoogleClient();
$token = $client->fetchAccessTokenWithAuthCode($code);
if (isset($token['error'])) {
$app->enqueueMessage('Authorization failed: ' . $token['error'], 'error');
$this->setRedirect('index.php');
return;
}
// Save tokens to Joomla user profile or your component table
$user = \Joomla\CMS\Factory::getUser();
$db = \Joomla\CMS\Factory::getDbo();
// Example: Insert tokens into your custom table (or user profile)
// Your table logic goes here...
$this->setRedirect('index.php?option=com_gcalendar&view=calendar');
}
private function getGoogleClient()
{
require_once JPATH_COMPONENT_SITE . '/vendor/autoload.php';
$client = new \Google_Client();
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri(' https://www.japansailing.com/index.php?option=com_gcalendar&task=calendar.callback');
$client->addScope(\Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');
$client->setPrompt('consent');
return $client;
}
}
✅ Step 5: Joomla View to Trigger OAuth
➤ Site View (site/src/View/Calendar/HtmlView.php
)
<?php
namespace Joomla\Component\Gcalendar\Site\View\Calendar;
\defined('_JEXEC') or die;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
class HtmlView extends BaseHtmlView
{
protected $items;
public function display($tpl = null)
{
$this->items = []; // You can retrieve calendar events here later
parent::display($tpl);
}
}
➤ Template (site/tmpl/calendar/default.php
)
<?php
\defined('_JEXEC') or die;
use Joomla\CMS\Router\Route;
$authLink = Route::_('index.php?option=com_gcalendar&task=calendar.authorize');
echo "<h1>Google Calendar Integration</h1>";
echo "<a href='$authLink' class='btn btn-primary'>Connect to Google Calendar</a>";
✅ Step 6: Create Admin Interface (Optional)
- Add an admin model/view/controller structure if you want backend management.
- Example: Manage calendar ID selection, event templates, default settings.
✅ Step 7: Package Your Joomla Component
- Zip your component folder (
com_gcalendar
). - Install in Joomla via Extensions ➜ Manage ➜ Install.
✅ Step 8: Database Table for Tokens (Optional But Recommended)
Create a table for storing:
user_id
access_token
refresh_token
expires_in
Example SQL:
CREATE TABLE `#__gcalendar_tokens` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`access_token` TEXT NOT NULL,
`refresh_token` TEXT,
`expires_at` TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `#__users`(`id`)
);
✅ Step 9: Security Best Practices
- Use Joomla ACL to control who can connect to Google Calendar.
- Encrypt refresh tokens in the database.
- Validate input and output strictly.
- Use Joomla’s session system to handle logged-in users.
✅ Final Deliverable: What You’ll Have
- A Joomla 5 component (
com_gcalendar
) that:- Authenticates users via Google OAuth 2.0
- Lets them create/edit/delete Google Calendar events from Joomla
- Can be packaged and installed on
www.japansailing.com
or any subdomain likekaikoma.japansailing.com
✅ Next Steps (Optional Enhancements)
Option | Description |
---|---|
FullCalendar.js | Create a dynamic calendar display on the frontend |
Event Notifications | Send Joomla emails when events are created/updated |
Multi-Calendar Support | Let users select which calendar they’re working with |
Admin Settings | Allow backend settings to configure global Google API credentials |
✅ Ready for Code Samples?
If you want, I can:
- Provide a zip file structure you can start from.
- Walk you through setting up the token database table and saving tokens properly.
- Show you how to integrate FullCalendar.js into the Joomla 5 component.
What’s your preference?