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)
|
2013-10-17 22:57:32 +02:00
|
|
|
{
|
|
|
|
if (is_numeric($key) and !$disallowNumeric)
|
|
|
|
{
|
2013-10-28 11:19:15 +01:00
|
|
|
$post = R::findOne(self::getTableName(), 'id = ?', [$key]);
|
2013-10-17 22:57:32 +02:00
|
|
|
if (!$post)
|
2013-10-21 14:32:47 +02:00
|
|
|
{
|
|
|
|
if ($throw)
|
|
|
|
throw new SimpleException('Invalid post ID "' . $key . '"');
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-17 22:57:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-28 11:19:15 +01:00
|
|
|
$post = R::findOne(self::getTableName(), 'name = ?', [$key]);
|
2013-10-17 22:57:32 +02:00
|
|
|
if (!$post)
|
2013-10-21 14:32:47 +02:00
|
|
|
{
|
|
|
|
if ($throw)
|
|
|
|
throw new SimpleException('Invalid post name "' . $key . '"');
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-17 22:57:32 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-19 20:58:51 +02:00
|
|
|
public static function validateSource($source)
|
|
|
|
{
|
|
|
|
$source = trim($source);
|
|
|
|
|
|
|
|
$maxLength = 100;
|
|
|
|
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';
|
|
|
|
}
|
2013-10-15 20:22:52 +02:00
|
|
|
}
|