1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice
  2. If you update to Joomla 3.9.16, you will have to update Fabrik from the latest GitHub version, to fix a bug introduced in Joomla, which makes it impossible to list front end folder locations in backend JForms. This affects things like template selection.
    Dismiss Notice

Passing message through loop | 0 - syntax error, unexpected 'test' (T_STRING)

Discussion in 'Community' started by SoilentRed, Feb 13, 2020.

  1. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    I have a form with a textarea element. This element gets formatted and passed through a loop and inserted into a few rows. It works perfectly fine except when the submitter presses [Enter] to start a new line in their message. When the submitter has a new line in their message, I get the following error:

    Code (Text):
    0 - syntax error, unexpected 'test' (T_STRING)
    Here's is the OnAfterProcess php plugin code I'm running
    Code (Text):

    $myDb = JFactory::getDbo();
    $myQuery = $myDb->getQuery( true );
    $selection = '{sms___which_groups_raw}';
    $sels = explode( ",", $selection );
    $bad_symbols = array( ",", ".", "-", " " );
    $timezone = '{sms___tz}';
    $scheduledDateTime = $formModel->getElementData('sms___scheduled_datetime');
    $scheduledDateTime = new DateTime($scheduledDateTime);
    // $list = array();
    // start building the query ..
    $myQuery->insert( 'afab_smsq' );
    $myQuery->columns(
      array(
        $myDb->quoteName( 'date_time' ),
        $myDb->quoteName( 'f_name' ),
        $myDb->quoteName( 'l_name' ),
        $myDb->quoteName( 'full_name' ),
        $myDb->quoteName( 'message' ),
        $myDb->quoteName( 'scheduled_datetime' ),
        $myDb->quoteName( 'company' ),
        $myDb->quoteName( 'subject' ),
        $myDb->quoteName( 'agent_id' ),
        $myDb->quoteName( 'mobile' ),
        $myDb->quoteName( 'status' )
      )
    );
    foreach ( $sels as $sel ) {
    $scheduled = 'CONVERT_TZ(' . $myDb->quote($scheduledDateTime->format('Y-m-d H:i:s')) . ', "+0:00", "'.$timezone.'")';
      $insel = ( int )$sel;
      $myDb->setQuery( 'SELECT cell_phone, userid, name, first_name, last_name, organization FROM #__acymailing_subscriber JOIN #__acymailing_listsub ON #__acymailing_subscriber.subid = #__acymailing_listsub.subid WHERE #__acymailing_listsub.listid = ' . $insel . ' AND #__acymailing_subscriber.enabled = 1' );
      $rows = $myDb->loadObjectList();
      foreach ( $rows as $row ) {
        $cellphone = str_replace( $bad_symbols, "", $row->cell_phone );
        if ( empty( $cellphone ) ) {
        } else {

          $myQuery->values(
            implode( ',',
              array(
                $myDb->quote( '{sms___date_time}' ),
                $myDb->quote($row->first_name),
                $myDb->quote($row->last_name),
                $myDb->quote($row->name),
                $myDb->quote(html_entity_decode( '{sms___message}', ENT_QUOTES | ENT_XML1, 'UTF-8') ),
                $scheduled,
                $myDb->quote($row->organization),
                $myDb->quote( '{sms___subject}' ),
                $myDb->quote( '{sms___nia_agent_id}' ),
                $myDb->quote($cellphone),
                0
              )
            )
          );
        }
      }
    }
      // run the query
      $myDb->setQuery( $myQuery );
      // uncomment this line if you need to debug
      // var_dump($myQuery);exit;
      $myDb->execute();
     
    It looks like the message gets submitted successfully in all cases to the SMS table, but not the SMSQ table that it's supposed to loop through.

    I need to use the ENT_QUOTES decoder because it was converting all my special characters. for example, the "&" symbol was being converted to "&". But I also need to be able to insert a new line in the textarea without it breaking.

    Help and guidance are always valued.
     
  2. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    Friendly Bump.
    [​IMG]
     
  3. SoilentRed

    SoilentRed Caaan do!

    Level: Community
  4. SoilentRed

    SoilentRed Caaan do!

    Level: Community
  5. troester

    troester Well-Known Member Staff Member

    Level: Community
    There's no 'test' in your code, so the error is not related to it.
    Or you have some unbalanced quotes or some unquoted placeholder somewhere.
     
  6. lousyfool

    lousyfool Active Member

    Level: Community
    There are quirks in the code, at least in the query, and particularly for the columns: either you build the array outside the query and implode it inside the query, or you simply make it that comma-separated list of column names inside the query.
    Technically ok, but completely unnecessary: the values method. You're building an array, only to implode it immediately again - all on the same line. Why not make it just a comma-separated list to start with...
    https://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase

    There might be more, but once you've fixed the columns issue (and perhaps cleaned up and re-structured the code, also for better readability), you should be at least much closer to the finish line.
     
    Last edited: Mar 2, 2020
  7. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    @troester , 'test' was part of what was submitted in the textarea.
    something like
    Code (Text):
    test this is a test
    test i repeat this is a test.
    @lousyfool
    This route looks like it may be my only course of action. I was hoping it was really just a syntax thing I could nip in the butt. Thank you.
     
  8. troester

    troester Well-Known Member Staff Member

    Level: Community
    html_entity_decode( '{sms___message}', ENT_QUOTES | ENT_XML1, 'UTF-8')

    I would debug this outside your loop/query.... I don't know if your newline is \n\r or <br> or ... and what html_entity_decode is doing with it. Maybe you have to do some more string handling to get what you need.
     
  9. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    I'm noticing now that even when I put the element outside the loop it has the same behavior.
    Code (Text):
    $outsideLoop = '{sms___message}';
    when the element contains one line, for example:
    Code (Text):
    this is all on one line
    a vardump on $outsideLoop returns
    Code (Text):
    C:\wamp64\www\nia\plugins\fabrik_form\php\php.php(557) : eval()'d code:80:string 'this is all on one line' (length=23)
    good.
    BUT, when I have a message with multiple lines like
    Code (Text):
    this is all on
    two lines
    a vardump on $outsideLoop returns
    Code (Text):

    0 - syntax error, unexpected 'lines' (T_STRING)
    You may not be able to visit this page because of:

    [LIST=1]
    [*]an out-of-date bookmark/favourite
    [*]a search engine that has an out-of-date listing for this site
    [*]a mistyped address
    [*]you have no access to this page
    [*]The requested resource was not found.
    [*]An error has occurred while processing your request.
    [/LIST]
    Please try one of the following pages:

    [LIST]
    [*]Home Page
    [/LIST]
    If difficulties persist, please contact the System Administrator of this site and report the error below.
     
    Bad.
    Is it something about the way the textarea element is set up?

    I'm going to try to explode the new lines through a loop and insert them as a string. but the more I look at this the more I think something may be off with the element

    EDIT: attached are screencaps of my textarea setup
     

    Attached Files:

    Last edited: Mar 3, 2020
  10. troester

    troester Well-Known Member Staff Member

    Level: Community
  11. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    I need some help with this application as it is still wonky.
    Problem one: all the aforementioned issues with being unable to write multiple lines.
    Problem Two: When texts are passed through twilio, users are getting multiple messages.
    If anyone wants to throw in a bid, please let me know.
     

Share This Page