Added item per page parameter for users (+ language)

This commit is contained in:
Dmitry Khomutov 2017-01-04 01:22:58 +07:00
parent d78f9f0e5f
commit 110543e983
10 changed files with 236 additions and 83 deletions

View file

@ -1,7 +1,7 @@
PHP Censor
----------
PHP Censor is a fork of PHPCI (And B8Framework). PHP Censor is a free and open source (BSD-2-Clause license) continuous
PHP Censor is a fork of PHPCI (And B8Framework) and is a free and open source (BSD-2-Clause license) continuous
integration tool specifically designed for PHP. We've built it with simplicity in mind, so whilst it doesn't
do *everything* Jenkins can do, it is a breeze to set up and use.
@ -23,11 +23,6 @@ What it doesn't do (yet)
* Install PEAR or PECL extensions.
* Deployments
Documentation
=============
[PHP Censor documentation](docs/README.md)
Tests
=====
@ -40,7 +35,29 @@ For Phar plugin tests set 'phar.readonly' setting to Off (0) in `php.ini` config
For database B8Framework tests create empty 'b8_test' MySQL database on 'localhost' with user/password: `root/root`.
Migrations
==========
Run to apply latest migrations:
```bash
cd /path/to/php-censor
./bin/console php-censor-migrations:migrate
```
Run to create new migration:
```bash
cd /path/to/php-censor
./bin/console php-censor-migrations:create NewMigrationName
```
Documentation
=============
[PHP Censor documentation](docs/README.md)
License
=======
The PHP Censor is open source software licensed under the BSD-2-Clause license.
PHP Censor is open source software licensed under the BSD-2-Clause license.

View file

@ -240,6 +240,9 @@ class InstallCommand extends Command
$url = $helper->ask($input, $output, $question);
}
$config['language'] = 'en';
$config['per_page'] = 10;
$config['url'] = $url;
$config['worker'] = $this->getQueueInformation($input, $output, $helper);

View file

@ -73,9 +73,7 @@ class ProjectController extends PHPCensor\Controller
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
}
$perPage = is_numeric(b8\Config::getInstance()->get('php-censor.per_page'))
? (integer)b8\Config::getInstance()->get('php-censor.per_page')
: 10;
$perPage = $_SESSION['php-censor-user']->getFinalPerPage();
$page = $this->getParam('p', 1);
$builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), (($page - 1) * $perPage), $perPage);
$pages = $builds[1] == 0 ? 1 : ceil($builds[1] / $perPage);

View file

@ -14,6 +14,7 @@ use b8\Exception\HttpException\NotFoundException;
use b8\Form;
use PHPCensor\Controller;
use PHPCensor\Helper\Lang;
use PHPCensor\Model\User;
use PHPCensor\Service\UserService;
/**
@ -61,36 +62,25 @@ class UserController extends Controller
*/
public function profile()
{
/** @var User $user */
$user = $_SESSION['php-censor-user'];
if ($this->request->getMethod() == 'POST') {
$name = $this->getParam('name', null);
$email = $this->getParam('email', null);
$name = $this->getParam('name', null);
$email = $this->getParam('email', null);
$password = $this->getParam('password', null);
$language = $this->getParam('language', null);
$perPage = $this->getParam('per_page', null);
$currentLang = Lang::getLanguage();
$chosenLang = $this->getParam('language', $currentLang);
if ($chosenLang !== $currentLang) {
setcookie('php-censor-language', $chosenLang, time() + (10 * 365 * 24 * 60 * 60), '/');
Lang::setLanguage($chosenLang);
}
$_SESSION['php-censor-user'] = $this->userService->updateUser($user, $name, $email, $password);
$user = $_SESSION['php-censor-user'];
$_SESSION['php-censor-user'] = $this->userService->updateUser($user, $name, $email, $password, null, $language, $perPage);
$user = $_SESSION['php-censor-user'];
$this->view->updated = 1;
}
$this->layout->title = $user->getName();
$this->layout->title = $user->getName();
$this->layout->subtitle = Lang::get('edit_profile');
$values = $user->getDataArray();
if (array_key_exists('php-censor-language', $_COOKIE)) {
$values['language'] = $_COOKIE['php-censor-language'];
}
$form = new Form();
$form->setAction(APP_URL.'user/profile');
$form->setMethod('POST');
@ -100,6 +90,7 @@ class UserController extends Controller
$name->setContainerClass('form-group');
$name->setLabel(Lang::get('name'));
$name->setRequired(true);
$name->setValue($user->getName());
$form->addField($name);
$email = new Form\Element\Email('email');
@ -107,6 +98,7 @@ class UserController extends Controller
$email->setContainerClass('form-group');
$email->setLabel(Lang::get('email_address'));
$email->setRequired(true);
$email->setValue($user->getEmail());
$form->addField($email);
$password = new Form\Element\Password('password');
@ -114,24 +106,41 @@ class UserController extends Controller
$password->setContainerClass('form-group');
$password->setLabel(Lang::get('password_change'));
$password->setRequired(false);
$password->setValue(null);
$form->addField($password);
$lang = new Form\Element\Select('language');
$lang->setClass('form-control');
$lang->setContainerClass('form-group');
$lang->setLabel(Lang::get('language'));
$lang->setRequired(true);
$lang->setOptions(Lang::getLanguageOptions());
$lang->setValue(Lang::getLanguage());
$form->addField($lang);
$language = new Form\Element\Select('language');
$language->setClass('form-control');
$language->setContainerClass('form-group');
$language->setLabel(Lang::get('language'));
$language->setRequired(true);
$language->setOptions(array_merge(
['' => Lang::get('default') . ' (' . b8\Config::getInstance()->get('php-censor.language') . ')'],
Lang::getLanguageOptions())
);
$language->setValue($user->getLanguage());
$form->addField($language);
$perPage = new Form\Element\Select('per_page');
$perPage->setClass('form-control');
$perPage->setContainerClass('form-group');
$perPage->setLabel(Lang::get('per_page'));
$perPage->setRequired(true);
$perPage->setOptions([
null => Lang::get('default') . ' (' . b8\Config::getInstance()->get('php-censor.per_page') . ')',
10 => 10,
25 => 25,
50 => 50,
100 => 100,
]);
$perPage->setValue($user->getPerPage());
$form->addField($perPage);
$submit = new Form\Element\Submit();
$submit->setClass('btn btn-success');
$submit->setValue(Lang::get('save'));
$form->addField($submit);
$form->setValues($values);
$this->view->form = $form;
return $this->view->render();

View file

@ -10,6 +10,7 @@
namespace PHPCensor\Helper;
use b8\Config;
use PHPCensor\Model\User;
/**
* Languages Helper Class - Handles loading strings files and the strings within them.
@ -137,35 +138,22 @@ class Lang
if ($language_force && self::setLanguage($language_force)) {
return;
}
// Try cookies first:
if (isset($_COOKIE) && array_key_exists('php-censor-language', $_COOKIE) && self::setLanguage($_COOKIE['php-censor-language'])) {
return;
/** @var User $user */
$user = $_SESSION['php-censor-user'];
if (!is_object($user) && gettype($user) == 'object') {
$user = unserialize(serialize($_SESSION['php-censor-user']));
}
// Try user language:
if (isset($_SERVER) && array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
$langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $lang) {
$parts = explode(';', $lang);
$language = strtolower($parts[0]);
if (self::setLanguage($language)) {
return;
}
}
}
// Try the installation default language:
$language = $config->get('php-censor.language', null);
$language = $user->getLanguage();
if (self::setLanguage($language)) {
return;
}
// Fall back to English:
self::$language = self::DEFAULT_LANGUAGE;
self::$strings = self::loadLanguage();
// Try the installation default language:
$language = $config->get('php-censor.language', self::DEFAULT_LANGUAGE);
if (self::setLanguage($language)) {
return;
}
}
/**

View file

@ -9,7 +9,9 @@
return [
'language_name' => 'English',
'language' => 'Language',
'language' => 'Language',
'per_page' => 'Items per page',
'default' => 'Default',
// Log in:
'log_in_to_app' => 'Log in to PHP Censor',

View file

@ -9,7 +9,9 @@
return [
'language_name' => 'Pусский',
'language' => 'язык',
'language' => 'Язык',
'per_page' => 'Количество элементов на странице',
'default' => 'По умолчанию',
// Log in:
'log_in_to_app' => 'Войти в PHP Censor',

View file

@ -0,0 +1,32 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddedLanguageAndPerPageForUser extends AbstractMigration
{
public function up()
{
$table = $this->table('user');
if (!$table->hasColumn('language')) {
$table->addColumn('language', 'string', ['limit' => 5, 'null' => true])->save();
}
if (!$table->hasColumn('per_page')) {
$table->addColumn('per_page', 'integer', ['null' => true])->save();
}
}
public function down()
{
$table = $this->table('user');
if ($table->hasColumn('language')) {
$table->removeColumn('language')->save();
}
if ($table->hasColumn('per_page')) {
$table->removeColumn('per_page')->save();
}
}
}

View file

@ -6,6 +6,7 @@
namespace PHPCensor\Model\Base;
use b8\Config;
use PHPCensor\Model;
/**
@ -37,6 +38,8 @@ class UserBase extends Model
'hash' => null,
'is_admin' => null,
'name' => null,
'language' => null,
'per_page' => null,
];
/**
@ -49,6 +52,8 @@ class UserBase extends Model
'hash' => 'getHash',
'is_admin' => 'getIsAdmin',
'name' => 'getName',
'language' => 'getLanguage',
'per_page' => 'getPerPage',
// Foreign key getters:
];
@ -62,6 +67,8 @@ class UserBase extends Model
'hash' => 'setHash',
'is_admin' => 'setIsAdmin',
'name' => 'setName',
'language' => 'setLanguage',
'per_page' => 'setPerPage',
// Foreign key setters:
];
@ -95,6 +102,16 @@ class UserBase extends Model
'length' => 250,
'default' => null,
],
'language' => [
'type' => 'varchar',
'length' => 5,
'default' => null,
],
'per_page' => [
'type' => 'int',
'length' => 11,
'default' => null,
],
];
/**
@ -119,7 +136,7 @@ class UserBase extends Model
*/
public function getId()
{
$rtn = $this->data['id'];
$rtn = $this->data['id'];
return $rtn;
}
@ -131,7 +148,7 @@ class UserBase extends Model
*/
public function getEmail()
{
$rtn = $this->data['email'];
$rtn = $this->data['email'];
return $rtn;
}
@ -143,7 +160,7 @@ class UserBase extends Model
*/
public function getHash()
{
$rtn = $this->data['hash'];
$rtn = $this->data['hash'];
return $rtn;
}
@ -155,7 +172,7 @@ class UserBase extends Model
*/
public function getIsAdmin()
{
$rtn = $this->data['is_admin'];
$rtn = $this->data['is_admin'];
return $rtn;
}
@ -167,7 +184,31 @@ class UserBase extends Model
*/
public function getName()
{
$rtn = $this->data['name'];
$rtn = $this->data['name'];
return $rtn;
}
/**
* Get the value of Language / language.
*
* @return string
*/
public function getLanguage()
{
$rtn = $this->data['language'];
return $rtn;
}
/**
* Get the value of PerPage / per_page.
*
* @return string
*/
public function getPerPage()
{
$rtn = $this->data['per_page'];
return $rtn;
}
@ -271,4 +312,51 @@ class UserBase extends Model
$this->_setModified('name');
}
/**
* Set the value of Language / language.
*
* Must not be null.
* @param $value string
*/
public function setLanguage($value)
{
if ($this->data['language'] === $value) {
return;
}
$this->data['language'] = $value;
$this->_setModified('language');
}
/**
* Set the value of PerPage / per_page.
*
* Must not be null.
* @param $value string
*/
public function setPerPage($value)
{
if ($this->data['per_page'] === $value) {
return;
}
$this->data['per_page'] = $value;
$this->_setModified('per_page');
}
/**
* @return integer
*/
public function getFinalPerPage()
{
$perPage = $this->getPerPage();
if ($perPage) {
return (integer)$perPage;
}
return (integer)Config::getInstance()->get('php-censor.per_page', 10);
}
}

View file

@ -9,6 +9,7 @@
namespace PHPCensor\Service;
use PHPCensor\Helper\Lang;
use PHPCensor\Model\User;
use PHPCensor\Store\UserStore;
@ -34,33 +35,43 @@ class UserService
/**
* Create a new user within PHPCI.
* @param $name
* @param $emailAddress
* @param $password
* @param bool $isAdmin
* @return \PHPCensor\Model\User
*
* @param string $name
* @param string $emailAddress
* @param string $password
* @param bool $isAdmin
* @param string $language
* @param integer $perPage
*
* @return User
*/
public function createUser($name, $emailAddress, $password, $isAdmin = false)
public function createUser($name, $emailAddress, $password, $isAdmin = false, $language = null, $perPage = null)
{
$user = new User();
$user->setName($name);
$user->setEmail($emailAddress);
$user->setHash(password_hash($password, PASSWORD_DEFAULT));
$user->setIsAdmin(($isAdmin ? 1 : 0));
$user->setLanguage($language);
$user->setPerPage($perPage);
return $this->store->save($user);
}
/**
* Update a user.
* @param User $user
* @param $name
* @param $emailAddress
* @param null $password
* @param null $isAdmin
* @return \PHPCensor\Model\User
*
* @param User $user
* @param string $name
* @param string $emailAddress
* @param string $password
* @param bool $isAdmin
* @param string $language
* @param integer $perPage
*
* @return User
*/
public function updateUser(User $user, $name, $emailAddress, $password = null, $isAdmin = null)
public function updateUser(User $user, $name, $emailAddress, $password = null, $isAdmin = null, $language = null, $perPage = null)
{
$user->setName($name);
$user->setEmail($emailAddress);
@ -73,6 +84,9 @@ class UserService
$user->setIsAdmin(($isAdmin ? 1 : 0));
}
$user->setLanguage($language);
$user->setPerPage($perPage);
return $this->store->save($user);
}