PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/Smarty-2.6.26/plugins/block.split.php

http://sifo.googlecode.com/
PHP | 148 lines | 74 code | 19 blank | 55 comment | 13 complexity | a9e56b572b69b8554bbeb6c07ef242d1 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /*
  8. Distributed under the terms of the BSD Licence:
  9. Copyright (c) 2007, Internet Brands, Inc (www.internetbrands.com)
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are met:
  13. * Redistributions of source code must retain the above copyright notice, this list of
  14. conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation and/or other
  17. materials provided with the distribution.
  18. * Neither the name of the Internet Brands nor the names of its contributors may be used
  19. to endorse or promote products derived from this software without specific prior
  20. written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /**
  34. * Smarty {split}{/split} block plugin
  35. *
  36. * Type: block function<br>
  37. * Name: split<br>
  38. * Purpose: loop structure that splits an array into chunks to allow for column formatting -- return chunks
  39. * in column format
  40. * @param array
  41. * <pre>
  42. * Params: count: number of columns
  43. * from: source array
  44. * name: smarty param to assign chunks to
  45. * </pre>
  46. * @author Kevin Sours (kevin.sours@internetbrands.com)
  47. * Internet Brands (http://ibbydev.blogspot.com/)
  48. * @param string contents of the block
  49. * @param Smarty
  50. * @return string $content -- smarty content is not modified.
  51. */
  52. function smarty_block_split($params, $content, &$smarty, &$repeat) {
  53. if (!array_key_exists('from', $params)) {
  54. $smarty->trigger_error("split: missing 'from' parameter", E_USER_WARNING);
  55. $repeat = false;
  56. return;
  57. }
  58. //if the source var isn't set, quietly treat it as empty.
  59. if(is_null($params['from'])) {
  60. $repeat = false;
  61. return;
  62. }
  63. if (!is_array($params['from'])) {
  64. $smarty->trigger_error("split: 'from' parameter must be an array", E_USER_WARNING);
  65. $repeat = false;
  66. return;
  67. }
  68. if (!isset($params['item'])) {
  69. $smarty->trigger_error("split: missing 'item' parameter", E_USER_WARNING);
  70. $repeat = false;
  71. return;
  72. }
  73. $count = 2;
  74. if (isset($params['count'])) {
  75. $count = $params['count'];
  76. }
  77. $name = $params['item'];
  78. $indexName = "smarty.IB.split.$name.index";
  79. $index = $smarty->get_template_vars($indexName);
  80. if(!$index) {
  81. $index = 0;
  82. }
  83. $lastName = "smarty.IB.split.$name.last";
  84. $source = $params["from"];
  85. if(is_null($content)) {
  86. $index++;
  87. $smarty->assign($indexName, $index);
  88. $amount = split_helper_getsize($source, $count, $index);
  89. split_helper_doslice(&$smarty, $name, $lastName, $source, $amount);
  90. }
  91. else {
  92. if($index == $count) {
  93. $smarty->clear_assign($indexName);
  94. $smarty->clear_assign($lastName);
  95. $smarty->clear_assign($name);
  96. }
  97. else {
  98. $index++;
  99. $smarty->assign($indexName, $index);
  100. $amount = split_helper_getsize($source, $count, $index);
  101. split_helper_doslice(&$smarty, $name, $lastName, $source, $amount);
  102. $repeat = true;
  103. }
  104. return $content;
  105. }
  106. }
  107. function split_helper_getsize($source, $count, $index) {
  108. $amount = (int) (count($source)/$count);
  109. $extra = count($source) % $count;
  110. if($extra >= $index) {
  111. $amount++;
  112. }
  113. return $amount;
  114. }
  115. function split_helper_doslice(&$smarty, $name, $lastName, $source, $amount) {
  116. $last = $smarty->get_template_vars($lastName);
  117. if(!$last) {
  118. $last = 0;
  119. }
  120. $slice = array_slice($source, $last, $amount, false);
  121. $smarty->assign($name, $slice);
  122. $smarty->assign($lastName, ($last + $amount));
  123. return $slice;
  124. }
  125. ?>