Skip to content

Instantly share code, notes, and snippets.

@vojty
Created August 19, 2014 14:43
Show Gist options
  • Save vojty/a95cd29db957e46f0585 to your computer and use it in GitHub Desktop.
Save vojty/a95cd29db957e46f0585 to your computer and use it in GitHub Desktop.
<?php
namespace Avdily;
use Nette;
class TemplateHelpers extends Nette\Object {
/** @var string */
private $wwwDir;
public function __construct($wwwDir) {
$this->wwwDir = $wwwDir;
}
/**
* For CSS & JS file versioning
* @param type $file
* @return type
*/
public function mtime($file) {
return $file . '?v=' . filemtime($this->wwwDir . $file);
}
public static function nbspReplacer($string) {
$sp = "&nbsp";
$after = "a, i, o, u, k, s, v, z, A, I, O, U, K, S, V, Z, by, co, či, do, je, ke, ku, na, no, od, po, se, ta, to, ve, za, ze, že, aby, což, když, kde, kdy, který, která, které, nad, pod, před, při, Co, Či, Do, Je, Ke, Ku, Na, No, Od, Po, Se, Ta, To, Ve, Za, Ze, Že, Aby, Což, Když, Kde, Kdy, Který, Která, Které, Nad, Pod, Před, Při";
$before = "m, g, l, q, t, w, J, %, ks, mm, cm, km, mg, dkg, kg, ml, cl, dl, hl, m3, km3, mm2, cm2, dm2, m2, km2, ha, Pa, hPa, kPa, MPa, bar, mbar, nbar, atm, psi, kW, MW, HP, m/s, km/h, m/min, MPH, cal, Wh, kWh, kp·m, °C, °F, kB, dB, MB, GB, kHz, MHz, Kč";
$after .= $after . ", Bc., BcA., ing., Ing., Ing.arch., MUDr., MVDr., MgA., Mgr., JUDr., PhDr., RNDr., PharmDr., ThLic., ThDr., prof., doc., PaedDr., Dr., PhMr.";
$before.= $before . ", Ph.D, CSc., Th.D., DrSc., dr. h. c., DiS.";
$afterAr = explode(", ", $after);
$beforeAr = explode(", ", $before);
$work = explode(" ", $string);
$out = "";
for ($i = 0; $i < (count($work) - 1); $i++) {
if (in_array($work[$i], $afterAr) || in_array($work[$i + 1], $beforeAr)) {
$out .= $work[$i] . $sp;
} else {
$out .= $work[$i] . " ";
}
}
$out .= $work[count($work) - 1];
return $out;
}
/**
* Smarty {mailto} function plugin
*
* @link http://www.smarty.net/manual/en/language.function.mailto.php {mailto}
* (Smarty online manual)
* @version 1.2
* @author Monte Ohrt <monte at ohrt dot com>
* @author credits to Jason Sweat (added cc, bcc and subject functionality)
*/
public static function email($email, $encoding = 'none', $text = NULL) {
$address = $email;
$address_text = (is_null($text) ? $address : $text);
switch ($encoding) {
// FIXME: (rodneyrehm) document.write() excues me what? 1998 has passed!
case 'javascript':
$string = 'document.write(\'<a href="mailto:' . $address . '">' . $address_text . '</a>\');';
$js_encode = '';
for ($x = 0, $_length = strlen($string); $x < $_length; $x++) {
$js_encode .= '%' . bin2hex($string[$x]);
}
return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
break;
/* smarty javascript charcode */
case 'javascript_charcode':
$string = '<a href="mailto:' . $address . '">' . $address_text . '</a>';
for ($x = 0, $y = strlen($string); $x < $y; $x++) {
$ord[] = ord($string[$x]);
}
$_ret = "<script type=\"text/javascript\" language=\"javascript\">\n"
. "{document.write(String.fromCharCode("
. implode(',', $ord)
. "))"
. "}\n"
. "</script>\n";
return $_ret;
break;
/* smarty hex encoding */
case 'hex':
preg_match('!^(.*)(\?.*)$!', $address, $match);
if (!empty($match[2])) {
trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.", E_USER_WARNING);
return;
}
$address_encode = '';
for ($x = 0, $_length = strlen($address); $x < $_length; $x++) {
if (preg_match('!\w!' . 'u', $address[$x])) {
$address_encode .= '%' . bin2hex($address[$x]);
} else {
$address_encode .= $address[$x];
}
}
$mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
return '<a href="' . $mailto . $address_encode . '">' . $address_text . '</a>';
break;
/* drupal protection */
case 'drupal':
$address = str_replace('@', '[at]', $address);
return '<a href="mailto:' . $address . '">' . $address . '</a>';
break;
/* texy protection */
case 'texy':
$address = str_replace('@', '<!-- ANTISPAM -->&#64;<!-- /ANTISPAM -->', $address);
return '<a href="mailto:' . $address . '">' . $address . '</a>';
break;
/* no encoding */
default:
return '<a href="mailto:' . $address . '">' . $address_text . '</a>';
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment