From 306fa31e9e120f86a656e82530c30d13207a141b Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Wed, 28 Dec 2016 21:26:22 +0700 Subject: [PATCH 1/5] Added item per page parameter (default value in main config) --- app/config.example.yml | 4 ++- public/assets/js/app.js | 2 +- .../Controller/ProjectController.php | 36 ++++++++++++------- src/PHPCensor/View/Project/view.phtml | 1 + 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/config.example.yml b/app/config.example.yml index fba80e9d..8542ba01 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -8,7 +8,9 @@ b8: username: php-censor-user password: php-censor-password php-censor: - url: 'http://php-censor.local' + language: en + per_page: 10 + url: 'http://php-censor.local' worker: host: localhost queue: php-censor-queue diff --git a/public/assets/js/app.js b/public/assets/js/app.js index df0fa789..d5dad8ad 100644 --- a/public/assets/js/app.js +++ b/public/assets/js/app.js @@ -45,7 +45,7 @@ var PHPCensor = { getProjectBuilds: function () { $.ajax({ - url: APP_URL + 'project/builds/' + PROJECT_ID + '?branch=' + PROJECT_BRANCH, + url: APP_URL + 'project/builds/' + PROJECT_ID + '?branch=' + PROJECT_BRANCH + '&per_page=' + PER_PAGE, success: function (data) { $('#latest-builds').html(data); diff --git a/src/PHPCensor/Controller/ProjectController.php b/src/PHPCensor/Controller/ProjectController.php index c2aaa520..dbc24e58 100644 --- a/src/PHPCensor/Controller/ProjectController.php +++ b/src/PHPCensor/Controller/ProjectController.php @@ -72,10 +72,12 @@ class ProjectController extends PHPCensor\Controller throw new NotFoundException(Lang::get('project_x_not_found', $projectId)); } - $per_page = 10; + $perPage = is_numeric(b8\Config::getInstance()->get('php-censor.per_page')) + ? (integer)b8\Config::getInstance()->get('php-censor.per_page') + : 10; $page = $this->getParam('p', 1); - $builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), (($page - 1) * $per_page)); - $pages = $builds[1] == 0 ? 1 : ceil($builds[1] / $per_page); + $builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), (($page - 1) * $perPage), $perPage); + $pages = $builds[1] == 0 ? 1 : ceil($builds[1] / $perPage); if ($page > $pages) { $response = new b8\Http\Response\RedirectResponse(); @@ -90,8 +92,9 @@ class ProjectController extends PHPCensor\Controller $this->view->branches = $this->projectStore->getKnownBranches($projectId); $this->view->page = $page; $this->view->pages = $pages; + $this->view->perPage = $perPage; - $this->layout->title = $project->getTitle(); + $this->layout->title = $project->getTitle(); $this->layout->subtitle = $this->view->branch; return $this->view->render(); @@ -141,12 +144,17 @@ class ProjectController extends PHPCensor\Controller } /** - * AJAX get latest builds. - */ + * AJAX get latest builds. + * + * @param int $projectId + * + * @return b8\Http\Response + */ public function builds($projectId) { - $branch = $this->getParam('branch', ''); - $builds = $this->getLatestBuildsHtml($projectId, urldecode($branch)); + $branch = $this->getParam('branch', ''); + $perPage = (integer)$this->getParam('per_page', 10); + $builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), 0, $perPage); $this->response->disableLayout(); $this->response->setContent($builds[0]); @@ -156,12 +164,14 @@ class ProjectController extends PHPCensor\Controller /** * Render latest builds for project as HTML table. * - * @param $projectId - * @param string $branch A urldecoded branch name. - * @param int $start + * @param int $projectId + * @param string $branch A urldecoded branch name. + * @param int $start + * @param int $perPage + * * @return array */ - protected function getLatestBuildsHtml($projectId, $branch = '', $start = 0) + protected function getLatestBuildsHtml($projectId, $branch = '', $start = 0, $perPage = 10) { $criteria = ['project_id' => $projectId]; if (!empty($branch)) { @@ -169,7 +179,7 @@ class ProjectController extends PHPCensor\Controller } $order = ['id' => 'DESC']; - $builds = $this->buildStore->getWhere($criteria, 10, $start, [], $order); + $builds = $this->buildStore->getWhere($criteria, $perPage, $start, [], $order); $view = new b8\View('BuildsTable'); foreach ($builds['items'] as &$build) { diff --git a/src/PHPCensor/View/Project/view.phtml b/src/PHPCensor/View/Project/view.phtml index 58071311..3483eb6d 100644 --- a/src/PHPCensor/View/Project/view.phtml +++ b/src/PHPCensor/View/Project/view.phtml @@ -2,6 +2,7 @@
From c67b3ee7cb891f867fa622f4b669a58a856ff54e Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Wed, 28 Dec 2016 21:27:05 +0700 Subject: [PATCH 2/5] Fixed 'admin_name' string localization --- src/PHPCensor/Command/InstallCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index e0a27aa2..6a641a6a 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -192,7 +192,7 @@ class InstallCommand extends Command } if (!$adminName = $input->getOption('admin-name')) { - $questionName = new Question(Lang::get('admin-name')); + $questionName = new Question(Lang::get('admin_name')); $adminName = $helper->ask($input, $output, $questionName); } From 9e3a344c1765641205c772ad94c34e77c8180c20 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Wed, 28 Dec 2016 21:28:17 +0700 Subject: [PATCH 3/5] Added const DEFAULT_LANGUAGE to Lang class --- src/PHPCensor/Helper/Lang.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/PHPCensor/Helper/Lang.php b/src/PHPCensor/Helper/Lang.php index 2a25f7eb..d6e2a83c 100644 --- a/src/PHPCensor/Helper/Lang.php +++ b/src/PHPCensor/Helper/Lang.php @@ -18,6 +18,8 @@ use b8\Config; */ class Lang { + const DEFAULT_LANGUAGE = 'en'; + /** * @var string */ @@ -36,7 +38,7 @@ class Lang /** * @var array */ - protected static $en_strings = []; + protected static $default_strings = []; /** * Get a specific string from the language file. @@ -51,8 +53,8 @@ class Lang if (array_key_exists($string, self::$strings)) { $vars[0] = self::$strings[$string]; return call_user_func_array('sprintf', $vars); - } elseif ('en' !== self::$language && array_key_exists($string, self::$en_strings)) { - $vars[0] = self::$en_strings[$string]; + } elseif (self::DEFAULT_LANGUAGE !== self::$language && array_key_exists($string, self::$default_strings)) { + $vars[0] = self::$default_strings[$string]; return call_user_func_array('sprintf', $vars); } @@ -129,7 +131,7 @@ class Lang */ public static function init(Config $config, $language_force = null) { - self::$en_strings = self::loadLanguage('en'); + self::$default_strings = self::loadLanguage(self::DEFAULT_LANGUAGE); self::loadAvailableLanguages(); if ($language_force && self::setLanguage($language_force)) { @@ -156,13 +158,13 @@ class Lang } // Try the installation default language: - $language = $config->get('php-censor.basic.language', null); + $language = $config->get('php-censor.language', null); if (self::setLanguage($language)) { return; } // Fall back to English: - self::$language = 'en'; + self::$language = self::DEFAULT_LANGUAGE; self::$strings = self::loadLanguage(); } From d78f9f0e5fc9d32c01609cc987c6c11885b55980 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Fri, 30 Dec 2016 23:40:14 +0700 Subject: [PATCH 4/5] Added custom console Application class with migrations --- app/phinx.php | 36 ---------- bin/console | 4 +- docs/en/installing.md | 4 +- docs/en/updating.md | 2 +- docs/en/workers/cron.md | 2 +- docs/en/workers/worker.md | 2 +- src/PHPCensor/Command/InstallCommand.php | 4 +- src/PHPCensor/Command/UpdateCommand.php | 68 ------------------- src/PHPCensor/Console/Application.php | 68 +++++++++++++++++++ src/PHPCensor/Controller/BuildController.php | 1 + .../Controller/BuildStatusController.php | 1 + src/PHPCensor/Controller/GroupController.php | 1 + src/PHPCensor/Controller/HomeController.php | 1 + src/PHPCensor/Controller/PluginController.php | 2 +- .../Controller/ProjectController.php | 1 + .../Controller/SessionController.php | 1 + .../Controller/SettingsController.php | 1 + .../Controller/WebhookController.php | 1 + src/PHPCensor/Model/User.php | 1 + 19 files changed, 85 insertions(+), 116 deletions(-) delete mode 100644 app/phinx.php delete mode 100644 src/PHPCensor/Command/UpdateCommand.php create mode 100644 src/PHPCensor/Console/Application.php diff --git a/app/phinx.php b/app/phinx.php deleted file mode 100644 index e2a5dc7e..00000000 --- a/app/phinx.php +++ /dev/null @@ -1,36 +0,0 @@ -get('b8.database.servers.write'); - -if (!is_array($writeServers)) { - $writeServers = [$writeServers]; -} - -$conf = [ - 'paths' => [ - 'migrations' => 'src/PHPCensor/Migrations', - ], - 'environments' => [ - 'default_migration_table' => 'migration', - 'default_database' => 'php-censor', - 'php-censor' => [ - 'adapter' => 'mysql', - 'host' => end($writeServers), - 'name' => $config->get('b8.database.name'), - 'user' => $config->get('b8.database.username'), - 'pass' => $config->get('b8.database.password'), - ], - ], -]; - -return $conf; diff --git a/bin/console b/bin/console index 4d1cddd7..9360d461 100755 --- a/bin/console +++ b/bin/console @@ -11,7 +11,6 @@ use PHPCensor\Command\RunCommand; use PHPCensor\Command\RebuildCommand; -use PHPCensor\Command\UpdateCommand; use PHPCensor\Command\InstallCommand; use PHPCensor\Command\DaemonCommand; use PHPCensor\Command\PollCommand; @@ -20,8 +19,8 @@ use PHPCensor\Command\CreateBuildCommand; use PHPCensor\Command\WorkerCommand; use PHPCensor\Command\RebuildQueueCommand; use PHPCensor\Service\BuildService; -use Symfony\Component\Console\Application; use b8\Store\Factory; +use PHPCensor\Console\Application; define('IS_CONSOLE', true); @@ -32,7 +31,6 @@ $application = new Application(); $application->add(new RunCommand($loggerConfig->getFor('RunCommand'))); $application->add(new RebuildCommand($loggerConfig->getFor('RunCommand'))); $application->add(new InstallCommand); -$application->add(new UpdateCommand($loggerConfig->getFor('UpdateCommand'))); $application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand'))); $application->add(new PollCommand($loggerConfig->getFor('PollCommand'))); $application->add(new CreateAdminCommand(Factory::getStore('User'))); diff --git a/docs/en/installing.md b/docs/en/installing.md index 0880aab1..d3814259 100644 --- a/docs/en/installing.md +++ b/docs/en/installing.md @@ -19,7 +19,7 @@ Installing PHP Censor from Composer * Download Composer if you haven't already: `curl -sS https://getcomposer.org/installer | php` * Download PHP Censor: `./composer.phar create-project corpsee/php-censor php-censor --keep-vcs --no-dev` * Go to the newly created PHP Censor directory, and install Composer dependencies: `cd php-censor && ../composer.phar install` -* Run the PHP Censor installer: `./console php-censor:install` +* Run the PHP Censor installer: `./bin/console php-censor:install` * [Add a virtual host to your web server](virtual_host.md), pointing to the `public` directory within your new PHP Censor directory. You'll need to set up rewrite rules to point all non-existent requests to PHP Censor. * [Set up the PHP Censor Worker](workers/worker.md), or you can run builds using the [daemon](workers/daemon.md) or [a cron-job](workers/cron.md) to run PHP Censor builds. @@ -30,6 +30,6 @@ Installing PHP Censor Manually * [Download PHP Censor](https://github.com/corpsee/php-censor/releases/latest) and unzip it. * Go to the PHP Censor directory: `cd /var/www/php-censor` * Install dependencies using Composer: `composer install` -* Install PHP Censor itself: `./console php-censor:install` +* Install PHP Censor itself: `./bin/console php-censor:install` * [Add a virtual host to your web server](virtual_host.md), pointing to the `public` directory within your new PHP Censor directory. You'll need to set up rewrite rules to point all non-existent requests to PHP Censor. * [Set up the PHP Censor Worker](workers/worker.md), or you can run builds using the [daemon](workers/daemon.md) or [a cron-job](workers/cron.md) to run PHP Censor builds. diff --git a/docs/en/updating.md b/docs/en/updating.md index 74a5380c..33bb8e8c 100644 --- a/docs/en/updating.md +++ b/docs/en/updating.md @@ -5,7 +5,7 @@ Updating PHP Censor to the latest release, or even dev-master updates is somethi 1. Go to your PHP Censor root folder in a Terminal. 2. Pull the latest code. This would look like this: `git pull` -3. Update the PHP Censor database: `./console php-censor:update` +3. Update the PHP Censor database: `./bin/console php-censor-migrations:migrate` 4. Update the composer and its packages: `composer self-update && composer update` 5. Return to the PHP Censor admin screens and check your desired plugins are still installed correctly. 7. Run a build to make sure everything is working as expected. diff --git a/docs/en/workers/cron.md b/docs/en/workers/cron.md index d0a2da1f..d3c69f08 100644 --- a/docs/en/workers/cron.md +++ b/docs/en/workers/cron.md @@ -11,7 +11,7 @@ Setting up the Cron Job You'll want to set up PHP Censor to run as a regular cronjob, so run `crontab -e` and enter the following: ```sh -* * * * * /usr/bin/php /path/to/php-censor/console php-censor:run-builds +* * * * * /usr/bin/php /path/to/php-censor/bin/console php-censor:run-builds ``` **Note:** Make sure you change the `/path/to/php-censor` to the directory in which you installed PHP Censor, and update the PHP path if necessary. diff --git a/docs/en/workers/worker.md b/docs/en/workers/worker.md index 07be7937..c9ea6a71 100644 --- a/docs/en/workers/worker.md +++ b/docs/en/workers/worker.md @@ -37,7 +37,7 @@ Using your preferred text editor, create a file named `php-censor.conf` under `/ ``` [program:php-censor] -command=/path/to/php-censor/latest/console php-censor:worker +command=/path/to/php-censor/bin/console php-censor:worker process_name=%(program_name)s_%(process_num)02d stdout_logfile=/var/log/php-censor.log stderr_logfile=/var/log/php-censor-err.log diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index 6a641a6a..dc926024 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -382,9 +382,7 @@ class InstallCommand extends Command { $output->write(Lang::get('setting_up_db')); - $phinxBinary = escapeshellarg(ROOT_DIR . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'phinx'); - $phinxScript = escapeshellarg(APP_DIR . 'phinx.php'); - shell_exec($phinxBinary . ' migrate -c ' . $phinxScript); + shell_exec(ROOT_DIR . 'bin/console php-censor-migrations:migrate'); $output->writeln(''.Lang::get('ok').''); } diff --git a/src/PHPCensor/Command/UpdateCommand.php b/src/PHPCensor/Command/UpdateCommand.php deleted file mode 100644 index 180460d1..00000000 --- a/src/PHPCensor/Command/UpdateCommand.php +++ /dev/null @@ -1,68 +0,0 @@ - - * @package PHPCI - * @subpackage Console - */ -class UpdateCommand extends Command -{ - /** - * @var \Monolog\Logger - */ - protected $logger; - - public function __construct(Logger $logger, $name = null) - { - parent::__construct($name); - $this->logger = $logger; - } - - protected function configure() - { - $this - ->setName('php-censor:update') - ->setDescription(Lang::get('update_app')); - } - - /** - * Generates Model and Store classes by reading database meta data. - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - if (!$this->verifyInstalled()) { - return; - } - - $output->write(Lang::get('updating_app')); - - shell_exec(ROOT_DIR . 'vendor/bin/phinx migrate -c "' . APP_DIR . 'phinx.php"'); - - $output->writeln(''.Lang::get('ok').''); - } - - protected function verifyInstalled() - { - $config = Config::getInstance(); - $url = $config->get('php-censor.url'); - - return !empty($url); - } -} diff --git a/src/PHPCensor/Console/Application.php b/src/PHPCensor/Console/Application.php new file mode 100644 index 00000000..5df1c378 --- /dev/null +++ b/src/PHPCensor/Console/Application.php @@ -0,0 +1,68 @@ +get('b8.database', []); + + $phinxSettings = [ + 'paths' => [ + 'migrations' => 'src/PHPCensor/Migrations', + ], + 'environments' => [ + 'default_migration_table' => 'migration', + 'default_database' => 'php-censor', + 'php-censor' => [ + 'adapter' => 'mysql', + 'host' => $databaseSettings['servers']['write'], + 'name' => $databaseSettings['name'], + 'user' => $databaseSettings['username'], + 'pass' => $databaseSettings['password'], + ], + ], + ]; + + $phinxConfig = new PhinxConfig($phinxSettings); + + $this->add( + (new Create()) + ->setConfig($phinxConfig) + ->setName('php-censor-migrations:create') + ); + $this->add( + (new Migrate()) + ->setConfig($phinxConfig) + ->setName('php-censor-migrations:migrate') + ); + $this->add( + (new Rollback()) + ->setConfig($phinxConfig) + ->setName('php-censor-migrations:rollback') + ); + $this->add( + (new Status()) + ->setConfig($phinxConfig) + ->setName('php-censor-migrations:status') + ); + } +} diff --git a/src/PHPCensor/Controller/BuildController.php b/src/PHPCensor/Controller/BuildController.php index 73a15f47..6bc2d353 100644 --- a/src/PHPCensor/Controller/BuildController.php +++ b/src/PHPCensor/Controller/BuildController.php @@ -1,4 +1,5 @@ Date: Wed, 4 Jan 2017 01:22:58 +0700 Subject: [PATCH 5/5] Added item per page parameter for users (+ language) --- README.md | 31 ++++-- src/PHPCensor/Command/InstallCommand.php | 3 + .../Controller/ProjectController.php | 4 +- src/PHPCensor/Controller/UserController.php | 67 +++++++------ src/PHPCensor/Helper/Lang.php | 36 +++---- src/PHPCensor/Languages/lang.en.php | 4 +- src/PHPCensor/Languages/lang.ru.php | 4 +- ...2_added_language_and_per_page_for_user.php | 32 ++++++ src/PHPCensor/Model/Base/UserBase.php | 98 ++++++++++++++++++- src/PHPCensor/Service/UserService.php | 40 +++++--- 10 files changed, 236 insertions(+), 83 deletions(-) create mode 100644 src/PHPCensor/Migrations/20170103163312_added_language_and_per_page_for_user.php diff --git a/README.md b/README.md index 9812dfe8..237fc0ab 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index dc926024..dbff37b3 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -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); diff --git a/src/PHPCensor/Controller/ProjectController.php b/src/PHPCensor/Controller/ProjectController.php index 5cd2dad4..52959f07 100644 --- a/src/PHPCensor/Controller/ProjectController.php +++ b/src/PHPCensor/Controller/ProjectController.php @@ -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); diff --git a/src/PHPCensor/Controller/UserController.php b/src/PHPCensor/Controller/UserController.php index 217a2e82..b151ac83 100644 --- a/src/PHPCensor/Controller/UserController.php +++ b/src/PHPCensor/Controller/UserController.php @@ -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(); diff --git a/src/PHPCensor/Helper/Lang.php b/src/PHPCensor/Helper/Lang.php index d6e2a83c..c02d3be4 100644 --- a/src/PHPCensor/Helper/Lang.php +++ b/src/PHPCensor/Helper/Lang.php @@ -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; + } } /** diff --git a/src/PHPCensor/Languages/lang.en.php b/src/PHPCensor/Languages/lang.en.php index 92bdb05a..b63abeb5 100644 --- a/src/PHPCensor/Languages/lang.en.php +++ b/src/PHPCensor/Languages/lang.en.php @@ -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', diff --git a/src/PHPCensor/Languages/lang.ru.php b/src/PHPCensor/Languages/lang.ru.php index e0bbf670..a96f87ca 100644 --- a/src/PHPCensor/Languages/lang.ru.php +++ b/src/PHPCensor/Languages/lang.ru.php @@ -9,7 +9,9 @@ return [ 'language_name' => 'Pусский', - 'language' => 'язык', + 'language' => 'Язык', + 'per_page' => 'Количество элементов на странице', + 'default' => 'По умолчанию', // Log in: 'log_in_to_app' => 'Войти в PHP Censor', diff --git a/src/PHPCensor/Migrations/20170103163312_added_language_and_per_page_for_user.php b/src/PHPCensor/Migrations/20170103163312_added_language_and_per_page_for_user.php new file mode 100644 index 00000000..b6352b19 --- /dev/null +++ b/src/PHPCensor/Migrations/20170103163312_added_language_and_per_page_for_user.php @@ -0,0 +1,32 @@ +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(); + } + } +} diff --git a/src/PHPCensor/Model/Base/UserBase.php b/src/PHPCensor/Model/Base/UserBase.php index 1b97883d..99aedeb2 100644 --- a/src/PHPCensor/Model/Base/UserBase.php +++ b/src/PHPCensor/Model/Base/UserBase.php @@ -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); + } } diff --git a/src/PHPCensor/Service/UserService.php b/src/PHPCensor/Service/UserService.php index d3e2cb5f..3b6bb7a6 100644 --- a/src/PHPCensor/Service/UserService.php +++ b/src/PHPCensor/Service/UserService.php @@ -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); }