This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now we can manage site news, though some function is not ready.. BREAKING CHANGE: DB structure change: TABLE `news` added
- Loading branch information
Showing
12 changed files
with
448 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 2019/5/31 | ||
* Time: 10:09 | ||
*/ | ||
|
||
namespace apps\controllers; | ||
|
||
|
||
use apps\models\form\NewEditForm; | ||
use Rid\Http\Controller; | ||
|
||
class NewsController extends Controller | ||
{ | ||
public function actionIndex() { | ||
|
||
$query = app()->request->get('query','title'); | ||
$search = app()->request->get('search',''); | ||
if (empty($search)) { | ||
$count = app()->pdo->createCommand('SELECT COUNT(*) FROM `news`;')->queryScalar(); | ||
} else { | ||
$count = app()->pdo->createCommand([ | ||
['SELECT COUNT(*) FROM `news` WHERE 1=1 '], | ||
['AND `title` LIKE :search ', 'params' => ['search' => "%$search%"], 'if' => ($query == 'title' && !empty($search))], | ||
['AND `body` LIKE :search ', 'params' => ['search' => "%$search%"], 'if' => ($query == 'body' && !empty($search))], | ||
['AND `title` LIKE :st OR `body` LIKE :sb ', 'params' => ['st' => "%$search%",'sb' => "%$search%"], 'if' => ($query == 'both' && !empty($search))], | ||
])->queryScalar(); | ||
} | ||
|
||
$page = app()->request->get('page',1); | ||
if (!filter_var($page,FILTER_VALIDATE_INT)) $page = 1; | ||
$limit = 10; | ||
|
||
$news = app()->pdo->createCommand([ | ||
['SELECT * FROM `news` WHERE 1=1 '], | ||
['AND `title` LIKE :search ', 'params' => ['search' => "%$search%"], 'if' => ($query == 'title' && !empty($search))], | ||
['AND `body` LIKE :search ', 'params' => ['search' => "%$search%"], 'if' => ($query == 'body' && !empty($search))], | ||
['AND `title` LIKE :st OR `body` LIKE :sb ', 'params' => ['st' => "%$search%",'sb' => "%$search%"], 'if' => ($query == 'both' && !empty($search))], | ||
['ORDER BY create_at DESC '], | ||
['LIMIT :offset, :rows', 'params' => ['offset' => ($page - 1) * $limit, 'rows' => $limit]], | ||
])->queryAll(); | ||
|
||
return $this->render('news/index', ['news' => $news, 'query' => $query, 'search' => $search, 'count' => $count, 'limit' => $limit]); | ||
|
||
} | ||
|
||
public function actionNew() { | ||
if (app()->request->isPost()) { | ||
$newform = new NewEditForm(); | ||
$newform->setData(app()->request->post()); | ||
$success = $newform->validate(); | ||
if (!$success) { | ||
return $this->render('errors/action_fail', ['title' => 'new blog failed', 'msg' => $newform->getError()]); | ||
} else { | ||
$newform->flush(); // Save the news | ||
return app()->response->redirect('/news'); | ||
} | ||
} elseif (app()->user->isPrivilege('manage_news')) { | ||
return $this->render('news/edit'); | ||
} | ||
return $this->render('errors/action_fail', ['title' => 'Action Failed', 'msg' => 'action not allowed']); | ||
} | ||
|
||
public function actionEdit() | ||
{ | ||
if (app()->request->isPost()) { | ||
$newform = new NewEditForm(); | ||
$newform->setData(app()->request->post()); | ||
$success = $newform->validate(); | ||
if (!$success) { | ||
return $this->render('errors/action_fail', ['title' => 'Upload Failed', 'msg' => $newform->getError()]); | ||
} else { | ||
$newform->flush(); // Save the news | ||
return app()->response->redirect('/news'); | ||
} | ||
} elseif (app()->user->isPrivilege('manage_news')) { | ||
$id = app()->request->get('id', 0); | ||
if (filter_var($id, FILTER_VALIDATE_INT) && $id > 0) { | ||
// TODO add other check | ||
$news = app()->pdo->createCommand('SELECT * FROM news WHERE id= :id')->bindParams(['id' => $id])->queryOne(); | ||
return $this->render('news/edit', ['news' => $news]); | ||
} | ||
} | ||
return $this->render('errors/action_fail', ['title' => 'Action Failed', 'msg' => 'action not allowed']); | ||
} | ||
|
||
public function actionDelete() { | ||
if (app()->user->isPrivilege('manage_news')) { | ||
$id = app()->request->get('id',0); | ||
if (filter_var($id,FILTER_VALIDATE_INT) && $id > 0) { | ||
// TODO add other check | ||
app()->pdo->createCommand('DELETE FROM news WHERE id= :id')->bindParams(['id'=>$id])->execute(); | ||
} | ||
return app()->response->redirect('/news'); | ||
} | ||
return $this->render('errors/action_fail', ['title' => 'Action Failed', 'msg' => 'action not allowed']); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 2019/5/31 | ||
* Time: 19:56 | ||
*/ | ||
|
||
namespace apps\models\form; | ||
|
||
|
||
use Rid\Validators\Validator; | ||
|
||
class NewEditForm extends Validator | ||
{ | ||
public $id = 0; | ||
|
||
public $user_id; | ||
public $title; | ||
public $body; | ||
public $notify = 0; | ||
public $force_read = 0; | ||
|
||
public function __construct(array $config = []) | ||
{ | ||
parent::__construct($config); | ||
$this->user_id = app()->user->getId(); | ||
} | ||
|
||
public static function inputRules() | ||
{ | ||
return [ | ||
'id' => 'integer', | ||
'title' => [ | ||
['required'], | ||
['maxlength', ['max' => 255]] | ||
], | ||
'body' => 'required', | ||
'notify' => [ | ||
['Integer'], | ||
['Between', ['min' => 0, 'max' => 1]] | ||
], | ||
'force_read' => [ | ||
['Integer'], | ||
['Between', ['min' => 0, 'max' => 1]] | ||
] | ||
]; | ||
} | ||
|
||
public function flush() { | ||
if ($this->id == 0) { // This is new news | ||
app()->pdo->createCommand('INSERT INTO news (user_id,create_at,title,body,notify,force_read) VALUES (:uid,NOW(),:title,:body,:notify,:fread);')->bindParams([ | ||
'uid' => $this->user_id, 'title' => $this->title, 'body' => $this->body, | ||
'notify' => $this->notify, 'fread' => $this->force_read | ||
])->execute(); | ||
} else { // This is news edit | ||
app()->pdo->createCommand('UPDATE news SET user_id = :uid, title = :title, body = :body, notify = :notify, force_read = :fread WHERE id=:id')->bindParams([ | ||
'id' => $this->id, 'uid' => $this->user_id, | ||
'title' => $this->title, 'body' => $this->body, | ||
'notify' => $this->notify, 'fread' => $this->force_read | ||
])->execute(); | ||
} | ||
// Clean News Cache | ||
app()->redis->del('Site:recent_news'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 2019/5/31 | ||
* Time: 10:12 | ||
* | ||
* @var League\Plates\Template\Template $this | ||
*/ | ||
?> | ||
|
||
<?= $this->layout('layout/base') ?> | ||
|
||
<?php $this->start('title')?>Site News Edit<?php $this->end();?> | ||
|
||
<?php $this->start('container') ?> | ||
<div class="row"> | ||
<div class="text-center"><h2>New/Edit Site News</h2></div> | ||
<div class="col-md-8 col-md-offset-2"> | ||
<form method="post"> | ||
<?php if (isset($news)): ?> | ||
<label> | ||
<input name="id" value="<?= $news['id'] ?>" style="display: none"> | ||
</label> | ||
<?php endif; ?> | ||
<div class="form-group"> | ||
<label for="title">Title</label> | ||
<input type="text" class="form-control" id="title" name="title" placeholder="The title of news" value="<?= isset($news) ? $news['title'] : '' ?>"> | ||
</div> | ||
<div class="form-group"> | ||
<label for="body">Body</label> | ||
<textarea class="form-control" id="body" name="body" cols="100" rows="20" placeholder=""><?= isset($news) ? $news['body'] : '' ?></textarea> | ||
</div> | ||
<div class="form-group"> | ||
<div class="checkbox"> | ||
<label> | ||
<input type="checkbox" name="notify" value="1"<?= isset($news) && $news['notify'] ? ' checked' : '' ?>> Notify All Member. | ||
</label> | ||
</div><!-- TODO add support --> | ||
<div class="checkbox"> | ||
<label> | ||
<input type="checkbox" name="force_read" value="1"<?= isset($news) && $news['force_read'] ? ' checked' : '' ?>> All Member <b>MUST</b> Read this News, Before they can do other things!!! | ||
</label> | ||
</div><!-- TODO add support --> | ||
</div> | ||
<div class="text-center"> | ||
<button type="submit" class="btn btn-primary">提交</button> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
<?php $this->end();?> |
Oops, something went wrong.