|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_encrypt; |
| 4 | + |
| 5 | +use Drupal\Core\Entity\EntityInterface; |
| 6 | +use Drupal\Core\Session\AccountInterface; |
| 7 | +use Drupal\webform\WebformInterface; |
| 8 | +use Drupal\webform_encrypt\WebformEncryptSubmissionStorage; |
| 9 | +use Drupal\webform_revisions\Controller\WebformRevisionsController; |
| 10 | + |
| 11 | +/** |
| 12 | + * This class extension WebformEncryptSubmissionStorage. |
| 13 | + * |
| 14 | + * This is to enabled encryption and decryption for data and checks if webform |
| 15 | + * revisions are enabled and runs the same code (copied here as multiple |
| 16 | + * inherits is not a thing in PHP). |
| 17 | + * |
| 18 | + * So the getColumns is an moduleExists check and the rest is copied from |
| 19 | + * webform revision storage class. |
| 20 | + */ |
| 21 | +class WebformOs2FormsEncryptSubmissionStorage extends WebformEncryptSubmissionStorage { |
| 22 | + |
| 23 | + /** |
| 24 | + * {@inheritdoc} |
| 25 | + */ |
| 26 | + public function getColumns(WebformInterface $webform = NULL, EntityInterface $source_entity = NULL, AccountInterface $account = NULL, $include_elements = TRUE) { |
| 27 | + if (!\Drupal::moduleHandler()->moduleExists('webform_revisions')) { |
| 28 | + return parent::getColumns($webform, $source_entity, $account, $include_elements); |
| 29 | + } |
| 30 | + else { |
| 31 | + $view_any = $webform && $webform->access('submission_view_any'); |
| 32 | + |
| 33 | + $columns = []; |
| 34 | + |
| 35 | + // Serial number. |
| 36 | + $columns['serial'] = [ |
| 37 | + 'title' => $this->t('#'), |
| 38 | + ]; |
| 39 | + |
| 40 | + // Submission ID. |
| 41 | + $columns['sid'] = [ |
| 42 | + 'title' => $this->t('SID'), |
| 43 | + ]; |
| 44 | + |
| 45 | + // Submission label. |
| 46 | + $columns['label'] = [ |
| 47 | + 'title' => $this->t('Submission title'), |
| 48 | + 'sort' => FALSE, |
| 49 | + ]; |
| 50 | + |
| 51 | + // UUID. |
| 52 | + $columns['uuid'] = [ |
| 53 | + 'title' => $this->t('UUID'), |
| 54 | + ]; |
| 55 | + |
| 56 | + // Draft. |
| 57 | + $columns['in_draft'] = [ |
| 58 | + 'title' => $this->t('In draft'), |
| 59 | + ]; |
| 60 | + |
| 61 | + if (empty($account)) { |
| 62 | + // Sticky (Starred/Unstarred). |
| 63 | + $columns['sticky'] = [ |
| 64 | + 'title' => $this->t('Starred'), |
| 65 | + ]; |
| 66 | + |
| 67 | + // Locked. |
| 68 | + $columns['locked'] = [ |
| 69 | + 'title' => $this->t('Locked'), |
| 70 | + ]; |
| 71 | + |
| 72 | + // Notes. |
| 73 | + $columns['notes'] = [ |
| 74 | + 'title' => $this->t('Notes'), |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + // Created. |
| 79 | + $columns['created'] = [ |
| 80 | + 'title' => $this->t('Created'), |
| 81 | + ]; |
| 82 | + |
| 83 | + // Completed. |
| 84 | + $columns['completed'] = [ |
| 85 | + 'title' => $this->t('Completed'), |
| 86 | + ]; |
| 87 | + |
| 88 | + // Changed. |
| 89 | + $columns['changed'] = [ |
| 90 | + 'title' => $this->t('Changed'), |
| 91 | + ]; |
| 92 | + |
| 93 | + // Source entity. |
| 94 | + if ($view_any && empty($source_entity)) { |
| 95 | + $columns['entity'] = [ |
| 96 | + 'title' => $this->t('Submitted to'), |
| 97 | + 'sort' => FALSE, |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + // Submitted by. |
| 102 | + if (empty($account)) { |
| 103 | + $columns['uid'] = [ |
| 104 | + 'title' => $this->t('User'), |
| 105 | + ]; |
| 106 | + } |
| 107 | + |
| 108 | + // Submission language. |
| 109 | + if ($view_any && \Drupal::moduleHandler()->moduleExists('language')) { |
| 110 | + $columns['langcode'] = [ |
| 111 | + 'title' => $this->t('Language'), |
| 112 | + ]; |
| 113 | + } |
| 114 | + |
| 115 | + // Remote address. |
| 116 | + $columns['remote_addr'] = [ |
| 117 | + 'title' => $this->t('IP address'), |
| 118 | + ]; |
| 119 | + |
| 120 | + // Webform and source entity for entity.webform_submission.collection. |
| 121 | + // @see /admin/structure/webform/submissions/manage |
| 122 | + if (empty($webform) && empty($source_entity)) { |
| 123 | + $columns['webform_id'] = [ |
| 124 | + 'title' => $this->t('Webform'), |
| 125 | + ]; |
| 126 | + $columns['entity'] = [ |
| 127 | + 'title' => $this->t('Submitted to'), |
| 128 | + 'sort' => FALSE, |
| 129 | + ]; |
| 130 | + } |
| 131 | + |
| 132 | + // Webform elements. |
| 133 | + if ($webform && $include_elements) { |
| 134 | + /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */ |
| 135 | + $element_manager = \Drupal::service('plugin.manager.webform.element'); |
| 136 | + $content_entity_id = $webform->getContentEntityID(); |
| 137 | + $revision_ids = $this->database->query( |
| 138 | + 'SELECT revision FROM {config_entity_revisions_revision} WHERE id = :id', |
| 139 | + [':id' => $content_entity_id] |
| 140 | + )->fetchCol(); |
| 141 | + if (!$revision_ids) { |
| 142 | + return parent::getColumns($webform, $source_entity, $account, $include_elements); |
| 143 | + } |
| 144 | + |
| 145 | + foreach ($revision_ids as $revision_id) { |
| 146 | + $revisionController = WebformRevisionsController::create(\Drupal::getContainer()); |
| 147 | + $webform = $revisionController->loadConfigEntityRevision($revision_id, $webform->id()); |
| 148 | + $elements = $webform->getElementsInitializedFlattenedAndHasValue('view'); |
| 149 | + foreach ($elements as $element) { |
| 150 | + /** @var \Drupal\webform\Plugin\WebformElementInterface $element_plugin */ |
| 151 | + $element_plugin = $element_manager->createInstance($element['#type']); |
| 152 | + // Replace tokens which can be used in an element's #title. |
| 153 | + $element_plugin->replaceTokens($element, $webform); |
| 154 | + $columns += $element_plugin->getTableColumn($element); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Operations. |
| 160 | + $columns['operations'] = [ |
| 161 | + 'title' => $this->t('Operations'), |
| 162 | + 'sort' => FALSE, |
| 163 | + ]; |
| 164 | + |
| 165 | + // Add name and format to all columns. |
| 166 | + foreach ($columns as $name => &$column) { |
| 167 | + $column['name'] = $name; |
| 168 | + $column['format'] = 'value'; |
| 169 | + } |
| 170 | + |
| 171 | + return $columns; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments