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

/views/helpers/rating.php

http://github.com/CakeDC/ratings
PHP | 185 lines | 105 code | 18 blank | 62 comment | 20 complexity | 18af721ee80b42009a9f9721aceca8b6 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010, Cake Development Corporation (http://cakedc.com)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. App::import('Helper', 'Html');
  12. /**
  13. * CakePHP Ratings Plugin
  14. *
  15. * Rating helper
  16. *
  17. * @package ratings
  18. * @subpackage ratings.views.helpers
  19. */
  20. class RatingHelper extends AppHelper {
  21. /**
  22. * helpers variable
  23. *
  24. * @var array
  25. */
  26. public $helpers = array ('Html', 'Form', 'Js' => 'Jquery');
  27. /**
  28. * Allowed types of html list elements
  29. *
  30. * @var array $allowedTypes
  31. */
  32. public $allowedTypes = array('ul', 'ol', 'radio');
  33. /**
  34. * Default settings
  35. *
  36. * @var array $defaults
  37. */
  38. public $defaults = array(
  39. 'stars' => 5,
  40. 'item' => null,
  41. 'value' => 0,
  42. 'type' => 'ul',
  43. 'createForm' => false,
  44. 'url' => array(),
  45. 'link' => true,
  46. 'redirect' => true,
  47. 'class' => 'rating');
  48. /**
  49. * Displays a bunch of rating links wrapped into a list element of your choice
  50. *
  51. * @param array $options
  52. * @param array $urlHtmlAttributes Attributes for the rating links inside the list
  53. * @return string markup that displays the rating options
  54. */
  55. public function display($options = array(), $urlHtmlAttributes = array()) {
  56. $options = array_merge($this->defaults, $options);
  57. if (empty($options['item'])) {
  58. throw new Exception(__d('ratings', 'You must set the id of the item you want to rate.', true), E_USER_NOTICE);
  59. }
  60. if ($options['type'] == 'radio') {
  61. return $this->starForm($options, $urlHtmlAttributes);
  62. }
  63. $stars = null;
  64. for ($i = 1; $i <= $options['stars']; $i++) {
  65. $link = null;
  66. if ($options['link'] == true) {
  67. $url = array_merge($options['url'], array('rate' => $options['item'], 'rating' => $i));
  68. if ($options['redirect']) {
  69. $url['redirect'] = 1;
  70. }
  71. $link = $this->Html->link($i, $url, $urlHtmlAttributes);
  72. }
  73. $stars .= $this->Html->tag('li', $link, array('class' => 'star' . $i));
  74. }
  75. if (in_array($options['type'], $this->allowedTypes)) {
  76. $type = $options['type'];
  77. } else {
  78. $type = 'ul';
  79. }
  80. $stars = $this->Html->tag($type, $stars, array('class' => $options['class'] . ' ' . 'rating-' . round($options['value'], 0)));
  81. return $stars;
  82. }
  83. /**
  84. * Bar rating
  85. *
  86. * @param integer value
  87. * @param integer total amount of rates
  88. * @param array options
  89. * @return string
  90. */
  91. public function bar($value = 0, $total = 0, $options = array()) {
  92. $defaultOptions = array(
  93. 'innerClass' => 'inner',
  94. 'innerHtml' => '<span>%value%</span>',
  95. 'innerOptions' => array(),
  96. 'outerClass' => 'barRating',
  97. 'outerOptions' => array(),
  98. 'element' => null);
  99. $options = array_merge($defaultOptions, $options);
  100. $percentage = $this->percentage($value, $total);
  101. if (!empty($options['element'])) {
  102. $View =& ClassRegistry:: getObject('view');
  103. return $View->element($options['element'], array(
  104. 'value' => $value,
  105. 'percentage' => $percentage,
  106. 'total' => $total));
  107. }
  108. $options['innerOptions']['style'] = 'width: ' . $percentage . '%';
  109. $innerContent = str_replace('%value%', $value, $options['innerHtml']);
  110. $innerContent = str_replace('%percentage%', $percentage, $innerContent);
  111. $inner = $this->Html->div($options['innerClass'], $innerContent, $options['innerOptions']);
  112. return $this->Html->div($options['outerClass'], $inner, $options['outerOptions']);
  113. }
  114. /**
  115. * Calculates the percentage value
  116. *
  117. * @param integer value
  118. * @param integer total amount
  119. * @param integer precision of rounding
  120. * @return mixed float or integer based on the precision value
  121. */
  122. public function percentage($value = 0, $total = 0, $precision = 2) {
  123. if ($total) {
  124. return (round($value / $total, $precision) * 100);
  125. }
  126. return 0;
  127. }
  128. /**
  129. * Displays a star form
  130. *
  131. * @param array $options
  132. * @param array $urlHtmlAttributes Attributes for the rating links inside the list
  133. * @return string markup that displays the rating options
  134. */
  135. public function starForm($options = array(), $urlHtmlAttributes = array()) {
  136. $options = array_merge($this->defaults, $options);
  137. $flush = false;
  138. if (empty($options['item'])) {
  139. trigger_error(__d('ratings', 'You must set the id of the item you want to rate.', true), E_USER_NOTICE);
  140. }
  141. $result = '';
  142. if ($options['createForm']) {
  143. $result .= $this->Form->create($options['createForm']) . "\n";
  144. }
  145. $inputField = 'rating';
  146. if (!empty($options['inputField'])) {
  147. $inputField = $options['inputField'];
  148. }
  149. $result .= $this->Form->input($inputField, array(
  150. 'type' => 'radio',
  151. 'value' => isset($options['value']) ? round($options['value']) : 0,
  152. 'options' => array_combine(range(1, $options['stars']), range(1, $options['stars']))));
  153. if ($options['createForm']) {
  154. if (!empty($options['target']) && !empty($options['createForm']['url']) && !empty($options['createForm']['ajaxOptions'])) {
  155. $result .= $this->Js->submit(__d('ratings', 'Rate!', true), array_merge(array('url' => $options['createForm']['url']), $options['createForm']['ajaxOptions'])) . "\n";
  156. $flush = true;
  157. } else {
  158. $result .= $this->Form->submit(__d('ratings', 'Rate!', true)) . "\n";
  159. }
  160. $result .= $this->Form->end() . "\n";
  161. if ($flush) {
  162. $this->Js->writeBuffer();
  163. }
  164. }
  165. return $result;
  166. }
  167. }