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

PHP form scripts

Discussion in 'Professional Support' started by bea, Jul 12, 2018.

  1. bea

    bea Active Member

    Level: Community
    Hi,
    I have two different scripts running on one form (1).
    1st script: insert a new record with onBeforeStore on table 1
    2nd script: inserts records with onAfterProcess on table 2 (depends on values on table 1)
    Require Once set both to YES

    The problem now:
    My first script updates the control field, if a record is already generated ('fab_cp1_line_tour___record') from 1 to 2 (new record generated)
    My 2nd script is watching ('fab_cp1_line_tour___record') and runs if value is 1. When this script runs, the value is already 2, because of 1st script and nothing happens

    To solve this, I've tried to combine insert script and updateFormData in 2nd script and stop updating 'record' from 1st script >> (Wiki). It doesn' work.
    Part of 2nd script:
    PHP:
    ...
    if($record[0] == 1 && $status[0] == 2 && $order == 3 && ($security_checks_col == '3' || $security_status[0] == 3)) {
    $query=$mydb->getQuery(true);
    $query->insert('fab_deviation_standard')
    ->set('parent_id = '.$mydb->quote($parent_id))
    ->set('machine = '.$mydb->quote($machine))
    ->set('pw = '.$mydb->quote($period))
    ->set('date_time = '.$mydb->quote($date))
    ->set('shift = '.$mydb->quote($shift))
    ->set('color = '.$mydb->quote($color))
    ->set('status_icon = '.$mydb->quote($security_status))
    ->set('target = '.$mydb->quote('100%'))
    ->set('indicator = '.$mydb->quote('Sicherheit'))
    ->set('deviation = '.$mydb->quote('Checks: '.$security_checks_pc.' --- '.'Beinaheunfälle: '.$security_nearmiss.' --- '.'Vorfälle: '.$security_incidents))
    ->set('comment = '.$mydb->quote($security_comment));
    $mydb->setQuery($query);
    $mydb->execute();
    $id=$mydb->insertid();
    $formModel->updateFormData('fab_cp1_line_tour___record', '2');
    }
    ...
    Hopefully I'm clear enough...
    Many thanks
    Bianka
     
  2. bggann

    bggann Member

    Level: Professional
    I'm afraid there is not enough context here. I'm not sure what you are trying to achieve. Can you do this by accessing the form data rather then reading and writing the db, or does the form depend on a record/table different than it is accessing. What's the goal.
     
  3. bea

    bea Active Member

    Level: Community
    Hi,
    I just want to update the element 'fab_cp1_line_tour___record' from '1' to '2' on table 1, when the second script run as true, to prevent double insert records into table 2.

    $formModel->updateFormData('fab_cp1_line_tour___record', '2'); at the end of insert query doesn't work.

    Thanks.
     
  4. troester

    troester Well-Known Member Staff Member

    Level: Community
    You can't update form data in onAfterProcess (there the data is already saved), you must update the database directly.
     
  5. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    As Troester says, onAfterProcess runs after all the form data has been processed and written to the database.

    When running onBeforeProcess, you can modify the submitted data with updateFormData(), and your changes will be written to the database. But when running onAfterProcess, you'll have to run your own update query.

    -- hugh
     
  6. bea

    bea Active Member

    Level: Community
    Hi troester & Hugh,
    I get a bit confused of calculation, scripts and validation... I've to sort the order.
    Both scripts are running like expected, but I have to clean up everything.

    Now I try to figure out, how to run a cron script to insert today() in a date field before the form will be edited.
    This will easier to get the other data with scripts into the form.

    Anyhow I need your help to check the files.

    Below my cron script, with error message: Call to a member function getElementData() on null
    Maybe you can give me hint to get this work. (PHP Code text area, Row Limit: 1, list order: DESC; shift and date are empty; status=1)
    PHP:
    $mydb= JFactory::getDbo();

    $shift=$formModel->getElementData('fab_cp1_line_tour___shift',true);
    $shift=is_array($shift) ? $shift[0] : $shift;
    $status= $formModel->getElementData('fab_cp1_line_tour___status',true);
    $status=is_array($status) ? $status[0] : $status;

    $date = date("Y-m-d");
    if ($shift[0] < '1' && $status[0] == '1')
    {
      $formModel->updateFormData('fab_cp1_line_tour___date', $date, true);
    }
     
    Cheers,
    Bianka
     
  7. bea

    bea Active Member

    Level: Community
    next morning :) ... I got it with some sample scripts from joomla.org
    If I run the script from backend, the message says: O updated, but 1 row was updated...
    It would be great to get more information about the PHP setup
    Cheers,
    Bianka



    PHP:
    $db = JFactory::getDbo();

    $query = $db->getQuery(true);
    $today=date('Y-m-d');

    // Fields to update.
    $fields = array(
        $db->quoteName('date') . ' = ' . $db->quote($today)
    );

    // Conditions for which records should be updated.
    $conditions = array(
        $db->quoteName('status') . ' = 1',
        $db->quoteName('shift') . ' is null'
    );

    $query->update($db->quoteName('fab_cp1_line_tour'))->set($fields)->where($conditions);

    $db->setQuery($query);

    $result = $db->execute();
     
  8. troester

    troester Well-Known Member Staff Member

    Level: Community
    Fabrik doesn't know what you are doing in a cron php script (you don't need to update anything, you may send emails, create files, whatever).
    So the standard message is "0 updated".

    But you can set the variable
    $processed
    to return (any integer).

    So you could add
    $processed = (int)$result;
    http://fabrikar.com/forums/index.php?wiki/php-common-tasks/#update
     
  9. bea

    bea Active Member

    Level: Community
    Hi troester,
    I get step by step into F3...

    I've managed to get 3 cron scripts running :). I need this scripts to get data before validations run on the form.
    The first script updates the date field and calculates the shift time. the second script needs information(shift, integer) from the first script, so I had to separate them.
    Today I can't think about it, to put them into one script... for the moment I'm happy.
    If you have time, would be great to have a look.
    Many thanks in advance.
    Bianka

    PHP:

    $db = JFactory::getDbo();
    // update Datum CP1
    $query = $db->getQuery(true);
    $today=date('Y-m-d');

    $fields = array(
        $db->quoteName('date') . ' = ' . $db->quote($today)
    );
    $conditions = array(
        $db->quoteName('status') . ' = 1',
        $db->quoteName('record') . ' = 1'
    );

    $query->update($db->quoteName('fab_cp1_line_tour'))->set($fields)->where($conditions);
    $db->setQuery($query);
    $result = $db->execute();


    // update Schicht CP1
    $now = strtotime(date('H:i'));
    $early = strtotime("04:00");
    $late = strtotime("12:00");
    $night = strtotime("20:00");

    if (($now >= $early) && ($now < $late)) {
      $db->setQuery("UPDATE fab_cp1_line_tour SET shift = " . $db->Quote('1') . " WHERE status = " . $db->Quote('1'));
      $db->execute();
      }
      if (($now >= $late) && ($now < $night)) {
      $db->setQuery("UPDATE fab_cp1_line_tour SET shift = " . $db->Quote('2') . " WHERE status = " . $db->Quote('1'));
      $db->execute();
      }
       if (($now >= $night) && ($now < $early)) {
      $db->setQuery("UPDATE fab_cp1_line_tour SET shift = " . $db->Quote('3') . " WHERE status = " . $db->Quote('1'));
      $db->execute();
      }
    PHP:
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $today = date('Y-m-d');
    $query
        ->select('COUNT(*)','shift')
        ->from($db->quoteName('fab_cp1_line_tour'))
        ->where($db->quoteName('date') ." = ".$db->quote($today))
        ->group($db->quoteName('shift'))
        ->order($db->quoteName('shift') . ' DESC');

    $db->setQuery($query);
    $count = $db->loadResult();

    if ($count == '1') {
      $db->setQuery("UPDATE fab_cp1_line_tour SET shiftorder = " . $db->Quote('1') . " WHERE status = " . $db->Quote('1'));
      $db->execute();
      }
     
    if ($count == '2')  {
      $db->setQuery("UPDATE fab_cp1_line_tour SET shiftorder = " . $db->Quote('2') . " WHERE status = " . $db->Quote('1'));
      $db->execute();
      }
    if ($count == '3') {
      $db->setQuery("UPDATE fab_cp1_line_tour SET shiftorder = " . $db->Quote('3') . " WHERE status = " . $db->Quote('1'));
      $db->execute();
      }
     
  10. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Well, you could just do the stuff in script 2 in script 1, after you've done the update. Pretty much just copy and paste it to the end of script 1, except you don't need to set up $db and $query again, just add ->clear() to the query builder chain.

    Code (Text):

    $today=date('Y-m-d');
    $query
       ->clear()
       ->select('COUNT(*)','shift')
       ->from($db->quoteName('fab_cp1_line_tour'))
       ->where($db->quoteName('date')." = ".$db->quote($today))
       ->group($db->quoteName('shift'))
       ->order($db->quoteName('shift').' DESC');

    // etc etc same same
     
     
  11. bea

    bea Active Member

    Level: Community
    Many thanks !
     
  12. bea

    bea Active Member

    Level: Community
    Hi Hugh,
    sorry I've to reopen the thread, because my night time didn't work:
    Script returns now Late instead of night
    $night = strtotime("20:00");

    var_dump:
    int(1531742400) $late 12:00
    int(1531771200) $night 20:00
    int(1531773780) $now ca.22:45

    Many thanks again -;)
     
    Last edited: Jul 16, 2018
  13. troester

    troester Well-Known Member Staff Member

    Level: Community
    (($now>=$night)&&($now<$early))
    is
    now >= 20:00 AND now < 4:00
    which can never be true

    I think it should be
    ( ($now>=$night) || ($now<$early) )
     
    Last edited: Jul 16, 2018
  14. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Well, I think your logic isn't right.

    You tests are:

    ($now>=$early)&&($now<$late)
    22:45 >= 04:00 ? yes
    22:45 < 12:00 ? No

    ($now>=$late)&&($now<$night)
    22:45 >= 12:00 ? Yes
    22:45 < 20:00 ? No

    ($now>=$night)&&($now<$early)
    22:45 >= 20:00 ? Yes
    22:45 < 04:00 ? No

    So none of those tests have both parts true, so none of them trigger.

    I suspect that you may need to create another one, for $early_tomorrow, like ...

    Code (Text):

    // add a 24 hour's worth of seconds to get early tomorrow
    $early_tomorrow = $early + (24 * 60 * 60);
     
    ... and the last test should be ...

    Code (Text):

    if(($now>=$night)&&($now<$early_tomorrow)){
     
    Although that's only if I'm understanding what you are trying to do.

    -- hugh
     
  15. bea

    bea Active Member

    Level: Community
    Before you wrote the post, I've tried with if (($now>=$night) ||($now<$early)) and for the moment this change was fine.
    But I took your $early_tomorrow and will check, if it works.
    thanks again ;)
     
  16. troester

    troester Well-Known Member Staff Member

    Level: Community
    (($now>=$night)&&($now<$early_tomorrow))
    won't do if $now < 4:00 (because it's not >= $night)
    You need OR, or you must add to $now, too or you can just do

    if () {
    shift1
    }
    elseif () {
    shift2
    }
    else { //if it's not 1 or 2 it must be 3
    shift3
    }
     
  17. bea

    bea Active Member

    Level: Community
    another night-mare... I've forgotten, that the date in the night is not $today.
    I have to think about the script again....

    $date = new DateTime('2014-01-03');
    $date->modify('-7 hours');
    $today = $date->format('Y-m-d');

    and it doesn't count correctly in the night :confused:
     
    Last edited: Jul 17, 2018

Share This Page