/src/Reports/PagesWithoutReviewScheduleReport.php

https://github.com/silverstripe-labs/silverstripe-contentreview · PHP · 167 lines · 117 code · 22 blank · 28 comment · 13 complexity · aa9d24ba8fafb7a799918a2a353ed219 MD5 · raw file

  1. <?php
  2. namespace SilverStripe\ContentReview\Reports;
  3. use SilverStripe\CMS\Controllers\CMSPageEditController;
  4. use SilverStripe\CMS\Model\SiteTree;
  5. use SilverStripe\CMS\Model\VirtualPage;
  6. use SilverStripe\ContentReview\Compatibility\ContentReviewCompatability;
  7. use SilverStripe\Core\ClassInfo;
  8. use SilverStripe\Forms\CheckboxField;
  9. use SilverStripe\Forms\FieldList;
  10. use SilverStripe\ORM\ArrayList;
  11. use SilverStripe\ORM\DataObject;
  12. use SilverStripe\ORM\SS_List;
  13. use SilverStripe\Reports\Report;
  14. use SilverStripe\SiteConfig\SiteConfig;
  15. use SilverStripe\Versioned\Versioned;
  16. /**
  17. * Show all pages that need to be reviewed.
  18. */
  19. class PagesWithoutReviewScheduleReport extends Report
  20. {
  21. /**
  22. * @return string
  23. */
  24. public function title()
  25. {
  26. return _t(__CLASS__ . ".TITLE", "Pages without a scheduled review.");
  27. }
  28. /**
  29. * @return FieldList
  30. */
  31. public function parameterFields()
  32. {
  33. $params = FieldList::create();
  34. $params->push(CheckboxField::create("ShowVirtualPages", "Show Virtual Pages"));
  35. return $params;
  36. }
  37. /**
  38. * @return array
  39. */
  40. public function columns()
  41. {
  42. $linkBase = singleton(CMSPageEditController::class)->Link("show");
  43. $linkPath = parse_url($linkBase ?? '', PHP_URL_PATH);
  44. $linkQuery = parse_url($linkBase ?? '', PHP_URL_QUERY);
  45. $fields = [
  46. "Title" => [
  47. "title" => "Page name",
  48. "formatting" => "<a href='{$linkPath}/\$ID?{$linkQuery}' title='Edit page'>\$value</a>",
  49. ],
  50. "NextReviewDate" => [
  51. "title" => "Review Date",
  52. "casting" => "Date->Full",
  53. ],
  54. "OwnerNames" => [
  55. "title" => "Owner",
  56. ],
  57. "LastEditedByName" => "Last edited by",
  58. "AbsoluteLink" => [
  59. "title" => "URL",
  60. "formatting" => function ($value, $item) {
  61. $liveLink = $item->AbsoluteLiveLink;
  62. $stageLink = $item->AbsoluteLink();
  63. return sprintf(
  64. "%s <a href='%s'>%s</a>",
  65. $stageLink,
  66. $liveLink ? $liveLink : $stageLink . "?stage=Stage",
  67. $liveLink ? "(live)" : "(draft)"
  68. );
  69. },
  70. ],
  71. "ContentReviewType" => [
  72. "title" => "Settings are",
  73. "formatting" => function ($value, $item) use ($linkPath, $linkQuery) {
  74. if ($item->ContentReviewType == "Inherit") {
  75. $options = $item->getOptions();
  76. if ($options && $options instanceof SiteConfig) {
  77. return "Inherited from <a href='admin/settings'>Settings</a>";
  78. } elseif ($options) {
  79. return sprintf(
  80. "Inherited from <a href='%s/%d?%s'>%s</a>",
  81. $linkPath,
  82. $options->ID,
  83. $linkQuery,
  84. $options->Title
  85. );
  86. }
  87. }
  88. return $value;
  89. },
  90. ],
  91. ];
  92. return $fields;
  93. }
  94. /**
  95. * @param array $params
  96. * @param array|string|null $sort
  97. * @param int|null $limit
  98. *
  99. * @return SS_List
  100. */
  101. public function sourceRecords($params = [], $sort = null, $limit = null)
  102. {
  103. Versioned::set_stage(Versioned::DRAFT);
  104. $records = SiteTree::get();
  105. $compatibility = ContentReviewCompatability::start();
  106. // If there's no review dates set, default to all pages due for review now.
  107. // Show virtual pages?
  108. if (empty($params["ShowVirtualPages"])) {
  109. $virtualPageClasses = ClassInfo::subclassesFor(VirtualPage::class);
  110. $records = $records->where(sprintf(
  111. "\"SiteTree\".\"ClassName\" NOT IN ('%s')",
  112. implode("','", array_values($virtualPageClasses ?? []))
  113. ));
  114. }
  115. // Apply sort and limit if appropriate.
  116. if ($sort !== null) {
  117. $records = $records->sort($sort);
  118. }
  119. if ($limit !== null) {
  120. $records = $records->limit($limit);
  121. }
  122. // Trim out calculated values
  123. $list = $records->filterByCallback(function ($record) {
  124. return !$this->hasReviewSchedule($record);
  125. });
  126. ContentReviewCompatability::done($compatibility);
  127. return $list;
  128. }
  129. /**
  130. * @param DataObject $record
  131. *
  132. * @return bool
  133. */
  134. protected function hasReviewSchedule(DataObject $record)
  135. {
  136. if (!$record->obj("NextReviewDate")->exists()) {
  137. return false;
  138. }
  139. $options = $record->getOptions();
  140. if ($options && $options->OwnerGroups()->count() == 0 && $options->OwnerUsers()->count() == 0) {
  141. return false;
  142. }
  143. return true;
  144. }
  145. }