Commit c7ed6a76 authored by Aaron Wells's avatar Aaron Wells Committed by Gerrit Code Review
Browse files

New Dwoo tag {ifconfig} for an if/else based on get_config()

Bug #1168213

Change-Id: I578ff2a111a8eb6ec5534647fd7a2288a97fe696
parent 00768be5
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2013 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage dwoo
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2013 Catalyst IT Ltd http://catalyst.net.nz
*
* This file incorporates portions of the Dwoo_Plugin_if class, covered by the
* following copyright and permission notice:
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://dwoo.org/LICENSE Modified BSD License
* @link http://dwoo.org/
* @date 2008-10-23
*/
/**
* A dwoo tag that displays the content in the block if the Mahara config setting matches a particular
* value.
*
* See the init() function for an explanation of its parameters. It can be used in the following ways:
* {ifconfig key=foo}bar{/ifconfig} : Same as if (get_config('foo')) { echo "bar"; }
* {ifconfig key=foo plugintype=block pluginname=blog} : Uses get_config_plugin()
* {ifconfig key=foo plugintype=block pluginid=12} : Uses get_config_plugin_instance()
*
* It can also take a compareTo tag. If present, the value of the config will be compared to it.
*
* Example: {ifconfig key=foo compareTo=false}
* Same as: if (get_config('foo') == false) {
*
* This can be a string, integer, true, false, or null. It's compared with "==".
*
* This tag can also take an {else} tag, the same as {if}.
*/
class Dwoo_Plugin_ifconfig extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
{
/**
* This function does nothing. Dwoo uses it to determine by reflection what attributes the tag
* can take.
*
* @param string $key The only required attribute.
* @param string $plugintype (Optional) Supply if you want to get a plugin or plugin-instance config
* @param string $pluginname (Optional) Supply if you want to get a plugin config
* @param integer $pluginid (Optional) Supply if you want to get a plugin instance config
* @param variable $compareTo (Opional) Value to compare the config to (with ==). Defaults to TRUE.
*/
public function init($key, $plugintype=null, $pluginname=null, $pluginid=null, $compareTo=true) {
}
/**
* Called when Dwoo hits the opening {ifconfig} tag.
* @param Dwoo_Compiler $compiler
* @param array $params
* @param unknown_type $prepend
* @param unknown_type $append
* @param unknown_type $type
*/
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) {
return '';
}
/**
* Called when Dwoo hits the closing {ifconfig} tag. Returns a PHP string that will make the
* appropriate get_config(), get_config_plugin() or get_config_plugin_instance() call when the
* compiled template is executed.
*
* @param Dwoo_Compiler $compiler
* @param array $params
* @param unknown_type $prepend
* @param unknown_type $append
* @param string $content The contents between the opening and closing tags
*/
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) {
$params = $compiler->getCompiledParams($params);
$key = $params['key'];
$plugintype = $params['plugintype'];
$pluginname = $params['pluginname'];
$pluginid = trim($params['pluginid'], '\'"');
$compareTo = $params['compareTo'];
if ($plugintype != 'null' && $pluginname != 'null') {
$function = "get_config_plugin({$plugintype}, {$pluginname}, {$key})";
}
else if ($plugintype != 'null' && $pluginid != 'null') {
$function = "get_config_plugin_instance({$plugintype}, {$pluginid}, {$key})";
}
else {
$function = "get_config({$key})";
}
$pre = Dwoo_Compiler::PHP_OPEN."if ({$function} == {$compareTo}) {\n".Dwoo_Compiler::PHP_CLOSE;
$post = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
if (isset($params['hasElse'])) {
$post .= $params['hasElse'];
}
return $pre . $content . $post;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment