wikipoints/ground/classes/Helper.php

71 lines
2.1 KiB
PHP

<?php
class Helper
{
public static function boolval($var)
{
return (bool) $var;
}
/**
* TYPES: 'clear','html','js'
* @param type $string
* @param type $replaceType
* @return type
*/
public static function replaceLinks($string, $replaceType = 'clear')
{
preg_match_all("((\[#(\d+)[>\|](.*?)\])|(\[#(\d+)\]))", $string, $links);
$links = isset($links[0]) ? $links[0] : FALSE;
if ($links) {
foreach ($links as $link) {
$idAndLink = mb_substr($link, 2, -1);
$delimiter = strpos($idAndLink, '|') === FALSE ? '>' : '|';
$idAndLink = explode($delimiter, $idAndLink);
$linkId = $idAndLink[0];
$linkName = isset($idAndLink[1]) ? $idAndLink[1] : '';
if ($linkId) {
$linkPoint = Point::model()->getByPK($linkId);
if ($linkPoint && $linkPoint->id) {
$linkTitle = str_replace('"', 'ʺ', $linkPoint->name);
switch ($replaceType) {
case 'html':
$linkName = $linkName ? $linkName : '<span class="glyphicon glyphicon-link"></span>';
$string = str_replace($link, '<a href="/page/point/id/' . $linkId . '" title="' . $linkTitle . '">' . $linkName . '</a>', $string);
break;
case 'js':
$linkName = $linkName ? $linkName : '<span class="glyphicon glyphicon-link"></span>';
$string = str_replace($link, '<a href="/?point=' . $linkId . '" title="' . $linkTitle . '">' . $linkName . '</a>', $string);
break;
default:
$string = str_replace($link, $linkName, $string);
break;
}
}
}
}
}
return $string;
}
public static function buildOG($point)
{
$og = array();
$og['title'] = str_replace('"', 'ʺ', $point->name) . ' [wikipoints.ru]';
$point->description = str_replace('"', 'ʺ', $point->description);
$og['description'] = mb_substr($point->description, 0, 97, 'UTF-8') . '...';
$og['site_name'] = 'WikiPoints: Лучшие выходные - это выходные в путешествии!';
$og['type'] = 'website';
$og['url'] = 'http://wikipoints.ru/?point=' . $point->id;
$og['image'] = $point->getImg('middle');
return $og;
}
}