/** * Action delete */ public function action_delete() { // Auto render off $this->auto_render = FALSE; // Get id from param, if there is nothing then throw to 404 $id = $this->request->param('key'); if (!$id) { throw HTTP_Exception::factory(404); } // Get division, if there is nothing then throw to 404 $division = Tbl::factory('divisions')->get($id); if (!$division) { throw HTTP_Exception::factory(404); } // Database transaction start Database::instance()->begin(); // Try try { /** * Check other tables */ // used by items $used_items = (bool) Tbl::factory('items')->where('division_id', '=', $division->id)->read()->count(); // used by categories $used_categories = (bool) Tbl::factory('categories')->where('division_id', '=', $division->id)->read()->count(); // used by fields $used_fields = (bool) Tbl::factory('fields')->where('division_id', '=', $division->id)->read()->count(); // Build tables array $tables = array(); if ($used_items) { $tables[] = 'items'; } if ($used_categories) { $tables[] = 'categories'; } if ($used_fields) { $tables[] = 'fields'; } // If this division is used when throw to warning if ($used_items or $used_categories or $used_fields) { throw new Warning_Exception(Kohana::message('general', 'division_is_used'), array(':tables' => implode(', ', $tables))); } /** * Delete */ // Delete file まずファイルを消す! $file_delete_success = Cms_Helper::delete_file($division->segment, $this->settings->front_tpl_dir . '/division'); if ($file_delete_success) { Cms_Helper::delete_dir($division->segment, $this->settings->item_dir); Cms_Helper::delete_dir($division->segment, $this->settings->image_dir . '/item'); } // Delete $division->delete(); // Database commit Database::instance()->commit(); // Add success notice Notice::add(Notice::SUCCESS, Kohana::message('general', 'delete_success')); $this->redirect(URL::site("{$this->settings->backend_name}/divisions/index", 'http')); } catch (HTTP_Exception_302 $e) { $this->redirect($e->location()); } catch (Validation_Exception $e) { // Database rollback Database::instance()->rollback(); // Add validation notice Notice::add(Notice::VALIDATION, Kohana::message('general', 'delete_failed'), NULL, $e->errors('validation')); } catch (Warning_Exception $e) { // Database rollback Database::instance()->rollback(); // Add Notice::add(Notice::WARNING, $e->getMessage()); } catch (Exception $e) { // Database rollback Database::instance()->rollback(); // Add error notice Notice::add(Notice::ERROR, $e->getMessage()); } // Redirect to wrapper edit $this->redirect(URL::site("{$this->settings->backend_name}/divisions/edit/{$division->id}", 'http')); }
/** * Action delete */ public function action_delete() { // Auto render off $this->auto_render = FALSE; // Get id from param, if there is nothing then throw to 404 $id = $this->request->param('key'); if (!$id) { throw HTTP_Exception::factory(404); } // Get tag, if there is nothing then throw to 404 $user = Tbl::factory('users')->get($id); if (!$user) { throw HTTP_Exception::factory(404); } /** * Delete */ // Database transaction start Database::instance()->begin(); // Try try { // Delete roles_users $roles_users_ids = Tbl::factory('roles_users')->where('user_id', '=', $user->id)->read()->as_array(NULL, 'id'); if ($roles_users_ids) { foreach ($roles_users_ids as $roles_users_id) { Tbl::factory('roles_users')->get($roles_users_id)->delete(); } } // Delate users_details $users_details_ids = Tbl::factory('users_details')->where('user_id', '=', $user->id)->read()->as_array(NULL, 'id'); if ($users_details_ids) { foreach ($users_details_ids as $users_details_id) { Tbl::factory('users_details')->get($users_details_id)->delete(); } } // Delete $user->delete(); // Delete image user dir Cms_Helper::delete_dir($user->username, $this->settings->image_dir . '/user', TRUE); // Database commit Database::instance()->commit(); // Add success notice Notice::add(Notice::SUCCESS, Kohana::message('general', 'delete_success')); } catch (HTTP_Exception_302 $e) { $this->redirect($e->location()); } catch (Validation_Exception $e) { // Database rollback Database::instance()->rollback(); // Add validation notice Notice::add(Notice::VALIDATION, Kohana::message('general', 'delete_failed'), NULL, $e->errors('validation')); } catch (Exception $e) { // Database rollback Database::instance()->rollback(); // Add error notice Notice::add(Notice::ERROR, $e->getMessage()); } // Redirect to wrapper edit $this->redirect(URL::site("{$this->settings->backend_name}/users", 'http')); }
/** * Action delete */ public function action_delete() { // Auto render off $this->auto_render = FALSE; // Get division $division = Tbl::factory('divisions')->where('id', '=', $this->item->division_id)->read(1); // Database transaction start Database::instance()->begin(); // Try try { /** * Delete */ // Delete items categories カテゴリーの削除 $used_category_ids = Tbl::factory('items_categories')->where('item_id', '=', $this->item->id)->read()->as_array(NULL, 'id'); if ($used_category_ids) { foreach ($used_category_ids as $used_category_id) { Tbl::factory('items_categories')->get($used_category_id)->delete(); } } // Delete items fields フィールドの削除 $used_field_ids = Tbl::factory('items_fields')->where('item_id', '=', $this->item->id)->read()->as_array(NULL, 'id'); if ($used_field_ids) { foreach ($used_field_ids as $used_field_id) { Tbl::factory('items_fields')->get($used_field_id)->delete(); } } // Delete items tags タグの削除 $used_tag_ids = Tbl::factory('items_tags')->where('item_id', '=', $this->item->id)->read()->as_array(NULL, 'id'); if ($used_tag_ids) { foreach ($used_tag_ids as $used_tag_id) { Tbl::factory('items_tags')->get($used_tag_id)->delete(); } } // Delete item comments コメントの削除 $used_comment_ids = Tbl::factory('received_comments')->where('item_id', '=', $this->item->id)->read()->as_array(NULL, 'id'); if ($used_comment_ids) { foreach ($used_comment_ids as $used_comment_id) { Tbl::factory('received_comments')->get($used_comment_id)->delete(); } } // Delete images イメージの削除 $used_image_ids = Tbl::factory('images')->where('item_id', '=', $this->item->id)->read()->as_array(NULL, 'id'); if ($used_image_ids) { foreach ($used_image_ids as $used_image_id) { Tbl::factory('images')->get($used_image_id)->delete(); } } // Delete image files and directory イメージファイルとディレクトリの削除 Cms_Helper::delete_dir($this->item->segment, $this->settings->image_dir . '/item/' . $division->segment, TRUE); // Delete file Cms_Helper::delete_file($this->item->segment, $this->settings->item_dir . '/' . $division->segment); // Delete $this->item->delete(); // Database commit Database::instance()->commit(); // Add success notice Notice::add(Notice::SUCCESS, Kohana::message('general', 'delete_success')); } catch (HTTP_Exception_302 $e) { $this->redirect($e->location()); } catch (Validation_Exception $e) { // Database rollback Database::instance()->rollback(); // Add validation notice Notice::add(Notice::VALIDATION, Kohana::message('general', 'delete_failed'), NULL, $e->errors('validation')); } catch (Exception $e) { // Database rollback Database::instance()->rollback(); // Add error notice Notice::add(Notice::ERROR); } /** * Redirect to wrapper edit */ $this->redirect(URL::site("{$this->settings->backend_name}/items/{$division->segment}", 'http')); }