Refactored duplicate code
This commit is contained in:
parent
17013e8fe5
commit
a03d7179ba
4 changed files with 69 additions and 53 deletions
|
@ -348,35 +348,14 @@ class PostController
|
|||
|
||||
|
||||
/* safety */
|
||||
$suppliedSafety = intval(InputHelper::get('safety'));
|
||||
if (!in_array($suppliedSafety, PostSafety::getAll()))
|
||||
throw new SimpleException('Invalid safety type "' . $suppliedSafety . '"');
|
||||
$suppliedSafety = InputHelper::get('safety');
|
||||
$suppliedSafety = Model_Post::validateSafety($suppliedSafety);
|
||||
|
||||
|
||||
/* tags */
|
||||
$suppliedTags = trim(InputHelper::get('tags'));
|
||||
$suppliedTags = preg_split('/[,;\s]+/', $suppliedTags);
|
||||
$suppliedTags = array_filter($suppliedTags, function($x) { return $x != ''; });
|
||||
$suppliedTags = array_unique($suppliedTags);
|
||||
foreach ($suppliedTags as $tag)
|
||||
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
|
||||
throw new SimpleException('Invalid tag "' . $tag . '"');
|
||||
if (empty($suppliedTags))
|
||||
throw new SimpleException('No tags set');
|
||||
|
||||
$dbTags = [];
|
||||
foreach ($suppliedTags as $tag)
|
||||
{
|
||||
$dbTag = R::findOne('tag', 'name = ?', [$tag]);
|
||||
if (!$dbTag)
|
||||
{
|
||||
$dbTag = R::dispense('tag');
|
||||
$dbTag->name = $tag;
|
||||
R::store($dbTag);
|
||||
}
|
||||
$dbTags []= $dbTag;
|
||||
}
|
||||
|
||||
$suppliedTags = InputHelper::get('tags');
|
||||
$suppliedTags = Model_Post::validateTags($suppliedTags);
|
||||
$dbTags = Model_Tag::insertOrUpdate($suppliedTags);
|
||||
|
||||
/* db storage */
|
||||
$dbPost = R::dispense('post');
|
||||
|
@ -421,16 +400,14 @@ class PostController
|
|||
if ($suppliedSafety !== null)
|
||||
{
|
||||
PrivilegesHelper::confirmWithException($this->context->user, Privilege::EditPostSafety, $secondary);
|
||||
$suppliedSafety = intval($suppliedSafety);
|
||||
if (!in_array($suppliedSafety, PostSafety::getAll()))
|
||||
throw new SimpleException('Invalid safety type "' . $suppliedSafety . '"');
|
||||
$suppliedSafety = Model_Post::validateSafety($suppliedSafety);
|
||||
$post->safety = $suppliedSafety;
|
||||
$edited = true;
|
||||
}
|
||||
|
||||
|
||||
/* tags */
|
||||
$suppliedTags = trim(InputHelper::get('tags'));
|
||||
$suppliedTags = InputHelper::get('tags');
|
||||
if ($suppliedTags !== null)
|
||||
{
|
||||
PrivilegesHelper::confirmWithException($this->context->user, Privilege::EditPostTags, $secondary);
|
||||
|
@ -438,28 +415,8 @@ class PostController
|
|||
if (InputHelper::get('tags-token') != $currentToken)
|
||||
throw new SimpleException('Someone else has changed the tags in the meantime');
|
||||
|
||||
$suppliedTags = preg_split('/[,;\s]+/', $suppliedTags);
|
||||
$suppliedTags = array_filter($suppliedTags, function($x) { return $x != ''; });
|
||||
$suppliedTags = array_unique($suppliedTags);
|
||||
foreach ($suppliedTags as $tag)
|
||||
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
|
||||
throw new SimpleException('Invalid tag "' . $tag . '"');
|
||||
if (empty($suppliedTags))
|
||||
throw new SimpleException('No tags set');
|
||||
|
||||
$dbTags = [];
|
||||
foreach ($suppliedTags as $tag)
|
||||
{
|
||||
$dbTag = R::findOne('tag', 'name = ?', [$tag]);
|
||||
if (!$dbTag)
|
||||
{
|
||||
$dbTag = R::dispense('tag');
|
||||
$dbTag->name = $tag;
|
||||
R::store($dbTag);
|
||||
}
|
||||
$dbTags []= $dbTag;
|
||||
}
|
||||
|
||||
$suppliedTags = Model_Post::validateTags($suppliedTags);
|
||||
$dbTags = Model_Tag::insertOrUpdate($suppliedTags);
|
||||
$post->sharedTag = $dbTags;
|
||||
$edited = true;
|
||||
}
|
||||
|
|
39
src/Models/Model_Post.php
Normal file
39
src/Models/Model_Post.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
class Model_Post extends RedBean_SimpleModel
|
||||
{
|
||||
public static function validateSafety($safety)
|
||||
{
|
||||
$safety = intval($safety);
|
||||
|
||||
if (!in_array($safety, PostSafety::getAll()))
|
||||
throw new SimpleException('Invalid safety type "' . $safety . '"');
|
||||
|
||||
return $safety;
|
||||
}
|
||||
|
||||
public static function validateTag($tag)
|
||||
{
|
||||
$tag = trim($tag);
|
||||
|
||||
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
|
||||
throw new SimpleException('Invalid tag "' . $tag . '"');
|
||||
|
||||
return $tag;
|
||||
}
|
||||
|
||||
public static function validateTags($tags)
|
||||
{
|
||||
$tags = trim($tags);
|
||||
$tags = preg_split('/[,;\s]+/', $tags);
|
||||
$tags = array_filter($tags, function($x) { return $x != ''; });
|
||||
$tags = array_unique($tags);
|
||||
|
||||
foreach ($tags as $key => $tag)
|
||||
$tags[$key] = self::validateTag($tag);
|
||||
|
||||
if (empty($tags))
|
||||
throw new SimpleException('No tags set');
|
||||
|
||||
return $tags;
|
||||
}
|
||||
}
|
20
src/Models/Model_Tag.php
Normal file
20
src/Models/Model_Tag.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
class Model_Tag extends RedBean_SimpleModel
|
||||
{
|
||||
public static function insertOrUpdate($tags)
|
||||
{
|
||||
$dbTags = [];
|
||||
foreach ($tags as $tag)
|
||||
{
|
||||
$dbTag = R::findOne('tag', 'name = ?', [$tag]);
|
||||
if (!$dbTag)
|
||||
{
|
||||
$dbTag = R::dispense('tag');
|
||||
$dbTag->name = $tag;
|
||||
R::store($dbTag);
|
||||
}
|
||||
$dbTags []= $dbTag;
|
||||
}
|
||||
return $dbTags;
|
||||
}
|
||||
}
|
|
@ -63,7 +63,7 @@ class Model_User extends RedBean_SimpleModel
|
|||
throw new SimpleException('User with this name is already registered and awaits e-mail confirmation');
|
||||
|
||||
if (!$dbUser->staff_confirmed and \Chibi\Registry::getConfig()->registration->staffActivation)
|
||||
throw new SimpleException('User with this name is already registered and awaits staff confirmation');
|
||||
throw new SimpleException('User with this name is already registered and awaits staff confirmation');
|
||||
|
||||
throw new SimpleException('User with this name is already registered');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue