Social.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ ezTFAoXlBd71qtHYnEoBr+6tIREoFKJbIzCry5puFak=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.8
  11. */
  12. /**
  13. * social network links display view helper class
  14. * displays links for a certain listing if the listing is specified, or general site links otherwise
  15. */
  16. namespace Ppb\View\Helper;
  17. use Cube\View\Helper\AbstractHelper,
  18. Cube\Controller\Front,
  19. Ppb\Db\Table\Row\Listing as ListingModel;
  20. class Social extends AbstractHelper
  21. {
  22. protected $_networks = array(
  23. 'Email' => array(
  24. 'img' => '/social/email.png',
  25. 'link' => '[EMAIL_FRIEND_URL]',
  26. 'target' => '_self',
  27. ),
  28. 'Facebook' => array(
  29. 'img' => '/social/facebook.png',
  30. 'link' => 'http://www.facebook.com/sharer.php?u=[URL]',
  31. ),
  32. 'Twitter' => array(
  33. 'img' => '/social/twitter.png',
  34. 'link' => 'http://twitter.com/intent/tweet?text=[TEXT]&amp;url=[URL]',
  35. ),
  36. 'GooglePlus' => array(
  37. 'img' => '/social/googleplus.png',
  38. 'link' => 'https://plus.google.com/share?url=[URL]',
  39. ),
  40. 'Pinterest' => array(
  41. 'img' => '/social/pinterest.png',
  42. 'link' => 'http://pinterest.com/pin/create/button/?url=[URL]&amp;media=[IMG]&amp;description=[TEXT]',
  43. ),
  44. 'RSS' => array(
  45. 'img' => '/social/rss.png',
  46. 'link' => '[RSS_URL]',
  47. 'target' => '_self',
  48. ),
  49. );
  50. /**
  51. *
  52. * listing model
  53. *
  54. * @var \Ppb\Db\Table\Row\Listing
  55. */
  56. protected $_listing;
  57. /**
  58. *
  59. * set social networks array
  60. *
  61. * @param array $networks
  62. *
  63. * @return $this
  64. */
  65. public function setNetworks(array $networks)
  66. {
  67. $this->_networks = $networks;
  68. return $this;
  69. }
  70. /**
  71. *
  72. * get social networks array
  73. *
  74. * @return array
  75. */
  76. public function getNetworks()
  77. {
  78. return $this->_networks;
  79. }
  80. /**
  81. *
  82. * add a network to the array
  83. *
  84. * @param string $name
  85. * @param array $network
  86. *
  87. * @return $this
  88. */
  89. public function addNetwork($name, $network)
  90. {
  91. $this->_networks[$name] = $network;
  92. return $this;
  93. }
  94. /**
  95. *
  96. * remove a network from the networks array
  97. *
  98. * @param string $name
  99. *
  100. * @return $this
  101. */
  102. public function removeNetwork($name)
  103. {
  104. if (array_key_exists($name, $this->_networks)) {
  105. unset($this->_networks[$name]);
  106. }
  107. return $this;
  108. }
  109. /**
  110. *
  111. * get listing model
  112. *
  113. * @return \Ppb\Db\Table\Row\Listing
  114. * @throws \InvalidArgumentException
  115. */
  116. public function getListing()
  117. {
  118. return $this->_listing;
  119. }
  120. /**
  121. *
  122. * set listing model
  123. *
  124. * @param \Ppb\Db\Table\Row\Listing $listing
  125. *
  126. * @throws \InvalidArgumentException
  127. * @return $this
  128. */
  129. public function setListing(ListingModel $listing)
  130. {
  131. if (!$listing instanceof ListingModel) {
  132. throw new \InvalidArgumentException("The advert model must be an instance of \Ppb\Db\Table\Row\Listing");
  133. }
  134. $this->_listing = $listing;
  135. return $this;
  136. }
  137. /**
  138. *
  139. * clear listing object
  140. *
  141. * @return $this
  142. */
  143. public function clearListing()
  144. {
  145. $this->_listing = null;
  146. return $this;
  147. }
  148. /**
  149. *
  150. * display social network links for the selected link or listing
  151. *
  152. * @param string $template
  153. *
  154. * @return string
  155. */
  156. public function display($template = null)
  157. {
  158. $links = $this->getLinks();
  159. if ($template === null) {
  160. $output = array();
  161. foreach ($links as $link) {
  162. $output[] = '<div class="social-button">
  163. <a href="' . $link['href'] . '" target="' . $link['target'] . '" rel="nofollow"><img src="' . $link['img'] . '" alt="' . $link['name'] . '"></a>
  164. </div>';
  165. }
  166. return implode(' ', $output);
  167. }
  168. else {
  169. foreach ($links as $link) {
  170. $template = str_replace('%' . $link['name'] . '%', $link['href'], $template);
  171. }
  172. }
  173. return $template;
  174. }
  175. /**
  176. *
  177. * generate social links
  178. *
  179. * @return array
  180. */
  181. public function getLinks()
  182. {
  183. $listing = $this->getListing();
  184. $view = $this->getView();
  185. $settings = Front::getInstance()->getBootstrap()->getResource('settings');
  186. $sitePath = $settings['site_path'];
  187. $imgBaseUrl = $view->baseUrl . \Ppb\Utility::URI_DELIMITER . \Ppb\Utility::getFolder('img');
  188. $uploadsPath = $sitePath . \Ppb\Utility::URI_DELIMITER . \Ppb\Utility::getFolder('uploads');
  189. $output = array();
  190. if ($listing instanceof ListingModel) {
  191. $url = urlencode($sitePath . $this->getView()->url($listing->link(), null, false, null, false));
  192. $text = urlencode($listing->getData('name'));
  193. $img = urlencode($listing->getMainImage(true));
  194. $desc = urlencode(substr(strip_tags($listing->getData('description')), 0, 150));
  195. $emailFriendUrl = $view->url(array('module' => 'listings', 'controller' => 'listing', 'action' => 'email-friend', 'id' => $listing->getData('id')));
  196. $rssUrl = null;
  197. }
  198. else {
  199. $url = urlencode($sitePath);
  200. $text = urlencode($settings['sitename']);
  201. $img = urlencode($uploadsPath . \Ppb\Utility::URI_DELIMITER . $settings['site_logo_path']);
  202. $desc = urlencode($settings['meta_description']);
  203. $emailFriendUrl = null;
  204. $rssUrl = $view->url(array('module' => 'app', 'controller' => 'rss', 'action' => 'index'));
  205. }
  206. foreach ($this->_networks as $name => $network) {
  207. $href = str_replace(
  208. array('[URL]', '[TEXT]', '[IMG]', '[DESC]', '[RSS_URL]', '[EMAIL_FRIEND_URL]'),
  209. array($url, $text, $img, $desc, $rssUrl, $emailFriendUrl),
  210. $network['link']);
  211. $target = (isset($network['target'])) ? $network['target'] : '_blank';
  212. if ($listing && $name == 'RSS') {
  213. }
  214. else if (!$listing && $name == 'Email') {
  215. }
  216. else {
  217. $output[] = array(
  218. 'name' => $name,
  219. 'href' => $href,
  220. 'target' => $target,
  221. 'img' => $imgBaseUrl . $network['img'],
  222. );
  223. }
  224. }
  225. return $output;
  226. }
  227. /**
  228. *
  229. * main method, only returns object instance
  230. *
  231. * @param \Ppb\Db\Table\Row\Listing $listing
  232. *
  233. * @return $this
  234. */
  235. public function social(ListingModel $listing = null)
  236. {
  237. if ($listing !== null) {
  238. $this->setListing($listing);
  239. }
  240. return $this;
  241. }
  242. }