From 3083feae5e6398e9730e56c6a490e71d6b98c5e9 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Thu, 18 Mar 2021 15:07:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20create=20scheduled=20job=20on=20?= =?UTF-8?q?installation=20to=20trigger=20TwingleSync.sync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRM/TwingleCampaign/Upgrader.php | 97 +++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 3 deletions(-) diff --git a/CRM/TwingleCampaign/Upgrader.php b/CRM/TwingleCampaign/Upgrader.php index a2b5da2..f17783e 100644 --- a/CRM/TwingleCampaign/Upgrader.php +++ b/CRM/TwingleCampaign/Upgrader.php @@ -6,6 +6,7 @@ use CRM_TwingleCampaign_BAO_CustomGroup as CustomGroup; use CRM_TwingleCampaign_BAO_Configuration as Configuration; use CRM_TwingleCampaign_BAO_OptionValue as OptionValue; use CRM_TwingleCampaign_Utils_ExtensionCache as Cache; +use CRM_TwingleCampaign_ExtensionUtil as E; /** * Collection of upgrade steps. @@ -55,6 +56,30 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { $ov = new OptionValue($option_value); $ov->create(); } + + // setup cron job to trigger synchronization + try { + civicrm_api3('Job', 'create', [ + 'run_frequency' => "Hourly", + 'name' => "TwingleSync", + 'api_entity' => "TwingleSync", + 'api_action' => "sync", + 'description' => E::ts("Syncronizes all TwingleProjects an TwingleEvents between CiviCRM and Twingle"), + 'is_active' => 1, + ]); + } catch (CiviCRM_API3_Exception $e) { + Civi::log()->error( + E::LONG_NAME . + ' could not create scheduled job on extension installation: ' . + $e->getMessage() + ); + CRM_Core_Session::setStatus( + E::ts('Could not create scheduled job "TwingleSync".'), + E::ts('Scheduled Job'), + error + ); + CRM_Utils_System::setUFMessage(E::ts('Could not create scheduled job "TwingleSync". Your Campaigns will not get synchronized to Twingle.')); + } } /** @@ -93,21 +118,87 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { // Delete all settings for this extension Configuration::deleteAll(); + // Delete cron job + try { + $jobId = civicrm_api3('Job', 'getsingle', [ + 'name' => "TwingleSync", + ])['id']; + civicrm_api3('Job', 'delete', [ + 'id' => $jobId, + ]); + } catch (CiviCRM_API3_Exception $e) { + Civi::log()->error( + E::LONG_NAME . + ' could not delete scheduled job on extension uninstallation: ' . + $e->getMessage() + ); + CRM_Core_Session::setStatus( + E::ts('Could not delete scheduled job "TwingleSync".'), + E::ts('Scheduled Job'), + error + ); + } + } /** * @throws \Exception */ public function enable() { - + // Enable cron job + try { + $jobId = civicrm_api3('Job', 'getsingle', [ + 'name' => "TwingleSync", + ])['id']; + civicrm_api3('Job', 'create', [ + 'id' => $jobId, + 'is_active' => 1, + ]); + } catch (CiviCRM_API3_Exception $e) { + Civi::log()->error( + E::LONG_NAME . + ' could not activate scheduled job on extension activation: ' . + $e->getMessage() + ); + CRM_Core_Session::setStatus( + E::ts('Could not activate scheduled job "TwingleSync". Your Campaigns will not get synchronized to Twingle.'), + E::ts('Scheduled Job'), + error + ); + } } /** * @throws \Exception */ - public function disable() { + public function disable() { - } + // Disable cron job + try { + $jobId = civicrm_api3('Job', 'getsingle', [ + 'name' => 'TwingleSync', + ])['id']; + civicrm_api3('Job', 'create', [ + 'id' => $jobId, + 'is_active' => 0, + ]); + } catch (CiviCRM_API3_Exception $e) { + Civi::log()->error( + E::LONG_NAME . + ' could not disable scheduled job on extension deactivation: ' . + $e->getMessage() + ); + CRM_Core_Session::setStatus( + E::ts('Could not disable scheduled job "TwingleSync".'), + E::ts('Scheduled Job'), + error + ); + } + + // Remove Twingle api key from settings + Civi::settings()->revert('twingle_api_key'); + + } /** * Example: Run a couple simple queries.