|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace wcf\system\wsdb\importer; |
| 4 | + |
| 5 | +use wcf\data\wsdb\option\Option; |
| 6 | +use wcf\data\wsdb\option\OptionAction; |
| 7 | +use wcf\system\importer\AbstractImporter; |
| 8 | +use wcf\system\importer\ImportHandler; |
| 9 | +use wcf\system\WCF; |
| 10 | +use wcf\util\JSON; |
| 11 | +use wcf\util\StringUtil; |
| 12 | + |
| 13 | +final class LinkOptionsImporter extends AbstractImporter |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var array<string, string> |
| 17 | + */ |
| 18 | + private array $optionTypeMap = [ |
| 19 | + 'boolean' => 'boolean', |
| 20 | + 'checkboxes' => 'checkboxes', |
| 21 | + 'date' => 'date', |
| 22 | + 'integer' => 'integer', |
| 23 | + 'float' => 'float', |
| 24 | + 'multiSelect' => 'checkboxes', |
| 25 | + 'radioButton' => 'radioButton', |
| 26 | + 'select' => 'select', |
| 27 | + 'text' => 'text', |
| 28 | + 'textarea' => 'textarea', |
| 29 | + 'URL' => 'url', |
| 30 | + ]; |
| 31 | + |
| 32 | + #[\Override] |
| 33 | + public function import($oldID, array $data, array $additionalData = []): int |
| 34 | + { |
| 35 | + $databaseID = ImportHandler::getInstance()->getNewID('dev.hanashi.wsdb.links', 1); |
| 36 | + if (!$databaseID) { |
| 37 | + return 0; |
| 38 | + } |
| 39 | + |
| 40 | + if (!isset($this->optionTypeMap[$data['optionType']])) { |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + $configuration = []; |
| 45 | + if ($data['required']) { |
| 46 | + $configuration['required'] = 1; |
| 47 | + } |
| 48 | + if (!empty($data['selectOptions'])) { |
| 49 | + $selectOptions = []; |
| 50 | + $lines = \explode("\n", StringUtil::unifyNewlines($data['selectOptions'])); |
| 51 | + foreach ($lines as $line) { |
| 52 | + $lineSplitted = \explode(':', $line, 2); |
| 53 | + if (\count($lineSplitted) == 2) { |
| 54 | + $selectOptions[] = [ |
| 55 | + 'key' => $lineSplitted[0], |
| 56 | + 'value' => [$lineSplitted[1]], |
| 57 | + ]; |
| 58 | + } else { |
| 59 | + $selectOptions[] = [ |
| 60 | + 'key' => $lineSplitted[0], |
| 61 | + 'value' => [$lineSplitted[0]], |
| 62 | + ]; |
| 63 | + } |
| 64 | + } |
| 65 | + $configuration['selectOptions'] = JSON::encode($selectOptions); |
| 66 | + } |
| 67 | + |
| 68 | + $action = new OptionAction([], 'create', [ |
| 69 | + 'data' => [ |
| 70 | + 'databaseID' => $databaseID, |
| 71 | + 'showOrder' => $data['showOrder'], |
| 72 | + 'name' => $data['optionTitle'], |
| 73 | + 'description' => $data['optionDescription'], |
| 74 | + 'optionType' => $this->optionTypeMap[$data['optionType']], |
| 75 | + 'configuration' => JSON::encode($configuration), |
| 76 | + 'isDisabled' => $data['isDisabled'], |
| 77 | + 'isLimitedToCategories' => $additionalData['categories'] === [] ? 0 : 1, |
| 78 | + ], |
| 79 | + ]); |
| 80 | + $newOption = $action->executeAction()['returnValues']; |
| 81 | + \assert($newOption instanceof Option); |
| 82 | + |
| 83 | + ImportHandler::getInstance()->saveNewID('dev.hanashi.wsdb.links.option', $oldID, $newOption->optionID); |
| 84 | + |
| 85 | + $sql = "INSERT INTO wcf1_wsdb_option_to_record_category |
| 86 | + (optionID, categoryID) |
| 87 | + VALUES (?, ?)"; |
| 88 | + $statement = WCF::getDB()->prepare($sql); |
| 89 | + |
| 90 | + foreach ($additionalData['categories'] as $categoryID) { |
| 91 | + $newCategoryID = ImportHandler::getInstance()->getNewID('dev.hanashi.wsdb.links.category', $categoryID); |
| 92 | + if (!$newCategoryID) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + $statement->execute([$newOption->optionID, $newCategoryID]); |
| 97 | + } |
| 98 | + |
| 99 | + return $newOption->optionID; |
| 100 | + } |
| 101 | +} |
0 commit comments