Added popup functionality and Library

* doPopup() JavaScript call creates a centered popup div with user
    defined width, height, title, content, and calls an optional
    callback function

* Library with currently non-modifiable book list, and a popup with the
    books' details

Signed-off-by: Gergely Polonkai <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely
2012-08-08 22:15:51 +02:00
parent 624e56389e
commit 55cde1594b
19 changed files with 1128 additions and 12 deletions

View File

@@ -0,0 +1,135 @@
<?php
namespace KekRozsak\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response;
use KekRozsak\FrontBundle\Entity\Book;
use KekRozsak\FrontBundle\Entity\BookCopy;
class BookController extends Controller
{
/**
* @Route("/konyvtar", name="KekRozsakFrontBundle_bookList")
* @Template()
*/
public function listAction()
{
$query = $this->getDoctrine()->getEntityManager()->createQuery('SELECT b FROM KekRozsakFrontBundle:Book b ORDER BY b.author ASC, b.title ASC, b.year ASC');
$books = $query->getResult();
return array(
'books' => $books,
);
}
/**
* @Route("/konyvadat/{id}/ajax.{_format}", name="KekRozsakFrontBundle_bookAjaxData", defaults={"_format": "html"}, options={"expose": true})
* @Template()
* @ParamConverter("book")
*/
public function ajaxDataAction(Book $book)
{
return array(
'book' => $book,
);
}
/**
* @Route("/konyv/torles/{id}", name="KekRozsakFrontBundle_bookDeleteCopy", requirements={"id": "\d+"}, options={"expose": true})
* @ParamConverter("book")
*/
public function ajaxDeleteBookAction(Book $book)
{
$copies = $book->getUsersCopies($this->get('security.context')->getToken()->getUser());
$em = $this->getDoctrine()->getEntityManager();
$copies->forAll(function($key, $copy) use ($book, $em) {
$book->removeCopy($copy);
$em->remove($copy);
});
$em->persist($book);
$em->flush();
return new Response();
}
/**
* @Route("/konyv/ujpeldany/{id}", name="KekRozsakFrontBundle_bookAddCopy", requirements={"id": "\d+"}, options={"expose": true})
* @ParamConverter("book")
*/
public function ajaxAddBookAction(Book $book)
{
$user = $this->get('security.context')->getToken()->getUser();
$copies = $book->getUsersCopies($user);
if ($copies->count() == 0)
{
$copy = new BookCopy($book, $user);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($copy);
$em->flush();
}
return new Response();
}
/**
* @Route("/konyv/kolcsonozheto/{id}/{newValue}", name="KekRozsakFrontBundle_bookSetCopyBorrowable", requirements={"id": "\d+"}, options={"expose": true})
* @ParamConverter("book")
*/
public function ajaxSetBookCopyBorrowableAction(Book $book, $newValue)
{
$user = $this->get('security.context')->getToken()->getUser();
$copies = $book->getUsersCopies($user);
$em = $this->getDoctrine()->getEntityManager();
$copies->forAll(function($key, $copy) use ($em, $newValue) {
$copy->setBorrowable($newValue);
$em->persist($copy);
});
$em->flush();
return new Response();
}
/**
* @Route("/konyv/megveheto/{id}/{newValue}", name="KekRozsakFrontBundle_bookSetCopyForSale", requirements={"id": "\d+"}, options={"expose": true})
* @ParamConverter("book")
*/
public function ajaxSetBookCopyForSaleAction(Book $book, $newValue)
{
$user = $this->get('security.context')->getToken()->getUser();
$copies = $book->getUsersCopies($user);
$em = $this->getDoctrine()->getEntityManager();
$copies->forAll(function($key, $copy) use ($em, $newValue) {
$copy->setBuyable($newValue);
$em->persist($copy);
});
$em->flush();
return new Response();
}
/**
* @Route("/konyv/szeretnek/{id}/{wantToBuy}", name="KekRozsakFrontBundle_bookWantOne", requirements={"id": "\d+"}, options={"expose": true})
* @ParamConverter("book")
*/
public function ajaxWantABookAction(Book $book, $wantToBuy)
{
$user = $this->get('security.context')->getToken()->getUser();
if ($wantToBuy)
{
$book->addWouldBuy($user);
}
else
{
$book->addWouldBorrow($user);
}
$em = $this->getDoctrine()->getEntityManager();
$em->persist($book);
$em->flush();
return new Response();
}
}

View File

@@ -0,0 +1,299 @@
<?php
namespace KekRozsak\FrontBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
use Doctrine\Common\Collections\ArrayCollection;
use KekRozsak\SecurityBundle\Entity\User;
/**
* KekRozsak\FrontBundle\Entity\Book
*
* @ORM\Entity
* @ORM\Table(name="books")
*
* @DoctrineAssert\UniqueEntity(fields={"author", "title", "year"})
*/
class Book
{
public function __construct()
{
$this->copies = new ArrayCollection();
$this->wouldBorrow = new ArrayCollection();
$this->wouldBuy = new ArrayCollection();
}
/**
* @var integer $id
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @var Doctrine\Common\Collections\ArrayCollection $copies
*
* @ORM\OneToMany(targetEntity="BookCopy", mappedBy="book")
*/
protected $copies;
/**
* Remove a copy
*
* @param KekRozsak\FrontBundle\Entity\BookCopy $copy
* @return Book
*/
public function removeCopy(BookCopy $copy)
{
$this->copies->removeElement($copy);
}
/**
* Get copies
*
* @return Doctrine\Common\Collections\ArrayCollection
*/
public function getCopies()
{
return $this->copies;
}
public function getCopiesBorrowed()
{
return $this->copies->filter(function($copy) {
return ($copy->getBorrower() !== null);
});
}
public function getCopiesBorrowedByUser(User $user)
{
return $this->copies->filter(function($copy) use ($user) {
return ($copy->getBorrower() == $user);
});
}
public function getCopiesBorrowedReturnedByUser(User $user)
{
return $this->copies->filter(function($copy) use ($user) {
return ($copy->getBorrower() == $user) && ($copy->isBorrowerReturned());
});
}
public function getCopiesBorrowable()
{
return $this->copies->filter(function($copy) {
return $copy->isBorrowable();
});
}
public function getUsersCopies(User $user)
{
return $this->copies->filter(function ($copy) use ($user) {
return ($copy->getOwner() == $user);
});
}
public function getUsersCopiesBorrowable(User $user)
{
return $this->copies->filter(function($copy) use ($user) {
return (($copy->getOwner() == $user) && $copy->isBorrowable());
});
}
public function getUsersCopiesBuyable(User $user)
{
return $this->copies->filter(function($copy) use ($user) {
return (($copy->getOwner() == $user) && $copy->isBuyable());
});
}
/**
* @var string $author
*
* @ORM\Column(type="string", length=100, nullable=false)
*/
protected $author;
/**
* Set author
*
* @param string $author
* @return Book
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* @var string $title
*
* @ORM\Column(type="string", length=100, nullable=false)
*/
protected $title;
/**
* Set title
*
* @param string $title
* @return Book
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @var integer $year
*
* @ORM\Column(type="integer", nullable=false)
*/
protected $year;
/**
* Set year
*
* @param integer $year
* @return Book
*/
public function setYear($year)
{
$this->year = $year;
return $this;
}
/**
* Get year
*
* @return integer
*/
public function getYear()
{
return $this->year;
}
/**
* @var boolean $commentable
*
* @ORM\Column(type="boolean", nullable=false)
*/
protected $commentable;
/**
* @var Doctrine\Common\Collections\ArrayCollection $wouldBorrow
*
* @ORM\ManyToMany(targetEntity="KekRozsak\SecurityBundle\Entity\User")
* @ORM\JoinTable(name="book_would_borrow")
*/
protected $wouldBorrow;
/**
* Add a user for want-to-borrowers
*
* @param KekRozsak\SecurityBundle\Entity\User $user
* @return Book
*/
public function addWouldBorrow(User $user)
{
$this->wouldBorrow->add($user);
return $this;
}
/**
* Get wouldBorrow list
*
* @return Doctrine\Common\Collections\ArrayCollection
*/
public function getWouldBorrow()
{
return $this->wouldBorrow;
}
/**
* Check if specified user would borrow this book
*
* @param KekRozsak\SecurityBundle\Entity\User $user
* @return boolean
*/
public function userWouldBorrow(User $user)
{
return $this->wouldBorrow->contains($user);
}
/**
* @var Doctrine\Common\Collections\ArrayCollection $wouldBuy
*
* @ORM\ManyToMany(targetEntity="KekRozsak\SecurityBundle\Entity\User")
* @ORM\JoinTable(name="book_would_buy")
*/
protected $wouldBuy;
/**
* Add a user for want-to-buyers
*
* @param KekRozsak\SecurityBundle\Entity\User $user
* @return Book
*/
public function addWouldBuy(User $user)
{
$this->wouldBuy->add($user);
return $this;
}
/**
* Get wouldBuy list
*
* @return Doctrine\Common\Collections\ArrayCollection
*/
public function getWouldBuy()
{
return $this->wouldBuy;
}
/**
* Check if specified user would buy this book
*
* @param KekRozsak\SecurityBundle\Entity\User $user
* @return boolean
*/
public function userWouldBuy(User $user)
{
return $this->wouldBuy->contains($user);
}
}

View File

@@ -0,0 +1,165 @@
<?php
namespace KekRozsak\FrontBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
use KekRozsak\FrontBundle\Entity\Book;
use KekRozsak\SecurityBundle\Entity\User;
/**
* KekRozsak\FrontBundle\Entity\BookCopy
* @ORM\Entity
* @ORM\Table(name="book_copies", uniqueConstraints={
* @ORM\UniqueConstraint(columns={"owner_id", "book_id"})
* })
*
* @DoctrineAssert\UniqueEntity(fields={"owner_id", "book_id"})
*/
class BookCopy
{
public function __construct(Book $book, User $owner)
{
$this->book = $book;
$this->owner = $owner;
$this->borrowable = false;
$this->buyable = false;
$this->borrower = null;
$this->borrowerReturned = true;
}
/**
* @var integer $id
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @var KekRozsak\FrontBundle\Entity\Book $book
*
* @ORM\ManyToOne(targetEntity="Book", inversedBy="copies")
* @ORM\JoinColumn(nullable=false)
*/
protected $book;
/**
* @var KekRozsak\SecurityBundle\Entity\User $owner
*
* @ORM\ManyToOne(targetEntity="KekRozsak\SecurityBundle\Entity\User")
* @ORM\JoinColumn(nullable=false)
*/
protected $owner;
/**
* Get owner
*
* @return KekRozsak\SecurityBundle\Entity\User
*/
public function getOwner()
{
return $this->owner;
}
/**
* @var string $ownerComment
*
* @ORM\Column(type="text", name="owner_comment", nullable=true)
*/
protected $ownerComment;
/**
* @var boolean $borrowable
*
* @ORM\Column(type="boolean", nullable=false)
*/
protected $borrowable;
/**
* Set borrowable
*
* @param boolean $borrowable
* @return BookCopy
*/
public function setBorrowable($borrowable)
{
$this->borrowable = $borrowable;
return $this;
}
/**
* Get borrowable
*
* @return boolean
*/
public function isBorrowable()
{
return $this->borrowable;
}
/**
* @var boolean $buyable
*
* @ORM\Column(type="boolean", nullable=false)
*/
protected $buyable;
/**
* Set buyable
*
* @param boolean $buyable
* @return BookCopy
*/
public function setBuyable($buyable)
{
$this->buyable = $buyable;
return $this;
}
/**
* Get borrowable
*
* @return boolean
*/
public function isBuyable()
{
return $this->buyable;
}
/**
* @var KekRozsak\SecurityBundle\Entity\User $borrower
*
* @ORM\OneToOne(targetEntity="KekRozsak\SecurityBundle\Entity\User")
*/
protected $borrower;
/**
* Get borrower
*
* @return KekRozsak\SecurityBundle\Entity\User
*/
public function getBorrower()
{
return $this->borrower;
}
/**
* @var boolean $borrowerReturned
*
* @ORM\Column(type="boolean", nullable=false, name="borrower_returned")
*/
protected $borrowerReturned;
/**
* Get borrowerReturned
*
* @return boolean
*/
public function isBorrowerReturned()
{
return $this->borrowerReturned();
}
}

View File

@@ -0,0 +1,63 @@
{# vim: ft=htmljinja
#}
<p>
<strong>Szerző:</strong> {{ book.author }}<br />
<strong>Cím:</strong> {{ book.title }}<br />
<strong>Kiadás éve:</strong> {{ book.year }}<br />
</p>
<p>
Nekem van <strong>{{ book.usersCopies(app.user)|length }}</strong>, ebből kölcsön van adva <strong>X</strong>.<br />
A teljes közösségnek összesen <strong>{{ book.copies|length }}</strong> példánya van.<br />
Kölcsönkérhető <strong>{{ book.copiesBorrowable|length }}</strong> példány, ebből <strong>{{ book.copiesBorrowedByUser(app.user)|length }}</strong> nálam van.<br />
</p>
<p>
{% if book.usersCopies(app.user)|length == 0 %}
<span class="gomb add-copy-button" id="add-copy-button-{{ book.id }}">[Nekem is van egy]</span>
{% else %}
<span class="gomb delete-copy-button" id="delete-copy-button-{{ book.id }}">[Nincs már]</span>
{# TODO
<span class="gomb">[Eladtam valakinek a körben]</span>
#}
{% if book.usersCopiesBorrowable(app.user)|length == 0 %}
<span class="gomb mine-is-borrowable-button" id="mine-is-borrowable-button-{{ book.id }}">[Az enyém is kölcsönkérhető]</span>
{% else %}
<span class="gomb mine-is-not-borrowable-button" id="mine-is-not-borrowable-button-{{ book.id }}">[Nem szeretném kölcsönadni]</span>
{% endif %}
{% endif %}
{% if book.usersCopies(app.user)|length > 0 %}
{% if book.usersCopiesBuyable(app.user)|length == 0 %}
<span class="gomb mine-is-for-sale-button" id="mine-is-for-sale-button-{{ book.id }}">[Az enyém eladó]</span>
{% else %}
<span class="gomb mine-is-not-for-sale-button" id="mine-is-not-for-sale-button-{{ book.id }}">[Nem szeretném eladni]</span>
{% endif %}
{% endif %}
{% if book.copiesBorrowedByUser(app.user)|length == 0 and book.usersCopies(app.user)|length == 0 and not book.userWouldBorrow(app.user) %}
<span class="gomb want-to-borrow-button" id="want-to-borrow-button-{{ book.id }}">[Kérek egyet kölcsön]</span>
{% endif %}
{% if book.usersCopies(app.user)|length == 0 and not book.userWouldBuy(app.user) %}
<span class="gomb want-to-buy-button" id="want-to-buy-button-{{ book.id }}">[Vennék egyet]</span>
{% endif %}
</p>
{% if book.wouldBuy|length > 0 %}
<p>
Ők szeretnének egyet kölcsönkérni:<br />
<ul>
{% for user in book.wouldBorrow %}
<li>{{ user|userdataspan }}</li>
{% endfor %}
</ul>
</p>
{% endif %}
{% if book.wouldBuy|length > 0 %}
<p>
Ők szeretnének venni egyet:<br />
<ul>
{% for user in book.wouldBuy %}
<li>{{ user|userdataspan }}</li>
{% endfor %}
</ul>
</p>
{% endif %}

View File

@@ -0,0 +1,133 @@
{# vim: ft=htmljinja
#}
{% extends '::main_template.html.twig' %}
{% block title %} - Könyvtár{% endblock %}
{% block content %}
<h3>Könyvtár</h3>
[Saját könyveim] [Nálam lévő kölcsönzött könyvek]
{% if books|length > 0 %}
<table>
<thead>
<tr>
<td>Szerző</td>
<td>Cím</td>
<td>Év</td>
<td>Összes</td>
<td>Kölcsönözhető</td>
<td>Saját</td>
<td>Nálam (Vissza)</td>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr class="book-row popup-opener" id="book-{{ book.id }}">
<td class="popup-opener">{{ book.author }}</td>
<td>{{ book.title }}</td>
<td>{{ book.year }}</td>
<td>{{ book.copies|length }}</td>
<td>{{ book.copiesBorrowable|length }}</td>
<td>{{ book.usersCopies(app.user)|length }}</td>
<td>{{ book.copiesBorrowedByUser(app.user)|length }} ({{ book.copiesBorrowedReturnedByUser(app.user)|length }})</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endblock %}
{% block bottomscripts %}
<script type="text/javascript">
$('.book-row').click(function() {
bookid = 0;
if (!$(this).attr('id').match(/^book-\d+$/))
return false;
bookid = $(this).attr('id').replace(/^book-/, '');
bookUrl = Routing.generate('KekRozsakFrontBundle_bookAjaxData', { id: bookid, _format: 'html' });
bookCallback = function() {
// TODO: Change alert() calls to HTML flashes
$('.delete-copy-button').click(function() {
bookid = 0;
if (!$(this).attr('id').match(/^delete-copy-button-\d+$/))
return false;
bookid = $(this).attr('id').replace(/^delete-copy-button-/, '');
url = Routing.generate('KekRozsakFrontBundle_bookDeleteCopy', { id: bookid });
$.ajax({
method: 'GET',
url: url
}).done(function() {
doPopup('', 'Betöltés...', bookUrl, 400, 300, bookCallback);
}).error(function() {
alert('Nem sikerült törölni');
});
});
$('.add-copy-button').click(function() {
bookid = 0;
if (!$(this).attr('id').match(/^add-copy-button-\d+$/))
return false;
bookid = $(this).attr('id').replace(/^add-copy-button-/, '');
url = Routing.generate('KekRozsakFrontBundle_bookAddCopy', { id: bookid });
$.ajax({
method: 'GET',
url: url
}).done(function() {
doPopup('', 'Betöltés...', bookUrl, 400, 300, bookCallback);
}).error(function() {
alert('Nem sikerült bejegyezni ezt a példányt');
});
});
$('.mine-is-borrowable-button, .mine-is-not-borrowable-button').click(function() {
bookid = 0;
if (!$(this).attr('id').match(/^mine-is-(not-)?borrowable-button-\d+$/))
return false;
isBorrowable = ($(this).attr('id').match(/^mine-is-not-borrowable-button-\d+$/)) ? 0 : 1;
bookid = $(this).attr('id').replace(/^mine-is-(not-)?borrowable-button-/, '');
url = Routing.generate('KekRozsakFrontBundle_bookSetCopyBorrowable', { id: bookid, newValue: isBorrowable });
$.ajax({
method: 'GET',
url: url
}).done(function() {
doPopup('', 'Betöltés...', bookUrl, 400, 300, bookCallback);
}).error(function() {
alert('Nem sikerült bejegyezni ezt a példányt');
});
});
$('.mine-is-for-sale-button, .mine-is-not-for-sale-button').click(function() {
bookid = 0;
if (!$(this).attr('id').match(/^mine-is-(not-)?for-sale-button-\d+$/))
return false;
isForSale = ($(this).attr('id').match(/^mine-is-not-for-sale-button-\d+$/)) ? 0 : 1;
bookid = $(this).attr('id').replace(/^mine-is-(not-)?for-sale-button-/, '');
url = Routing.generate('KekRozsakFrontBundle_bookSetCopyForSale', { id: bookid, newValue: isForSale });
$.ajax({
method: 'GET',
url: url
}).done(function() {
doPopup('', 'Betöltés...', bookUrl, 400, 300, bookCallback);
}).error(function() {
alert('Nem sikerült bejegyezni ezt a példányt');
});
});
$('.want-to-buy-button, .want-to-borrow-button').click(function() {
bookid = 0;
if (!$(this).attr('id').match(/^want-to-(buy|borrow)-button-\d+$/))
return false;
toBuy = ($(this).attr('id').match(/^want-to-buy-button-\d+$/)) ? 1 : 0;
bookid = $(this).attr('id').replace(/^want-to-(buy|borrow)-button-/, '');
url = Routing.generate('KekRozsakFrontBundle_bookWantOne', { id: bookid, wantToBuy: toBuy });
$.ajax({
method: 'GET',
url: url
}).done(function() {
doPopup('', 'Betöltés...', bookUrl, 400, 300, bookCallback);
}).error(function() {
alert('Nem sikerült bejegyezni a kérést');
});
});
};
doPopup('', 'Betöltés...', bookUrl, 400, 300, bookCallback);
});
</script>
{% endblock bottomscripts %}