Pages

Sunday, July 25, 2021

Auto-generated menu : a simple idea to save time in your developpement


Most of the time, I need to do small developpement in my job and I need to do it quickly. I have to think how to improve my developpement.For example, recently, I need to add menu items quickly and when you read the code of your web site you've made few month later, it 's sometimes quite difficult. 
That's why I decided to save my menu in database. Now, menu is auto-generated.

Why do I need to save a menu in database ? 

In my work, I made a business web site with cake-php 3.9.X , Postgresql database and Bootstrap.

The goal of this tool is to display reports (generated automatically in pdf with Businness Object).Every report has a specific name.If click on an item of my menu, I need severeal paremeters in order to display the correct report like name, date, enable or disable... 

Here is an example of a  menu :

I've met a problem when I wanted  to add a submenu, I need to write to much codes :

<?php

//
$ssdomain='INFORMATION';
if($selectedSSDomain == $ssdomain) {
$ssdomainLabel = "<span class='navbar-custom'>CONTACT</span>";
} else {
$ssdomainLabel = "CONTACT";
}
echo "<form method='post' action='index.php' id='form_contact'>";
echo "<input type='hidden' name='domain' value='Information'/>";
echo "<input type='hidden' name='ssdomain' value='CONTACT'/>";
echo "<input type='hidden' name='startWithReport' value='key_1'/>";
if (isset($_POST['chooseReportDate'])) {
$tmpDate = $_POST['chooseReportDate'];
echo "<input type='hidden' name='chooseReportDate' value=\"$tmpDate\"/>";
}
if(isset($_POST['select_org']) ) {
$code = $_POST['select_org'];
echo "<input type='hidden' name='select_org' value=\"$code\"/>";
}

echo "<a class='dropdown-item' onclick=\"document.getElementById('form_contact').submit(); return false;\" >$ssdomainLabel</a>";

echo "</form>";

In my business, it happens quite often (every 6 month) and I need to remember all parameters !That's why I decided to save my menu in database. Now, it's really easy to add a new menu !


Table Menu

First, we need to add all your menu information in a table of our database as text, position, enable...

Here is the Menu table :





drop table yuzu.menu;
CREATE TABLE yuzu.menu (
                      id int primary key ,
                      id_parent int,
                      shortkey varchar(50) NOT NULL,
                      name varchar(50) NOT NULL,
                      position int,
                      grey bool
);


This table contains :
  • id : menu id
  • id_parent : To indicate that the menu is a submenu
  • shortkey : Use to identify the choosen menu
  • position : We need to order menu.
For each level of menu, we indicated the position. For example, HOME is first (position 1) then INFORMATION is second.
The submenu is also sortered with CONTACT in second position and PROFILE in the first position :



HomeController

In my controller, I retreived the Menu table and stored it in a session.    


Display auto generated menu

I wrote some code to display the menu automatically. To achieved that, I defined one method : function displayMenu($menus){}



Here is the code of two methods :

        <?php


        function getSortedMenu($menus, $currentIdParent) {
            $mainMenus = array();
            // Sort Menu
            foreach ($menus as $arr) {
                $submenus = $arr[0];
                $idParent = $submenus['id_parent'];
                if ($idParent == $currentIdParent) {
                    $position = $submenus['position'];
                    $mainMenus[$position]=$submenus;
                }
            }
            ksort($mainMenus);
            return $mainMenus;
        }


        function displayMenu($menus) {
            $mainMenus = getSortedMenu($menus, 0);
            foreach($mainMenus as $levelOnes) {

                echo "<li class=\"nav-item dropdown\">";
                $shortkey = "dropdown_".$levelOnes['shortkey'];
                echo "<a class=\"nav-link dropdown-toggle text-menu\" id='$shortkey' data-toggle=\"dropdown\" href='#'>".$levelOnes['name']."</a>";
                echo "<div class='dropdown-menu' aria-labelledby='$shortkey'>";

                foreach ($menus as $arr) {

                    $menu = $arr[0];
                    if($menu['id_parent']==$levelOnes['id']) {
                        $shortkey = "form_".$menu['shortkey'];

                        echo "<form method='post' action='/home' id='".$shortkey."'>";
                        $disabled="";
                        if ($menu['grey']) {
                            $disabled='disabled';
                        }
                        echo "<input type='hidden' name='menu_selected_id' value='".$menu['id']."'/>";
                        echo "<a class='dropdown-item $disabled text-menu' href='#'>" . $menu['name'] . "</a>";
                        echo "</form>";
                    }
                }
                echo "</div>";
                echo "</li>";
            }
        }
        ?>

This code used bootstrap and will certainly be improved.I think it's often a good way to auto-generated things. I hope it will give you some idea for your source code. 

Notes :

  • We have only two levels because I didn't need a level three of my menu.
  • The next step  is to manage authorization ! 

Versions :

CakePHP 3.9
Postgresql 9.6
Bootstrap 4.2.1

Sunday, July 11, 2021

PHPStorm Warning : Illegal string offset

 

Here is a comment about "Illegal string offset warning" I had in phpstorm.



I found the solution on StackOverflow : 

The error Illegal string offset 'whatever' in... generally means: you're trying to use a string as a full array.


In my case, I defined the following :

    /**
     * isAuthorized
     * @param string $user user authorized
     */
    public function isAuthorized($user)
    {
        // Autorisations
        // admin peut tout faire
        if ($user['role'] === 'admin') {
            return true;
        }

In fact, I made a mistake. The $user variable was defined in method comment as a string. But, $user is an array ! That's why I had this warning.

To avoid this warning, you have to change the type of the variable in method comment and a good practice is to add an "s" at the end of the variable name in order to identify easily the array variable. 


    /**
     * isAuthorized
     * @param array $users user authorized
     */
    public function isAuthorized($users)
    {
        // Autorisations
        // admin peut tout faire
        if ($users['role'] === 'admin') {
            return true;
        }


Sunday, July 4, 2021

PHPStorm Warning : Unresolved function or method $()

 I noticed this warning on my project : "Unresolved function or method $()"


I found the solution on the idea support (See https://intellij-support.jetbrains.com/hc/en-us/community/posts/360002260719--jQuery-shortcut-underlined-as-unresolved-function-or-method-)

The solution :

Types resolving won't work when using minified library version (jquery-3.3.1.min.js), but downloading typings (@types/jquery) should work:
- in Settings | Languages & Frameworks | JavaScript | Libraries, press Download..., choose jquery from the list
Or, install it in your project:
- in terminal, cd to your project root folder
- run npm i @types/jquery  command



Version :
- phpstorm 2021.1.3



Débutez en crypto-monnaie de manière ludique avec les projets "Move to earn"

Les projets "Move to earn" sont des projets très en vogue en ce moment dans le monde de la crypto-monnaie. Le principe est simple ...