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 {
|
|
|
|
/*
|
2014-06-21 03:18:21 +02:00
|
|
|
* Protected properties
|
2014-04-27 14:44:40 +02:00
|
|
|
*/
|
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
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param string $url (optional) Submit url
|
2014-04-27 14:44:40 +02:00
|
|
|
*/
|
|
|
|
public function __construct(Entry $entry = null, $url = null) {
|
2014-08-11 23:15:53 +02:00
|
|
|
if ($entry !== null) {
|
2014-06-21 03:18:21 +02:00
|
|
|
$this->setEntry($entry);
|
|
|
|
}
|
2014-04-27 14:44:40 +02:00
|
|
|
$this->setUrl($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Entry
|
|
|
|
*
|
|
|
|
* @param Entry $entry Entry Control
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-04-27 14:44:40 +02:00
|
|
|
*/
|
|
|
|
public function setEntry(Entry $entry) {
|
2014-06-21 03:18:21 +02:00
|
|
|
$this->entry = $entry->checkId()->setScriptEvents(true);
|
2014-04-27 14:44:40 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set the submit url
|
2014-04-27 14:44:40 +02:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param string $url Submit url
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-04-27 14:44:40 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Get the script text
|
2014-04-27 14:44:40 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getScriptText() {
|
2014-06-21 03:18:21 +02:00
|
|
|
$url = $this->buildCompatibleUrl();
|
|
|
|
$entryName = $this->entry->getName();
|
|
|
|
$link = Builder::escapeText($entryName . $url . '=', true);
|
|
|
|
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
|
|
|
";
|
2014-04-27 14:44:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Build the submit url compatible for the Entry parameter
|
2014-04-27 14:44:40 +02:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|