Skip to content

Commit

Permalink
Added requirements to configuration
Browse files Browse the repository at this point in the history
It is now possible to disable a configuration item based on whether or
not another configuration item is enabled.

This was needed for the logon background album selection, since it
requires the sharing feature to be enabled.

At this moment the function is only implemented for SELECT boxes as that
is the one currently needed, it should be easy to expand it to other
type of configuration items once the need is there.

Fixes #72
  • Loading branch information
jeroenrnl committed Apr 16, 2016
1 parent 7c22587 commit 1fc9bc2
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 8 deletions.
29 changes: 29 additions & 0 deletions UnitTests/configTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @author Jeroen Roos
*/

require_once "testSetup.php";

use db\insert;
use db\param;
use db\db;
Expand Down Expand Up @@ -88,5 +90,32 @@ public function testUnkownConfItem() {
conf::set("path.images", getcwd() . "/.images");
}

/**
* Test an item which requires another item to be enabled
*/
public function testConfRequirements() {
$loginAlb=conf::get("interface.logon.background.album");
$this->assertNull($loginAlb);

$share=conf::get("share.enable");
$this->assertFalse($share);

$loginAlb=conf::set("interface.logon.background.album", 7);
$loginAlb->update();

// Despite setting it, retrieving it will still return the
// default value.
$loginAlb=conf::get("interface.logon.background.album");
$this->assertNull($loginAlb);

// Now we enable sharing:
$share=conf::set("share.enable", true);
$share->update();
$share=conf::get("share.enable");
$this->assertTrue($share);

// And now, it will return the previously set album number
$loginAlb=conf::get("interface.logon.background.album");
$this->assertEquals(7, $loginAlb);
}
}
1 change: 1 addition & 0 deletions php/classes/confDefault.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private static function getConfigInterface() {
$intLogonBgAlbum->addOptions(album::getSelectArray());
$intLogonBgAlbum->setOptionsTranslate(false);
$intLogonBgAlbum->setDefault(null);
$intLogonBgAlbum->requiresEnabled(new confItemBool("share.enable"));

$interface[]=$intLogonBgAlbum;

Expand Down
41 changes: 40 additions & 1 deletion php/classes/confItem.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ abstract class confItem extends zophTable {
protected $required=false;
/** @var bool internal, internal settings can not be changed from webinterface */
protected $internal=false;
/** @var array list of items that MUST be enabled for this item to be enabled */
protected $requiresEnabled=array();
/** @var array list of unmet requirements */
protected $unmet=array();

/**
* Create confItem object
Expand Down Expand Up @@ -124,7 +128,7 @@ final public function getDesc() {
* @return string value
*/
final public function getValue() {
if (!isset($this->fields["value"]) || $this->fields["value"]===null) {
if (!isset($this->fields["value"]) || $this->fields["value"]===null || !$this->requirementsMet()) {
return $this->getDefault();
} else {
return $this->fields["value"];
Expand Down Expand Up @@ -220,6 +224,41 @@ final public function setDefault($default) {
$this->default=$default;
}

/**
* This item requires another item to be enabled
*/
final public function requiresEnabled(confItemBool $item) {
$this->requiresEnabled[]=$item;
}

/**
* Are all requirements met?
*/
final protected function requirementsMet() {
$met=true;
foreach ($this->requiresEnabled as $req) {
$req->lookup();
if ((bool) $req->getValue() === false) {
$met=false;
$this->unmet[$req->getName()]="enabled";
}
}
return $met;
}

/**
* Return a template block to show the unmet requirements for this
* confItem.
* @return block overview of unmet items
*/
final protected function displayUnmetRequirements() {
if (!$this->requirementsMet()) {
return new block("confUnmetRequirements", array(
"unmet" => $this->unmet
));
}
}

/**
* Display the item
*/
Expand Down
12 changes: 7 additions & 5 deletions php/classes/confItemSelect.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ public function display() {
return;
}
$params=array(
"label" => e(translate($this->getLabel(), 0)),
"name" => e($this->getName()),
"value" => e($this->getValue()),
"desc" => e(translate($this->getDesc(), 0)),
"hint" => e(translate($this->getHint(), 0)),
"label" => e(translate($this->getLabel(), 0)),
"name" => e($this->getName()),
"value" => e($this->getValue()),
"desc" => e(translate($this->getDesc(), 0)),
"unmet" => $this->displayUnmetRequirements(),
"hint" => e(translate($this->getHint(), 0)),
"enabled" => (bool) $this->requirementsMet()
);
if ($this->translate) {
$params["options"] = translate($this->getOptions(), 0);
Expand Down
5 changes: 3 additions & 2 deletions php/templates/default/blocks/confItemSelect.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
if (!ZOPH) { die("Illegal call"); }
?>
<label for="<?php echo $tpl_name; ?>"><?php echo $tpl_label; ?></label>
<select name="<?php echo $tpl_name ?>">
<select <?= ($tpl_enabled ? "" : "disabled") ?> name="<?php echo $tpl_name ?>">
<?php foreach ($tpl_options as $option=>$label): ?>
<?php if ($tpl_value==$option): ?>
<?php $selected="selected"; ?>
Expand All @@ -43,7 +43,8 @@
<?php endif; ?>
<?php if (!empty($tpl_desc)): ?>
<div class="desc">
<?php echo $tpl_desc ?>
<?= $tpl_desc ?>
<?= $tpl_unmet ?>
</div>
<?php endif; ?>

33 changes: 33 additions & 0 deletions php/templates/default/blocks/confUnmetRequirements.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Template for a list of unmet requirements
*
* Zoph 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 2 of the License, or
* (at your option) any later version.
*
* Zoph 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 Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Jeroen Roos
* @package ZophTemplates
*/

if (!ZOPH) {
die("Illegal call");
}
?>
<span class="unmetRequirements">
<br>
<?= translate("This item is disabled because it requires") ?>
<?php foreach ($tpl_unmet as $name => $value): ?>
<b><?= $name ?></b> <?= translate("to be") ?> <?= $value ?>
<?php endforeach; ?>
</span>

9 changes: 9 additions & 0 deletions php/templates/default/css.php
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,15 @@
float: left;
}

select:disabled {
background: #ddd;
border: 1px solid #bbb;
}

span.unmetRequirements {
color: red;
}

form.geotag select,
form.import select,
form.geotag input,
Expand Down

0 comments on commit 1fc9bc2

Please sign in to comment.