Skip to content

Instantly share code, notes, and snippets.

@numlock1052
Last active November 4, 2023 05:52
Show Gist options
  • Select an option

  • Save numlock1052/1fcfa42ac5b8ecc5768de6913cd39ba8 to your computer and use it in GitHub Desktop.

Select an option

Save numlock1052/1fcfa42ac5b8ecc5768de6913cd39ba8 to your computer and use it in GitHub Desktop.
Display the job number wherever it should be visible in HumHub without modifying the PHP code

image

To use this script open your HumHub with admin rights and go to:

# Top menu -> Administration -> Settings -> Advanced -> Statistics

Inside the "HTML tracking code" copy paste this code :

<style>
    .thumbnail {
      	margin: 25px;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19) !important;
        min-height: 300px;
    }
  	.img-profile-header-background {
        height: 100px!important;
    }
  	.profile-user-photo-container {
    	display: none;
    }
</style>
<script>
(() => {
    // Version 1.0.1
    const DEBUG = true;

    const _debug = (...args) => {
        if (DEBUG) console.log(...args);
    };

    const getId = (url) => {
        var id = undefined;
        url.split("&").every((element) => {
            if (element.indexOf("id=") === 0) {
                id = element.replace("id=", "");
                return false;
            }
            return true;
        });
        return id;
    };

    const setListTaskNumber = () => {
        // Add Task-%number% for every task in the list : /index.php?r=tasks%2Flist
        var elements = document.querySelectorAll(".task-list-item");
        if (elements) {
            var tasks = [].slice.call(elements);
            if (tasks && Array.isArray(tasks)) {
                tasks.forEach((element) => {
                    var id = element.getAttribute("data-task-id");
                    var detail = element.querySelector(".toggleTaskDetails");
                    if (detail && detail.innerText.indexOf("Task-") === -1) {
                        _debug("setListTaskNumber");
                        detail.innerHTML = ["<b>Task-", id, "</b> / ", detail.innerText].join("");
                    }
                });
            }
        }
    };

    const setListActivyTaskNumber = () => {
        // Add Task-%number% in the global tasks list : /index.php?r=tasks%2Fglobal
        var elements = document.querySelectorAll("div[data-task-url]");
        if (elements) {
            var tasks = [].slice.call(elements);
            if (tasks && Array.isArray(tasks)) {
                tasks.forEach((element) => {
                    var url = element.getAttribute("data-task-url");
                    if (url && url.indexOf("/index.php?r=tasks%2Ftask%2Fview&id=") > -1) {
                        var id = getId(url);
                        var heading = element.querySelector(".media-heading");
                        if (heading && heading.innerHTML.indexOf("<b>Task-") === -1) {
                            _debug("setListActivyTaskNumber");
                            heading.innerHTML = ["<b>Task-", id, "</b> / ", heading.innerText].join("");
                        }
                    }
                });
            }
        }
    };

    const setTaskNumberProgressionEdition = () => {
        // Add Task-%number% in detail of task : /index.php?r=tasks%2Ftask%2Fview&id
        var elements = document.querySelectorAll('button[data-action-click="task.changeState"]');
        if (elements) {
            var buttons = [].slice.call(elements);
            if (buttons && Array.isArray(buttons)) {
                buttons.forEach((element) => {
                    var url = element.getAttribute("data-action-click-url");
                    if (
                        url &&
                        (url.indexOf("/index.php?r=tasks%2Ftask%2Fproceed&id=") > -1 ||
                            url.indexOf("/index.php?r=tasks%2Ftask%2Frevert&id=") > -1)
                    ) {
                        var id = getId(url);
                        var text = document.querySelector(".task-head").querySelector("strong").childNodes[1];
                        var task = [" Task-", id, " / "].join("");
                        if (id && text && text.data.indexOf(task) === -1) {
                            _debug("setTaskNumberProgressionEdition");
                            text.data = [task, text.data].join("");
                        }
                    }
                });
            }
        }
    };

    const setTaskNumberActualityAndGlobalView = () => {
        // Add Task-%number% in the dashboard view : /index.php?r=dashboard%2Fdashboard
        var elements = document.querySelectorAll(".wall-entry-task");
        if (elements) {
            var tasks = [].slice.call(elements);
            if (tasks && Array.isArray(tasks)) {
                tasks.forEach((element) => {
                    var button = element.querySelector("a.btn");
                    if (button && button.href.indexOf("/index.php?r=tasks%2Ftask%2Fview&id=") > -1) {
                        var id = getId(button.href);
                        if (id) {
                            var body = element.parentNode.parentNode.parentNode;
                            var link = body.querySelector(".media-heading a");
                            var task = ["Task-", id, " / "].join("");
                            if (link && link.innerText.indexOf(task) === -1) {
                                _debug("setTaskNumbeDashboard");
                                link.innerText = [task, link.innerText].join("");
                            }
                        }
                    }
                });
            }
        }
    };

    const setFormTaskNumber = () => {
        // Add Task-%number% for every task forms (edition popup)
        var elements = document.querySelectorAll("form");
        if (elements) {
            var forms = [].slice.call(elements);
            var form = undefined;
            if (forms && Array.isArray(forms)) {
                forms.every((element) => {
                    if (
                        element &&
                        element.action &&
                        element.action.indexOf("/index.php?r=tasks%2Ftask%2Fedit&id=") > -1
                    ) {
                        form = element;
                        return false;
                    }
                    return true;
                });
                if (form) {
                    var id = getId(form.action);
                    if (id && form && !form.querySelector(".field-task-id")) {
                        _debug("setFormTaskNumber");
                        var titleField = form.querySelector(".field-task-title");
                        var body = titleField.parentNode;
                        var taskIdDiv = document.createElement("DIV");
                        taskIdDiv.setAttribute("class", "form-group field-task-id");
                        taskIdDiv.innerHTML = [
                            '<label class="control-label"><h4>Task number : #<b>',
                            id,
                            "</b></h4></label>",
                        ].join("");
                        body.prepend(taskIdDiv);
                    }
                }
            }
        }
    };

    const setMediaListTaskNumber = () => {
        // Add Task-%number% in the media-list block
        var elements = document.querySelectorAll(".media-list a");
        if (elements) {
            var links = [].slice.call(elements);
            if (links && Array.isArray(links)) {
                links.forEach((link) => {
                    if (
                        link &&
                        link.querySelector("strong") &&
                        link.href.indexOf("index.php?r=tasks%2Ftask%2Fview&id=") > -1
                    ) {
                        var id = getId(link.href);
                        var strong = link.querySelector("strong");
                        var task = ["Task-", id, " / "].join("");
                        if (id && strong && link.innerText.indexOf(task) === -1) {
                            strong.innerText = [task, strong.innerText].join("");
                        }
                    }
                });
            }
        }
    };

    const setAll = () => {
        setListTaskNumber();
        setListActivyTaskNumber();
        setTaskNumberActualityAndGlobalView();
        setTaskNumberProgressionEdition();
        setFormTaskNumber();
        setMediaListTaskNumber();
    };

    const delayedUpdate = () => {
        setTimeout(() => setAll(), 500);
        setTimeout(() => setAll(), 1000);
    };

    $(document).bind("click", () => delayedUpdate());

    $(window).on("popstate", () => delayedUpdate());

    delayedUpdate();
})();
</script>

Related to the product : https://www.humhub.com/en/
Tested version 1.14.3 / 15.09.2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment