2015-01-29 16:56:58 +01:00
< ? php
namespace Wallabag\CoreBundle\Controller ;
use Nelmio\ApiDocBundle\Annotation\ApiDoc ;
use Symfony\Bundle\FrameworkBundle\Controller\Controller ;
2015-01-29 20:32:11 +01:00
use Symfony\Component\HttpFoundation\Request ;
2015-02-04 17:54:23 +01:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException ;
2015-02-06 07:45:32 +01:00
use Wallabag\CoreBundle\Entity\Entry ;
2015-02-06 18:15:19 +01:00
use Wallabag\CoreBundle\Entity\Tag ;
2015-01-30 09:05:54 +01:00
use Wallabag\CoreBundle\Service\Extractor ;
2015-01-29 16:56:58 +01:00
2015-01-30 07:50:52 +01:00
class WallabagRestController extends Controller
2015-01-29 16:56:58 +01:00
{
2015-02-10 08:35:43 +01:00
/**
* Retrieve salt for a giver user .
*
* @ ApiDoc (
* parameters = {
* { " name " = " username " , " dataType " = " string " , " required " = true , " description " = " username " }
* }
* )
2015-02-11 11:52:10 +01:00
* @ return array
2015-02-10 08:35:43 +01:00
*/
public function getSaltAction ( $username )
{
$user = $this
-> getDoctrine ()
-> getRepository ( 'WallabagCoreBundle:User' )
-> findOneByUsername ( $username );
if ( is_null ( $user )) {
throw $this -> createNotFoundException ();
}
2015-02-11 15:15:06 +01:00
return array ( $user -> getSalt () ? : null );
2015-02-10 08:35:43 +01:00
}
2015-01-29 16:56:58 +01:00
/**
2015-01-30 07:50:52 +01:00
* Retrieve all entries . It could be filtered by many options .
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
2015-01-30 07:50:52 +01:00
* parameters = {
2015-02-05 18:21:31 +01:00
* { " name " = " archive " , " dataType " = " boolean " , " required " = false , " format " = " true or false, all entries by default " , " description " = " filter by archived status. " },
* { " name " = " star " , " dataType " = " boolean " , " required " = false , " format " = " true or false, all entries by default " , " description " = " filter by starred status. " },
* { " name " = " delete " , " dataType " = " boolean " , " required " = false , " format " = " true or false, default '0' " , " description " = " filter by deleted status. " },
2015-01-30 07:50:52 +01:00
* { " name " = " sort " , " dataType " = " string " , " required " = false , " format " = " 'created' or 'updated', default 'created' " , " description " = " sort entries by date. " },
* { " name " = " order " , " dataType " = " string " , " required " = false , " format " = " 'asc' or 'desc', default 'desc' " , " description " = " order of sort. " },
* { " name " = " page " , " dataType " = " integer " , " required " = false , " format " = " default '1' " , " description " = " what page you want. " },
* { " name " = " perPage " , " dataType " = " integer " , " required " = false , " format " = " default'30' " , " description " = " results per page. " },
* { " name " = " tags " , " dataType " = " string " , " required " = false , " format " = " api%2Crest " , " description " = " a list of tags url encoded. Will returns entries that matches ALL tags. " },
* }
2015-01-29 16:56:58 +01:00
* )
2015-02-06 07:45:32 +01:00
* @ return Entry
2015-01-29 16:56:58 +01:00
*/
2015-01-29 20:32:11 +01:00
public function getEntriesAction ( Request $request )
2015-01-29 16:56:58 +01:00
{
2015-02-04 18:06:42 +01:00
$isArchived = $request -> query -> get ( 'archive' );
$isStarred = $request -> query -> get ( 'star' );
2015-02-04 18:12:13 +01:00
$isDeleted = $request -> query -> get ( 'delete' , 0 );
2015-01-30 07:50:52 +01:00
$sort = $request -> query -> get ( 'sort' , 'created' );
$order = $request -> query -> get ( 'order' , 'desc' );
$page = $request -> query -> get ( 'page' , 1 );
$perPage = $request -> query -> get ( 'perPage' , 30 );
$tags = $request -> query -> get ( 'tags' , array ());
$entries = $this
-> getDoctrine ()
2015-02-06 07:45:32 +01:00
-> getRepository ( 'WallabagCoreBundle:Entry' )
2015-02-09 22:40:20 +01:00
-> findEntries ( $this -> getUser () -> getId (), $isArchived , $isStarred , $isDeleted , $sort , $order );
2015-01-30 07:50:52 +01:00
2015-02-10 13:47:57 +01:00
if ( ! ( $entries )) {
2015-01-30 07:50:52 +01:00
throw $this -> createNotFoundException ();
}
return $entries ;
2015-01-29 16:56:58 +01:00
}
/**
2015-01-30 07:50:52 +01:00
* Retrieve a single entry
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
* requirements = {
* { " name " = " entry " , " dataType " = " integer " , " requirement " = " \ w+ " , " description " = " The entry ID " }
* }
* )
2015-02-06 07:45:32 +01:00
* @ return Entry
2015-01-29 16:56:58 +01:00
*/
2015-02-06 07:45:32 +01:00
public function getEntryAction ( Entry $entry )
2015-01-29 16:56:58 +01:00
{
return $entry ;
}
/**
2015-01-30 07:50:52 +01:00
* Create an entry
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
2015-01-30 07:50:52 +01:00
* parameters = {
* { " name " = " url " , " dataType " = " string " , " required " = true , " format " = " http://www.test.com/article.html " , " description " = " Url for the entry. " },
* { " name " = " title " , " dataType " = " string " , " required " = false , " description " = " Optional, we'll get the title from the page. " },
* { " name " = " tags " , " dataType " = " string " , " required " = false , " format " = " tag1,tag2,tag3 " , " description " = " a comma-separated list of tags. " },
* }
2015-01-29 16:56:58 +01:00
* )
2015-02-06 07:45:32 +01:00
* @ return Entry
2015-01-29 16:56:58 +01:00
*/
2015-01-30 09:05:54 +01:00
public function postEntriesAction ( Request $request )
2015-01-29 16:56:58 +01:00
{
2015-01-30 09:05:54 +01:00
//TODO gérer si on passe les tags
2015-01-31 15:14:10 +01:00
$url = $request -> request -> get ( 'url' );
$content = Extractor :: extract ( $url );
2015-02-10 12:12:44 +01:00
$entry = new Entry ( $this -> getUser ());
2015-01-31 15:14:10 +01:00
$entry -> setUrl ( $url );
2015-02-05 18:19:34 +01:00
$entry -> setTitle ( $request -> request -> get ( 'title' ) ? : $content -> getTitle ());
2015-01-30 09:05:54 +01:00
$entry -> setContent ( $content -> getBody ());
$em = $this -> getDoctrine () -> getManager ();
$em -> persist ( $entry );
$em -> flush ();
return $entry ;
2015-01-29 16:56:58 +01:00
}
/**
2015-01-30 07:50:52 +01:00
* Change several properties of an entry
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
* requirements = {
* { " name " = " entry " , " dataType " = " integer " , " requirement " = " \ w+ " , " description " = " The entry ID " }
2015-01-30 07:50:52 +01:00
* },
* parameters = {
* { " name " = " title " , " dataType " = " string " , " required " = false },
* { " name " = " tags " , " dataType " = " string " , " required " = false , " format " = " tag1,tag2,tag3 " , " description " = " a comma-separated list of tags. " },
2015-02-05 18:21:31 +01:00
* { " name " = " archive " , " dataType " = " boolean " , " required " = false , " format " = " true or false " , " description " = " archived the entry. " },
* { " name " = " star " , " dataType " = " boolean " , " required " = false , " format " = " true or false " , " description " = " starred the entry. " },
* { " name " = " delete " , " dataType " = " boolean " , " required " = false , " format " = " true or false " , " description " = " flag as deleted. Default false. In case that you don't want to *really* remove it.. " },
2015-01-30 07:50:52 +01:00
* }
2015-01-29 16:56:58 +01:00
* )
2015-02-06 07:45:32 +01:00
* @ return Entry
2015-01-29 16:56:58 +01:00
*/
2015-02-06 07:45:32 +01:00
public function patchEntriesAction ( Entry $entry , Request $request )
2015-01-29 16:56:58 +01:00
{
2015-02-05 18:21:31 +01:00
$title = $request -> request -> get ( " title " );
$tags = $request -> request -> get ( " tags " , array ());
$isArchived = $request -> request -> get ( " archive " );
$isDeleted = $request -> request -> get ( " delete " );
$isStarred = $request -> request -> get ( " star " );
if ( ! is_null ( $title )) {
$entry -> setTitle ( $title );
}
if ( ! is_null ( $isArchived )) {
2015-02-05 22:33:36 +01:00
$entry -> setArchived ( $isArchived );
2015-02-05 18:21:31 +01:00
}
if ( ! is_null ( $isDeleted )) {
$entry -> setDeleted ( $isDeleted );
}
if ( ! is_null ( $isStarred )) {
2015-02-05 22:33:36 +01:00
$entry -> setStarred ( $isStarred );
2015-02-05 18:21:31 +01:00
}
$em = $this -> getDoctrine () -> getManager ();
$em -> flush ();
return $entry ;
2015-01-29 16:56:58 +01:00
}
/**
2015-01-30 07:50:52 +01:00
* Delete ** permanently ** an entry
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
2015-01-30 07:50:52 +01:00
* requirements = {
* { " name " = " entry " , " dataType " = " integer " , " requirement " = " \ w+ " , " description " = " The entry ID " }
* }
2015-01-29 16:56:58 +01:00
* )
2015-02-06 07:45:32 +01:00
* @ return Entry
2015-01-29 16:56:58 +01:00
*/
2015-02-06 07:45:32 +01:00
public function deleteEntriesAction ( Entry $entry )
2015-01-29 16:56:58 +01:00
{
2015-02-04 17:54:23 +01:00
if ( $entry -> isDeleted ()) {
throw new NotFoundHttpException ( 'This entry is already deleted' );
}
$em = $this -> getDoctrine () -> getManager ();
$entry -> setDeleted ( 1 );
$em -> flush ();
return $entry ;
2015-01-29 16:56:58 +01:00
}
/**
2015-01-30 07:50:52 +01:00
* Retrieve all tags for an entry
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
* requirements = {
* { " name " = " entry " , " dataType " = " integer " , " requirement " = " \ w+ " , " description " = " The entry ID " }
* }
* )
*/
2015-02-06 07:45:32 +01:00
public function getEntriesTagsAction ( Entry $entry )
2015-01-31 19:09:34 +01:00
{
2015-01-29 16:56:58 +01:00
}
/**
2015-01-30 07:50:52 +01:00
* Add one or more tags to an entry
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
* requirements = {
* { " name " = " entry " , " dataType " = " integer " , " requirement " = " \ w+ " , " description " = " The entry ID " }
2015-01-30 07:50:52 +01:00
* },
* parameters = {
* { " name " = " tags " , " dataType " = " string " , " required " = false , " format " = " tag1,tag2,tag3 " , " description " = " a comma-separated list of tags. " },
* }
2015-01-29 16:56:58 +01:00
* )
*/
2015-02-06 07:45:32 +01:00
public function postEntriesTagsAction ( Entry $entry )
2015-01-31 19:09:34 +01:00
{
2015-01-29 16:56:58 +01:00
}
/**
2015-01-30 07:50:52 +01:00
* Permanently remove one tag for an entry
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
* requirements = {
* { " name " = " tag " , " dataType " = " string " , " requirement " = " \ w+ " , " description " = " The tag " },
* { " name " = " entry " , " dataType " = " integer " , " requirement " = " \ w+ " , " description " = " The entry ID " }
* }
* )
*/
2015-02-06 18:15:19 +01:00
public function deleteEntriesTagsAction ( Entry $entry , Tag $tag )
2015-01-29 16:56:58 +01:00
{
}
/**
2015-01-30 07:50:52 +01:00
* Retrieve all tags
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
* )
*/
2015-01-31 19:09:34 +01:00
public function getTagsAction ()
{
2015-01-29 16:56:58 +01:00
}
/**
2015-01-30 07:50:52 +01:00
* Retrieve a single tag
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
2015-01-30 07:50:52 +01:00
* requirements = {
2015-01-29 16:56:58 +01:00
* { " name " = " tag " , " dataType " = " string " , " requirement " = " \ w+ " , " description " = " The tag " }
2015-01-30 07:50:52 +01:00
* }
2015-01-29 16:56:58 +01:00
* )
*/
2015-02-06 18:15:19 +01:00
public function getTagAction ( Tag $tag )
2015-01-31 19:09:34 +01:00
{
2015-01-29 16:56:58 +01:00
}
/**
2015-01-30 07:50:52 +01:00
* Permanently remove one tag from ** every ** entry
2015-01-29 16:56:58 +01:00
*
* @ ApiDoc (
* requirements = {
* { " name " = " tag " , " dataType " = " string " , " requirement " = " \ w+ " , " description " = " The tag " }
* }
* )
*/
2015-02-06 18:15:19 +01:00
public function deleteTagAction ( Tag $tag )
2015-01-29 16:56:58 +01:00
{
}
2015-01-31 19:09:34 +01:00
}