1

How to get $activity_id or $case_id variables in HOOKs when cron job execute in Joomla?

I have 2 functions in HOOK for CiviCase

  • function joomla_civicrm_tokens(&$tokens)
  • function joomla_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null)

1 - I can get variables for the tokens in Scheduled Reminders when I run a job manually.

enter image description here

2 - But if a server executed Cron Job i will get message: Failure, Error message: You have an error ...

I know it's happen because i get $case_id from joomla session $case_id = $session->get("case_id");

Could you explain me how to get $case_id or $activity_id from civicrm when cron job execute

Below you can find my HOOKs:

function joomla_civicrm_tokens(&$tokens) {
  if (isset($tokens['charter'])) {
    return;
  }

  $tokens['charter'] = array('charter.vardump' => 'vardump','charter.note2' => 'Note 2','charter.note' => 'Note','charter.cid' => 'CID','charter.caseid' => 'Case ID','charter.clientlastname' => 'Client Sur', 'charter.clientfirstname' => 'Client Name', 'charter.date_short' => 'Today\'s Date: dd/mm/yyyy' /*etc...*/ );

//  print_r($tokens);
}


function joomla_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
  // Date tokens
  if (!empty($tokens['charter'])) {
  
    $db = JFactory::getDBO();
    $session = JFactory::getSession();
    $case_id =  $session->get("case_id");
                

/*
    $sql = "SELECT * FROM  `civicrm_case_activity` WHERE `activity_id` = ".$activity_id;
    $db->setQuery($sql);
    $rows  = $db->loadObjectList();     
    foreach ($rows as $row)
    {
        $case_id =  $row->case_id;
    }

*/



    $sql = "SELECT * FROM  `civicrm_case_yachts` WHERE `case_id` = ".$case_id." AND `selected` = 1;";

    $db->setQuery($sql);
    $rows  = $db->loadObjectList();
        
    foreach ($rows as $row)
        {
    $id =  $row->id;
    $case_id =  $row->case_id;
    $case_id_forlink =  $row->case_id;
    $name =  $row->name;
    $model =  $row->model;
    $yachtname =  $row->name;
    $link =  $row->link;
    $order =  $row->order_number;
    $company =  $row->company;
    $email =  $row->email;
    $phone =  $row->phone;
    $selected =  $row->selected;
    $base =  $row->base;
    $address =  $row->address;
    $country =  $row->country;
    $days =  $row->days;
    $endofcharter =  date('d-m-Y', strtotime($row->endofcharter));
    $total =  $row->total;
    $firstpayment =  $row->firstpayment;
    $secondpayment =  $row->secondpayment;
    $extras =  $row->extras;
    $servicepack =  $row->servicepack;
    $deposit =  $row->deposit;
    $totaltocompany =  $row->totaltocompany;
    $firstpaymenttocompany =  $row->firstpaymenttocompany;      
    $secondpaymenttocompany =  $row->secondpaymenttocompany;    
    $ourcomission =  $row->ourcomission;    
    $agentcomission =  $row->agentcomission;
    $note =  $row->note;


}

    $sql ='SELECT start_date FROM  `civicrm_case` WHERE `id` = '.$case_id;
    $db->setQuery($sql);
//  $startofcharter = date('d-m-Y', strtotime($db->loadResult()));
    $startofcharter = date("d-m-Y", strtotime("-".$days." days", strtotime($endofcharter)));    


    $first_payment_date = date('d-m-Y', strtotime("+3 days"));
    $second_payment_date = date("d-m-Y", strtotime("-35 days", strtotime($startofcharter)));
    $totalplusextras = $total + $extras + $servicepack; 
    $firstpaymentandbankcomission = $firstpayment + 25;
    $secondpaymentandbankcomission = $secondpayment + 25;
    
    
    $sql ='SELECT co.*, ca.`case_id` as `case_id` FROM `civicrm_contact` co left join `civicrm_case_contact` ca on co.`id` = ca.`contact_id` WHERE case_id = '.$case_id;
    $db->setQuery($sql);
    $rows  = $db->loadObjectList();
        
    foreach ($rows as $row)
        {
        $firstname =  $row->first_name;
        $lastname =  $row->last_name;
    }


// get contact_id for to do case link
    $sql ='SELECT * FROM `civicrm_case_contact` WHERE case_id = '.$case_id;
    $db->setQuery($sql);
    $rows  = $db->loadObjectList();
        
    foreach ($rows as $row)
        {
        $cid =  $row->contact_id;

    }
    
    
// get note 2
    $sql ='SELECT * FROM  `civicrm_case` WHERE `id` = '.$case_id;
    $db->setQuery($sql);    
    $rows  = $db->loadObjectList();
        
    foreach ($rows as $row)
        {
            $note2 =  $row->details;
        }
    


    $db->disconnect();



$link = mysqli_connect("localhost", "user", "password", "DB") or die ("sorry");    
$sql = 'SELECT `course` FROM `__eb_currencies` WHERE `id`=2';

$euro = mysqli_query($link, $sql);
$euro_1 = mysqli_fetch_assoc($euro);
$euro_final0 = round($euro_1['course'] + $euro_1['course']*2/100, 2);
mysqli_close($link);  

$totalrub_first = round($euro_final0 * $firstpaymentandbankcomission, 0);
$totalrub_second = round($euro_final0 * $secondpaymentandbankcomission, 0);
  
    $date = array('charter.vardump' => $vardump,'charter.cid' => $cid,'charter.caseid' => $case_id_forlink,'charter.clientlastname' => $lastname, 'charter.clientfirstname' => $firstname, 'charter.date_short' => date('d-m-Y'), 'charter.yachtmodel' => $model, 'charter.yachtname' => $yachtname, 'charter.numbercontract' => $order, 'charter.startdate' => $startofcharter, 'charter.enddate' => $endofcharter, 'charter.company' => $company, 'charter.companyemail' => $email, 'charter.companyphone' => $phone, 'charter.companyaddress' => $address, 'charter.country' => $country, 'charter.base' => $base, 'charter.days' => $days, 'charter.total' => $total, 'charter.firstpayment' => $firstpayment, 'charter.secondpayment' => $secondpayment, 'charter.extras' => $extras, 'charter.servicepack' => $servicepack, 'charter.deposit' => $deposit, 'charter.firstpaymentdate' => $first_payment_date, 'charter.secondpaymentdate' => $second_payment_date, 'charter.totalplusextras' => $totalplusextras, 'charter.paymentpluscomission' => $firstpaymentandbankcomission, 'charter.secondpaymentpluscomission' => $secondpaymentandbankcomission, 'charter.totalrubfirst' => $totalrub_first, 'charter.totalrubsecond' => $totalrub_second, 'charter.totaltocompany' => $totaltocompany, 'charter.firsttocompany' => $firstpaymenttocompany, 'charter.secondtocompany' => $secondpaymenttocompany, 'charter.ourcomission' => $ourcomission,'charter.note' => $note,'charter.note2' => $note2, 'charter.agentcomission' => $agentcomission
    );
    foreach ($cids as $cid) {
      $values[$cid] = empty($values[$cid]) ? $date : $values[$cid] + $date;
    }
  }
}
2
  • How are you running cron? If running as the anonymous user joomla might not initialize a session.
    – Demerit
    Sep 20, 2021 at 14:01
  • Like this: wget --no-check-certificate -O - -q -t 1 '<cron-url>?name=<username>&pass=<password>&key=<site-key>' Sep 21, 2021 at 10:57

1 Answer 1

0

If you want the tokens only in the context of a schedule reminder, you should use the new better way to do tokens instead : TokenProcessor.

hook_civicrm_tokenValues is only working for Contact entity. You can't really add tokens on other entities like CiviCase using this hook.

If you need the token to work in every contexts (transactional email, mass mailings, schedule reminders, Print/merge document) then you might consider the following workaround.

If you are sure you can find the proper case_id for the current contact, for example if it's the latest created or the only one of a certain type, then you could do something like :

function example_civicrm_tokens() {
  $tokens['mytokens'] = [
    'mytokens.casetoken' => E::ts('Token related to a Case'),
  ];
}

function example_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {

  foreach ($cids as $cid) {
    $result = civicrm_api3('Case', 'get', [
      'sequential' => 1,
      'contact_id' => $cid,
      'case_type_id' => ...
    ]);
    if ($result['count'] == 1) {
      $case_id = $result['values'][0]['id'];
      // do your stuff now that you have a $case_id for the current contact
      $values[$cid]['mytokens.casetoken'] = ...
    }
    else {
      // case_id is either non existent or gives several results which is not workable
      // let's keep token empty
      $values[$cid]['mytokens.casetoken'] = '';
    }
  }

}
2
  • Thank you very much for your reply and advice about TokenProcessor. As well thanks for code, but I'm not sure if I can find the right case_id for the current contact, because each contact can have many of the same cases by type and creation time. Maybe you have an idea how to get the current activity_id which was executed by Cron Job... For me will be easy to resolve the thing when i'll get this variable. Sep 20, 2021 at 22:32
  • No sorry, only contact_id is provided using hook_civicirm_tokenValues. You really need to use TokenProcessor in that case.
    – samuelsov
    Sep 22, 2021 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.