Added News
This commit is contained in:
9
application/News/News.php
Normal file
9
application/News/News.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class News extends mfBaseModel {
|
||||
public $editor;
|
||||
|
||||
protected function afterLoad() {
|
||||
$this->editor = new User($this->edit_by);
|
||||
}
|
||||
}
|
||||
86
application/News/NewsController.php
Normal file
86
application/News/NewsController.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
class NewsController extends mfBaseController {
|
||||
|
||||
protected function init() {
|
||||
$this->needlogin=true;
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
$this->me = $me;
|
||||
$this->layout()->set("me",$me);
|
||||
|
||||
if(!$me->is(["Admin"])) {
|
||||
$this->redirect("Dashboard");
|
||||
}
|
||||
}
|
||||
|
||||
protected function indexAction() {
|
||||
$this->layout()->setTemplate("News/Index");
|
||||
|
||||
$newss = NewsModel::getAll();
|
||||
$this->layout()->set("newss", $newss);
|
||||
}
|
||||
|
||||
protected function saveAction($request) {
|
||||
if(!$this->me->isAdmin()) {
|
||||
return $this->redirect("News");
|
||||
}
|
||||
|
||||
$id = false;
|
||||
|
||||
$cl = new News();
|
||||
|
||||
$cl->subject = $request['subject'];
|
||||
$cl->text = preg_replace('/<[^>]*script/','',$request['new_text']);
|
||||
|
||||
|
||||
if(!$id) {
|
||||
$cl->create_by = $this->me->id;
|
||||
}
|
||||
$cl->edit_by = $this->me->id;
|
||||
|
||||
if(!$cl->save()) {
|
||||
$this->layout()->setFlash("Beim Speichern ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut!", "error");
|
||||
$this->layout()->set("new_news", $cl);
|
||||
$this->layout()->setTemplate("News/Index");
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("News gespeichert!","success");
|
||||
$this->redirect("News");
|
||||
|
||||
}
|
||||
|
||||
protected function deleteAction($request) {
|
||||
if(!$this->me->isAdmin()) {
|
||||
$this->redirect("News");
|
||||
}
|
||||
|
||||
if(!isset($request['id']) || !is_numeric($request['id']) || $request['id'] < 1) {
|
||||
$this->layout()->setFlash("Eintrag nicht gefunden", "error");
|
||||
$this->redirect("News");
|
||||
}
|
||||
$id = $request['id'];
|
||||
|
||||
$cl = new News($id);
|
||||
$cl->delete();
|
||||
$this->layout()->setFlash("Eintrag mit ID $id wurde gelöscht.","success");
|
||||
$this->redirect("News");
|
||||
|
||||
}
|
||||
|
||||
private function getNews() {
|
||||
$cl = [];
|
||||
|
||||
$res = $this->db()->select("News", "*", "1=1 ORDER BY `create` DESC");
|
||||
if(!$this->db()->num_rows($res)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
while($data = $this->db()->fetch_object($res)) {
|
||||
$cl[] = new News($data);
|
||||
}
|
||||
|
||||
return $cl;
|
||||
}
|
||||
}
|
||||
108
application/News/NewsModel.php
Normal file
108
application/News/NewsModel.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
class NewsModel {
|
||||
public $subject = null;
|
||||
public $text = null;
|
||||
|
||||
public $create_by = null;
|
||||
public $edit_by = null;
|
||||
public $create = null;
|
||||
public $edit = null;
|
||||
|
||||
|
||||
public static function create(Array $data) {
|
||||
$model = new News();
|
||||
|
||||
foreach($data as $field => $value) {
|
||||
if(property_exists(get_called_class(), $field)) {
|
||||
$model ->$field = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
|
||||
if($model->create_by === null) {
|
||||
$model->create_by = $me->id;
|
||||
}
|
||||
if($model->edit_by === null) {
|
||||
$model->edit_by = $me->id;
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public static function getOne($id) {
|
||||
if(!is_numeric($id) || !$id) {
|
||||
throw new Exception("Invalid number", 400);
|
||||
}
|
||||
$item = [];
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("News", "*", "id=$id LIMIT 1");
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new News($data);
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
|
||||
public static function getAll() {
|
||||
$items = [];
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("News", "*", "1 = 1 ORDER BY `create` DESC,`edit` DESC");
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new News($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
public static function getFirst() {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$res = $db->select("News", "*", "$where ORDER BY `create`,`edit` DESC LIMIT 1");
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new News($data);
|
||||
if($item->id) {
|
||||
return $item;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function search($filter) {
|
||||
$items = [];
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$res = $db->select("News", "*", "$where ORDER BY `create`,`edit` DESC");
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new News($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
private function getSqlFilter($filter) {
|
||||
$where = "1=1 ";
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
//var_dump($filter);exit;
|
||||
|
||||
|
||||
//var_dump($filter, $where);exit;
|
||||
return $where;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user