Source of file HealthCheckProvider.php
Size: 10,467 Bytes - Last Modified: 2021-12-23T10:41:57+00:00
/var/www/docs.ssmods.com/process/src/src/Model/HealthCheckProvider.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 | <?php namespace Sunnysideup\HealthCheckProvider\Model; use SilverStripe\Control\Director; use SilverStripe\Forms\CheckboxSetField; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; use SilverStripe\Forms\HTMLReadonlyField; use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\ReadonlyField; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Member; use SilverStripe\Security\Security; class HealthCheckProvider extends DataObject { /** * @var string */ private const VIEW_URL = 'https://check.silverstripe-webdevelopment.com/report/view/'; /** * @var string */ private const NOT_PROVIDED_PHRASE = 'not provided'; protected $cacheForData = []; ####################### ### Names Section ####################### private static $singular_name = 'Report'; private static $plural_name = 'Reports'; private static $table_name = 'HealthCheckProvider'; ####################### ### Model Section ####################### private static $db = [ 'MainUrl' => 'Varchar(255)', 'OtherUrls' => 'Text', 'SendNow' => 'Boolean', 'Sent' => 'Boolean', 'SendCode' => 'Varchar(125)', 'ResponseCode' => 'Varchar(125)', 'HasError' => 'Boolean', 'Data' => 'Text', ]; private static $has_one = [ 'Editor' => Member::class, ]; private static $many_many = [ 'HealthCheckItemProviders' => HealthCheckItemProvider::class, ]; ####################### ### Further DB Field Details ####################### private static $indexes = [ 'SendCode' => true, 'ResponseCode' => true, ]; private static $default_sort = [ 'Created' => 'DESC', ]; ####################### ### Field Names and Presentation Section ####################### private static $field_labels = [ 'SendNow' => 'Send now?', 'Sent' => 'Has been sent', 'HealthCheckItemProviders' => 'Pieces of Info', ]; private static $summary_fields = [ 'Title' => 'Health Report Data', 'Sent.Nice' => 'Sent', 'HasError.Nice' => 'Error', 'Editor.Title' => 'Editor', ]; ####################### ### Casting Section ####################### private static $casting = [ 'Title' => 'Varchar', ]; private $checkLoop = 0; /** * casted variable */ public function getTitle(): string { $str = 'Health Check for ' . $this->MainUrl; if ($this->Sent) { $str .= '; Sent on ' . $this->dbObject('LastEdited')->Nice(); } else { $str .= '; Not sent yet'; } return $str; } ####################### ### can Section ####################### public function canDelete($member = null) { if ($this->Sent) { return false; } return parent::canDelete($member); } public function canEdit($member = null) { if ($this->Sent) { return false; } return parent::canEdit($member); } ####################### ### write Section ####################### protected function onBeforeWrite() { parent::onBeforeWrite(); if (! $this->EditorID) { $user = Security::getCurrentUser(); if ($user) { $this->EditorID = Security::getCurrentUser()->ID; } } if (! $this->MainUrl) { $this->MainUrl = $this->getSiteURL(); } if ($this->Sent) { $this->HasError = !$this->getCodesMatch(); } } public function getCodesMatch(): bool { if ($this->Sent) { return $this->SendCode === $this->ResponseCode; } return true; } protected function onAfterWrite() { parent::onAfterWrite(); if ($this->checkLoop < 3) { ++$this->checkLoop; if (! $this->SendCode) { foreach (HealthCheckItemProvider::get()->filter(['Include' => true]) as $item) { $this->HealthCheckItemProviders()->add($item); } $data = $this->retrieveDataInnerInner(); if (count($data)) { //send code first because included in data. $this->SendCode = $this->createSendCode(); //create data. $this->Data = json_encode($this->retrieveDataInner()); //loop $this->write(); } } } } ####################### ### CMS Edit Section ####################### public function populateDefaults() { parent::populateDefaults(); $this->MainUrl = $this->getSiteURL(); return $this; } public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName( [ 'SendCode', 'SendNow', 'ResponseCode', 'Sent', 'Data', 'EditorID', ] ); if ($this->exists()) { $fields->removeByName( [ 'HealthCheckItemProviders', ] ); if ($this->Sent) { $viewLink = $this->ViewLink(); if ($viewLink) { $fields->addFieldsToTab( 'Root.Main', [ HTMLReadonlyField::create( 'Link', 'Open report', '<a href="' . $viewLink . '">View Link</a>' ), ] ); } } else { $fields->removeByName( [ 'HasError', ] ); $fields->addFieldsToTab( 'Root.Main', [ CheckboxSetField::create( 'HealthCheckItemProviders', 'Data Points to be Provided', HealthCheckItemProvider::get()->filter(['Include' => true])->map('ID', 'CodeNice') )->setDescription('Please untick any data that you prefer not to provide.'), ] ); } $fields->addFieldsToTab( 'Root.Main', [ TextField::create('MainUrl', 'Main URL'), TextField::create('MainUrl', 'Main URL'), TextField::create('OtherUrls', 'Other Urls') ->setDescription('Separate by comma - e.g. new.mysite.com, otherurl.com, etc ...'), ReadonlyField::create('HasBeenSent', 'Sent', $this->dbObject('Sent')->Nice()), ReadonlyField::create('Editor Email', 'Editor Email', $this->Editor()->Email), ReadonlyField::create('LastEdited'), ] ); $fields->addFieldsToTab( 'Root.Output', [ ReadonlyField::create('SendCode'), ReadonlyField::create('ResponseCode'), LiteralField::create( 'Output', '<h2>Data</h2><pre>' . json_encode(json_decode($this->Data), JSON_PRETTY_PRINT) . '</pre>' ), ] ); $fields->addFieldsToTab( 'Root.PiecesOfInfo', [ GridField::create( 'HealthCheckItemProvidersList', 'Data List', HealthCheckItemProvider::get(), GridFieldConfig_RecordEditor::create() ), ] ); } else { $fields->removeByName( [ 'HasError', ] ); } return $fields; } public function ViewLink() { if ($this->ResponseCode) { return self::VIEW_URL . $this->ResponseCode . '/'; } } protected function hasAnswers(): bool { return (int) $this->HealthCheckItemProviders()->count() > 0; } protected function createSendCode(): string { $array = $this->retrieveDataInnerInner(); $serialized = serialize($array); return hash('ripemd160', $this->ID . $serialized); } /** * Return the host website URL * @return string URL of the host website */ protected function getSiteURL(): string { $base = Director::host(); return rtrim($base, '/'); } ####################### ### Calculations ####################### protected function retrieveDataInner(): array { return [ 'ID' => $this->ID, 'SendCode' => $this->SendCode, 'MainUrl' => $this->MainUrl, 'OtherUrls' => $this->OtherUrls, 'Editor' => [ 'Email' => $this->Editor()->Email, 'FirstName' => $this->Editor()->FirstName, 'Surname' => $this->Editor()->Surname, ], 'Data' => $this->retrieveDataInnerInner(), ]; } protected function retrieveDataInnerInner(): array { if (count($this->cacheForData) === 0) { $this->cacheForData = []; $includeIDList = $this->HealthCheckItemProviders() ->filter(['Include' => true]) ->column('ID'); $list = HealthCheckItemProvider::get(); foreach ($list as $item) { $shortName = $item->getCode(); if (in_array($item->ID, $includeIDList, false)) { $this->cacheForData[$shortName] = $item->findAnswer($this); } else { $this->cacheForData[$shortName] = SELF::NOT_PROVIDED_PHRASE; } } } return $this->cacheForData; } } |