wikipoints/classes/Helper.php

158 lines
4.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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="/point/' . $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 replaceImgs($string, $replaceType = 'clear', $size = 'middle')
{
preg_match_all("(\[img[\|]#(\d+)\])", $string, $images);
$images = isset($images[0]) ? $images[0] : FALSE;
if ($images) {
foreach ($images as $img) {
$imgAndId = mb_substr($img, 2, -1);
$imgAndId = explode('#', $imgAndId);
$imgId = (int)$imgAndId[1];
$imgObj = ArticlePhotos::model()->getByPK($imgId);
if ($imgObj->name) {
switch ($replaceType) {
case 'html':
$imgObj->caption = $linkTitle = str_replace('"', 'ʺ', $imgObj->caption);
$imgTag = '<div style="text-align: center;"><img src="/photos/articles/'.$size.'/' . $imgObj->name . '" title="' . $imgObj->caption . '" class="imageInArticle" /></div>';
$string = str_replace($img, $imgTag, $string);
break;
default:
$imgTag = '';
$string = str_replace($img, $imgTag, $string);
break;
}
} else {
switch ($replaceType) {
case 'html':
$imgTag = '<div style="text-align: center;"><img src="/img/noPhoto.png" title="Фотография потерялась" class="imageInArticle" /></div>';
$string = str_replace($img, $imgTag, $string);
break;
default:
$imgTag = '';
$string = str_replace($img, $imgTag, $string);
break;
}
}
}
}
return $string;
}
static function replaceUrls($string)
{
$pr = '((https{0,1}:\/\/[\da-z\.-]+\.[a-z]{2,6})([\/\w\?=&#%+,-]|(\.[^\s]))*\/?)';
$rep = '<a href="/go/away/?url=$0" title="Ссылка откроется в новом окне" target="_blank">$0</a>';
return preg_replace($pr, $rep, $string);
}
public static function getFirstImg($string)
{
preg_match_all("(\[img[\|]#(\d+)\])", $string, $images);
$images = isset($images[0]) ? $images[0] : FALSE;
$img = isset($images[0]) ? $images[0] : FALSE;
if ($img) {
$imgId = (int) mb_substr($img, 6, -1);
$imgObj = ArticlePhotos::model()->getByPK($imgId);
return $imgObj->name ? (int)$imgObj->id : 0;
}
return 0;
}
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, 99, 'UTF-8') . '…';
$og['site_name'] = App::$defaultTitle;
$og['type'] = 'website';
$og['url'] = 'http://wikipoints.ru/?point=' . $point->id;
$og['image'] = $point->getImg('middle');
return $og;
}
public static function buildArticleOG($article)
{
$og = array();
$og['title'] = str_replace('"', 'ʺ', $article->title) . ' [wikipoints.ru]';
$article->text = Markdown::getClear(str_replace('"', 'ʺ', $article->text));
$og['description'] = mb_substr($article->text, 0, 99, 'UTF-8') . '…';
$og['site_name'] = App::$defaultTitle;
$og['type'] = 'website';
$og['url'] = 'http://wikipoints.ru/article/' . $article->id;
$og['image'] = $article->getImg('middle');
return $og;
}
/**
* Проверка на то, что это URL
* @param type $src
* @return type
*/
public static function isUrl($src)
{
// TODO: Переписать на что-то нормальное с регулярками
return substr(strtolower($src), 0, 4) == 'http';
}
}