[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] FAQs: adding faq message on create, edit, and delete

"luci \(@luciash\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <69b057a383a3c_3b3a41264211e@gitlab-sidekiq-low-urgency-cpu-bound-v2-6df5f9ffdd-pk5vd.mail>

luci pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
1cb4b89c by Grace Nshokano at 2026-03-10T17:31:59+00:00
[FIX] FAQs: adding faq message on create, edit, and delete
---
* [FIX] FAQs: remove hardcoded title limit and derive max length from DB schema

* [FIX] FAQs: align title length validation with shared no-truncation pattern

* [FIX] FAQ: trim description input on save

* [FIX] faq: use Untitled fallback title in delete feedback

* [FIX]: fix: lint issue fix

* [FIX]: fix: lint issue

* [FIX]: fix: adding faq message on create, edit and delete

See merge request tikiwiki/tiki!8497

- - - - -


3 changed files:

- lib/faqs/faqlib.php
- templates/tiki-list_faqs.tpl
- tiki-list_faqs.php


Changes:

=====================================
lib/faqs/faqlib.php
=====================================
@@ -12,6 +12,35 @@
  */
 class FaqLib extends TikiLib
 {
+    /**
+     * Fallback limit of the title field of the tiki_faqs table.
+     */
+    public const MAX_FAQ_TITLE_LENGTH = 200;
+
+    /**
+     * Reads the FAQ title length directly from DB schema to avoid hardcoded usage.
+     * Falls back to MAX_FAQ_TITLE_LENGTH if schema lookup fails.
+     */
+    public function getFaqTitleMaxLength(): int
+    {
+        static $cachedLength = null;
+
+        if ($cachedLength !== null) {
+            return $cachedLength;
+        }
+
+        $column = $this->fetchAll("SHOW COLUMNS FROM `tiki_faqs` LIKE 'title'");
+        $type = is_array($column) ? ($column[0]['Type'] ?? '') : '';
+
+        if (preg_match('/^varchar\((\d+)\)/i', $type, $matches)) {
+            $cachedLength = (int) $matches[1];
+            return $cachedLength;
+        }
+
+        $cachedLength = self::MAX_FAQ_TITLE_LENGTH;
+        return $cachedLength;
+    }
+
     /**
      * @param $offset
      * @param $maxRecords


=====================================
templates/tiki-list_faqs.tpl
=====================================
@@ -97,12 +97,12 @@
                         {tr}Title:{/tr}
                     </label>
                     <div class="col-md-8">
-                        <input type="text" class="form-control" name="title" maxlength="200" value="{$title|escape}">
+                        <input type="text" class="form-control" name="title" maxlength="{$MAX_FAQ_TITLE_LENGTH}" value="{$title|escape}">
                         {jq}
                             $("input[name=title]").on("keyup", function () {
                                 var length = $(this).val().length;
-                                if(length > 200) {
-                                    alert("{tr}You have reached the number of characters allowed (200 max) for the FAQ title field.{/tr}");
+                                if(length > {$MAX_FAQ_TITLE_LENGTH}) {
+                                    alert("{tr _0=$MAX_FAQ_TITLE_LENGTH}You have reached the number of characters allowed (%0 max) for the FAQ title field.{/tr}");
                                 }
                             });
                         {/jq}


=====================================
tiki-list_faqs.php
=====================================
@@ -32,6 +32,9 @@ $auto_query_args = ['offset', 'find', 'sort_mode', 'faqId'];
 $access->check_feature('feature_faqs');
 $access->check_permission('tiki_p_view_faqs');
 //get_strings tra('Admin FAQs')
+$maxFaqTitleLength = $faqlib->getFaqTitleMaxLength();
+$smarty->assign('MAX_FAQ_TITLE_LENGTH', $maxFaqTitleLength);
+
 if (! isset($_REQUEST["faqId"])) {
     $_REQUEST["faqId"] = 0;
 }
@@ -51,33 +54,59 @@ if (isset($_REQUEST["remove"]) && $access->checkCsrf()) {
     if ($tiki_p_admin_faqs != 'y') {
         Feedback::errorAndDie(tra("You do not have the permission that is needed to use this feature"), \Laminas\Http\Response::STATUS_CODE_401);
     }
-    $faqlib->remove_faq($_REQUEST["remove"]);
+    try {
+        $faqToRemove = $faqlib->get_faq($_REQUEST["remove"]);
+        if ($faqToRemove) {
+            $faqTitle = htmlspecialchars($faqToRemove['title'] ?? tra('Untitled'), ENT_QUOTES, 'UTF-8');
+            $faqlib->remove_faq($_REQUEST["remove"]);
+            Feedback::success(tr("FAQ '%0' has been successfully deleted.", $faqTitle));
+        } else {
+            Feedback::error(tra("The FAQ you are trying to delete was not found."));
+        }
+    } catch (Exception $e) {
+        Feedback::error(tr("An error occurred while deleting the FAQ: %0", $e->getMessage()));
+    }
 }
 if (isset($_REQUEST["save"])) {
-    if (empty($_REQUEST["title"])) {
-        Feedback::errorAndDie(tra("You can not create a FAQ without a title "), \Laminas\Http\Response::STATUS_CODE_409);
-    }
     $access->checkCsrf();
     $access->check_permission('tiki_p_admin_faqs');
-    if (mb_strlen($_REQUEST["title"]) > 200) {
-        Feedback::errorAndDie(tra("You have exceeded the number of characters allowed (200 max) for the FAQ title field"), \Laminas\Http\Response::STATUS_CODE_409);
-    }
-    if (isset($_REQUEST["canSuggest"]) && $_REQUEST["canSuggest"] == 'on') {
-        $canSuggest = 'y';
+
+    $title = trim($_REQUEST["title"] ?? '');
+    $description = trim($_REQUEST["description"] ?? '');
+    $canSuggest = (isset($_REQUEST["canSuggest"]) && $_REQUEST["canSuggest"] === 'on') ? 'y' : 'n';
+    $submittedFaqId = (int) ($_REQUEST["faqId"] ?? 0);
+
+    // Preserve submitted values when validation fails.
+    $smarty->assign('faqId', $submittedFaqId);
+    $smarty->assign('title', $title);
+    $smarty->assign('description', $description);
+    $smarty->assign('canSuggest', $canSuggest);
+
+    if ($title === '') {
+        Feedback::error(tra("You cannot create a FAQ without a title."));
+    } elseif (! Feedback::validateFieldLength("Title", $title, $maxFaqTitleLength)) {
     } else {
-        $canSuggest = 'n';
+        $isEdit = $submittedFaqId > 0;
+        $fid = $faqlib->replace_faq($submittedFaqId, $title, $description, $canSuggest);
+        $escapedTitle = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
+        $successMessage = $isEdit ?
+            tr("FAQ '%0' has been successfully updated.", $escapedTitle) :
+            tr("FAQ '%0' has been successfully created.", $escapedTitle);
+        Feedback::success($successMessage);
+        // Categorize
+        $cat_type = 'faq';
+        $cat_objid = $fid;
+        $cat_desc = substr($description, 0, 200);
+        $cat_name = $title;
+        $cat_href = "tiki-view_faq.php?faqId=" . $cat_objid;
+        include_once("categorize.php");
+
+        // Clear the form
+        $smarty->assign('faqId', 0);
+        $smarty->assign('title', '');
+        $smarty->assign('description', '');
+        $smarty->assign('canSuggest', '');
     }
-    $fid = $faqlib->replace_faq($_REQUEST["faqId"], $_REQUEST["title"], $_REQUEST["description"], $canSuggest);
-    $cat_type = 'faq';
-    $cat_objid = $fid;
-    $cat_desc = substr($_REQUEST["description"], 0, 200);
-    $cat_name = $_REQUEST["title"];
-    $cat_href = "tiki-view_faq.php?faqId=" . $cat_objid;
-    include_once("categorize.php");
-    $smarty->assign('faqId', 0);
-    $smarty->assign('title', '');
-    $smarty->assign('description', '');
-    $smarty->assign('canSuggest', '');
 }
 if (! isset($_REQUEST["sort_mode"])) {
     $sort_mode = 'title_asc';



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/1cb4b89c8040f57bb88271ca096e19be9a4996f4

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/1cb4b89c8040f57bb88271ca096e19be9a4996f4
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.