PageRenderTime 226ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/View/Helper/BatchHelper.php

http://github.com/ProLoser/CakePHP-Batch
PHP | 267 lines | 152 code | 18 blank | 97 comment | 17 complexity | 3aea2911023866ad1bc3fe04dc68d006 MD5 | raw file
  1. <?php
  2. /**
  3. * Filter Helper
  4. *
  5. * Generates a form to use to filter the pagination results of the current page
  6. *
  7. * @package default
  8. * @author Dean
  9. */
  10. class BatchHelper extends Helper {
  11. public $helpers = array('Form');
  12. public $model = '';
  13. /**
  14. * Starts a new form with modifications necessary for the batch plugin.
  15. * Supports both filter and batch functions so that it can wrap an entire table.
  16. *
  17. * @param string $model
  18. * @param string $params
  19. * @param string $inputDefaults
  20. * @return void
  21. * @author Dean Sofer
  22. */
  23. public function create($model, $params = array(), $inputDefaults = array()) {
  24. $this->model = $model;
  25. if (!isset($params['class']))
  26. $params['class'] = 'batch';
  27. $params['url'] = $this->here;
  28. $params['inputDefaults'] = array_merge(array(
  29. 'empty' => __(' -- '),
  30. 'div' => false,
  31. 'label' => false,
  32. ), $inputDefaults);
  33. return $this->Form->create($model, $params);
  34. }
  35. /**
  36. * Simply closes the form. Additional functionality (if necessar) may be added later
  37. *
  38. * @return void
  39. * @author Dean Sofer
  40. */
  41. public function end() {
  42. return $this->Form->end();
  43. }
  44. /**
  45. * Generates a filters form
  46. *
  47. * @param string $model
  48. * @param string $fields
  49. * @return void
  50. * @author Dean
  51. */
  52. public function form($model, $fields = null, $blacklist = null) {
  53. $output = $this->Form->create($model, array('class' => 'filters'));
  54. $this->model = $model;
  55. if (!empty($fields)) {
  56. $fields = array_merge($fields, array(
  57. 'legend' => false,
  58. 'fieldset' => false,
  59. ));
  60. $output .= $this->Form->inputs($fields, $blacklist);
  61. }
  62. $output .= $this->Form->submit(__('Filter'), array('name' => 'data[filter]'));
  63. $output .= $this->Form->submit(__('Reset'), array('name' => 'data[reset]'));
  64. $output .= $this->Form->end();
  65. return $output;
  66. }
  67. /**
  68. * Generates a filtering row for use in a paginated table
  69. *
  70. * Pass null values in the fields array to generate empty header cells
  71. * Pass true to force the filter/reset buttons to appear somewhere other than the end
  72. * Example: $this->Batch->filter(array(null, 'name', 'date' => array('minYear' => 2000)))
  73. *
  74. * @param string $fields
  75. * @return void
  76. * @author Dean
  77. */
  78. public function filter($fields = array()) {
  79. $output = '<tr class="filters">';
  80. if (!empty($fields)) {
  81. foreach ($fields as $field => $options) {
  82. if (is_int($field)) {
  83. $field = $options;
  84. $options = array();
  85. }
  86. if (empty($field)) {
  87. $output .= '<th>&nbsp;</th>';
  88. } elseif ($field === true) {
  89. $output = '<th class="actions">';
  90. $output .= $this->filterButtons();
  91. $output .= '</th>';
  92. } else {
  93. $options['group'] = 'Filter';
  94. $output .= '<th>' . $this->_input($field, $options) . '</th>';
  95. }
  96. }
  97. }
  98. if (!in_array(true, $fields, true)) {
  99. $output .= '<th class="actions">';
  100. $output .= $this->filterButtons();
  101. $output .= '</th>';
  102. }
  103. $output .= '</tr>';
  104. return $output;
  105. }
  106. /**
  107. * Small helper function for filter method
  108. *
  109. * @return string
  110. * @author Dean Sofer
  111. */
  112. public function filterButtons() {
  113. $output = $this->Form->submit(__('Filter'), array('div' => false, 'name' => 'data[Filter][filter]'));
  114. $output .= $this->Form->submit(__('Reset'), array('div' => false, 'name' => 'data[Filter][reset]'));
  115. return $output;
  116. }
  117. /**
  118. * Generates a filtering row for use in a paginated table
  119. *
  120. * Pass null values in the fields array to generate empty header cells
  121. * Pass true to force the filter/reset buttons to appear somewhere other than the end
  122. * Example: $this->Batch->filter(array(null, 'name', 'date' => array('minYear' => 2000)))
  123. *
  124. * @param string $fields
  125. * @return void
  126. * @author Dean
  127. */
  128. public function batch($fields = array(), $options = array()) {
  129. $options = array_merge(array(
  130. ), $options);
  131. $output = '<tr class="batch">';
  132. if (!empty($fields)) {
  133. foreach ($fields as $field => $attributes) {
  134. if (is_int($field)) {
  135. $field = $attributes;
  136. $attributes = array();
  137. }
  138. if (empty($field)) {
  139. $output .= '<th>&nbsp;</th>';
  140. } elseif ($field === true) {
  141. $output = '<th class="actions">';
  142. $output .= $this->batchButtons();
  143. $output .= '</th>';
  144. } else {
  145. $attributes['group'] = 'Batch';
  146. if (!isset($attributes['disabled']))
  147. $attributes['disabled'] = true;
  148. $output .= '<th>';
  149. $output .= $this->Form->checkbox(null, array('name' => null, 'id' => null, 'checked' => !$attributes['disabled'], 'hiddenField' => false));
  150. $output .= $this->_input($field, $attributes);
  151. $output .= '</th>';
  152. }
  153. }
  154. }
  155. if (!in_array(true, $fields, true)) {
  156. $output .= '<th class="actions">';
  157. $output .= $this->batchButtons();
  158. $output .= '</th>';
  159. }
  160. $output .= '</tr>';
  161. return $output;
  162. }
  163. /**
  164. * Small helper function for batch method
  165. *
  166. * @return string
  167. * @author Dean Sofer
  168. */
  169. public function batchButtons() {
  170. $output = $this->Form->submit(__('Update'), array('div' => false, 'name' => 'data[Batch][update]', 'onclick' => "return confirm('" . __('Are you sure you want to update the selected records?') . "');"));
  171. $output .= $this->Form->submit(__('Delete'), array('div' => false, 'name' => 'data[Batch][delete]', 'onclick' => "return confirm('" . __('Are you sure you want to delete the selected records?') . "');"));
  172. return $output;
  173. }
  174. /**
  175. * Generates a checkbox used for batch actions for the current row of items
  176. *
  177. * @param string $recordId
  178. * @return string
  179. * @author Dean Sofer
  180. */
  181. public function checkbox($recordId) {
  182. $field = 'BatchRecords.' . $recordId;
  183. $params = array('value' => $recordId, 'hiddenField' => false, 'class' => 'batch');
  184. return $this->Form->checkbox($field, $params);
  185. }
  186. /**
  187. * Generates a checkbox used only for toggling all batch checkboxes at once
  188. *
  189. * @return string
  190. * @author Dean Sofer
  191. */
  192. public function all($options = array()) {
  193. $options = array_merge(array(
  194. 'value' => false,
  195. 'id' => false,
  196. 'name' => false,
  197. 'hiddenField' => false,
  198. 'class' => 'batch-all'
  199. ), $options);
  200. return $this->Form->checkbox('', $options);
  201. }
  202. /**
  203. * Generates a form input, allowing default options to be passed and handling cake versions separately
  204. *
  205. * @param string $model
  206. * @param string $field
  207. * @param array $options
  208. * @param array $defaults
  209. * @return string $output
  210. * @author Dean
  211. */
  212. protected function _input($field, $options = array(), $defaults = array()) {
  213. $position = strpos($field, '.');
  214. if ($position !== false) {
  215. $model = substr($field, 0, $position);
  216. $field = substr($field, $position + 1);
  217. } else {
  218. $model = $this->model;
  219. }
  220. switch ($this->_fieldType($model, $field)) {
  221. case 'string':
  222. case 'text':
  223. $options += array('type' => 'text');
  224. break;
  225. case 'boolean':
  226. $options += array('options' => array(true => __('Yes'), false => __('No')));
  227. break;
  228. }
  229. $group = $options['group'];
  230. unset($options['group']);
  231. $output = $this->Form->input($group . '.' . $model . '.' . $field, array_merge($defaults, $options));
  232. return $output;
  233. }
  234. /**
  235. * Returns the field datatype based on the model schema
  236. *
  237. * @param string $model
  238. * @param string $field
  239. * @return string $type
  240. * @author Dean
  241. */
  242. protected function _fieldType($model, $field) {
  243. $type = null;
  244. if (isset($this->Form->fieldset[$model]['fields'][$field]['type'])) {
  245. $type = $this->Form->fieldset[$model]['fields'][$field]['type'];
  246. }
  247. return $type;
  248. }
  249. }