Add badges/categories for news

This commit is contained in:
Dennis Eichhorn 2017-08-05 15:06:21 +02:00
parent d0c3f95cd4
commit b73141c968
4 changed files with 180 additions and 19 deletions

View File

@ -64,33 +64,28 @@ class Installer extends InstallerAbstract
)->execute();
$dbPool->get('core')->con->prepare(
'CREATE TABLE if NOT EXISTS `' . $dbPool->get('core')->prefix . 'news_tag` (
`news_tag_id` int(11) NOT NULL AUTO_INCREMENT,
`news_tag_news` int(11) NOT NULL,
`news_tag_tag` varchar(20) NOT NULL,
PRIMARY KEY (`news_tag_id`),
KEY `news_tag_news` (`news_tag_news`)
'CREATE TABLE if NOT EXISTS `' . $dbPool->get('core')->prefix . 'news_badge` (
`news_badge_id` int(11) NOT NULL AUTO_INCREMENT,
`news_badge_title` varchar(20) NOT NULL,
PRIMARY KEY (`news_badge_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;'
)->execute();
$dbPool->get('core')->con->prepare(
'ALTER TABLE `' . $dbPool->get('core')->prefix . 'news_tag`
ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'news_tag_ibfk_1` FOREIGN KEY (`news_tag_news`) REFERENCES `' . $dbPool->get('core')->prefix . 'news` (`news_id`);'
)->execute();
$dbPool->get('core')->con->prepare(
'CREATE TABLE if NOT EXISTS `' . $dbPool->get('core')->prefix . 'news_group` (
`news_group_id` int(11) NOT NULL AUTO_INCREMENT,
`news_group_news` int(11) NOT NULL,
`news_group_group` int(11) NOT NULL,
PRIMARY KEY (`news_group_id`),
KEY `news_group_news` (`news_group_news`)
'CREATE TABLE if NOT EXISTS `' . $dbPool->get('core')->prefix . 'news_badge_relation` (
`news_badge_relation_id` int(11) NOT NULL AUTO_INCREMENT,
`news_badge_relation_news` int(11) NOT NULL,
`news_badge_relation_badge` int(11) NOT NULL,
PRIMARY KEY (`news_badge_relation_id`),
KEY `news_badge_relation_news` (`news_badge_relation_news`),
KEY `news_badge_relation_badge` (`news_badge_relation_badge`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;'
)->execute();
$dbPool->get('core')->con->prepare(
'ALTER TABLE `' . $dbPool->get('core')->prefix . 'news_group`
ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'news_group_ibfk_1` FOREIGN KEY (`news_group_news`) REFERENCES `' . $dbPool->get('core')->prefix . 'news` (`news_id`);'
'ALTER TABLE `' . $dbPool->get('core')->prefix . 'news_badge_relation`
ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'news_badge_relation_ibfk_1` FOREIGN KEY (`news_badge_relation_news`) REFERENCES `' . $dbPool->get('core')->prefix . 'news` (`news_id`),
ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'news_badge_relation_ibfk_2` FOREIGN KEY (`news_badge_relation_badge`) REFERENCES `' . $dbPool->get('core')->prefix . 'news_badge` (`news_badge_id`);'
)->execute();
break;
}

51
Models/Badge.php Normal file
View File

@ -0,0 +1,51 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace Modules\News\Models;
/**
* News article class.
*
* @category Module
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Badge
{
private $id = 0;
private $name = '';
public function __construct()
{
}
public function getId() : int
{
return $this->id;
}
public function setName(string $name) /* : void */
{
$this->name = $name;
}
public function getName() : string
{
return $this->name;
}
}

97
Models/BadgeMapper.php Normal file
View File

@ -0,0 +1,97 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace Modules\News\Models;
use Modules\Admin\Models\AccountMapper;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Query\Builder;
use phpOMS\DataStorage\Database\Query\Column;
use phpOMS\DataStorage\Database\RelationType;
class BadgeMapper extends DataMapperAbstract
{
/**
* Columns.
*
* @var array
* @since 1.0.0
*/
static protected $columns = [
'news_badge_id' => ['name' => 'news_badge_id', 'type' => 'int', 'internal' => 'id'],
'news_badge_title' => ['name' => 'news_badge_title', 'type' => 'string', 'internal' => 'title'],
];
/**
* Primary table.
*
* @var string
* @since 1.0.0
*/
protected static $table = 'news_badge';
/**
* Primary field name.
*
* @var string
* @since 1.0.0
*/
protected static $primaryField = 'news_badge_id';
/**
* Create object.
*
* @param mixed $obj Object
* @param int $relations Behavior for relations creation
*
* @return mixed
*
* @since 1.0.0
*/
public static function create($obj, int $relations = RelationType::ALL)
{
try {
$objId = parent::create($obj, $relations);
if($objId === null || !is_scalar($objId)) {
return $objId;
}
$query = new Builder(self::$db);
$query->prefix(self::$db->getPrefix())
->insert(
'account_permission_account',
'account_permission_from',
'account_permission_for',
'account_permission_id1',
'account_permission_id2',
'account_permission_r',
'account_permission_w',
'account_permission_m',
'account_permission_d',
'account_permission_p'
)
->into('account_permission')
->values($obj->getCreatedBy(), 'news', 'news', 1, $objId, 1, 1, 1, 1, 1);
self::$db->con->prepare($query->toSql())->execute();
} catch (\Exception $e) {
return false;
}
return $objId;
}
}

View File

@ -118,6 +118,14 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable
*/
private $featured = false;
/**
* Badge.
*
* @var bool
* @since 1.0.0
*/
private $badges = [];
/**
* Constructor.
*
@ -129,6 +137,16 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable
$this->publish = new \DateTime('NOW');
}
public function getBadges() : array
{
return $this->badges;
}
public function addBadge(Badge $badge) /* : void */
{
$this->badges[] = $badge;
}
/**
* @return string
*