SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/AdminDashboard.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\Helpers;
  15.  
  16. use karmabunny\pdb\Exceptions\QueryException;
  17.  
  18.  
  19. /**
  20.  * Dashboard shown when a user first logs in to the admin
  21.  */
  22. class AdminDashboard
  23. {
  24.  
  25. /**
  26.   * Render the admin dashboard
  27.   *
  28.   * @return string HTML
  29.   */
  30. public static function render()
  31. {
  32. $out = '';
  33. $out .= self::firstRun();
  34.  
  35. if (AdminPerms::canAccess('access_noapproval')) {
  36. $out .= self::needApproval();
  37. }
  38.  
  39. $out .= self::moderationButton();
  40. $out .= self::newContent();
  41.  
  42. return $out;
  43. }
  44.  
  45.  
  46. /**
  47.   * Show the "first run" message, which welcomes new operators to the admin
  48.   *
  49.   * @return string HTML
  50.   */
  51. private static function firstRun()
  52. {
  53. if (!AdminAuth::hasDatabaseRecord()) {
  54. return '';
  55. }
  56.  
  57. $q = "SELECT firstrun FROM ~operators WHERE id = ?";
  58. $firstrun = Pdb::query($q, [AdminAuth::getId()], 'val');
  59. if ($firstrun !== '1') {
  60. return '';
  61. }
  62.  
  63. $view = new View('sprout/admin/dashboard/first_run');
  64. return $view->render();
  65. }
  66.  
  67.  
  68. /**
  69.   * A list of new content (pages, files, etc)
  70.   *
  71.   * @return string HTML
  72.   */
  73. private static function newContent()
  74. {
  75. // New content
  76. $tables = array();
  77. $tables['Page'] = 'pages';
  78. $tables['File'] = 'files';
  79.  
  80. $q = [];
  81. $params = [];
  82. foreach ($tables as $name => $t) {
  83. $controller = Inflector::singular($t);
  84. $where = ($t == 'files' ? "t.name != ''" : '1');
  85. $q[] = "(
  86. SELECT CONCAT(?, t.id) AS id, t.name, '{$name}' AS type, DATE_FORMAT(t.date_added, '%W %D') AS d, t.date_added
  87. FROM ~{$t} AS t
  88. WHERE t.date_added > DATE_SUB(NOW(), INTERVAL 1 WEEK)
  89. AND {$where}
  90. ORDER BY t.date_added DESC
  91. LIMIT 5
  92. )";
  93. $params[] = "SITE/admin/edit/{$controller}/";
  94. }
  95.  
  96. $q = implode (' UNION ', $q);
  97. $q .= ' ORDER BY date_added DESC LIMIT 20';
  98. try {
  99. $res = Pdb::query($q, $params, 'arr');
  100. } catch (QueryException $ex) {
  101. // Assume DB has no tables
  102. $res = [];
  103. }
  104.  
  105. if (count($res) === 0) {
  106. return '';
  107. }
  108.  
  109. // Create the itemlist
  110. $itemlist = new Itemlist();
  111. $itemlist->main_columns = array('Type' => 'type', 'Name' => 'name', 'Added' => 'd');
  112. $itemlist->items = $res;
  113. $itemlist->addAction('edit', '%ne%');
  114.  
  115. return '<h3>New content</h3>' . $itemlist->render();
  116. }
  117.  
  118.  
  119. /**
  120.   * A list of "need approval" pages currently in the system
  121.   *
  122.   * @return string HTML
  123.   */
  124. private static function needApproval()
  125. {
  126. $q = "SELECT pages.id, pages.name, DATE_FORMAT(page_revisions.date_modified, '%d/%m/%Y') AS date_modified, page_revisions.modified_editor
  127. FROM ~page_revisions AS page_revisions
  128. INNER JOIN ~pages AS pages ON page_revisions.page_id = pages.id
  129. WHERE page_revisions.status = ? AND subsite_id = ?
  130. GROUP BY pages.id
  131. ORDER BY page_revisions.date_modified DESC
  132. LIMIT 5";
  133. $params = ['need_approval', $_SESSION['admin']['active_subsite']];
  134. try {
  135. $res = Pdb::query($q, $params, 'arr');
  136. } catch (QueryException $ex) {
  137. // Assume DB has no tables
  138. $res = [];
  139. }
  140.  
  141. if (count($res) === 0) {
  142. return '';
  143. }
  144.  
  145. // Create the itemlist
  146. $itemlist = new Itemlist();
  147. $itemlist->main_columns = array('Name' => 'name', 'Date modified' => 'date_modified', 'Editor' => 'modified_editor');
  148. $itemlist->items = $res;
  149. $itemlist->addAction('edit', 'SITE/admin/edit/page/%%#main-tabs-revs');
  150.  
  151. return '<h3>Pages needing approval</h3>' . $itemlist->render();
  152. }
  153.  
  154.  
  155. /**
  156.   * A button to moderate content, if there is actually content to moderate
  157.   *
  158.   * @return string HTML
  159.   */
  160. private static function moderationButton()
  161. {
  162. $has_moderation = false;
  163.  
  164. $moderators = Register::getModerators();
  165. foreach ($moderators as $class) {
  166. $inst = new $class;
  167. if (! $inst) continue;
  168. if (! $inst instanceof Moderate) continue;
  169.  
  170. $list = $inst->getList();
  171. if ($list === null) continue;
  172.  
  173. if (count($list) > 0) {
  174. $has_moderation = true;
  175. break;
  176. }
  177. }
  178.  
  179. if (!$has_moderation) {
  180. return '';
  181. }
  182.  
  183. $view = new View('sprout/admin/dashboard/moderation_button');
  184. return $view->render();
  185. }
  186.  
  187. }