/**
  * Creates an Link object based on a DB row.
  *
  * @param array $row The DB row containing Link data.
  * @return \WebLinks\Domain\Link
  */
 protected function buildDomainObject($row)
 {
     $link = new Link();
     $link->setId($row['link_id']);
     $link->setUrl($row['link_url']);
     $link->setTitle($row['link_title']);
     if (array_key_exists('user_id', $row)) {
         // Find and set the associated author
         $userId = $row['user_id'];
         $user = $this->userDAO->find($userId);
         $link->setAuthor($user);
     }
     return $link;
 }
 /**
  * Submit link controller.
  * 
  * @param Request $request Incoming request
  * @param Application $app Silex application
  */
 public function submitLinkAction(Request $request, Application $app)
 {
     $linkFormView = null;
     if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) {
         $link = new Link();
         $author = $app['user'];
         $link->setAuthor($author);
         $linkForm = $app['form.factory']->create(new LinkType(), $link);
         $linkForm->handleRequest($request);
         if ($linkForm->isSubmitted() && $linkForm->isValid()) {
             $app['dao.link']->save($link);
             $app['session']->getFlashBag()->add('success', 'The link was successfully added.');
         }
         $linkFormView = $linkForm->createView();
     }
     return $app['twig']->render('link_form.html.twig', array('title' => 'New link', 'linkForm' => $linkFormView));
 }
Example #3
0
 /**
  * Saves a link into the database.
  * 
  * $param \WebLinks\Domain\Link $link The link to save
  */
 public function save(Link $link)
 {
     $linkData = array('link_title' => $link->getTitle(), 'link_url' => $link->getUrl(), 'user_id' => $link->getAuthor()->getId());
     if ($link->getId()) {
         // The link has already been saved : update it
         $this->getDb()->update('t_link', $linkData, array('link_id' => $link->getId()));
     } else {
         // The link has never been saved : insert it
         $this->getDb()->insert('t_link', $linkData);
         // Get the id of the newly created link and set it on the entity
         $id = $this->getDb()->lastInsertId();
         $link->setId($id);
     }
 }
Example #4
0
 /**
  * Converts an Link object into an associative array for JSON encoding
  * 
  * @param Link $link Link object
  * 
  * @return array Associative array whose fields are the link properties.
  */
 private function buildLinkArray(Link $link)
 {
     $data = array('id' => $link->getId(), 'title' => $link->getTitle(), 'url' => $link->getUrl(), 'author' => $link->getAuthor()->getUsername());
     return $data;
 }
use Symfony\Component\HttpFoundation\Request;
use WebLinks\Domain\Link;
use WebLinks\Form\Type\LinkType;
// Home page
$app->get('/', function () use($app) {
    $links = $app['dao.link']->findAll();
    return $app['twig']->render('index.html.twig', array('links' => $links));
});
// Submit link page
$app->match('/link/submit', function (Request $request) use($app) {
    $valid = false;
    $user = $app['security']->getToken()->getUser();
    $linkFormView = null;
    if ($app['security']->isGranted('IS_AUTHENTICATED_FULLY')) {
        // A user is fully authenticated : he can add links
        $link = new Link();
        $link->setAuthor($user);
        $linkForm = $app['form.factory']->create(new LinkType(), $link);
        $linkForm->handleRequest($request);
        if ($linkForm->isSubmitted() && $linkForm->isValid()) {
            $app['dao.link']->save($link);
            $app['session']->getFlashBag()->add('success', 'Your link was succesfully added.');
        }
        $linkFormView = $linkForm->createView();
    }
    return $app['twig']->render('link_submit.html.twig', array('linkForm' => $linkFormView, 'valid' => $valid));
});
// Login form
$app->get('/login', function (Request $request) use($app) {
    return $app['twig']->render('login.html.twig', array('error' => $app['security.last_error']($request), 'last_username' => $app['session']->get('_security.last_username')));
})->bind('login');