|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Assets; |
| 4 | + |
| 5 | +use ProcessMaker\Managers\ExportManager; |
| 6 | +use ProcessMaker\Models\Screen; |
| 7 | +use Illuminate\Support\Arr; |
| 8 | + |
| 9 | +class DataSourcesInScreen |
| 10 | +{ |
| 11 | + public $type = 'ProcessMaker\Packages\Connectors\DataSources\Models\DataSource'; |
| 12 | + |
| 13 | + public $owner = Screen::class; |
| 14 | + |
| 15 | + /** |
| 16 | + * Get the data sources (ex. watchers) used in a screen |
| 17 | + * |
| 18 | + * @param Screen $screen |
| 19 | + * @param array $dataSources |
| 20 | + * |
| 21 | + * @return array |
| 22 | + */ |
| 23 | + public function referencesToExport(Screen $screen, array $dataSources = []) |
| 24 | + { |
| 25 | + $watchers = $screen->watchers; |
| 26 | + if (is_array($watchers)) { |
| 27 | + $this->findInArray($watchers, function ($item) use (&$dataSources) { |
| 28 | + if (is_array($item)) { |
| 29 | + $scriptId = (string) ($item['script_id'] ?? ''); |
| 30 | + $id = (string) ($item['script']['id'] ?? ''); |
| 31 | + if (str_starts_with($scriptId, 'data_source-') || str_starts_with($id, 'data_source-')) { |
| 32 | + $numericId = str_replace('data_source-', '', str_starts_with($scriptId, 'data_source-') ? $scriptId : $id); |
| 33 | + if (is_numeric($numericId)) { |
| 34 | + $dataSources[] = [$this->type, (int) $numericId]; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + $config = $screen->versionFor(null)->config; |
| 42 | + if (is_array($config)) { |
| 43 | + $this->findInArray($config, function ($item) use (&$dataSources) { |
| 44 | + if (is_array($item) && isset($item['component']) && $item['component'] === 'FormSelectList' && !empty($item['config']['options']['selectedDataSource'])) { |
| 45 | + $dataSources[] = [$this->type, (int) $item['config']['options']['selectedDataSource']]; |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + return $dataSources; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Update references used in an imported screen |
| 55 | + * |
| 56 | + * @param Screen $screen |
| 57 | + * @param array $references |
| 58 | + * @param ExportManager $exportManager |
| 59 | + * |
| 60 | + * @return void |
| 61 | + */ |
| 62 | + public function updateReferences(Screen $screen, array $references, ExportManager $exportManager) |
| 63 | + { |
| 64 | + $watches = $screen->watchers; |
| 65 | + if (is_array($watches)) { |
| 66 | + foreach ($watches as &$watcher) { |
| 67 | + $id = (string) ($watcher['script']['id'] ?? ''); |
| 68 | + if (str_starts_with($id, 'data_source-')) { |
| 69 | + $oldRef = str_replace('data_source-', '', $id); |
| 70 | + if (isset($references[$this->type][$oldRef])) { |
| 71 | + $newRef = $references[$this->type][$oldRef]->getKey(); |
| 72 | + } else { |
| 73 | + $newRef = null; |
| 74 | + $exportManager->addLogMessage( |
| 75 | + 'DataSourcesInScreen:references', |
| 76 | + __( |
| 77 | + 'Imported file does not contain the data source #:dataSource assigned to a watcher', |
| 78 | + ['dataSource' => $oldRef] |
| 79 | + ), |
| 80 | + false, |
| 81 | + __("Missing watcher's data source") |
| 82 | + ); |
| 83 | + } |
| 84 | + if ($newRef) { |
| 85 | + $watcher['script_id'] = $newRef; |
| 86 | + $watcher['script']['id'] = "data_source-$newRef"; |
| 87 | + $watcher['script']['title'] = $references[$this->type][$oldRef]->name; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + $screen->watchers = $watches; |
| 93 | + |
| 94 | + $config = $screen->config; |
| 95 | + if (is_array($config)) { |
| 96 | + $this->findInArray($config, function ($item, $keyDotNotation) use ($references, &$config) { |
| 97 | + if (is_array($item) && isset($item['component']) && $item['component'] === 'FormSelectList' && !empty($item['config']['options']['selectedDataSource'])) { |
| 98 | + $oldRef = $item['config']['options']['selectedDataSource']; |
| 99 | + if (isset($references[$this->type][$oldRef])) { |
| 100 | + $newRef = $references[$this->type][$oldRef]->getKey(); |
| 101 | + Arr::set($config, "$keyDotNotation.config.options.selectedDataSource", $newRef); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + $screen->config = $config; |
| 106 | + } |
| 107 | + |
| 108 | + $screen->save(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Find recursively in an array |
| 113 | + * |
| 114 | + * @param array $array |
| 115 | + * @param callable $callback |
| 116 | + * @param array $path |
| 117 | + * |
| 118 | + * @return void |
| 119 | + */ |
| 120 | + private function findInArray(array $array, callable $callback, array $path = []) |
| 121 | + { |
| 122 | + call_user_func($callback, $array, implode('.', $path)); |
| 123 | + foreach ($array as $key => $item) { |
| 124 | + if (is_array($item)) { |
| 125 | + $this->findInArray($item, $callback, array_merge($path, [$key])); |
| 126 | + } else { |
| 127 | + call_user_func($callback, $item, implode('.', array_merge($path, [$key]))); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments