sunnysideup /
silverstripe-templateoverview
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Sunnysideup\TemplateOverview\Api; |
||
| 4 | |||
| 5 | use SilverStripe\Admin\CMSMenu; |
||
| 6 | use SilverStripe\Control\Director; |
||
| 7 | use SilverStripe\Core\Injector\Injector; |
||
| 8 | use SilverStripe\ORM\DB; |
||
| 9 | use SilverStripe\Versioned\Versioned; |
||
| 10 | use Sunnysideup\TemplateOverview\Api\Providers\AllLinksArchiveAdmin; |
||
| 11 | use Sunnysideup\TemplateOverview\Api\Providers\AllLinksControllerInfo; |
||
| 12 | use Sunnysideup\TemplateOverview\Api\Providers\AllLinksDataObjects; |
||
| 13 | use Sunnysideup\TemplateOverview\Api\Providers\AllLinksModelAdmin; |
||
| 14 | use Sunnysideup\TemplateOverview\Api\Providers\AllLinksReports; |
||
| 15 | |||
| 16 | class AllLinks extends AllLinksProviderBase |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var mixed|mixed[] |
||
| 20 | */ |
||
| 21 | public $archiveCMSLinks; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $allNonCMSLinks = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $pagesOnFrontEnd = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $dataObjectsOnFrontEnd = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $otherControllerMethods = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $customNonCMSLinks = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $templateoverviewtestsLinks = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $allCMSLinks = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $pagesInCMS = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $dataObjectsInCMS = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $modelAdmins = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $reportLinks = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $leftAndMainLnks = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $customCMSLinks = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * url snippets that if found in links should exclude the link altogether. |
||
| 90 | * e.g. 'admin/registry'. |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private static $exclude_list = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | private static $number_of_examples = 1; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | private static $custom_links = [ |
||
| 105 | 'Security/login', |
||
| 106 | 'Security/logout', |
||
| 107 | 'Security/lostpassword', |
||
| 108 | 'Security/lostpassword/passwordsent', |
||
| 109 | ]; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | private static $controller_name_space_filter = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $link |
||
| 118 | */ |
||
| 119 | public static function is_admin_link(string $link): bool |
||
| 120 | { |
||
| 121 | return 'admin' === substr(ltrim((string) $link, '/'), 0, 5); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Sanitise a model class' name for inclusion in a link. |
||
| 126 | * |
||
| 127 | * @param string $class |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public static function sanitise_class_name($class) |
||
| 132 | { |
||
| 133 | return str_replace('\\', '-', $class); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * returns an array of allNonCMSLinks => [] , allCMSLinks => [], otherControllerMethods => []. |
||
| 138 | */ |
||
| 139 | public function getAllLinks(): array |
||
| 140 | { |
||
| 141 | $array1 = $this->Config()->get('custom_links'); |
||
| 142 | $array2 = []; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 143 | $array2 = $this->getCustomisedLinks(); |
||
| 144 | foreach (array_merge($array1, $array2) as $link) { |
||
| 145 | $link = '/' . ltrim($link, '/') . '/'; |
||
| 146 | if (self::is_admin_link($link)) { |
||
| 147 | $this->customCMSLinks[] = $link; |
||
| 148 | } else { |
||
| 149 | $this->customNonCMSLinks[] = $link; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $this->pagesOnFrontEnd = $this->ListOfPagesLinks(); |
||
| 154 | $this->dataObjectsOnFrontEnd = $this->ListOfDataObjectsLinks(false); |
||
| 155 | $this->templateoverviewtestsLinks = $this->ListOfAllTemplateoverviewtestsLinks(); |
||
| 156 | |||
| 157 | $this->allNonCMSLinks = $this->addToArrayOfLinks($this->allNonCMSLinks, $this->pagesOnFrontEnd); |
||
| 158 | $this->allNonCMSLinks = $this->addToArrayOfLinks($this->allNonCMSLinks, $this->dataObjectsOnFrontEnd); |
||
| 159 | $this->allNonCMSLinks = $this->addToArrayOfLinks($this->allNonCMSLinks, $this->customNonCMSLinks); |
||
| 160 | $this->allNonCMSLinks = $this->addToArrayOfLinks($this->allNonCMSLinks, $this->templateoverviewtestsLinks); |
||
| 161 | sort($this->allNonCMSLinks); |
||
| 162 | |||
| 163 | $this->pagesInCMS = $this->ListOfPagesLinks(true); |
||
| 164 | $this->dataObjectsInCMS = $this->ListOfDataObjectsLinks(true); |
||
| 165 | $this->modelAdmins = $this->ListOfAllModelAdmins(); |
||
| 166 | $this->archiveCMSLinks = $this->ListOfAllArchiveCMSLinks(); |
||
| 167 | $this->leftAndMainLnks = $this->ListOfAllLeftAndMains(); |
||
| 168 | $this->reportLinks = $this->listOfAllReports(); |
||
| 169 | |||
| 170 | $this->allCMSLinks = $this->addToArrayOfLinks($this->allCMSLinks, $this->pagesInCMS); |
||
| 171 | $this->allCMSLinks = $this->addToArrayOfLinks($this->allCMSLinks, $this->dataObjectsInCMS); |
||
| 172 | $this->allCMSLinks = $this->addToArrayOfLinks($this->allCMSLinks, $this->modelAdmins); |
||
| 173 | $this->allCMSLinks = $this->addToArrayOfLinks($this->allCMSLinks, $this->archiveCMSLinks); |
||
| 174 | $this->allCMSLinks = $this->addToArrayOfLinks($this->allCMSLinks, $this->leftAndMainLnks); |
||
| 175 | $this->allCMSLinks = $this->addToArrayOfLinks($this->allCMSLinks, $this->reportLinks); |
||
| 176 | $this->allCMSLinks = $this->addToArrayOfLinks($this->allCMSLinks, $this->customCMSLinks); |
||
| 177 | sort($this->allCMSLinks); |
||
| 178 | |||
| 179 | $this->otherControllerMethods = $this->ListOfAllControllerMethods(); |
||
| 180 | |||
| 181 | return [ |
||
| 182 | 'allNonCMSLinks' => $this->allNonCMSLinks, |
||
| 183 | 'allCMSLinks' => $this->allCMSLinks, |
||
| 184 | 'otherLinks' => $this->otherControllerMethods, |
||
| 185 | ]; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * returns a list of all model admin links. |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function ListOfAllModelAdmins() |
||
| 194 | { |
||
| 195 | $obj = Injector::inst()->get(AllLinksModelAdmin::class); |
||
| 196 | $obj->setNumberOfExamples($this->Config()->number_of_examples); |
||
| 197 | |||
| 198 | return $obj->getAllLinksInner(); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * returns a list of all archive links. |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function ListOfAllArchiveCMSLinks() |
||
| 207 | { |
||
| 208 | $obj = Injector::inst()->get(AllLinksArchiveAdmin::class); |
||
| 209 | $obj->setNumberOfExamples($this->Config()->number_of_examples); |
||
| 210 | |||
| 211 | return $obj->getAllLinksInner(); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function getCustomisedLinks(): array |
||
| 215 | { |
||
| 216 | $obj = Injector::inst()->get(AllLinksControllerInfo::class); |
||
| 217 | |||
| 218 | return $obj->getCustomisedLinks(); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function ListOfAllControllerMethods(): array |
||
| 222 | { |
||
| 223 | $obj = Injector::inst()->get(AllLinksControllerInfo::class); |
||
| 224 | $obj->setValidNameSpaces($this->Config()->controller_name_space_filter); |
||
| 225 | |||
| 226 | return $obj->getAllLinksInner(); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function ListOfAllTemplateoverviewtestsLinks(): array |
||
| 230 | { |
||
| 231 | $obj = Injector::inst()->get(AllLinksControllerInfo::class); |
||
| 232 | $list = $obj->getAllLinksInner(); |
||
|
0 ignored issues
–
show
|
|||
| 233 | $list = $obj->getLinksAndActions(); |
||
| 234 | |||
| 235 | return array_keys($list['CustomLinks'] ?? []); |
||
| 236 | } |
||
| 237 | |||
| 238 | public function ListOfDataObjectsLinks(bool $inCMS): array |
||
| 239 | { |
||
| 240 | $obj = Injector::inst()->get(AllLinksDataObjects::class); |
||
| 241 | $obj->setNumberOfExamples($this->Config()->number_of_examples); |
||
| 242 | |||
| 243 | return $obj->getAllLinksInner($inCMS); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Takes {@link #$classNames}, gets the URL of the first instance of it |
||
| 248 | * (will exclude extensions of the class) and |
||
| 249 | * appends to the {@link #$urls} list to be checked. |
||
| 250 | * |
||
| 251 | * @param bool $pageInCMS |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function ListOfPagesLinks($pageInCMS = false) |
||
| 256 | { |
||
| 257 | //first() will return null or the object |
||
| 258 | $return = []; |
||
| 259 | $siteTreeClassNames = $this->getListOfAllClasses(); |
||
| 260 | foreach ($siteTreeClassNames as $class) { |
||
| 261 | for ($i = 0; $i < $this->Config()->number_of_examples; ++$i) { |
||
| 262 | $excludedClasses = $this->arrayExcept($siteTreeClassNames, $class); |
||
| 263 | $page = Versioned::get_by_stage($class, Versioned::LIVE) |
||
| 264 | ->exclude(['ClassName' => $excludedClasses]) |
||
| 265 | ->sort(DB::get_conn()->random() . ' ASC') |
||
| 266 | ->first(); |
||
| 267 | if (!$page instanceof \SilverStripe\ORM\DataObject) { |
||
| 268 | $page = Versioned::get_by_stage($class, Versioned::DRAFT) |
||
| 269 | ->exclude(['ClassName' => $excludedClasses]) |
||
| 270 | ->sort(DB::get_conn()->random() . ' ASC') |
||
| 271 | ->first(); |
||
| 272 | } |
||
| 273 | |||
| 274 | if (null !== $page) { |
||
| 275 | if ($pageInCMS) { |
||
| 276 | $url = $page->CMSEditLink(); |
||
| 277 | $return[] = $url; |
||
| 278 | $return[] = str_replace('/edit/', '/settings/', $url); |
||
| 279 | $return[] = str_replace('/edit/', '/history/', $url); |
||
| 280 | } else { |
||
| 281 | $url = $page->Link(); |
||
| 282 | $return[] = $url; |
||
| 283 | } |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | return $return; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function ListOfAllLeftAndMains() |
||
| 295 | { |
||
| 296 | //first() will return null or the object |
||
| 297 | $return = []; |
||
| 298 | $list = CMSMenu::get_cms_classes(); |
||
| 299 | foreach ($list as $class) { |
||
| 300 | if ($this->isValidClass($class)) { |
||
| 301 | $obj = Injector::inst()->get($class); |
||
| 302 | if ($obj) { |
||
| 303 | $url = $obj->Link(); |
||
| 304 | if ($url) { |
||
| 305 | $return[] = $url; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | return $return; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * returns a list of all reports. |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function ListOfAllReports() |
||
| 320 | { |
||
| 321 | $reportsLinks = Injector::inst()->get(AllLinksReports::class); |
||
| 322 | |||
| 323 | return $reportsLinks->getAllLinksInner(); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Pushes an array of items to an array. |
||
| 328 | * |
||
| 329 | * @param array $array Array to push items to (will overwrite) |
||
| 330 | * @param array $pushArray array of items to push to $array |
||
| 331 | */ |
||
| 332 | protected function addToArrayOfLinks($array, $pushArray): array |
||
| 333 | { |
||
| 334 | $excludeList = $this->Config()->exclude_list; |
||
| 335 | foreach ($pushArray as $pushItem) { |
||
| 336 | if ($pushItem) { |
||
| 337 | //clean |
||
| 338 | if (self::is_admin_link($pushItem)) { |
||
| 339 | $pushItem = str_replace('?stage=Stage', '', (string) $pushItem); |
||
| 340 | } |
||
| 341 | |||
| 342 | $pushItem = self::sanitise_class_name($pushItem); |
||
| 343 | $pushItem = '/' . Director::makeRelative($pushItem); |
||
| 344 | //is it a file? |
||
| 345 | if (strpos($pushItem, '.') > (strlen( (string) $pushItem) - 6)) { |
||
| 346 | $pushItem = rtrim($pushItem, '/'); |
||
| 347 | } |
||
| 348 | |||
| 349 | if ('' !== $pushItem) { |
||
| 350 | if (!empty($excludeList)) { |
||
| 351 | foreach ($excludeList as $excludeItem) { |
||
| 352 | if (false !== stripos((string) $pushItem, $excludeItem)) { |
||
| 353 | continue 2; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | if (!in_array($pushItem, $array, true)) { |
||
| 359 | $array[] = $pushItem; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | return $array; |
||
| 366 | } |
||
| 367 | } |
||
| 368 |