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

scheduled task

Discussion in 'Community' started by mogy74, Oct 9, 2018.

  1. mogy74

    mogy74 New Member

    Level: Community
    Hi to all
    I need to run a scheduled task for update many row about a date controll
    (i need to check if a subcrition is valid or not)

    I try to do it with php plug in but after run update record are 0
    i write log but report just error 4
    i think proplem is in the update query but i realy can't understand where...
    this is the code
    $adesso = date('Y-m-d' , strtotime("now"));
    $non = 'Contratto Scaduto';
    $si = 'Contratto Valido';
    $mai = 'Contratto Non Presente';

    $db = JFactory::getDBO();
    $ids = JRequest::getVar( 'ids', array(), 'method', 'array' );
    foreach ($ids AS $id) {
    $row = $model->getRow($id);
    $scadenza = $row->anagrafica_collaboratori___data_fine_contratto;
    $scadenza = $scadenza->format('Y-m-d');


    $non = 'Contratto Scaduto';
    $si = 'Contratto Valido';
    $mai = 'Contratto Non Presente';
    //var_dump($controllo);
    if ($scadenza == 0){
    $query="UPDATE anagrafica_collaboratori SET stato_contratto = '$mai'";
    $db->setQuery($query);
    $db->query();

    }else{
    if ($adesso > $scadenza){
    $query="UPDATE anagrafica_collaboratori SET stato_contratto = '$non'";
    $db->setQuery($query);
    $db->query();
    }else{
    $query="UPDATE anagrafica_collaboratori SET stato_contratto = '$si'";
    $db->setQuery($query);
    $db->query();
    }
    }
    }

    I try to update 'stato_contratto' with different message stored in $non ,$si and $mai
    but seems don't work

    some one have did the same and know the solution?
    any help are welcome
    Thanks in advance
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Where did you find the code that you based this on? It's using JRequest, which means it's very old. Also it doesn't look like the code was designed to run in the cron plugin ... it's not setting up $model, and getRow() wouldn't be the right method to call. Also the date handling is broken, you can't cvall format() on a string. Etc etc.

    If you've selected a table in the PHP cron plugin settings, then you don't need to load any models, the list data is in $data.

    Something like this should work.

    Code (Text):

    $adesso = new DateTime();
    $non = 'Contratto Scaduto';
    $si = 'Contratto Valido';
    $mai = 'Contratto Non Presente';
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    foreach ($data as $group) {
       foreach ($group as $row) {
           // this assumes your Primary Key field is called 'id', change $row->id to the field name if it isn't
           $query->clear()
                ->update('anagrafica_collaboratori')
                ->where('id = ' . (int)$row->id);

          if (empty($scadenza){
             $query->set('stato_contratto = ' . $db->quote($mai));
          }
          else
          {
             $scadenza = new DateTime($row->anagrafica_collaboratori___data_fine_contratto);

             if ($adesso > $scadenza)
             {
                $query->set('stato_contratto = ' . $db->quote($non));
             }
             else
             {
                $query->set('stato_contratto = ' . $db->quote($si));
             }
          }
          $db->setQuery($query);
          $db->execute();
       }
    }
     
     
  3. mogy74

    mogy74 New Member

    Level: Community
    Hi , thanks for your help,
    yes you have right this code looks old , but was working in a php plugin (the app i build have almost 4 years), so i use it again for scheduled task,

    I try to run your code but the result is the same , i mean o row modify and the database is empty, i have the same error in log file in the table fabrik log
    4,syntax error, unexpected '{',/var/www/html/gestionale/plugins/fabrik_cron/php/php.php(75) : eval()'d code,15

    i don't know where
     
  4. mogy74

    mogy74 New Member

    Level: Community
    this simple query workin on mysql __> update anagrafica_collaboratore set stato_contratto = 'some word' where data_fine_contratto > now();
    I just to do the same
    just for be more clear
    Thanks
     
  5. troester

    troester Well-Known Member Staff Member

    Level: Community
    As the message says: there's a typo in line 15, a closing " )" is missing
    if (empty($scadenza)) {

    You can't use code 1:1 from a list or form php plugin in a cron php plugin, it's different surroundings.
     
  6. mogy74

    mogy74 New Member

    Level: Community
    thanks, i saw it and correct , but now i have another issue , result is the same and logs write 8,Undefined property: stdClass::$id
     
  7. troester

    troester Well-Known Member Staff Member

    Level: Community
    Is you PK named id?
     
  8. mogy74

    mogy74 New Member

    Level: Community
    yes it is the primary key in db is id ,
     
  9. mogy74

    mogy74 New Member

    Level: Community
    i try to do this task for fix the database result , i have a calc element in list who work very good but as you know work in list view and don't update database , for do it i have do open and save every record so i think the best way to do it is have a scheduled task
    maybe i did a wrong approach for this?
    Thanks for any help
     
  10. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Ooops, it should probably be $row->tablename___id (the full element name). Or you could try $row->__pk_val, which is something we should set to the PK val, used a lot in our code so we don't have to know the actual element name.
     
  11. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    And no, you didn't do it wrong. A calc will indeed only write to the database during form submission. So if you have a lot of rows that need the calc to be done, such that you don't want to have to edit and save each one, you either have to run a manual query in mysql, or a cron job.

    -- hugh
     
  12. mogy74

    mogy74 New Member

    Level: Community
    Thanks to all for help, i try to fix the code but i always return 0 record update and sintax error in log

    4,syntax error, unexpected ';',/var/www/html/gestionale/plugins/fabrik_element/calc/calc.php(371) : eval()'d code,1

    always unexpected ';'

    looks like i have some $ empty ,
    There is a way to dump variabiles in cron?

    i'm quite sure the problem is there
    Thanks for support me
     
  13. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    You won't be able to dump anything if there's a syntax error, as the code will error out before it can run the dump.

    The code I gave you wouldn't generate that error, so I presume you've modified it? Can you paste your code?

    In calc's, the usual cause of that error is if you aren't quoting a placeholder, and have something like ...

    $foo = {mytable___something};

    ... which will generate the "unexpected ;" is that element is empty. You need to wrap quotes around it, like ...

    $foo = '{mytable___something}';

    -- hugh
     
  14. mogy74

    mogy74 New Member

    Level: Community
    Hi , no i didn't modify the code , is the same you gave me, i'm cofused now about , we are working in cron tab so why i have a error in calc.php? the element i want to update in fact is a calc element in the list, but workin good,

    anyway this is the code i use now
    $adesso = new DateTime();
    $non = 'Contratto Scaduto';
    $si = 'Contratto Valido';
    $mai = 'Contratto Non Presente';
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    foreach ($data as $group) {
    foreach ($group as $row) {
    // this assumes your Primary Key field is called 'id', change $row->id to the field name if it isn't
    $query->clear()
    ->update('anagrafica_collaboratori')
    ->where('id = ' . (int)$row->anagrafica_collaboratori___id); //also i try without (int)

    if (empty($scadenza)){
    $query->set('stato_contratto = ' . $db->quote($mai));
    }
    else
    {
    $scadenza = new DateTime($row->anagrafica_collaboratori___data_fine_contratto);

    if ($adesso > $scadenza)
    {
    $query->set('stato_contratto = ' . $db->quote($non));
    }
    else
    {
    $query->set('stato_contratto = ' . $db->quote($si));
    }
    }
    $db->setQuery($query);
    $db->execute();
    }
    }

    i also try the same code without the if else condiction for semplyfy the query , i aslo try to let some $ empty for geerate a sql error and i saw that id is correct and also the query untile where condiction , i realy don't undersant
    but i wont surrender :)

    Thanks
     
  15. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Ah, so what is the code in your calc?

    -- hugh
     
  16. mogy74

    mogy74 New Member

    Level: Community
    Hi,
    this one ,
    $adesso = date('Y-m-d' , strtotime("now"));
    $scadenza = new DateTime('{anagrafica_collaboratori___data_fine_contratto}');
    $scadenza = $scadenza->format('Y-m-d');
    $controllo = '{anagrafica_collaboratori___data_fine_contratto}';
    $non = 'Contratto Scaduto';
    $si = 'Contratto Valido';
    $mai = 'Contratto Non Presente';
    //echo '<pre>';
    //print_r($data);
    //echo '</pre>';
    //exit;
    //var_dump($controllo);
    if ($controllo == 0){
    return $mai;

    }else{
    if ($adesso > $scadenza){
    return $non;
    }else{
    return $si;
    }
    }

    and working on save , i just need to do the same with crone tab
     
  17. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Is that the only calc?

    BTW, the reason the calc's run when you run the cron job is probably because you have "Calc on save only" set to No, so the calc(s) run when the list loads ... and running the cron job loads the list.

    -- hugh
     
  18. mogy74

    mogy74 New Member

    Level: Community
    No , i have several calc in my list , is the plug in i use most :) thanks for suggestion , so next step i think i have to unplubblish one by one all calc and find the one who give me problem, i will let you know

    have a good week end
     
  19. mogy74

    mogy74 New Member

    Level: Community
    with all clac unplublish still 0 record update and this warrning in log
    8192,mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead,/var/www/html/gestionale/libraries/joomla/database/driver/mysql.php,89
    :)
     

Share This Page