SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/ChildrenGalleryWidget.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Widgets;
  15.  
  16. use Sprout\Helpers\Form;
  17. use Sprout\Helpers\Navigation;
  18. use Sprout\Helpers\Pdb;
  19. use Sprout\Helpers\TreenodeInMenuMatcher;
  20. use Sprout\Helpers\TreenodeValueMatcher;
  21. use Sprout\Helpers\View;
  22.  
  23.  
  24. /**
  25. * Shows a list of pages that are related to this one
  26. **/
  27. class ChildrenGalleryWidget extends Widget
  28. {
  29. protected $friendly_name = "Children page gallery";
  30. protected $friendly_desc = 'A list of the children pages of this page, in a friendly gallery format';
  31.  
  32.  
  33. /**
  34.   * Does the front-end rendering of this widget
  35.   *
  36.   * @param int $orientation The orientation of the widget
  37.   **/
  38. public function render($orientation)
  39. {
  40. $this->settings['max_depth'] = (int) @$this->settings['max_depth'];
  41. if ($this->settings['max_depth'] <= 0) $this->settings['max_depth'] = 1;
  42.  
  43. $this->settings['thumb_rows'] = (int) @$this->settings['thumb_rows'];
  44. if ($this->settings['thumb_rows'] < 2) $this->settings['thumb_rows'] = 4;
  45.  
  46. $this->settings['hide_blanks'] = (int) @$this->settings['hide_blanks'];
  47.  
  48. $root_node = Navigation::getRootNode();
  49. if ($root_node == null) return;
  50.  
  51. $this->settings['parent'] = (int) @$this->settings['parent'];
  52. if ($this->settings['parent'] == 0) {
  53. $matcher = Navigation::getPageNodeMatcher();
  54. if ($matcher == null) return;
  55.  
  56. } else {
  57. $matcher = new TreenodeValueMatcher('id', $this->settings['parent']);
  58.  
  59. }
  60.  
  61. $page_node = $root_node->findNode($matcher);
  62. if ($page_node == null) return;
  63.  
  64. $page_node->filterChildren(new TreenodeInMenuMatcher());
  65.  
  66. if (count($page_node->children) == 0) {
  67. $page_node->removeFilter();
  68. return null;
  69. }
  70.  
  71. switch ($this->settings['thumb_rows']) {
  72. case 2:
  73. $image_resize = 'c600x493';
  74. break;
  75.  
  76. case 3:
  77. $image_resize = 'c480x394';
  78. break;
  79.  
  80. case 5:
  81. $image_resize = 'c332x270';
  82. break;
  83.  
  84. default:
  85. $image_resize = 'c362x306';
  86. break;
  87. }
  88.  
  89. $view = new View('sprout/children_page_gallery');
  90. $view->page_node = $page_node;
  91. $view->hide_blanks = $this->settings['hide_blanks'];
  92. $view->idx = 0;
  93. $view->thumb_rows = $this->settings['thumb_rows'];
  94. $view->image_resize = $image_resize;
  95.  
  96. $html = $view->render();
  97.  
  98. $page_node->removeFilter();
  99.  
  100. return $html;
  101. }
  102.  
  103.  
  104.  
  105. /**
  106.   * Returns the settings for the article list widget
  107.   * See {@link Widget::getSettingsForm} for full documentation
  108.   **/
  109. public function getSettingsForm()
  110. {
  111. $out = '';
  112.  
  113. $q = "SELECT id, name FROM ~subsites ORDER BY record_order";
  114. $res = Pdb::query($q, [], 'arr');
  115.  
  116. $pages = [];
  117.  
  118. foreach ($res as $row) {
  119. $root = Navigation::loadPageTree($row['id'], true, false);
  120. $children = $root->getAllChildren();
  121.  
  122. $pages[$row['name']] = $children;
  123. }
  124.  
  125. Form::nextFieldDetails('Parent Page', false);
  126.  
  127. if (count($pages) > 1) {
  128. $out .= "<p><strong>Note:</strong> Pages are ordered by subsite, all pages for all subsites are available by scrolling down the option list.</p><br>";
  129. $out .= Form::dropdown('parent', [], $pages);
  130. } else {
  131. $out .= Form::dropdown('parent', [], reset($pages));
  132. }
  133.  
  134. $out .= '<div class="field-group-wrap -clearfix">';
  135. $out .= '<div class="field-group-item col col--one-half">';
  136.  
  137. Form::nextFieldDetails('Options', false);
  138. $out .= Form::checkboxList(['hide_blanks' => 'Hide pages with no gallery image']);
  139.  
  140. $out .= '</div>';
  141. $out .= '<div class="field-group-item col col--one-half">';
  142.  
  143. Form::nextFieldDetails('Thumbnails per row', false);
  144. $out .= Form::dropdown('thumb_rows', [], ['2'=> '2', '3' => '3', '4' => '4', '5' => '5']);
  145.  
  146. $out .= '</div></div>';
  147.  
  148. return $out;
  149. }
  150.  
  151. }
  152.  
  153.  
  154.  
  155.