silverstripe /
silverstripe-registry
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\Registry; |
||
| 4 | |||
| 5 | use SilverStripe\Admin\ModelAdmin; |
||
| 6 | use SilverStripe\Core\ClassInfo; |
||
| 7 | use SilverStripe\ORM\DataObject; |
||
| 8 | |||
| 9 | class RegistryAdmin extends ModelAdmin |
||
| 10 | { |
||
| 11 | private static $url_segment = 'registry'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 12 | |||
| 13 | private static $menu_title = 'Registry'; |
||
|
0 ignored issues
–
show
|
|||
| 14 | |||
| 15 | /** |
||
| 16 | * Hide the registry section completely if we have no registries to manage. |
||
| 17 | * |
||
| 18 | * {@inheritDoc} |
||
| 19 | */ |
||
| 20 | public function canView($member = null) |
||
| 21 | { |
||
| 22 | $managedModels = $this->getManagedModels(); |
||
| 23 | if (count($managedModels) == 0) { |
||
| 24 | return false; |
||
| 25 | } |
||
| 26 | |||
| 27 | return parent::canView($member); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function getManagedModels() |
||
| 31 | { |
||
| 32 | $models = ClassInfo::implementorsOf(RegistryDataInterface::class); |
||
| 33 | |||
| 34 | foreach ($models as $alias => $className) { |
||
| 35 | $models[$className] = [ |
||
| 36 | 'title' => singleton($className)->i18n_singular_name(), |
||
| 37 | ]; |
||
| 38 | unset($models[$alias]); |
||
| 39 | } |
||
| 40 | |||
| 41 | return $models; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function getExportFields() |
||
| 45 | { |
||
| 46 | $fields = []; |
||
| 47 | foreach (singleton($this->modelClass)->summaryFields() as $field => $spec) { |
||
| 48 | $fields[$field] = $field; |
||
| 49 | } |
||
| 50 | return $fields; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Gets a unique filename to use for importing the uploaded CSV data |
||
| 55 | * |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getCsvImportFilename() |
||
| 59 | { |
||
| 60 | $feed = RegistryImportFeed::singleton(); |
||
| 61 | |||
| 62 | return sprintf('%s/%s', $feed->getStoragePath($this->modelClass), $feed->getImportFilename()); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function import($data, $form, $request) |
||
| 66 | { |
||
| 67 | if (!$this->showImportForm |
||
| 68 | || (is_array($this->showImportForm) && !in_array($this->modelClass, $this->showImportForm)) |
||
| 69 | ) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | $importers = $this->getModelImporters(); |
||
| 74 | $loader = $importers[$this->modelClass]; |
||
| 75 | |||
| 76 | $fileContents = !empty($data['_CsvFile']['tmp_name']) ? file_get_contents($data['_CsvFile']['tmp_name']) : ''; |
||
| 77 | // File wasn't properly uploaded, show a reminder to the user |
||
| 78 | if (!$fileContents) { |
||
| 79 | $form->sessionMessage( |
||
| 80 | _t('SilverStripe\\Admin\\ModelAdmin.NOCSVFILE', 'Please browse for a CSV file to import'), |
||
| 81 | 'bad' |
||
| 82 | ); |
||
| 83 | $this->redirectBack(); |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!empty($data['EmptyBeforeImport']) && $data['EmptyBeforeImport']) { //clear database before import |
||
| 88 | $loader->deleteExistingRecords = true; |
||
| 89 | } |
||
| 90 | |||
| 91 | $results = $loader->load($data['_CsvFile']['tmp_name']); |
||
| 92 | |||
| 93 | // copy the uploaded file into the export path |
||
| 94 | RegistryImportFeed::singleton() |
||
| 95 | ->getAssetHandler() |
||
| 96 | ->setContent($this->getCsvImportFilename(), $fileContents); |
||
| 97 | |||
| 98 | $message = ''; |
||
| 99 | if ($results->CreatedCount()) { |
||
| 100 | $message .= _t( |
||
| 101 | 'SilverStripe\\Admin\\ModelAdmin.IMPORTEDRECORDS', |
||
| 102 | "Imported {count} records.", |
||
| 103 | ['count' => $results->CreatedCount()] |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | if ($results->UpdatedCount()) { |
||
| 107 | $message .= _t( |
||
| 108 | 'SilverStripe\\Admin\\ModelAdmin.UPDATEDRECORDS', |
||
| 109 | "Updated {count} records.", |
||
| 110 | ['count' => $results->UpdatedCount()] |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | if ($results->DeletedCount()) { |
||
| 114 | $message .= _t( |
||
| 115 | 'SilverStripe\\Admin\\ModelAdmin.DELETEDRECORDS', |
||
| 116 | "Deleted {count} records.", |
||
| 117 | ['count' => $results->DeletedCount()] |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | if (!$results->CreatedCount() && !$results->UpdatedCount()) { |
||
| 121 | $message .= _t('SilverStripe\\Admin\\ModelAdmin.NOIMPORT', "Nothing to import"); |
||
| 122 | } |
||
| 123 | |||
| 124 | $form->sessionMessage($message, 'good'); |
||
| 125 | $this->redirectBack(); |
||
| 126 | } |
||
| 127 | } |
||
| 128 |