<?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);
}
}
function validateDate($date, $format = 'Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
var_dump(validateDate('2012-02-28 12:12:12')); # true
var_dump(validateDate('2012-02-30 12:12:12')); # false
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false
var_dump(validateDate('14:50', 'H:i')); # true
var_dump(validateDate('14:77', 'H:i')); # false
var_dump(validateDate(14, 'H')); # true
var_dump(validateDate('14', 'H')); # true
var_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true
# or
var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # true
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true
# or
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true
var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # false
# ...
function($) {
$.fn.extend( {
limiter: function(limit, elem) {
$(this).on("keyup focus", function() {
setCount(this, elem);
});
function setCount(src, elem) {
var chars = src.value.length;
if (chars > limit) {
src.value = src.value.substr(0, limit);
chars = limit;
}
elem.html( limit - chars );
}
setCount($(this)[0], elem);
}
});
})(jQuery);
$.preloadImages = function() {
for(var i = 0; i<arguments.length; i++) {
$("<img />").attr("src", arguments[i]);
}
}
$(document).ready(function() {
$.preloadImages("hoverimage1.jpg","hoverimage2.jpg");
});
$(function() {
$.fn.sortList = function() {
var mylist = $(this);
var listitems = $('li', mylist).get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : 1;
});
$.each(listitems, function(i, itm) {
mylist.append(itm);
});
}
$("ul#demoOne").sortList();
});
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>TEST</title>
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<style>
#foo {
width: 300px;
padding: 10px;
height: 100px;
border: 1px solid black;
overflow: auto;
position: relative;
}
p
{
border: 1px solid red;
margin: 10px;
}
</style>
</head>
<body>
<input type="button" value="id1" />
<input type="button" value="id2" />
<input type="button" value="id3" />
<div id="foo">
<p id="id1">Toto je první odstavec. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p id="id2">Toto je druhý odstavec. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p id="id3">Treti. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<!-- několik dalších odstavců -->
</div>
<script>
var $foo = jQuery('#foo');
$('input').click(function() {
var lastParagraphPosition = jQuery('#' + $(this).attr("value") ).position();
var scrollPosition = $foo.scrollTop() + lastParagraphPosition.top;
jQuery('#foo').scrollTop(scrollPosition);
});
</script>
</body>
</html>
jQuery.fn.scrollTo = function(elem) {
$(this).scrollTop($(this).scrollTop() - $(this).offset().top + $(elem).offset().top);
return this;
};
function makeSafe(thisText, allowSpace){
var w = "!@#$%^&*()+=[]\\\';,./{}|\":<>?";
var s = 'abcdefghijklmnopqrstuvwxyz0123456789-';
var x = new Array('àáâãäå', 'çč', 'èéêëě','š','ř','ž','á', 'ìíîï', 'ñ', 'ðóòôõöø', 'ùúûüů', 'ýÿ',' ','.');
var r = new Array('a', 'c', 'e','s','r','z','a', 'i', 'n', 'o', 'u', 'y','-','-');
if(allowSpace){
s = s + ' ';
}
thisText = thisText.toLowerCase();
var newText = new Array();
for (i = 0; i < thisText.length; i++){
thisChar = thisText.charAt(i);
if(w.indexOf(thisChar) == -1){
if(s.match(''+thisChar+'')){
newText[i] = thisChar;
}else{
for (j = 0; j < x.length; j++){
if(x[j].match(thisChar)){
newText[i] = r[j];
}
}
}
}
}
return newText.join('');
}