2014-05-14 23:24:00 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FML\Script\Features;
|
|
|
|
|
|
|
|
use FML\Controls\Entry;
|
|
|
|
use FML\Controls\Quad;
|
|
|
|
use FML\Models\CheckBoxDesign;
|
|
|
|
use FML\Script\Builder;
|
|
|
|
use FML\Script\Script;
|
|
|
|
use FML\Script\ScriptInclude;
|
|
|
|
use FML\Script\ScriptLabel;
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Script Feature for creating a CheckBox behavior
|
2014-05-14 23:24:00 +02:00
|
|
|
*
|
|
|
|
* @author steeffeen
|
|
|
|
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
|
|
|
class CheckBoxFeature extends ScriptFeature {
|
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const FUNCTION_UPDATE_QUAD_DESIGN = 'FML_UpdateQuadDesign';
|
|
|
|
const VAR_CHECKBOX_ENABLED = 'FML_CheckBox_Enabled';
|
|
|
|
const VAR_CHECKBOX_DESIGNS = 'FML_CheckBox_Designs';
|
|
|
|
const VAR_CHECKBOX_ENTRY_ID = 'FML_CheckBox_EntryId';
|
|
|
|
|
|
|
|
/*
|
2014-06-21 03:18:21 +02:00
|
|
|
* Protected properties
|
2014-05-14 23:24:00 +02:00
|
|
|
*/
|
|
|
|
/** @var Quad $quad */
|
|
|
|
protected $quad = null;
|
|
|
|
/** @var Entry $entry */
|
|
|
|
protected $entry = null;
|
2014-05-18 19:45:50 +02:00
|
|
|
protected $default = null;
|
2014-05-14 23:24:00 +02:00
|
|
|
/** @var CheckBoxDesign $enabledDesign */
|
|
|
|
protected $enabledDesign = null;
|
|
|
|
/** @var CheckBoxDesign $disabledDesign */
|
|
|
|
protected $disabledDesign = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new CheckBox Feature
|
|
|
|
*
|
2014-05-20 15:44:45 +02:00
|
|
|
* @param Quad $quad (optional) CheckBox Quad
|
|
|
|
* @param Entry $entry (optional) Hidden Entry
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param bool $default (optional) Default value
|
2014-05-14 23:24:00 +02:00
|
|
|
*/
|
2014-05-18 19:45:50 +02:00
|
|
|
public function __construct(Quad $quad = null, Entry $entry = null, $default = null) {
|
2014-08-11 23:15:53 +02:00
|
|
|
if ($quad !== null) {
|
2014-06-21 03:18:21 +02:00
|
|
|
$this->setQuad($quad);
|
|
|
|
}
|
2014-08-11 23:15:53 +02:00
|
|
|
if ($entry !== null) {
|
2014-06-21 03:18:21 +02:00
|
|
|
$this->setEntry($entry);
|
|
|
|
}
|
2014-08-11 23:15:53 +02:00
|
|
|
if ($default !== null) {
|
2014-06-21 03:18:21 +02:00
|
|
|
$this->setDefault($default);
|
|
|
|
}
|
2014-05-14 23:24:00 +02:00
|
|
|
$this->setEnabledDesign(CheckBoxDesign::defaultEnabledDesign());
|
|
|
|
$this->setDisabledDesign(CheckBoxDesign::defaultDisabledDesign());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the CheckBox Quad
|
|
|
|
*
|
|
|
|
* @param Quad $quad CheckBox Quad
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-14 23:24:00 +02:00
|
|
|
*/
|
2014-06-21 03:18:21 +02:00
|
|
|
public function setQuad(Quad $quad) {
|
|
|
|
$this->quad = $quad->checkId()->setScriptEvents(true);
|
2014-05-14 23:24:00 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the CheckBox Quad
|
|
|
|
*
|
|
|
|
* @return \FML\Controls\Quad
|
|
|
|
*/
|
|
|
|
public function getQuad() {
|
|
|
|
return $this->quad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the CheckBox Entry
|
|
|
|
*
|
|
|
|
* @param Entry $entry CheckBox Entry
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-14 23:24:00 +02:00
|
|
|
*/
|
2014-06-21 03:18:21 +02:00
|
|
|
public function setEntry(Entry $entry) {
|
|
|
|
$this->entry = $entry->checkId();
|
2014-05-14 23:24:00 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:44:22 +02:00
|
|
|
/**
|
|
|
|
* Get the managed Entry
|
|
|
|
*
|
|
|
|
* @return \FML\Controls\Entry
|
|
|
|
*/
|
|
|
|
public function getEntry() {
|
|
|
|
return $this->entry;
|
|
|
|
}
|
|
|
|
|
2014-05-18 19:45:50 +02:00
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set the default value
|
2014-05-18 19:45:50 +02:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param bool $default Default value
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-18 19:45:50 +02:00
|
|
|
*/
|
|
|
|
public function setDefault($default) {
|
|
|
|
$this->default = (bool)$default;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:24:00 +02:00
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set the enabled Design
|
2014-05-14 23:24:00 +02:00
|
|
|
*
|
2014-05-18 19:45:50 +02:00
|
|
|
* @param CheckBoxDesign $checkBoxDesign Enabled CheckBox Design
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-14 23:24:00 +02:00
|
|
|
*/
|
|
|
|
public function setEnabledDesign(CheckBoxDesign $checkBoxDesign) {
|
|
|
|
$this->enabledDesign = $checkBoxDesign;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set the disabled Design
|
2014-05-14 23:24:00 +02:00
|
|
|
*
|
2014-05-18 19:45:50 +02:00
|
|
|
* @param CheckBoxDesign $checkBoxDesign Disabled CheckBox Design
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-14 23:24:00 +02:00
|
|
|
*/
|
|
|
|
public function setDisabledDesign(CheckBoxDesign $checkBoxDesign) {
|
|
|
|
$this->disabledDesign = $checkBoxDesign;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \FML\Script\Features\ScriptFeature::prepare()
|
|
|
|
*/
|
|
|
|
public function prepare(Script $script) {
|
|
|
|
if ($this->getQuad()) {
|
|
|
|
$script->setScriptInclude(ScriptInclude::TEXTLIB);
|
2014-05-18 19:45:50 +02:00
|
|
|
$script->addScriptFunction(self::FUNCTION_UPDATE_QUAD_DESIGN, $this->buildUpdateQuadDesignFunction());
|
2014-05-14 23:24:00 +02:00
|
|
|
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->buildInitScriptText(), true);
|
|
|
|
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $this->buildClickScriptText());
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Build the function text
|
2014-05-14 23:24:00 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-05-18 19:45:50 +02:00
|
|
|
protected function buildUpdateQuadDesignFunction() {
|
2014-06-21 03:18:21 +02:00
|
|
|
return "
|
2014-05-14 23:24:00 +02:00
|
|
|
Void " . self::FUNCTION_UPDATE_QUAD_DESIGN . "(CMlQuad _Quad) {
|
|
|
|
declare " . self::VAR_CHECKBOX_ENABLED . " as Enabled for _Quad = True;
|
|
|
|
Enabled = !Enabled;
|
2014-05-16 22:44:22 +02:00
|
|
|
_Quad.StyleSelected = Enabled;
|
2014-05-14 23:24:00 +02:00
|
|
|
declare " . self::VAR_CHECKBOX_DESIGNS . " as Designs for _Quad = Text[Boolean];
|
|
|
|
declare Design = Designs[Enabled];
|
|
|
|
declare DesignParts = TextLib::Split(\"|\", Design);
|
|
|
|
if (DesignParts.count > 1) {
|
|
|
|
_Quad.Style = DesignParts[0];
|
|
|
|
_Quad.Substyle = DesignParts[1];
|
|
|
|
} else {
|
|
|
|
_Quad.ImageUrl = Design;
|
|
|
|
}
|
2014-06-21 03:18:21 +02:00
|
|
|
declare " . self::VAR_CHECKBOX_ENTRY_ID . " as EntryId for _Quad = " . Builder::EMPTY_STRING . ";
|
|
|
|
if (EntryId != " . Builder::EMPTY_STRING . ") {
|
2014-05-14 23:24:00 +02:00
|
|
|
declare Value = \"0\";
|
|
|
|
if (Enabled) {
|
|
|
|
Value = \"1\";
|
|
|
|
}
|
|
|
|
declare Entry <=> (Page.GetFirstChild(EntryId) as CMlEntry);
|
|
|
|
Entry.Value = Value;
|
|
|
|
}
|
|
|
|
}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Build the init script text
|
2014-05-14 23:24:00 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function buildInitScriptText() {
|
2014-06-21 03:18:21 +02:00
|
|
|
$quadId = $this->getQuad()->getId(true, true);
|
|
|
|
$entryId = '""';
|
2014-05-14 23:24:00 +02:00
|
|
|
if ($this->entry) {
|
2014-06-21 03:18:21 +02:00
|
|
|
$entryId = $this->entry->getId(true, true);
|
2014-05-14 23:24:00 +02:00
|
|
|
}
|
2014-05-18 19:45:50 +02:00
|
|
|
$default = Builder::getBoolean($this->default);
|
2014-05-14 23:24:00 +02:00
|
|
|
$enabledDesignString = $this->enabledDesign->getDesignString();
|
|
|
|
$disabledDesignString = $this->disabledDesign->getDesignString();
|
2014-06-21 03:18:21 +02:00
|
|
|
return "
|
|
|
|
declare Quad_CheckBox <=> (Page.GetFirstChild({$quadId}) as CMlQuad);
|
2014-05-14 23:24:00 +02:00
|
|
|
declare Text[Boolean] " . self::VAR_CHECKBOX_DESIGNS . " as Designs for Quad_CheckBox;
|
2014-06-21 03:18:21 +02:00
|
|
|
Designs[True] = {$enabledDesignString};
|
|
|
|
Designs[False] = {$disabledDesignString};
|
2014-05-14 23:24:00 +02:00
|
|
|
declare Boolean " . self::VAR_CHECKBOX_ENABLED . " as Enabled for Quad_CheckBox;
|
2014-05-15 23:05:57 +02:00
|
|
|
Enabled = !{$default};
|
2014-05-14 23:24:00 +02:00
|
|
|
declare Text " . self::VAR_CHECKBOX_ENTRY_ID . " as EntryId for Quad_CheckBox;
|
2014-06-21 03:18:21 +02:00
|
|
|
EntryId = {$entryId};
|
2014-05-14 23:24:00 +02:00
|
|
|
" . self::FUNCTION_UPDATE_QUAD_DESIGN . "(Quad_CheckBox);
|
|
|
|
";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Build the script text for Quad clicks
|
2014-05-14 23:24:00 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function buildClickScriptText() {
|
2014-06-21 03:18:21 +02:00
|
|
|
$quadId = $this->getQuad()->getId(true, true);
|
|
|
|
return "
|
|
|
|
if (Event.ControlId == {$quadId}) {
|
2014-05-14 23:24:00 +02:00
|
|
|
declare Quad_CheckBox <=> (Event.Control as CMlQuad);
|
|
|
|
" . self::FUNCTION_UPDATE_QUAD_DESIGN . "(Quad_CheckBox);
|
|
|
|
}";
|
|
|
|
}
|
|
|
|
}
|