Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
- added Nette::Application
Browse files Browse the repository at this point in the history
- added examples
  • Loading branch information
dg committed Oct 10, 2010
1 parent 8fbcad5 commit 0671f0c
Show file tree
Hide file tree
Showing 69 changed files with 2,514 additions and 0 deletions.
1 change: 1 addition & 0 deletions Akrabat/app/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deny from all
42 changes: 42 additions & 0 deletions Akrabat/app/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php


/**
* Load Nette
*/
require_once dirname(__FILE__) . '/../../../Nette/loader.php';


/**
* Configure application
*/
Environment::loadConfig();


/**
* Prepare & setup
*/
Debug::enable(E_ALL | E_STRICT);

require_once 'dibi.compact.php';
dibi::connect(Environment::getConfig('database'));


$application = Environment::getApplication();
$router = $application->getRouter();
$router[] = new Route('index.php', array(
'presenter' => 'default',
), Route::ONE_WAY);

$router[] = new Route('<presenter>/<view>/<id>', array(
'presenter' => 'default',
'view' => 'default',
'id' => NULL,
));



/**
* Run!
*/
$application->run();
15 changes: 15 additions & 0 deletions Akrabat/app/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[common]
set> date.timezone = "Europe/Prague"
set> iconv.internal_encoding = "%encoding%"
set> mbstring.internal_encoding = "%encoding%"
set> include_path = "%appDir%/../libs;%appDir%;"

[production < common]
database> driver = sqlite
database> file = "%modelsDir%/demo.db"
database> lazy = TRUE
database> result:objects = TRUE

service> Nette::Security::IAuthenticator = Users

[development < production]
19 changes: 19 additions & 0 deletions Akrabat/app/models/Albums.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php



class Albums extends DibiTable
{

protected $blankRow = array(
'artist' => '',
'title' => '',
);

/* CREATE TABLE [albums] (
[id] INTEGER NOT NULL PRIMARY KEY,
[artist] VARCHAR(100) NOT NULL,
[title] VARCHAR(100) NOT NULL
) */

}
37 changes: 37 additions & 0 deletions Akrabat/app/models/Users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php



class Users extends DibiTable implements /*Nette::Security::*/IAuthenticator
{

/* CREATE TABLE [users] (
[id] INTEGER NULL PRIMARY KEY,
[username] VARCHAR(50) UNIQUE NOT NULL,
[password] VARCHAR(50) NOT NULL,
[real_name] VARCHAR(100) NOT NULL
) */


/**
* Performs an authentication
* @param array
* @return void
* @throws AuthenticationException
*/
public function authenticate(array $credentials)
{
$row = $this->fetch(array('username' => $credentials['username']));
if (!$row) {
throw new AuthenticationException('', AuthenticationException::IDENTITY_NOT_FOUND);
}

if ($row->password !== $credentials['password']) {
throw new AuthenticationException('', AuthenticationException::INVALID_CREDENTIAL);
}

unset($row->password);
return new Identity($row->username, array(), $row);
}

}
Binary file added Akrabat/app/models/demo.db
Binary file not shown.
67 changes: 67 additions & 0 deletions Akrabat/app/presenters/AuthPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

require_once dirname(__FILE__) . '/BasePresenter.php';


class AuthPresenter extends BasePresenter
{
/** @persistent */
public $backlink = '';


protected function startup()
{
require_once 'models/Users.php';

parent::startup();
}



/********************* view Default *********************/



public function prepareDefault()
{
$this->forward('login');
}



/********************* view Login *********************/



public function renderLogin($backlink)
{
$this->backlink = $backlink;
$this->template->title = "Log in";
}



public function handleLogin()
{
$request = $this->request;
if (!$request->isPost()) return;

// collect the data from the user
$username = trim($request->post['username']);
$password = trim($request->post['password']);

if (empty($username)) {
$this->template->message = 'Please provide a username.';
} else {
try {
$user = Environment::getUser();
$user->authenticate($username, $password);
$this->redirect($this->backlink);

} catch (AuthenticationException $e) {
$this->template->message = 'Login failed.';
}
}
}

}
12 changes: 12 additions & 0 deletions Akrabat/app/presenters/BasePresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


abstract class BasePresenter extends Presenter
{

protected function startup()
{
$this->template->registerFilter('TemplateFilters::curlyBrackets');
}

}
174 changes: 174 additions & 0 deletions Akrabat/app/presenters/DefaultPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

require_once dirname(__FILE__) . '/BasePresenter.php';


class DefaultPresenter extends BasePresenter
{

protected function startup()
{
require_once 'models/Albums.php';

// user authentication
$user = Environment::getUser();
if (!$user->isAuthenticated()) {
$this->redirect('auth:login', $this->backlink());
}

$this->template->user = $user->getIdentity();
parent::startup();
}



/********************* view Default *********************/



public function renderDefault()
{
$this->template->title = "My Albums";
$album = new Albums();
$this->template->albums = $album->findAll('artist', 'title');
}



/********************* view Add *********************/



public function renderAdd()
{
$this->template->title = "Add New Album";

// set up an "empty" album
$album = new Albums();
$this->template->album = $album->createBlank();

// additional view fields required by form
$this->template->action = $this->link('add!');
$this->template->buttonText = 'Add';
}



public function handleAdd()
{
$request = $this->request;
if (!$request->isPost()) return;

$artist = trim($request->post['artist']);
$title = trim($request->post['title']);

if ($artist != '' && $title != '') {
$data = array(
'artist' => $artist,
'title' => $title,
);
$album = new Albums();
$album->insert($data);

$this->redirect('default');
}
}



/********************* view Edit *********************/



public function renderEdit($id = 0)
{
$this->template->title = "Edit Album";
$album = new Albums();
if ($id > 0) {
$this->template->album = $album->fetch($id);
} else {
$this->template->album = $album->createBlank();
}

// additional view fields required by form
$this->template->action = $this->link('save!', $id);
$this->template->buttonText = 'Update';
}



public function handleSave($id = 0)
{
$request = $this->request;
if (!$request->isPost()) return;

$artist = trim($request->post['artist']);
$title = trim($request->post['title']);

if ($id !== 0) {
if ($artist != '' && $title != '') {
$data = array(
'artist' => $artist,
'title' => $title,
);
$album = new Albums();
$album->update($id, $data);

$this->redirect('default');

} else {
//$this->template->album = $album->fetch($id);
}
}
}



/********************* view Delete *********************/



public function renderDelete($id = 0)
{
$this->template->title = "Delete Album";

if ($id > 0) {
// only render if we have an id and can find the album.
$album = new Albums();
$this->template->album = $album->fetch($id);
if (!$this->template->album) {
$this->redirect('default');
}
}

$this->template->action = $this->link('delete!', $id);
}



public function handleDelete($id = 0)
{
$request = $this->request;
if (!$request->isPost()) return;

$del = $request->post['del'];
if ($del == 'Yes' && $id > 0) {
$album = new Albums();
$album->delete($id);
}
$this->redirect('default');
}



/********************* common commands *********************/



function handleLogout()
{
Environment::getUser()->signOut();
$this->redirect('default');
}

}
21 changes: 21 additions & 0 deletions Akrabat/app/templates/auth/login.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1>{$title}</h1>

{if !empty($message)}
<div id="message">
{$message}
</div>
{/if}

<form action="{$component->link('login!')}" method="post">
<div>
<label for="username">Username</label>
<input type="text" name="username" value=""/>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" value=""/>
</div>
<div id="formbutton">
<input type="submit" name="login" value="Login" />
</div>
</form>
Loading

0 comments on commit 0671f0c

Please sign in to comment.