• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

scheduled task

mogy74

Member
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
 
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:
$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();
   }
}
 
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
 
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
 
As the message says: there's a typo in line 15, a closing " )" is missing
if (empty($scadenza)) {

but was working in a php plugin
You can't use code 1:1 from a list or form php plugin in a cron php plugin, it's different surroundings.
 
thanks, i saw it and correct , but now i have another issue , result is the same and logs write 8,Undefined property: stdClass::$id
 
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
 
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.
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
:)
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top