Calc option (UUID) + Javascript disable element

Discussion in 'Standard Support' started by jo-ka, Jan 11, 2018 at 5:34 PM.

  1. jo-ka

    jo-ka Member

    Level: Standard
    Hello.

    I need to find a way to use a calc element (ServiceOrderGUID), that will just do the calculation when the field was no previously filled and based on a yes/no element (watch element)

    Let me explain better:

    I will have a form where I will receive support tickets. In this form, I have a Yes/No element where the back-end user will decide if this ticket will generate a service order or not.

    So, if the ticket doesn't generate any service order (option 0 from Yes/No element), this calc field will be empty. If the element is set to Yes (option 1), then this calc will generate a new GUID on the Service Order table.

    Then, with this new calculated field, I will use the upsert table to input the new GUID on the Service Order table, which generate a link between the 2 forms.

    The problem is that the PK of the Service Order table is an GUID type field, it's not an traditional incremental ID field and I can't change this, because it's part of an ERP system, so my calc element is doing something like this, remember, watching ServiceOrderYesNo for AJAX update:

    if('{fab_tickets___ServiceOrderYesNo}' == 1)
    {
    $db = JFactory::getDbo();
    $db->setQuery('SELECT UUID( )');
    $uuid = $db->loadResult();
    return $uuid;
    }
    else {
    return null;
    }


    This works on new records (of course) and the UUID changes all the times the page is refreshed or the record is edited.

    I cant' find a way to have the calc returning the field value that was previously saved, once the YES/NO option is set to 1 (Yes), when editing the record or refreshing the page so I can guarantee that the GUID doesn't change.

    Also, but I think that I need to use some JS, I need to disable the YES/NO button, once the user set the option to YES, because once the Service Order is created in the other table, I can't loose the link between the 2 tables.

    Tried with JS option but couldn't do it also. My approach for JS was something like:

    On Load, if the ServiceOrderGUID field is not empty, then ServiceOrderYesNo is disabled.

    Didn't worked.

    Is my approach completely wrong? Is there a better way to do this?

    Thanks in advance.
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    I'd do both the UUID generation and upserting with a form submission plugin, running onAfterProcess, not a calc & upsert.

    Something like ...

    Code (Text):

    if ($formModel->formDataWithTableName['fab_tickets___ServiceOrderYesNo_raw'] === '1') {
       if (empty($formModel->formDataWithTableName['fab_tickets___uuid_raw'])) {
          $db = JFactory::getDbo();
          $query = $db->getQuery();
          $query->update('fab_tickets')->set('uuid = UUID()')->where('id = ' . (int)'{rowid}');
          $db->setQuery($query);
          // uncomment this one time for testing
          // var_dump((string)$query);exit;
          $db->execute();
          $query->clear()
             ->insert('fab_serviceorder AS s')
             ->leftJoin('fab_tickets AS t ON t.id = ' . (int)'{rowid}')
             ->set('s.uuid = t.uuid');
          $db->setQuery($query);
          // uncomment this one time for testing
          // var_dump((string)$query);exit;
          $db->execute();
       }
    }
     
    Obviously change table / field names to suit.

    If you need to do updating on edit to update rather than insert, add that as an 'else' on the empty() test.

    -- hugh
     
  3. Sophist

    Sophist Moderator Staff Member

    Level: Community
    Or in the calc, check whether the existing value of the calc element is blank and if it is create a new guid otherwise return the existing value.
     
  4. Sophist

    Sophist Moderator Staff Member

    Level: Community
    But I agree with Hugh in principle - if possible you should let the upsert create the gui and then store that in the calc field rather than let the calc field create the guid without checking that it is unique in the upsert field.
     
  5. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Just don't use a calc, period. Or an upsert. Do it all in one hand-rolled submission plugin.

    -- hugh
     
  6. jo-ka

    jo-ka Member

    Level: Standard
    Dear Hugh, thank's for your suggestion.

    I assume that you are suggesting to use the PHP form plugin, right?
    I've tried to use this code, OnAfterProcess , with the necessary changes, but I can't still not make it work. I really don't know why. Then I've tried a simple table update and also I couldn't make it work...

    I've the made this on other table, just for testing:

    $db = JFactory::getDbo();
    $query = $db->getQuery();
    $query->update('fab_tickets')->set('User = ' . $db->quote('23'))->where('id = ' . (int)'{rowid}');
    $db->setQuery($query);
    $db->execute();

    But also didn't worked... I have this error message: 1054 Unknown column 'enabled' in 'where clause'
    What am I doing wrong?

    Thank's in advance.
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    It's probably the missing 'true' arg to getQuery, which should be:

    Code (Text):

    $query = $db->getQuery(true);
     
    If you don't use that arg, you get the current query object cached in the database object, that has whatever query was last executed in it. The 'true' says "create a new query object". And obviously the last query has a where clause using 'enabled' in it.

    -- hugh
     
  8. jo-ka

    jo-ka Member

    Level: Standard
    OK, I'll try this.
    But I'm not sure I understand the quoted sentence. Can you explain me better please?
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    The second line in the code you quoted in your previous post is missing (true).

    Looks at the line I gave you, and compare it to your line.

    -- hugh
     

Share This Page