Переделал замену ссылок в wiki-разметке

This commit is contained in:
Krivchikov Dmitry 2015-08-29 18:50:05 +03:00
parent 1348709a62
commit f50413b1d4
3 changed files with 28 additions and 9 deletions

View File

@ -8,9 +8,15 @@ class Helper
return (bool) $var;
}
public static function replaceLinks($string, $linkTemplate = '/page/point/id/')
/**
* TYPES: 'clear','html','js'
* @param type $string
* @param type $replaceType
* @return type
*/
public static function replaceLinks($string, $replaceType = 'clear')
{
preg_match_all("(\[#(\d+)[>\|](.*?)\])", $string, $links);
preg_match_all("((\[#(\d+)[>\|](.*?)\])|(\[#(\d+)\]))", $string, $links);
$links = isset($links[0]) ? $links[0] : FALSE;
if ($links) {
foreach ($links as $link) {
@ -19,13 +25,26 @@ class Helper
$idAndLink = explode($delimiter, $idAndLink);
$linkId = $idAndLink[0];
$linkName = isset($idAndLink[1]) ? $idAndLink[1] : '<span class="glyphicon glyphicon-link"></span>';
$linkName = isset($idAndLink[1]) ? $idAndLink[1] : '';
if ($linkId) {
$linkPoint = Point::model()->getByPK($linkId);
if ($linkPoint->id) {
$linkPoint->name = str_replace('"', 'ʺ', $linkPoint->name);
$string = str_replace($link, '<a href="' . $linkTemplate . $linkId . '" title="' . $linkPoint->name . '">' . $linkName . '</a>', $string);
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;
}
}
}
}

View File

@ -53,7 +53,7 @@ class JsonController extends Controller
$point = Point::model()->getByPK($id);
$point->descriptionHtml = nl2br($point->description);
$point->descriptionHtml = Helper::replaceLinks($point->descriptionHtml, '/?point=');
$point->descriptionHtml = Helper::replaceLinks($point->descriptionHtml, 'js');
$point->name = htmlspecialchars($point->name);
$point->moderate = $point->moderate ? 'show' : 'hide';
$imgs = $point->photos;

View File

@ -124,7 +124,7 @@ class PageController extends Controller
$point->categoryIcon = $categories[$point->categoryId]['icon'];
$point->categoryName = $categories[$point->categoryId]['name'];
$point->descriptionHtml = Helper::replaceLinks($point->descriptionHtml);
$point->descriptionHtml = Helper::replaceLinks($point->descriptionHtml, 'html');
// preg_match_all("/.*?(([.?!](?:\s|$))|$)/s", $point->descriptionHtml, $items);
preg_match_all("/.*?((([^.?!\s]{3,}?)[.?!](?:\s|$))|$)/s", $point->descriptionHtml, $items);
$point->inFavorites = Favorite::model()->checkIsMy($pointId);
@ -220,7 +220,7 @@ class PageController extends Controller
$categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));
$point->descriptionHtml = Helper::replaceLinks($point->descriptionHtml);
$point->description = Helper::replaceLinks($point->description);
self::renderPartial('vkPoint.php', array(
'point' => $point,