Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/ninja-fo.../includes/Database
File: CalderaSubmissionDataSource.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace NinjaForms\Includes\Database;
[2] Fix | Delete
[3] Fix | Delete
use NinjaForms\Includes\Contracts\SubmissionDataSource as ContractsSubmissionDataSource;
[4] Fix | Delete
use NinjaForms\Includes\Entities\SingleSubmission;
[5] Fix | Delete
use NinjaForms\Includes\Entities\SubmissionFilter;
[6] Fix | Delete
[7] Fix | Delete
use Caldera_Forms_Entry_Update;
[8] Fix | Delete
use Caldera_Forms_Entry_Bulk;
[9] Fix | Delete
use Caldera_Forms;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Retrieves a single Caldera Forms submission by its entry id
[13] Fix | Delete
*/
[14] Fix | Delete
class CalderaSubmissionDataSource implements ContractsSubmissionDataSource
[15] Fix | Delete
{
[16] Fix | Delete
[17] Fix | Delete
const TIMESTAMP_FORMAT = 'Y-m-d H:i:s';
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Identifier of where submission is stored
[21] Fix | Delete
*
[22] Fix | Delete
* @var string
[23] Fix | Delete
*/
[24] Fix | Delete
protected $dataSource = 'cf_form_entries';
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Collection of submissions keyed on Submission Record Id
[28] Fix | Delete
*
[29] Fix | Delete
* @var SingleSubmission[]
[30] Fix | Delete
*/
[31] Fix | Delete
protected $submissionCollection = [];
[32] Fix | Delete
[33] Fix | Delete
/** @var SubmissionFilter */
[34] Fix | Delete
protected $submissionFilter;
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Cross reference from NF Form Id to the CF form Id
[38] Fix | Delete
*
[39] Fix | Delete
* Array is in format:
[40] Fix | Delete
* [
[41] Fix | Delete
* nfFormId => cfFormId,
[42] Fix | Delete
* ex: '174' => 'CF60b547cfac102'
[43] Fix | Delete
* ]
[44] Fix | Delete
* @var array
[45] Fix | Delete
*/
[46] Fix | Delete
protected $formIdLookup = [];
[47] Fix | Delete
[48] Fix | Delete
/** @inheritDoc */
[49] Fix | Delete
public function retrieveSubmissionMeta(SubmissionFilter $submissionFilter): array
[50] Fix | Delete
{
[51] Fix | Delete
$this->submissionFilter = $submissionFilter;
[52] Fix | Delete
[53] Fix | Delete
if ([] !== $this->submissionFilter->getNfFormIds()) {
[54] Fix | Delete
[55] Fix | Delete
$formIdCollection = $this->submissionFilter->getNfFormIds();
[56] Fix | Delete
[57] Fix | Delete
$this->formId = $formIdCollection[0];
[58] Fix | Delete
[59] Fix | Delete
foreach ($formIdCollection as $nfFormId) {
[60] Fix | Delete
[61] Fix | Delete
$this->lookupCfFormIdByNfFormId($nfFormId);
[62] Fix | Delete
[63] Fix | Delete
if (!\is_null($this->formIdLookup[$nfFormId])) {
[64] Fix | Delete
$this->retrieveSubmissionMetaByCfFormId($this->formIdLookup[$nfFormId]);
[65] Fix | Delete
}
[66] Fix | Delete
}
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
if(''!==$this->submissionFilter->getSearchString()){
[70] Fix | Delete
$this->applySearchCriterion();
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
return $this->submissionCollection;
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/** @inheritDoc */
[77] Fix | Delete
public function retrieveSingleSubmission(SingleSubmission $singleSubmission): SingleSubmission{
[78] Fix | Delete
[79] Fix | Delete
$submissionRecordId = $singleSubmission->getSubmissionRecordId();
[80] Fix | Delete
[81] Fix | Delete
// Initialize submission collection b/c retrievals only happen on submissions in collection
[82] Fix | Delete
$this->submissionCollection[$submissionRecordId]=$singleSubmission;
[83] Fix | Delete
[84] Fix | Delete
// Populate values
[85] Fix | Delete
$singleSubmission = $this->retrieveSubmissionValues($singleSubmission);
[86] Fix | Delete
[87] Fix | Delete
return $singleSubmission;
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* Apply search string filter to submission collection
[92] Fix | Delete
*
[93] Fix | Delete
* Runs a WP Query to search for all post Ids with both a form Id from the
[94] Fix | Delete
* submission filter and also a search string in any meta value of the same
[95] Fix | Delete
* form. It then filters the submission collection to only those
[96] Fix | Delete
* submissions that meet these additional requirements.
[97] Fix | Delete
*
[98] Fix | Delete
* @return void
[99] Fix | Delete
*/
[100] Fix | Delete
protected function applySearchCriterion(): void
[101] Fix | Delete
{
[102] Fix | Delete
global $wpdb;
[103] Fix | Delete
[104] Fix | Delete
$entriesTable = $wpdb->prefix . 'cf_form_entries';
[105] Fix | Delete
$valuesTable = $wpdb->prefix . 'cf_form_entry_values';
[106] Fix | Delete
[107] Fix | Delete
$searchString = '%'.$this->submissionFilter->getSearchString() .'%';
[108] Fix | Delete
[109] Fix | Delete
/** @var array $searchCollection Array of submission Ids that match both form Id and search criterion */
[110] Fix | Delete
$searchCollection = [];
[111] Fix | Delete
[112] Fix | Delete
foreach ($this->submissionFilter->getNfFormIds() as $nfFormId) {
[113] Fix | Delete
[114] Fix | Delete
if (\is_null($this->formIdLookup[$nfFormId])) {
[115] Fix | Delete
continue;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
$cfFormId = $this->formIdLookup[$nfFormId];
[119] Fix | Delete
[120] Fix | Delete
$searchResultIds = $wpdb->get_results(
[121] Fix | Delete
$wpdb->prepare(
[122] Fix | Delete
"SELECT
[123] Fix | Delete
$entriesTable.id
[124] Fix | Delete
FROM
[125] Fix | Delete
$entriesTable
[126] Fix | Delete
INNER JOIN $valuesTable ON
[127] Fix | Delete
($entriesTable.id = $valuesTable.entry_id)
[128] Fix | Delete
WHERE
[129] Fix | Delete
$entriesTable.form_id = '%s' AND $valuesTable.value LIKE '%s'
[130] Fix | Delete
GROUP BY
[131] Fix | Delete
$entriesTable.id
[132] Fix | Delete
",
[133] Fix | Delete
$cfFormId,
[134] Fix | Delete
$searchString
[135] Fix | Delete
),
[136] Fix | Delete
ARRAY_A
[137] Fix | Delete
);
[138] Fix | Delete
[139] Fix | Delete
$submissionIds = \array_column($searchResultIds, 'id');
[140] Fix | Delete
[141] Fix | Delete
$searchCollection = array_merge($searchCollection,$submissionIds);
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
$this->submissionCollection = \array_intersect_key($this->submissionCollection, \array_flip($searchCollection));
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Adds CF lookup for given NF form id
[149] Fix | Delete
*
[150] Fix | Delete
* If given NF id lookup is not set, checks to see if there is a
[151] Fix | Delete
* corresponding CF form. If not, the value is set to `null` so that
[152] Fix | Delete
* the instance knows that the form id has been checked.
[153] Fix | Delete
*
[154] Fix | Delete
* @param string $nfFormId
[155] Fix | Delete
* @return void
[156] Fix | Delete
*/
[157] Fix | Delete
protected function lookupCfFormIdByNfFormId(string $nfFormId): void
[158] Fix | Delete
{
[159] Fix | Delete
if(isset($this->formIdLookup[$nfFormId])){
[160] Fix | Delete
return;
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
$form =Ninja_Forms()->form( $nfFormId )->get();
[164] Fix | Delete
[165] Fix | Delete
$cfFormId= $form->get_setting('key',null);
[166] Fix | Delete
[167] Fix | Delete
$this->formIdLookup[$nfFormId]=$cfFormId;
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
/** @inheritDoc */
[171] Fix | Delete
public function retrieveSubmissionValues(SingleSubmission $singleSubmission): SingleSubmission
[172] Fix | Delete
{
[173] Fix | Delete
$submissionRecordId = $singleSubmission->getSubmissionRecordId();
[174] Fix | Delete
[175] Fix | Delete
global $wpdb;
[176] Fix | Delete
[177] Fix | Delete
$table = $wpdb->prefix . 'cf_form_entry_values';
[178] Fix | Delete
[179] Fix | Delete
$r = $wpdb->get_results($wpdb->prepare("SELECT `slug`,`value` FROM $table WHERE `entry_id` = '%s'", $submissionRecordId), ARRAY_A);
[180] Fix | Delete
$cfSub = array_combine(array_column($r, 'slug'), array_column($r, 'value'));
[181] Fix | Delete
[182] Fix | Delete
$updatedFieldValueCollection = [];
[183] Fix | Delete
[184] Fix | Delete
foreach ($singleSubmission->getSubmissionFieldCollection() as $fieldSlug => $submissionField) {
[185] Fix | Delete
[186] Fix | Delete
$value = isset($cfSub[$fieldSlug]) ? $cfSub[$fieldSlug] : null;
[187] Fix | Delete
[188] Fix | Delete
$submissionField->setValue($value);
[189] Fix | Delete
[190] Fix | Delete
$updatedFieldValueCollection[$fieldSlug] = $submissionField;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
$singleSubmission->setSubmissionFieldCollection($updatedFieldValueCollection);
[194] Fix | Delete
[195] Fix | Delete
// only populate collection if submission is already present
[196] Fix | Delete
if (isset($this->submissionCollection[$submissionRecordId])) {
[197] Fix | Delete
$this->submissionCollection[$submissionRecordId] = $singleSubmission;
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return $singleSubmission;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/** @inheritDoc */
[204] Fix | Delete
public function deleteSubmission(SingleSubmission $singleSubmission): ContractsSubmissionDataSource
[205] Fix | Delete
{
[206] Fix | Delete
$submissionId = $singleSubmission->getSubmissionRecordId();
[207] Fix | Delete
$entry = Caldera_Forms::get_entry_detail( $submissionId );
[208] Fix | Delete
if($entry['status'] === "active"){
[209] Fix | Delete
Caldera_Forms_Entry_Update::update_entry_status( "trash", $submissionId);
[210] Fix | Delete
} else {
[211] Fix | Delete
Caldera_Forms_Entry_Bulk::delete_entries([$submissionId]);
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
return $this;
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
/** @inheritDoc */
[218] Fix | Delete
public function restoreSubmission(SingleSubmission $singleSubmission): ContractsSubmissionDataSource
[219] Fix | Delete
{
[220] Fix | Delete
$submissionId = $singleSubmission->getSubmissionRecordId();
[221] Fix | Delete
[222] Fix | Delete
Caldera_Forms_Entry_Update::update_entry_status( "active", $submissionId);
[223] Fix | Delete
[224] Fix | Delete
return $this;
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
/** @inheritDoc */
[228] Fix | Delete
public function updateSubmission(SingleSubmission $singleSubmission): ContractsSubmissionDataSource
[229] Fix | Delete
{
[230] Fix | Delete
// @TODO: Use CF API to delete submission
[231] Fix | Delete
return $this;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
/**
[235] Fix | Delete
* Retrieve submissions for a given CF form Id
[236] Fix | Delete
* @return void
[237] Fix | Delete
*/
[238] Fix | Delete
protected function retrieveSubmissionMetaByCfFormId(string $formId): void
[239] Fix | Delete
{
[240] Fix | Delete
global $wpdb;
[241] Fix | Delete
[242] Fix | Delete
$submissionRecordIdQuery = "select * from " . $wpdb->prefix . "cf_form_entries posts where form_id=%s";
[243] Fix | Delete
$args[] = $formId;
[244] Fix | Delete
[245] Fix | Delete
$userId = $this->submissionFilter->getUserId();
[246] Fix | Delete
[247] Fix | Delete
// Filter on post author as submitter's user id
[248] Fix | Delete
if (!\is_null($userId)) {
[249] Fix | Delete
[250] Fix | Delete
$submissionRecordIdQuery .= " AND user_id = %d";
[251] Fix | Delete
$args[]=$userId;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
$recordCollection = $wpdb->get_results($wpdb->prepare($submissionRecordIdQuery, $args));
[255] Fix | Delete
$statuses = $this->submissionFilter->getStatus();
[256] Fix | Delete
[257] Fix | Delete
foreach ($recordCollection as $queryObject) {
[258] Fix | Delete
//filter by status
[259] Fix | Delete
if( empty($statuses) || in_array( $queryObject->status, $statuses ) ){
[260] Fix | Delete
$submissionRecordId = $queryObject->id;
[261] Fix | Delete
$subDate = $queryObject->datestamp;
[262] Fix | Delete
$status = $queryObject->status;
[263] Fix | Delete
$submitterId = $queryObject->user_id;
[264] Fix | Delete
[265] Fix | Delete
$include = $this->includeByDateFilter($subDate);
[266] Fix | Delete
[267] Fix | Delete
if (!$include) {
[268] Fix | Delete
continue;
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
$this->submissionCollection[$submissionRecordId] = SingleSubmission::fromArray([
[272] Fix | Delete
'submissionRecordId' => $submissionRecordId,
[273] Fix | Delete
'timestamp' => $subDate,
[274] Fix | Delete
'formId' => $formId,
[275] Fix | Delete
'dataSource' => $this->dataSource,
[276] Fix | Delete
'status' => $status,
[277] Fix | Delete
'submitterId' => $submitterId
[278] Fix | Delete
]);
[279] Fix | Delete
}
[280] Fix | Delete
}
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
/**
[284] Fix | Delete
* Boolean to include as per date filter
[285] Fix | Delete
*
[286] Fix | Delete
* true=>include, false=>omit
[287] Fix | Delete
*
[288] Fix | Delete
* @return boolean
[289] Fix | Delete
*/
[290] Fix | Delete
protected function includeByDateFilter($subDate): bool
[291] Fix | Delete
{
[292] Fix | Delete
$include = true;
[293] Fix | Delete
[294] Fix | Delete
$startDateUnix = $this->submissionFilter->getStartDate();
[295] Fix | Delete
$startDate = (new \DateTime())->setTimestamp($startDateUnix)->format(self::TIMESTAMP_FORMAT);
[296] Fix | Delete
[297] Fix | Delete
$endDateUnix = $this->submissionFilter->getEndDate();
[298] Fix | Delete
$endDate = (new \DateTime())->setTimestamp($endDateUnix)->format(self::TIMESTAMP_FORMAT);
[299] Fix | Delete
[300] Fix | Delete
if ('' !== $startDate && $subDate < $startDate) {
[301] Fix | Delete
$include = false;
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
if ('' !== $endDate && $subDate > $endDate) {
[305] Fix | Delete
$include = false;
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
return $include;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
/** @inheritDoc */
[312] Fix | Delete
public function getDataSource(): string
[313] Fix | Delete
{
[314] Fix | Delete
return $this->dataSource;
[315] Fix | Delete
}
[316] Fix | Delete
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function