PageRenderTime 40ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/pi.dreebbble.php

https://github.com/olivierbon/Dreebbble
PHP | 309 lines | 205 code | 40 blank | 64 comment | 38 complexity | e5833aa48dbfe61103f5b86afa502b9d MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin file for dreebbble.
  4. *
  5. * This file must be placed in the
  6. * /expressionengine/third_party/dreebbble folder in your ExpressionEngine installation.
  7. *
  8. * @package Dreebbble
  9. * @version 1.4
  10. * @author Olivier Bon <oli@builtwithmomentum.com>
  11. * @link http://olivierbon.com/projects/dreebbble
  12. * @copyright Copyright (c) 2010, Olivier Bon Studios
  13. * @license http://creativecommons.org/licenses/by-sa/3.0/ Creative Commons Attribution-Share Alike 3.0 Unported
  14. */
  15. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  16. $plugin_info = array('pi_name' => 'Dreebbble',
  17. 'pi_version' => '1.4',
  18. 'pi_author' => 'Olivier Bon',
  19. 'pi_author_url' => 'http://olivierbon.com',
  20. 'pi_description' => 'Retrieve latest shots from a dribbble player or a list of shots',
  21. 'pi_usage' => Dreebbble::usage()
  22. );
  23. /**
  24. * Dreebbble Class
  25. *
  26. * @package Dreebbble
  27. * @category Plugin
  28. * @author Olivier Bon
  29. * @copyright Copyright (c) 2010, Olivier Bon Studios
  30. * @link http://olivierbon.com/projects/dreebbble
  31. */
  32. define("ENDPOINT", "http://api.dribbble.com/");
  33. class Dreebbble {
  34. var $return_data;
  35. protected $player;
  36. protected $limit;
  37. protected $list;
  38. protected $image_size;
  39. private $EE;
  40. /**
  41. * Fetch latest shot for a given player
  42. **/
  43. public function get_latest()
  44. {
  45. $this->EE =& get_instance();
  46. $list = ( $this->EE->TMPL->fetch_param('list') );
  47. $image_size = ( $this->EE->TMPL->fetch_param('image_size') ) ? $this->EE->TMPL->fetch_param('image_size'): 'thumb';
  48. $limit = ( $this->EE->TMPL->fetch_param('limit') ) ? $this->EE->TMPL->fetch_param('limit'): '5';
  49. if($list)
  50. {
  51. $params = "shots/$list?per_page=$limit";
  52. }
  53. else
  54. {
  55. $player = $this->EE->TMPL->fetch_param('player');
  56. $params = "players/$player/shots?per_page=$limit";
  57. }
  58. if ( ! $items = $this->dribbble_json2php($params))
  59. {
  60. return false;
  61. }
  62. if (count($items) == 0)
  63. {
  64. return $this->EE->TMPL->no_results;
  65. }
  66. // ----------------------------------------
  67. // Loop through the shots
  68. // ----------------------------------------
  69. foreach($items->shots as $shot)
  70. {
  71. $tagdata = $this->EE->TMPL->tagdata;
  72. // Set shot attributes
  73. $id = $shot->id;
  74. $title = $shot->title;
  75. $url = $shot->url;
  76. $image_url = $shot->image_url;
  77. $image_teaser_url = $shot->image_teaser_url;
  78. $width = $shot->width;
  79. $height = $shot->height;
  80. $views_count = $shot->views_count;
  81. $likes_count = $shot->likes_count;
  82. $comments_count = $shot->comments_count;
  83. $rebounds_count = $shot->rebounds_count;
  84. // Set player attributes
  85. $player_id = $shot->player->id;
  86. $player_name = $shot->player->name;
  87. $player_url = $shot->player->url;
  88. $player_avatar_url = $shot->player->avatar_url;
  89. $player_location = $shot->player->location;
  90. // ----------------------------------------
  91. // Parse "single" variables
  92. // ----------------------------------------
  93. foreach ($this->EE->TMPL->var_single as $var)
  94. {
  95. // Parse shot id
  96. if ($var == 'id')
  97. {
  98. $tagdata = $this->EE->TMPL->swap_var_single($var, $id, $tagdata);
  99. }
  100. // Parse shot title
  101. if ($var == 'title')
  102. {
  103. $tagdata = $this->EE->TMPL->swap_var_single($var, $title, $tagdata);
  104. }
  105. // Parse shot url
  106. if ($var == 'url')
  107. {
  108. $tagdata = $this->EE->TMPL->swap_var_single($var, $url, $tagdata);
  109. }
  110. // Parse image url
  111. if ($var == 'image_url')
  112. {
  113. if($image_size == "original")
  114. {
  115. $tagdata = $this->EE->TMPL->swap_var_single($var, $image_url, $tagdata);
  116. }
  117. elseif($image_size == "thumb")
  118. {
  119. $tagdata = $this->EE->TMPL->swap_var_single($var, $image_teaser_url, $tagdata);
  120. }
  121. }
  122. // Parse image width
  123. if ($var == 'width')
  124. {
  125. $tagdata = $this->EE->TMPL->swap_var_single($var, $width, $tagdata);
  126. }
  127. // Parse image height
  128. if ($var == 'height')
  129. {
  130. $tagdata = $this->EE->TMPL->swap_var_single($var, $height, $tagdata);
  131. }
  132. // Parse image views_count
  133. if ($var == 'views_count')
  134. {
  135. $tagdata = $this->EE->TMPL->swap_var_single($var, $views_count, $tagdata);
  136. }
  137. // Parse image likes_count
  138. if ($var == 'likes_count')
  139. {
  140. $tagdata = $this->EE->TMPL->swap_var_single($var, $likes_count, $tagdata);
  141. }
  142. // Parse image comments_count
  143. if ($var == 'comments_count')
  144. {
  145. $tagdata = $this->EE->TMPL->swap_var_single($var, $comments_count, $tagdata);
  146. }
  147. // Parse image rebounds_count
  148. if ($var == 'rebounds_count')
  149. {
  150. $tagdata = $this->EE->TMPL->swap_var_single($var, $rebounds_count, $tagdata);
  151. }
  152. // Parse player_id
  153. if ($var == 'player_id')
  154. {
  155. $tagdata = $this->EE->TMPL->swap_var_single($var, $player_id, $tagdata);
  156. }
  157. // Parse player_name
  158. if ($var == 'player_name')
  159. {
  160. $tagdata = $this->EE->TMPL->swap_var_single($var, $player_name, $tagdata);
  161. }
  162. // Parse player_avatar_url
  163. if ($var == 'player_avatar_url')
  164. {
  165. $tagdata = $this->EE->TMPL->swap_var_single($var, $player_avatar_url, $tagdata);
  166. }
  167. // Parse player_location
  168. if ($var == 'player_location')
  169. {
  170. $tagdata = $this->EE->TMPL->swap_var_single($var, $player_location, $tagdata);
  171. }
  172. // Parse image url
  173. }
  174. // End parse single
  175. $this->return_data .= $tagdata;
  176. }
  177. //// End loop through the results
  178. return $this->return_data;
  179. }
  180. // End get_latest
  181. function dribbble_json2php($params)
  182. {
  183. $file = ENDPOINT.$params;
  184. $ch = curl_init($file);
  185. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  186. $contents = curl_exec($ch);
  187. if ($contents)
  188. {
  189. $items = json_decode($contents);
  190. return $items;
  191. }
  192. else
  193. {
  194. echo "Dreebbble requires the CURL library to be installed. Please check with your hosting provider";
  195. }
  196. }// End dribbble_json2php()
  197. // --------------------------------------------------------------------
  198. /**
  199. * Usage
  200. *
  201. * Plugin Usage
  202. *
  203. * @access public
  204. * @return string
  205. */
  206. function usage()
  207. {
  208. ob_start();
  209. ?>
  210. This plugin allows you to display the latest shots for a given player or from a chosen list.
  211. The tags works this way:
  212. :: Single Player:
  213. {exp:dreebbble:get_latest player="dribbble" limit="5"}
  214. {/exp:dreebbble:get_latest}
  215. :: List:
  216. {exp:dreebbble:get_latest list="everyone" limit="5"}
  217. {/exp:dreebbble:get_latest}
  218. Available Parameters so far:
  219. player: This may be a player id or username, e.g. '39' or 'dribbble'.
  220. limit: Use this to limit the amount of shots you would like to show.
  221. list: This enables you to retrieve one of the 3 available lists on dribbble. Use debuts, everyone or popular
  222. image_size: By default the plugin will return the small version of the shot. However, you can set image_size to "original" to return the main image.
  223. Variables available:
  224. id: The shot's id
  225. title: The shot's title
  226. url: The shot's url
  227. image_url: The url for the shot's image
  228. width: The shot's width
  229. height: The shot's height
  230. view_count: The shot's view count
  231. likes_count: The shot's like count
  232. comments_count: The shot's comment count
  233. rebounds_count: The shot's rebound count
  234. player_id: The player's id
  235. player_name: The player's name
  236. player_location: The player's location
  237. player_avatar_url: The player's avatar url
  238. Here's an example of how to use it in your templates:
  239. The below will retrieve a given player's 5 latest shots (large image)
  240. <ul>
  241. {exp:dreebbble:get_latest player="dribbble" limit="5" image_size="original"}
  242. <li>
  243. <h2><a href="{url}">{title}</a></h2>
  244. <h3>By: {player_name} from {player_location}</h3>
  245. <img src="{image_url}" alt="{title}">
  246. </li>
  247. {/exp:dreebbble:get_latest}
  248. </ul>
  249. This example will retrieve the five latest shots from everyone (thumbnail version)
  250. <ul>
  251. {exp:dreebbble:get_latest list="everyone" limit="5"}
  252. <li>
  253. <h2><a href="{url}">{title}</a></h2>
  254. <h3>By: {player_name} from {player_location}</h3>
  255. <img src="{image_url}" alt="{title}">
  256. </li>
  257. {/exp:dreebbble:get_latest}
  258. </ul>
  259. <?php
  260. $buffer = ob_get_contents();
  261. ob_end_clean();
  262. return $buffer;
  263. }
  264. // END
  265. }
  266. // END CLASS
  267. /* End of file pi.dreebbble.php */
  268. /* Location: ./system/expressionengine/third_party/dreebbble/pi.dreebbble.php */