PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/sourcecoast/articleContent.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 474 lines | 391 code | 62 blank | 21 comment | 146 complexity | 4fd9ead514021d322be1a33e06423c6c MD5 | raw file
  1. <?php
  2. /**
  3. * @package JLinked
  4. * @copyright (C) 2011-2013 by Source Coast - All rights reserved
  5. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
  6. */
  7. // Check to ensure this file is included in Joomla!
  8. defined('_JEXEC') or die('Restricted access');
  9. jimport('sourcecoast.utilities');
  10. define('SC_TYPE_ALL', '0');
  11. define('SC_TYPE_INCLUDE', '1');
  12. define('SC_TYPE_EXCLUDE', '2');
  13. define('SC_NO', '0');
  14. define('SC_YES', '1');
  15. define('SC_VIEW_NONE', "0");
  16. define('SC_VIEW_TOP', "1");
  17. define('SC_VIEW_BOTTOM', "2");
  18. define('SC_VIEW_BOTH', "3");
  19. define('SC_INTRO_CHARS', '1');
  20. define('SC_INTRO_WORDS', '2');
  21. class SCArticleContent
  22. {
  23. static function isArticleView($view)
  24. {
  25. return ($view == 'article' || $view == 'item');
  26. }
  27. static function getSocialItemViewPosition($article, $view, $showInArticleView, $showInFrontpageView, $showInCategoryView, $showInSectionView)
  28. {
  29. $returnValue = "0";
  30. if ($view == 'article' && $article->id != null)
  31. $returnValue = $showInArticleView;
  32. else if ($view == 'frontpage' || $view == 'featured')
  33. $returnValue = $showInFrontpageView;
  34. else if ($view == 'category' && $article->catid != null)
  35. $returnValue = $showInCategoryView;
  36. return $returnValue;
  37. }
  38. static function getSocialK2ItemViewPosition($article, $view, $layout, $task, $showInItemView, $showInTagView, $showInCategoryView, $showInUserpageView, $showInLatestView)
  39. {
  40. $returnValue = "0";
  41. if ($view == 'item' && $article->id != null)
  42. $returnValue = $showInItemView;
  43. else if ($view == 'itemlist')
  44. {
  45. if (SCArticleContent::_isK2Layout($layout, $task, 'category')
  46. || SCArticleContent::_isK2Layout($layout, $task, 'search')
  47. || SCArticleContent::_isK2Layout($layout, $task, 'date')
  48. )
  49. $returnValue = $showInCategoryView;
  50. else if (SCArticleContent::_isK2Layout($layout, $task, 'generic') || SCArticleContent::_isK2Layout($layout, $task, 'tag'))
  51. $returnValue = $showInTagView;
  52. else if (SCArticleContent::_isK2Layout($layout, $task, 'user') && JRequest::getInt('id', 0))
  53. $returnValue = $showInUserpageView;
  54. }
  55. else if ($view == 'latest')
  56. $returnValue = $showInLatestView;
  57. return $returnValue;
  58. }
  59. private static function _isK2Layout($layout, $task, $targetLayout)
  60. {
  61. return ($layout == $targetLayout || $task == $targetLayout);
  62. }
  63. static function showSocialItemInArticle($article, $articleIncludeIds, $articleExcludeIds, $catIncludeType, $catIds, $sectIncludeType, $sectIds)
  64. {
  65. //Show in Article
  66. $includeArticles = explode(",", $articleIncludeIds);
  67. $excludeArticles = explode(",", $articleExcludeIds);
  68. //Specific Article is included or excluded, then show or don't show it.
  69. if ($includeArticles != null && in_array($article->id, $includeArticles))
  70. return true;
  71. else if ($excludeArticles != null && in_array($article->id, $excludeArticles))
  72. return false;
  73. //Show in Category
  74. $categories = unserialize($catIds);
  75. $inCategoryArray = $categories != null && in_array($article->catid, $categories);
  76. if ($catIncludeType == SC_TYPE_INCLUDE)
  77. {
  78. if ($inCategoryArray)
  79. return true;
  80. else
  81. return false;
  82. }
  83. else if ($catIncludeType == SC_TYPE_EXCLUDE)
  84. {
  85. if ($inCategoryArray)
  86. return false;
  87. else
  88. return true;
  89. }
  90. return true;
  91. }
  92. static function showSocialItemInK2Item($article, $articleIncludeIds, $articleExcludeIds, $catIncludeType, $catIds)
  93. {
  94. //Show in Article
  95. $includeArticles = explode(",", $articleIncludeIds);
  96. $excludeArticles = explode(",", $articleExcludeIds);
  97. //Specific Article is included or excluded, then show or don't show it.
  98. if ($includeArticles != null && in_array($article->id, $includeArticles))
  99. return true;
  100. else if ($excludeArticles != null && in_array($article->id, $excludeArticles))
  101. return false;
  102. //Show in Category
  103. $categories = unserialize($catIds);
  104. $inCategoryArray = $categories != null && in_array($article->catid, $categories);
  105. if ($catIncludeType == SC_TYPE_INCLUDE)
  106. {
  107. if ($inCategoryArray)
  108. return true;
  109. else
  110. return false;
  111. }
  112. else if ($catIncludeType == SC_TYPE_EXCLUDE)
  113. {
  114. if ($inCategoryArray)
  115. return false;
  116. else
  117. return true;
  118. }
  119. return true;
  120. }
  121. static function getCurrentURL($article, $isJoomla)
  122. {
  123. if ($isJoomla)
  124. return SCArticleContent::_getCurrentArticleURL($article);
  125. else
  126. return SCArticleContent::_getCurrentItemURL($article);
  127. }
  128. private static function _getCurrentArticleURL($article)
  129. {
  130. require_once(JPATH_SITE . '/components/com_content/helpers/route.php');
  131. if (isset($article->catslug))
  132. $url = ContentHelperRoute::getArticleRoute($article->slug, $article->catslug);
  133. else if (isset($article->catid))
  134. $url = ContentHelperRoute::getArticleRoute($article->slug, $article->catid);
  135. else
  136. $url = ContentHelperRoute::getArticleRoute($article->slug);
  137. $url = SCArticleContent::_getCompleteURL($url);
  138. return $url;
  139. }
  140. private static function _getCurrentItemURL($article)
  141. {
  142. require_once(JPATH_SITE . '/components/com_k2/helpers/route.php');
  143. $url = K2HelperRoute::getItemRoute($article->id . ":" . urlencode($article->alias), $article->catid);
  144. $url = SCArticleContent::_getCompleteURL($url);
  145. return $url;
  146. }
  147. private static function _getCompleteURL($url)
  148. {
  149. $url = JRoute::_($url, true);
  150. $jUri = JURI::getInstance();
  151. $url = rtrim($jUri->toString(array('scheme', 'host' ,'port')), '/') . $url;
  152. return $url;
  153. }
  154. private static function _prependToIntrotext(& $article, $fbText)
  155. {
  156. if (isset($article->text))
  157. $article->text = $fbText . $article->text;
  158. if (isset($article->introtext))
  159. $article->introtext = $fbText . $article->introtext;
  160. }
  161. private static function _prependToFulltext(& $article, $fbText)
  162. {
  163. if (isset($article->text))
  164. SCArticleContent::_prependAfterSplitter($article->text, $fbText);
  165. if (isset($article->fulltext))
  166. SCArticleContent::_prependAfterSplitter($article->fulltext, $fbText);
  167. }
  168. private static function _appendToIntrotext(& $article, $fbText)
  169. {
  170. if (isset($article->text))
  171. $article->text = $article->text . $fbText;
  172. else if (isset($article->introtext))
  173. $article->introtext = $article->introtext . $fbText;
  174. }
  175. private static function _appendToFulltext(& $article, $fbText)
  176. {
  177. if (isset($article->text))
  178. $article->text = $article->text . $fbText;
  179. else if (isset($article->fulltext))
  180. $article->fulltext = $article->fulltext . $fbText;
  181. }
  182. private static function _prependAfterSplitter(& $text, $fbText)
  183. {
  184. $articleText = str_replace('{K2Splitter}', '', $text, $count);
  185. $text = $fbText . $articleText;
  186. if ($count)
  187. $text = '{K2Splitter}' . $text;
  188. }
  189. private static function _appendBeforeSplitter(& $text, $fbText)
  190. {
  191. $articleText = str_replace('{K2Splitter}', '', $text, $count);
  192. $text = $articleText . $fbText;
  193. if ($count)
  194. $text .= '{K2Splitter}';
  195. }
  196. static function addClassToFBText($fbText, $className)
  197. {
  198. $newFbText = str_replace('scsocialbuttons', 'scsocialbuttons '. $className, $fbText);
  199. return $newFbText;
  200. }
  201. static function addTextToArticle(& $article, $fbText, $showTextPosition)
  202. {
  203. $hasFullText = isset($article->fulltext) && $article->fulltext != "";
  204. $introtextStartsWithSplitter = isset($article->introtext) && strpos($article->introtext, '{K2Splitter}') === 0;
  205. $textStartsWithSplitter = isset($article->text) && strpos($article->text, '{K2Splitter}') === 0;
  206. $hasIntroText = isset($article->introtext) && $article->introtext != "";
  207. if ($textStartsWithSplitter || $introtextStartsWithSplitter)
  208. $hasIntroText = false;
  209. $topText = SCArticleContent::addClassToFBText($fbText, "top");
  210. $bottomText = SCArticleContent::addClassToFBText($fbText, "bottom");
  211. if ($showTextPosition == SC_VIEW_TOP)
  212. {
  213. if (!$hasIntroText && $hasFullText)
  214. {
  215. if (isset($article->text))
  216. SCArticleContent::_prependAfterSplitter($article->text, $topText);
  217. if (isset($article->fulltext))
  218. SCArticleContent::_prependAfterSplitter($article->fulltext, $topText);
  219. }
  220. else
  221. {
  222. SCArticleContent::_prependToIntrotext($article, $topText);
  223. }
  224. }
  225. else if ($showTextPosition == SC_VIEW_BOTH)
  226. {
  227. //If introtext is present, we have to be careful of where to put the bottom item, because of K2Splitter
  228. if ($hasIntroText)
  229. {
  230. if ($hasFullText)
  231. {
  232. //If fulltext is present, it means there's already something after fulltext, so safe to
  233. //just add at the bottom of text.
  234. SCArticleContent::_prependToIntrotext($article, $topText);
  235. SCArticleContent::_appendToFulltext($article, $bottomText);
  236. }
  237. else
  238. {
  239. //If full text is not present, then we must add the bottom portion before K2Splitter
  240. SCArticleContent::_prependToIntrotext($article, $topText);
  241. if (isset($article->text))
  242. SCArticleContent::_appendBeforeSplitter($article->text, $bottomText);
  243. if (isset($article->introtext))
  244. SCArticleContent::_appendBeforeSplitter($article->introtext, $bottomText);
  245. }
  246. }
  247. else if ($hasFullText)
  248. {
  249. //If fulltext is present, 1it means there's already something after fulltext, so safe to
  250. //just add at the bottom of text.
  251. SCArticleContent::_prependToFulltext($article, $topText);
  252. SCArticleContent::_appendToFulltext($article, $bottomText);
  253. }
  254. }
  255. else if ($showTextPosition == SC_VIEW_BOTTOM)
  256. {
  257. if ($hasFullText)
  258. {
  259. //If fulltext is present, it means there's already something after fulltext, so safe to
  260. //just add at the bottom of text.
  261. SCArticleContent::_appendToFulltext($article, $bottomText);
  262. }
  263. else if ($hasIntroText)
  264. {
  265. //If full text is not present, then we must add the bottom portion before K2Splitter
  266. if (isset($article->text))
  267. SCArticleContent::_appendBeforeSplitter($article->text, $bottomText);
  268. if (isset($article->introtext))
  269. SCArticleContent::_appendBeforeSplitter($article->introtext, $bottomText);
  270. }
  271. }
  272. }
  273. static function getFirstCategoryText($category, $numCharacters = 100, $socialGraphFirstText='1')
  274. {
  275. $categoryText = '';
  276. if(isset($category->description))
  277. $categoryText = SCArticleContent::getSelectedText($category->description, $socialGraphFirstText, $numCharacters);
  278. return $categoryText;
  279. }
  280. static function getFirstArticleText($article, $numCharacters = 100, $socialGraphFirstText='1')
  281. {
  282. $articleText = '';
  283. if(isset($article->introtext) && trim(strip_tags($article->introtext)) != "")
  284. {
  285. $articleText = $article->introtext;
  286. }
  287. else if(isset($article->text) && trim(strip_tags($article->text)) != "")
  288. {
  289. $articleText = $article->text;
  290. }
  291. else if(isset($article->fulltext) && trim(strip_tags($article->fulltext)) != "")
  292. {
  293. $articleText = $article->fulltext;
  294. }
  295. $articleText = SCArticleContent::getSelectedText($articleText, $socialGraphFirstText, $numCharacters);
  296. return $articleText;
  297. }
  298. static function getSelectedText($contentText, $socialGraphFirstText, $numCharacters)
  299. {
  300. $articleText = strip_tags($contentText);
  301. $articleText = preg_replace( '/\s+/', ' ', $articleText );
  302. $articleText = str_replace('{K2Splitter}', '', $articleText);
  303. SCSocialUtilities::stripSystemTags($articleText, 'JFBC');
  304. SCSocialUtilities::stripSystemTags($articleText, 'JLinked');
  305. SCSocialUtilities::stripSystemTags($articleText, 'SCOpenGraph');
  306. SCSocialUtilities::stripSystemTags($articleText, 'SCTwitterShare');
  307. SCSocialUtilities::stripSystemTags($articleText, 'SCGooglePlusOne');
  308. SCSocialUtilities::stripSystemTags($articleText, 'loadposition');
  309. $articleText = SCStringUtilities::trimNBSP($articleText);
  310. $addEllipsis = false;
  311. if($socialGraphFirstText == '1')
  312. {
  313. $addEllipsis = strlen($articleText) > $numCharacters;
  314. if(function_exists('mb_substr'))
  315. $articleText = mb_substr($articleText, 0, $numCharacters, 'UTF-8');
  316. else
  317. $articleText = substr($articleText, 0, $numCharacters);
  318. }
  319. else if($socialGraphFirstText == '2')
  320. {
  321. $parts = mb_split('\s+', $articleText);
  322. $selParts = array_slice($parts, 0, $numCharacters);
  323. $articleText = implode(" ", $selParts);
  324. $addEllipsis = count($parts) > $numCharacters;
  325. }
  326. else
  327. $articleText = '';
  328. if($addEllipsis)
  329. $articleText .= '...';
  330. return $articleText;
  331. }
  332. static function getImageFromCategory($article)
  333. {
  334. $image = NULL;
  335. $fullImagePath = '';
  336. if(isset($article->catid))
  337. {
  338. $fullImagePath = SCArticleContent::getCategoryImage($article->catid);
  339. }
  340. return $fullImagePath;
  341. }
  342. static function getCategoryImage($catid)
  343. {
  344. $content = JCategories::getInstance('content');
  345. $category = $content->get($catid);
  346. $image = $category->getParams()->get('image');
  347. $fullImagePath = '';
  348. if($image)
  349. {
  350. $fullImagePath = SCArticleContent::_getImageLink($image);
  351. }
  352. return $fullImagePath;
  353. }
  354. static function getFirstImage($article)
  355. {
  356. $fullImagePath = '';
  357. if (isset($article->text))
  358. $articleText = $article->text;
  359. else
  360. $articleText = $article->introtext;
  361. if (preg_match_all('/<img [^>]*src=["|\']([^"|\']+)/i', $articleText, $matches))
  362. {
  363. $fullImagePath = SCArticleContent::_getImageLink($matches[1][0]);
  364. }
  365. return $fullImagePath;
  366. }
  367. private static function _getImageLink($path)
  368. {
  369. $juri = JURI::getInstance();
  370. $basePath = str_replace(array($juri->getScheme() . "://", $juri->getHost()), "", $juri->base());
  371. if (strpos($path, $basePath) === 0)
  372. {
  373. $path = substr($path, strlen($basePath));
  374. $path = $juri->base() . $path;
  375. }
  376. else if (strpos($path, "http") !== 0)
  377. $path = $juri->base() . $path;
  378. return $path;
  379. }
  380. static function getK2MainImage($article)
  381. {
  382. $imageName = 'media/k2/items/cache/'.md5('Image'.$article->id).'_XL.jpg';
  383. jimport('joomla.filesystem.file');
  384. if(JFile::exists(JPATH_SITE.'/'. $imageName))
  385. return JURI::base() . $imageName;
  386. else
  387. return '';
  388. }
  389. static function getK2CategoryImage($catid)
  390. {
  391. $category = SCArticleContent::getK2Category($catid);
  392. $image = $category->image;
  393. $imageName = 'media/k2/categories/'.$image;
  394. jimport('joomla.filesystem.file');
  395. if(JFile::exists(JPATH_SITE.'/'. $imageName))
  396. return JURI::base() . $imageName;
  397. else
  398. return '';
  399. }
  400. static function getCurrentK2CategoryId()
  401. {
  402. $catid = JRequest::getInt('id');
  403. return $catid;
  404. }
  405. static function getK2Category($catid)
  406. {
  407. JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_k2/tables');
  408. $category = JTable::getInstance('K2Category', 'Table');
  409. $category->load($catid);
  410. return $category;
  411. }
  412. }