AdminPresenter.php 3.09 KB
Newer Older
1
2
<?php

3
4
5
use Sunra\PhpSimple\HtmlDomParser;
use Nette\Application\UI;

6
7
8
9
10
/**
 * Homepage presenter.
 */
class AdminPresenter extends BasePresenter
{
11
12
13
14
15
16
17
18
19
20
21
    public function renderDefault()
    {
        if (!$this->user->isLoggedIn()) {
            $this->redirect('Sign:in');
        }
    }

    public function createComponentAddForm()
    {
        $form = new UI\Form;
        $form->addText('sentence', 'Veta:')
22
        ->setAttribute('placeholder','Sem napíšte vetu.');
23
24

        $form->addText('url', 'sme.sk URL:')
25
        ->setAttribute('placeholder','http://www.sme.sk/c/7054947/vydajte-ujgurov-su-to-teroristi-odkazuje-slovensku-cina.html');
26
27
        $form->addTextArea('paragraph', 'Text:')
        ->setAttribute('placeholder', 'Sem napíšte vety, ktoré chcete importovať do systému.');
28
29
30
31
32
33
34
35
36
37
38
39

        $form->addSubmit('send', 'Parsovať/Vložiť');

        // call method signInFormSucceeded() on success
        $form->onSuccess[] = $this->addFormSucceeded;
        return $form;
    }

    public function addFormSucceeded($form)
    {
        $values = $form->getValues();

40
41
        if ($values->sentence)
        {
42
            if ($this->insertSentence($values->sentence))
Martin Gregor's avatar
Martin Gregor committed
43
                $this->flashMessage("Veta '$values->sentence' bola uložená.",'alert alert-success');
44
        }
45
46

         if ($values->url)
47
        {
48

49
            $this->parseArticle($values->url);
Martin Gregor's avatar
Martin Gregor committed
50
            $this->flashMessage("Článok '$values->url' bol spracovaný.",'alert alert-success');
51
        }
52
53
54

        if ($values->paragraph)
        {
55
            $success = TRUE;
56
57
            foreach (explode('.',$values->paragraph) as $sentence)
            {
58
59
60
61
62
63
64
                if (!$this->insertSentence($sentence))
                {
                    $success = FALSE;
                }
            }
            if ($success)
            {
Martin Gregor's avatar
Martin Gregor committed
65
                $this->flashMessage('Vety z paragrafu boli spracované.','alert alert-success');
66
67
            }
        }
68
        $this->redirect('this');
69
70
71
72
    }

    private function insertSentence($sentence)
    {
Martin Gregor's avatar
Martin Gregor committed
73
        $sentence = trim($sentence,'    "\'„“.');
74
        $word_count = count(explode(' ',$sentence));
75
76
77
        $exists = $this->db->table('sentences')->where(['sentence' => $sentence])->count('*');
        if ($word_count > 5 && !$exists)
        {
78
79
80
            $result = $this->db->table('sentences')->insert(['sentence' => $sentence,'word_count' => $word_count]);
            if ($result)
            {
Martin Gregor's avatar
Martin Gregor committed
81
                $this->flashMessage("Veta '$sentence' nebola spracovaná", 'alert alert-danger');
82
83
                return FALSE;
            }
84
        }
85
86
        else
        {
Martin Gregor's avatar
Martin Gregor committed
87
            $this->flashMessage("Veta '$sentence' obsahuje nedostatočný počet slov alebo v systéme už existuje.",'alert alert-danger');
88
89
90
91
            return FALSE;
        }

        return TRUE;
92
    }
93

94
95
96
97
98
99
100
101
102
103
    private function parseArticle($url)
    {
        $dom = HtmlDomParser::file_get_html( $url );
        $text = $dom->find('#article-box #itext_content',0)->plaintext;
        $sentences = preg_split('/[.?!]/',$text);
        foreach ($sentences as $sentence)
        {
            $this->insertSentence($sentence);
        }
    }
104
}