Skip to content
Permalink
Browse files

User input eraser add, content app imrpove, etc

  • Loading branch information...
zenn1989 committed Jan 3, 2019
1 parent ef80b0f commit df5b6ffd06e655bab56a472c4b8f86c4d4c999c5
@@ -27,7 +27,6 @@
* @property int $views
* @property int $rating
* @property string $source
* @property string $comment_hash
* @property string $created_at
* @property string $updated_at
* @property string|null $deleted_at
@@ -27,7 +27,8 @@ trait ActionUpdate
public function update(?string $id = null): ?string
{
// get item with trashed objects
$record = ContentEntity::withTrashed()
$record = ContentEntity::with(['commentPosts', 'commentPosts.user', 'commentPosts.user.profile'])
->withTrashed()
->findOrNew($id);
$isNew = $record->id === null;
$cloneId = (int)$this->request->query->get('from', 0);
@@ -28,6 +28,10 @@ class User extends AdminController
delete as actionDelete;
}
use User\ActionClear {
clear as actionClear;
}
use User\ActionInviteList {
inviteList as actionInvitelist;
}
@@ -0,0 +1,51 @@
<?php
namespace Apps\Controller\Admin\User;
use Apps\ActiveRecord\User;
use Apps\Model\Admin\User\FormUserClear;
use Ffcms\Core\App;
use Ffcms\Core\Arch\View;
use Ffcms\Core\Exception\NotFoundException;
use Ffcms\Core\Network\Request;
use Ffcms\Core\Network\Response;
/**
* Trait ActionClear
* @package Apps\Controller\Admin\User
* @property Request $request
* @property Response $response
* @property View $view
*/
trait ActionClear
{
/** Cleanup user added data - content, comments, feedback
* @param string $id
* @return string|null
* @throws NotFoundException
* @throws \Ffcms\Core\Exception\SyntaxException
*/
public function clear($id): ?string
{
// find user object by passed id
$user = User::with('profile')->find($id);
if (!$user) {
throw new NotFoundException(__('User not found'));
}
// initialize and process form model
$model = new FormUserClear($user);
if ($model->send() && $model->validate()) {
$model->make();
App::$Session->getFlashBag()->add('success', __('User input data clear successful'));
$this->response->redirect('user/index');
}
// render output view
return $this->view->render('user/user_clear', [
'model' => $model
]);
}
}
@@ -2,9 +2,11 @@
namespace Apps\Model\Admin\Content;
use Apps\ActiveRecord\CommentPost;
use Apps\ActiveRecord\Content;
use Apps\ActiveRecord\ContentCategory;
use Apps\ActiveRecord\ContentTag;
use Apps\ActiveRecord\User;
use Ffcms\Core\App;
use Ffcms\Core\Arch\Model;
use Ffcms\Core\Helper\Crypt;
@@ -14,6 +16,7 @@
use Ffcms\Core\Helper\Type\Any;
use Ffcms\Core\Helper\Type\Integer;
use Ffcms\Core\Helper\Type\Str;
use Illuminate\Support\Collection;
/**
* Class FormContentUpdate. Create and update content items business model
@@ -30,7 +33,7 @@ class FormContentUpdate extends Model
public $metaTitle;
public $metaKeywords = [];
public $metaDescription = [];
public $display = '1';
public $display = 1;
public $source;
public $addRating = 0;
public $createdAt;
@@ -44,7 +47,7 @@ class FormContentUpdate extends Model
/**
* FormContentUpdate constructor. Pass content active record inside
* @param Content $content
* @param Content|Collection $content
* @param int $cloneId
*/
public function __construct(Content $content, int $cloneId = 0)
@@ -189,10 +192,6 @@ public function save()
if ((int)$this->addRating !== 0) {
$this->_content->rating += (int)$this->addRating;
}
// check if special comment hash is exist
if ($this->_new || Str::length($this->_content->comment_hash) < 32) {
$this->_content->comment_hash = $this->generateCommentHash();
}
// check if date is updated
if (!Str::likeEmpty($this->createdAt) && !Str::startsWith('0000', Date::convertToDatetime($this->createdAt, Date::FORMAT_SQL_TIMESTAMP))) {
@@ -212,7 +211,7 @@ public function save()
$this->_content->save();
// update tags data in special table (relation: content->content_tags = oneToMany)
ContentTag::where('content_id', '=', $this->_content->id)->delete();
ContentTag::where('content_id', $this->_content->id)->delete();
$insertData = [];
foreach ($this->metaKeywords as $lang => $keys) {
// split keywords to tag array
@@ -243,7 +242,7 @@ public function save()
* Get allowed category ids as array
* @return array
*/
public function categoryIds()
public function categoryIds(): ?array
{
$data = ContentCategory::getSortedCategories();
return array_keys($data);
@@ -253,34 +252,59 @@ public function categoryIds()
* Validate path filter
* @return bool
*/
public function validatePath()
public function validatePath(): bool
{
// try to find this item
$find = Content::where('path', '=', $this->path);
$find = Content::where('path', $this->path);
// exclude self id
if ($this->_content->id !== null && Any::isInt($this->_content->id)) {
if ($this->_content->id && Any::isInt($this->_content->id)) {
$find->where('id', '!=', $this->_content->id);
}
// limit only current category id
$find->where('category_id', '=', $this->categoryId);
$find->where('category_id', $this->categoryId);
return $find->count() < 1;
}
/**
* Generate random string for comment hash value
* @return string
* Get users id->nick+mail list
* @return array|null
*/
private function generateCommentHash()
public function getUserIdName(): ?array
{
$hash = Crypt::randomString(mt_rand(32, 128));
$find = Content::where('comment_hash', '=', $hash)->count();
// hmmm, is always exist? Chance of it is TOOOO low, but lets recursion re-generate
if ($find !== 0) {
return $this->generateCommentHash();
$users = [];
User::with('profile')->get()->each(function ($user) use (&$users) {
/** @var User $user */
$users[$user->id] = ($user->profile->nick ?? 'id' . $user->id) . ' (' . $user->email . ')';
});
return $users;
}
/**
* Check if news is new
* @return bool
*/
public function isNew(): bool
{
return $this->_new;
}
/**
* Get content comments
* @return CommentPost[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|null
*/
public function getComments()
{
if ($this->isNew()) {
return null;
}
return $hash;
return $this->_content->commentPosts;
/**return CommentPost::with(['user', 'user.profile'])
->where('app_name', 'content')
->where('app_relation_id', $this->_content->id)
->get();*/
}
}
@@ -0,0 +1,107 @@
<?php
namespace Apps\Model\Admin\User;
use Apps\ActiveRecord\CommentAnswer;
use Apps\ActiveRecord\CommentPost;
use Apps\ActiveRecord\Content;
use Apps\ActiveRecord\ContentRating;
use Apps\ActiveRecord\ContentTag;
use Apps\ActiveRecord\FeedbackPost;
use Apps\ActiveRecord\User;
use Apps\ActiveRecord\WallAnswer;
use Apps\ActiveRecord\WallPost;
use Ffcms\Core\Arch\Model;
/**
* Class FormUserClear
* @package Apps\Model\Admin\User
*/
class FormUserClear extends Model
{
public $comments;
public $content;
public $feedback;
public $wall;
/** @var User */
private $_user;
/**
* FormUserClear constructor. Pass user model inside
* @param User $user
*/
public function __construct(User $user)
{
$this->_user = $user;
parent::__construct(true);
}
/**
* Validation rules
* @return array
*/
public function rules(): array
{
return [
[['comments', 'content', 'feedback', 'wall'], 'required'],
[['comments', 'content', 'feedback', 'wall'], 'boolean']
];
}
/**
* Display labels
* @return array
*/
public function labels(): array
{
return [
'comments' => __('Comments and answers'),
'content' => __('Content'),
'feedback' => __('Feedback requests'),
'wall' => __('Wall posts and answers')
];
}
/**
* Make delete
* @throws \Exception
*/
public function make()
{
if ((bool)$this->comments) {
CommentPost::where('user_id', $this->_user->id)->delete();
CommentAnswer::where('user_id', $this->_user->id)->delete();
}
if ((bool)$this->content) {
$contents = Content::where('author_id', $this->_user->id);
$ids = $contents->pluck('id')->toArray();
if ($ids && count($ids) > 0) {
ContentTag::whereIn('content_id', $ids)->delete();
ContentRating::whereIn('content_id', $ids)->delete();
$contents->delete();
}
}
if ((bool)$this->feedback) {
FeedbackPost::where('user_id', $this->_user->id)
->update(['readed' => true, 'closed' => true]);
}
if ((bool)$this->wall) {
WallPost::where('sender_id', $this->_user->id)->delete();
WallAnswer::where('user_id', $this->_user->id)->delete();
}
}
/**
* Get user object
* @return User
*/
public function getUser()
{
return $this->_user;
}
}
@@ -14,7 +14,9 @@
*/
class FormUserDelete extends Model
{
/** @var User[] */
public $users;
public $delete = false;
private $_ids;
@@ -35,8 +37,9 @@ public function before()
{
// try to find each user
foreach ($this->_ids as $id) {
/** @var User $user */
$user = App::$User->identity($id);
if ($user !== null) {
if ($user) {
$this->users[] = $user;
}
}
@@ -50,7 +53,20 @@ public function labels(): array
{
return [
'email' => __('Email'),
'login' => __('Login')
'login' => __('Login'),
'delete' => __('Delete user content')
];
}
/**
* Validation rules
* @return array
*/
public function rules(): array
{
return [
['delete', 'required'],
['delete', 'boolean']
];
}
@@ -61,12 +77,17 @@ public function labels(): array
public function delete()
{
foreach ($this->users as $user) {
/** @var User $user */
$uid = $user->getParam('id');
// delete wall records
WallPost::where('target_id', '=', $uid)
->orWhere('sender_id', '=', $uid)
->delete();
$uid = $user->id;
// delete whole website info for this user
if ((bool)$this->delete) {
$model = new FormUserClear($user);
$model->comments = true;
$model->content = true;
$model->feedback = true;
$model->wall = true;
$model->make();
}
// delete avatars
File::remove('/upload/user/avatar/big/' . $uid . '.jpg');
File::remove('/upload/user/avatar/medium/' . $uid . '.jpg');

0 comments on commit df5b6ff

Please sign in to comment.
You can’t perform that action at this time.