A rework of the model structure
This commit is contained in:
parent
942df7d10d
commit
530851a10f
24 changed files with 253 additions and 822 deletions
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
|
||||
require_once APPLICATION_PATH . '/Acl.php';
|
||||
|
||||
/**
|
||||
|
|
@ -22,7 +21,7 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
|
||||
$navConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'navigation');
|
||||
$navigation = new Zend_Navigation($navConfig);
|
||||
|
||||
|
||||
$view = $this->getResource('view');
|
||||
$view->navigation()->setTranslator($this->getResource('translate'));
|
||||
$view->navigation($navigation);
|
||||
|
|
@ -163,7 +162,7 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
|
||||
|
||||
// Route for admin?
|
||||
$router->addRoute('admin', new Zend_Controller_Router_Route(':lang/admin/:controller/:action',array(
|
||||
$router->addRoute('admin', new Zend_Controller_Router_Route(':lang/admin/:controller/:action/*',array(
|
||||
'module' => 'admin',
|
||||
'controller' => 'index',
|
||||
'action' => 'index'
|
||||
|
|
@ -192,6 +191,7 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
// Include global model directory
|
||||
set_include_path(implode(PATH_SEPARATOR, array(
|
||||
APPLICATION_PATH . '/models',
|
||||
APPLICATION_PATH . '/models/rows',
|
||||
get_include_path()
|
||||
)));
|
||||
|
||||
|
|
@ -229,7 +229,7 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
'locale' => 'sv',
|
||||
'content' => APPLICATION_PATH . '/languages/se.ini'
|
||||
));
|
||||
|
||||
|
||||
$translate->addTranslation(APPLICATION_PATH . '/languages/en.ini', 'en');
|
||||
|
||||
Zend_Form::setDefaultTranslator($translate);
|
||||
|
|
@ -239,20 +239,6 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
return $translate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup datastorage
|
||||
*/
|
||||
protected function _initDatastorage()
|
||||
{
|
||||
$this->bootstrap('autoloader');
|
||||
|
||||
// Include datapoints directory
|
||||
set_include_path(implode(PATH_SEPARATOR, array(
|
||||
APPLICATION_PATH . '/datastorage',
|
||||
get_include_path()
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup a sweet and simple database
|
||||
* connection.
|
||||
|
|
@ -270,15 +256,6 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
// Use the adapter by default
|
||||
Zend_Db_Table::setDefaultAdapter($database);
|
||||
|
||||
/*
|
||||
* Fire up the data service
|
||||
* This is not needed as the data service will be
|
||||
* started when it is first accessed. Also, the
|
||||
* data service will automaticly load datapoints
|
||||
* on demand.
|
||||
*/
|
||||
$dbLayer = Fiktiv_Data_Service::getInstance();
|
||||
|
||||
return $database;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
<?php
|
||||
|
||||
class About extends Fiktiv_Model_Abstract
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -1,175 +0,0 @@
|
|||
<?php
|
||||
|
||||
class BlogPost extends Fiktiv_Model_Abstract
|
||||
{
|
||||
protected $_data = array();
|
||||
|
||||
protected $_default = array(
|
||||
'id' => 0,
|
||||
'title' => null,
|
||||
'content' => null,
|
||||
'pubDate' => null,
|
||||
'author' => null,
|
||||
'permalink' => null,
|
||||
'isPublished' => false,
|
||||
'tags' => array(),
|
||||
);
|
||||
|
||||
public function setAttribs(array $data) {
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
|
||||
switch(strtolower($key)) {
|
||||
case 'title':
|
||||
$this->setTitle($value);
|
||||
break;
|
||||
case 'content':
|
||||
$this->setContent($value);
|
||||
break;
|
||||
case 'author':
|
||||
$this->setAuthor($value);
|
||||
break;
|
||||
case 'pubdate':
|
||||
$this->setPubDate(new Zend_Date($value));
|
||||
break;
|
||||
case 'tags':
|
||||
$this->setTags($value);
|
||||
break;
|
||||
case 'permalink':
|
||||
$this->setPermalink($value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function setId($id)
|
||||
{
|
||||
if (is_numeric($id))
|
||||
$this->_data['id'] = $id;
|
||||
}
|
||||
|
||||
public function setTitle($title)
|
||||
{
|
||||
if (is_string($title) && !isset($title[30])) {
|
||||
|
||||
$this->_data['title'] = $title;
|
||||
$this->generatePermalink();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setContent($content)
|
||||
{
|
||||
if (is_string($content)) {
|
||||
|
||||
$this->_data['content'] = $content;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setAuthor($author)
|
||||
{
|
||||
if ($author instanceof User) {
|
||||
|
||||
$this->_data['author'] = $author;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function addTag($tag)
|
||||
{
|
||||
if (is_string($tag)) {
|
||||
$this->_data['tags'][] = $tag;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setTags(array $tags)
|
||||
{
|
||||
// make sure the all elements are strings
|
||||
foreach ($tags as $key => $tag) {
|
||||
|
||||
if (!is_string($tag)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$this->_data['tags'] = $tags;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setPubDate($date)
|
||||
{
|
||||
|
||||
if (Fiktiv_Date::isDate($date)) {
|
||||
|
||||
$this->_data['pubDate'] = new Fiktiv_Date($date);
|
||||
|
||||
} else if ($date instanceof Zend_Date) {
|
||||
|
||||
$this->_data['pubDate'] = $date;
|
||||
|
||||
} else {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isPublished($flag = null)
|
||||
{
|
||||
if (null === $flag) {
|
||||
return $this->_data['isPublished'];
|
||||
}
|
||||
|
||||
$this->_data['isPublished'] = (boolean)$flag;
|
||||
}
|
||||
|
||||
public function setPermalink($permalink)
|
||||
{
|
||||
if (is_string($permalink)) {
|
||||
$this->_data['permalink'] = $permalink;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function generatePermalink()
|
||||
{
|
||||
$permalink = $this->_data['title'];
|
||||
|
||||
// All to lowercase
|
||||
$permalink = strtolower($permalink);
|
||||
|
||||
// Remove special characters
|
||||
$permalink = filter_var($permalink, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
|
||||
// Replace stuff
|
||||
$replace = array(
|
||||
' ' => '-',
|
||||
'å' => 'a',
|
||||
'ä' => 'a',
|
||||
'ö' => 'o',
|
||||
'_' => '-',
|
||||
'!' => '',
|
||||
'?' => '',
|
||||
'.' => '',
|
||||
',' => '',
|
||||
);
|
||||
foreach($replace as $char => $value) {
|
||||
$permalink = str_replace($char,$value,$permalink);
|
||||
}
|
||||
|
||||
$this->_data['permalink'] = $permalink;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Mapper_About extends Fiktiv_Model_Mapper_DbTableAbstract
|
||||
{
|
||||
public function getContent($locale = null)
|
||||
{
|
||||
if ($locale === null) {
|
||||
$locale = 'se';
|
||||
}
|
||||
|
||||
//$ad = new Zend_Db_Adapter_Abstract();
|
||||
//$ad->fet
|
||||
$adapter = $this->_dbTable->getAdapter();
|
||||
|
||||
$select = $adapter
|
||||
->select()
|
||||
->from('About', array('Title', 'Content'))
|
||||
->where('Locale = ?', $locale);
|
||||
|
||||
return $adapter->fetchPairs($select);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,139 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract
|
||||
{
|
||||
|
||||
protected $_mapperUser = null;
|
||||
|
||||
public function __construct($dbtable = null) {
|
||||
parent::__construct($dbtable);
|
||||
|
||||
$this->_mapperUser = new Mapper_User(new Table_User());
|
||||
}
|
||||
|
||||
protected function _createBlogPosts($obj)
|
||||
{
|
||||
$posts = array();
|
||||
foreach($obj as $post)
|
||||
$posts[] = $this->_createBlogPost($post);
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
protected function _createBlogPost($obj)
|
||||
{
|
||||
|
||||
if ($obj instanceof Zend_Db_Table_Row)
|
||||
$obj = $obj->toArray();
|
||||
|
||||
|
||||
if ($obj instanceof stdClass)
|
||||
$obj = (array) $obj;
|
||||
|
||||
|
||||
if (is_array($obj)) {
|
||||
|
||||
// Get Author
|
||||
if (isset($obj['userId'])) {
|
||||
$obj['author'] = Fiktiv_Data_Service::getInstance()->User->findById($obj['userId']);
|
||||
unset($obj['userId']);
|
||||
}
|
||||
|
||||
// Get tags
|
||||
$obj['tags'] = $this->getTags($obj['id']);
|
||||
|
||||
$blogPost = new BlogPost($obj);
|
||||
|
||||
} else {
|
||||
$blogPost = null;
|
||||
}
|
||||
|
||||
return $blogPost;
|
||||
}
|
||||
|
||||
public function getTags($blogPostId)
|
||||
{
|
||||
return Fiktiv_Data_Service::getInstance()->Tag->findByBlogPost($blogPostId);
|
||||
}
|
||||
|
||||
public function findById($id)
|
||||
{
|
||||
if (is_numeric($id)) {
|
||||
return $this->_createBlogPost($this->_dbTable->find($id)->current());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findByPermalink($permalink)
|
||||
{
|
||||
if (is_string($permalink)) {
|
||||
return $this->_createBlogPost($this->_dbTable->fetchRow($this->quoteInto('permalink LIKE ?', $permalink)));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findAllPostByAuthor($user)
|
||||
{
|
||||
if ($user instanceof User) {
|
||||
return $this->_createBlogPost($this->_dbTable->findByUser($user->getId()));
|
||||
}
|
||||
}
|
||||
|
||||
public function findByTag($tag)
|
||||
{
|
||||
$rows = $this->_dbTable->getAdapter()
|
||||
->select()->from('Post')
|
||||
->join('Post_has_Tag', 'Post_has_Tag.postId = Post.id')
|
||||
->join('Tag', 'Tag.id = Post_has_Tag.tagId')
|
||||
->where('Tag.name = ?', $tag)
|
||||
->query()->fetchAll();
|
||||
|
||||
return $this->_createBlogPosts($rows);
|
||||
}
|
||||
|
||||
public function findByMonth($month = null)
|
||||
{
|
||||
return $this->_dbTable->select()
|
||||
->from($this->_dbTable->_name,array('Month' => 'DATE_FORMAT(pubDate,"%Y %M")', 'Date' => 'DATE_FORMAT(pubDate,"%Y-%M")'))
|
||||
->group('Month')
|
||||
->query()
|
||||
->fetchAll();
|
||||
}
|
||||
|
||||
public function findByDate($date)
|
||||
{
|
||||
$date = new Fiktiv_Date($date);
|
||||
|
||||
$where = $this->quoteInto('pubDate LIKE ?', $date->getIso());
|
||||
|
||||
return $this->_createBlogPosts($this->_dbTable->fetchAll($where));
|
||||
}
|
||||
|
||||
public function findAll($limit = null)
|
||||
{
|
||||
|
||||
if (!is_numeric($limit))
|
||||
throw new Fiktiv_Exception('limit must be numeric');
|
||||
|
||||
return $this->_createBlogPosts($this->_dbTable->fetchAll(null, 'pubDate DESC', $limit));
|
||||
}
|
||||
|
||||
public function save(BlogPost $post)
|
||||
{
|
||||
$data = $post->toArray();
|
||||
|
||||
unset($data['id']);
|
||||
|
||||
$tags = $data['tags'];
|
||||
unset($data['tags']);
|
||||
|
||||
|
||||
|
||||
return $this->_dbTable->update(
|
||||
$data,
|
||||
$this->quoteInto('id = ?',$post->getId())
|
||||
);
|
||||
}
|
||||
}
|
||||
110
application/models/ModelBlogPost.php
Normal file
110
application/models/ModelBlogPost.php
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
class ModelBlogPost extends Fiktiv_Model_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'Post';
|
||||
protected $_primary = 'id';
|
||||
protected $_rowClass = 'Post';
|
||||
|
||||
protected $_dependentTables = array('ModelUser', 'ModelPostTag');
|
||||
|
||||
public function findByMonth()
|
||||
{
|
||||
$posts = $this->select()->from($this->_name, array('Month' => 'DATE_FORMAT(pubDate,"%Y %M")', 'Date' => 'DATE_FORMAT(pubDate,"%Y-%M")'))
|
||||
->group('Month')
|
||||
->query()
|
||||
->fetchAll();
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
public function findByAuthor($user)
|
||||
{
|
||||
if ($user instanceof User) {
|
||||
|
||||
} else if (is_integer($user)) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function findByTag($tag)
|
||||
{
|
||||
// Is this right?
|
||||
|
||||
$tags = new ModelTag();
|
||||
$row = $tags->fetchRow("name = 'Linux'");
|
||||
$posts = $row->findManyToManyRowset('ModelBlogPost', 'ModelPostTag');
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
public function findByDate($date, $format)
|
||||
{
|
||||
// This is a joke, FIXIT!
|
||||
|
||||
$date = new Fiktiv_Date($date,$format);
|
||||
|
||||
$where = $this->quoteInto('DATE_FORMAT(pubDate,"%Y %M") LIKE ?', $date->toString('Y MMMM'));
|
||||
|
||||
return $this->fetchAll($where);
|
||||
}
|
||||
|
||||
public function findByPermlink($permlink)
|
||||
{
|
||||
return $this->fetchRow($this->quoteInto('permlink', $permlink));
|
||||
}
|
||||
|
||||
public function createPost($data, $id = null)
|
||||
{
|
||||
$myTags = explode(',', $data['tags']);
|
||||
unset($data['tags']);
|
||||
$data['userId'] = Zend_Auth::getInstance()->getIdentity()->id;
|
||||
|
||||
$data['pubDate'] = Zend_Date::now()->getIso();
|
||||
$data['isPublished'] = 1;
|
||||
|
||||
if ($id) {
|
||||
$row = $this->find($id)->current();
|
||||
$row->setFromArray($data);
|
||||
$row->save();
|
||||
} else {
|
||||
$row = $this->find($this->insert($data))->current();
|
||||
}
|
||||
|
||||
if ($myTags) {
|
||||
$tags = new ModelTag();
|
||||
$tags->tagsToPost($myTags, $row->id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function generatePermalink()
|
||||
{
|
||||
$permalink = $this->_data['title'];
|
||||
|
||||
// All to lowercase
|
||||
$permalink = strtolower($permalink);
|
||||
|
||||
// Remove special characters
|
||||
$permalink = filter_var($permalink, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
|
||||
// Replace stuff
|
||||
$replace = array(
|
||||
' ' => '-',
|
||||
'å' => 'a',
|
||||
'ä' => 'a',
|
||||
'ö' => 'o',
|
||||
'_' => '-',
|
||||
'!' => '',
|
||||
'?' => '',
|
||||
'.' => '',
|
||||
',' => '',
|
||||
);
|
||||
foreach($replace as $char => $value) {
|
||||
$permalink = str_replace($char,$value,$permalink);
|
||||
}
|
||||
|
||||
$this->_data['permalink'] = $permalink;
|
||||
}
|
||||
}
|
||||
21
application/models/ModelPostTag.php
Normal file
21
application/models/ModelPostTag.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
class ModelPostTag extends Fiktiv_Model_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'Post_has_Tag';
|
||||
|
||||
protected $_referenceMap = array(
|
||||
'ModelBlogPost' => array(
|
||||
'columns' => 'postId',
|
||||
'refTableClass' => 'ModelBlogPost',
|
||||
'refColumns' => 'id'
|
||||
),
|
||||
'ModelTag' => array(
|
||||
'columns' => 'tagId',
|
||||
'refTableClass' => 'ModelTag',
|
||||
'refColumns' => 'id'
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
35
application/models/ModelTag.php
Normal file
35
application/models/ModelTag.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
class ModelTag extends Fiktiv_Model_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'Tag';
|
||||
|
||||
protected $_dependentTables = array('ModelPostTag');
|
||||
|
||||
public function tagsToPost($tags, $postId)
|
||||
{
|
||||
$postsTags = new ModelPostTag();
|
||||
|
||||
$postsTags->delete($this->quoteInto('postId = ?', $postId));
|
||||
|
||||
foreach ($tags as $key => $tag) {
|
||||
$tags[$key] = trim($tag);
|
||||
|
||||
$tag = $this->fetchRow($this->quoteInto('name = ?', $tags[$key]));
|
||||
|
||||
if (!$tag) {
|
||||
$tagId = $this->insert(array('name' => $tags[$key]));
|
||||
} else {
|
||||
$tagId = $tag->id;
|
||||
}
|
||||
|
||||
|
||||
$postsTags->insert(array(
|
||||
'postId' => $postId,
|
||||
'tagId' => $tagId
|
||||
));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +1,27 @@
|
|||
<?php
|
||||
|
||||
class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract
|
||||
class ModelUser extends Fiktiv_Model_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'User';
|
||||
protected $_primary = 'id';
|
||||
protected $_rowClass = 'User';
|
||||
|
||||
protected function _createUser($object)
|
||||
{
|
||||
|
||||
if ($object instanceof stdClass)
|
||||
$object = (array) $object;
|
||||
|
||||
|
||||
if ($object instanceof Zend_Db_Table_Row)
|
||||
$object = $object->toArray();
|
||||
|
||||
|
||||
$user = null;
|
||||
|
||||
if (is_array($object))
|
||||
$user = new User($object);
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
protected $_referenceMap = array(
|
||||
'ModelBlogPost' => array(
|
||||
'columns' => array('id'),
|
||||
'refTableClass' => 'ModelBlogPost',
|
||||
'refColumns' => array('userId')
|
||||
)
|
||||
);
|
||||
|
||||
public function findById($id)
|
||||
{
|
||||
if (is_numeric($id)) {
|
||||
return $this->_createUser($this->_dbTable->find($id)->current());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -55,7 +49,7 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract
|
|||
*/
|
||||
public function findRandom()
|
||||
{
|
||||
return $this->_createUser($this->_dbTable->fetchAll(null, 'RAND()', 1)->current());
|
||||
return $this->fetchAll(null, 'RAND()', 1)->current();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -70,7 +64,7 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract
|
|||
$auth = Zend_Auth::getInstance();
|
||||
|
||||
// Setup auth adapter
|
||||
$authAdapter = new Zend_Auth_Adapter_DbTable($this->_dbTable->getAdapter(), $this->_dbTable->_name, 'email', 'password');
|
||||
$authAdapter = new Zend_Auth_Adapter_DbTable($this->getAdapter(), $this->_name, 'email', 'password');
|
||||
|
||||
// Set credentials
|
||||
$authAdapter->setIdentity($email);
|
||||
|
|
@ -84,7 +78,8 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract
|
|||
|
||||
// Keep all but password and salt in session.
|
||||
$storage = $auth->getStorage();
|
||||
$storage->write($this->_createUser($authAdapter->getResultRowObject(null, array('password', 'salt'))));
|
||||
$user = $authAdapter->getResultRowObject(null, array('password', 'salt'));
|
||||
$storage->write($user);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -105,25 +100,11 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract
|
|||
if (!is_numeric($userId))
|
||||
return false;
|
||||
|
||||
return $this->_dbTable->update(
|
||||
return $this->update(
|
||||
array(
|
||||
'password' => hash('sha256',$password)
|
||||
),
|
||||
$this->_dbTable->getAdapter()->quoteInto('id = ?',$userId)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save User object
|
||||
*/
|
||||
public function save(User $user)
|
||||
{
|
||||
$data = $user->toArray();
|
||||
unset($data['id']);
|
||||
|
||||
return $this->_dbTable->update($data,
|
||||
$this->_dbTable->getAdapter()->quoteInto('id = ?',$user->getId())
|
||||
$this->getAdapter()->quoteInto('id = ?',$userId)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Table_About extends Fiktiv_Db_Table_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'about';
|
||||
protected $_primary = 'id';
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Table_BlogPost extends Fiktiv_Db_Table_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'Post';
|
||||
protected $_primary = 'id';
|
||||
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Table_PosthasTag extends Fiktiv_Db_Table_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'Post_has_Tag';
|
||||
protected $_referenceMap = array(
|
||||
'Post' => array(
|
||||
'columns' => 'postId',
|
||||
'refTableClass' => 'Post',
|
||||
'refColumns' => 'id'
|
||||
),
|
||||
'Tag' => array(
|
||||
'columns' => 'tagId',
|
||||
'refTableClass' => 'Tag',
|
||||
'refColumns' => 'id'
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Table_Tag extends Fiktiv_Db_Table_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'Tag';
|
||||
protected $_primary = 'id';
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Table_User extends Fiktiv_Db_Table_Abstract
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'User';
|
||||
protected $_primary = 'id';
|
||||
|
||||
}
|
||||
|
|
@ -1,173 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* User
|
||||
*
|
||||
*/
|
||||
class User extends Fiktiv_Model_Abstract
|
||||
{
|
||||
const AVATAR_NONE = 0;
|
||||
const AVATAR_FIKTIV = 1;
|
||||
const AVATAR_GRVTAR = 2;
|
||||
|
||||
protected $_data = array();
|
||||
|
||||
protected $_default = array(
|
||||
'id' => 0,
|
||||
'email' => null,
|
||||
'firstName' => null,
|
||||
'lastName' => null,
|
||||
'regDate' => null,
|
||||
'isDeleted' => false,
|
||||
'avatar' => self::AVATAR_NONE,
|
||||
'avatarImage' => null,
|
||||
'userRole' => Acl::ROLE_VISITOR
|
||||
);
|
||||
|
||||
public function setId($id)
|
||||
{
|
||||
if (is_numeric($id))
|
||||
$this->_data['id'] = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user email
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function setEmail($email)
|
||||
{
|
||||
$validator = new Zend_Validate_EmailAddress();
|
||||
|
||||
if ($validator->isValid($email)) {
|
||||
$this->_data['email'] = $email;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user lastname
|
||||
*
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function setLastName($name)
|
||||
{
|
||||
if (is_string($name) && preg_match('/^[A-ö\ \.\-]{0,20}$/', $name)) {
|
||||
$this->_data['lastName'] = $name;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user firstname
|
||||
*
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function setFirstName($name)
|
||||
{
|
||||
$name = trim($name);
|
||||
|
||||
if (is_string($name) && preg_match('/^[A-ö]{2,8}\-?[A-ö]{2,8}$/', $name)) {
|
||||
$this->_data['firstName'] = $name;
|
||||
return true;
|
||||
}
|
||||
|
||||
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}))\-' .
|
||||
'(?:(?:0[1-9])|(?:1[012]))\-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01]))$/', $date)) {
|
||||
|
||||
$this->_data['regDate'] = $date;
|
||||
} else if ($date instanceof Zend_Date) {
|
||||
|
||||
$this->_data['regDate'] = $date->toString('yyyy-MM-dd');
|
||||
} else {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setAvatar($type)
|
||||
{
|
||||
|
||||
if (self::AVATAR_NONE === $type) {
|
||||
|
||||
$this->_data['avatar'] = self::AVATAR_NONE;
|
||||
$this->_data['avatarImage'] = null;
|
||||
|
||||
} else if (self::AVATAR_GRVTAR === $type) {
|
||||
|
||||
$this->_data['avatar'] = self::AVATAR_GRVTAR;
|
||||
$this->_data['avatarImage'] = null;
|
||||
|
||||
} else if (is_string($type)) {
|
||||
|
||||
$this->_data['avatar'] = self::AVATAR_FIKTIV;
|
||||
$this->_data['avatarImage'] = $type;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function setPassword($password)
|
||||
{
|
||||
if (!is_string($password))
|
||||
throw new Fiktiv_Exception('password must be string');
|
||||
|
||||
Fiktiv_Data_Service::getInstance()->User->setPassword($this, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user registration date
|
||||
*
|
||||
* @return string $_regDate
|
||||
*/
|
||||
public function userSince()
|
||||
{
|
||||
return !empty($_regDate) ? $_regDate : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current status of the account.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isActive()
|
||||
{
|
||||
return !$this->_data['isDeleted'];
|
||||
}
|
||||
|
||||
/**
|
||||
* String representation of the object
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return '(' . __CLASS__ . '){' . $this->_data['email'] . ', ' . $this->_data['firstName'] . ' ' . $this->_data['lastName'] . '}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert object to array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->_data;
|
||||
}
|
||||
|
||||
}
|
||||
36
application/models/rows/Post.php
Normal file
36
application/models/rows/Post.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
class Post extends Fiktiv_Model_Row_Abstract
|
||||
{
|
||||
protected $_tags = null;
|
||||
|
||||
|
||||
public function getPubDate()
|
||||
{
|
||||
return new Fiktiv_Date($this->pubDate);
|
||||
}
|
||||
|
||||
public function hasTags()
|
||||
{
|
||||
if (null === $this->_tags) {
|
||||
$this->_tags = $this->getTags();
|
||||
}
|
||||
|
||||
return (0 !== $this->_tags->count());
|
||||
}
|
||||
|
||||
public function getTags()
|
||||
{
|
||||
// Quickie!
|
||||
if (null !== $this->_tags) {
|
||||
return $this->_tags;
|
||||
}
|
||||
|
||||
return $this->findManyToManyRowset('ModelTag', 'ModelPostTag');
|
||||
}
|
||||
|
||||
public function getAuthor()
|
||||
{
|
||||
return $this->findDependentRowset('ModelUser')->current();
|
||||
}
|
||||
}
|
||||
9
application/models/rows/User.php
Normal file
9
application/models/rows/User.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
/**
|
||||
* User
|
||||
*
|
||||
*/
|
||||
class User extends Fiktiv_Model_Row_Abstract
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Access Fiktiv_Db_Service instance from controller
|
||||
* Not easier than calling the class itself but what the heck.
|
||||
*/
|
||||
class Fiktiv_Controller_Action_Helper_DataService extends Zend_Controller_Action_Helper_Abstract
|
||||
{
|
||||
public function dataService()
|
||||
{
|
||||
return Fiktiv_Data_Service::getInstance();
|
||||
}
|
||||
|
||||
public function direct()
|
||||
{
|
||||
return $this->dataService();
|
||||
}
|
||||
}
|
||||
|
|
@ -32,6 +32,15 @@ class Fiktiv_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract
|
|||
if ($translate->isAvailable($lang)) {
|
||||
$translate->setLocale($lang);
|
||||
}
|
||||
|
||||
// Set database locale
|
||||
$lookup = array(
|
||||
'sv' => 'sv_SE',
|
||||
'en' => 'en_US'
|
||||
);
|
||||
|
||||
$database = $bootstrap->getResource('database');
|
||||
$database->query("SET lc_time_names = ?;", $lookup[$lang]);
|
||||
}
|
||||
|
||||
protected function test()
|
||||
|
|
|
|||
|
|
@ -1,68 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Data service to allow easy access to data storage
|
||||
*
|
||||
*/
|
||||
class Fiktiv_Data_Service
|
||||
{
|
||||
/**
|
||||
* Hold the singleton instance
|
||||
*
|
||||
* @var Fiktiv_Data_Service
|
||||
*/
|
||||
protected static $_instance = null;
|
||||
|
||||
/**
|
||||
* Set of datapoints that the service can access
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_dataStorage = array();
|
||||
|
||||
/**
|
||||
* Returns the current singleton instance
|
||||
*
|
||||
* @return Fiktiv_Data_Service $_instance
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$_instance === null) {
|
||||
self::$_instance = new Fiktiv_Data_Service();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
if (array_key_exists($name, $this->_dataStorage)) {
|
||||
|
||||
// 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($table);
|
||||
}
|
||||
|
||||
|
||||
} else if (class_exists($name)) {
|
||||
|
||||
$this->_dataStorage[$name] = new $name($table);
|
||||
}
|
||||
|
||||
return $this->_dataStorage[$name];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,74 +1,9 @@
|
|||
<?php
|
||||
|
||||
abstract class Fiktiv_Model_Abstract
|
||||
abstract class Fiktiv_Model_Abstract extends Zend_Db_Table_Abstract
|
||||
{
|
||||
protected $_default = array();
|
||||
|
||||
public function __construct($data = array())
|
||||
public function quoteInto($text, $value, $type = null, $count = null)
|
||||
{
|
||||
$data = (array)$data;
|
||||
|
||||
$data = array_merge($this->_default, array_intersect_key($data, $this->_default));
|
||||
|
||||
$this->setAttribs($data);
|
||||
}
|
||||
|
||||
public function __call($name, $args)
|
||||
{
|
||||
|
||||
// getX methods
|
||||
if ('get' == substr($name,0,3)) {
|
||||
$property = substr($name, 3);
|
||||
|
||||
// Wrong! use mb_
|
||||
$property[0] = strtolower($property[0]);
|
||||
|
||||
if (isset($this->_data[$property]))
|
||||
return $this->_data[$property];
|
||||
|
||||
|
||||
} else if ('set' == substr($name,0,3)) {
|
||||
$property = substr($name, 3);
|
||||
|
||||
// Wrong! use mb_
|
||||
$property[0] = strtolower($property[0]);
|
||||
|
||||
if (in_array($property,$this->_default))
|
||||
$this->_data[$property] = $args[0];
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$methodName = 'set' . ucfirst($name);
|
||||
if (method_exists($this, $methodName)) {
|
||||
return $this->$methodName($value);
|
||||
}
|
||||
|
||||
if (array_key_exists($name, $this->_default))
|
||||
return $this->_data[$name] = $value;
|
||||
}
|
||||
|
||||
// Direct access to read data fields
|
||||
public function __get($name)
|
||||
{
|
||||
if (isset($this->_data[$name])) {
|
||||
return $this->_data[$name];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function setAttribs(array $data)
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$class = get_class($this);
|
||||
return Fiktiv_Data_Service::getInstance()->$class->save($this);
|
||||
return $this->getAdapter()->quoteInto($text, $value, $type, $count);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
abstract class Fiktiv_Model_Mapper_DbTableAbstract
|
||||
{
|
||||
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__);
|
||||
}
|
||||
}
|
||||
|
||||
public function update($data, $where)
|
||||
{
|
||||
return $this->_dbTable->update($data, $where);
|
||||
}
|
||||
|
||||
public function quoteInto($text, $value)
|
||||
{
|
||||
return $this->_dbTable->getAdapter()->quoteInto($text,$value);
|
||||
}
|
||||
}
|
||||
6
library/Fiktiv/Model/Row/Abstract.php
Normal file
6
library/Fiktiv/Model/Row/Abstract.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
abstract class Fiktiv_Model_Row_Abstract extends Zend_Db_Table_Row_Abstract
|
||||
{
|
||||
|
||||
}
|
||||
Reference in a new issue