Skip to content

Commit

Permalink
add stat values; show letter block
Browse files Browse the repository at this point in the history
  • Loading branch information
bmanolov committed May 31, 2014
1 parent de35da7 commit b0f517f
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/Command/ImportRbeHtmlCommand.php
Expand Up @@ -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));
}

Expand Down
22 changes: 22 additions & 0 deletions app/Command/UpdateStatValuesCommand.php
@@ -0,0 +1,22 @@
<?php namespace App\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateStatValuesCommand extends ContainerAwareCommand {

private $name = 'app:update-stat-values';

protected function configure() {
$this
->setName($this->name)
->setDescription('Update statistic values');
}

/** {@inheritdoc} */
protected function execute(InputInterface $input, OutputInterface $output) {
$this->getContainer()->get('repository.stat_value')->updateLetterCounts();
}

}
2 changes: 1 addition & 1 deletion app/Controller/Controller.php
Expand Up @@ -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');
}

}
1 change: 1 addition & 0 deletions app/Controller/DefaultController.php
Expand Up @@ -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(),
));
}

Expand Down
2 changes: 2 additions & 0 deletions app/Entity/EntityManager.php
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions app/Entity/EntityRepository.php
Expand Up @@ -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);
}
}
11 changes: 8 additions & 3 deletions app/Entity/StatValue.php
Expand Up @@ -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; }
}
32 changes: 32 additions & 0 deletions app/Entity/StatValueRepository.php
Expand Up @@ -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));
}
}
1 change: 1 addition & 0 deletions app/Entity/Word.php
Expand Up @@ -12,6 +12,7 @@
* )
*/
class Word {

/**
* @ORM\Column(type="integer")
* @ORM\Id
Expand Down
2 changes: 2 additions & 0 deletions app/Resources/views/Default/index.html.twig
Expand Up @@ -42,4 +42,6 @@
</div>
{% endfor %}

{% include 'App:Default:letter_block.html.twig' %}

{% endblock %}
16 changes: 16 additions & 0 deletions app/Resources/views/Default/letter_block.html.twig
@@ -0,0 +1,16 @@
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
Списък на думите по буква
</h3>
</div>
<div class="panel-body">
<div class="text-center">
<div class="btn-group">
{% for letter, count in letterCounts %}
<a class="btn btn-default btn-sm" href="{{ path('letter', {'letter': letter}) }}" title="{{ count }} думи, започващи с „{{ letter }}“">{{ letter }}</a>
{% endfor %}
</div>
</div>
</div>
</div>
8 changes: 6 additions & 2 deletions app/config/services.xml
Expand Up @@ -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">
<services>
<service id="app.entity_manager" class="App\Entity\EntityManager">
<service id="entity_manager" class="App\Entity\EntityManager">
<argument type="service" id="doctrine.orm.entity_manager" />
</service>

<service id="app.word.repository" class="App\Entity\WordRepository"
<service id="repository.word" class="App\Entity\WordRepository"
factory-service="doctrine.orm.entity_manager" factory-method="getRepository">
<argument>App:Word</argument>
</service>
<service id="repository.stat_value" class="App\Entity\StatValueRepository"
factory-service="doctrine.orm.entity_manager" factory-method="getRepository">
<argument>App:StatValue</argument>
</service>
</services>
</container>

0 comments on commit b0f517f

Please sign in to comment.