Skip to content

Commit

Permalink
Merge 268d77d into 2e79185
Browse files Browse the repository at this point in the history
  • Loading branch information
ohga21 committed Aug 27, 2018
2 parents 2e79185 + 268d77d commit 9f1b31d
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 122 deletions.
51 changes: 32 additions & 19 deletions Controller/TopicFrameSettingsController.php
Expand Up @@ -77,33 +77,39 @@ class TopicFrameSettingsController extends TopicsAppController {
* edit
*
* @return void
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function edit() {
$this->RoomsForm->setRoomsForCheckbox();
$this->PluginsForm->setPluginsRoomForCheckbox($this, $this->PluginsForm->findOptions);

$options = Hash::extract(
$this->viewVars['pluginsRoom'], '{n}.Plugin.key'
);
$pluginKeys = [];
foreach ($this->viewVars['pluginsRoom'] as $item) {
$pluginKeys[] = $item['Plugin']['key'];
}

$blocks = $this->TopicFrameSetting->getBlocks($options, array_keys($this->viewVars['rooms']));
$blocks = $this->TopicFrameSetting->getBlocks($pluginKeys, array_keys($this->viewVars['rooms']));
$this->set('selectBlocks', $blocks);

if ($this->request->is('put') || $this->request->is('post')) {
//登録処理
$data = $this->data;

$data['TopicFramesRoom']['room_id'] = Hash::get($data, 'TopicFramesRoom.room_id');
if (! $data['TopicFramesRoom']['room_id']) {
$data['TopicFramesRoom']['room_id'] = array();
$data['TopicFramesRoom']['room_id'] = [];
if (! empty($this->data['TopicFramesRoom']['room_id'])) {
$data['TopicFramesRoom']['room_id'] = $this->data['TopicFramesRoom']['room_id'];
}

$data['TopicFramesPlugin']['plugin_key'] = Hash::get($data, 'TopicFramesPlugin.plugin_key');
if (! $data['TopicFramesPlugin']['plugin_key']) {
$data['TopicFramesPlugin']['plugin_key'] = array();
$data['TopicFramesPlugin']['plugin_key'] = [];
if (! empty($this->data['TopicFramesPlugin']['plugin_key'])) {
$data['TopicFramesPlugin']['plugin_key'] = $this->data['TopicFramesPlugin']['plugin_key'];
}

$data['TopicFramesBlock'] = Hash::get($data, 'TopicFramesBlock');
$data['TopicFramesBlock'] = isset($data['TopicFramesBlock'])
? $data['TopicFramesBlock']
: null;

if ($this->TopicFrameSetting->saveTopicFrameSetting($data)) {
$this->redirect(NetCommonsUrl::backToPageUrl(true));
Expand Down Expand Up @@ -139,15 +145,22 @@ public function edit() {
'conditions' => ['frame_key' => Current::read('Frame.key')],
));

$this->request->data['TopicFramesBlock'] = Hash::get($result, 'TopicFramesBlock', array());
if (! $this->request->data['TopicFramesBlock']) {
$block = Hash::extract($blocks, '{s}.{s}');
$this->request->data['TopicFramesBlock'] = array(
'block_key' => Hash::get($block, '0.key'),
'plugin_key' => Hash::get($block, '0.plugin_key'),
'room_id' => Hash::get($block, '0.room_id'),
);
if (isset($result['TopicFramesBlock'])) {
$topicFramesBlock = $result['TopicFramesBlock'];
} else {
$topicFramesBlock = [];
foreach ($blocks as $block) {
foreach ($block as $item) {
$topicFramesBlock = [
'block_key' => $item['key'],
'plugin_key' => $item['plugin_key'],
'room_id' => $item['room_id'],
];
break 2; // 1件目を取得してループを抜ける
}
}
}
$this->request->data['TopicFramesBlock'] = $topicFramesBlock;
}
}
}
49 changes: 29 additions & 20 deletions Controller/TopicsController.php
Expand Up @@ -74,14 +74,19 @@ public function beforeFilter() {
* index
*
* @return void
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function index() {
$topicFrameSetting = $this->viewVars['topicFrameSetting'];
$displayType = $this->viewVars['topicFrameSetting']['display_type'];

$conditions = array();

$days = Hash::get($this->params['named'], 'days');
$days = isset($this->params['named']['days'])
? $this->params['named']['days']
: null;
if ($days) {
$date = new DateTime();
$date->sub(new DateInterval('P' . $days . 'D'));
Expand All @@ -91,11 +96,10 @@ public function index() {

if ($displayType === TopicFrameSetting::DISPLAY_TYPE_ROOMS) {
//ルームごとに表示する
$roomId = Hash::get($this->request->query, 'room_id');
if ($roomId) {
$conditionsByRoom['Room.id'] = $roomId;
} else {
$conditionsByRoom = array();
$conditionsByRoom = [];
if (isset($this->request->query['room_id']) &&
! empty($this->request->query['room_id'])) {
$conditionsByRoom['Room.id'] = $this->request->query['room_id'];
}

$rooms = $this->TopicFramesRoom->getRooms(
Expand All @@ -117,11 +121,10 @@ public function index() {

} elseif ($displayType === TopicFrameSetting::DISPLAY_TYPE_PLUGIN) {
//プラグインごとに表示する
$pluginKey = Hash::get($this->request->query, 'plugin_key');
if ($pluginKey) {
$conditionsByPlugin['Plugin.key'] = $pluginKey;
} else {
$conditionsByPlugin = array();
$conditionsByPlugin = [];
if (isset($this->request->query['plugin_key']) &&
! empty($this->request->query['plugin_key'])) {
$conditionsByPlugin['Plugin.key'] = $this->request->query['plugin_key'];
}

$plugins = $this->TopicFramesPlugin->getPlugins(
Expand Down Expand Up @@ -164,28 +167,34 @@ public function index() {
*/
private function __getTopics($topicFrameSetting, $conditions) {
$options = $this->TopicFrameSetting->getQueryOptions($topicFrameSetting, $conditions);
$status = isset($this->params['named']['status'])
? $this->params['named']['status']
: 0;

$this->Paginator->settings = array(
'Topic' => $this->Topic->getQueryOptions(
Hash::get($this->params['named'], 'status', '0'), $options
),
'Topic' => $this->Topic->getQueryOptions($status, $options),
);

$maxPage = Hash::get($this->params['named'], 'page', '1');
$startPage = Hash::get($this->params['named'], 'startPage', $maxPage);
$maxPage = isset($this->params['named']['page'])
? $this->params['named']['page']
: '1';
$startPage = isset($this->params['named']['startPage'])
? $this->params['named']['startPage']
: $maxPage;
$topics = array();
for ($page = $startPage; $page <= $maxPage; $page++) {
$this->params['named'] = Hash::insert($this->params['named'], 'page', $page);
$result = $this->Paginator->paginate('Topic');
foreach ($result as $topic) {
unset($topic['Topic']['search_contents']);
$topics[] = $topic;
}
}
$topics = Hash::remove($topics, '{n}.Topic.search_contents');

$paging = $this->request['paging'];
$paging = Hash::remove($paging, 'Topic.order');
$paging = Hash::remove($paging, 'Topic.options');
$paging = Hash::remove($paging, 'Topic.paramType');
unset($paging['Topic']['order']);
unset($paging['Topic']['options']);
unset($paging['Topic']['paramType']);

return array('topics' => $topics, 'paging' => $paging['Topic']);
}
Expand Down
16 changes: 11 additions & 5 deletions Model/Behavior/TopicsBehavior.php
Expand Up @@ -116,9 +116,8 @@ public function saveTopics(Model $model) {
$this->_deleteTopicUserStatus($model);

//新着に表示させる会員のリスト登録
$topicIds = Hash::extract($model->data, $model->Topic->alias . '.{n}.id');
foreach ($topicIds as $topicId) {
$this->_saveTopicReadable($model, $topicId);
foreach ($model->data[$model->Topic->alias] as $topic) {
$this->_saveTopicReadable($model, $topic['id']);
}

return true;
Expand All @@ -129,6 +128,9 @@ public function saveTopics(Model $model) {
*
* @param Model $model 呼び出し元のモデル
* @return bool
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function beforeDeleteTopics(Model $model) {
$model->loadModels([
Expand All @@ -143,15 +145,19 @@ public function beforeDeleteTopics(Model $model) {
'fields' => array('id', 'key'),
'conditions' => array('id' => $model->id)
));
$model->contentKey = Hash::get($content, $model->alias . '.key');
$model->contentKey = isset($content[$model->alias]['key'])
? $content[$model->alias]['key']
: null;

} elseif ($model->blockId && ! $model->blockKey) {
$block = $model->Block->find('first', array(
'recursive' => -1,
'fields' => array('id', 'key'),
'conditions' => array('id' => $model->blockId)
));
$model->blockKey = Hash::get($block, $model->Block->alias . '.key');
$model->blockKey = isset($block[$model->Block->alias]['key'])
? $block[$model->Block->alias]['key']
: null;
}

//削除するトピックID取得
Expand Down
41 changes: 18 additions & 23 deletions Model/Topic.php
Expand Up @@ -343,7 +343,7 @@ public function __construct($id = false, $table = null, $ds = null) {
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = Hash::merge($this->validate, array(
$this->validate = array_merge($this->validate, array(
'language_id' => array(
'numeric' => array(
'rule' => array('numeric'),
Expand Down Expand Up @@ -564,19 +564,24 @@ private function __getRoomsConditions($now) {
);

//room_idの取得
$editableRoomIds = array_merge(
Hash::extract(
$rooms, '{n}.RolesRoom[role_key=' . Role::ROOM_ROLE_KEY_ROOM_ADMINISTRATOR . '].room_id'
),
Hash::extract(
$rooms, '{n}.RolesRoom[role_key=' . Role::ROOM_ROLE_KEY_CHIEF_EDITOR . '].room_id'
),
Hash::extract(
$rooms, '{n}.RolesRoom[role_key=' . Role::ROOM_ROLE_KEY_EDITOR . '].room_id'
)
);
$editableRoomIds = []; // 編集権限の条件生成
$readableRoomIds = []; // 閲覧権限の条件生成
$blockEditableRoomIds = []; //ブロック公開設定の条件生成
foreach ($rooms as $room) {
switch ($room['RolesRoom']['role_key']) {
case Role::ROOM_ROLE_KEY_ROOM_ADMINISTRATOR:
case Role::ROOM_ROLE_KEY_CHIEF_EDITOR:
$editableRoomIds[] = $room['RolesRoom']['room_id'];
$blockEditableRoomIds[] = $room['RolesRoom']['room_id'];
break;
case Role::ROOM_ROLE_KEY_EDITOR:
$editableRoomIds[] = $room['RolesRoom']['room_id'];
break;
}
$readableRoomIds[] = $room['Room']['id'];
}
$readableRoomIds = array_diff(
Hash::extract($rooms, '{n}.Room.id'), $editableRoomIds
$readableRoomIds, $editableRoomIds
);

$roomConditions = array();
Expand All @@ -592,16 +597,6 @@ private function __getRoomsConditions($now) {
//is_activeの条件生成
$roomConditions = $this->__getIsActiveConditions($now, $roomConditions, $readableRoomIds);

//ブロック公開設定の条件生成
$blockEditableRoomIds = array_merge(
Hash::extract(
$rooms, '{n}.RolesRoom[role_key=' . Role::ROOM_ROLE_KEY_ROOM_ADMINISTRATOR . '].room_id'
),
Hash::extract(
$rooms, '{n}.RolesRoom[role_key=' . Role::ROOM_ROLE_KEY_CHIEF_EDITOR . '].room_id'
)
);

$blockPubConditions['OR'] = array(
array($this->Block->alias . '.public_type' => self::TYPE_PUBLIC),
array(
Expand Down
2 changes: 1 addition & 1 deletion Model/TopicFrameSetting.php
Expand Up @@ -88,7 +88,7 @@ class TopicFrameSetting extends TopicsAppModel {
* @throws BadRequestException
*/
public function beforeValidate($options = array()) {
$this->validate = Hash::merge($this->validate, array(
$this->validate = array_merge($this->validate, array(
'frame_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
Expand Down
14 changes: 9 additions & 5 deletions Model/TopicFramesBlock.php
Expand Up @@ -55,7 +55,7 @@ class TopicFramesBlock extends TopicsAppModel {
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = Hash::merge($this->validate, array(
$this->validate = array_merge($this->validate, array(
'frame_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
Expand All @@ -80,7 +80,9 @@ public function beforeValidate($options = array()) {
public function validateRequestData($data) {
$blockKeys = Hash::extract($data, 'Block.{n}.key');

$check = Hash::get($data, 'TopicFrameSetting' . '.block_key', array());
$check = isset($data['TopicFrameSetting']['block_key'])
? $data['TopicFrameSetting']['block_key']
: [];
foreach ($check as $blockKey) {
if (! in_array($blockKey, $blockKeys, true)) {
return false;
Expand Down Expand Up @@ -122,8 +124,10 @@ public function getConditions($topicFrameSetting, $conditions) {
* @throws InternalErrorException
*/
public function saveTopicFramesBlock($data) {
$blockKey = Hash::get($data, $this->alias . '.block_key');
if ($blockKey && Hash::get($data, 'TopicFrameSetting.select_block')) {
$blockKey = isset($data[$this->alias]['block_key'])
? $data[$this->alias]['block_key']
: null;
if ($blockKey && $data['TopicFrameSetting']['select_block']) {
$conditions = array(
'TopicFramesBlock' . '.frame_key' => Current::read('Frame.key'),
'TopicFramesBlock' . '.block_key !=' => $blockKey,
Expand All @@ -137,7 +141,7 @@ public function saveTopicFramesBlock($data) {
'TopicFramesBlock' . '.block_key' => $blockKey,
);
if (! $this->find('count', ['recursive' => -1, 'conditions' => $conditions])) {
$saveData = $this->create(Hash::get($data, $this->alias));
$saveData = $this->create($data[$this->alias]);
if (! $this->save($saveData)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
Expand Down
24 changes: 18 additions & 6 deletions Model/TopicFramesPlugin.php
Expand Up @@ -36,7 +36,7 @@ class TopicFramesPlugin extends TopicsAppModel {
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = Hash::merge($this->validate, array(
$this->validate = array_merge($this->validate, array(
'frame_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
Expand All @@ -59,9 +59,16 @@ public function beforeValidate($options = array()) {
* @return bool
*/
public function validateRequestData($data) {
$pluginKeys = Hash::extract($data, 'Plugin.{n}.key');
$pluginKeys = [];
if (isset($data['Plugin'])) {
foreach ($data['Plugin'] as $plugin) {
$pluginKeys[] = $plugin['key'];
}
}

$check = Hash::get($data, 'TopicFrameSetting' . '.plugin_key', array());
$check = isset($data['TopicFramesPlugin']['plugin_key'])
? $data['TopicFramesPlugin']['plugin_key']
: [];
foreach ($check as $pluginKey) {
if (! in_array($pluginKey, $pluginKeys, true)) {
return false;
Expand All @@ -79,8 +86,8 @@ public function validateRequestData($data) {
* @return array 条件配列
*/
public function getConditions($topicFrameSetting, $conditions = []) {
if (Hash::get($conditions, 'Topic.plugin_key')) {
$conditions['Topic.plugin_key'] = Hash::get($conditions, 'Topic.plugin_key');
if (isset($conditions['Topic']['plugin_key'])) {
$conditions['Topic.plugin_key'] = $conditions['Topic']['plugin_key'];
} elseif ($topicFrameSetting['TopicFrameSetting']['select_plugin']) {
$pluginKeys = $this->find('list', array(
'recursive' => -1,
Expand Down Expand Up @@ -157,7 +164,12 @@ public function getPlugins($topicFrameSetting, $conditions = []) {
* @throws InternalErrorException
*/
public function saveTopicFramesPlugin($data) {
$pluginKeys = Hash::get($data, $this->alias . '.plugin_key', array());
$pluginKeys = [];
foreach ($data[$this->alias] as $frame) {
if (isset($frame['plugin_key'])) {
$pluginKeys[] = $frame['plugin_key'];
}
}

$saved = $this->find('list', array(
'recursive' => -1,
Expand Down

0 comments on commit 9f1b31d

Please sign in to comment.