Example #1
0
void Articles::translate_treat() {
    TREAT_PAGE();
    CHECK_PERMISSION_OR_GO_TO_LOGIN();

    forms::articles::Translate form;
    form.load(context());
    if (!form.validate()) {
        set_message(_("The form you've submitted is not valid"));
        go_back_to_previous_page();
    }

    
    const std::string origLang = get_interface_lang();
    const std::string origSlug = form.slug.value();
    const std::string translationSlug = form.translationSlug.value();
    const std::string translationLang = form.transLang.selected_id();
    const std::string title = form.title.value();
    const std::string content = form.content.value();
    std::string summary = form.summary.value();
    
    if (summary.empty()) {
        summary = "translated from " + origLang + ":" + origSlug;
    }

    int resultCode = articlesModel->translate_from_lang_and_slug(
       origLang,
       origSlug,
       translationLang,
       translationSlug,
       title,
       content
    );

    // TODO add something to say that the article
    // as been created (and not simply that a "version"
    // has been added
    // TODO also add something to keep trace of the
    //      translation link in the history 
    if (resultCode <= 0) {
        if (resultCode == ARTICLE_ALREADY_TRANSLATED_ERROR) {
            set_message(_("This article has already a translation in that language."));
        } else if (resultCode == ARTICLE_SAME_TRANSLATION_LANGUAGE_ERROR) {
            set_message(_("You can't translate an article in the same language."));
        } else {
            set_message(_("Error while trying to translate."));
        }
        go_back_to_previous_page();
        return;
    }
    
    // simply here for readability 
    int translationId = resultCode;
    
    results::Article translationArticle(
        translationId,
        translationLang,
        translationSlug,
        title,
        content
    );

    historyModel->add_version(
        translationArticle,
        get_current_user_id(),
        summary
    );
    // we redirect to the article we've just added
    redirect(
        "http://" +
        translationLang +
        "." +
        Config::get_base_host() +
        "/articles/show/" + translationSlug
    );

}
Example #2
0
void Articles::edit_treat() {
    TREAT_PAGE();
    CHECK_PERMISSION_OR_GO_TO_LOGIN();

    forms::articles::Edit form;
    form.load(context());

    if (!form.validate()) {
        //TODO add a more precise message
        set_message(_("The form is not valid."));
        go_back_to_previous_page();
        return;
    }

    //TODO check if the last_version_id the form send us
    // is different from what we have in database
    // if so , error message and redirect 
    const int lastVersionId = std::stoi(form.lastVersion.value());
    
    const std::string lang = get_interface_lang();
    const std::string slug = form.slug.value();
    const std::string title = form.title.value();
    const std::string content = form.content.value();
    const std::string summary = form.summary.value();

    // TODO maybe replace this by storing the id in an hidden 
    int articleId = articlesModel->get_id_from_lang_and_slug(
        lang,
        slug
    );
 
    results::Article article(
        articleId,
        lang,
        slug,
        title,
        content
    );
 
 
    if (lastVersionId != historyModel->get_last_version_id_of(articleId)) {
        set_message(_(
            "Error, someone has edited the article while you"
            "were also editing it"
        ));
        int conflictId = articlesModel->save_conflict(article);
        redirect(
            "/articles/show-conflict/" + std::to_string(conflictId)
        );
        return;
    }

    articlesModel->edit_from_lang_and_slug(
        lang,
        slug,
        title,
        content
    );
       

    historyModel->add_version(
        article,
        get_current_user_id(),
        summary
    );

    // we invalidate the cache for this article
    cache().rise(lang+slug);

    // we show the edit articles if the user wants to 
    // save it
    if (form.saveAndView.value()) {
        redirect(
            "/articles/show/" + form.slug.value()
        );
    // we continue in edit mode if the user wants to save and continue
    // to edit
    } else if (form.saveAndContinue.value()) {
        redirect(
            "/articles/edit/" + form.slug.value()
        );
    } else {
        go_back_to_previous_page();
    }
}
Example #3
0
void Articles::create_treat() {
    TREAT_PAGE();
    CHECK_PERMISSION_OR_GO_TO_LOGIN();

    forms::articles::Create form;
    form.load(context());

    if (!form.validate()) {
        set_message(_("The form is not valid."));
        go_back_to_previous_page();
        return;
    }
    
    const std::string lang = get_interface_lang();
    const std::string slug = form.slug.value();
    const std::string title = form.title.value();
    const std::string content = form.content.value();
    const std::string summary = form.summary.value();
    // we save in database the articles
    int articleId = articlesModel->create_from_lang_and_slug(
        lang,
        slug,
        title,
        content
    );

    if (articleId <= 0) {
        set_message(_("Error while trying to add the article"));
        go_back_to_previous_page();
        return;
    }
    
    results::Article article(
        articleId,
        lang,
        slug,
        title,
        content
    );

    // TODO add something to say that the article
    // as been created (and not simply that a "version"
    // has been added
    historyModel->add_version(
        article,
        get_current_user_id(),
        summary
    );


    // if save => display newly created articles
    if (form.saveAndView.value()) {
        redirect(
            "/articles/show/" + form.slug.value()
        );

    // if save and continue => turn now in edit mode
    } else if (form.saveAndContinue.value()) {
        redirect(
            "/articles/edit/" + form.slug.value()
        );
    } else {
        go_back_to_previous_page();
    }


}
Example #4
0
void Articles::create_treat() {
    TREAT_PAGE();
    LOGIN_REQUIRED();

    forms::articles::Create form;
    form.load(context());

    // if cancel => go back to main page
    if (form.cancel.value()) {
        // TODO most of the time if we are on a create page
        // its because we've clicked on a link to a non-existing
        // page, so it would be better that "cancel" return us to that page
        // maybe by storing the page url in the create form ?
        redirect(
            tatowiki::Config::main_url_from_lang(get_interface_lang())
        );
        go_back_to_previous_page();
        return;
    }

    if (!form.validate()) {
        add_error(_("The form is not valid."));
        go_back_to_previous_page();
        return;
    }

    const std::string lang = get_interface_lang();
    const std::string slug = form.slug.value();
    const std::string title = form.title.value();
    const std::string content = form.content.value();
    const std::string summary = form.summary.value();
    // we save in database the articles
    int articleId = articlesModel->create_from_lang_and_slug(
        lang,
        slug,
        title,
        content
    );

    if (articleId <= 0) {
        add_error(_("Error while trying to add the article"));
        go_back_to_previous_page();
        return;
    }
    
    results::Article article(
        articleId,
        lang,
        slug,
        title,
        content
    );

    // TODO add something to say that the article
    // as been created (and not simply that a "version"
    // has been added
    historyModel->add_version(
        article,
        get_current_user_id(),
        summary
    );


    // if save => display newly created articles
    if (form.saveAndView.value()) {
        redirect(
            "/articles/show/" + form.slug.value()
        );

    // if save and continue => turn now in edit mode
    } else if (form.saveAndContinue.value()) {
        redirect(
            "/articles/edit/" + form.slug.value()
        );
    } else {
        go_back_to_previous_page();
    }


}