Use Row_CustomAction to Run Custom File

Hi,

I want to use a “Row_CustomAction” in List Page Load that execute/run an existing Custom File.
My Custom File is: rechdocx.php
The custom file rechdocx.php is working properly and it is generating a Word Docx file.
With CustomAction “CreateMultiRechnung” I want to create multi outputs for the selected word docs.

Below is my code.
Any recommendation where to start or a better way to go?

// Page Load event
function Page_Load() {
	//echo "Page Load";
	$this->Lieferadresse->DisplayValueSeparator = " "; 
    $this->CustomActions["CreateMultiRechnung"] = new ListAction("CreateMultiRechnung", "Create Multi Rechnung", IsLoggedIn(), ACTION_POSTBACK, ACTION_MULTIPLE,"","");
}

// Row Custom Action event
function Row_CustomAction($action, $row)
{
    // Return false to abort
if ($action == "CreateMultiRechnung") {
    /mpol/rechdocx?Code=$row["Code"]&KNr=$row["Kundenr"]&LAd=$row["Lieferadresse"]&Vorlage=rechnung.docx&ZZTage=30&$row["Id"]=7
}
    return true;
}

thanks
mpol_ch

mpol_ch wrote:

I want to create multi outputs for the selected word docs.

Please explain in more detail. An example of the output would be good to comprehend your needs.

Hi,
I am generating with custom file rechdocx.php a Word file with help of phpword.
Now, i want call/run the custom file rechdocx.php more than once to generate many word files, when I select more than one rows.

The custom file get the parameters with help of URL.I hope it is clearer now.

There should be no problem at all. Just get the selected records key in Row_CustomAction server event as a comma separated values (assume the primary key field of the table is ID and you’re using >= v2021), for example:

if ($action == "CreateMultiRechnung") {
    $multiple_id .= $row['ID'] . ",";
    if ($this->SelectedIndex == $this->SelectedCount) { // Last row
        $multiple_id = trim($multiple_id ','); // remove the last comma
        $this->setMessage('Multiple ID: ' . $multiple_id); // display it to check
        header('Location: yourcustomfile?multiple_id='.$multiple_id);
    }
}

Then in your Custom File get those multiple id by using Get() global function, explode it, and then process the result as you want.

Hi,
thanks.
But I am trying this way and it does not work.
The expected result is to have a .docx generated.When I call the custom file “Rechdocxsave.php” with app eq ht…/…/rechdocsave then it works.
Is this way correct?
Will it work in this way?

// Row Custom Action event
function Row_CustomAction($action, $row)
{
    // Return false to abort
if ($action == "CreateMultiRechnung") {
 exec('views/Rechdocxsave.php');
}
    return true;
}

mpol_ch

Hi,
I am getting with the following code only one record, even multiple rows are selected.
Is there an example for seleced multiple rows?So that I can get for each selected row a call of my file?

// Row Custom Action event
function Row_CustomAction($action, $row)
{
    // Return false to abort
if ($action == "CreateMultiRechnung") {
   header('Location:mpol/rechdocxsave?Code='.$row['Code'].'&KNr='.$row['Kundenr'].'&LAd='.$row['Lieferadresse'].'&Vorlage=rechnung.docx&ZZTage=30&Id='.$row['Id']);
exit;
}
  return true;  
}

mpol_ch

Your code and your logic is wrong. Please refer the code and the logic that I posted above. I have already tested it before posted to this forum, and I already make sure it works as expected.

thank you for your input.
I tried to use your code a below.
But I am getting an errot that multiple_id is undefined.

// Row Custom Action event
    function Row_CustomAction($action, $row)
    {
        if ($action == "CreateMultiRechnung") {
        $multiple_id .= $row['Id'] . ",";
        if ($this->SelectedIndex == $this->SelectedCount) { // Last row
        $multiple_id = trim($multiple_id, ','); // remove the last comma
        $this->setMessage('Multiple ID: ' . $multiple_id); // display it to check
        header('Location:mpol/rechdocxsave?Code='.$row['Code'].'&KNr='.$row['Kundenr'].'&LAd='.$row['Lieferadresse'].'&Vorlage=rechnung.docx&ZZTage=30&Id='.$row['Id']);
        } 
    }
    // Return false to abort
    return true;
    }

mpol_ch

Maybe this will make more clear on Row_CustomAction($action, $row).Whatever action you made in ListAction, the return results will be stored in $this->getFilterFromRecordKeys();The working step will be something like

$rows = $this->getFilterFromRecordKeys();
foreach ($rows as $row) {
  Row_CustomAction($action, $row); // means Row_CustomAction will execute multiple times.
}

Correct me if I’m wrong.

Put this code:

global $multiple_id;

before this line:

if ($action == "CreateMultiRechnung") {

and then try again.

thank you for the hint
Now the code is working but the expected ceration of .docx files are not working.
With “header('Location:mpol/rechdocxsave…” I am trying to generate a word file for each selected row.
Do you have an idea why not?

   function Row_CustomAction($action, $row)
  {
  // Return false to abort
  global $multiple_id;
  if ($action == "CreateMultiRechnung") {
  $multiple_id .= $row['Id'] . ",";
   if ($this->SelectedIndex == $this->SelectedCount) { // Last row
      $multiple_id = trim($multiple_id, ','); // remove the last comma
      $this->setMessage('Multiple ID: ' . $multiple_id); // display it to check
      header('Location:mpol/rechdocxsave?Code='.$row['Code'].'&KNr='.$row['Kundenr'].'&LAd='.$row['Lieferadresse'].'&Vorlage=rechnung.docx&ZZTage=30&Id='.$row['Id']);
    }
   } 
    return true;
    }

Thanks

It actually depends on how you write PHP code in your Custom File to generate that .docx files.

With customaction it is generating one doch.
But not the multi files eq. to multiple selection.

Then you need to re-evaluate your code in your Custom File. You should change the logic in there, to loop through those multiple ids, and then generate the .docx files as many as the ids.

OK.

Can I use exec or shell_exec?
Will is work?Thanks
mpol_ch

Yes, you can. Just Google php execute shell command for more info.

  1. Row_CustomAction is fired for each selected row, your code only needs to consider the current row.
  2. You should not use header('Location: …) which means redirecting. To execute code in your file you may use include() or curl(), depending on the code in your file.

HiI have used the following code but the there is no created file.
When I call “http://192.168.33.46/2022/rechnungsbox2022/rechdocxsave” then I a getting a file created.
This is a custom file which generates a docx file with help of phpwork.
Once it works then I will try with parameters.
I am seeing the output with echo but there is no output genereated…

    function Row_CustomAction($action, $row)
    {
    // Return false to abort
    global $multiple_id;
    if ($action == "CreateMultiRechnung") {
    $multiple_id .= $row['Id'] . ",";
    $url = "http://192.168.33.46/2022/rechnungsbox2022/rechdocxsave";
    $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_HEADER, true);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_exec($ch);
   curl_close($ch);
  echo $url;
   //header('Location:mpol/rechdocxsave');
     if ($this->SelectedIndex == $this->SelectedCount) { // Last row
        $multiple_id = trim($multiple_id, ','); // remove the last comma
        $this->setMessage('Multiple ID: ' . $multiple_id); // display it to check
        }
   } 
      return true;
  }

thanks
mpol_ch

arbei wrote:

To execute code in your file you may use include() or curl(), depending on the code in your file.

If your code uses session variables, curl() won’t work because you did not pass the cookie. You may post the code in /2022/rechnungsbox2022/rechdocxsave for discussion.

Hi,
here is the file rechdocxsave.php. It is a custom file with checked “Include common files”.


<?php // Dieser Code wird für den CustomActionTeil verwendet, um die massen docx zu generieren // $RId=$_GET['Id']; // Rechnung ID // $Code=$_GET['Code']; // Code // $KNr=$_GET['KNr']; // Kunden ID // $LAd=$_GET['LAd']; // Lieferadresse ID // $ZZTage = $_GET['ZZTage']; // $Vorlage = $_GET['Vorlage']; $RId=7; // Rechnung ID $Code=5; // Code $KNr=2; // Kunden ID $LAd=3; // Lieferadresse ID $ZZTage = 30; $Vorlage = 'rechnung.docx'; $phpWord = new \PhpOffice\PhpWord\PhpWord(); //$getvorlage = "SELECT Vorlage FROM rechnung WHERE Id =".$RId; $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('./mpol/vorlagen/'.$Vorlage); $myResult = ExecuteStatement("UPDATE rechnung SET ZahlFrist=(CURDATE() + INTERVAL $ZZTage DAY) WHERE Id=".$RId); $firma="SELECT * FROM rechnung WHERE Id =".$RId; $stmt = ExecuteQuery($firma); $Rech = $stmt->fetch(); $templateProcessor->setValue('RId', $Rech['Id']); $templateProcessor->setValue('Code', $Rech['Code']); $templateProcessor->setValue('RKNr', $Rech['Kundenr']); $templateProcessor->setValue('LAd', $Rech['Lieferadresse']); $templateProcessor->setValue('RDatum',date('d.m.Y', strtotime($Rech['Datum']))); $templateProcessor->setValue('ZZTage', $Rech['ZahlZielTage']); $templateProcessor->setValue('ZahlFrist',date('d.m.Y', strtotime($Rech['ZahlFrist']))); $templateProcessor->setValue('Status', $Rech['Status']); $templateProcessor->setValue('RTotal', $Rech['RTotal']); $templateProcessor->setValue('Vorlage', $Rech['Vorlage']); $templateProcessor->setValue('CName', $Rech['CName']); $templateProcessor->setValue('UName', $Rech['UName']); $templateProcessor->setValue('CDate', date('d.m.Y', strtotime($Rech['CDate']))); $templateProcessor->setValue('UDate', date('d.m.Y', strtotime($Rech['UDate']))); ....... setlocale(LC_ALL, array('de_CH.UTF-8','de_CH@chf','de_CH','swissgerman')); $filename ='rechnung_'.$RId.'.docx'; $templateProcessor->saveAs('mpol/'.$filename); ``` > mpol_ch