PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/wildflower/views/helpers/pages_list.php

http://github.com/klevo/wildflower
PHP | 303 lines | 213 code | 41 blank | 49 comment | 38 complexity | bf0accd63d83b0e3a2c59143e710075e MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class PagesListHelper extends AppHelper {
  3. public $helpers = array('Html', 'Time');
  4. private $_defaultSettings = array(
  5. 'id' => 'pages-list',
  6. 'class' => 'list'
  7. );
  8. private $_defaultSidebarSettings = array(
  9. 'id' => 'sidebar-pages-list',
  10. 'class' => 'sidebar-list'
  11. );
  12. private $_emptyMessage = 'No pages yet.';
  13. private $_limits = null;
  14. private $_treeSettings = array(
  15. 'model' => 'Page',
  16. 'alias' => 'title',
  17. 'left' => 'lft',
  18. 'right' => 'rght',
  19. 'primaryKey' => 'id'
  20. );
  21. /**
  22. * Create list of last modified pages
  23. *
  24. * @param array $pages
  25. * @param array $settings
  26. * @return string List HTML
  27. */
  28. function createLastModified($pages, $settings = array()) {
  29. if (empty($pages)) {
  30. return "<p>{$this->_emptyMessage}</p>";
  31. }
  32. $settings = array_merge($this->_defaultSettings, $settings);
  33. $listHtml = "<ul id=\"{$settings['id']}\" class=\"{$settings['class']}\">\n";
  34. foreach ($pages as $page) {
  35. // Start list item
  36. $cssId = "sidebar-page-{$page['Page']['id']}";
  37. $listHtml .= "\t<li id=\"$cssId\" class=\"actions-handle\">";
  38. // Edit link (don't escape, alredy happened in model)
  39. $listHtml .= "<h3>"
  40. . $this->Html->link($page['Page']['title'],
  41. array('controller' => 'pages', 'action' => 'edit', $page['Page']['id']),
  42. null,
  43. null,
  44. false)
  45. . "</h3>\n";
  46. // If a page is modified sooner then 3 weeks ago display a date, else time ago in words
  47. $threeWeeksAgoStamp = strtotime('-3 week');
  48. $pageTimeStamp = strtotime($page['Page']['updated']);
  49. $date = null;
  50. if ($pageTimeStamp > $threeWeeksAgoStamp) {
  51. $date = $this->Time->timeAgoInWords($page['Page']['updated']);
  52. } else {
  53. $date = $this->Time->niceShort($pageTimeStamp);
  54. }
  55. // Date and user
  56. $listHtml .= "<p>$date</p>\n";
  57. // Actions
  58. $listHtml .= '<small class="actions">'
  59. . $this->Html->link($this->Html->image('cross.gif'),
  60. array('controller' => 'pages', 'action' => 'delete', $page['Page']['id']),
  61. array('class' => 'delete', 'rel' => 'post'),
  62. null,
  63. false)
  64. . " "
  65. . $this->Html->link('View', $page['Page']['url'], array('class' => 'view'))
  66. . "</small>\n";
  67. // Close list item
  68. $listHtml .= "</li>\n\n";
  69. }
  70. // Close list
  71. $listHtml .= "</ul>\n\n";
  72. return $listHtml;
  73. }
  74. /**
  75. * Create a list of pages for sidebar
  76. *
  77. * @param array $pages
  78. * @param array $settings
  79. * @return string List HTML
  80. */
  81. function createSidebar($pages, $settings = array()) {
  82. if (empty($pages)) {
  83. return '';
  84. }
  85. $settings = array_merge($this->_defaultSidebarSettings, $settings);
  86. $listHtml = "<ul id=\"{$settings['id']}\" class=\"{$settings['class']}\">\n";
  87. // All pages link
  88. $currentClass = '';
  89. if ($this->params['action'] == 'admin_index') {
  90. $currentClass = ' current';
  91. }
  92. $listHtml .= "<li class=\"all$currentClass\">"
  93. . $this->Html->link('All pages', array('controller' => 'pages', 'action' => 'index'))
  94. . "</li>\n";
  95. foreach ($pages as $page) {
  96. // Start list item
  97. $cssId = "sidebar-page-{$page['Page']['id']}";
  98. // CSS classes
  99. $cssClasses = array();
  100. // Is current edited?
  101. $editedPageId = null;
  102. if ($this->params['action'] == 'admin_edit') {
  103. $editedPageId = $this->params['pass'][0];
  104. }
  105. if ($page['Page']['id'] == $editedPageId) {
  106. $cssClasses[] = 'current';
  107. }
  108. // Is home page?
  109. $homePageId = Configure::read('AppSettings.home_page_id');
  110. if ($page['Page']['id'] == $homePageId) {
  111. $cssClasses[] = 'homepage';
  112. }
  113. $classAttr = '';
  114. if (!empty($cssClasses)) {
  115. $classAttr = ' class="' . join(' ', $cssClasses) . '"';
  116. }
  117. $listHtml .= "\t<li id=\"$cssId\"$classAttr>";
  118. // Edit link (don't escape, alredy happened in model)
  119. $listHtml .= $this->Html->link($page['Page']['title'],
  120. array('controller' => 'pages', 'action' => 'edit', $page['Page']['id']),
  121. null,
  122. null,
  123. false)
  124. . "\n";
  125. // Close list item
  126. $listHtml .= "</li>";
  127. }
  128. // Close list
  129. $listHtml .= "</ul>\n\n";
  130. return $listHtml;
  131. }
  132. /**
  133. * Generate nested list from tree data
  134. *
  135. * @param array $data
  136. * @param array $indexArray Settings
  137. * @param string $ulClass
  138. * @return string Nested list
  139. */
  140. function createSitemap($data, $indexArray = array(), $ulClass = 'list', $addData = null) {
  141. extract($this->_treeSettings);
  142. extract($indexArray);
  143. // determin limits to know when the last top node is found.
  144. if (isset($data[0][$model][$left])) {
  145. $floor= $data[0][$model][$left];
  146. $ceil= $data[0][$model][$right];
  147. foreach ($data as $node) {
  148. if ($node[$model][$right] > $ceil) {
  149. $ceil= $node[$model][$right];
  150. }
  151. }
  152. } else {
  153. $floor = $ceil = 0;
  154. }
  155. $this->_limits = array(array($floor - 1, $ceil + 1));
  156. $this->_childCount= 1;
  157. if ($ulClass) {
  158. $return= "\r\n" . '<ul class="' . $ulClass . '">' . "\n";
  159. } else {
  160. $return= "\n" . '<ul>' . "\n";
  161. }
  162. foreach ($data as $i => $node) {
  163. $return .= "\n" . str_repeat("\t", $this->_childCount);
  164. $hasChildren= false;
  165. $last= false;
  166. $class= array ();
  167. if ($node[$model][$right] <> $node[$model][$left] + 1) { // Has some children
  168. $hasChildren= true;
  169. list ($parentLeft, $parentRight)= $this->_get_parent_indexes($node[$model][$left]);
  170. $last= ife($parentRight == ($node[$model][$right] + 1), true, false);
  171. $this->_limits[] = array($node[$model][$left], $node[$model][$right]);
  172. }
  173. if (!$hasChildren && (!isset ($data[$i +1]) || ($node[$model][$right] + 1 <> $data[$i +1][$model][$left]))) {
  174. // it's the last item or the last in the current series
  175. $last= true;
  176. $class[]= 'last';
  177. }
  178. $title = $node[$model][$alias];
  179. $id = $node[$model][$primaryKey];
  180. $controller = low(Inflector::pluralize($model));
  181. $lModel = low($model);
  182. $class[] = "sitemap-$lModel-$id";
  183. if ($class) {
  184. $return .= '<li class="' . implode($class, ' ') . '">';
  185. } else {
  186. $return .= '<li>';
  187. }
  188. // Link to the item
  189. $return .= $this->Html->link($title, $node[$model]['url']);
  190. if (isset ($data[$i +1])) {
  191. // If it's not the absolute last item
  192. if ($node[$model][$right] < $data[$i +1][$model][$left]) { // Close uls
  193. for ($j= 1; $j <= ($data[$i +1][$model][$left] - $node[$model][$right] - 1); $j++) {
  194. $this->_childCount--;
  195. if ($this->_childCount < 0) {
  196. trigger_error(__('child count less than 0 in ' . __METHOD__, E_USER_WARNING));
  197. }
  198. $return .= '</li>';
  199. $return .= "\n" . str_repeat("\t", $this->_childCount) . '</ul>';
  200. }
  201. $return .= '</li>';
  202. }
  203. elseif ($node[$model][$right] <> $node[$model][$left] + 1) { // Has some children
  204. $return .= "\n" . str_repeat("\t", $this->_childCount) . '<ul>';
  205. $this->_childCount++;
  206. } else {
  207. $return .= '</li>';
  208. }
  209. } else {
  210. // Last item in data list, close ul items
  211. $return .= '</li>';
  212. for (; $this->_childCount > 1; $this->_childCount--) {
  213. $return .= "\n" . str_repeat("\t", $this->_childCount - 1) . '</ul>';
  214. $return .= "\n" . str_repeat("\t", $this->_childCount - 1) . '</li>';
  215. }
  216. }
  217. }
  218. $return .= "\n" . '</ul>' . "\n";
  219. return $return;
  220. }
  221. /**
  222. * Find path to a page
  223. *
  224. * @param array $data
  225. * @param string $model Model name
  226. * @param int $startId id to start with
  227. */
  228. function _findPath($data, $model, $startId) {
  229. $parentId = null;
  230. $path = '';
  231. foreach($data as $node) {
  232. if($node[$model]['id'] == $startId) {
  233. $parentId = $node[$model]['parent_id'];
  234. $path = $node[$model]['slug'];
  235. }
  236. }
  237. if ($parentId) {
  238. return $this->_findPath($data, $model, $parentId) . "/$path";
  239. } else {
  240. return "/$path";
  241. }
  242. }
  243. function _get_parent_indexes($thisLeft) {
  244. if (!$this->_limits) {
  245. return array (
  246. 0,
  247. 0
  248. );
  249. }
  250. $parentLeft= $this->_limits[count($this->_limits) - 1][0];
  251. $parentRight= $this->_limits[count($this->_limits) - 1][1];
  252. if ($parentRight < $thisLeft) {
  253. unset ($this->_limits[count($this->_limits) - 1]);
  254. $this->_limits= array_values($this->_limits);
  255. return $this->_get_parent_indexes($thisLeft);
  256. }
  257. return array (
  258. $parentLeft,
  259. $parentRight
  260. );
  261. }
  262. }