TrackManiaControl/libs/FML/Script/Features/EntrySubmit.php

143 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2014-04-27 14:44:40 +02:00
<?php
namespace FML\Script\Features;
use FML\Controls\Entry;
2014-05-14 23:24:00 +02:00
use FML\Script\Builder;
use FML\Script\Script;
2014-04-27 14:44:40 +02:00
use FML\Script\ScriptInclude;
2014-05-14 23:24:00 +02:00
use FML\Script\ScriptLabel;
2014-04-27 14:44:40 +02:00
/**
* Script Feature for submitting an Entry
*
2014-05-14 23:24:00 +02:00
* @author steeffeen
2017-03-25 18:40:15 +01:00
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
2014-05-14 23:24:00 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-04-27 14:44:40 +02:00
*/
2017-03-25 18:40:15 +01:00
class EntrySubmit extends ScriptFeature
{
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var Entry $entry Entry
*/
protected $entry = null;
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var string $url Sumit url
*/
protected $url = null;
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/**
* Construct a new Entry Submit
*
* @api
* @param Entry $entry (optional) Entry Control
* @param string $url (optional) Submit url
*/
public function __construct(Entry $entry = null, $url = null)
{
if ($entry) {
$this->setEntry($entry);
}
if ($url) {
$this->setUrl($url);
}
}
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/**
* Get the Entry
*
* @api
* @return Entry
*/
public function getEntry()
{
return $this->entry;
}
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/**
* Set the Entry
*
* @api
* @param Entry $entry Entry Control
* @return static
*/
public function setEntry(Entry $entry)
{
$entry->setScriptEvents(true)
->checkId();
$this->entry = $entry;
return $this;
}
/**
* Get the submit url
*
* @api
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set the submit url
*
* @api
* @param string $url Submit url
* @return static
*/
public function setUrl($url)
{
$this->url = (string)$url;
return $this;
}
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
$script->setScriptInclude(ScriptInclude::TEXTLIB);
$controlScript = new ControlScript($this->entry, $this->getScriptText(), ScriptLabel::ENTRYSUBMIT);
$controlScript->prepare($script);
return $this;
}
/**
* Get the script text
*
* @return string
*/
protected function getScriptText()
{
$url = $this->buildCompatibleUrl();
$entryName = $this->entry->getName();
$link = Builder::escapeText($entryName . $url . "=");
return "
2014-05-20 15:44:45 +02:00
declare Value = TextLib::URLEncode(Entry.Value);
2014-06-21 03:18:21 +02:00
OpenLink({$link}^Value, CMlScript::LinkType::Goto);
2014-05-20 15:44:45 +02:00
";
2017-03-25 18:40:15 +01:00
}
/**
* Build the submit url compatible for the Entry parameter
*
* @return string
*/
protected function buildCompatibleUrl()
{
$url = $this->url;
$paramsBegin = stripos($url, '?');
if (!is_int($paramsBegin) || $paramsBegin < 0) {
$url .= '?';
} else {
$url .= '&';
}
return $url;
}
2014-04-27 14:44:40 +02:00
}