diff --git a/ground/classes/Helper.php b/ground/classes/Helper.php index 691df62..7d4c896 100644 --- a/ground/classes/Helper.php +++ b/ground/classes/Helper.php @@ -123,4 +123,18 @@ class Helper 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, 97, 'UTF-8') . '...'; + $og['site_name'] = 'WikiPoints: Лучшие выходные - это выходные в путешествии!'; + $og['type'] = 'website'; + $og['url'] = 'http://wikipoints.ru/page/article/id/' . $article->id; + $og['image'] = $article->getImg('middle'); + + return $og; + } + } diff --git a/ground/classes/Markdown.php b/ground/classes/Markdown.php index c8bb025..5adff6b 100644 --- a/ground/classes/Markdown.php +++ b/ground/classes/Markdown.php @@ -60,4 +60,53 @@ class Markdown $rep = '$1'; return preg_replace($pr, $rep, $string); } + + static function getClear($string) + { + // Headers + for ($index = 3; $index > 0; $index--) { + + $pr = '/(^#{' . $index . '} .{1,})/m'; + + preg_match_all($pr, $string, $headers); + + if (isset($headers[0][0])) { + foreach ($headers[0] as $header) { + $hI = $index+1; + $string = str_replace($header, '', $string); + } + } + } + + // Bold & Italic + $pr = '(\*{1,3}(.+?)\*{1,3})'; + $rep = '$1'; + $string = preg_replace($pr, $rep, $string); + + // Uderline + $pr = '(\_{1}(.+?)\_{1})'; + $rep = '$1'; + $string = preg_replace($pr, $rep, $string); + + // Images + $pr = '(\[img[\|]#(\d+)\])'; + $rep = ' '; + $string = preg_replace($pr, $rep, $string); + + // \n & \r + $string = str_replace(array("\n","\r"), ' ', $string); + + // multi-space + $pr = '(\s{2,})'; + $rep = ' '; + $string = preg_replace($pr, $rep, $string); + + // URL + $pr = '((https{0,1}:\/\/[\da-z\.-]+\.[a-z]{2,6})[\/\w\?=&#%+\.,-]*\/?)'; + $rep = '$1'; + $string = preg_replace($pr, $rep, $string); + + return $string; + } + } diff --git a/ground/controllers/PageController.php b/ground/controllers/PageController.php index 0947849..edfaba1 100644 --- a/ground/controllers/PageController.php +++ b/ground/controllers/PageController.php @@ -199,7 +199,7 @@ class PageController extends Controller self::render('article.php', array( 'article' => $article, 'randomPoint' => Point::model()->getRandom(), -// 'og' => Helper::buildOG($point), + 'og' => Helper::buildArticleOG($article), // 'user' => App::$user, )); } else { diff --git a/ground/models/Article.php b/ground/models/Article.php index 4724cbf..9dde028 100644 --- a/ground/models/Article.php +++ b/ground/models/Article.php @@ -15,4 +15,20 @@ class Article extends Model return new self(); } + public function getImg($size = 'small') + { + $imgId = (int)$this->photoId; + if ($imgId) { + + $img = ArticlePhotos::model()->getByPK($imgId); + + if ($img) { + return '/photos/articles/' . $size . '/' . $img->name; + } + + } + + return '/img/logo.png'; + } + } diff --git a/ground/views/articles/index.php b/ground/views/articles/index.php index 8d2beb3..0cb2b58 100644 --- a/ground/views/articles/index.php +++ b/ground/views/articles/index.php @@ -16,15 +16,4 @@ -