Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

掲示板のリファクタリング #94

Merged
merged 8 commits into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 62 additions & 47 deletions Controller/BbsArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
*/
class BbsArticlesController extends BbsesAppController {

/**
* サイト内リンクのID
*
* @var int
*/
const LINK_ID_FORMAT = 'bbs-article-%s';

/**
* use models
*
Expand Down Expand Up @@ -104,11 +111,9 @@ public function index() {
$query['order'] = array('BbsArticle.created' => 'desc');
}
//表示件数
if (isset($this->params['named']['limit'])) {
$query['limit'] = (int)$this->params['named']['limit'];
} else {
$query['limit'] = $this->viewVars['bbsFrameSetting']['articles_per_page'];
}
$query['limit'] = (int)Hash::get(
$this->params['named'], 'limit', $this->viewVars['bbsFrameSetting']['articles_per_page']
);

$this->Paginator->settings = $query;
try {
Expand Down Expand Up @@ -145,29 +150,37 @@ public function view() {
if (! $bbsArticle) {
return $this->throwBadRequest();
}
$this->set('currentBbsArticle', $bbsArticle);

$conditions = $this->BbsArticle->getWorkflowConditions();

//事前準備
$result = $this->__prepare($bbsArticle);
if (! $result) {
return $this->throwBadRequest();
}
$this->set('currentBbsArticle', $bbsArticle);

//新着データを既読にする
$this->BbsArticle->saveTopicUserStatus($bbsArticle);

//根記事を指していない場合、根記事+ページ内リンクに遷移する
if ($bbsArticle['BbsArticleTree']['article_no'] !== '1') {
$url = NetCommonsUrl::blockUrl(array(
'action' => 'view',
'key' => $this->viewVars['rootBbsArticle']['BbsArticle']['key'],
'#' => '/' . sprintf(self::LINK_ID_FORMAT, $bbsArticle['BbsArticleTree']['id'])
));
return $this->redirect($url);
}

//子記事の取得
$this->BbsArticleTree->Behaviors->load('Tree', array(
'scope' => $conditions
'scope' => $this->BbsArticle->getWorkflowConditions()
));
$children = $this->BbsArticleTree->children(
$bbsArticle['BbsArticleTree']['id'], false, null, 'BbsArticleTree.id DESC', null, 1, 1
);
$children = Hash::combine($children, '{n}.BbsArticleTree.id', '{n}');

$this->set('bbsArticleChildren', $children);

//新着データを既読にする
$this->BbsArticle->saveTopicUserStatus($bbsArticle);
}

/**
Expand All @@ -186,15 +199,11 @@ public function add() {
unset($data['BbsArticle']['id']);

if ($bbsArticle = $this->BbsArticle->saveBbsArticle($data)) {
$url = NetCommonsUrl::actionUrl(array(
'controller' => $this->params['controller'],
$url = NetCommonsUrl::blockUrl(array(
'action' => 'view',
'block_id' => $this->data['Block']['id'],
'frame_id' => $this->data['Frame']['id'],
'key' => $bbsArticle['BbsArticle']['key']
));
$this->redirect($url);
return;
return $this->redirect($url);
}
$this->NetCommons->handleValidationError($this->BbsArticle->validationErrors);

Expand All @@ -220,9 +229,8 @@ public function add() {
* @return void
*/
public function reply() {
$this->view = 'edit';

$bbsArticleKey = Hash::get($this->request->params, 'key', null);

$bbsArticle = $this->BbsArticle->getWorkflowContents('first', array(
'recursive' => 0,
'conditions' => array(
Expand All @@ -237,10 +245,6 @@ public function reply() {

//事前準備
$this->set('currentBbsArticle', $bbsArticle);
$result = $this->__prepare($bbsArticle);
if (! $result) {
return $this->throwBadRequest();
}

if ($this->request->is('post')) {
$data = $this->data;
Expand All @@ -252,12 +256,9 @@ public function reply() {

$bbsArticle = $this->BbsArticle->saveBbsArticle($data);
if ($bbsArticle) {
$url = NetCommonsUrl::actionUrl(array(
'controller' => $this->params['controller'],
$url = NetCommonsUrl::blockUrl(array(
'action' => 'view',
'block_id' => $this->data['Block']['id'],
'frame_id' => $this->data['Frame']['id'],
'key' => $bbsArticle['BbsArticle']['key']
'key' => $bbsArticle['BbsArticle']['key'],
));
return $this->redirect($url);
}
Expand Down Expand Up @@ -324,12 +325,10 @@ public function edit() {
$data['BbsArticle']['status'] = $this->Workflow->parseStatus();
unset($data['BbsArticle']['id']);

if ($bbsArticle = $this->BbsArticle->saveBbsArticle($data)) {
$url = NetCommonsUrl::actionUrl(array(
'controller' => $this->params['controller'],
$bbsArticle = $this->BbsArticle->saveBbsArticle($data);
if ($bbsArticle) {
$url = NetCommonsUrl::blockUrl(array(
'action' => 'view',
'block_id' => $this->data['Block']['id'],
'frame_id' => $this->data['Frame']['id'],
'key' => $bbsArticle['BbsArticle']['key']
));
return $this->redirect($url);
Expand All @@ -341,7 +340,6 @@ public function edit() {
$this->request->data['Bbs'] = $this->viewVars['bbs'];
$this->request->data['Frame'] = Current::read('Frame');
$this->request->data['Block'] = Current::read('Block');

}

$comments = $this->BbsArticle->getCommentsByContentKey($this->request->data['BbsArticle']['key']);
Expand Down Expand Up @@ -389,11 +387,8 @@ public function delete() {
}

if (isset($parentBbsArticle)) {
$url = NetCommonsUrl::actionUrl(array(
'controller' => $this->params['controller'],
$url = NetCommonsUrl::blockUrl(array(
'action' => 'view',
'block_id' => $this->data['Block']['id'],
'frame_id' => $this->data['Frame']['id'],
'key' => $parentBbsArticle['BbsArticle']['key']
));
} else {
Expand All @@ -405,29 +400,47 @@ public function delete() {
/**
* approve
*
* @return void
* @return mixed
*/
public function approve() {
if (! $this->request->is('put')) {
return $this->throwBadRequest();
}

$data = $this->data;
$bbsArticle = $this->BbsArticle->getWorkflowContents('first', array(
'recursive' => 0,
'conditions' => array(
$this->BbsArticle->alias . '.bbs_id' => $this->data['BbsArticle']['bbs_id'],
$this->BbsArticle->alias . '.key' => $this->data['BbsArticle']['key']
)
));
if (! $bbsArticle) {
return $this->throwBadRequest();
}
//ステータスチェック
if ($bbsArticle['BbsArticle']['status'] !== WorkflowComponent::STATUS_APPROVED) {
return $this->throwBadRequest();
}

$data['BbsArticle'] = $bbsArticle['BbsArticle'];
unset($data['BbsArticle']['created'], $data['BbsArticle']['created_user']);
unset($data['BbsArticle']['modified'], $data['BbsArticle']['modified_user']);
unset($data['BbsArticle']['id']);
$data['BbsArticle']['status'] = $this->Workflow->parseStatus();
if (! $data['BbsArticle']['status']) {

//リクエストのステータスチェック
if ($data['BbsArticle']['status'] !== WorkflowComponent::STATUS_PUBLISHED) {
return $this->throwBadRequest();
}

if ($this->BbsArticle->saveCommentAsPublish($data)) {
$result = $this->BbsArticle->saveBbsArticle($data);
if ($result) {
$this->NetCommons->setFlashNotification(
__d('net_commons', 'Successfully saved.'), array('class' => 'success')
);

$url = NetCommonsUrl::actionUrl(array(
'controller' => $this->params['controller'],
$url = NetCommonsUrl::blockUrl(array(
'action' => 'view',
'block_id' => $this->data['Block']['id'],
'frame_id' => $this->data['Frame']['id'],
'key' => $this->data['BbsArticle']['key']
));
return $this->redirect($url);
Expand All @@ -452,6 +465,8 @@ private function __prepare($bbsArticle) {
if (! $result) {
return false;
}
} else {
$this->set('rootBbsArticle', $bbsArticle);
}

if (! $bbsArticle['BbsArticleTree']['parent_id']) {
Expand Down
5 changes: 5 additions & 0 deletions Locale/jpn/LC_MESSAGES/bbses.po
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,8 @@ msgstr "1ページに表示するコメント数"
# メール関連
msgid "MailSetting.mail_fixed_phrase_body.popover"
msgstr "{X-SITE_NAME} : サイト名<br>{X-PLUGIN_NAME} : プラグイン名<br>{X-ROOM} : ルーム名<br>{X-BLOCK_NAME} : ブロック名<br>{X-BBS_NAME} : 掲示板名<br>{X-SUBJECT} : 記事タイトル<br>{X-USER} : 投稿者<br>{X-TO_DATE} : 投稿日時<br>{X-BODY} : 記事本文<br>{X-URL} : 投稿内容のURL"


#: Bbses/View/Elements/BbsArticles/reply_link.ctp:36
msgid "Created: "
msgstr "投稿日時: "
88 changes: 23 additions & 65 deletions Model/BbsArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,6 @@ class BbsArticle extends BbsesAppModel {

//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'BbsArticleTree' => array(
'type' => 'INNER',
'className' => 'Bbses.BbsArticleTree',
'foreignKey' => false,
'conditions' => 'BbsArticleTree.bbs_article_key=BbsArticle.key',
'fields' => '',
'order' => ''
),
);

/**
* Constructor. Binds the model's database table to the object.
*
Expand All @@ -117,6 +101,22 @@ public function __construct($id = false, $table = null, $ds = null) {
'Bbs' => 'Bbses.Bbs',
'BbsArticleTree' => 'Bbses.BbsArticleTree',
]);

$this->bindModel(array(
'belongsTo' => array(
'BbsArticleTree' => array(
'type' => 'INNER',
'className' => 'Bbses.BbsArticleTree',
'foreignKey' => false,
'conditions' => array(
'BbsArticle.key = BbsArticleTree.bbs_article_key',
'BbsArticle.language_id' => Current::read('Language.id', '0'),
),
'fields' => '',
'order' => ''
)
)
), false);
}

/**
Expand Down Expand Up @@ -259,6 +259,7 @@ public function deleteBbsArticle($data) {
if (! $bbsArticleTree) {
return false;
}

$bbsArticleTrees = $this->BbsArticleTree->find('list', array(
'recursive' => -1,
'fields' => array('id', 'bbs_article_key'),
Expand All @@ -271,67 +272,24 @@ public function deleteBbsArticle($data) {
try {
//BbsArticleの削除
$this->contentKey = $bbsArticleTrees;
if (! $this->deleteAll(array($this->alias . '.key' => $bbsArticleTrees), false, true)) {
$conditions = array(
$this->alias . '.key' => array_values($bbsArticleTrees)
);
if (! $this->deleteAll($conditions, false, true)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

//Treeデータの削除
$conditions = array(
$this->BbsArticleTree->alias . '.bbs_article_key' => $data['BbsArticle']['key']
$this->BbsArticleTree->alias . '.bbs_article_key' => $this->data['BbsArticle']['key']
);
if (! $this->BbsArticleTree->deleteAll($conditions, false, true)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

//Bbsのbbs_article_count、bbs_article_modified
$this->updateBbsByBbsArticle(
$this->data['Bbs']['id'], $data['Bbs']['key'],
$this->data['BbsArticle']['language_id']
);

//コメント数の更新
$this->updateBbsArticleChildCount(
$this->data['BbsArticleTree']['root_id'],
$this->data['BbsArticle']['language_id']
);

//トランザクションCommit
$this->commit();

} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}

return true;
}

/**
* Save Comment as publish
*
* @param array $data received post data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function saveCommentAsPublish($data) {
//トランザクションBegin
$this->begin();
$this->set($data);

try {
//BbsArticle登録処理
$this->id = (int)$data['BbsArticle']['id'];
if (! $this->saveField('status', $data['BbsArticle']['status'], false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
if (! $this->saveField('is_active', true, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

//コメント数の更新
$this->updateBbsArticleChildCount(
$data['BbsArticleTree']['root_id'],
$data['BbsArticle']['language_id']
$data['Bbs']['id'], $data['Bbs']['key'], $data['BbsArticle']['language_id']
);

//トランザクションCommit
Expand Down
Loading