SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/FileListWidget.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\FileConstants;
  17. use Sprout\Helpers\Form;
  18. use Sprout\Helpers\Pdb;
  19. use Sprout\Helpers\View;
  20. use Sprout\Helpers\WidgetArea;
  21.  
  22.  
  23. /**
  24. * doc list widget
  25. **/
  26. class FileListWidget extends Widget
  27. {
  28. protected $friendly_name = "File list";
  29. protected $friendly_desc = "Displays a list of up to 50 files";
  30. protected $default_settings = ['order' => FileConstants::ORDER_NAME];
  31.  
  32.  
  33. /**
  34.   * Returns the output of the doc list widget
  35.   * See {@link Widget::render} for full documentation
  36.   *
  37.   * @param int $orientation The orientation of the widget.
  38.   **/
  39. public function render($orientation)
  40. {
  41. $this->settings['category'] = (int) @$this->settings['category'];
  42. if ($this->settings['category'] == 0) return;
  43.  
  44. // Load the docs from the database
  45. $q = "SELECT files.*
  46. FROM ~files AS files
  47. INNER JOIN ~files_cat_join AS joiner ON joiner.file_id = files.id
  48. WHERE joiner.cat_id = ?
  49. ORDER BY {$this->getOrderSql()}
  50. LIMIT 50";
  51. $res = Pdb::query($q, [$this->settings['category']], 'arr');
  52.  
  53. if ($orientation == WidgetArea::ORIENTATION_TALL) {
  54. $view = new View('sprout/filelist_tall');
  55. } else if ($orientation == WidgetArea::ORIENTATION_WIDE) {
  56. $view = new View('sprout/filelist_wide');
  57. }
  58.  
  59. $view->res = $res;
  60.  
  61. return $view->render();
  62. }
  63.  
  64. /**
  65.   * Returns the settings for the doc list widget
  66.   * See {@link Widget::getSettingsForm} for full documentation
  67.   **/
  68. public function getSettingsForm()
  69. {
  70. $out = '';
  71.  
  72. $q = "SELECT cat.id, CONCAT(cat.name, ' (', COUNT(file.id), ')')
  73. FROM ~files_cat_list AS cat
  74. LEFT JOIN ~files_cat_join AS joiner ON cat.id = joiner.cat_id
  75. LEFT JOIN ~files AS file ON joiner.file_id = file.id
  76. GROUP BY cat.id
  77. ORDER BY cat.name";
  78. $cats = Pdb::query($q, [], 'map');
  79.  
  80. Form::nextFieldDetails('Category', true);
  81. $out .= Form::dropdown('category', [], $cats);
  82.  
  83. Form::nextFieldDetails('Display order', true);
  84. $out .= Form::dropdown('order', [], FileConstants::$order_names);
  85.  
  86. return $out;
  87. }
  88.  
  89.  
  90. /**
  91.   * Returns a URL for editing the contents of this widget
  92.   * See {@link Widget::getEditUrl} for full documentation
  93.   **/
  94. public function getEditUrl()
  95. {
  96. if (empty($this->settings['category'])) return null;
  97.  
  98. return 'admin/contents/file?_category_id=' . $this->settings['category'];
  99. }
  100.  
  101.  
  102. /**
  103.   * Returns a label which describes the contents of this widget
  104.   * See {@link Widget::get_info_label} for full documentation
  105.   **/
  106. public function getInfoLabels()
  107. {
  108. if (empty($this->settings['category'])) return null;
  109.  
  110. $cats = Pdb::lookup('files_cat_list');
  111.  
  112. return array(
  113. 'Category' => $cats[$this->settings['category']],
  114. 'Order' => FileConstants::$order_names[$this->settings['order']],
  115. );
  116. }
  117.  
  118.  
  119. /**
  120.   * Returns the SQL which should be used for ordering the articles
  121.   **/
  122. private function getOrderSql()
  123. {
  124. switch ($this->settings['order']) {
  125. case FileConstants::ORDER_NAME:
  126. return 'files.name ASC';
  127.  
  128. case FileConstants::ORDER_MANUAL:
  129. return 'joiner.record_order ASC';
  130.  
  131. case FileConstants::ORDER_OLDEST:
  132. return 'IFNULL(files.date_published, files.date_added) ASC';
  133.  
  134. case FileConstants::ORDER_NEWEST:
  135. return 'IFNULL(files.date_published, files.date_added) DESC';
  136.  
  137. default:
  138. return 'id ASC';
  139. }
  140. }
  141.  
  142. }
  143.