Skip to content

Commit

Permalink
Merge pull request #15 from bolt/tests/start
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
bobdenotter committed Oct 26, 2015
2 parents 9c18bee + e59135d commit 7b3ce6a
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 297 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.*.swp
composer.lock
vendor/
tests/tmp/
181 changes: 102 additions & 79 deletions Extension.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
<?php
/**
* Labels Extension for Bolt
* @author Bob den Otter <[email protected]>
*/

namespace Bolt\Extension\Bolt\Labels;

require_once __DIR__ . '/include/Model.php';

use Bolt\Application;
use Bolt\BaseExtension;
use Bolt\Library as Lib;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Extension extends \Bolt\BaseExtension
/**
* Labels Extension for Bolt
*
* @author Bob den Otter <[email protected]>
*/
class Extension extends BaseExtension
{
public function __construct(Application $app)
{
parent::__construct($app);
}
protected $labels;

public function getName()
{
return "labels";
return 'labels';
}

public function initialize()
{
$this->app->before(array($this, 'before'));

// Twig functions
$this->addTwigFunction('l', 'twigL');
$this->addTwigFunction('setlanguage', 'twigSetLanguage');

$this->before();

$this->boltPath = $this->app['config']->get('general/branding/path');

$this->fileName = $this->app['paths']['configpath'] . '/extensions/labels.json';

$this->addMenuOption("Label translations", "$this->boltPath/labels", "fa:flag");

$this->app->get($this->boltPath . '/labels', array($this, 'translationsGET'))->bind('labels');
$this->app->get($this->boltPath . '/labels/list', array($this, 'listTranslations'))->bind('list_labels');
$this->app->post($this->boltPath . '/labels/save', array($this, 'labelsSavePost'))->bind('save_labels');

$root = $this->app['resources']->getUrl('bolt');

// Admin menu
$this->addMenuOption('Label translations', $root . 'labels', 'fa:flag');

// Routess
$this->app->get($root . 'labels', array($this, 'translationsGET'))
->bind('labels')
;
$this->app->get($root . 'labels/list', array($this, 'listTranslations'))
->bind('list_labels')
;
$this->app->post($root . 'labels/save', array($this, 'labelsSavePost'))
->bind('save_labels')
;
}

/**
* Set the current language
*
* @param Request $request
*/
public function before()
public function before(Request $request)
{
$lang = $this->config['default'];

Expand All @@ -72,6 +78,7 @@ public function before()

public function extractLanguage($lang)
{
$matches = array();
if (preg_match('/^([a-z]{2})\./', $lang, $matches)) {
return $matches[1];
} else {
Expand All @@ -81,22 +88,44 @@ public function extractLanguage($lang)

public function loadLabels()
{
$jsonFile = $this->app['resources']->getPath('extensionsconfig') . '/labels.json';
$fs = new Filesystem();

// Check that the user's JSON file exists, else copy in the default
if (!$fs->exists($jsonFile)) {
try {
$fs->copy($this->getBasePath() . '/files/labels.json', $jsonFile);
} catch (IOException $e) {
$this->app['session']->getFlashBag()->set('error',
'The labels file at <tt>app/config/extensions/labels.json</tt> does not exist, and can not be created. Changes can NOT saved, until you fix this.');
}
}

// Check the file is writable
try {
if (is_readable($this->fileName)) {
$labels = file_get_contents($this->fileName);
} else {
$labels = file_get_contents(__DIR__ . "/files/labels.json");
$fs->touch($jsonFile);
} catch (IOException $e) {
$this->app['session']->getFlashBag()->set('error',
'The labels file at <tt>app/config/extensions/labels.json</tt> is not writable. Changes can NOT saved, until you fix this.');
}

// Read the contents of the file
try {
$finder = new Finder();
$finder
->files()
->name('labels.json')
->in($this->app['resources']->getPath('extensionsconfig'))
;

foreach ($finder->files() as $file) {
$this->labels = json_decode($file->getContents(), true);
continue;
}
$this->labels = json_decode($labels, true);
} catch (\Exception $e) {
$this->app['session']->getFlashBag()->set('error', 'There was an issue loading the labels.');
$this->labels = [];
return false;
$this->app['session']->getFlashBag()->set('error', sprintf('There was an issue loading the labels: %s', $e->getMessage()));
$this->labels = false;
}

return true;

}

/**
Expand Down Expand Up @@ -125,18 +154,16 @@ public function getCurrentLanguage()
$twigGlobals = $this->app['twig']->getGlobals();
if (isset($twigGlobals['lang'])) {
return $twigGlobals['lang'];
}
else {
} else {
return null;
}
}


public function translationsGET(Request $request)
{
$this->requireUserPermission('labels');

if (empty($this->labels)) {
if ($this->labels === null) {
$this->loadLabels();
}

Expand All @@ -146,25 +173,17 @@ public function translationsGET(Request $request)

$data = [];

foreach($this->labels as $label => $row) {
foreach ($this->labels as $label => $row) {
$values = [];
foreach($languages as $l) {
foreach ($languages as $l) {
$values[] = $row[strtolower($l)] ?: '';
}
$data[] = array_merge([$label], $values);
}

if (!file_exists($this->fileName) && !is_writable(dirname($this->fileName))) {
$this->app['session']->getFlashBag()->set('error',
'The labels file at <tt>../app/config/extensions/labels.json</tt> does not exist, and can not be created. Changes can NOT saved, until you fix this.');
} else if (file_exists($this->fileName) && !is_writable($this->fileName)) {
$this->app['session']->getFlashBag()->set('error',
'The labels file at <tt>../app/config/extensions/labels.json</tt> is not writable. Changes can NOT saved, until you fix this.');
}

$twigvars = [
'columns' => array_merge([ 'Label'], $languages),
'data' => $data
'data' => $data
];

return $this->render('import_form.twig', $twigvars);
Expand All @@ -177,7 +196,7 @@ public function addLabel($label)
$jsonarr = json_encode($this->labels);

if (!file_put_contents($this->fileName, $jsonarr)) {
echo "[error saving labels]";
echo '[error saving labels]';
}
}

Expand All @@ -191,7 +210,7 @@ public function labelsSavePost(Request $request)

$arr = [];

foreach($labels as $labelrow) {
foreach ($labels as $labelrow) {
$key = strtolower(trim(array_shift($labelrow)));
$values = array_combine($columns, $labelrow);
$arr[$key] = $values;
Expand All @@ -204,36 +223,29 @@ public function labelsSavePost(Request $request)
return Lib::redirect('labels');
}

if (!file_exists($this->fileName) && !is_writable(dirname($this->fileName))) {
$this->app['session']->getFlashBag()->set('error',
'The labels file at <tt>../app/config/extensions/labels.json</tt> does not exist, and can not be created. Changes were NOT saved.');
} else if (file_exists($this->fileName) && !is_writable($this->fileName)) {
$fs = new Filesystem();
try {
$jsonFile = $this->app['resources']->getPath('extensionsconfig') . '/labels.json';
$fs->dumpFile($jsonFile, $jsonarr);
$this->app['session']->getFlashBag()->set('success', 'Changes to the labels have been saved.');
} catch (IOException $e) {
$this->app['session']->getFlashBag()->set('error',
'The labels file at <tt>../app/config/extensions/labels.json</tt> is not writable. Changes were NOT saved.');
}

if (!file_put_contents($this->fileName, $jsonarr)) {
$this->app['session']->getFlashBag()->set('error',
'There was an issue saving the file. Changes were NOT saved.');
return Lib::redirect('labels');
}

$this->app['session']->getFlashBag()->set('success', 'Changes to the labels have been saved.');
return Lib::redirect('labels');

}

/**
* Twig function {{ l() }} in Labels extension.
*/
public function twigL($label, $lang = false)
{

if (!$this->isValidLanguage($lang)) {
$lang = $this->getCurrentLanguage();
}

if (empty($this->labels)) {
if ($this->labels === null) {
$this->loadLabels();
}

Expand All @@ -251,28 +263,39 @@ public function twigL($label, $lang = false)
if ($this->config['add_missing'] && empty($this->labels[$label])) {
$this->addLabel($label);
}

}

return new \Twig_Markup($res, 'UTF-8');
}

public function twigSetLanguage($lang) {
public function twigSetLanguage($lang)
{
$this->setCurrentLanguage($lang);
return '';
}

public function __get($name) {
switch ($name) {
case 'currentLanguage':
return $this->getCurrentLanguage();
}
public function __get($name)
{
return $name === 'currentLanguage' ? $this->getCurrentLanguage() : null;
}

private function render($template, $data) {
$this->app['twig.loader.filesystem']->addPath(dirname(__FILE__) . '/templates');
/**
* {@inheritDoc}
*/
protected function getDefaultConfig()
{
return array(
'languages' => array('en', 'nl', 'de', 'fy', 'fr'),
'default' => 'nl',
'current' => 'nl',
);
}

private function render($template, $data)
{
$this->app['twig.loader.filesystem']->addPath(__DIR__ . '/templates');

if ($this->app['config']->getWhichEnd()=='backend') {
if ($this->app['config']->getWhichEnd() === 'backend') {
$this->app['htmlsnippets'] = true;
$this->addCss('assets/handsontable.full.min.css');
$this->addJavascript('assets/handsontable.full.min.js', true);
Expand Down
19 changes: 17 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
"name": "bolt/labels",
"description": "Database-backed translation labels",
"type": "bolt-extension",
"minimum-stability" : "dev",
"prefer-stable" : true,
"require": {
"bolt/bolt": ">=2.0.0,<3.0.0"
},
"require-dev" : {
"phpunit/phpunit" : "^4.7"
},
"license": "MIT",
"authors": [
{"name": "Two Kings", "email": "[email protected]"},
Expand All @@ -19,8 +24,18 @@
"Bolt\\Extension\\Bolt\\Labels\\": ""
}
},

"autoload-dev" : {
"psr-4" : {
"Bolt\\Tests\\" : {
"Bolt\\Extension\\Bolt\\Labels\\Tests\\" : "tests/",
"Bolt\\Tests\\" : "vendor/bolt/bolt/tests/phpunit/unit/"
}
}
},
"extra": {
"bolt-assets" : "assets/"
"bolt-assets" : "assets/",
"branch-alias" : {
"dev-master" : "2.1.*-dev"
}
}
}
Loading

0 comments on commit 7b3ce6a

Please sign in to comment.