Files
Phraseanet/lib/Doctrine/Repositories/StoryWZRepository.php
2012-02-15 19:29:03 +01:00

120 lines
2.5 KiB
PHP

<?php
namespace Repositories;
use Doctrine\ORM\EntityRepository;
/**
* StoryWZRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class StoryWZRepository extends EntityRepository
{
public function findByUser(\User_Adapter $user, $sort)
{
$dql = 'SELECT s FROM Entities\StoryWZ s WHERE s.usr_id = :usr_id ';
if ($sort == 'date')
{
$dql .= ' ORDER BY s.created DESC';
}
$query = $this->_em->createQuery($dql);
$query->setParameters(array('usr_id' => $user->get_id()));
$stories = $query->getResult();
foreach ($stories as $key => $story)
{
try
{
$story->getRecord()->get_title();
}
catch (\Exception_Record_AdapterNotFound $e)
{
$this->getEntityManager()->remove($story);
unset($stories[$key]);
}
}
$this->getEntityManager()->flush();
if ($sort == 'name')
{
uasort($stories, array('\\Repositories\\StoryWZRepository', 'title_compare'));
}
return $stories;
}
public function findByUserAndId(\User_Adapter $user, $id)
{
$story = $this->find($id);
if($story)
{
try
{
$story->getRecord()->get_title();
}
catch (\Exception_Record_AdapterNotFound $e)
{
$this->getEntityManager()->remove($story);
throw new \Exception_NotFound('Story not found');
}
if($story->getUser()->get_id() !== $user->get_id())
{
throw new \Exception_Forbidden('You have not access to ths story');
}
}
else
{
throw new \Exception_NotFound('Story not found');
}
return $story;
}
protected static function title_compare(\Entities\StoryWZ $a, \Entities\StoryWZ $b)
{
if ($a->getRecord()->get_title() == $b->getRecord()->get_title())
{
return 0;
}
return ($a->getRecord()->get_title() < $b->getRecord()->get_title()) ? -1 : 1;
}
public function findUserStory(\User_Adapter $user, \record_adapter $Story)
{
$story = $this->findOneBy(
array(
'usr_id' => $user->get_id(),
'sbas_id' => $Story->get_sbas_id(),
'record_id' => $Story->get_record_id(),
)
);
if ($story)
{
try
{
$record = $story->getRecord();
}
catch (\Exception_Record_AdapterNotFound $e)
{
$this->getEntityManager()->remove($story);
$this->getEntityManager()->flush();
$story = null;
}
}
return $story;
}
}