Skip to content

Commit

Permalink
Merge c5c617f into f7f4425
Browse files Browse the repository at this point in the history
  • Loading branch information
carakas committed Jan 17, 2019
2 parents f7f4425 + c5c617f commit c4c6390
Show file tree
Hide file tree
Showing 47 changed files with 374 additions and 347 deletions.
3 changes: 2 additions & 1 deletion src/Backend/Core/Engine/FormImage.php
Expand Up @@ -2,6 +2,7 @@

namespace Backend\Core\Engine;

use ForkCMS\Utility\Thumbnails;
use SpoonFilter;
use SpoonFormImage;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function generateThumbnails($path, $filename): void
$this->moveFile($path . '/source/' . $filename);

// generate the thumbnails
Model::generateThumbnails($path, $path . '/source/' . $filename);
Model::get(Thumbnails::class)->generate($path, $path . '/source/' . $filename);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Backend/Core/Engine/Model.php
Expand Up @@ -865,7 +865,7 @@ public static function submitSpam(
*
* @param int $id The id for the extra.
* @param string $key The key you want to update.
* @param string|array $value The new value.
* @param mixed $value The new value.
*
* @throws Exception If key parameter is not allowed
*/
Expand Down Expand Up @@ -899,14 +899,14 @@ public static function updateExtraData(int $id, string $key, $value): void
{
$database = self::getContainer()->get('database');

$data = (string) $database->getVar(
$serializedData = (string) $database->getVar(
'SELECT i.data
FROM modules_extras AS i
WHERE i.id = ?',
[$id]
);

$data = $data === null ? [] : unserialize($data);
$data = empty($serializedData) ? [] : unserialize($serializedData);
$data[$key] = $value;
$database->update('modules_extras', ['data' => serialize($data)], 'id = ?', [$id]);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Backend/Core/Engine/Url.php
Expand Up @@ -333,6 +333,10 @@ public function getAction(): string

public function getModule(): string
{
if ($this->module === null) {
throw new Exception('Module has not yet been set.');
}

return $this->module;
}

Expand All @@ -343,11 +347,6 @@ private function setAction(string $action, string $module = null): void
$this->setModule($module);
}

// check if module is set
if ($this->getModule() === null) {
throw new Exception('Module has not yet been set.');
}

// is this action allowed?
if (!Authentication::isAllowedAction($action, $this->getModule())) {
// set correct headers
Expand Down
54 changes: 33 additions & 21 deletions src/Backend/Modules/Blog/Actions/Edit.php
Expand Up @@ -3,6 +3,7 @@
namespace Backend\Modules\Blog\Actions;

use Backend\Modules\Blog\Form\BlogDeleteType;
use ForkCMS\Utility\Thumbnails;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
use Backend\Core\Engine\Base\ActionEdit as BackendBaseActionEdit;
Expand Down Expand Up @@ -389,7 +390,7 @@ private function validateForm(): void
// only copy if the new name differs from the old filename
if (preg_replace($regex, '$1', $newName) != preg_replace($regex, '$1', $item['image'])) {
// loop folders
foreach (BackendModel::getThumbnailFolders($imagePath, true) as $folder) {
foreach ($this->get(Thumbnails::class)->getFolders($imagePath, true) as $folder) {
$filesystem->copy($folder['path'] . '/' . $item['image'], $folder['path'] . '/' . $newName);
}

Expand All @@ -414,38 +415,49 @@ private function validateForm(): void
$this->url->getModule()
);

// active
if ($item['status'] == 'active') {
if ($item['status'] === 'active') {
// edit search index
BackendSearchModel::saveIndex(
$this->getModule(),
$item['id'],
['title' => $item['title'], 'text' => $item['text']]
);

// build URL
$redirectUrl = BackendModel::createUrlForAction('Index') .
'&report=edited&var=' . rawurlencode($item['title']) .
'&id=' . $this->id . '&highlight=row-' . $item['revision_id'];
} elseif ($item['status'] == 'draft') {
// draft: everything is saved, so redirect to the edit action
$redirectUrl = BackendModel::createUrlForAction('Edit') .
'&report=saved-as-draft&var=' . rawurlencode($item['title']) .
'&id=' . $item['id'] . '&draft=' . $item['revision_id'] .
'&highlight=row-' . $item['revision_id'];
}

// append to redirect URL
if ($this->categoryId != null) {
$redirectUrl .= '&category=' . $this->categoryId;
}

// everything is saved, so redirect to the overview
$this->redirect($redirectUrl);
$this->redirect($this->getRedirectUrl($item));
}
}
}

private function getRedirectUrl(array $blogPost): string
{
$redirectAction = 'Index';

$parameters = [
'id' => $blogPost['id'],
'highlight=row' => $blogPost['revision_id'],
'var' => $blogPost['title'],
'report' => 'edited',
];

if ($this->categoryId !== null) {
$parameters['category'] = $this->categoryId;
}

if ($blogPost['status'] === 'draft') {
$redirectAction = 'Edit';
$parameters['report'] = 'saved-as-draft';
$parameters['draft'] = $blogPost['revision_id'];
}

return BackendModel::createUrlForAction($redirectAction) . '&' . http_build_query(
$parameters,
null,
'&',
PHP_QUERY_RFC3986
);
}

private function loadDeleteForm(): void
{
$deleteForm = $this->createForm(
Expand Down
79 changes: 30 additions & 49 deletions src/Backend/Modules/Blog/Engine/Model.php
Expand Up @@ -7,6 +7,7 @@
use Backend\Core\Engine\Model as BackendModel;
use Backend\Core\Language\Language as BL;
use Backend\Modules\Tags\Engine\Model as BackendTagsModel;
use ForkCMS\Utility\Thumbnails;

/**
* In this file we store all generic functions that we will be using in the blog module
Expand Down Expand Up @@ -168,7 +169,7 @@ public static function delete($ids): void
$images = $database->getColumn('SELECT image FROM blog_posts WHERE id IN (' . $idPlaceHolders . ')', $ids);

foreach ($images as $image) {
BackendModel::deleteThumbnails(FRONTEND_FILES_PATH . '/Blog/images', $image);
BackendModel::get(Thumbnails::class)->delete(FRONTEND_FILES_PATH . '/Blog/images', $image);
}

// delete records
Expand Down Expand Up @@ -353,34 +354,13 @@ public static function get(int $id): array
* Get the comments
*
* @param string $status The type of comments to get.
* @param int $limit The maximum number of items to retrieve.
* @param int $offset The offset.
* @param int $limit The maximum number of items to retrieve.
* @param int $offset The offset.
*
* @return array
*/
public static function getAllCommentsForStatus(string $status, int $limit = 30, int $offset = 0): array
{
if ($status !== null) {
$status = (string) $status;
}
$limit = (int) $limit;
$offset = (int) $offset;

// no status passed
if ($status === null) {
return (array) BackendModel::getContainer()->get('database')->getRecords(
'SELECT i.id, UNIX_TIMESTAMP(i.created_on) AS created_on, i.author, i.email, i.website, i.text, i.type, i.status,
p.id AS post_id, p.title AS post_title, m.url AS post_url, p.language AS post_language
FROM blog_comments AS i
INNER JOIN blog_posts AS p ON i.post_id = p.id AND i.language = p.language
INNER JOIN meta AS m ON p.meta_id = m.id
WHERE i.language = ?
GROUP BY i.id
LIMIT ?, ?',
[BL::getWorkingLanguage(), $offset, $limit]
);
}

return (array) BackendModel::getContainer()->get('database')->getRecords(
'SELECT i.id, UNIX_TIMESTAMP(i.created_on) AS created_on, i.author, i.email, i.website, i.text, i.type, i.status,
p.id AS post_id, p.title AS post_title, m.url AS post_url, p.language AS post_language
Expand Down Expand Up @@ -470,7 +450,7 @@ public static function getCategory($id): array
/**
* Get a category id by title
*
* @param string $title The title of the category.
* @param string $title The title of the category.
* @param string $language The language to use, if not provided we will use the working language.
*
* @return int
Expand Down Expand Up @@ -546,7 +526,7 @@ public static function getCommentStatusCount(): array
* Get the latest comments for a given type
*
* @param string $status The status for the comments to retrieve.
* @param int $limit The maximum number of items to retrieve.
* @param int $limit The maximum number of items to retrieve.
*
* @return array
*/
Expand Down Expand Up @@ -588,7 +568,7 @@ public static function getMaximumId(): int
/**
* Get all data for a given revision
*
* @param int $id The id of the item.
* @param int $id The id of the item.
* @param int $revisionId The revision to get.
*
* @return array
Expand All @@ -608,7 +588,7 @@ public static function getRevision(int $id, int $revisionId): array
* Retrieve the unique URL for an item
*
* @param string $url The URL to base on.
* @param int $id The id of the item to ignore.
* @param int $id The id of the item to ignore.
*
* @return string
*/
Expand Down Expand Up @@ -659,7 +639,7 @@ public static function getUrl(string $url, int $id = null): string
* Retrieve the unique URL for a category
*
* @param string $url The string whereon the URL will be based.
* @param int $id The id of the category to ignore.
* @param int $id The id of the category to ignore.
*
* @return string
*/
Expand Down Expand Up @@ -736,9 +716,9 @@ public static function insert(array $item): int
* The comments array is an array of arrays with comment properties. A comment should have
* at least 'author', 'email', and 'text' properties.
*
* @param array $item The data to insert.
* @param array $meta The metadata to insert.
* @param array $tags The tags to connect to this post.
* @param array $item The data to insert.
* @param array $meta The metadata to insert.
* @param array $tags The tags to connect to this post.
* @param array $comments The comments attached to this post.
*
* @throws Exception
Expand Down Expand Up @@ -792,9 +772,6 @@ public static function insertCompletePost(array $item, array $meta = [], $tags =
}

// Build meta
if (!is_array($meta)) {
$meta = [];
}
if (!isset($meta['keywords'])) {
$meta['keywords'] = $item['title'];
}
Expand Down Expand Up @@ -987,8 +964,16 @@ public static function reCalculateCommentCount(array $ids): bool
public static function update(array $item): int
{
$database = BackendModel::getContainer()->get('database');

// get the record of the exact item we're editing
$revision = self::getRevision($item['id'], $item['revision_id']);

// assign values
$item['created_on'] = BackendModel::getUTCDate('Y-m-d H:i:s', $revision['created_on']);
$item['num_comments'] = $revision['num_comments'];

// check if new version is active
if ($item['status'] == 'active') {
if ($item['status'] === 'active') {
// archive all older active versions
$database->update(
'blog_posts',
Expand All @@ -997,15 +982,8 @@ public static function update(array $item): int
[$item['id'], $item['status']]
);

// get the record of the exact item we're editing
$revision = self::getRevision($item['id'], $item['revision_id']);

// assign values
$item['created_on'] = BackendModel::getUTCDate('Y-m-d H:i:s', $revision['created_on']);
$item['num_comments'] = $revision['num_comments'];

// if it used to be a draft that we're now publishing, remove drafts
if ($revision['status'] == 'draft') {
if ($revision['status'] === 'draft') {
$database->delete(
'blog_posts',
'id = ? AND status = ?',
Expand All @@ -1021,7 +999,7 @@ public static function update(array $item): int
$rowsToKeep = (int) BackendModel::get('fork.settings')->get('Blog', 'max_num_revisions', 20);

// set type of archive
$archiveType = ($item['status'] == 'active' ? 'archived' : $item['status']);
$archiveType = ($item['status'] === 'active' ? 'archived' : $item['status']);

// get revision-ids for items to keep
$revisionIdsToKeep = (array) $database->getColumn(
Expand Down Expand Up @@ -1059,8 +1037,11 @@ public static function update(array $item): int

// make sure that an image that will be deleted, is not used by a revision that is not to be deleted
foreach ($imagesOfDeletedRevisions as $imageOfDeletedRevision) {
if (!in_array($imageOfDeletedRevision, $imagesToKeep)) {
BackendModel::deleteThumbnails(FRONTEND_FILES_PATH . '/Blog/images', $imageOfDeletedRevision);
if (!in_array($imageOfDeletedRevision, $imagesToKeep, true)) {
BackendModel::get(Thumbnails::class)->delete(
FRONTEND_FILES_PATH . '/Blog/images',
$imageOfDeletedRevision
);
}
}

Expand Down Expand Up @@ -1088,7 +1069,7 @@ public static function update(array $item): int
/**
* Update an existing category
*
* @param array $item The new data.
* @param array $item The new data.
* @param array $meta The new meta-data.
*
* @return int
Expand Down Expand Up @@ -1134,7 +1115,7 @@ public static function updateComment(array $item): int
/**
* Updates one or more comments' status
*
* @param array $ids The id(s) of the comment(s) to change the status for.
* @param array $ids The id(s) of the comment(s) to change the status for.
* @param string $status The new status.
*/
public static function updateCommentStatuses(array $ids, string $status): void
Expand Down
2 changes: 1 addition & 1 deletion src/Backend/Modules/Blog/Tests/Actions/EditTest.php
Expand Up @@ -76,7 +76,7 @@ public function testEditingOurBlogPost(): void

// our url and our page should contain the new title of our blogpost
self::assertContains(
'&report=edited&var=Edited%20blogpost%20for%20functional%20tests&id=1',
'&id=1&highlight%3Drow=2&var=Edited%20blogpost%20for%20functional%20tests&report=edited',
$client->getHistory()->current()->getUri()
);
self::assertContains(
Expand Down
Expand Up @@ -78,19 +78,19 @@ public function __construct(ContentBlock $contentBlock = null)
return;
}

$this->id = $contentBlock->getId();
$this->extraId = $contentBlock->getExtraId();
$this->isVisible = !$contentBlock->isHidden();
$this->title = $contentBlock->getTitle();
$this->text = $contentBlock->getText();
$this->template = $contentBlock->getTemplate();
$this->userId = $contentBlock->getUserId();
$this->locale = $contentBlock->getLocale();
$this->status = $contentBlock->getStatus();
$this->revisionId = $contentBlock->getRevisionId();
$this->id = $this->contentBlockEntity->getId();
$this->extraId = $this->contentBlockEntity->getExtraId();
$this->isVisible = !$this->contentBlockEntity->isHidden();
$this->title = $this->contentBlockEntity->getTitle();
$this->text = $this->contentBlockEntity->getText();
$this->template = $this->contentBlockEntity->getTemplate();
$this->userId = $this->contentBlockEntity->getUserId();
$this->locale = $this->contentBlockEntity->getLocale();
$this->status = $this->contentBlockEntity->getStatus();
$this->revisionId = $this->contentBlockEntity->getRevisionId();
}

public function forOtherLocale(int $id, int $extraId, Locale $locale)
public function forOtherLocale(int $id, int $extraId, Locale $locale): void
{
$this->id = $id;
$this->contentBlockEntity = null;
Expand Down
1 change: 0 additions & 1 deletion src/Backend/Modules/Extensions/Actions/DetailModule.php
Expand Up @@ -26,7 +26,6 @@ class DetailModule extends BackendBaseActionIndex
*
* @var BackendDataGridArray
*/
private $dataGridCronjobs;
private $dataGridEvents;

/**
Expand Down

0 comments on commit c4c6390

Please sign in to comment.