Skip to content

Commit

Permalink
Allow plugins to use expressions to check whether they should be loaded
Browse files Browse the repository at this point in the history
Also add theme an URL parameter to getXwd(), so the plugin expression can check the current theme. This allows plugins that are specifically written for themes
  • Loading branch information
torinfo committed Nov 5, 2023
1 parent 2245b38 commit 48adc5c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
8 changes: 8 additions & 0 deletions editor/js/language.js
Expand Up @@ -395,6 +395,14 @@ var EDITOR = (function ($, parent) {
language_files = (languagecodevariable=='en-GB' ? en_language_files : foreign_language_files);
$(language_files).each(function() {
var _this = this;
// If _this.u contains getXwd, add parameters for plugin conditions
if (_this.u.indexOf("getXwd") != -1) {
_this.u += "?simple_mode=" + simple_mode;
_this.u += "&simple_lo_page=" + simple_lo_page;
_this.u += "&template_sub_pages=" + template_sub_pages;
_this.u += "&languagecode=" + languagecodevariable;
_this.u += "&theme=" + theme;
}
$.ajax({
type: "GET",
url: _this.u,
Expand Down
5 changes: 5 additions & 0 deletions modules/xerte/edithtml.php
Expand Up @@ -25,6 +25,7 @@
* Time: 12:24
*/

require_once (__DIR__ . "/../../website_code/php/xmlInspector.php");

function get_children ($parent_id, $lookup, $column, $type) {
// children
Expand Down Expand Up @@ -87,6 +88,9 @@ function output_editor_code($row_edit, $xerte_toolkits_site, $read_status, $vers
$data_url = $xerte_toolkits_site->users_file_area_short . $row_edit['template_id'] . "-" . $row_username['username'] . "-" . $row_edit['template_name'] . "/data.xml";
$rlo_url = $xerte_toolkits_site->site_url . $xerte_toolkits_site->users_file_area_short . $row_edit['template_id'] . "-" . $row_username['username'] . "-" . $row_edit['template_name'];

$xml = new XerteXMLInspector();
$xml->loadTemplateXML($preview);
$theme = $xml->getTheme();

// Derived templates
$xwd_url = "modules/" . $row_edit['template_framework'] . "/templates/" . $row_edit['template_name'] . "/";
Expand Down Expand Up @@ -476,6 +480,7 @@ function output_editor_code($row_edit, $xerte_toolkits_site, $read_status, $vers
}
echo "templateframework=\"" . $row_edit['template_framework'] . "\";\n";
echo "oai_pmh_available=" . ($oai_pmh ? "true" : "false") . ";\n";
echo "theme=\"" . $theme . "\";\n";
?>

function bunload(){
Expand Down
66 changes: 66 additions & 0 deletions modules/xerte/parent_templates/Nottingham/wizards/getXwd.php
Expand Up @@ -26,6 +26,56 @@ function getParentPath($path)

}

function evaluateConditionExpression($ctree)
{
switch ($ctree['type']) {
case "Literal":
return $ctree['value'];
case "LogicalExpression":
if ($ctree['operator'] == "&&") {
return evaluateConditionExpression($ctree['left']) && evaluateConditionExpression($ctree['right']);
} else {
return evaluateConditionExpression($ctree['left']) || evaluateConditionExpression($ctree['right']);
}
case "BinaryExpression":
switch ($ctree['operator']) {
case "==":
return evaluateConditionExpression($ctree['left']) == evaluateConditionExpression($ctree['right']);
case "!=":
return evaluateConditionExpression($ctree['left']) != evaluateConditionExpression($ctree['right']);
case "<":
return evaluateConditionExpression($ctree['left']) < evaluateConditionExpression($ctree['right']);
case "<=":
return evaluateConditionExpression($ctree['left']) <= evaluateConditionExpression($ctree['right']);
case ">":
return evaluateConditionExpression($ctree['left']) > evaluateConditionExpression($ctree['right']);
case ">=":
return evaluateConditionExpression($ctree['left']) >= evaluateConditionExpression($ctree['right']);
default:
return null;
}
case "MemberExpression":
break;
case "Identifier":
if (isset($_REQUEST[$ctree['name']])) {
return $_REQUEST[$ctree['name']];
} else if (isset($_SESSION[$ctree['name']])) {
return $_SESSION[$ctree['name']];
} else {
try {
$value = eval($ctree['name']);
return $value;
}
catch (Exception $e){};
return null;
}
break;
default:
// Unexpected node parsed
return null;
}
}

$xwd_path = dirname(__DIR__) .'/';

if (file_exists($xwd_path . "wizards/" . $_SESSION['toolkits_language'] . "/data.xwd" ))
Expand Down Expand Up @@ -62,6 +112,22 @@ function getParentPath($path)
{
if (substr($plugin_file, -4) == ".xwd")
{
// Check condition for this file, currently only theme
$xml = simplexml_load_file($plugin_path . "/" . $plugin_file);
$condition = (string)$xml['cond'];
_debug("Condition: " . $condition);
if ($condition != null && $condition != "")
{
require_once (__DIR__ . "/../../../../../website_code/php/phpep/PHPEP.php");
$phpep = new PHPEP($condition);
$ctree = $phpep->exec();
$result = evaluateConditionExpression($ctree);
_debug("Result of evalutaion of condition: " . ($result === true ? 'true' : ($result === false ? 'false' : $result)));
if ($result !== true)
{
continue;
}
}
// Merge the custom file into the main file.
$merged->addFile($plugin_path . "/" . $plugin_file);
}
Expand Down

0 comments on commit 48adc5c

Please sign in to comment.