Blog start and some model stuff
This commit is contained in:
parent
126ef0ccbd
commit
9779dbf5ba
16 changed files with 294 additions and 126 deletions
|
|
@ -12,7 +12,7 @@ app.url = "fiktivkod.org"
|
|||
defaults.lang = sv
|
||||
|
||||
// Database
|
||||
db.adapter = mongo
|
||||
db.adapter = mysqli
|
||||
db.host = 127.0.0.1
|
||||
db.username = fiktivkod
|
||||
db.password = pw
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
<?php
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
?>
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class Table_User extends Zend_Db_Table_Abstract implements Fiktiv_Data_Storage
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'User';
|
||||
protected $_primary = 'id';
|
||||
protected $_rowClass = 'User';
|
||||
|
||||
/**
|
||||
* Authenticate user
|
||||
* TODO: Evaluate, should this be here? Datapoint = data access only?
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
*/
|
||||
public function login($email, $password)
|
||||
{
|
||||
$auth = Zend_Auth::getInstance();
|
||||
|
||||
// Setup auth adapter
|
||||
$authAdapter = new Zend_Auth_Adapter_DbTable($this->getAdapter(), $this->_name, 'email', 'password');
|
||||
|
||||
// Set credentials
|
||||
$authAdapter->setIdentity($email);
|
||||
$authAdapter->setCredential(hash('sha256',$password));
|
||||
|
||||
// Authenticate
|
||||
$result = $auth->authenticate($authAdapter);
|
||||
|
||||
// Check result
|
||||
if ($result->isValid()) {
|
||||
// Keep all but password and salt in session.
|
||||
$storage = $auth->getStorage();
|
||||
$storage->write($authAdapter->getResultRowObject(null, array('password', 'salt')));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch user based on email
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function findByEmail($email)
|
||||
{
|
||||
// Atleast 6 character long
|
||||
if (is_string($email) && isset($email[5])) {
|
||||
|
||||
return $this->fetchRow($this->getAdapter()->quoteInto('email = ?', $email));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get random user
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function findRandom()
|
||||
{
|
||||
return $this->fetchAll(null, 'RAND()', 1)->current();
|
||||
}
|
||||
}
|
||||
86
application/models/BlogPost.php
Normal file
86
application/models/BlogPost.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
class BlogPost
|
||||
{
|
||||
|
||||
protected $_title;
|
||||
protected $_content;
|
||||
protected $_author;
|
||||
protected $_pubDate;
|
||||
protected $_isPublished = false;
|
||||
|
||||
|
||||
public function setTitle($title)
|
||||
{
|
||||
if (is_string($title) && null == $title[30]) {
|
||||
|
||||
$this->_title = $title;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function setContent($content)
|
||||
{
|
||||
if (is_string($content)) {
|
||||
|
||||
$this->_content = $content;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function setAuthor($author)
|
||||
{
|
||||
if ($author instanceof User) {
|
||||
|
||||
$this->_author = $author;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function setPubDate($date)
|
||||
{
|
||||
if (is_string($date) && preg_match('/^(?:(?:19[0-9]{2})|(?:20[0-9]{2}))\-' .
|
||||
'(?:(?:0[1-9])|(?:1[012]))\-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01]))$/', $date)) {
|
||||
|
||||
$this->_pubDate = $date;
|
||||
} else if ($date instanceof Zend_Date) {
|
||||
|
||||
$this->_pubDate = $date->toString('yyyy-MM-dd');
|
||||
} else {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public function isPublished($flag = null)
|
||||
{
|
||||
if (null === $flag) {
|
||||
return $this->_isPublished;
|
||||
}
|
||||
|
||||
$this->_isPublished = (boolean)$flag;
|
||||
}
|
||||
|
||||
public function __call($name,$args) {
|
||||
|
||||
// getX methods
|
||||
if ('get' == substr($name,0,3)) {
|
||||
$dataPart = '_'.lcfirst(substr($name,3));
|
||||
if (isset($this->$dataPart))
|
||||
return $this->$dataPart;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
47
application/models/Mapper/BlogPost.php
Normal file
47
application/models/Mapper/BlogPost.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
class Mapper_BlogPost extends Fiktiv_Data_Mapper_DbTable_Abstract
|
||||
{
|
||||
|
||||
protected function _createBlogPost($obj)
|
||||
{
|
||||
$blogPost = new BlogPost();
|
||||
|
||||
if ($obj instanceof Zend_Db_Table_Row) {
|
||||
|
||||
$blogPost->setTitle($obj->title);
|
||||
$blogPost->setContent($obj->content);
|
||||
$blogPost->setPubDate($obj->pubDate);
|
||||
|
||||
$mapperUser = new Mapper_User(new Table_User());
|
||||
$user = $mapperUser->findById($obj->userId);
|
||||
$blogPost->setAuthor($user);
|
||||
|
||||
} else {
|
||||
$blogPost = null;
|
||||
}
|
||||
|
||||
return $blogPost;
|
||||
}
|
||||
|
||||
public function findAllPostByAuthor($user)
|
||||
{
|
||||
if ($user instanceof User) {
|
||||
return $this->_createBlogPost($this->_dbTable->findByUser($user->getId()));
|
||||
}
|
||||
}
|
||||
|
||||
public function findAll($limit = null)
|
||||
{
|
||||
$rows = $this->_dbTable->fetchAll();
|
||||
|
||||
$posts = array();
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$posts[] = $this->_createBlogPost($row);
|
||||
}
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,35 +1,42 @@
|
|||
<?php
|
||||
|
||||
class Mapper_User
|
||||
class Mapper_User extends Fiktiv_Data_Mapper_DbTable_Abstract
|
||||
{
|
||||
|
||||
protected $_dbTable = null;
|
||||
|
||||
|
||||
public function __construct($dbtable = null)
|
||||
protected function _createUser($object)
|
||||
{
|
||||
$this->setDbTable($dbtable);
|
||||
|
||||
$user = new User();
|
||||
|
||||
if ($object instanceof Zend_Db_Table_Row) {
|
||||
|
||||
$user->setEmail($object->email);
|
||||
$user->setFirstName($object->firstName);
|
||||
$user->setLastName($object->lastName);
|
||||
|
||||
} else if (is_array($object)) {
|
||||
|
||||
$user->setEmail($object['email']);
|
||||
$user->setFirstName($object['firstName']);
|
||||
$user->setLastName($object['lastName']);
|
||||
|
||||
} else {
|
||||
$user = null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
|
||||
public function setDbTable($dbtable)
|
||||
|
||||
public function findById($id)
|
||||
{
|
||||
if (null === $dbtable) {
|
||||
|
||||
$this->_dbTable = new Table_User();
|
||||
} else {
|
||||
|
||||
if ($dbtable instanceof Zend_Db_Table) {
|
||||
$this->_dbTable = $dbtable;
|
||||
} else if (is_string($dbtable) && class_exists($dbtable)) {
|
||||
$this->_dbTable = new $dbtable();
|
||||
} else {
|
||||
throw new Fiktiv_Exception('Invalid database table supplied to ' . __CLASS__);
|
||||
}
|
||||
if (is_numeric($id)) {
|
||||
return $this->_createUser($this->_dbTable->find($id)->current());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetch user based on email
|
||||
*
|
||||
|
|
@ -40,7 +47,9 @@ class Mapper_User
|
|||
// Atleast 6 character long
|
||||
if (is_string($email) && isset($email[5])) {
|
||||
|
||||
return $this->_dbTable->fetchRow($this->getAdapter()->quoteInto('email = ?', $email));
|
||||
$user = $this->_createUser($this->_dbTable->fetchRow($this->_dbTable->getAdapter()->quoteInto('email = ?', $email)));
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
@ -53,17 +62,7 @@ class Mapper_User
|
|||
*/
|
||||
public function findRandom()
|
||||
{
|
||||
$row = $this->_dbTable->fetchAll(null, 'RAND()', 1)->current();
|
||||
|
||||
if ($row) {
|
||||
$user = new User();
|
||||
$user->setFirstName($row->firstName);
|
||||
$user->setLastName($row->lastName);
|
||||
$user->setRegDate($row->regDate);
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
return $this->_createUser($this->_dbTable->fetchAll(null, 'RAND()', 1)->current());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
9
application/models/Table/BlogPost.php
Normal file
9
application/models/Table/BlogPost.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
class Table_BlogPost extends Zend_Db_Table_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'Post';
|
||||
protected $_primary = 'id';
|
||||
|
||||
}
|
||||
|
|
@ -5,4 +5,5 @@ class Table_User extends Zend_Db_Table_Abstract
|
|||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'User';
|
||||
protected $_primary = 'id';
|
||||
|
||||
}
|
||||
|
|
@ -5,13 +5,32 @@
|
|||
*/
|
||||
class User
|
||||
{
|
||||
protected $_id = 0;
|
||||
protected $_email;
|
||||
protected $_firstName;
|
||||
protected $_lastName;
|
||||
protected $_regDate;
|
||||
protected $_isDeleted = false;
|
||||
|
||||
|
||||
/**
|
||||
* Set user email
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function setEmail($email)
|
||||
{
|
||||
$validator = new Zend_Validate_EmailAddress();
|
||||
|
||||
if ($validator->isValid($email)) {
|
||||
$this->_email = $email;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set user lastname
|
||||
*
|
||||
|
|
@ -37,6 +56,8 @@ class User
|
|||
*/
|
||||
public function setFirstName($name)
|
||||
{
|
||||
$name = trim($name);
|
||||
|
||||
if (is_string($name) && preg_match('/^[A-ö]{2,8}\-?[A-ö]{2,8}$/', $name)) {
|
||||
$this->_firstName = $name;
|
||||
return true;
|
||||
|
|
@ -44,8 +65,14 @@ class User
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set the user registration date
|
||||
*
|
||||
* @param Zend_Date | string $date
|
||||
* @return boolean
|
||||
*/
|
||||
public function setRegDate($date)
|
||||
{
|
||||
if (is_string($date) && preg_match('/^(?:(?:19[0-9]{2})|(?:20[0-9]{2}))\-' .
|
||||
|
|
@ -76,14 +103,25 @@ class User
|
|||
|
||||
|
||||
/**
|
||||
* Controll wheter the user is currently active on the
|
||||
* site.
|
||||
* TODO: Make sure we have a way to see if the user is logged in.
|
||||
* Returns the current status of the account.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isActive()
|
||||
{
|
||||
return false;
|
||||
return !$this->_isDeleted;
|
||||
}
|
||||
|
||||
|
||||
public function __call($name,$args) {
|
||||
|
||||
// getX methods
|
||||
if ('get' == substr($name,0,3)) {
|
||||
$dataPart = '_'.lcfirst(substr($name,3));
|
||||
if (isset($this->$dataPart))
|
||||
return $this->$dataPart;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -93,6 +131,6 @@ class User
|
|||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return '{' . $this->_firstName . ' ' . $this->_lastName . '}';
|
||||
return '(' . __CLASS__ . '){' . $this->_email . ' ' . $this->_firstName . ' ' . $this->_lastName . '}';
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,9 @@ class Blog_IndexController extends Fiktiv_Controller_Action
|
|||
public function indexAction()
|
||||
{
|
||||
// TODO: Set latest as default
|
||||
|
||||
$this->view->posts = $this->dbService->BlogPost->findAll();
|
||||
|
||||
}
|
||||
|
||||
public function latestAction()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,14 @@
|
|||
<h1><?=$this->translate('u:blog') ?></h1>
|
||||
|
||||
<p><?=$this->translate('BLOG_TXT') ?></p>
|
||||
<p><?=$this->translate('BLOG_TXT') ?></p>
|
||||
|
||||
|
||||
<?php foreach ($this->posts as $post): ?>
|
||||
|
||||
<div>
|
||||
<h3><?=$post->getTitle() ?></h3>
|
||||
<div><?=$post->getContent() ?></div>
|
||||
<div><?=$post->getPubDate().' '.$post->getAuthor()->getFirstName() ?></div>
|
||||
</div>
|
||||
|
||||
<?php endforeach; ?>
|
||||
|
|
@ -9,7 +9,7 @@ class IndexController extends Fiktiv_Controller_Action
|
|||
//echo $m->name;
|
||||
|
||||
// Quick access
|
||||
$me = $this->dbService->Users->findRandom();
|
||||
$me = $this->dbService->User->findRandom();
|
||||
echo $me;
|
||||
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ class IndexController extends Fiktiv_Controller_Action
|
|||
//$me = $dbLayer->users->findByEmail('fredric@fiktivkod.org');
|
||||
|
||||
$dbLayer = $this->_helper->dbService();
|
||||
$me = $dbLayer->Users->findByEmail('fredric@fiktivkod.org');
|
||||
$me = $dbLayer->User->findByEmail('fredric@fiktivkod.org');
|
||||
echo $me;
|
||||
|
||||
}
|
||||
|
|
|
|||
24
library/Fiktiv/Data/Mapper/DbTable/Abstract.php
Normal file
24
library/Fiktiv/Data/Mapper/DbTable/Abstract.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
abstract class Fiktiv_Data_Mapper_DbTable_Abstract
|
||||
{
|
||||
protected $_dbTable = null;
|
||||
|
||||
|
||||
public function __construct($dbtable = null)
|
||||
{
|
||||
$this->setDbTable($dbtable);
|
||||
}
|
||||
|
||||
|
||||
public function setDbTable($dbtable)
|
||||
{
|
||||
if ($dbtable instanceof Zend_Db_Table_Abstract) {
|
||||
$this->_dbTable = $dbtable;
|
||||
} else if (is_string($dbtable) && class_exists($dbtable)) {
|
||||
$this->_dbTable = new $dbtable();
|
||||
} else {
|
||||
throw new Fiktiv_Exception('Invalid database table supplied to ' . __CLASS__);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -34,14 +34,17 @@ class Fiktiv_Data_Service
|
|||
}
|
||||
|
||||
/**
|
||||
* Provides access to the underlaying datapoint objects.
|
||||
* TODO: Better / faster loading of datapoints?
|
||||
* Provides access to data mappers
|
||||
*
|
||||
* @param $name
|
||||
* @return Fiktiv_Data_Point
|
||||
*/
|
||||
public function __get($name) {
|
||||
|
||||
// Class prefix
|
||||
$table = 'Table_'.$name;
|
||||
$name = 'Mapper_'.$name;
|
||||
|
||||
/*
|
||||
* Two ways to go, if we already have the datapoint object we
|
||||
* will choose a faster route
|
||||
|
|
@ -50,13 +53,13 @@ class Fiktiv_Data_Service
|
|||
|
||||
// If we for some very odd reason have lost the object, re-create it!
|
||||
if (!is_object($this->_dataStorage[$name])) {
|
||||
$this->_dataStorage[$name] = new $name();
|
||||
$this->_dataStorage[$name] = new $name($table);
|
||||
}
|
||||
|
||||
|
||||
} else if (class_exists($name)) {
|
||||
|
||||
$this->_dataStorage[$name] = new $name();
|
||||
$this->_dataStorage[$name] = new $name($table);
|
||||
}
|
||||
|
||||
return $this->_dataStorage[$name];
|
||||
|
|
|
|||
24
library/Fiktiv/Performance.php
Normal file
24
library/Fiktiv/Performance.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
class Fiktiv_Performance
|
||||
{
|
||||
|
||||
protected static $_start = null;
|
||||
protected static $_end = null;
|
||||
|
||||
public static function begin()
|
||||
{
|
||||
self::$_start = microtime(true);
|
||||
}
|
||||
|
||||
public static function end()
|
||||
{
|
||||
self::$_end = microtime(true);
|
||||
return self::$_end - self::$_start;
|
||||
}
|
||||
|
||||
public static function results()
|
||||
{
|
||||
return self::$_end - self::$_start;
|
||||
}
|
||||
}
|
||||
|
|
@ -113,7 +113,7 @@ div#wrapper.small {
|
|||
}
|
||||
|
||||
#footer {
|
||||
border-top: 1px solid #bcbcbc;
|
||||
border-top: 1px solid #bcbcbc;
|
||||
height: 20px;
|
||||
text-align: center;
|
||||
padding: 8px 15px 0;
|
||||
|
|
|
|||
Reference in a new issue