Adding pages feature (index.php and htaccess) see #5848
parent
c71810f3d3
commit
dd5f3fef8f
@ -0,0 +1,8 @@ |
||||
<IfModule mod_rewrite.c> |
||||
Options -MultiViews |
||||
|
||||
RewriteEngine On |
||||
#RewriteBase /path/to/app |
||||
RewriteCond %{REQUEST_FILENAME} !-f |
||||
RewriteRule ^ index.php [L] |
||||
</IfModule> |
@ -0,0 +1,160 @@ |
||||
<?php |
||||
|
||||
require_once '../inc/global.inc.php'; |
||||
|
||||
use Silex\Application; |
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert; |
||||
use Doctrine\ORM\Tools\Pagination\Paginator; |
||||
|
||||
use Pagerfanta\Pagerfanta; |
||||
use Pagerfanta\Adapter\DoctrineORMAdapter; |
||||
use Pagerfanta\View\TwitterBootstrapView; |
||||
//use Pagerfanta\View\DefaultView; |
||||
//use Pages\PagesAdmin; |
||||
|
||||
class PagesController { |
||||
/* |
||||
function indexAction(Application $app, $page) { |
||||
return $this->listAction($app, $page); |
||||
}*/ |
||||
|
||||
function addAction(Application $app) { |
||||
$request = $app['request']; |
||||
$form = $this->getForm($app); |
||||
|
||||
if ('POST' == $request->getMethod()) { |
||||
$form->bindRequest($request); |
||||
if ($form->isValid()) { |
||||
$page = $form->getData(); |
||||
$em = $app['orm.em']; |
||||
/*$page_data = $form->getData(); |
||||
$page->setContent($page_data['content']); |
||||
$page->setSlug($page_data['slug']); |
||||
$page->setTitle($page_data['title']); |
||||
$em->persist($page);*/ |
||||
$em->persist($page); |
||||
$em->flush(); |
||||
return $app->redirect($app['url_generator']->generate('show', array('id'=> $page->getId())), 201); |
||||
} |
||||
} |
||||
return $app['template']->render_template('pages/add.tpl', array('form' => $form->createView())); |
||||
} |
||||
|
||||
function editAction(Application $app, $id) { |
||||
$request = $app['request']; |
||||
$page = $app['orm.em']->find('Entity\EntityPages', $id); |
||||
|
||||
if (empty($page)) { |
||||
$app->abort(404, "Page $id does not exist."); |
||||
} |
||||
|
||||
$form = $this->getForm($app, $page); |
||||
|
||||
if ('POST' == $request->getMethod()) { |
||||
$form->bind($request); |
||||
if ($form->isValid()) { |
||||
$em = $app['orm.em']; |
||||
//$page = $form->getData(); |
||||
$em->persist($page); |
||||
$em->flush(); |
||||
return $app->redirect($app['url_generator']->generate('show', array('id'=> $page->getId())), 201); |
||||
} |
||||
} |
||||
return $app['template']->render_template('pages/add.tpl', array('form' => $form->createView())); |
||||
} |
||||
|
||||
function showAction(Application $app, $id) { |
||||
$page = $app['orm.em']->find('Entity\EntityPages', $id); |
||||
$actions = Display::url(Display::return_icon('list.png', get_lang('Listing'), array(), ICON_SIZE_MEDIUM), $app['url_generator']->generate('index')); |
||||
return $app['template']->render_template('pages/show.tpl', array( |
||||
'page' => $page, |
||||
'actions' => $actions, |
||||
|
||||
)); |
||||
} |
||||
|
||||
function deleteAction(Application $app, $id) { |
||||
$em = $app['orm.em']; |
||||
$page = $em->find('Entity\EntityPages', $id); |
||||
$em->remove($page); |
||||
$em->flush(); |
||||
return $app->redirect($app['url_generator']->generate('index'), 201); |
||||
} |
||||
|
||||
function listAction(Application $app, $page = 1) { |
||||
/*$app['sonata.crud_controller']->listAction(); |
||||
require_once 'pageadmin.php'; |
||||
$pages_admin = new PagesAdmin('a', 'b', 'Controller'); |
||||
$html = $pages_admin->getDatagrid(); |
||||
var_dump($html);*/ |
||||
|
||||
$em = $app['orm.em']; |
||||
$dql = 'SELECT a FROM Entity\EntityPages a'; |
||||
$query = $em->createQuery($dql)->setFirstResult(0) |
||||
->setMaxResults(100); |
||||
|
||||
//or using the repository |
||||
// |
||||
//$query = $em->getRepository('Entity\EntityPages')->getLatestPages(); |
||||
|
||||
$adapter = new DoctrineORMAdapter($query); |
||||
$pagerfanta = new Pagerfanta($adapter); |
||||
|
||||
$routeGenerator = function($page) use ($app) { |
||||
return $app['url_generator']->generate('list', array('page' => $page)); |
||||
}; |
||||
|
||||
$pagerfanta->setMaxPerPage(2); // 10 by default |
||||
$pagerfanta->setCurrentPage($page); |
||||
|
||||
//$view = new DefaultView(); |
||||
$view = new TwitterBootstrapView(); |
||||
|
||||
$pagination = $view->render($pagerfanta, $routeGenerator, array( |
||||
'proximity' => 3, |
||||
)); |
||||
|
||||
$actions = Display::url(Display::return_icon('add.png', get_lang('Add'), array(), ICON_SIZE_MEDIUM), $app['url_generator']->generate('add')); |
||||
//$paginator = new Paginator($query, $fetchJoinCollection = true); |
||||
|
||||
return $app['template']->render_template('pages/listing.tpl', array( |
||||
//'pages' => $paginator->getIterator(), |
||||
'pages' => $pagerfanta, |
||||
'pagination' => $pagination, |
||||
'actions' => $actions |
||||
)); |
||||
} |
||||
|
||||
function getForm(Application $app, $entity = null) { |
||||
if (empty($entity)) { |
||||
$entity = new Entity\EntityPages(); |
||||
} |
||||
$form = $app['form.factory']->createBuilder('form', $entity); |
||||
$form->add('title', 'text', array( |
||||
'constraints' => array(new Assert\NotBlank(), new Assert\MinLength(5)) |
||||
)); |
||||
$form->add('slug', 'text', array( |
||||
//'constraints' => array(new Assert\NotBlank()) |
||||
)); |
||||
$form->add('content', 'textarea', array( |
||||
// 'constraints' => array() |
||||
)); |
||||
return $form->getForm(); |
||||
} |
||||
} |
||||
|
||||
//$app->match('/{page}', 'PagesController::indexAction')->bind('index'); |
||||
$app->get('/', 'PagesController::listAction')->bind('index'); |
||||
$app->get('/page', 'PagesController::listAction')->bind('list'); |
||||
$app->get('/show/{id}', 'PagesController::showAction') |
||||
->bind('show') |
||||
->assert('id', '\d+'); |
||||
$app->get('/delete/{id}', 'PagesController::deleteAction') |
||||
->bind('delete') |
||||
->assert('id', '\d+'); |
||||
$app->match('/edit/{id}', 'PagesController::editAction', 'GET|POST') |
||||
->bind('edit') |
||||
->assert('id', '\d+'); |
||||
$app->match('/add', 'PagesController::addAction', 'GET|POST')->bind('add'); |
||||
$app->run(); |
@ -1,38 +0,0 @@ |
||||
<?php |
||||
|
||||
namespace DoctrineMigrations; |
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration, |
||||
Doctrine\DBAL\Schema\Schema; |
||||
|
||||
/** |
||||
* Auto-generated Migration: Please modify to your need! |
||||
*/ |
||||
class Version20121220233847 extends AbstractMigration |
||||
{ |
||||
public function up(Schema $schema) |
||||
{ |
||||
/*$this->addSql('CREATE TABLE pages ( |
||||
id INTEGER NOT NULL AUTO_INCREMENT, |
||||
title VARCHAR(255) NOT NULL, |
||||
slug VARCHAR(255) NOT NULL, |
||||
content TEXT NOT NULL, |
||||
PRIMARY KEY (id) |
||||
)');*/ |
||||
|
||||
$table = $schema->createTable('pages'); |
||||
$table->addColumn('id', 'string'); |
||||
$table->addColumn('title', 'string'); |
||||
$table->addColumn('slug', 'string'); |
||||
$table->addColumn('content', 'text'); |
||||
$table->addColumn('created', 'datetime'); |
||||
$table->addColumn('updated', 'datetime'); |
||||
$table->setPrimaryKey('id'); |
||||
} |
||||
|
||||
public function down(Schema $schema) |
||||
{ |
||||
//$this->addSql('DROP TABLE pages'); |
||||
$schema->dropTable('pages'); |
||||
} |
||||
} |
Loading…
Reference in new issue