Skip to content

Commit

Permalink
User input eraser add, content app imrpove, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
zenn1989 committed Jan 3, 2019
1 parent ef80b0f commit df5b6ff
Show file tree
Hide file tree
Showing 20 changed files with 401 additions and 69 deletions.
1 change: 0 additions & 1 deletion Apps/ActiveRecord/Content.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* @property int $views * @property int $views
* @property int $rating * @property int $rating
* @property string $source * @property string $source
* @property string $comment_hash
* @property string $created_at * @property string $created_at
* @property string $updated_at * @property string $updated_at
* @property string|null $deleted_at * @property string|null $deleted_at
Expand Down
3 changes: 2 additions & 1 deletion Apps/Controller/Admin/Content/ActionUpdate.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ trait ActionUpdate
public function update(?string $id = null): ?string public function update(?string $id = null): ?string
{ {
// get item with trashed objects // get item with trashed objects
$record = ContentEntity::withTrashed() $record = ContentEntity::with(['commentPosts', 'commentPosts.user', 'commentPosts.user.profile'])
->withTrashed()
->findOrNew($id); ->findOrNew($id);
$isNew = $record->id === null; $isNew = $record->id === null;
$cloneId = (int)$this->request->query->get('from', 0); $cloneId = (int)$this->request->query->get('from', 0);
Expand Down
4 changes: 4 additions & 0 deletions Apps/Controller/Admin/User.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class User extends AdminController
delete as actionDelete; delete as actionDelete;
} }


use User\ActionClear {
clear as actionClear;
}

use User\ActionInviteList { use User\ActionInviteList {
inviteList as actionInvitelist; inviteList as actionInvitelist;
} }
Expand Down
51 changes: 51 additions & 0 deletions Apps/Controller/Admin/User/ActionClear.php
Original file line number Original file line Diff line number Diff line change
@@ -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
]);
}
}
68 changes: 46 additions & 22 deletions Apps/Model/Admin/Content/FormContentUpdate.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@


namespace Apps\Model\Admin\Content; namespace Apps\Model\Admin\Content;


use Apps\ActiveRecord\CommentPost;
use Apps\ActiveRecord\Content; use Apps\ActiveRecord\Content;
use Apps\ActiveRecord\ContentCategory; use Apps\ActiveRecord\ContentCategory;
use Apps\ActiveRecord\ContentTag; use Apps\ActiveRecord\ContentTag;
use Apps\ActiveRecord\User;
use Ffcms\Core\App; use Ffcms\Core\App;
use Ffcms\Core\Arch\Model; use Ffcms\Core\Arch\Model;
use Ffcms\Core\Helper\Crypt; use Ffcms\Core\Helper\Crypt;
Expand All @@ -14,6 +16,7 @@
use Ffcms\Core\Helper\Type\Any; use Ffcms\Core\Helper\Type\Any;
use Ffcms\Core\Helper\Type\Integer; use Ffcms\Core\Helper\Type\Integer;
use Ffcms\Core\Helper\Type\Str; use Ffcms\Core\Helper\Type\Str;
use Illuminate\Support\Collection;


/** /**
* Class FormContentUpdate. Create and update content items business model * Class FormContentUpdate. Create and update content items business model
Expand All @@ -30,7 +33,7 @@ class FormContentUpdate extends Model
public $metaTitle; public $metaTitle;
public $metaKeywords = []; public $metaKeywords = [];
public $metaDescription = []; public $metaDescription = [];
public $display = '1'; public $display = 1;
public $source; public $source;
public $addRating = 0; public $addRating = 0;
public $createdAt; public $createdAt;
Expand All @@ -44,7 +47,7 @@ class FormContentUpdate extends Model


/** /**
* FormContentUpdate constructor. Pass content active record inside * FormContentUpdate constructor. Pass content active record inside
* @param Content $content * @param Content|Collection $content
* @param int $cloneId * @param int $cloneId
*/ */
public function __construct(Content $content, int $cloneId = 0) public function __construct(Content $content, int $cloneId = 0)
Expand Down Expand Up @@ -189,10 +192,6 @@ public function save()
if ((int)$this->addRating !== 0) { if ((int)$this->addRating !== 0) {
$this->_content->rating += (int)$this->addRating; $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 // check if date is updated
if (!Str::likeEmpty($this->createdAt) && !Str::startsWith('0000', Date::convertToDatetime($this->createdAt, Date::FORMAT_SQL_TIMESTAMP))) { if (!Str::likeEmpty($this->createdAt) && !Str::startsWith('0000', Date::convertToDatetime($this->createdAt, Date::FORMAT_SQL_TIMESTAMP))) {
Expand All @@ -212,7 +211,7 @@ public function save()
$this->_content->save(); $this->_content->save();


// update tags data in special table (relation: content->content_tags = oneToMany) // 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 = []; $insertData = [];
foreach ($this->metaKeywords as $lang => $keys) { foreach ($this->metaKeywords as $lang => $keys) {
// split keywords to tag array // split keywords to tag array
Expand Down Expand Up @@ -243,7 +242,7 @@ public function save()
* Get allowed category ids as array * Get allowed category ids as array
* @return array * @return array
*/ */
public function categoryIds() public function categoryIds(): ?array
{ {
$data = ContentCategory::getSortedCategories(); $data = ContentCategory::getSortedCategories();
return array_keys($data); return array_keys($data);
Expand All @@ -253,34 +252,59 @@ public function categoryIds()
* Validate path filter * Validate path filter
* @return bool * @return bool
*/ */
public function validatePath() public function validatePath(): bool
{ {
// try to find this item // try to find this item
$find = Content::where('path', '=', $this->path); $find = Content::where('path', $this->path);
// exclude self id // 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); $find->where('id', '!=', $this->_content->id);
} }


// limit only current category id // limit only current category id
$find->where('category_id', '=', $this->categoryId); $find->where('category_id', $this->categoryId);

return $find->count() < 1; return $find->count() < 1;
} }


/** /**
* Generate random string for comment hash value * Get users id->nick+mail list
* @return string * @return array|null
*/ */
private function generateCommentHash() public function getUserIdName(): ?array
{ {
$hash = Crypt::randomString(mt_rand(32, 128)); $users = [];
$find = Content::where('comment_hash', '=', $hash)->count(); User::with('profile')->get()->each(function ($user) use (&$users) {
// hmmm, is always exist? Chance of it is TOOOO low, but lets recursion re-generate /** @var User $user */
if ($find !== 0) { $users[$user->id] = ($user->profile->nick ?? 'id' . $user->id) . ' (' . $user->email . ')';
return $this->generateCommentHash(); });

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();*/
} }
} }
107 changes: 107 additions & 0 deletions Apps/Model/Admin/User/FormUserClear.php
Original file line number Original file line Diff line number Diff line change
@@ -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;
}
}
37 changes: 29 additions & 8 deletions Apps/Model/Admin/User/FormUserDelete.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*/ */
class FormUserDelete extends Model class FormUserDelete extends Model
{ {
/** @var User[] */
public $users; public $users;
public $delete = false;


private $_ids; private $_ids;


Expand All @@ -35,8 +37,9 @@ public function before()
{ {
// try to find each user // try to find each user
foreach ($this->_ids as $id) { foreach ($this->_ids as $id) {
/** @var User $user */
$user = App::$User->identity($id); $user = App::$User->identity($id);
if ($user !== null) { if ($user) {
$this->users[] = $user; $this->users[] = $user;
} }
} }
Expand All @@ -50,7 +53,20 @@ public function labels(): array
{ {
return [ return [
'email' => __('Email'), 'email' => __('Email'),
'login' => __('Login') 'login' => __('Login'),
'delete' => __('Delete user content')
];
}

/**
* Validation rules
* @return array
*/
public function rules(): array
{
return [
['delete', 'required'],
['delete', 'boolean']
]; ];
} }


Expand All @@ -61,12 +77,17 @@ public function labels(): array
public function delete() public function delete()
{ {
foreach ($this->users as $user) { foreach ($this->users as $user) {
/** @var User $user */ $uid = $user->id;
$uid = $user->getParam('id'); // delete whole website info for this user
// delete wall records if ((bool)$this->delete) {
WallPost::where('target_id', '=', $uid) $model = new FormUserClear($user);
->orWhere('sender_id', '=', $uid) $model->comments = true;
->delete(); $model->content = true;
$model->feedback = true;
$model->wall = true;
$model->make();
}

// delete avatars // delete avatars
File::remove('/upload/user/avatar/big/' . $uid . '.jpg'); File::remove('/upload/user/avatar/big/' . $uid . '.jpg');
File::remove('/upload/user/avatar/medium/' . $uid . '.jpg'); File::remove('/upload/user/avatar/medium/' . $uid . '.jpg');
Expand Down
Loading

0 comments on commit df5b6ff

Please sign in to comment.