Skip to content

Commit

Permalink
Merge pull request #195 from NetCommons3/issues/735
Browse files Browse the repository at this point in the history
Issues/735
  • Loading branch information
s-nakajima committed Oct 6, 2019
2 parents 4be3c16 + 400851f commit 15a1e98
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 104 deletions.
82 changes: 82 additions & 0 deletions Config/Migration/1570072256_add_rooms_delete_tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* ルーム削除時に不要データを削除するために使用するテーブルの追加
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('NetCommonsMigration', 'NetCommons.Config/Migration');

/**
* ルーム削除時に不要データを削除するために使用するテーブルの追加
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Config\Migration
*/
class AddRoomsDeleteTables extends NetCommonsMigration {

/**
* Migration description
*
* @var string
*/
public $description = 'add_delete_room_associations';

/**
* Actions to be performed
*
* @var array $migration
*/
public $migration = array(
'up' => array(
'create_table' => array(
'room_delete_related_tables' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'),
'room_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false, 'key' => 'index', 'comment' => 'ルームID'),
'delete_table_name' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '削除するテーブル名 rooms|blokcs|pages|users|frames|roles_rooms', 'charset' => 'utf8'),
'field_name' => array('type' => 'string', 'null' => false, 'default' => 'id', 'collate' => 'utf8_general_ci', 'comment' => 'カラム名', 'charset' => 'utf8'),
'value' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '値', 'charset' => 'utf8'),
'foreign_field_conditions' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 1200, 'collate' => 'utf8_general_ci', 'comment' => '対象とするカラムリストをJSON形式で保持 ex) {"id":["*.block_id"], "key":["*.block_key"]}', 'charset' => 'utf8'),
'start_time' => array('type' => 'datetime', 'null' => true, 'default' => null),
'created_user' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'modified_user' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'room_id' => array('column' => 'room_id', 'unique' => 0),
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB'),
),
),
),
'down' => array(
'drop_table' => array(
'room_delete_related_tables'
),
),
);

/**
* Before migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function before($direction) {
return true;
}

/**
* After migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function after($direction) {
return true;
}
}
24 changes: 24 additions & 0 deletions Config/Schema/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ public function after($event = array()) {
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);

/**
* room_delete_related_tables table
*
* @var array
*/
public $room_delete_related_tables = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'),
'room_id' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false, 'key' => 'index', 'comment' => 'ルームID'),
'delete_table_name' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '削除するテーブル名 rooms|blokcs|pages|users|frames|roles_rooms', 'charset' => 'utf8'),
'field_name' => array('type' => 'string', 'null' => false, 'default' => 'id', 'collate' => 'utf8_general_ci', 'comment' => 'カラム名', 'charset' => 'utf8'),
'value' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '値', 'charset' => 'utf8'),
'foreign_field_conditions' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 1200, 'collate' => 'utf8_general_ci', 'comment' => '対象とするカラムリストをJSON形式で保持 ex) {"id":["*.block_id"], "key":["*.block_key"]}', 'charset' => 'utf8'),
'start_time' => array('type' => 'datetime', 'null' => true, 'default' => null),
'created_user' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'modified_user' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'room_id' => array('column' => 'room_id', 'unique' => 0)
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);

/**
* rooms_languages table
*
Expand Down
40 changes: 19 additions & 21 deletions Model/Behavior/DeleteRoomAssociationsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
class DeleteRoomAssociationsBehavior extends ModelBehavior {

/**
* 外部キーにroom_idおよびrole_room_idがあるテーブルのデータ削除
* 外部キーにrole_room_idがあるテーブルのデータ削除
*
* @param Model $model ビヘイビア呼び出し元モデル
* @param int $roomId ルームID
* @return bool True on success
* @throws InternalErrorException
*/
public function deleteRoomAssociations(Model $model, $roomId) {
public function deleteRolesRoomByRoom(Model $model, $roomId) {
//外部キーがrole_room_idのデータを削除
$model->loadModels([
'RolesRoom' => 'Rooms.RolesRoom',
Expand All @@ -37,11 +38,14 @@ public function deleteRoomAssociations(Model $model, $roomId) {
$model->RolesRoom->alias . '.room_id' => $roomId,
),
));

$rolesRoomIds = array_keys($rolesRoomIds);
$this->queryDeleteAll($model, 'roles_room_id', $rolesRoomIds);

//外部キーがroom_idのデータを削除
$this->queryDeleteAll($model, 'room_id', $roomId);
if (!$model->RolesRoom->deleteAll([$model->RolesRoom->alias . '.id' => $rolesRoomIds], false)) {
CakeLog::error('[room deleting] RolesRoom->deleteAll ' . implode(', ', $rolesRoomIds));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

return true;
}
Expand Down Expand Up @@ -70,13 +74,11 @@ public function deletePagesByRoom(Model $model, $roomId) {
$pageIds = array_keys($pageIds);

//Tree構成の関係で、ページの削除はdeleteAllでやる
CakeLog::info('[room deleting] Page->deleteAll ' . implode(', ', $pageIds));
if (!$model->Page->deleteAll(array($model->Page->alias . '.id' => $pageIds), false)) {
CakeLog::error('[room deleting] Page->deleteAll ' . implode(', ', $pageIds));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

$this->queryDeleteAll($model, 'page_id', $pageIds);

return true;
}

Expand All @@ -86,6 +88,7 @@ public function deletePagesByRoom(Model $model, $roomId) {
* @param Model $model ビヘイビア呼び出し元モデル
* @param int $roomId ルームID
* @return bool True on success
* @throws InternalErrorException
*/
public function deleteFramesByRoom(Model $model, $roomId) {
$model->loadModels([
Expand All @@ -100,13 +103,10 @@ public function deleteFramesByRoom(Model $model, $roomId) {
),
));
$frameIds = array_keys($frames);
$frameKeys = array_values($frames);

//外部キーがframe_idのデータを削除
$this->queryDeleteAll($model, 'frame_id', $frameIds);

//外部キーがframe_keyのデータを削除
$this->queryDeleteAll($model, 'frame_key', array_unique($frameKeys));
if (!$model->Frame->deleteAll(array($model->Frame->alias . '.id' => $frameIds), false)) {
CakeLog::error('[room deleting] Frame->deleteAll ' . implode(', ', $frameIds));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

return true;
}
Expand All @@ -117,6 +117,7 @@ public function deleteFramesByRoom(Model $model, $roomId) {
* @param Model $model ビヘイビア呼び出し元モデル
* @param int $roomId ルームID
* @return bool True on success
* @throws InternalErrorException
*/
public function deleteBlocksByRoom(Model $model, $roomId) {
$model->loadModels([
Expand All @@ -131,13 +132,10 @@ public function deleteBlocksByRoom(Model $model, $roomId) {
),
));
$blockIds = array_keys($blocks);
$blockKeys = array_values($blocks);

//外部キーがblock_idのデータを削除
$this->queryDeleteAll($model, 'block_id', $blockIds);

//外部キーがblock_keyのデータを削除
$this->queryDeleteAll($model, 'block_key', array_unique($blockKeys));
if (!$model->Block->deleteAll(array($model->Block->alias . '.id' => $blockIds), false)) {
CakeLog::error('[room deleting] Block->deleteAll ' . implode(', ', $blockIds));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

return true;
}
Expand Down
31 changes: 20 additions & 11 deletions Model/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
/**
* Room Model
*
* @property Space $Space
* @property Room $ParentRoom
* @property Room $ChildRoom
* @property Language $Language
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
Expand All @@ -22,6 +17,12 @@
/**
* Room Model
*
* @property Space $Space
* @property Room $ParentRoom
* @property Room $ChildRoom
* @property Language $Language
* @property RoomDeleteRelatedTable $RoomDeleteRelatedTable
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Model
*
Expand Down Expand Up @@ -422,12 +423,25 @@ public function afterSave($created, $options = array()) {
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function beforeDelete($cascade = true) {
$this->loadModels([
'RoomDeleteRelatedTable' => 'Rooms.RoomDeleteRelatedTable',
]);

$children = $this->children($this->id, false, 'Room.id', 'Room.rght');
$this->_childRoomIds = Hash::extract($children, '{n}.Room.id');
$this->_childRoomIds = [];
foreach ($children as $room) {
$this->_childRoomIds[] = $room['Room']['id'];
}
$deleteRoomIds = $this->_childRoomIds;
$deleteRoomIds[] = $this->id;

foreach ($deleteRoomIds as $childRoomId) {
//ルーム削除情報を登録する
$this->RoomDeleteRelatedTable->insertByRoomId($childRoomId);

//roles_roomsデータ削除
$this->deleteRolesRoomByRoom($childRoomId);

//frameデータの削除
$this->deleteFramesByRoom($childRoomId);

Expand Down Expand Up @@ -456,11 +470,6 @@ public function afterDelete() {
if (! $this->deleteAll(array($this->alias . '.id' => $this->_childRoomIds), false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

//Roomの関連データの削除
foreach ($deleteRoomIds as $childRoomId) {
$this->deleteRoomAssociations($childRoomId);
}
}

/**
Expand Down

0 comments on commit 15a1e98

Please sign in to comment.