diff --git a/app/Command/ImportRbeHtmlCommand.php b/app/Command/ImportRbeHtmlCommand.php index 2393280..35fe984 100644 --- a/app/Command/ImportRbeHtmlCommand.php +++ b/app/Command/ImportRbeHtmlCommand.php @@ -24,7 +24,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { throw new \InvalidArgumentException("File '$htmlInput' does not exist."); } ini_set('memory_limit', '1G'); - $importer = new RbeImporter($this->getContainer()->get('app.word.repository')); + $importer = new RbeImporter($this->getContainer()->get('repository.word')); $importer->importWordsFromHtml(file_get_contents($htmlInput)); } diff --git a/app/Command/UpdateStatValuesCommand.php b/app/Command/UpdateStatValuesCommand.php new file mode 100644 index 0000000..fc921d4 --- /dev/null +++ b/app/Command/UpdateStatValuesCommand.php @@ -0,0 +1,22 @@ +setName($this->name) + ->setDescription('Update statistic values'); + } + + /** {@inheritdoc} */ + protected function execute(InputInterface $input, OutputInterface $output) { + $this->getContainer()->get('repository.stat_value')->updateLetterCounts(); + } + +} diff --git a/app/Controller/Controller.php b/app/Controller/Controller.php index b5914d7..586a748 100644 --- a/app/Controller/Controller.php +++ b/app/Controller/Controller.php @@ -8,7 +8,7 @@ abstract class Controller extends SymfonyController { /** @return \App\Entity\EntityManager */ public function em() { - return $this->em ?: $this->em = $this->container->get('app.entity_manager'); + return $this->em ?: $this->em = $this->container->get('entity_manager'); } } diff --git a/app/Controller/DefaultController.php b/app/Controller/DefaultController.php index d36cf24..3698c85 100644 --- a/app/Controller/DefaultController.php +++ b/app/Controller/DefaultController.php @@ -13,6 +13,7 @@ public function indexAction(Request $request) { return $this->render('App:Default:index.html.twig', array( 'words' => $this->em()->getWordRepository()->findByName($searchTerm), 'searchTerm' => $searchTerm, + 'letterCounts' => $this->em()->getStatValueRepository()->getLetterCounts(), )); } diff --git a/app/Entity/EntityManager.php b/app/Entity/EntityManager.php index 7e8236a..4d16b37 100644 --- a/app/Entity/EntityManager.php +++ b/app/Entity/EntityManager.php @@ -24,6 +24,8 @@ public function getRepository($entityName) { /** @return WordRepository */ public function getWordRepository() { return $this->getRepository('Word'); } + /** @return StatValueRepository */ + public function getStatValueRepository() { return $this->getRepository('StatValue'); } /** * A proxy to Doctrine EntityManager methods diff --git a/app/Entity/EntityRepository.php b/app/Entity/EntityRepository.php index 9910cf8..0703c0a 100644 --- a/app/Entity/EntityRepository.php +++ b/app/Entity/EntityRepository.php @@ -43,4 +43,12 @@ public function execute($query, array $params = array(), array $types = array()) return $this->getEntityManager()->getConnection()->executeUpdate($query, $params, $types); } + /** + * Execute and SQL SELECT query and fetch all rows + * @param string $query The SQL query. + * @return array + */ + public function executeAndFetch($query) { + return $this->getEntityManager()->getConnection()->fetchAll($query); + } } diff --git a/app/Entity/StatValue.php b/app/Entity/StatValue.php index c856e65..4cfc1d0 100644 --- a/app/Entity/StatValue.php +++ b/app/Entity/StatValue.php @@ -24,15 +24,20 @@ class StatValue { /** * @var string - * @ORM\Column(type="text") + * @ORM\Column(type="array") */ private $value; + public function __construct($name, $value = null) { + $this->name = $name; + $this->value = $value; + } + public function getId() { return $this->id; } - public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } + public function setName($name) { $this->name = $name; } - public function setValue($value) { $this->value = $value; } public function getValue() { return $this->value; } + public function setValue($value) { $this->value = $value; } } diff --git a/app/Entity/StatValueRepository.php b/app/Entity/StatValueRepository.php index 72be06a..68f739b 100644 --- a/app/Entity/StatValueRepository.php +++ b/app/Entity/StatValueRepository.php @@ -5,4 +5,36 @@ */ class StatValueRepository extends EntityRepository { + const NAME_LETTER_COUNTS = 'letter_counts'; + + public function updateLetterCounts() { + $sql = 'SELECT first_letter AS letter, COUNT(*) AS count + FROM word + GROUP BY first_letter + ORDER BY first_letter'; + $letterCounts = array(); + foreach ($this->executeAndFetch($sql) as $row) { + $letterCounts[$row['letter']] = $row['count']; + } + $this->update(self::NAME_LETTER_COUNTS, $letterCounts); + } + + public function update($name, $value) { + $statValue = $this->findByName($name); + if ($statValue) { + $statValue->setValue($value); + } else { + $statValue = new StatValue($name, $value); + } + $this->save($statValue); + } + + public function getLetterCounts() { + return $this->findByName(self::NAME_LETTER_COUNTS)->getValue(); + } + + /** @return StatValue */ + public function findByName($name) { + return $this->findOneBy(array('name' => $name)); + } } diff --git a/app/Entity/Word.php b/app/Entity/Word.php index bad3945..2865d33 100644 --- a/app/Entity/Word.php +++ b/app/Entity/Word.php @@ -12,6 +12,7 @@ * ) */ class Word { + /** * @ORM\Column(type="integer") * @ORM\Id diff --git a/app/Resources/views/Default/index.html.twig b/app/Resources/views/Default/index.html.twig index a521f23..0c93383 100644 --- a/app/Resources/views/Default/index.html.twig +++ b/app/Resources/views/Default/index.html.twig @@ -42,4 +42,6 @@ {% endfor %} + {% include 'App:Default:letter_block.html.twig' %} + {% endblock %} diff --git a/app/Resources/views/Default/letter_block.html.twig b/app/Resources/views/Default/letter_block.html.twig new file mode 100644 index 0000000..a6a46e7 --- /dev/null +++ b/app/Resources/views/Default/letter_block.html.twig @@ -0,0 +1,16 @@ +
+
+

+ Списък на думите по буква +

+
+
+
+
+ {% for letter, count in letterCounts %} + {{ letter }} + {% endfor %} +
+
+
+
diff --git a/app/config/services.xml b/app/config/services.xml index 66678bc..a5e87b8 100644 --- a/app/config/services.xml +++ b/app/config/services.xml @@ -4,13 +4,17 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services/services-1.0.xsd"> - + - App:Word + + App:StatValue +