TrackManiaControl/application/core/Libs/FML/Script/Features/EntrySubmit.php

102 lines
2.2 KiB
PHP
Raw 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
2014-04-27 14:44:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 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
*/
class EntrySubmit extends ScriptFeature {
/*
* Protected Properties
*/
2014-05-14 23:24:00 +02:00
/** @var Entry $entry */
2014-04-27 14:44:40 +02:00
protected $entry = null;
protected $url = null;
/**
* Construct a new Entry Submit Feature
*
2014-05-14 23:24:00 +02:00
* @param Entry $entry (optional) Entry Control
* @param string $url (optional) Submit Url
2014-04-27 14:44:40 +02:00
*/
public function __construct(Entry $entry = null, $url = null) {
$this->setEntry($entry);
$this->setUrl($url);
}
/**
* Set the Entry
*
* @param Entry $entry Entry Control
* @return \FML\Script\Features\EntrySubmit
*/
public function setEntry(Entry $entry) {
$entry->checkId();
$entry->setScriptEvents(true);
$this->entry = $entry;
return $this;
}
/**
* Set the Submit Url
*
* @param string $url Submit Url
* @return \FML\Script\Features\EntrySubmit
*/
public function setUrl($url) {
2014-05-14 23:24:00 +02:00
$this->url = (string)$url;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* @see \FML\Script\Features\ScriptFeature::prepare()
*/
public function prepare(Script $script) {
$script->setScriptInclude(ScriptInclude::TEXTLIB);
2014-05-20 15:44:45 +02:00
$controlScript = new ControlScript($this->entry, $this->getScriptText(), ScriptLabel::ENTRYSUBMIT);
$controlScript->prepare($script);
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Get the Script Text
*
* @return string
*/
protected function getScriptText() {
2014-05-14 23:24:00 +02:00
$url = $this->buildCompatibleUrl();
$entryName = Builder::escapeText($this->entry->getName());
2014-04-27 14:44:40 +02:00
$scriptText = "
2014-05-20 15:44:45 +02:00
declare Value = TextLib::URLEncode(Entry.Value);
OpenLink(\"{$url}{$entryName}=\"^Value, CMlScript::LinkType::Goto);
";
2014-04-27 14:44:40 +02:00
return $scriptText;
}
/**
* Build the Submit Url compatible for the Entry Parameter
*
* @return string
*/
protected function buildCompatibleUrl() {
2014-05-14 23:24:00 +02:00
$url = $this->url;
2014-04-27 14:44:40 +02:00
$paramsBegin = stripos($url, '?');
if (!is_int($paramsBegin) || $paramsBegin < 0) {
$url .= '?';
2014-05-14 23:24:00 +02:00
} else {
2014-04-27 14:44:40 +02:00
$url .= '&';
}
return $url;
}
}