Implemented new enums

This commit is contained in:
Marcin Kurczewski 2014-05-04 19:06:40 +02:00
parent 977989ffed
commit d3beb8bc53
32 changed files with 290 additions and 131 deletions

View file

@ -40,24 +40,22 @@ class Access
$user = Auth::getCurrentUser();
$minAccessRank = AccessRank::Nobody;
$key = TextCaseConverter::convert(Privilege::toString($privilege->primary),
$key = TextCaseConverter::convert($privilege->toString(),
TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE);
if (isset(self::$privileges[$key]))
{
$minAccessRank = self::$privileges[$key];
}
if ($privilege->secondary != null)
{
$key2 = $key . '.' . strtolower($privilege->secondary);
if (isset(self::$privileges[$key2]))
{
$minAccessRank = self::$privileges[$key2];
}
}
$privilege->secondary = null;
$key2 = TextCaseConverter::convert($privilege->toString(),
TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE);
return intval($user->accessRank) >= $minAccessRank;
if (isset(self::$privileges[$key]))
$minAccessRank = self::$privileges[$key];
elseif (isset(self::$privileges[$key2]))
$minAccessRank = self::$privileges[$key2];
return $user->getAccessRank()->toInteger() >= $minAccessRank;
}
public static function assertAuthentication()
@ -98,7 +96,7 @@ class Access
return array_filter(PostSafety::getAll(), function($safety)
{
return Access::check(new Privilege(Privilege::ListPosts, PostSafety::toString($safety)))
return Access::check(new Privilege(Privilege::ListPosts, $safety->toString()))
and Auth::getCurrentUser()->hasEnabledSafety($safety);
});
}

View file

@ -27,7 +27,7 @@ class AddPostJob extends AbstractJob
//- move this to PostEntity::isValid()
//- create IValidatable interface
//- enforce entity validity upon calling save() in models
if (empty($post->type))
if (empty($post->getType()))
throw new SimpleException('No post type detected; upload faled');
//save to db
@ -43,7 +43,7 @@ class AddPostJob extends AbstractJob
: TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($post),
'tags' => TextHelper::reprTags($post->getTags()),
'safety' => PostSafety::toString($post->safety),
'safety' => $post->getSafety()->toString(),
'source' => $post->source]);
//finish

View file

@ -6,9 +6,9 @@ class EditPostSafetyJob extends AbstractPostEditJob
public function execute()
{
$post = $this->post;
$newSafety = $this->getArgument(self::SAFETY);
$newSafety = new PostSafety($this->getArgument(self::SAFETY));
$oldSafety = $post->safety;
$oldSafety = $post->getSafety();
$post->setSafety($newSafety);
if (!$this->skipSaving)
@ -19,7 +19,7 @@ class EditPostSafetyJob extends AbstractPostEditJob
LogHelper::log('{user} changed safety of {post} to {safety}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($post),
'safety' => PostSafety::toString($post->safety)]);
'safety' => $post->getSafety()->toString()]);
}
return $post;

View file

@ -6,13 +6,13 @@ class EditUserAccessRankJob extends AbstractUserEditJob
public function execute()
{
$user = $this->user;
$newAccessRank = UserModel::validateAccessRank($this->getArgument(self::NEW_ACCESS_RANK));
$newAccessRank = new AccessRank($this->getArgument(self::NEW_ACCESS_RANK));
$oldAccessRank = $user->accessRank;
$oldAccessRank = $user->getAccessRank();
if ($oldAccessRank == $newAccessRank)
return $user;
$user->accessRank = $newAccessRank;
$user->setAccessRank($newAccessRank);
if (!$this->skipSaving)
UserModel::save($user);
@ -20,7 +20,7 @@ class EditUserAccessRankJob extends AbstractUserEditJob
LogHelper::log('{user} changed {subject}\'s access rank to {rank}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'subject' => TextHelper::reprUser($user),
'rank' => AccessRank::toString($newAccessRank)]);
'rank' => $newAccessRank->toString()]);
return $user;
}

View file

@ -38,7 +38,7 @@ class GetPostContentJob extends AbstractJob
if ($post->hidden)
$privileges []= new Privilege(Privilege::ViewPost, 'hidden');
$privileges []= new Privilege(Privilege::ViewPost, PostSafety::toString($post->safety));
$privileges []= new Privilege(Privilege::ViewPost, $post->getSafety()->toString());
return $privileges;
}

View file

@ -18,7 +18,7 @@ class GetPostJob extends AbstractPostJob
if ($post->hidden)
$privileges []= new Privilege(Privilege::ViewPost, 'hidden');
$privileges []= new Privilege(Privilege::ViewPost, PostSafety::toString($post->safety));
$privileges []= new Privilege(Privilege::ViewPost, $post->getSafety()->toString());
return $privileges;
}

View file

@ -20,7 +20,7 @@ class GetPostThumbJob extends AbstractJob
if ($post->hidden)
Access::assert(new Privilege(Privilege::ListPosts, 'hidden'));
Access::assert(new Privilege(Privilege::ListPosts, PostSafety::toString($post->safety)));
Access::assert(new Privilege(Privilege::ListPosts, $post->getSafety()->toString()));
$post->generateThumb($width, $height);

View file

@ -70,7 +70,7 @@ class Auth
}
else
{
$_SESSION['logged-in'] = $user->accessRank != AccessRank::Anonymous;
$_SESSION['logged-in'] = $user->getAccessRank()->toInteger() != AccessRank::Anonymous;
$_SESSION['user'] = serialize($user);
}
}
@ -87,7 +87,7 @@ class Auth
$dummy = UserModel::spawn();
$dummy->id = null;
$dummy->name = UserModel::getAnonymousName();
$dummy->accessRank = AccessRank::Anonymous;
$dummy->setAccessRank(new AccessRank(AccessRank::Anonymous));
return $dummy;
}
}

View file

@ -81,7 +81,7 @@ class UserController
$user->enablePostTagTitles(InputHelper::get('post-tag-titles'));
$user->enableHidingDislikedPosts(InputHelper::get('hide-disliked-posts'));
if ($user->accessRank != AccessRank::Anonymous)
if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous)
UserModel::save($user);
if ($user->id == Auth::getCurrentUser()->id)
Auth::setCurrentUser($user);
@ -168,12 +168,12 @@ class UserController
Privilege::ChangeUserSettings,
Access::getIdentity($user)));
if (!in_array($safety, PostSafety::getAll()))
throw new SimpleExcetpion('Invalid safety');
$safety = new PostSafety($safety);
$safety->validate();
$user->enableSafety($safety, !$user->hasEnabledSafety($safety));
if ($user->accessRank != AccessRank::Anonymous)
if ($user->getAccessRank()->toInteger() != AccessRank::Anonymous)
UserModel::save($user);
Auth::setCurrentUser($user);
}

View file

@ -1,21 +1,23 @@
<?php
class Enum
abstract class Enum
{
public static function toString($constant)
public abstract function toString();
public function _toString($constant)
{
$cls = new ReflectionClass(get_called_class());
$constants = $cls->getConstants();
return array_search($constant, $constants);
}
public static function toDisplayString($constant)
public function toDisplayString()
{
return TextCaseConverter::convert(static::toString($constant),
return TextCaseConverter::convert($this->toString(),
TextCaseConverter::SNAKE_CASE,
TextCaseConverter::BLANK_CASE);
}
public static function getAll()
public static function getAllConstants()
{
$cls = new ReflectionClass(get_called_class());
$constants = $cls->getConstants();

5
src/IValidatable.php Normal file
View file

@ -0,0 +1,5 @@
<?php
interface IValidatable
{
function validate();
}

View file

@ -88,8 +88,17 @@ abstract class AbstractCrudModel implements IModel
TextCaseConverter::LOWER_CAMEL_CASE);
}
if (property_exists($entity, $key))
{
$reflectionProperty = new ReflectionProperty(get_class($entity), $key);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($entity, $val);
}
else
{
$entity->$key = $val;
}
}
return $entity;
}

View file

@ -4,22 +4,22 @@ use \Chibi\Database as Database;
class PostEntity extends AbstractEntity
{
public $type;
protected $type;
public $name;
public $origName;
public $fileHash;
public $fileSize;
public $mimeType;
public $safety;
protected $safety;
public $hidden;
public $uploadDate;
public $imageWidth;
public $imageHeight;
public $uploaderId;
public $source;
public $commentCount;
public $favCount;
public $score;
public $commentCount = 0;
public $favCount = 0;
public $score = 0;
public function getUploader()
{
@ -173,9 +173,26 @@ class PostEntity extends AbstractEntity
$this->hidden = boolval($hidden);
}
public function setSafety($safety)
public function getType()
{
$this->safety = PostModel::validateSafety($safety);
return $this->type;
}
public function setType(PostType $type)
{
$type->validate();
$this->type = $type;
}
public function getSafety()
{
return $this->safety;
}
public function setSafety(PostSafety $safety)
{
$safety->validate();
$this->safety = $safety;
}
public function setSource($source)
@ -233,7 +250,7 @@ class PostEntity extends AbstractEntity
$srcPath = $this->getFullPath();
$dstPath = $this->getThumbDefaultPath($width, $height);
if ($this->type == PostType::Youtube)
if ($this->getType()->toInteger() == PostType::Youtube)
{
return ThumbnailHelper::generateFromUrl(
'http://img.youtube.com/vi/' . $this->fileHash . '/mqdefault.jpg',
@ -263,13 +280,13 @@ class PostEntity extends AbstractEntity
case 'image/png':
case 'image/jpeg':
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
$this->type = PostType::Image;
$this->setType(new PostType(PostType::Image));
$this->imageWidth = $imageWidth;
$this->imageHeight = $imageHeight;
break;
case 'application/x-shockwave-flash':
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
$this->type = PostType::Flash;
$this->setType(new PostType(PostType::Flash));
$this->imageWidth = $imageWidth;
$this->imageHeight = $imageHeight;
break;
@ -280,7 +297,7 @@ class PostEntity extends AbstractEntity
case 'video/x-flv':
case 'video/3gpp':
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
$this->type = PostType::Video;
$this->setType(new PostType(PostType::Video));
$this->imageWidth = $imageWidth;
$this->imageHeight = $imageHeight;
break;
@ -315,7 +332,7 @@ class PostEntity extends AbstractEntity
if (preg_match('/youtube.com\/watch.*?=([a-zA-Z0-9_-]+)/', $srcUrl, $matches))
{
$youtubeId = $matches[1];
$this->type = PostType::Youtube;
$this->setType(new PostType(PostType::Youtube));
$this->mimeType = null;
$this->fileSize = null;
$this->fileHash = $youtubeId;
@ -360,7 +377,7 @@ class PostEntity extends AbstractEntity
$x []= TextHelper::reprTag($tag->name);
foreach ($this->getRelations() as $relatedPost)
$x []= TextHelper::reprPost($relatedPost);
$x []= $this->safety;
$x []= $this->getSafety()->toInteger();
$x []= $this->source;
$x []= $this->fileHash;
natcasesort($x);

View file

@ -12,10 +12,21 @@ class UserEntity extends AbstractEntity
public $emailConfirmed;
public $joinDate;
public $lastLoginDate;
public $accessRank;
protected $accessRank;
public $settings;
public $banned;
public function getAccessRank()
{
return $this->accessRank;
}
public function setAccessRank(AccessRank $accessRank)
{
$accessRank->validate();
$this->accessRank = $accessRank;
}
public function getAvatarUrl($size = 32)
{
$subject = !empty($this->emailConfirmed)
@ -44,32 +55,31 @@ class UserEntity extends AbstractEntity
$this->settings = $settings;
}
public function hasEnabledSafety($safety)
public function hasEnabledSafety(PostSafety $safety)
{
$all = $this->getSetting(UserModel::SETTING_SAFETY);
if (!$all)
return $safety == PostSafety::Safe;
return $all & PostSafety::toFlag($safety);
return $safety->toInteger() == PostSafety::Safe;
return $all & $safety->toFlag();
}
public function enableSafety($safety, $enabled)
public function enableSafety(PostSafety $safety, $enabled)
{
$all = $this->getSetting(UserModel::SETTING_SAFETY);
if (!$all)
$all = PostSafety::toFlag(PostSafety::Safe);
$new = $all;
if (!$enabled)
{
$new &= ~PostSafety::toFlag($safety);
if (!$new)
$new = PostSafety::toFlag(PostSafety::Safe);
$new &= ~$safety->toFlag();
}
else
{
$new |= PostSafety::toFlag($safety);
$new |= $safety->toFlag();
}
if (!$new)
$new = (new PostSafety(PostSafety::Safe))->toFlag();
$this->setSetting(UserModel::SETTING_SAFETY, $new);
}

View file

@ -1,5 +1,5 @@
<?php
class AccessRank extends Enum
class AccessRank extends Enum implements IValidatable
{
const Anonymous = 0;
const Registered = 1;
@ -7,4 +7,35 @@ class AccessRank extends Enum
const Moderator = 3;
const Admin = 4;
const Nobody = 5;
protected $accessRank;
public function __construct($accessRank)
{
$this->accessRank = $accessRank;
}
public function toInteger()
{
return $this->accessRank;
}
public function toString()
{
return self::_toString($this->accessRank);
}
public static function getAll()
{
return array_map(function($constantName)
{
return new self($constantName);
}, self::getAllConstants());
}
public function validate()
{
if (!in_array($this->accessRank, self::getAllConstants()))
throw new SimpleException('Invalid access rank "%s"', $this->accessRank);
}
}

View file

@ -1,12 +1,43 @@
<?php
class PostSafety extends Enum
class PostSafety extends Enum implements IValidatable
{
const Safe = 1;
const Sketchy = 2;
const Unsafe = 3;
public static function toFlag($safety)
protected $safety;
public function __construct($safety)
{
return pow(2, $safety);
$this->safety = $safety;
}
public function toInteger()
{
return $this->safety;
}
public function toFlag()
{
return pow(2, $this->safety);
}
public function toString()
{
return self::_toString($this->safety);
}
public static function getAll()
{
return array_map(function($constantName)
{
return new self($constantName);
}, self::getAllConstants());
}
public function validate()
{
if (!in_array($this->safety, self::getAllConstants()))
throw new SimpleException('Invalid safety type "%s"', $this->safety);
}
}

View file

@ -1,8 +1,31 @@
<?php
class PostType extends Enum
class PostType extends Enum implements IValidatable
{
const Image = 1;
const Flash = 2;
const Youtube = 3;
const Video = 4;
protected $type;
public function __construct($type)
{
$this->type = $type;
}
public function toInteger()
{
return $this->type;
}
public function toString()
{
return self::_toString($this->type);
}
public function validate()
{
if (!in_array($this->type, self::getAllConstants()))
throw new SimpleException('Invalid post type "%s"', $this->type);
}
}

View file

@ -51,6 +51,14 @@ class Privilege extends Enum
public function __construct($primary, $secondary = null)
{
$this->primary = $primary;
$this->secondary = $secondary;
$this->secondary = strtolower($secondary);
}
public function toString()
{
$string = self::_toString($this->primary);
if ($this->secondary)
$string .= '.' . $this->secondary;
return $string;
}
}

View file

@ -6,23 +6,33 @@ class PostModel extends AbstractCrudModel
{
protected static $config;
public static function getTableName()
{
return 'post';
}
public static function init()
{
self::$config = getConfig();
}
public static function getTableName()
{
return 'post';
}
public static function convertRow($row)
{
$entity = parent::convertRow($row);
if (isset($row['type']))
$entity->setType(new PostType($row['type']));
if (isset($row['safety']))
$entity->setSafety(new PostSafety($row['safety']));
return $entity;
}
public static function spawn()
{
$post = new PostEntity;
$post->score = 0;
$post->favCount = 0;
$post->commentCount = 0;
$post->safety = PostSafety::Safe;
$post->setSafety(new PostSafety(PostSafety::Safe));
$post->hidden = false;
$post->uploadDate = time();
do
@ -40,13 +50,13 @@ class PostModel extends AbstractCrudModel
self::forgeId($post);
$bindings = [
'type' => $post->type,
'type' => $post->getType()->toInteger(),
'name' => $post->name,
'orig_name' => $post->origName,
'file_hash' => $post->fileHash,
'file_size' => $post->fileSize,
'mime_type' => $post->mimeType,
'safety' => $post->safety,
'safety' => $post->getSafety()->toInteger(),
'hidden' => $post->hidden,
'upload_date' => $post->uploadDate,
'image_width' => $post->imageWidth,
@ -266,16 +276,6 @@ class PostModel extends AbstractCrudModel
public static function validateSafety($safety)
{
$safety = intval($safety);
if (!in_array($safety, PostSafety::getAll()))
throw new SimpleException('Invalid safety type "%s"', $safety);
return $safety;
}
public static function validateSource($source)
{
$source = trim($source);

View file

@ -8,7 +8,12 @@ class CommentSearchParser extends AbstractSearchParser
$this->statement->addInnerJoin('post', new Sql\EqualsFunctor('post_id', 'post.id'));
$crit = new Sql\ConjunctionFunctor();
$allowedSafety = Access::getAllowedSafety();
$allowedSafety = array_map(
function($safety)
{
return $safety->toInteger();
},
Access::getAllowedSafety());
$crit->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety)));
if (!Access::check(new Privilege(Privilege::ListPosts, 'hidden')))

View file

@ -14,8 +14,13 @@ class PostSearchParser extends AbstractSearchParser
$this->tags = [];
$crit = new Sql\ConjunctionFunctor();
$allowedSafety = Access::getAllowedSafety();
$crit->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety)));
$allowedSafety = array_map(
function($safety)
{
return $safety->toInteger();
},
Access::getAllowedSafety());
$crit->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety)));
$this->statement->setCriterion($crit);
if (count($tokens) > $config->browsing->maxSearchTokens)

View file

@ -5,7 +5,12 @@ class TagSearchParser extends AbstractSearchParser
{
protected function processSetup(&$tokens)
{
$allowedSafety = Access::getAllowedSafety();
$allowedSafety = array_map(
function($safety)
{
return $safety->toInteger();
},
Access::getAllowedSafety());
$this->statement
->addInnerJoin('post_tag', new Sql\EqualsFunctor('tag.id', 'post_tag.tag_id'))
->addInnerJoin('post', new Sql\EqualsFunctor('post.id', 'post_tag.post_id'))

View file

@ -14,17 +14,29 @@ class UserModel extends AbstractCrudModel
return 'user';
}
public static function convertRow($row)
{
$entity = parent::convertRow($row);
if (isset($row['access_rank']))
$entity->setAccessRank(new AccessRank($row['access_rank']));
return $entity;
}
public static function spawn()
{
$user = new UserEntity();
$user->setAccessRank(new AccessRank(AccessRank::Anonymous));
$user->passSalt = md5(mt_rand() . uniqid());
return $user;
}
public static function save($user)
{
if ($user->accessRank == AccessRank::Anonymous)
if ($user->getAccessRank()->toInteger() == AccessRank::Anonymous)
throw new Exception('Trying to save anonymous user into database');
Database::transaction(function() use ($user)
{
self::forgeId($user);
@ -38,7 +50,7 @@ class UserModel extends AbstractCrudModel
'email_confirmed' => $user->emailConfirmed,
'join_date' => $user->joinDate,
'last_login_date' => $user->lastLoginDate,
'access_rank' => $user->accessRank,
'access_rank' => $user->getAccessRank()->toInteger(),
'settings' => $user->settings,
'banned' => $user->banned
];
@ -234,15 +246,12 @@ class UserModel extends AbstractCrudModel
return $email;
}
public static function validateAccessRank($accessRank)
public static function validateAccessRank(AccessRank $accessRank)
{
$accessRank = intval($accessRank);
$accessRank->validate();
if (!in_array($accessRank, AccessRank::getAll()))
throw new SimpleException('Invalid access rank type "%s"', $accessRank);
if ($accessRank == AccessRank::Nobody)
throw new SimpleException('Cannot set special accesss rank "%s"', $accessRank);
if ($accessRank->toInteger() == AccessRank::Nobody)
throw new SimpleException('Cannot set special access rank "%s"', $accessRank->toString());
return $accessRank;
}

View file

@ -23,11 +23,11 @@
<label>
<input type="radio"
name="safety"
<?php if ($this->context->transport->post->safety == $safety): ?>
<?php if ($this->context->transport->post->getSafety() == $safety): ?>
checked="checked"
<?php endif ?>
value="<?= $safety ?>"/>
&nbsp;<?= ucfirst(PostSafety::toDisplayString($safety)) ?>
value="<?= $safety->toInteger() ?>"/>
&nbsp;<?= ucfirst($safety->toDisplayString()) ?>
</label>
<?php endforeach ?>
</div>

View file

@ -5,7 +5,7 @@ Assets::setPageThumb(\Chibi\Router::linkTo(
$post = $this->context->transport->post;
?>
<?php if ($post->type == PostType::Image): ?>
<?php if ($post->getType()->toInteger() == PostType::Image): ?>
<?php if (!empty($this->context->imageLink)): ?>
<a href="<?= $this->context->imageLink ?>">
@ -20,7 +20,7 @@ $post = $this->context->transport->post;
</a>
<?php endif ?>
<?php elseif ($post->type == PostType::Flash): ?>
<?php elseif ($post->getType()->toInteger() == PostType::Flash): ?>
<object
type="<?= $post->mimeType ?>"
@ -37,7 +37,7 @@ $post = $this->context->transport->post;
</object>
<?php elseif ($post->type == PostType::Youtube): ?>
<?php elseif ($post->getType()->toInteger() == PostType::Youtube): ?>
<iframe
style="width: 800px; height: 600px; border: 0;"
@ -46,7 +46,7 @@ $post = $this->context->transport->post;
</iframe>
<?php elseif ($post->type == PostType::Video): ?>
<?php elseif ($post->getType()->toInteger() == PostType::Video): ?>
<video style="max-width: 100%" controls>
<source

View file

@ -6,7 +6,7 @@ $classNames =
'post',
'post-type-' . TextCaseConverter::convert(
TextHelper::resolveMimeType($this->context->post->mimeType)
?: PostType::toString($this->context->post->type),
?: $this->context->post->getType()->toString(),
TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE),
];

View file

@ -86,9 +86,9 @@ Assets::addScript('../lib/tagit/jquery.tagit.js');
<?php if (!$checked): ?>
checked="checked"
<?php endif ?>
value="<?= $safety ?>" />
value="<?= $safety->toInteger() ?>" />
<?= ucfirst(PostSafety::toDisplayString($safety)) ?>
<?= ucfirst($safety->toDisplayString()) ?>
<?php $checked = true ?>
</label>

View file

@ -116,7 +116,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="key-value safety">
<span class="key">Safety:</span>
<span class="value safety-<?= $val = PostSafety::toDisplayString($this->context->transport->post->safety) ?>"
<span class="value safety-<?= $val = $this->context->transport->post->getSafety()->toDisplayString() ?>"
title="<?= $val ?>">
<?= $val ?>
</span>
@ -184,7 +184,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
</div>
<div class="unit hl-options">
<?php if ($this->context->transport->post->type != PostType::Youtube): ?>
<?php if ($this->context->transport->post->getType()->toInteger() != PostType::Youtube): ?>
<div class="hl-option">
<a title="Download" href="<?= \Chibi\Router::linkTo(
['PostController', 'fileView'],
@ -370,7 +370,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
</div>
<?php endif ?>
<div class="post-wrapper post-type-<?= PostType::toString($this->context->transport->post->type) ?>">
<div class="post-wrapper post-type-<?= $this->context->transport->post->getType()->toString() ?>">
<?= \Chibi\View::render('post-file-render', $this->context) ?>
</div>

View file

@ -112,24 +112,24 @@
<?php foreach (PostSafety::getAll() as $safety): ?>
<?php if (Access::check(new Privilege(
Privilege::ListPosts,
PostSafety::toString($safety)))): ?>
$safety->toString()))): ?>
<li class="safety-<?= TextCaseConverter::convert(
PostSafety::toString($safety),
$safety->toString(),
TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE) ?>">
<a class="<?= Auth::getCurrentUser()->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>"
href="<?= \Chibi\Router::linkTo(
['UserController', 'toggleSafetyAction'],
['safety' => $safety]) ?>"
['safety' => $safety->toInteger()]) ?>"
title="<?= sprintf('Searching %s posts: %s',
PostSafety::toDisplayString($safety),
$safety->toDisplayString(),
Auth::getCurrentUser()->hasEnabledSafety($safety)
? 'enabled'
: 'disabled') ?>">
<span><?= ucfirst(PostSafety::toDisplayString($safety)) ?></span>
<span><?= ucfirst($safety->toDisplayString()) ?></span>
</a>
</li>

View file

@ -91,14 +91,15 @@
<div class="input-wrapper">
<select name="access-rank" id="access-rank">
<?php foreach (AccessRank::getAll() as $rank): ?>
<?php if ($rank == AccessRank::Nobody) continue ?>
<?php if ($rank == InputHelper::get('access-rank') or (!InputHelper::get('access-rank')
and $rank == $this->context->transport->user->accessRank)): ?>
<option value="<?= $rank ?>" selected="selected">
<?php if ($rank->toInteger() == AccessRank::Nobody) continue ?>
<?php if ($rank->toInteger() == InputHelper::get('access-rank')
or (!InputHelper::get('access-rank') and
$rank == $this->context->transport->user->getAccessRank())): ?>
<option value="<?= $rank->toInteger() ?>" selected="selected">
<?php else: ?>
<option value="<?= $rank ?>">
<option value="<?= $rank->toInteger() ?>">
<?php endif ?>
<?= AccessRank::toDisplayString($rank) ?>
<?= $rank->toDisplayString() ?>
</option>
<?php endforeach ?>
</select>

View file

@ -11,19 +11,19 @@
<?php foreach (PostSafety::getAll() as $safety): ?>
<?php if (Access::check(new Privilege(
Privilege::ListPosts,
PostSafety::toString($safety)))): ?>
$safety->toString()))): ?>
<label>
<?php
$attrs = [];
$attrs['type'] = 'checkbox';
$attrs['name'] = 'safety[]';
$attrs['value'] = $safety;
$attrs['value'] = $safety->toInteger();
if ($this->context->transport->user->hasEnabledSafety($safety))
$attrs['checked'] = 'checked';
echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs);
?>
<?= ucfirst(PostSafety::toDisplayString($safety)) ?>
<?= ucfirst($safety->toDisplayString()) ?>
</label>
<?php endif ?>
<?php endforeach ?>

View file

@ -45,7 +45,7 @@ Assets::addStylesheet('user-view.css');
<span class="key">Access rank:</span>
<span
class="value"
title="<?= $val = AccessRank::toDisplayString($this->context->transport->user->accessRank) ?>">
title="<?= $val = $this->context->transport->user->getAccessRank()->toDisplayString() ?>">
<?= $val ?>