Skip to content

Commit

Permalink
вынес обработку текста в метода, редактирование статьи
Browse files Browse the repository at this point in the history
  • Loading branch information
visavi committed Jun 2, 2018
1 parent f35562d commit 2406353
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 166 deletions.
22 changes: 16 additions & 6 deletions app/Controllers/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,28 @@ public function rating()
}

/**
* Загрузка фотографии
* Загрузка изображений
*/
public function uploadImage()
{
$image = Request::file('image');
$id = int(Request::input('id'));
$token = check(Request::input('token'));

if ($id) {
$blog = Blog::query()->where('user_id', getUser('id'))->find($id);

if (! $blog) {
exit(json_encode([
'status' => 'error',
'message' => 'Blog not found'
]));
}
}

$existFiles = File::query()
->where('relate_type', Blog::class)
->where('relate_id', 0)
->where('relate_id', $id)
->where('user_id', getUser('id'))
->count();

Expand All @@ -300,7 +312,7 @@ public function uploadImage()
$fileName = uploadFile($image, UPLOADS . '/blogs');

$file = File::query()->create([
'relate_id' => 0,
'relate_id' => $id,
'relate_type' => Blog::class,
'hash' => $fileName,
'name' => $image->getClientOriginalName(),
Expand All @@ -323,7 +335,7 @@ public function uploadImage()
}

/**
* Удаление фотографии
* Удаление изображений
*/
public function deleteImage()
{
Expand Down Expand Up @@ -354,7 +366,5 @@ public function deleteImage()
'message' => current($validator->getErrors())
]);
}


}
}
245 changes: 111 additions & 134 deletions app/Controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,7 @@ public function view($id)
abort(404, 'Данной статьи не существует!');
}

preg_match_all('/\[attach=(.*?)\]/', $blog->text, $attachId);

if (! empty($attachId[1])) {
$attached = array_unique($attachId[1]);

$images = File::query()
->where('relate_type', Blog::class)
->where('relate_id', $blog->id)
->whereIn('id', $attached)
->pluck('hash', 'id')
->all();

if ($images) {
$search = [];
$replace = [];
foreach ($images as $key => $image) {
$search[] = '[attach=' . $key . ']';
$replace[] = '<img class="img-fluid" src="/uploads/blogs/' . $image . '" alt="image">';
}

$blog->text = str_replace($search, $replace, $blog->text);
}
}

$blog->text = $blog->parseAttach();
$text = preg_split('|\[nextpage\](<br * /?>)*|', $blog->text, -1, PREG_SPLIT_NO_EMPTY);

$total = count($text);
Expand Down Expand Up @@ -491,7 +468,8 @@ public function print($id)
abort(404, 'Данной статьи не существует!');
}

$blog['text'] = preg_replace('|\[nextpage\](<br * /?>)*|', '', $blog['text']);
$blog->text = $blog->parseAttach();
$blog->text = preg_replace('|\[nextpage\](<br * /?>)*|', '', $blog->text);

return view('blogs/print', compact('blog'));
}
Expand Down Expand Up @@ -763,130 +741,129 @@ public function top()
*/
public function search()
{
$find = check(Request::input('find'));
$type = int(Request::input('type'));
$where = int(Request::input('where'));
$find = check(Request::input('find'));
$type = int(Request::input('type'));
$where = int(Request::input('where'));

if (! getUser()) {
abort(403, 'Чтобы использовать поиск, необходимо авторизоваться');
}

if (empty($find)) {
return view('blogs/search');
} else {
}

if (! isUtf($find)) {
$find = winToUtf($find);
}

if (! isUtf($find)) {
$find = winToUtf($find);
if (utfStrlen($find) >= 3 && utfStrlen($find) <= 50) {
$findme = utfLower($find);
$findmewords = explode(" ", $findme);

$arrfind = [];
foreach ($findmewords as $valfind) {
if (utfStrlen($valfind) >= 3) {
$arrfind[] = $valfind;
}
}
array_splice($arrfind, 3);

$types = (empty($type)) ? 'AND' : 'OR';
$wheres = (empty($where)) ? 'title' : 'text';

$blogfind = ($types . $wheres . $find);

if (utfStrlen($find) >= 3 && utfStrlen($find) <= 50) {
$findme = utfLower($find);
$findmewords = explode(" ", $findme);
// ----------------------------- Поиск в названии -------------------------------//
if ($wheres === 'title') {

$arrfind = [];
foreach ($findmewords as $valfind) {
if (utfStrlen($valfind) >= 3) {
$arrfind[] = $valfind;
if ($type === 2) {
$arrfind[0] = $findme;
}
$search1 = (isset($arrfind[1]) && $type != 2) ? $types . " `title` LIKE '%" . $arrfind[1] . "%'" : '';
$search2 = (isset($arrfind[2]) && $type != 2) ? $types . " `title` LIKE '%" . $arrfind[2] . "%'" : '';

if (empty($_SESSION['blogfindres']) || $blogfind != $_SESSION['blogfind']) {

$result = Blog::query()
->select('id')
->whereRaw("title like '%".$arrfind[0]."%'".$search1.$search2)
->limit(500)
->pluck('id')
->all();

$_SESSION['blogfind'] = $blogfind;
$_SESSION['blogfindres'] = $result;
}

$total = count($_SESSION['blogfindres']);
$page = paginate(setting('blogpost'), $total);

if ($total > 0) {
$blogs = Blog::query()
->select('blogs.*', 'categories.name')
->whereIn('blogs.id', $_SESSION['blogfindres'])
->join('categories', 'blogs.category_id', '=', 'categories.id')
->orderBy('created_at', 'desc')
->offset($page->offset)
->limit($page->limit)
->with('user')
->get();

return view('blogs/search_title', compact('blogs', 'find', 'page'));
}

setInput(Request::all());
setFlash('danger', 'По вашему запросу ничего не найдено!');
redirect('/blogs/search');
}
array_splice($arrfind, 3);

$types = (empty($type)) ? 'AND' : 'OR';
$wheres = (empty($where)) ? 'title' : 'text';

$blogfind = ($types . $wheres . $find);

// ----------------------------- Поиск в названии -------------------------------//
if ($wheres == 'title') {

if ($type == 2) {
$arrfind[0] = $findme;
}
$search1 = (isset($arrfind[1]) && $type != 2) ? $types . " `title` LIKE '%" . $arrfind[1] . "%'" : '';
$search2 = (isset($arrfind[2]) && $type != 2) ? $types . " `title` LIKE '%" . $arrfind[2] . "%'" : '';

if (empty($_SESSION['blogfindres']) || $blogfind != $_SESSION['blogfind']) {

$result = Blog::query()
->select('id')
->whereRaw("title like '%".$arrfind[0]."%'".$search1.$search2)
->limit(500)
->pluck('id')
->all();

$_SESSION['blogfind'] = $blogfind;
$_SESSION['blogfindres'] = $result;
}

$total = count($_SESSION['blogfindres']);
$page = paginate(setting('blogpost'), $total);

if ($total > 0) {
$blogs = Blog::query()
->select('blogs.*', 'categories.name')
->whereIn('blogs.id', $_SESSION['blogfindres'])
->join('categories', 'blogs.category_id', '=', 'categories.id')
->orderBy('created_at', 'desc')
->offset($page->offset)
->limit($page->limit)
->with('user')
->get();

return view('blogs/search_title', compact('blogs', 'find', 'page'));
} else {
setInput(Request::all());
setFlash('danger', 'По вашему запросу ничего не найдено!');
redirect('/blogs/search');
}
// --------------------------- Поиск в текте -------------------------------//
if ($wheres === 'text') {

if ($type === 2) {
$arrfind[0] = $findme;
}
// --------------------------- Поиск в текте -------------------------------//
if ($wheres == 'text') {

if ($type == 2) {
$arrfind[0] = $findme;
}
$search1 = (isset($arrfind[1]) && $type != 2) ? $types . " `text` LIKE '%" . $arrfind[1] . "%'" : '';
$search2 = (isset($arrfind[2]) && $type != 2) ? $types . " `text` LIKE '%" . $arrfind[2] . "%'" : '';

if (empty($_SESSION['blogfindres']) || $blogfind != $_SESSION['blogfind']) {

$result = Blog::query()
->select('id')
->whereRaw("text like '%".$arrfind[0]."%'".$search1.$search2)
->limit(500)
->pluck('id')
->all();

$_SESSION['blogfind'] = $blogfind;
$_SESSION['blogfindres'] = $result;
}

$total = count($_SESSION['blogfindres']);
$page = paginate(setting('blogpost'), $total);

if ($total > 0) {
$blogs = Blog::query()
->select('blogs.*', 'categories.name')
->whereIn('blogs.id', $_SESSION['blogfindres'])
->join('categories', 'blogs.category_id', '=', 'categories.id')
->orderBy('created_at', 'desc')
->offset($page->offset)
->limit($page->limit)
->with('user')
->get();

return view('blogs/search_text', compact('blogs', 'find', 'page'));
} else {
setInput(Request::all());
setFlash('danger', 'По вашему запросу ничего не найдено!');
redirect('/blogs/search');
}
$search1 = (isset($arrfind[1]) && $type != 2) ? $types . " `text` LIKE '%" . $arrfind[1] . "%'" : '';
$search2 = (isset($arrfind[2]) && $type != 2) ? $types . " `text` LIKE '%" . $arrfind[2] . "%'" : '';

if (empty($_SESSION['blogfindres']) || $blogfind != $_SESSION['blogfind']) {

$result = Blog::query()
->select('id')
->whereRaw("text like '%".$arrfind[0]."%'".$search1.$search2)
->limit(500)
->pluck('id')
->all();

$_SESSION['blogfind'] = $blogfind;
$_SESSION['blogfindres'] = $result;
}
} else {
setInput(Request::all());
setFlash('danger', ['find' => 'Запрос должен содержать от 3 до 50 символов!']);
redirect('/blogs/search');
}

$total = count($_SESSION['blogfindres']);
$page = paginate(setting('blogpost'), $total);

if ($total > 0) {
$blogs = Blog::query()
->select('blogs.*', 'categories.name')
->whereIn('blogs.id', $_SESSION['blogfindres'])
->join('categories', 'blogs.category_id', '=', 'categories.id')
->orderBy('created_at', 'desc')
->offset($page->offset)
->limit($page->limit)
->with('user')
->get();

return view('blogs/search_text', compact('blogs', 'find', 'page'));
}

setInput(Request::all());
setFlash('danger', 'По вашему запросу ничего не найдено!');
redirect('/blogs/search');
}
} else {
setInput(Request::all());
setFlash('danger', ['find' => 'Запрос должен содержать от 3 до 50 символов!']);
redirect('/blogs/search');
}
}
}
34 changes: 34 additions & 0 deletions app/Models/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,38 @@ public static function logTagSize($count, $minCount, $maxCount, $minSize = 10, $

return round($minSize + (log(1 + $count) - $minCount) * ($diffSize / $diffCount));
}

/**
* Обрабатывает вставки изображений в тексте
*
* @return string текст статьт
*/
public function parseAttach()
{
preg_match_all('/\[attach=(.*?)\]/', $this->text, $attachId);

if (! empty($attachId[1])) {
$attached = array_unique($attachId[1]);

$images = File::query()
->where('relate_type', self::class)
->where('relate_id', $this->id)
->whereIn('id', $attached)
->pluck('hash', 'id')
->all();

if ($images) {
$search = [];
$replace = [];
foreach ($images as $key => $image) {
$search[] = '[attach=' . $key . ']';
$replace[] = '<img class="img-fluid" src="/uploads/blogs/' . $image . '" alt="image">';
}

return str_replace($search, $replace, $this->text);
}
}

return $this->text;
}
}

0 comments on commit 2406353

Please sign in to comment.