diff --git a/winixd/core/dirs.cpp b/winixd/core/dirs.cpp index 440a2dd..a97e5ae 100644 --- a/winixd/core/dirs.cpp +++ b/winixd/core/dirs.cpp @@ -31,7 +31,8 @@ * POSSIBILITY OF SUCH DAMAGE. * */ - + +#include #include "dirs.h" #include "error.h" #include "notify/notify.h" @@ -90,8 +91,9 @@ void Dirs::CheckRootDir() { i->item_content.privileges = 07555; log << log1 << "Dirs: there is no access for a root (admin) to the root dir, setting 07555 for the root directory" << logend; - i->update(false, false); - //db->EditPrivById(*i, i->id); + i->item_content.set_connector(model_connector); + i->item_content.date_modification = std::time(nullptr); + i->item_content.save(false); } return; @@ -99,22 +101,20 @@ void Dirs::CheckRootDir() log << log1 << "Dirs: there is no a root directory in the database (creating one)" << logend; + ItemModelData item_data; + item_data.prepare_unique_url = false; + Item root; root.set_connector(model_connector); - root.type = Item::dir; root.parent_id = -1; root.item_content.user_id = -1; root.item_content.group_id = -1; root.item_content.privileges = 07555; + root.item_content.date_creation = std::time(nullptr); + root.item_content.date_modification = root.item_content.date_creation; - // !! upewnic sie ze baza nie zmieni url (gdyby wczesniej juz byl w bazie pusty url) - // !! zrobic jakis wyjatek do wprowadzania roota? -// if( db->AddItem(root) == WINIX_ERR_OK ) -// { -// dir_tab.PushBack(root); -// } - if( root.insert() ) + if( root.insert(item_data) ) { dir_tab.PushBack(root); } diff --git a/winixd/core/image.cpp b/winixd/core/image.cpp index 0e191d1..b7813ea 100644 --- a/winixd/core/image.cpp +++ b/winixd/core/image.cpp @@ -498,7 +498,8 @@ void Image::ImageSavedCorrectly() { // IMPROVEME we need a separate model_connector for this thread Lock(); - file_work.update(false, false); + file_work.propagate_connector(); + file_work.item_content.update(false); //db->EditHasThumbById(true, file_work.id); } } diff --git a/winixd/core/system.cpp b/winixd/core/system.cpp index 5ca4a0d..4a96539 100644 --- a/winixd/core/system.cpp +++ b/winixd/core/system.cpp @@ -1073,7 +1073,10 @@ bool System::EditFile(Item & item, bool with_url, int notify_code, bool call_plu item.item_content.SetDateModifyToNow(); - bool status = item.update(with_url, true); + ItemModelData item_model_data; + item_model_data.prepare_unique_url = with_url; + + bool status = item.update(item_model_data); //Error status = db->EditItemById(item, with_url); if( status ) diff --git a/winixd/functions/cp.cpp b/winixd/functions/cp.cpp index 975480e..2a46aed 100644 --- a/winixd/functions/cp.cpp +++ b/winixd/functions/cp.cpp @@ -152,12 +152,13 @@ bool ok = true; if( CopyStaticFile(old_path, new_path) ) { //cur->request->status = db->EditFileById(item, item.id); - if( !item.update(false, true) ) + item.propagate_connector(); + + if( !item.item_content.update() ) { cur->request->status = WINIX_ERR_PERMISSION_DENIED; } - if( item.item_content.file_has_thumb ) CopyStaticFile(old_path_thumb, new_path_thumb); } diff --git a/winixd/functions/default.cpp b/winixd/functions/default.cpp index 450936d..bfdf658 100644 --- a/winixd/functions/default.cpp +++ b/winixd/functions/default.cpp @@ -58,6 +58,7 @@ bool Default::HasAccess() void Default::MakePost() { Item & dir = *cur->request->dir_tab.back(); + dir.propagate_connector(); dir.item_content.link_to = cur->request->PostVar(L"linkto"); dir.item_content.link_redirect = cur->request->IsPostVar(L"makeredirect") ? 1 : 0; @@ -66,7 +67,7 @@ void Default::MakePost() // !! IMPROVEME dodac sprawdzenie czy link_to jest pusty teraz //cur->request->status = db->EditLinkItem(dir.id, dir.link_to, dir.link_redirect); - if( dir.update(false, true) ) + if( dir.item_content.update() ) { log << log2 << "Default: changed link_to: " << dir.item_content.link_to << ", for dir_id: " << dir.id << logend; system->RedirectToLastDir(); diff --git a/winixd/functions/meta.cpp b/winixd/functions/meta.cpp index 937f79f..5b54ab2 100644 --- a/winixd/functions/meta.cpp +++ b/winixd/functions/meta.cpp @@ -75,10 +75,11 @@ bool Meta::EditAdminMeta(Item & item, const std::wstring & meta_str, bool use_se { if( Parse(meta_str) ) { + item.propagate_connector(); item.item_content.meta_admin = space; //if( db->EditAdminMetaById(space, item_id) == WINIX_ERR_OK ) - if( item.update(false, true) ) + if( item.item_content.update() ) { return true; } @@ -105,10 +106,11 @@ bool Meta::EditMeta(Item & item, const std::wstring & meta_str, bool use_ses_log { if( Parse(meta_str) ) { + item.propagate_connector(); item.item_content.meta = space; //if( db->EditMetaById(space, item_id) == WINIX_ERR_OK ) - if( item.update(false, true) ) + if( item.item_content.update() ) { return true; } diff --git a/winixd/functions/mkdir.cpp b/winixd/functions/mkdir.cpp index 69bb905..35e9204 100644 --- a/winixd/functions/mkdir.cpp +++ b/winixd/functions/mkdir.cpp @@ -90,6 +90,7 @@ return true; void Mkdir::PostFunMkdir(bool add_to_dir_tab, int privileges) { + cur->request->item.set_connector(model_connector); functions->ReadItem(cur->request->item, Item::dir); functions->SetUser(cur->request->item); cur->request->item.item_content.privileges = privileges; diff --git a/winixd/functions/mv.cpp b/winixd/functions/mv.cpp index 1273fed..8eff3b9 100644 --- a/winixd/functions/mv.cpp +++ b/winixd/functions/mv.cpp @@ -283,8 +283,10 @@ bool ok = true; if( MoveStaticFile(old_static_path, new_static_path) ) { + item.propagate_connector(); + //if( db->EditFileById(item, item.id) != WINIX_ERR_OK ) - if( !item.update(false, true) ) + if( !item.item_content.update() ) { log << log1 << "Mv: cannot move static file (database problem)" << logend; slog << logerror << T("internal_error") << logend; @@ -401,7 +403,7 @@ bool Mv::MoveDir(Item & src_dir, std::vector & dst_dir_tab, const std::ws } //Error status = db->EditParentUrlById(src_dir, src_dir.id); - if( src_dir.update(true, false) ) + if( src_dir.update(false) ) { log << log3 << "Mv: directory: " << old_url << " was moved to: "; system->dirs.LogDir(dst_dir_tab); @@ -530,7 +532,7 @@ bool Mv::MoveFileOrSymlink(Item & src_file, std::vector & dst_dir_tab, co src_file.parent_id = dst_dir_tab.back()->id; //Error status = db->EditParentUrlById(src_file, src_file.id); - if( src_file.update(true, false) ) + if( src_file.update(false) ) { if( src_file.type == Item::file ) log << log3 << "Mv: file: "; diff --git a/winixd/functions/privchanger.cpp b/winixd/functions/privchanger.cpp index 0bce5c9..58f255e 100644 --- a/winixd/functions/privchanger.cpp +++ b/winixd/functions/privchanger.cpp @@ -133,8 +133,10 @@ void PrivChanger::ChangePriv(Item & item, long user_id, long group_id, int privi return; } + item.propagate_connector(); + //cur->request->status = db->EditPrivById(item, item.id); - if( !item.update(false, true) ) + if( !item.item_content.update() ) { // IMPROVEME put some log here, and slog too } diff --git a/winixd/functions/sort.cpp b/winixd/functions/sort.cpp index 9caef7d..0e4dd5c 100644 --- a/winixd/functions/sort.cpp +++ b/winixd/functions/sort.cpp @@ -136,8 +136,11 @@ void Sort::UpdateSortIndex(Item & item, int sort_index) { item.sort_index = sort_index; + ItemModelData item_model_data; + item_model_data.prepare_unique_url = false; + //if( db->EditSortIndexItemById(item.id, sort_index) == WINIX_ERR_OK ) - if( item.update(false, false) ) + if( item.update(item_model_data, false) ) { log << log2 << "Sort: updated sort index, item_id=" << item.id diff --git a/winixd/functions/subject.cpp b/winixd/functions/subject.cpp index 437a47f..44b8b3e 100644 --- a/winixd/functions/subject.cpp +++ b/winixd/functions/subject.cpp @@ -76,8 +76,12 @@ void Subject::EditDirSubject() Item & dir = *cur->request->dir_tab.back(); cur->request->PostVar(L"subject", dir.subject); - dir.update(false, false); + + ItemModelData item_model_data; + item_model_data.prepare_unique_url = false; + //db->EditSubjectById(dir, dir.id); + dir.update(item_model_data, false); // !! IMPROVE ME // we need something like WINIX_DIR_CHANGED message @@ -91,7 +95,11 @@ void Subject::EditDirSubject() void Subject::EditFileSubject() { cur->request->PostVar(L"subject", cur->request->item.subject); - cur->request->item.update(false, false); + + ItemModelData item_model_data; + item_model_data.prepare_unique_url = false; + + cur->request->item.update(item_model_data, false); //db->EditSubjectById(cur->request->item, cur->request->item.id); plugin->Call(WINIX_FILE_CHANGED, &cur->request->item); diff --git a/winixd/functions/template.cpp b/winixd/functions/template.cpp index efd44fa..5f99460 100644 --- a/winixd/functions/template.cpp +++ b/winixd/functions/template.cpp @@ -124,8 +124,11 @@ void Template::ChangeTemplate(Item & item) { item.html_template = html_template; + ItemModelData item_model_data; + item_model_data.prepare_unique_url = false; + //cur->request->status = db->EditTemplateItemById(item.id, html_template); - if( item.update(false, false) ) + if( item.update(item_model_data, false) ) { PutLog(item); } diff --git a/winixd/functions/upload.cpp b/winixd/functions/upload.cpp index 269ce85..8a0a188 100644 --- a/winixd/functions/upload.cpp +++ b/winixd/functions/upload.cpp @@ -154,8 +154,11 @@ void Upload::UploadFile(Item & item, const std::wstring & tmp_filename) { if( UploadSaveStaticFile(item, tmp_filename) ) { + ItemModelData item_model_data; + item_model_data.prepare_unique_url = false; + //cur->request->status = db->EditFileById(item, item.id); - if( item.update(false, true) ) + if( item.update(item_model_data) ) { plugin->Call(WINIX_FILE_ADDED, &item); diff --git a/winixd/models/item.cpp b/winixd/models/item.cpp index 0adf8fc..5c848c3 100644 --- a/winixd/models/item.cpp +++ b/winixd/models/item.cpp @@ -52,6 +52,8 @@ Item::Item() void Item::map_fields() { + //ItemModelData * item_model_data = dynamic_cast(get_model_data()); + int type_helper = static_cast(type); field(L"id", id, false, false, true); @@ -87,7 +89,14 @@ void Item::after_insert() bool Item::insert(morm::ModelData * model_data, bool update_whole_tree) { - bool url_prepared_correctly = prepare_url(); + ItemModelData * item_model_data = dynamic_cast(model_data); + bool url_prepared_correctly = true; + + if( !item_model_data || item_model_data->prepare_unique_url ) + { + url_prepared_correctly = prepare_url(); + } + bool insert_status = morm::Model::insert(model_data, update_whole_tree); if( insert_status ) @@ -95,7 +104,7 @@ bool Item::insert(morm::ModelData * model_data, bool update_whole_tree) if( !url_prepared_correctly ) { PT::Toa(id, url); - morm::Model::update(model_data, false); + insert_status = morm::Model::update(model_data, false); } } @@ -103,36 +112,23 @@ bool Item::insert(morm::ModelData * model_data, bool update_whole_tree) } -bool Item::insert(morm::ModelData & model_data, bool update_whole_tree) -{ - return insert(&model_data, update_whole_tree); -} - - -bool Item::insert(bool update_whole_tree) -{ - return insert(nullptr, update_whole_tree); -} - - - -/* - * - * this can be done better - * instead of calculating if more than zero items are in a directory - * just get the one item (should be one in normal circumstances) - * and compare its 'id' with 'this.id' and if these ids are different - * then prepare a new url - * - * - */ -bool Item::update(morm::ModelData * model_data, bool url_was_changed, bool update_whole_tree) +bool Item::update(morm::ModelData * model_data, bool update_whole_tree) { + ItemModelData * item_model_data = dynamic_cast(model_data); bool url_prepared_correctly = true; - if( url_was_changed ) + if( !item_model_data || item_model_data->prepare_unique_url ) { - url_prepared_correctly = prepare_url(); + CalcItemsHelper helper = calc_items_by_url(parent_id, url); + + if( helper.size == 1 && helper.item_id == id ) + { + // the same item + } + else + { + url_prepared_correctly = prepare_url(); + } } bool update_status = morm::Model::update(model_data, update_whole_tree); @@ -142,7 +138,7 @@ bool Item::update(morm::ModelData * model_data, bool url_was_changed, bool updat if( !url_prepared_correctly ) { PT::Toa(id, url); - morm::Model::update(model_data, false); + update_status = morm::Model::update(model_data, false); } } @@ -150,54 +146,6 @@ bool Item::update(morm::ModelData * model_data, bool url_was_changed, bool updat } -bool Item::update(morm::ModelData & model_data, bool url_was_changed, bool update_whole_tree) -{ - return update(&model_data, url_was_changed, update_whole_tree); -} - - -bool Item::update(bool url_was_changed, bool update_whole_tree) -{ - return update(nullptr, url_was_changed, update_whole_tree); -} - - - -bool Item::save(morm::ModelData * model_data, bool url_was_changed, bool save_whole_tree) -{ - bool url_prepared_correctly = true; - - if( save_mode == DO_INSERT_ON_SAVE || (save_mode == DO_UPDATE_ON_SAVE && url_was_changed) ) - { - url_prepared_correctly = prepare_url(); - } - - bool save_status = morm::Model::save(model_data, save_whole_tree); - - if( save_status ) - { - if( !url_prepared_correctly ) - { - PT::Toa(id, url); - morm::Model::save(model_data, false); - } - } - - return save_status; -} - -bool Item::save(morm::ModelData & model_data, bool url_was_changed, bool save_whole_tree) -{ - return save(&model_data, url_was_changed, save_whole_tree); -} - - -bool Item::save(bool url_was_changed, bool save_whole_tree) -{ - return save(nullptr, url_was_changed, save_whole_tree); -} - - void Item::Clear() @@ -213,19 +161,19 @@ void Item::Clear() } -long Item::calc_items_by_url(long parent_id, const std::wstring & url) +CalcItemsHelper Item::calc_items_by_url(long parent_id, const std::wstring & url) { morm::Finder finder(model_connector); CalcItemsHelper helper = finder. prepare_to_select(). - raw("select count(id) as size from core.item"). + raw("select count(id) as size, min(id) as item_id from core.item"). where(). eq(L"parent_id", parent_id). eq(L"url", url). get(); - return helper.size; + return helper; } @@ -236,8 +184,8 @@ bool Item::prepare_url() { std::wstring temp_url; bool is_that_url; -int index = 1; const int max_index = 99; +size_t index = 1; std::wstring postfix; // only root dir may not have the url @@ -255,9 +203,9 @@ std::wstring postfix; PrepareNewFileName(url, postfix, temp_url); - long size = calc_items_by_url(parent_id, temp_url); + CalcItemsHelper helper = calc_items_by_url(parent_id, temp_url); - if( size > 0 ) + if( helper.size > 0 ) { is_that_url = true; } @@ -292,6 +240,10 @@ void Item::do_migration(morm::ModelConnector * model_connector) } +void Item::propagate_connector() +{ + item_content.set_connector(model_connector); +} } // namespace Winix diff --git a/winixd/models/item.h b/winixd/models/item.h index 471f087..6dd81d7 100644 --- a/winixd/models/item.h +++ b/winixd/models/item.h @@ -50,20 +50,31 @@ public: long size; - /* - * add an id of an item and compare it in 'update' method - * (in a case size==1) - * - */ + long item_id; void map_fields() { field(L"size", size); + field(L"item_id", item_id); } }; +// IMPROVEME rename me and move to a better place +class ItemModelData : public morm::ModelData +{ +public: + + ItemModelData() + { + prepare_unique_url = true; + } + + bool prepare_unique_url; +}; + + class Item : public morm::Model { public: @@ -139,18 +150,11 @@ public: bool prepare_url(); + using morm::Model::insert; + using morm::Model::update; bool insert(morm::ModelData * model_data, bool update_whole_tree = true); - bool insert(morm::ModelData & model_data, bool update_whole_tree = true); - bool insert(bool update_whole_tree = true); - - bool update(morm::ModelData * model_data, bool url_was_changed, bool update_whole_tree = true); - bool update(morm::ModelData & model_data, bool url_was_changed, bool update_whole_tree = true); - bool update(bool url_was_changed, bool update_whole_tree = true); - - bool save(morm::ModelData * model_data, bool url_was_changed, bool save_whole_tree = true); - bool save(morm::ModelData & model_data, bool url_was_changed, bool save_whole_tree = true); - bool save(bool url_was_changed, bool save_whole_tree = true); + bool update(morm::ModelData * model_data, bool update_whole_tree = true); /* @@ -166,9 +170,11 @@ public: */ static void do_migration(morm::ModelConnector * model_connector); + void propagate_connector(); + protected: - long calc_items_by_url(long parent_id, const std::wstring & url); + CalcItemsHelper calc_items_by_url(long parent_id, const std::wstring & url); }; diff --git a/winixd/plugins/seo/seo.cpp b/winixd/plugins/seo/seo.cpp index e74676d..3a4aef7 100644 --- a/winixd/plugins/seo/seo.cpp +++ b/winixd/plugins/seo/seo.cpp @@ -81,7 +81,10 @@ void Seo::MakePost() seo.Add(L"description", description); seo.Add(L"keywords", keywords); - item.update(false, true); + ItemModelData item_model_data; + item_model_data.prepare_unique_url = false; + + item.update(item_model_data); //db->EditSubjectById(item, item.id); //db->EditMetaById(item.meta, item.id); diff --git a/winixd/plugins/ticket/editticket.cpp b/winixd/plugins/ticket/editticket.cpp index 6ff5f74..5ab0ae4 100644 --- a/winixd/plugins/ticket/editticket.cpp +++ b/winixd/plugins/ticket/editticket.cpp @@ -198,6 +198,8 @@ return new_space; bool EditTicket::CloseTicket() { + cur->request->item.propagate_connector(); + PT::Space & ticket_space = cur->request->item.item_content.meta_admin.FindAddSpace(L"ticket"); ticket_space.Add(L"closed", true); @@ -205,7 +207,7 @@ bool EditTicket::CloseTicket() thread_space.Add(L"closed", true); //if( db->EditAdminMetaById(cur->request->item.ameta, cur->request->item.id) == WINIX_ERR_OK ) - if( cur->request->item.update(false, true) ) + if( cur->request->item.item_content.update() ) { log << log3 << "EditTicket: closing ticket" << logend; return true;