Skip to content

Commit

Permalink
Upload widget sources
Browse files Browse the repository at this point in the history
  • Loading branch information
zenn1989 committed Oct 23, 2016
1 parent 43636e7 commit cf3d682
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 0 deletions.
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "phpffcms/widget-newuser",
"description": "FFCMS widget to display new users in block",
"type": "library",
"keywords": ["php", "ffcms", "widget"],
"homepage": "http://ffcms.org",
"license": "MIT",
"authors": [
{
"name": "Pyatinskyi M.M.",
"email": "[email protected]",
"homepage": "http://ffcms.ru"
}
],
"require": {
"php": ">=5.5"
},
"autoload": {
"psr-0": {
"Apps\\": "src/",
"Widgets\\": "src/"
}
}
}
93 changes: 93 additions & 0 deletions src/Apps/Controller/Admin/Newuser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Apps\Controller\Admin;


use Extend\Core\Arch\AdminController;
use Ffcms\Core\App;
use Ffcms\Core\Helper\FileSystem\File;
use Apps\ActiveRecord\App as AppRecord;
use Ffcms\Core\Helper\Serialize;
use Apps\Model\Admin\Newuser\FormSettings;

/**
* Class Newuser. Admin controller to manage new users widget
* @package Apps\Controller\Admin
*/
class Newuser extends AdminController
{
const VERSION = '1.0.0';

public $type = 'widget';

private $appRoot;
private $tplDir;

/**
* Set default data - root path, tpl directory and language locale
*/
public function before()
{
$this->appRoot = realpath(__DIR__ . '/../../../');
$this->tplDir = realpath($this->appRoot . '/Apps/View/Admin/default/');
$langFile = $this->appRoot . '/I18n/Admin/' . $this->lang .'/Newuser.php';
if ($this->lang !== 'en' && File::exist($langFile)) {
App::$Translate->append($langFile);
}
}

/**
* Widget admin settings
* @return string
* @throws \Ffcms\Core\Exception\SyntaxException
* @throws \Ffcms\Core\Exception\NativeException
*/
public function actionIndex()
{
// init settings model
$model = new FormSettings($this->getConfigs());

// check if request is submited
if ($model->send() && $model->validate()) {
$this->setConfigs($model->getAllProperties());
App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
}

// render viewer
return $this->view->render('newuser/index', [
'model' => $model
], $this->tplDir);
}

/**
* Installation process function
*/
public static function install()
{
// initialize class for configs & names
$data = new \stdClass();
$data->configs = [
'count' => 12,
'cache' => 60
];
$data->name = [
'en' => 'New users',
'ru' => 'Новые пользователи'
];

// find widget record in db
$widget = AppRecord::where('type', 'widget')->where('sys_name', 'Newuser');
if ($widget->count() !== 1) {
return;
}

// update db data
$widget->update([
'name' => Serialize::encode($data->name),
'configs' => Serialize::encode($data->configs),
'disabled' => false,
'version' => static::VERSION
]);
}

}
69 changes: 69 additions & 0 deletions src/Apps/Model/Admin/Newuser/FormSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Apps\Model\Admin\Newuser;


use Ffcms\Core\Arch\Model;
use Ffcms\Core\Helper\Type\Obj;

/**
* Class FormSettings. Business logic of newuser widget settings
* @package Apps\Model\Admin\Newuser
*/
class FormSettings extends Model
{
public $count;
public $cache;

private $_configs;

/**
* FormSettings constructor. Pass default configs inside
* @param array $configs
*/
public function __construct(array $configs)
{
$this->_configs = $configs;
parent::__construct(true);
}

/**
* Set default properties from passed config array
*/
public function before()
{
if (!Obj::isArray($this->_configs)) {
return;
}

foreach ($this->_configs as $config => $value) {
if (property_exists($this, $config)) {
$this->{$config} = $value;
}
}
}

/**
* Form validation rules
* @return array
*/
public function rules()
{
return [
[['count', 'cache'], 'required'],
[['count', 'cache'], 'int']
];
}

/**
* Form display labels
* @return array
*/
public function labels()
{
return [
'count' => __('Count'),
'cache' => __('Cache')
];
}
}
30 changes: 30 additions & 0 deletions src/Apps/View/Admin/default/newuser/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Ffcms\Core\Helper\HTML\Form;
use Ffcms\Core\Helper\Url;

/** @var \Apps\Model\Admin\Newuser\FormSettings $model */
/** @var \Ffcms\Core\Arch\View $this */

$this->title = __('New users');
$this->breadcrumbs = [
Url::to('main/index') => __('Main'),
Url::to('newcontent/index') => __('New users'),
__('Settings')
];

?>

<h1><?= __('New users') ?></h1>
<hr />

<?php $form = new Form($model, ['class' => 'form-horizontal', 'method' => 'post']) ?>

<?= $form->start() ?>

<?= $form->field('count', 'text', ['class' => 'form-control'], __('How many new users will be displayed in block?'))?>
<?= $form->field('cache', 'text', ['class' => 'form-control'], __('Widget default cache time in seconds. Set 0 to disable caching'))?>

<?= $form->submitButton(__('Save'), ['class' => 'btn btn-primary']) ?>

<?= $form->finish() ?>
9 changes: 9 additions & 0 deletions src/I18n/Admin/ru/Newuser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
'New users' => 'Новые пользователи',
'Count' => 'Количество',
'How many new users will be displayed in block?' => 'Сколько новых пользователей отображать в блоке?',
'Cache' => 'Кэширование',
'Widget default cache time in seconds. Set 0 to disable caching' => 'Укажите время кэширования виджета в секундах. Установите 0 чтобы отключить'
];
5 changes: 5 additions & 0 deletions src/I18n/Front/ru/NewUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'Show all users' => 'Показать всех пользователей'
];
81 changes: 81 additions & 0 deletions src/Widgets/Front/Newuser/Newuser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Widgets\Front\Newuser;

use Apps\ActiveRecord\Profile;
use Extend\Core\Arch\FrontWidget;
use Ffcms\Core\App;
use Ffcms\Core\Helper\FileSystem\File;
use Ffcms\Core\Traits\ClassTools;
use Illuminate\Database\Eloquent\Collection;

/**
* Class Newuser. Front widget logic
* @package Widgets\Front\Newuser
*/
class Newuser extends FrontWidget
{
use ClassTools;

public $count;
public $cache;

private $_cacheName;

/**
* Initialize widget - set default properties from configs
*/
public function init()
{
// get configs and set properties
$cfg = $this->getConfigs();
if ($this->count === null) {
$this->count = (int)$cfg['count'];
}
if ($this->cache === null) {
$this->cache = (int)$cfg['cache'];
}
$this->_cacheName = 'widget.newuser.' . $this->createStringClassSnapshotHash();
// set translation
$root = realpath(__DIR__ . '/../../../');
$langFile = $root . '/I18n/Front/' . App::$Request->getLanguage() . '/Newuser.php';
if (File::exist($langFile)) {
App::$Translate->append($langFile);
}
}

/**
* Display widget data
* @return string
* @throws \Ffcms\Core\Exception\SyntaxException
* @throws \Ffcms\Core\Exception\NativeException
*/
public function display()
{
$records = null;
if ($this->cache > 0) {
if (App::$Cache->get($this->_cacheName) !== null) {
$records = App::$Cache->get($this->_cacheName);
} else {
$records = $this->getUserProfiles();
App::$Cache->set($this->_cacheName, $records, $this->cache);
}
} else {
$records = $this->getUserProfiles();
}

// render output view
return App::$View->render('widgets/newuser/index', [
'records' => $records
], __DIR__);
}

/**
* Get user profiles from db as active records collection
* @return Collection
*/
private function getUserProfiles()
{
return Profile::orderBy('created_at', 'DESC')->take($this->count)->get();
}
}
26 changes: 26 additions & 0 deletions src/Widgets/Front/Newuser/widgets/newuser/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/** @var \Illuminate\Database\Eloquent\Collection $records */

if ($records->count() < 1) {
echo __('No users found');
return;
}
?>

<div class="row">
<?php foreach ($records as $profile): ?>
<?php /** @var \Apps\ActiveRecord\Profile $profile */ ?>
<div class="col-md-6">
<a href="<?= \Ffcms\Core\Helper\Url::to('profile/show', $profile->user_id) ?>" class="thumbnail text-center">
<span><?= $profile->getNickname() ?></span><br />
<img src="<?= $profile->getAvatarUrl('small') ?>" alt="Profile avatar: <?= $profile->getNickname() ?>" style="height: 65px;width: 65px;"/>
<span class="label label-info"><?= \Ffcms\Core\Helper\Date::convertToDatetime($profile->created_at, 'd.m.Y') ?></span>
</a>
</div>
<?php endforeach; ?>
</div>
<div class="row">
<div class="col-md-12">
<?= \Ffcms\Core\Helper\Url::link(['profile/index', 'all'], __('Show all users')) ?>
</div>
</div>

0 comments on commit cf3d682

Please sign in to comment.