Source of file SDLTAdminCommon.php
Size: 7,957 Bytes - Last Modified: 2021-12-23T10:08:54+00:00
/var/www/docs.ssmods.com/process/src/src/Trait/SDLTAdminCommon.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 | <?php /** * This file contains the "SDLTAdminCommon" trait. * * @category SilverStripe_Project * @package SDLT * @author Catalyst I.T. SilverStripe Team 2019 <silverstripedev@catalyst.net.nz> * @copyright NZ Transport Agency * @license BSD-3 * @link https://www.catalyst.net.nz */ namespace NZTA\SDLT\Traits; use SilverStripe\Core\Config\Configurable; use SilverStripe\Security\Security; use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\FileField; use SilverStripe\Forms\FormAction; use SilverStripe\ORM\ValidationResult; use Swaggest\JsonSchema\Schema as JSONSchema; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\CheckboxField; trait SDLTAdminCommon { use Configurable; /** * Default location of task schema, allow dev to override for alternative schema * * @var string */ private static $json_schema_task = '/src/ImportJsonSchema/Schema/TaskSchema.json'; /** * Default location of questionnaire schema, allow dev to override for alternative schema * * @var string */ private static $json_schema_questionnaire = '/src/ImportJsonSchema/Schema/QuestionnaireSchema.json'; /** * Default location of security component schema, allow dev to override for alternative schema * * @var string */ private static $json_schema_security_component = '/src/ImportJsonSchema/Schema/SecurityComponentSchema.json'; /** * @var array */ private static $allowed_actions = array( 'ImportJsonForm', 'ImportForm', 'SearchForm' ); /** * Allow individual data-models to declare independent config for export fields * without polluting what appears in a GridField via $summary_fields. * * @return array */ public function getExportFields(): array { $summaryFields = $this->modelClass::config()->get('summary_fields') ?? []; $extraFields = $this->modelClass::config()->get('extra_export_fields') ?? []; if ($extraFields) { return array_merge($summaryFields, $extraFields); } return $summaryFields; } /** * Generate a CSV import form for a single {@link DataObject} subclass. * * @return Form|false */ public function ImportJsonForm() { $modelSNG = singleton($this->modelClass); $modelName = $modelSNG->i18n_singular_name(); // check if a import form should be generated if (!$this->showImportForm || (is_array($this->showImportForm) && !in_array($this->modelClass, $this->showImportForm)) ) { return false; } $importers = $this->getModelImporters(); if (!$importers || !isset($importers[$this->modelClass])) { return false; } if (!$modelSNG->canCreate(Security::getCurrentUser())) { return false; } $fields = FieldList::create( HiddenField::create('ClassName', false, $this->modelClass), FileField::create('_JsonFile', false) ->setAllowedExtensions(['json', 'Json', 'JSON']) ); if ($modelSNG->config()->show_overwrite_for_json_import) { $fields->push( CheckboxField::create( 'Overwrite', 'Overwrite an existing '. strtolower($modelName) . ' of the same name', false ) ); } $actions = new FieldList( FormAction::create('importJson', 'Import from Json') ->addExtraClass('btn btn-outline-secondary font-icon-upload') ); $form = new Form( $this, "ImportJsonForm", $fields, $actions ); return $form; } /** * Imports the submitted json file based on specifications given in * {@link self::model_importers}. * Redirects back with a success/failure message. * * @param array $data form data * @param Form $form form * @param HTTPRequest $request request * * @return bool|HTTPResponse */ public function importJson($data, $form, $request) { if (!$this->showImportForm || (is_array($this->showImportForm) && !in_array($this->modelClass, $this->showImportForm)) ) { return false; } // File wasn't properly uploaded, show a reminder to the user if (empty($_FILES['_JsonFile']['tmp_name']) || file_get_contents($_FILES['_JsonFile']['tmp_name']) == '' ) { $form->sessionMessage( 'Please browse for a Json file to import.', ValidationResult::TYPE_ERROR ); $this->redirectBack(); return false; } $overwrite = false; if (!empty($data['Overwrite']) && $data['Overwrite']) { $overwrite = true; } $schemaJson = $this->getSchemaJson(); $schema = JSONSchema::import(json_decode($schemaJson)); $incomingJsonString = file_get_contents($_FILES['_JsonFile']['tmp_name']); try { $incomingJson = (json_decode($incomingJsonString)); //check incoming json schema $schema->in($incomingJson); // Logic: save object $this->LoadJson($incomingJson, $overwrite); // return success message $form->sessionMessage( "Record created successfully.", 'good' ); return $this->redirectBack(); } catch (\Exception $e) { $form->sessionMessage( sprintf( 'Invalid JSON according to schema. Validator said: %s', $e->getMessage() ), ValidationResult::TYPE_ERROR ); $this->redirectBack(); return false; } } /** * get the json schema content for the curret model * * @return string; */ public function getSchemaJson() { $schemaJson = ''; $sdltFrameworkPath = BASE_PATH . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR .'nzta' . DIRECTORY_SEPARATOR .'sdlt-framework'; switch ($this->ImportClass) { case 'Questionnaire': $pathToSchema = $sdltFrameworkPath . $this->config()->json_schema_questionnaire; $schemaJson = file_get_contents($pathToSchema); break; case 'Task': $pathToSchema = $sdltFrameworkPath . $this->config()->json_schema_task; $schemaJson = file_get_contents($pathToSchema); break; case 'Security Component': $pathToSchema = $sdltFrameworkPath . $this->config()->json_schema_security_component; $schemaJson = file_get_contents($pathToSchema); break; } return $schemaJson; } /** * LoadJson : create record from json * * @param object $incomingJson incoming json * @param boolean $overwrite overwrite the existing record * @return void */ public function LoadJson($incomingJson, $overwrite) { if ($this->ImportClass == 'Security Component') { $this->modelClass::create_record_from_json($incomingJson); } else { $this->modelClass::create_record_from_json($incomingJson, $overwrite); } } /** * get current class name * * @return string; */ public function getImportClass() { $importClass = ''; $modelSNG = singleton($this->modelClass); $importClass = $modelSNG->i18n_singular_name(); return $importClass; } } |