Passed
Pull Request — master (#1932)
by Struan
06:02
created

AddTermView::display()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 22
nc 9
nop 0
dl 0
loc 33
rs 8.6346
c 1
b 0
f 0
1
<?php
2
3
# For adding glossary terms
4
5
namespace MySociety\TheyWorkForYou\GlossaryView;
6
7
class AddTermView {
8
  public $glossary;
9
10
  public function display(): array {
11
    if (!$this->has_access()) {
12
      return ['error' => _("You don't have permission to manage the glossary")];
13
    }
14
15
    $data = [];
16
    $term = filter_user_input(get_http_var('g'), 'strict');
17
    // glossary matches should always be quoted.
18
    // Just to be sure, we'll strip away any quotes and add them again.
19
    if (preg_match("/^(\"|\')/", $term)) {
20
        $term = preg_replace("/\"|\'/", "", $term);
21
    }
22
    $data['term'] = $term;
23
    $data['title'] = $term;
24
25
    $this->glossary = new \GLOSSARY(['s' => $term]);
26
    $data['glossary'] = $this->glossary;
27
28
    if (get_http_var('submitterm') != '') {
29
      $data = $this->add_glossary_entry($data);
30
    } elseif ((get_http_var('g') != '') && (get_http_var('previewterm') == '')) {
31
      $data = $this->check_glossary_entry($data);
32
    } elseif (get_http_var('previewterm') != '') {
33
      $data['definition'] = get_http_var('definition');
34
      $data['preview'] = 1;
35
    } else {
36
      $data = $this->add_example_urls($data);
37
    }
38
39
    $URL = new \MySociety\TheyWorkForYou\Url('glossary_addterm');
40
    $data['form_url'] = $URL->generate();
41
42
    return $data;
43
  }
44
45
  protected function has_access(): bool {
46
    global $THEUSER;
47
48
    if (!$THEUSER->is_able_to('addterm')) {
49
      return false;
50
    }
51
52
    return true;
53
  }
54
55
  protected function has_stop_words(): bool {
56
    if (in_array($this->glossary->query, $this->glossary->stopwords)) {
57
      return true;
58
    }
59
    return false;
60
  }
61
62
  protected function check_term_is_useful(array $data): array {
63
    if ($this->has_stop_words()) {
64
      $data['error'] = 'Sorry, that phrase appears too many times to be a useful as a link within the parliamentary record.';
65
    } elseif (isset($data['appearances']) && !$data['appearances']) {
66
      $data['error'] = "Unfortunately <strong>" . $data['term'] . "</strong>, doesn't seem to appear in hansard at all...</p>";
67
    } elseif ($this->glossary->num_search_matches > 0) {
68
      $data['show_matches'] = 1;
69
      $data['error'] = 'Existing matches';
70
      $data['count'] = $this->glossary->num_search_matches;
71
    }
72
73
    return $data;
74
  }
75
76
  protected function get_appearance_count(string $term): int {
77
    global $SEARCHENGINE;
78
    $SEARCHENGINE = new \SEARCHENGINE($term);
79
    $count = $SEARCHENGINE->run_count(0, 10000);
80
    return $count;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $count could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
81
  }
82
83
  protected function check_glossary_entry(array $data): array {
84
      $data['appearances'] = $this->get_appearance_count($data['term']);
85
      $data['definition'] = '';
86
      $data = $this->check_term_is_useful($data);
87
88
      if (!isset($data['error'])) {
89
        $data['definition'] = '';
90
        $list = new \HANSARDLIST();
91
        $examples = $list->display('search', [
92
          'num' => 5,
93
          's' => $data['term'],
94
          'view_override' => 'glossary_search',
95
        ], 'none');
96
        $data['examples'] = $examples['rows'];
97
      }
98
99
      return $data;
100
  }
101
102
  protected function add_glossary_entry(array $data): array {
103
      $data['submitted'] = 1;
104
      $data['body'] = get_http_var('definition');
105
      $data = $this->check_term_is_useful($data);
106
107
      $success = false;
108
      if (!isset($data['error'])) {
109
        $entry = [
0 ignored issues
show
Unused Code introduced by
The assignment to $entry is dead and can be removed.
Loading history...
110
          'title' => $data['term'],
111
          'body'  => $data['body'],
112
        ];
113
        $success = $this->glossary->create($data);
114
      }
115
116
      if (is_int($success)) {
117
        $data['success'] = 1;
118
      } elseif (is_array($success)) {
119
        $data = array_merge($data, $success);
120
      } else {
121
        if (!isset($data['error'])) {
122
          $data['error'] = "Sorry, there was an error and we were unable to add your Glossary item.";
123
        }
124
      }
125
126
      return $data;
127
  }
128
129
  protected function add_example_urls(array $data): array {
130
    $URL = new \MySociety\TheyWorkForYou\Url('glossary');
131
132
    $examples = [
133
      'technical' => [
134
        'name' => 'Early Day Motion', 'id' => 90, 
135
      ],
136
      'organisation' => [
137
        'name' => 'Devon County Council', 'id' => 12,
138
      ],
139
      'document' => [
140
        'name' => 'Hutton Report', 'id' => 7,
141
      ],
142
    ];
143
144
    $example_urls = [];
145
    foreach ($examples as $name => $example) {
146
      $URL->insert(["gl" => $example['id']]);
147
      $example['url'] = $URL->generate();
148
      $example_urls[$name] = $example;
149
    }  
150
151
    $data['example_urls'] = $example_urls;
152
    return $data;
153
  }
154
155
}
156