Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScheduledJob - Clean up form code & improve validation #26879

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions CRM/Admin/Form/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* Class for configuring jobs.
*/
class CRM_Admin_Form_Job extends CRM_Admin_Form {
public $_id = NULL;

/**
* @var bool
Expand Down Expand Up @@ -146,23 +145,20 @@ public function buildQuickForm($check = FALSE) {
* @throws CRM_Core_Exception
*/
public static function formRule($fields) {

$errors = [];

require_once 'api/api.php';

/** @var \Civi\API\Kernel $apiKernel */
$apiKernel = \Civi::service('civi_api_kernel');
$apiRequest = \Civi\API\Request::create($fields['api_entity'], $fields['api_action'], ['version' => 3]);
try {
$apiParams = CRM_Core_BAO_Job::parseParameters($fields['parameters']);
/** @var \Civi\API\Kernel $apiKernel */
$apiKernel = \Civi::service('civi_api_kernel');
$apiRequest = \Civi\API\Request::create($fields['api_entity'], $fields['api_action'], $apiParams);
$apiKernel->resolve($apiRequest);
}
catch (\Civi\API\Exception\NotImplementedException $e) {
$errors['api_action'] = ts('Given API command is not defined.');
}

if (!empty($errors)) {
return $errors;
catch (CRM_Core_Exception $e) {
$errors['parameters'] = ts('Parameters must be formatted as key=value on separate lines');
}

return empty($errors) ? TRUE : $errors;
Expand Down Expand Up @@ -249,7 +245,7 @@ public function postProcess() {
$dao->api_entity = $values['api_entity'];
$dao->api_action = $values['api_action'];
$dao->description = $values['description'];
$dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
$dao->is_active = $values['is_active'] ?? 0;

// CRM-17686
$ts = strtotime($values['scheduled_run_date']);
Expand Down
4 changes: 0 additions & 4 deletions CRM/Admin/Form/PaymentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,6 @@ public static function formRule($fields) {
$errors['_qf_default'] = ts('You must have at least the test or live section filled');
}

if (!empty($errors)) {
return $errors;
}

return empty($errors) ? TRUE : $errors;
}

Expand Down
21 changes: 21 additions & 0 deletions CRM/Core/BAO/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,25 @@ public static function copy($id, $params = []) {
return $copy;
}

/**
* Parse multi-line `$parameters` string into an array
*
* @param string|null $parameters
* @return array
* @throws CRM_Core_Exception
*/
public static function parseParameters(?string $parameters): array {
$result = ['version' => 3];
$lines = $parameters ? explode("\n", $parameters) : [];

foreach ($lines as $line) {
$pair = explode("=", $line);
if ($pair === FALSE || count($pair) !== 2 || !trim($pair[0]) || trim($pair[1]) === '') {
throw new CRM_Core_Exception('Malformed API parameters in scheduled job');
}
$result[trim($pair[0])] = trim($pair[1]);
}
return $result;
}

}
44 changes: 20 additions & 24 deletions CRM/Core/ScheduledJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@
*/
class CRM_Core_ScheduledJob {

/**
* @var int
* @deprecated
*/
public $version = 3;

/**
* @var int
*/
public $id;

public $name = NULL;

/**
* @var string
*/
public $parameters = '';

public $apiParams = [];

public $remarks = [];
Expand All @@ -30,31 +44,16 @@ class CRM_Core_ScheduledJob {
* @param array $params
*/
public function __construct($params) {
// Fixme - setting undeclared class properties!
foreach ($params as $name => $param) {
$this->$name = $param;
}

// version is set to 3 by default - if different number
// defined in params, it's replaced later on, however,
// it's practically useles, since it seems none of api v2
// will work properly in cron job setup. It might become
// useful when/if api v4 starts to emerge and will need
// testing in the cron job setup. To permanenty require
// hardcoded api version, it's enough to move below line
// under following if block.
$this->apiParams = ['version' => $this->version];

if (!empty($this->parameters)) {
$lines = explode("\n", $this->parameters);

foreach ($lines as $line) {
$pair = explode("=", $line);
if ($pair === FALSE || count($pair) != 2 || trim($pair[0]) == '' || trim($pair[1]) == '') {
$this->remarks[] .= 'Malformed parameters!';
break;
}
$this->apiParams[trim($pair[0])] = trim($pair[1]);
}
try {
$this->apiParams = CRM_Core_BAO_Job::parseParameters($this->parameters);
}
catch (CRM_Core_Exception $e) {
$this->remarks[] = $e->getMessage();
}
}

Expand Down Expand Up @@ -137,7 +136,4 @@ public function needsRunning() {
return ($now >= $nextTime);
}

public function __destruct() {
}

}