31da97b2e04627a7d7e859a50baa0fd4a74f7c96
[yaffs-website] / web / modules / contrib / search_api_synonym / src / Plugin / search_api_synonym / import / CSV.php
1 <?php
2
3 namespace Drupal\search_api_synonym\Plugin\search_api_synonym\import;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Link;
7 use Drupal\Core\Url;
8 use Drupal\file\Entity\File;
9 use Drupal\search_api_synonym\Import\ImportPluginBase;
10 use Drupal\search_api_synonym\Import\ImportPluginInterface;
11
12 /**
13  * Import of CSV files.
14  *
15  * @SearchApiSynonymImport(
16  *   id = "csv",
17  *   label = @Translation("CSV"),
18  *   description = @Translation("Synonym import plugin from CSV / delimited file.")
19  * )
20  */
21 class CSV extends ImportPluginBase implements ImportPluginInterface {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function parseFile(File $file, array $settings = []) {
27     $data = [];
28     $delimiter = $settings['delimiter'];
29     $enclosure = $settings['enclosure'];
30     $header_row = $settings['header_row'];
31     
32     $i = 1;
33     if (($handle = fopen($file->getFileUri(), 'r')) !== FALSE) {
34       while (($row = fgetcsv($handle, 1000, $delimiter, $enclosure)) !== FALSE) {
35         if ($header_row && $i++ == 1) {
36           continue;
37         }
38
39         if (!empty($row[0]) && !empty($row[1])) {
40           $data[] = [
41             'word' => $row[0],
42             'synonym' => $row['1'],
43             'type' => !empty($row['2']) ? $row['2'] : ''
44           ];
45         }
46       }
47       fclose($handle);
48     }
49
50     return $data;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
57     $example_url = 'internal:' . base_path() . drupal_get_path('module', 'search_api_synonym') . '/examples/example.csv';
58     $form['template'] = [
59       '#type' => 'item',
60       '#title' => $this->t('Example'),
61       '#markup' => Link::fromTextAndUrl(t('Download example file'), Url::fromUri($example_url))->toString()
62     ];
63     $form['delimiter'] = [
64       '#type' => 'select',
65       '#title' => t('Delimiter'),
66       '#description' => t('Field delimiter character used in the import file.'),
67       '#options' => [
68         ';' => $this->t('Semicolon'),
69         ',' => $this->t('Comma'),
70         '\t' => $this->t('Tab'),
71         '|' => $this->t('Pipe'),
72       ],
73       '#default_value' => ';',
74       '#required' => TRUE
75     ];
76     $form['enclosure'] = [
77       '#type' => 'select',
78       '#title' => t('Text qualifier'),
79       '#description' => t('Field enclosure character used in import file.'),
80       '#options' => [
81         '"' => '"',
82         "'" => "'",
83         '' => $this->t('None'),
84       ],
85       '#default_value' => '"'
86     ];
87     $form['header_row'] = [
88       '#type' => 'checkbox',
89       '#title' => $this->t('Header row'),
90       '#description' => $this->t('Does the file contain a header row that should be skipped in the import?'),
91       '#default_value' => FALSE,
92     ];
93     return $form;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
100     $values = $form_state->getValues();
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function allowedExtensions() {
107     return ['csv txt'];
108   }
109
110 }