szurubooru/src/Models/Model_Post.php

69 lines
1.4 KiB
PHP
Raw Normal View History

2013-10-15 20:22:52 +02:00
<?php
2013-10-28 11:19:15 +01:00
class Model_Post extends AbstractModel
2013-10-15 20:22:52 +02:00
{
2013-10-21 14:32:47 +02:00
public static function locate($key, $disallowNumeric = false, $throw = true)
{
if (is_numeric($key) and !$disallowNumeric)
{
2013-10-28 11:19:15 +01:00
$post = R::findOne(self::getTableName(), 'id = ?', [$key]);
if (!$post)
2013-10-21 14:32:47 +02:00
{
if ($throw)
throw new SimpleException('Invalid post ID "' . $key . '"');
return null;
}
}
else
{
2013-10-28 11:19:15 +01:00
$post = R::findOne(self::getTableName(), 'name = ?', [$key]);
if (!$post)
2013-10-21 14:32:47 +02:00
{
if ($throw)
throw new SimpleException('Invalid post name "' . $key . '"');
return null;
}
}
return $post;
}
2013-10-15 20:22:52 +02:00
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 validateSource($source)
{
$source = trim($source);
$maxLength = 200;
if (strlen($source) > $maxLength)
throw new SimpleException('Source must have at most ' . $maxLength . ' characters');
return $source;
}
2013-10-28 11:19:15 +01:00
public static function getTableName()
{
return 'post';
}
public static function getQueryBuilder()
{
return 'Model_Post_QueryBuilder';
}
public function isTaggedWith($tagName)
{
$tagName = trim(strtolower($tagName));
foreach ($this->sharedTag as $tag)
if (trim(strtolower($tag->name)) == $tagName)
return true;
return false;
}
2013-10-15 20:22:52 +02:00
}