Skip to content

Commit

Permalink
Support 3rd parties to create widgets for Astroid through plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
sonvnn committed Jul 16, 2024
1 parent f807a10 commit 7cf85a4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 31 deletions.
2 changes: 1 addition & 1 deletion assets/vendor/manager/dist/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/vendor/manager/dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/vendor/manager/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/vendor/manager/src/components/helpers/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ onBeforeMount(()=>{
activeDevice.value = layout.value.devices[0].code;
if (props.source === 'article_layouts') {
let url = constant.site_url+"administrator/index.php?option=com_ajax&astroid=getArticleFormTemplate&template="+constant.tpl_template_name+"&ts="+Date.now();
let url = constant.site_url+"administrator/index.php?option=com_ajax&astroid=getArticleFormTemplate&id="+constant.template_id+"&ts="+Date.now();
if (process.env.NODE_ENV === 'development') {
url = "articleformtemplate_ajax.txt?ts="+Date.now();
}
Expand Down
3 changes: 1 addition & 2 deletions framework/library/astroid/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ protected function getArticleFormTemplate()
{
try {
$app = Factory::getApplication();
define('ASTROID_TEMPLATE_NAME', $app->input->get('template', NULL, 'RAW'));
$this->response(Helper::getFormTemplate('article'));
$this->response(Helper::getFormTemplate('article', $app->input->get('id', NULL, 'RAW')));
} catch (\Exception $e) {
$this->errorResponse($e);
}
Expand Down
52 changes: 36 additions & 16 deletions framework/library/astroid/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\Registry\Registry;
use Joomla\CMS\Version;
use Joomla\CMS\Router\Route;
Expand Down Expand Up @@ -331,26 +332,48 @@ public static function getModules()
return $return;
}

public static function getAllAstroidElements($mode = '')
public static function getAllAstroidElements($mode = '', $template_id = null)
{
$template = Framework::getTemplate();
$template = Framework::getTemplate($template_id);
// Template Directories
$elements_dir = JPATH_LIBRARIES . '/astroid/framework/elements/';
$plugin_elements_dir = JPATH_SITE . "/plugins/astroid";
$template_elements_dir = JPATH_SITE . '/media/templates/site/' . $template->template . '/astroid/elements/';

// Getting Elements from Template Directories
$elements = array_filter(glob($elements_dir . '*'), 'is_dir');
$template_elements = array_filter(glob($template_elements_dir . '*'), 'is_dir');
$elements = Folder::folders($elements_dir, '.', false, true);

if (file_exists(Path::clean($plugin_elements_dir))) {
$plugin_folders = Folder::folders($plugin_elements_dir);
if (count($plugin_folders)) {
foreach ($plugin_folders as $plugin_folder) {
if (file_exists(Path::clean($plugin_elements_dir . '/' . $plugin_folder . '/elements/'))) {
// Merging Plugin Elements
$elements = array_merge($elements, Folder::folders($plugin_elements_dir . '/' . $plugin_folder . '/elements/', '.', false, true));
}
}
}
}

$template_elements = Folder::folders($template_elements_dir, '.', false, true);

// Merging Elements
$elements = array_merge($elements, $template_elements);

$return = array();

foreach ($elements as $element_dir) {
$xmlfile = $element_dir . '/' . (str_replace($template_elements_dir, '', str_replace($elements_dir, '', $element_dir))) . '.xml';
// String manipulation should be faster than pathinfo() on newer PHP versions.
$slash = strrpos($element_dir, DIRECTORY_SEPARATOR);

if ($slash === false) {
continue;
}

$type = substr($element_dir, $slash + 1);
$xmlfile = $element_dir . '/' . $type . '.xml';

if (file_exists($xmlfile)) {
$type = str_replace($template_elements_dir, '', str_replace($elements_dir, '', $element_dir));
$element = new Element($type, [], $template, $mode);
$return[] = $element;
}
Expand Down Expand Up @@ -602,21 +625,18 @@ public static function str_lreplace($search, $replace, $subject)
return $subject;
}

public static function getFormTemplate($mode = '') {
public static function getFormTemplate($mode = '', $template_id = null) {
$form_template = array();
$astroidElements = Helper::getAllAstroidElements($mode);
$astroidElements = Helper::getAllAstroidElements($mode, $template_id);
foreach ($astroidElements as $astroidElement) {
$form_template[$astroidElement->type] = $astroidElement->renderJson('addon');
}
if ($mode !== 'article_data') {
$sectionElement = new Element('section');
$form_template['section'] = $sectionElement->renderJson();
$rowElement = new Element('row');
$form_template['row'] = $rowElement->renderJson();
$columnElement = new Element('column');
$form_template['column'] = $columnElement->renderJson();
$sublayout = new Element('sublayout');
$form_template['sublayout'] = $sublayout->renderJson('sublayout');
$template = $template_id !== null ? Framework::getTemplate($template_id) : Framework::getTemplate();
foreach (['section', 'row', 'column', 'sublayout'] as $form_type) {
$form = new Element($form_type, [], $template, $mode);
$form_template[$form_type] = $form_type == 'sublayout' ? $form->renderJson('sublayout') : $form->renderJson();
}
}
return $form_template;
}
Expand Down
3 changes: 2 additions & 1 deletion framework/library/astroid/Helper/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class Constants
{
public static $astroid_version = '3.1.0-rc4';
public static $astroid_version = '3.1.0-rc5';
public static $fontawesome_version = '6.5.2';
public static $fancybox_version = '5.0';
public static $animatecss_version = '3.7.0';
Expand Down Expand Up @@ -46,6 +46,7 @@ public static function manager_configs($mode = '') : array
'site_url' => Uri::root(),
'base_url' => Uri::base(true),
'astroid_media_url' => ASTROID_MEDIA_URL,
'template_id' => $template->id,
'template_name' => $template->template.'-'.$template->id,
'tpl_template_name' => $template->template,
'template_title' => $template->title,
Expand Down
39 changes: 31 additions & 8 deletions framework/library/astroid/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

namespace Astroid;
use \Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
use Joomla\Filesystem\Path;
use Joomla\Registry\Registry;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\Filesystem\Folder;

defined('_JEXEC') or die;

Expand Down Expand Up @@ -189,8 +192,12 @@ public function isDefault($id = 0)
if (!$id) {
$id = $this->id;
}
$db = Factory::getDbo();
$query = "SELECT `home` FROM `#__template_styles` WHERE `id`='$id'";
$db = Factory::getContainer()->get(DatabaseInterface::class);
$query = $db
->getQuery(true)
->select('`home`')
->from('#__template_styles')
->where('`id`=' . $id);
$db->setQuery($query);
$result = $db->loadResult();
if ($result == 1) {
Expand Down Expand Up @@ -222,8 +229,12 @@ protected function _variableOverrides($variables)

private function _getById($id)
{
$db = Factory::getDbo();
$query = "SELECT `template`,`id`,`title`,`params`,`home` FROM `#__template_styles` WHERE `id`='$id'";
$db = Factory::getContainer()->get(DatabaseInterface::class);
$query = $db
->getQuery(true)
->select('`template`,`id`,`title`,`params`,`home`')
->from('#__template_styles')
->where('`id`=' . $id);
$db->setQuery($query);
$result = $db->loadObject();

Expand Down Expand Up @@ -342,12 +353,24 @@ public function getLayout()
public function getElementLayout($type)
{
$template_path = JPATH_SITE . "/media/templates/site/{$this->template}/astroid/elements";
if (file_exists($template_path . '/' . $type . '/' . $type . '.php')) {
return $template_path . '/' . $type . '/' . $type . '.php';
if (file_exists(Path::clean($template_path . '/' . $type . '/' . $type . '.php'))) {
return Path::clean($template_path . '/' . $type . '/' . $type . '.php');
}

if (file_exists(ASTROID_ELEMENTS . '/' . $type . '/' . $type . '.php')) {
return ASTROID_ELEMENTS . '/' . $type . '/' . $type . '.php';
$plugin_path = JPATH_SITE . "/plugins/astroid";
if (file_exists($plugin_path)) {
$plugin_folders = Folder::folders($plugin_path);
if (count($plugin_folders)) {
foreach ($plugin_folders as $plugin_folder) {
if (file_exists(Path::clean($plugin_path . '/' . $plugin_folder . '/elements/' . $type . '/' . $type . '.php'))) {
return Path::clean($plugin_path . '/' . $plugin_folder . '/elements/' . $type . '/' . $type . '.php');
}
}
}
}

if (file_exists(Path::clean(ASTROID_ELEMENTS . '/' . $type . '/' . $type . '.php'))) {
return Path::clean(ASTROID_ELEMENTS . '/' . $type . '/' . $type . '.php');
}

throw new \Exception("Astroid can not found layout for `" . $type . "` element.");
Expand Down

0 comments on commit 7cf85a4

Please sign in to comment.