PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wildflower/views/helpers/table.php

http://github.com/klevo/wildflower
PHP | 188 lines | 128 code | 32 blank | 28 comment | 23 complexity | 2b5a8f336b4ff0d8fec9304c37836f9b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Table Helper
  4. * Helper for generating data tables with CRUD links
  5. *
  6. * @author Robert Starsi <klevo@klevo.sk>
  7. */
  8. class TableHelper extends AppHelper {
  9. public $helpers = array('Html', 'Time');
  10. private $_defaultSettings = array(
  11. 'model' => '',
  12. 'editField' => 'title',
  13. 'left' => 'lft',
  14. 'right' => 'rght',
  15. 'class' => 'table',
  16. 'tree' => false,
  17. 'dateFields' => array('created', 'updated'));
  18. private $_emptyMessage = "There are no records to display.";
  19. /**
  20. * Generate mass edit table from Cake data
  21. *
  22. * @param array $data MPTT data from model
  23. * @param array $settings Settings
  24. */
  25. function create($data, $settings = array()) {
  26. if (empty($data)) {
  27. return "<p>{$this->_emptyMessage}</p>";
  28. }
  29. $settings = array_merge($this->_defaultSettings, $settings);
  30. $isTree = $settings['tree'];
  31. if (empty($settings['model'])) {
  32. trigger_error('Model class name can`t be empty.');
  33. }
  34. $modelPlural = Inflector::pluralize($settings['model']);
  35. $lModelPlural = low($modelPlural);
  36. $controller = $lModelPlural;
  37. $modelLow = low($settings['model']);
  38. $summary = " summary=\"$modelPlural table lists all site $lModelPlural and provides view, edit and delete actions.\"";
  39. $tableHtml = "\n<table cellspacing=\"0\"$summary";
  40. // Set CSS classes
  41. if (!empty($settings['class'])) {
  42. $tableHtml .= " class=\"{$settings['class']}\"";
  43. }
  44. $tableHtml .= ">\n";
  45. // Find out table column names
  46. $tableHeaderItems = array();
  47. if (isset($settings['fields']) && is_array($settings['fields'])) {
  48. $tableHeaderItems = $settings['fields'];
  49. } else {
  50. foreach ($data[0][$settings['model']] as $fieldName => $field) {
  51. $tableHeaderItems[] = $fieldName;
  52. }
  53. }
  54. $colsCount = count($tableHeaderItems);
  55. // Table head
  56. $tableHtml .= "<thead>\n";
  57. $tableHtml .= "<tr>";
  58. // Checkbox dummy cell
  59. $tableHtml .= "<th></th>";
  60. for ($i = 0; $i < $colsCount; $i++) {
  61. $field = Inflector::humanize($tableHeaderItems[$i]);
  62. $tableHtml .= "<th scope=\"col\">$field</th>";
  63. }
  64. $tableHtml .= "<th scope=\"col\" colspan=\"2\">Actions</th>";
  65. $tableHtml .= "</tr></thead>\n<tbody>";
  66. // Rows
  67. $rightNodes = array();
  68. foreach ($data as $key => $node) {
  69. $level = 0;
  70. if ($isTree) {
  71. // Check if we should remove a node from the stack
  72. while (!empty($rightNodes) && ($rightNodes[count($rightNodes) - 1] < $node[$settings['model']][$settings['right']])) {
  73. array_pop($rightNodes);
  74. }
  75. $level = count($rightNodes);
  76. }
  77. $cssClasses = low($settings['model']) . '-' . $node[$settings['model']]['id'] . " level-$level";
  78. // Alt CSS class for odd rows
  79. if ($key % 2 == 0) {
  80. $cssClasses .= " odd";
  81. }
  82. $id = $node[$settings['model']]['id'];
  83. $trId = "$modelLow-$id";
  84. $tableHtml .= "<tr id=\"$trId\" class=\"$cssClasses\">";
  85. // Add checkboxes
  86. $tableHtml .= "<td><input type=\"checkbox\" name=\"data[Page][$id]\" /></td>";
  87. // Add table cells
  88. for ($i = 0; $i < $colsCount; $i++) {
  89. $field = $tableHeaderItems[$i];
  90. $cell = $node[$settings['model']][$field];
  91. $tdClasses = array();
  92. // Nice short dates
  93. if (in_array($field, $settings['dateFields'])) {
  94. $cell = $this->Time->niceShort($cell);
  95. $tdClasses[] = 'date';
  96. }
  97. if ($i > 0 && strlen($cell) > Configure::read('Wildflower.table.maxlength')) {
  98. $cell = mb_substr($cell, 0, Configure::read('Wildflower.table.maxlength'), Configure::read('App.encoding')) . "...";
  99. }
  100. // Edit field
  101. if ($field == $settings['editField']) {
  102. $dashes = $spaces = '';
  103. if ($level > 0) {
  104. $dashes = '&ndash;';
  105. $spaces = str_repeat('&nbsp;&nbsp;', $level - 1);
  106. }
  107. $cell = $this->Html->link($cell, "/" . Configure::read('Routing.admin') . "/$controller/edit/$id", array(
  108. 'title' => "Edit this $modelLow"), null, false);
  109. $cell = "$spaces$dashes $cell";
  110. $tdClasses[] = 'primary-field';
  111. }
  112. $tdAttr = '';
  113. if (!empty($tdClasses)) {
  114. $tdAttr = ' class="' . join(' ', $tdClasses) . '"';
  115. }
  116. $tableHtml .= "<td$tdAttr>$cell</td>";
  117. }
  118. // Append actions
  119. switch ($controller) {
  120. // Decide the correct view method
  121. case 'posts':
  122. $slug = $node[$settings['model']]['slug'];
  123. $viewPath = '/' + WILDFLOWER_POSTS_INDEX + "/$slug";
  124. break;
  125. case 'pages':
  126. $viewPath = $node[$settings['model']]['url'];
  127. break;
  128. default:
  129. $viewPath = "/" . Configure::read('Routing.admin') . "/$controller/view/$id";
  130. }
  131. // Actions
  132. $actions = '';
  133. if ($isTree) {
  134. $moveUpImg = $this->Html->image('moveup.gif');
  135. $moveDownImg = $this->Html->image('movedown.gif');
  136. $moveActions = "<td class=\"action\">" . $this->Html->link($moveUpImg, array('action' => 'moveup', $id),
  137. array('class' => 'page-moveup', 'title' => 'Move this page up', 'escape' => false)) . "</td>"
  138. . "<td class=\"action\">" . $this->Html->link($moveDownImg, array('action' => 'movedown', $id),
  139. array('class' => 'page-movedown', 'title' => 'Move this page down', 'escape' => false)) . "</td>";
  140. $actions .= $moveActions;
  141. }
  142. $actions .= "<td class=\"action\">" . $this->Html->link('View', $viewPath) . "</td>";
  143. $tableHtml .= "$actions</tr>\n";
  144. // Add this node to the stack
  145. if ($isTree) {
  146. $rightNodes[] = $node[$settings['model']][$settings['right']];
  147. }
  148. }
  149. $tableHtml .= "</tbody>\n</table>\n";
  150. // Encapsulate into a form
  151. $formAction = $this->Html->url(array('action' => 'mass_update'));
  152. $formControls = "<p>With selected: <input type=\"submit\" value=\"Delete\" name=\"data[Delete]\" /> <input type=\"submit\" value=\"Mark as draft\" name=\"data[Draft]\" /> <input type=\"submit\" value=\"Publish\" name=\"data[Publish]\" /></p>";
  153. $html = "<form method=\"post\" action=\"$formAction\">$formControls\n$tableHtml</form>";
  154. return $html;
  155. }
  156. }