1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Email plugin doesn't attach my PDF & Placeholders issue

Discussion in 'Community' started by mvr10, Aug 4, 2019.

  1. mvr10

    mvr10 New Member

    Level: Community
    Hello again.
    I have nearly finished my attaching-a-custom-PDF-to-email program, but two last issues have appeared on my way.
    First problem: I've already learned to create the custom PDF with placeholders in its title and upload it into a specific folder on my hosting, but it turns out that "email form plugin" only attaches already existing PDFs. Could you help me there, please?
    Second problem: I need the PHP code I've used in order to explain it to you:
    PHP:

    <?php
    require_once('libraries/tcpdf/tcpdf.php');

    $foo = $formModel->getElementData('mytable_7_repeat___devices', false, '', 1);
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
      $pdf->SetAuthor('Sandhar');
      $pdf->SetTitle('Autorización de equipo/s: {source}<?php echo '$foo'; ?>{/source}');
      $pdf->SetSubject('Autorización');
      $pdf->SetHeaderData(PDF_HEADER_TITLE.' {source}<?php echo '$foo'; ?>{/source}', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
      $pdf->setFooterData(array(0,64,0), array(0,64,128));
      $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
      $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
      $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
      $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
      $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
      $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
      $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
      $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
      $pdf->setFontSubsetting(true);
      $pdf->SetFont('helvetica', '', 12, '', true);
      $pdf->AddPage();
    $html = <<<EOD
      <p style="text-align: center;"><span style="font-size: 18pt;"><strong>Autorización para Uso de Soportes</strong></span></p>
    <p style="text-align: center;"><span style="font-size: 18pt;"><strong>Fuera de las Instalaciones de la Empresa</strong></span></p>
    <p style="text-align: right;"> Sandhar a Fecha: {mysql_date}</p>
    <p> </p>
    <p> </p>
    <p><span style="text-decoration: underline;"><strong>Name</strong></span>: {mytable___name}</p>
    <p><span style="text-decoration: underline;"><strong>Surname</strong></span>: {mytable___surname}</p>
    <p><span style="text-decoration: underline;"><strong>Devices</strong></span>: {source}<?php echo '$foo'; ?>{/source} </p>
    <p> </p>
    <p style="text-align: center;"> </p>
    <hr />
    <p> </p>
    EOD
    ;
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    $filename = 'authorization_{mytable___name}{mytable___surname}.pdf';
    $partialpath = $_SERVER['DOCUMENT_ROOT'];
    $finalfolder = 'authorizations';
    $iswritabledocumentpath = $partialpath . '/' . $finalfolder . '/' . $filename;
    $isnotwritabledocumentpath = $partialpath . '/' . $filename;
    if (is_writable($partialpath . '/' . $finalfolder))
      {
      $pdf->Output($iswritabledocumentpath, 'F');
      }
    else
      {
        $pdf->Output($isnotwritabledocumentpath, 'F');
      }
    ?>
     
    I don't know if I'm using $foo correctly, especially here, where I want to display the device name:
    PHP:
    echo '$foo';
    I have a repeat group connected to a group with all my devices, and the form is associated to the repeat group. The HTML message of "email form plugin" doesn't seem to have problems with this, because the conflictive placeholder ({mytable_7_repeat___devices}) works perfectly there. But PHP code (which also should work with placeholders as well as the HTML message) of "PHP form plugin" only displays what I think it's the device ID. I can't use PHP neither in the HTML message nor in between ' ', even though I have Sourcerer (I know this last thing isn't related with Fabrik, but it's very bothering and I would appreciate a little help).

    If you need more information, please do not hesitate to tell me.
     
  2. troester

    troester Well-Known Member Staff Member

    Level: Community
    When are you running the PHP plugin, onBeforeStore?

    From where are you running this code, from a php file or from the php code box?
    In the box don't use <?php

    You are in php, do string concatenation
    ...
    $pdf->SetTitle('Autorización de equipo/s: ' .$foo);

    And surely you can't use Fabrik element placeholders in the HTML you are passing to tcpdf (this is outside Fabrik), so fetch your data before and do also string concatenation.
    I would start simple, hardcoded filename, hardcoded text, then go on if file creation and attachment is working.
    As I said, this is all outside Fabrik.
     
  3. mvr10

    mvr10 New Member

    Level: Community
    Hello Troester,
    I'm very new and I'm starting in the world of coding, so thanks for the comprension and patience. In relation to your questions, I'm running the PHP plugin from the box, and after the end of the form (getEndContent), but I've also tried with at the end of the form submission (onAfterProcess).

    Thanks
     
    Last edited: Aug 4, 2019
  4. mvr10

    mvr10 New Member

    Level: Community
    I have modified the code (again), because I didn't realise a tiny detail. This is it currently:
    PHP:
    //$foo
    $foo = $formModel->getElementData('my_repeat_table___dispositivo');
    //also $foo = $data['my_repeat_table___dispositivo'];
    Now "dispositivo" ({mytable_7_repeat___devices}) appears as "Array" in the PDF. What do I do?
     
    Last edited: Aug 4, 2019
  5. troester

    troester Well-Known Member Staff Member

    Level: Community
    What do you expect to get? A repeat has multiple entries. So do e.g. $xy[0] to get one value or implode your array to concatenate

    Gesendet von meinem SM-G930F mit Tapatalk
     
  6. mvr10

    mvr10 New Member

    Level: Community
    I expected to get the name of the device.

    What do you mean with this? (Sorry, as I said, I just started coding.)
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Your element is in a repeat group, so has multiple values. So the data ($foo) you get is an array.

    Code (Text):

    $foo = $formModel->getElementData('my_repeat_table___dispositivo');
    // if you just want the first entry, do ...
    $title = $foo[0];
    // or if you want to concatenate all entries with (say) a comma, do ...
    $title = implode(',', $foo);
     
    If you just want to use the first entry, use $foo[0].

    But as that's a repeat group ... I'm not sure what you want to use as the title if there are multiple instances of the group data.

    -- hugh
     
  8. mvr10

    mvr10 New Member

    Level: Community
    Hello Hugh,
    This might help:
    In this form, then, you can select one (or more than one) of the devices that are displayed. The device I want to appear on the PDF is/are that/those selected device. So I think that the title you are refering to would be what I just said. And I still have this huge problem:
    Thanks in advance.

    ---------------------------------------------------------------
    EDITED: Hugh, your two options haven't worked for me, both of them. Text keeps appearing as "Array".
    PHP:
    $foo = $formModel->getElementData('my_repeat_table___dispositivo');
    // if you just want the first entry, do ...
    $title = $foo[0];
    // or if you want to concatenate all entries with (say) a comma, do ...
    $title = implode(',', $foo);
    ----------------------------------------------------------------
    A piece of my PHP:
    PHP:
    //$foo and $title, for {mytable_7_repeat___dispositivo}
    $foo = $formModel->getElementData('nytable_7_repeat___dispositivo');
    $title = implode (',', $foo);
    //$title = $foo[0];

    [···]

    $html2 = '
    <p><span style="text-decoration: underline;"><strong>Dispositivos</strong></span>: </p>'
    . $title;
     
    Last edited: Aug 6, 2019
  9. troester

    troester Well-Known Member Staff Member

    Level: Community
    did you try to run the php plugin onBeforeStore?

    Which element type is diposivo?
    Try if you get something meaningful with $foo[0][0]

    Gesendet von meinem SM-G930F mit Tapatalk
     
    Last edited: Aug 6, 2019
  10. mvr10

    mvr10 New Member

    Level: Community
    I'll try that, thanks.
    I'll also try that, thanks.
    I didn't realise that "dispositivo" is a dropdown. I'll leave a screenshot of what I see below. "Grupo Dispositivos (EXT)" is the repeat group. Anyway, could I work with that in order to display the name of the device (that's what it means in Spanish "dispositivo" :))?


    ----------------------------------------------
    UPDATE:
    Yes, it doesn't work.
    I've already tried, only a number appears (which could be device ID, or now that I think, the dropdown value).

    [​IMG]
    [​IMG]
     
    Last edited: Aug 6, 2019
  11. troester

    troester Well-Known Member Staff Member

    Level: Community
    yup, then dispositivo is an array of arrays, $foo[0][1] should show the label of the first device

    Gesendet von meinem SM-G930F mit Tapatalk
     
  12. mvr10

    mvr10 New Member

    Level: Community
    Ok, I'll try it and I'll let you know.

    --------------------------------------------
    UPDATE: nope, doesn't work. Blank space appears (the same as when I put a placeholder).
     
    Last edited: Aug 7, 2019
  13. mvr10

    mvr10 New Member

    Level: Community
    When PHP runs on getEndContent "dispositivos" appears as an ID, but onAfterProcess as "Array".
     
  14. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Yeah, getEndContent is run during page load, onAfterProcess is run during submission, and the data is different during those two phases.

    Just do a ...

    Code (Text):

    var_dump($yourVariableName); exit;
     
    ... in your PDF code to see what the data is during submit. If it's a dropdown in a repeat group, it'll be an array of arrays, because dropdowns submit as an array, and repeat element data is always an array.

    -- hugh
     
  15. mvr10

    mvr10 New Member

    Level: Community
    Hello Hugh,
    Sorry for the wait. I'm going to attach a screenshot of what appears to me as soon as I try to submit the form. What am I supposed to do with this information?
    Thanks
     

    Attached Files:

  16. troester

    troester Well-Known Member Staff Member

    Level: Community
    I took the time for testing:
    Creating a file in a php plugin running "onBeforeStore" and attaching it in the email plugin is definitively working, so concentrate on the data you'll get there.
    Create the file (including some debug output)
    Code (Text):
    $contents = "some text here";
    $myapp = JFactory::getApplication();
    $fileNameWithPath = JPATH_ROOT . "/images/atest.txt";
    if(file_put_contents($fileNameWithPath,$contents )){
      $myapp->enqueueMessage("File ". basename($fileNameWithPath ) ." was successfully created.");
    }
    else{
      $myapp->enqueueMessage("Failed to create file");
    }
    Append it in the email plugin
    Code (Text):
    $myapp = JFactory::getApplication();
    $myapp->enqueueMessage("email attach running");
    $files[] = 'images/atest.txt';
    return $files;
    To access the data you need in the php plugin see
    http://fabrikar.com/forums/index.php?wiki/php-form-plugin/#accessing-form-data
    There are a bunch of possibilities, use the one fitting best for you.


    To access your data in the email plugin see
    http://fabrikar.com/forums/index.php?wiki/email-form-plugin/#attachments
    and/or the tooltips in the plugin.
    Also for how to attach the file
    upload_2019-8-10_12-40-53.png
     
  17. mvr10

    mvr10 New Member

    Level: Community
    Ok, I understand, I'll try that.
    Thanks, I appreciate your effort!
    ----------------------------------------------------------------
    UPDATE: still no luck, very frustrating. Thanks y'all for trying, though ;). Focusing on other projects now, maybe I'll restart this one in a while.
     
    Last edited: Aug 12, 2019

Share This Page