PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/system/expressionengine/third_party/splitter/pi.splitter.php

https://bitbucket.org/tdevonshire/guinness-jazz-festival
PHP | 324 lines | 253 code | 36 blank | 35 comment | 25 complexity | a76a797b7c608912a0685379323718b3 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. ========================================================
  4. --------------------------------------------------------
  5. Copyright: Oliver Heine
  6. http://utilitees.de/ee.php/splitter
  7. --------------------------------------------------------
  8. This addon may be used free of charge. Should you
  9. employ it in a commercial project of a customer or your
  10. own I'd appreciate a small donation.
  11. ========================================================
  12. File: pi.splitter.php
  13. --------------------------------------------------------
  14. Purpose: Split any kind of lists into blocks.
  15. ========================================================
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  17. ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  18. LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
  20. EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  24. OR OTHER DEALINGS IN THE SOFTWARE.
  25. =========================================================
  26. */
  27. $plugin_info = array(
  28. 'pi_name' => 'Splitter',
  29. 'pi_version' => '2.0',
  30. 'pi_author' => 'Oliver Heine',
  31. 'pi_author_url' => 'http://utilitees.de/ee.php/splitter',
  32. 'pi_description' => 'Split any kind of lists into blocks.',
  33. 'pi_usage' => Splitter::usage()
  34. );
  35. class Splitter {
  36. var $return_data;
  37. function Splitter ()
  38. {
  39. $this->EE =& get_instance();
  40. $tagdata = $this->EE->TMPL->tagdata;
  41. $delimiter = $this->EE->TMPL->fetch_param('delimiter');
  42. $blocks = $this->EE->TMPL->fetch_param('blocks');
  43. $class = $this->EE->TMPL->fetch_param('class');
  44. $block_start = $this->EE->TMPL->fetch_param('block_start','');
  45. $block_end = $this->EE->TMPL->fetch_param('block_end','');
  46. if (!defined('PAGINATION')) define('PAGINATION', '<!-- pagination //-->') ;
  47. // find pagination
  48. $pc = substr_count($tagdata, PAGINATION);
  49. if ( $pc == 2 || $pc == 4 )
  50. {
  51. $paginationp = "bottom";
  52. $begin = strpos($tagdata,PAGINATION);
  53. $end = strpos($tagdata,PAGINATION, $begin + strlen(PAGINATION));
  54. $pagination = substr($tagdata,$begin+strlen(PAGINATION),$end-$begin-strlen(PAGINATION));
  55. $tagdata = substr($tagdata, 0, $begin) . substr($tagdata,$end+strlen(PAGINATION));
  56. if ( $begin < 20 )
  57. {
  58. $paginationp = "top";
  59. }
  60. }
  61. if ( $pc == 4 )
  62. {
  63. $begin = strpos($tagdata,PAGINATION);
  64. $end = strpos($tagdata,PAGINATION, $begin + strlen(PAGINATION));
  65. $tagdata = substr($tagdata, 0, $begin) . substr($tagdata, $end+strlen(PAGINATION));
  66. }
  67. if ($blocks === FALSE OR $delimiter === FALSE)
  68. {
  69. $this->return_data = $tagdata;
  70. return;
  71. }
  72. $cat_array = explode($delimiter, $tagdata);
  73. foreach ($cat_array AS $key => $value )
  74. {
  75. $value = trim($value);
  76. if ( empty($value) )
  77. {
  78. unset($cat_array[$key]);
  79. }
  80. }
  81. $num_cats = count($cat_array);
  82. if ( $num_cats == 0 )
  83. {
  84. $this->return_data = $tagdata;
  85. return;
  86. }
  87. $limit = ceil($num_cats / $blocks);
  88. $mykeys = array_keys($cat_array);
  89. $cc = 0;
  90. $output = "\n<div class=\"$class\">";
  91. if ( !empty($block_start) )
  92. {
  93. $output .= $block_start;
  94. }
  95. $ll = 0;
  96. foreach ($mykeys AS $keyname)
  97. {
  98. if ($cc == $limit)
  99. {
  100. if ( !empty($block_end) )
  101. {
  102. $output .= $block_end;
  103. }
  104. $output .= "\n</div>\n\n<div class=\"$class\">";
  105. if ( !empty($block_start) )
  106. {
  107. $output .= $block_start;
  108. }
  109. $cc = 0;
  110. }
  111. $cc++;
  112. $output .= $cat_array[$keyname].$delimiter;
  113. }
  114. if ( !empty($block_end) )
  115. {
  116. $output .= $block_end;
  117. }
  118. $output .= "\n</div>\n";
  119. // re-add pagination
  120. if ( $pc == 2 )
  121. {
  122. if ($paginationp == "top")
  123. {
  124. $output = $pagination . $output;
  125. }
  126. else
  127. {
  128. $output .= $pagination;
  129. }
  130. }
  131. if ( $pc == 4 )
  132. {
  133. $output = $pagination . $output . $pagination;
  134. }
  135. $this->return_data = $output;
  136. }
  137. /* END */
  138. // ----------------------------------------
  139. // Plugin Usage
  140. // ----------------------------------------
  141. // This function describes how the plugin is used.
  142. // Make sure and use output buffering
  143. function usage()
  144. {
  145. ob_start();
  146. ?>
  147. This plugin may be used to split any kind of listings into several blocks with evenly distributed items. This can be manually created lists, lists produced by {exp:channel:entries}- or {exp:channel:categories}-tags or whatever.
  148. There must however be some text in between the single items that can be used as a delimiter.
  149. The number of items per block is determined automatically and blocks surrounded by <div></div> are created. You may specify a classname to be used in the <div>.
  150. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  151. EXAMPLE:
  152. ========
  153. {exp:channel:entries channel="default"}
  154. {title}<br />
  155. {/exp:channel:entries}
  156. You have a list of channel entries and want to create three evenly sized columns.
  157. Wrap the {exp:splitter}-tags around the {exp:channel:entries}-tags, specifiy the number of blocks you want, a text to be used as delimiter and the class you want to be applied to the <div>s.
  158. {exp:splitter blocks="3" delimiter="<br />" class="myclass"}
  159. {exp:channel:entries channel="default"}
  160. {title}<br />
  161. {/exp:channel:entries}
  162. {/exp:splitter}
  163. will produce
  164. <div class="myclass">
  165. Title 1<br />
  166. Title 2<br />
  167. Title 3<br />
  168. </div>
  169. <div class="myclass">
  170. Title 4<br />
  171. Title 5<br />
  172. Title 6<br />
  173. </div>
  174. <div class="myclass">
  175. Title 7<br />
  176. Title 8<br />
  177. Title 9<br />
  178. </div>
  179. To make columns you'd probably set the class to float:left and might want to specify a width. Like:
  180. <style>
  181. .myclass {
  182. float:left;
  183. width:33%;
  184. }
  185. </style>
  186. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  187. USAGE DETAILS:
  188. ==============
  189. TAG:
  190. ----
  191. {exp:splitter}{/exp:splitter}
  192. PARAMETERS:
  193. -----------
  194. blocks="X"
  195. Mandatory. Sets the number of blocks you want.
  196. block_start=""
  197. Optional. Text to be added after each opening <div>.
  198. block_end=""
  199. Optional. Text to be added before each closing </div>.
  200. class="X"
  201. Optional. Sets the CSS class name for the <div> surrounding a block of list items.
  202. delimiter="X"
  203. Mandatory. This must be a unique string in your list that separates any two items.
  204. --- Example 1 ---
  205. {exp:channel:categories channel="default" style="linear"}
  206. <li><a href="{path=site/index}">{category_name}</a></li>
  207. {/exp:channel:categories}
  208. In this example the delimiter would be: </li>
  209. --------------------
  210. --- Example 2 ---
  211. {exp:channel:entries channel="default"}
  212. <a href="{permalink=site/comments}">{title}</a><br />
  213. {/exp:channel:entries}
  214. In this example the delimiter would be: <br />
  215. --------------------
  216. --- Example 3 ---
  217. 1<!-- //-->2<!-- //-->3<!-- //-->4<!-- //-->5<!-- //-->
  218. In this example the delimiter would be: <!-- //-->
  219. --------------------
  220. I hope you get the point ;-)
  221. NOTE ON PAGINATION:
  222. -------------------
  223. If you use Splitter in combination with {exp:channel:entries} or
  224. {exp:comment:entries} and want to use pagination you have to place special markers
  225. inside your {paginate}-tagpair to help Splitter identify the pagination
  226. codeblock.
  227. Just insert <!-- pagination //--> after the opening and before the closing
  228. {paginate}-tag. The markers will be completely removed before the page is
  229. rendered.
  230. Example 1:
  231. ----------
  232. {paginate}<!-- pagination //-->
  233. <p>Page {current_page} of {total_pages} pages {pagination_links}</p>
  234. <!-- pagination //-->{/paginate}
  235. Example 2:
  236. ----------
  237. {paginate}<!-- pagination //-->
  238. {if previous_page}<a href="{auto_path}">Previous Page</a> &nbsp;{/if}
  239. {if next_page}<a href="{auto_path}">Next Page</a>{/if}
  240. <!-- pagination //-->{/paginate}
  241. CHANGELOG:
  242. ==========
  243. 2.0
  244. Inital release for Expression Engine 2
  245. <?php
  246. $buffer = ob_get_contents();
  247. ob_end_clean();
  248. return $buffer;
  249. }
  250. /* END */
  251. }
  252. /* END Class */
  253. /* End of file pi.splitter.php */
  254. /* Location: ./system/expressionengine/third_party/splitter/pi.splitter.php */