Skip to content

Commit

Permalink
More generic author handling
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotd committed Apr 29, 2020
1 parent a37b2ab commit 9b075f4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
85 changes: 85 additions & 0 deletions src/Resources/contao/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace DieSchittigs\ContaoContentApiBundle;

use Contao\UserModel;

/**
* ApiContentElement augments ArticleModel for the API.
*/
class Author extends AugmentedContaoModel
{
/**
* constructor.
*
* @param int $id id of the author/user
*/
public function __construct($id)
{
$this->model = UserModel::findById($id, ['disable'], ['']);
if (!$this->model) $this->model = null;
}

public function toJson(): ContaoJson
{
if (!$this->model) {
return parent::toJson();
}
$author = $this->model->row();
unset($author['id']);
unset($author['tstamp']);
unset($author['backendTheme']);
unset($author['themes']);
unset($author['imageSizes']);
unset($author['fullscreen']);
unset($author['uploader']);
unset($author['showHelp']);
unset($author['thumbnails']);
unset($author['useRTE']);
unset($author['useCE']);
unset($author['password']);
unset($author['pwChange']);
unset($author['admin']);
unset($author['groups']);
unset($author['inherit']);
unset($author['modules']);
unset($author['pagemounts']);
unset($author['alpty']);
unset($author['filemounts']);
unset($author['fop']);
unset($author['forms']);
unset($author['formp']);
unset($author['amg']);
unset($author['disable']);
unset($author['start']);
unset($author['stop']);
unset($author['session']);
unset($author['new_records']);
unset($author['tl_page_tree']);
unset($author['tl_page_node']);
unset($author['fieldset_states']);
unset($author['tl_image_size']);
unset($author['tl_page']);
unset($author['tl_user']);
unset($author['tl_settings']);
unset($author['tl_news_archive']);
unset($author['tl_article_tl_page_tree']);
unset($author['filetree']);
unset($author['dateAdded']);
unset($author['secret']);
unset($author['useTwoFactor']);
unset($author['lastLogin']);
unset($author['currentLogin']);
unset($author['locked']);
unset($author['news']);
unset($author['newp']);
unset($author['newsfeeds']);
unset($author['newsfeedp']);
unset($author['trustedTokenVersion']);
unset($author['backupCodes']);
unset($author['loginAttempts']);
unset($author['fields']);
unset($author['elements']);
return new ContaoJson($author);
}
}
9 changes: 1 addition & 8 deletions src/Resources/contao/ContaoJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Contao\Model;
use Contao\Controller;
use Contao\StringUtil;
use Contao\UserModel;

/**
* ContaoJson tries to pack "everything Contao" into a JSON-serializable package.
Expand Down Expand Up @@ -118,13 +117,7 @@ private function handleObject(object $object)
$data->{$key} = (new File($src, $object->size ?? null))->toJson();
}
} else if ($key == 'author' && is_numeric($value)) {
$userModel = UserModel::findById($value);
$author = (object) [
'id' => $userModel->id,
'name' => $userModel->name,
'singleSRC' => $userModel->singleSRC
];
$data->{$key} = new ContaoJson($author);
$data->{$key} = (new Author($value))->toJson();
} else {
$data->{$key} = new self($value);
}
Expand Down

0 comments on commit 9b075f4

Please sign in to comment.