Example #1
0
void Articles::show(std::string slug) {

    if(tatowiki::Config::isPrivate()) {
        CHECK_PERMISSION_OR_GO_TO_LOGIN();
    }

    contents::articles::Show c;
    init_content(c);
    c.markdown = mymarkdown;

    std::string lang = get_interface_lang();

    c.cacheKey = lang + slug;
    
    c.article = articlesModel->get_from_lang_and_slug(
        lang,
        slug
    );
   
    // if the article does not exists we redirect to the
    // page to create one
    if (!c.article.exists()) {
        redirect(
            "/articles/create/" + slug
        );
        return;
    }


    c.translatedIn = articlesModel->get_group_of(
        c.article.id,
        c.article.groupId
    );
    render("articles_show", c);
}
Example #2
0
void Articles::edit(const std::string slug) {
    CHECK_PERMISSION_OR_GO_TO_LOGIN();


    results::Article article = articlesModel->get_from_lang_and_slug(
        get_interface_lang(),
        slug
    );

    // if the articles does not exist we load the "create" form rather
    // than "edit" one
    if (!article.exists()) {
        redirect(
            "/articles/create/" + slug
        );
        return;
    }
    int lastVersionId = historyModel->get_last_version_id_of(
        article.id
    );

    contents::articles::Edit c(article,lastVersionId);
    init_content(c);
    //TODO get last_version_id of the article and
    // add it to the content

    render("articles_edit", c);
}
Example #3
0
void Articles::remove(const std::string slug) {

    CHECK_PERMISSION_OR_GO_TO_LOGIN();

    const bool success = articlesModel->remove(
        get_interface_lang(),
        slug
    );
    
    if (success) {
        set_message(_("The article has been removed"));
    } else {
        set_message(_("A problem occured while trying to remove"));
    }
    go_to_main_page();
}
Example #4
0
void Words::show_random() {

    results::Word word = wordModel.get_random_word();
    if (word.exists()) {
        response().set_redirect_header(
            "/" + get_interface_lang() +"/words/show-in" +
            "/" + word.text + 
            "/" + word.lang 
        );

     } else { // if there's no word in the database
        // TODO maybe redirect to /words/show
        // with an error message in session to explain no words exist
        go_back_to_previous_page();
    }
}
Example #5
0
void Articles::remove(const std::string slug) {

    LOGIN_REQUIRED();

    const bool success = articlesModel->remove(
        get_interface_lang(),
        slug
    );
    
    if (success) {
        add_success(_("The article has been removed"));
    } else {
        add_error(_("A problem occured while trying to remove"));
    }
    go_to_main_page();
}
Example #6
0
void Words::show_all_langs_filter_treat() {
	forms::LangsFilter langsFilter;
    langsFilter.load(context());

    if (langsFilter.validate()) {
        response().set_redirect_header(
            "/" + get_interface_lang() +"/words/show-all-in" +
            "/" + langsFilter.langFilter.selected_id()
        );

    } else {
        response().set_redirect_header(
            request().http_referer()
        );
    }

}
Example #7
0
void Articles::translate(const std::string slug) {
    CHECK_PERMISSION_OR_GO_TO_LOGIN();

    int articleToTranslateId = articlesModel->get_id_from_lang_and_slug(
        get_interface_lang(),
        slug
    );

    if (articleToTranslateId == ARTICLE_DOESNT_EXIST_ERROR) {
        set_message(_("The article you try to translate does not exist."));
        go_back_to_previous_page();
        return;
    }

    contents::articles::Translate c(slug);
    init_content(c);


    render("articles_translate", c);
}
Example #8
0
void Articles::translate(const std::string slug) {
    LOGIN_REQUIRED();

    int articleToTranslateId = articlesModel->get_id_from_lang_and_slug(
        get_interface_lang(),
        slug
    );

    if (articleToTranslateId == ARTICLE_DOESNT_EXIST_ERROR) {
        add_error(_("The article you try to translate does not exist."));
        go_back_to_previous_page();
        return;
    }

    contents::articles::Translate c(slug);
    init_content(c);


    render("articles_translate", c);
}
Example #9
0
void Articles::create(std::string slug) {
    CHECK_PERMISSION_OR_GO_TO_LOGIN();

    results::Article article = articlesModel->get_from_lang_and_slug(
        get_interface_lang(),
        slug
    );

    // if the article exists there's no need to create it
    // we display it directly instead
    if (article.exists()) {
        //TODO use mapper instead
        redirect(
            "/articles/show/" + slug
        );
        return;
    }

    contents::articles::Create c(slug);
    init_content(c);

    render("articles_create", c);
}
Example #10
0
/**
 * virtual page, used to received the data from the words/edit form
 * and treat them
 */
void Words::edit_treat() {
    CHECK_PERMISSION_OR_GO_TO_LOGIN();

	forms::EditWord editWord;
    editWord.load(context());
    
    if (!editWord.validate()) {
        go_back_to_previous_page();
        return;
    }
    // TODO : handle if something wrong happen while saving
    wordModel.edit_word(
        atoi(editWord.wordId.value().c_str()), 
        editWord.wordLang.selected_id(),
        editWord.wordString.value(),
        get_current_user_id()
    );

    response().set_redirect_header(
        "/" + get_interface_lang() +"/words/show-in"
        "/" + editWord.wordString.value() +
        "/" +editWord.wordLang.selected_id()
    );
}
Example #11
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 #12
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 #13
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 #14
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();
    }


}