Page 1 of 1

Row_Updated Server Event - no record with new date

Posted: Fri Jul 26, 2024 12:23 am
by mpol_ch

Hi,
I am using the below code to update a record.
When I update the recod it is removing the recored "Code" from table protokoll but it is not entering the "Code" for the new date in the table protokoll.
In this case I am only changing the field date.
Why is this not doing?

// Row_Updated event
function Row_Updated($rsold, &$rsnew)
{
    // Get the old and new values
    $codeOld = $rsold["Code"];
    $eventOld = $rsold["Event"];
    $dateOld = $rsold["Date"];
    $codeNew = $rsnew["Code"];
    $eventNew = $rsnew["Event"];
    $dateNew = $rsnew["Date"];

    // Remove the old code from the old event and date
    if (!empty($eventOld) && !empty($dateOld)) {
        $sqlOld =
            "SELECT `Id`, `Entschuldigt` FROM `protokoll` WHERE `Titel` = '" .
            AdjustSql($eventOld) .
            "' AND `Datum` = '" .
            AdjustSql($dateOld) .
            "'";
        $rsOld = Conn()->executeQuery($sqlOld);

        while ($rowOld = $rsOld->fetch()) {
            $idOld = $rowOld["Id"];
            $entschuldigtOld = $rowOld["Entschuldigt"];

            $entschuldigtArrayOld = explode(",", $entschuldigtOld);
            $index = array_search($codeOld, $entschuldigtArrayOld);
            if ($index !== false) {
                // Remove the old code
                unset($entschuldigtArrayOld[$index]);
                $newEntschuldigtOld = implode(",", $entschuldigtArrayOld);

                // Update the protokoll record
                $updateSqlOld =
                    "UPDATE `protokoll` SET `Entschuldigt` = '" .
                    AdjustSql($newEntschuldigtOld) .
                    "' WHERE `Id` = " .
                    AdjustSql($idOld);
                Conn()->executeUpdate($updateSqlOld);
            }
        }
    }

    // Add the new code to the new event and date
    if (!empty($eventNew) && !empty($dateNew)) {
        $sqlNew =
            "SELECT `Id`, `Entschuldigt` FROM `protokoll` WHERE `Titel` = '" .
            AdjustSql($eventNew) .
            "' AND `Datum` = '" .
            AdjustSql($dateNew) .
            "'";
        $rsNew = Conn()->executeQuery($sqlNew);

        while ($rowNew = $rsNew->fetch()) {
            $idNew = $rowNew["Id"];
            $entschuldigtNew = $rowNew["Entschuldigt"];

            $entschuldigtArrayNew = explode(",", $entschuldigtNew);
            if (!in_array($codeNew, $entschuldigtArrayNew)) {
                // Add the new code
                $entschuldigtArrayNew[] = $codeNew;
                $newEntschuldigtNew = implode(",", $entschuldigtArrayNew);

                // Update the protokoll record
                $updateSqlNew =
                    "UPDATE `protokoll` SET `Entschuldigt` = '" .
                    AdjustSql($newEntschuldigtNew) .
                    "' WHERE `Id` = " .
                    AdjustSql($idNew);
                Conn()->executeUpdate($updateSqlNew);
            }
        }
    }
}

mpol_ch


Re: Row_Updated Server Event - no record with new date

Posted: Fri Jul 26, 2024 4:08 pm
by mpol_ch

The change should be done in table protokoll WHERE $eventNew and $dateNew is matched.

The entry is comming from field "Code".
The field "Entschuldigt" is a comma separated field with intergers (Code).

When I update the record in table absences then the code is removing the "Code" from filed Entschuldigung of table protokoll where $eventOld & $dateOld is matched.
But it is not entering the Code into field "Entschuldigung" of table protokoll where $eventNew & $dateNew is matched.

mpol_ch


Re: Row_Updated Server Event - no record with new date

Posted: Fri Jul 26, 2024 6:16 pm
by arbei

You better check old and new values first, e.g.
var_dump($rsnow, $rsnew);
See if the values meet your conditions.


Re: Row_Updated Server Event - no record with new date

Posted: Fri Jul 26, 2024 7:32 pm
by mpol_ch

Perfect, thank you for suggestion.
The below code has solved my issue.
The reason was that field "Code" was not editable and therefore it did get a new value...

 // Row_Updated event
 function Row_Updated($rsold, &$rsnew) {
     global $Language;

     // Get the old and new values
     $codeOld = $rsold['Code'];
     $eventOld = $rsold['Event'];
     $dateOld = $rsold['Date'];
     $codeNew = $rsold['Code'];
     $eventNew = $rsnew['Event'];
     $dateNew = $rsnew['Date'];
 
     // Remove the old code from the old event and date
     if (!empty($eventOld) && !empty($dateOld)) {
         $sqlOld = "SELECT `Id`, `Entschuldigt` FROM `protokoll` WHERE `Titel` = '" . AdjustSql($eventOld) . "' AND `Datum` = '" . AdjustSql($dateOld) . "'";
         $rsOld = Conn()->executeQuery($sqlOld);

         while ($rowOld = $rsOld->fetch()) {
             $idOld = $rowOld['Id'];
             $entschuldigtOld = $rowOld['Entschuldigt'];

             $entschuldigtArrayOld = explode(',', $entschuldigtOld);
             $index = array_search($codeOld, $entschuldigtArrayOld);
             if ($index !== false) {
                 // Remove the old code
                 unset($entschuldigtArrayOld[$index]);
                 $newEntschuldigtOld = implode(',', $entschuldigtArrayOld);

                 // Update the protokoll record
                 $updateSqlOld = "UPDATE `protokoll` SET `Entschuldigt` = '" . AdjustSql($newEntschuldigtOld) . "' WHERE `Id` = " . AdjustSql($idOld);
                 Conn()->executeUpdate($updateSqlOld);
             }
         }
     }

     // Add the new code to the new event and date
     if (!empty($eventNew) && !empty($dateNew)) {
         $sqlNew = "SELECT `Id`, `Entschuldigt` FROM `protokoll` WHERE `Titel` = '" . AdjustSql($eventNew) . "' AND `Datum` = '" . AdjustSql($dateNew) . "'";
         $rsNew = Conn()->executeQuery($sqlNew);

         while ($rowNew = $rsNew->fetch()) {
             $idNew = $rowNew['Id'];
             $entschuldigtNew = $rowNew['Entschuldigt'];

             $entschuldigtArrayNew = explode(',', $entschuldigtNew);
             if (!in_array($codeNew, $entschuldigtArrayNew)) {
                 // Add the new code
                 $entschuldigtArrayNew[] = $codeNew;
                 $newEntschuldigtNew = implode(',', $entschuldigtArrayNew);

                 // Update the protokoll record
                 $updateSqlNew = "UPDATE `protokoll` SET `Entschuldigt` = '" . AdjustSql($newEntschuldigtNew) . "' WHERE `Id` = " . AdjustSql($idNew);
                 Conn()->executeUpdate($updateSqlNew);
             }
         }
     }

     // Set a success message with the old and new values
     $message = "Updated values: \n" .
                "Old Code: $codeOld, Old Event: $eventOld, Old Date: $dateOld \n" .
                "New Code: $codeNew, New Event: $eventNew, New Date: $dateNew";
     $this->setSuccessMessage($message);
 }

mpol_ ch