Skip to content

Commit 1b8dccb

Browse files
committed
GP-36357: Resolve merge conflicts.
2 parents daaa4b7 + 3bbe871 commit 1b8dccb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1770
-556
lines changed

CRM/Sqltasks/Action/CSVExport.php

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626
class CRM_Sqltasks_Action_CSVExport extends CRM_Sqltasks_Action {
2727
use CRM_Sqltasks_Action_EmailActionTrait;
28+
use CRM_Sqltasks_Action_SftpTrait;
2829

2930
/**
3031
* Types of CSV field enclosure
@@ -102,23 +103,6 @@ public static function getEncodingOptions() {
102103
return $encodings;
103104
}
104105

105-
/**
106-
* Parse the credentials
107-
* @return FALSE if nothing is entered, 'ERROR' if it cannot be parsed
108-
*/
109-
protected function getCredentials() {
110-
$credentials = $this->getConfigValue('upload');
111-
if (!empty($credentials)) {
112-
$credentials = trim($credentials);
113-
if (preg_match('#^sftp:\/\/(?<user>[^:]+):(?<password>[^@]+)@(?<host>[\w.-]+)(?<remote_path>\/[\/\w_-]+)$#', $credentials, $match)) {
114-
return $match;
115-
} else {
116-
return 'ERROR';
117-
}
118-
}
119-
return FALSE;
120-
}
121-
122106
/**
123107
* get the table with the contact_id column
124108
*/
@@ -367,35 +351,13 @@ public function execute() {
367351

368352
// 3) UPLOAD
369353
if ($this->getConfigValue('upload')) {
370-
$credentials = $this->getCredentials();
371-
if ($credentials && $credentials != 'ERROR') {
372-
define('NET_SFTP_LOGGING', NET_SFTP_LOG_SIMPLE);
373-
// connect
374-
if (stream_resolve_include_path('Net/SFTP.php') === FALSE) {
375-
$sftp = new phpseclib\Net\SFTP($credentials['host']);
376-
$mode = phpseclib\Net\SFTP::SOURCE_LOCAL_FILE;
377-
}
378-
else {
379-
// used for legacy versions of phpseclib
380-
require_once('Net/SFTP.php');
381-
$sftp = new Net_SFTP($credentials['host']);
382-
$mode = NET_SFTP_LOCAL_FILE;
383-
}
384-
if (!$sftp->login($credentials['user'], $credentials['password'])) {
385-
throw new Exception("Login to {$credentials['user']}@{$credentials['host']} Failed", 1);
386-
}
387-
388-
// upload
389-
$target_file = $credentials['remote_path'] . '/' . $filename;
390-
if (!$sftp->put($target_file, $filepath, $mode)) {
391-
throw new Exception("Upload to {$credentials['user']}@{$credentials['host']} failed: " . $sftp->getSFTPLog(), 1);
392-
}
393-
394-
$this->log("Uploaded file '{$filename}' to {$credentials['host']}/{$target_file}");
395-
396-
} else {
397-
throw new Exception("Upload failed, couldn't parse credentials", 1);
398-
}
354+
$this->retrySftp(
355+
function() use ($filename, $filepath) {
356+
return $this->uploadSftp($filename, $filepath);
357+
},
358+
Civi::settings()->get("sqltasks_sftp_max_retries"),
359+
Civi::settings()->get("sqltasks_sftp_retry_initial_wait")
360+
);
399361
}
400362
}
401363
}

CRM/Sqltasks/Action/CallTask.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ public function execute() {
5252

5353
$tasks = $this->getConfigValue('tasks');
5454
$categories = $this->getConfigValue('categories');
55+
$isExecuteDisabledTasks = $this->getConfigValue('is_execute_disabled_tasks') == 1;
5556
if (empty($tasks) && empty($categories)) {
5657
return;
5758
}
5859

5960
// generate query for task selection
60-
$query = "SELECT * FROM `civicrm_sqltasks` WHERE enabled=1 AND ";
61+
$query = "SELECT * FROM `civicrm_sqltasks` WHERE ";
62+
$query .= " archive_date IS NULL AND ";
63+
if (!$isExecuteDisabledTasks) {
64+
$query .= " enabled=1 AND ";
65+
}
66+
6167
$or_clauses = array();
6268
if (!empty($tasks)) {
6369
$or_clauses[] = '`id` IN (' . implode(',', $tasks) . ')';
@@ -78,7 +84,8 @@ public function execute() {
7884
continue;
7985
}
8086
// all good: execute!
81-
$logs = $task->execute();
87+
$taskExecutionResult = $task->execute();
88+
$logs = $taskExecutionResult['logs'];
8289
// log results from child task
8390
foreach ($logs as $log) {
8491
$this->log($log);

CRM/Sqltasks/Action/EmailActionTrait.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,26 @@ public function sendEmailMessage(array $params) {
2828
$to_email = explode(',', $to_email);
2929
}
3030

31-
foreach ($to_email as $email) {
32-
$email = trim($email);
33-
civicrm_api3('MessageTemplate', 'send', array_merge(
34-
$templateParams,
35-
['to_email' => $email],
36-
));
37-
$this->log("Sent {$this->id} message to '{$email}'");
31+
if (count($to_email) > 0) {
32+
$toEmail = '';
33+
$ccEmails = [];
34+
$emailNumber = 1;
35+
36+
foreach ($to_email as $email) {
37+
if ($emailNumber === 1) {
38+
$toEmail = trim($email);
39+
} else {
40+
$ccEmails[] = trim($email);
41+
}
42+
$emailNumber++;
43+
}
44+
45+
civicrm_api3('MessageTemplate', 'send', array_merge($templateParams, [
46+
'cc' => implode(',', $ccEmails),
47+
'to_email' => $toEmail
48+
]));
49+
50+
$this->log("Sent {$this->id} message to '{$email}' with cc: " . implode(',', $ccEmails) . ".");
3851
}
3952
}
4053

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
use CRM_Sqltasks_ExtensionUtil as E;
4+
5+
/**
6+
* This actions allows you to get value from specified db table
7+
*
8+
*/
9+
class CRM_Sqltasks_Action_ReturnValue extends CRM_Sqltasks_Action {
10+
11+
const API_RESULT_COLUMN = 'value';
12+
13+
/**
14+
* ReturnValue Key
15+
*/
16+
public $return_key;
17+
18+
/**
19+
* ReturnValue Value
20+
*/
21+
public $return_value;
22+
23+
/**
24+
* Get identifier string
25+
*/
26+
public function getID() {
27+
return 'return-value';
28+
}
29+
30+
/**
31+
* Get a human readable name
32+
*/
33+
public function getName() {
34+
return E::ts('Return Value');
35+
}
36+
37+
/**
38+
* Get default template order
39+
*
40+
* @return int
41+
*/
42+
public function getDefaultOrder() {
43+
return 1000;
44+
}
45+
46+
/**
47+
* Whether this action should be included in the template for new tasks
48+
*
49+
* @return bool
50+
*/
51+
public static function isDefaultTemplateAction() {
52+
return FALSE;
53+
}
54+
55+
/**
56+
* Get the table with "API_RESULT_COLUMN" column
57+
*/
58+
protected function getDataTable() {
59+
$table_name = $this->getConfigValue('table');
60+
return trim($table_name);
61+
}
62+
63+
/**
64+
* Check if this action is configured correctly
65+
*/
66+
public function checkConfiguration() {
67+
parent::checkConfiguration();
68+
69+
$data_table = $this->getDataTable();
70+
if (empty($data_table)) {
71+
throw new Exception("Data table not configured.", 1);
72+
}
73+
74+
// check if table exists
75+
$existing_table = CRM_Core_DAO::singleValueQuery("SHOW TABLES LIKE '{$data_table}';");
76+
if (!$existing_table) {
77+
throw new Exception("Table '{$data_table}' doesn't exist.", 1);
78+
}
79+
80+
// check if parameter is set
81+
$parameter = $this->getConfigValue('parameter');
82+
if (empty($parameter)) {
83+
throw new Exception("Parameter not set", 1);
84+
}
85+
86+
$reserved_parameters = ['log', 'files', 'runtime'];
87+
if (in_array($parameter, $reserved_parameters)) {
88+
throw new Exception("Parameter's name use reserved word", 1);
89+
}
90+
}
91+
92+
/**
93+
* RUN this action
94+
*
95+
* @throws \Exception
96+
*/
97+
public function execute() {
98+
$data_table = $this->getDataTable();
99+
$query = CRM_Core_DAO::executeQuery("SELECT `". self::API_RESULT_COLUMN ."` FROM `{$data_table}`");
100+
101+
$this->return_key = $this->getConfigValue('parameter');
102+
$this->return_value = $query->fetchValue();
103+
104+
$this->log("Set return value as '{$this->return_key}' => '{$this->return_value}'");
105+
}
106+
107+
}

CRM/Sqltasks/Action/SegmentationExport.php

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
class CRM_Sqltasks_Action_SegmentationExport extends CRM_Sqltasks_Action {
2525
use CRM_Sqltasks_Action_EmailActionTrait;
26+
use CRM_Sqltasks_Action_SftpTrait;
2627

2728
/**
2829
* Get identifier string
@@ -62,23 +63,6 @@ protected function getAllTemplates() {
6263
return $template_options;
6364
}
6465

65-
/**
66-
* Parse the credentials
67-
* @return FALSE if nothing is entered, 'ERROR' if it cannot be parsed
68-
*/
69-
protected function getCredentials() {
70-
$credentials = $this->getConfigValue('upload');
71-
if (!empty($credentials)) {
72-
$credentials = trim($credentials);
73-
if (preg_match('#^sftp:\/\/(?<user>[^:]+):(?<password>[^@]+)@(?<host>[\w.-]+)(?<remote_path>\/[\/\w_-]+)$#', $credentials, $match)) {
74-
return $match;
75-
} else {
76-
return 'ERROR';
77-
}
78-
}
79-
return FALSE;
80-
}
81-
8266
/**
8367
* get the preferred filename
8468
*/
@@ -203,6 +187,7 @@ public function checkConfiguration() {
203187

204188
/**
205189
* RUN this action
190+
* @throws Exception
206191
*/
207192
public function execute() {
208193
// get some basic data
@@ -314,35 +299,13 @@ public function execute() {
314299

315300
// PROCESS 2: UPLOAD
316301
if ($this->getConfigValue('upload')) {
317-
$credentials = $this->getCredentials();
318-
if ($credentials && $credentials != 'ERROR') {
319-
define('NET_SFTP_LOGGING', NET_SFTP_LOG_SIMPLE);
320-
// connect
321-
if (stream_resolve_include_path('Net/SFTP.php') === FALSE) {
322-
$sftp = new phpseclib\Net\SFTP($credentials['host']);
323-
$mode = phpseclib\Net\SFTP::SOURCE_LOCAL_FILE;
324-
}
325-
else {
326-
// used for legacy versions of phpseclib
327-
require_once('Net/SFTP.php');
328-
$sftp = new Net_SFTP($credentials['host']);
329-
$mode = NET_SFTP_LOCAL_FILE;
330-
}
331-
if (!$sftp->login($credentials['user'], $credentials['password'])) {
332-
throw new Exception("Login to {$credentials['user']}@{$credentials['host']} Failed", 1);
333-
}
334-
335-
// upload
336-
$target_file = $credentials['remote_path'] . '/' . $filename;
337-
if (!$sftp->put($target_file, $filepath, $mode)) {
338-
throw new Exception("Upload to {$credentials['user']}@{$credentials['host']} failed: " . $sftp->getSFTPLog(), 1);
339-
}
340-
341-
$this->log("Uploaded file '{$filename}' to {$credentials['host']}/{$target_file}");
342-
343-
} else {
344-
throw new Exception("Upload failed, couldn't parse credentials", 1);
345-
}
302+
$this->retrySftp(
303+
function() use ($filename, $filepath) {
304+
return $this->uploadSftp($filename, $filepath);
305+
},
306+
Civi::settings()->get("sqltasks_sftp_max_retries"),
307+
Civi::settings()->get("sqltasks_sftp_retry_initial_wait")
308+
);
346309
}
347310
}
348311

0 commit comments

Comments
 (0)