<?php
use Nette\Forms\Controls\TextInput,
Nette\Forms\Form,
Nette\Utils\Html;
/**
* AntispamControl
* add basic antispam feature to Nette forms.
*
* <code>
* // Register extension
* AntispamControl::register();
*
* // Add antispam to form
* $form->addAntispam();
* </code>
*
* @version 0.4
* @author Michal Mikoláš <nanuqcz@gmail.com>
* @license CC BY <http://creativecommons.org/licenses/by/3.0/cz/>
*/
class AntispamControl extends TextInput
{
/** @var int minimum delay [sec] to send form */
public static $minDelay = 5;
/**
* Register Antispam to Nette Forms
* @return void
*/
public static function register()
{
Form::extensionMethod('addAntispam', function(Form $form, $name = 'spam', $label = 'Toto pole vymažte', $msg = 'Byl detekován pokus o spam.'){
// "All filled" protection
$form[$name] = new AntispamControl($label, NULL, NULL, $msg);
// "Send delay" protection
$form->addHidden('form_created', strtr(time(), '0123456789', 'jihgfedcba'))
->addRule(
function($item){
if (AntispamControl::$minDelay <= 0) return TRUE; // turn off "Send delay protection"
$value = (int)strtr($item->value, 'jihgfedcba', '0123456789');
return $value <= (time() - AntispamControl::$minDelay);
},
$msg
);
return $form;
});
}
/**
* @param string|Html
* @param int
* @param int
* @param string
*/
public function __construct($label = '', $cols = NULL, $maxLength = NULL, $msg = '')
{
parent::__construct($label, $cols, $maxLength);
$this->setDefaultValue('http://');
$this->addRule(Form::BLANK, $msg);
}
/**
* @return TextInput
*/
public function getControl()
{
$control = parent::getControl();
$control = $this->addAntispamScript($control);
return $control;
}
/**
* @param Html
* @return Html
*/
protected function addAntispamScript(Html $control)
{
$control = Html::el('')->add($control);
$control->add( Html::el('script', array('type' => 'text/javascript'))->setHtml("
// Clear input value
var input = document.getElementById('" . $control[0]->id . "');
input.value = '';
// Hide input and label
if (input.parentNode.parentNode.nodeName == 'TR') {
// DefaultFormRenderer
input.parentNode.parentNode.style.display = 'none';
} else {
// Manual render
input.style.display = 'none';
var labels = input.parentNode.getElementsByTagName('label');
for (var i = 0; i < labels.length; i++) { // find and hide label
if (labels[i].getAttribute('for') == '" . $control[0]->id . "') {
labels[i].style.display = 'none';
}
}
}
")
);
return $control;
}
}
<?php
class ImageResponse extends \Nette\Object implements \Nette\Application\IResponse
{
/** @var \Nette\Image|string */
private $image;
/**
* @param \Nette\Image|string
*/
public function __construct($image)
{
if (!$image instanceof \Nette\Image && !file_exists($image)) {
throw new \Nette\InvalidArgumentException('Image must be Nette\Image or file path');
}
$this->image = $image;
}
/**
* @param \Nette\Http\IRequest
* @param \Nette\Http\IResponse
*/
public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse)
{
if ($this->image instanceof \Nette\Image) {
$this->image->send();
return;
}
$httpResponse->setContentType(\Nette\Utils\MimeTypeDetector::fromFile($this->image));
$length = filesize($this->image);
$handle = fopen($this->image, 'r');
$httpResponse->setHeader('Content-Length', $length);
while (!feof($handle)) {
echo fread($handle, 4e6);
}
fclose($handle);
}
}
<?php
$im = imagecreatefromjpeg("match-trailer.jpg");
$font = 'fonts/calibrib.ttf';
$fontNormal = 'fonts/calibri.ttf';
$teams = mb_strtoupper("FC Combix - FC Bohemians Praha B", 'UTF-8');
$white = ImageColorAllocate($im, 255, 255, 255);
$orange = ImageColorAllocate($im, 255, 127, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
// Týmy
ImagettfText($im, $fontSize = 34, 0, 30, 80, $white, $font, $teams);
// Kde
ImagettfText($im, $fontSize = 20, 0, 30, 170, $orange, $font, 'KDY:');
ImagettfText($im, $fontSize = 20, 0, 95, 170, $white, $fontNormal, '25.8.2014');
// Kde
ImagettfText($im, $fontSize = 20, 0, 30, 220, $orange, $font, 'KDE:');
ImagettfText($im, $fontSize = 20, 0, 95, 220, $white, $fontNormal, 'Stadiony Bohemians, Přípotoční 27/a');
// Vstupné
ImagettfText($im, $fontSize = 24, 0, 30, 420, $black, $font, 'VSTUPNÉ:');
ImagettfText($im, $fontSize = 30, 0, 180, 420, $orange, $font, '60 Kč');
// Vystup
imageJpeg($im, "upoutavka.jpg");
ImageDestroy($im);
header('Content-type: image/jpg');
echo $image;
<?php
use Nette\Application\UI;
class FooControl extends UI\Control
{
public function __construct(...)
{
//predani parametru a zavislosti
}
public function attached($presenter)
{
parent::attached($presenter); //nezapomenout zavolat
if($presenter instanceof UI\Presenter) { //jelikoz to muze byt i necho jinyho
//tady nejdriv muzes pracovat s presenterem - vytvaret linky, plnit sablonu....
}
}
public function handleFoo($bar) //zpracovani signalu foo s parametrem bar
{
//...
}
public function render()
{
$this->template->...
$this->template->render();
}
/*
jeste existujou pohledy pro komponenty, ale ty nedoporucuju, nefungujou s ajaxem
vykreslujou se pomoci {control foo:view}
v {control} muzes i predavat parametry, {control foo $xx, $yy} ale taky nedoporucuju, stejny problem jako s view
*/
public function renderView()
{
}
}
}
<?php
namespace Nette\Application\Responses;
use Nette;
/**
* CSV download response.
* Under New BSD license.
*
* @property-read string $name
* @property-read string $contentType
* @package Nette\Application\Responses
*/
class CsvResponse extends Nette\Object implements Nette\Application\IResponse
{
/** @var array */
private $data;
/** @var string */
private $name;
/** @var bool */
public $addHeading;
/** @var string */
public $glue;
/** @var string */
private $charset;
/** @var string */
private $contentType;
/**
* @param array[]|\Traversable $data
* @param string $name
* @param bool $addHeading
* @param string $glue
* @param string $charset
* @param string $contentType
* @throws \InvalidArgumentException
*/
public function __construct($data, $name = NULL, $addHeading = TRUE, $glue = ';', $charset = 'utf-8', $contentType = NULL)
{
if (is_array($data)) {
if (count($data) && !is_array(reset($data))) {
$invalid = TRUE;
}
} elseif (!$data instanceof \Traversable) {
$invalid = TRUE;
}
if (isset($invalid)) {
throw new \InvalidArgumentException(__CLASS__.": data must be array of arrays or instance of Traversable.");
}
if (empty($glue) || preg_match('/^[\n\r]+$/s', $glue) || $glue === '"') {
throw new \InvalidArgumentException(__CLASS__.": glue cannot be an empty or reserved character.");
}
$this->data = $data;
$this->name = $name;
$this->addHeading = $addHeading;
$this->glue = $glue;
$this->charset = $charset;
$this->contentType = $contentType ? $contentType : 'text/csv';
}
/**
* Returns the file name.
* @return string
*/
final public function getName()
{
return $this->name;
}
/**
* Returns the MIME content type of a downloaded content.
* @return string
*/
final public function getContentType()
{
return $this->contentType;
}
/**
* Sends response to output.
* @param Nette\Http\IRequest $httpRequest
* @param Nette\Http\IResponse $httpResponse
*/
public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
{
$httpResponse->setContentType($this->contentType, $this->charset);
if (empty($this->name)) {
$httpResponse->setHeader('Content-Disposition', 'attachment');
} else {
$httpResponse->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"');
}
$data = $this->formatCsv();
$httpResponse->setHeader('Content-Length', strlen($data));
print $data;
}
protected function formatCsv()
{
if (empty($this->data)) {
return '';
}
$csv = array();
if (!is_array($this->data)) {
$this->data = iterator_to_array($this->data);
}
$firstRow = reset($this->data);
if ($this->addHeading) {
if (!is_array($firstRow)) {
$firstRow = iterator_to_array($firstRow);
}
$labels = array();
foreach (array_keys($firstRow) as $key) {
$labels[] = ucfirst(str_replace(array("_", '"'), array(' ', '""'), $key));
}
$csv[] = '"'.join('"'.$this->glue.'"', $labels).'"';
}
foreach ($this->data as $row) {
if (!is_array($row)) {
$row = iterator_to_array($row);
}
foreach ($row as &$value) {
$value = str_replace(array('"'), array('""'), $value);
}
$csv[] = '"'.join('"'.$this->glue.'"', $row).'"';
}
return join("\r\n", $csv);
}
}