Skip to content

Instantly share code, notes, and snippets.

@pe7er
Last active Jul 10, 2018
Embed
What would you like to do?
Joomla PHP CLI script to remove old RSFormPro submissions from the RSFormPro database tables
<?php
/*
Joomla PHP CLI script to remove old form submissions in RSFormPro database tables
by Peter Martin, https://db8.nl (Twitter: @pe7er)
You can use RSFormPro in your Joomla website as form component to interact with your visitors.
It can be configured to send an email notification to the visitor and the site admin.
By default RSFormPro adds the submitted form data in the database.
It's nice to have the forum submission as backup in your database
but if you do not remove the old submissions, it will be stored in your database for ever.
One of the items in the European GDPR (General Data Protection Regulation) privacy laws
is that you should not store privacy related information (Name, address, email, phone) for an unlimited time.
More information about the GDPR and how you can comply: https://data2.eu
Use this script to remove old (by default older than 1 month) data from the RSFormPro tables.
You can change the period in the line that says:
$removeBeforeDate = $db->quote(Factory::getDate('now -1 month')->toSql());
Create a Cron Tab on the server to trigger this script every day.
Note: The script is AS IS. Use at your own risk.
This script is licensed under GNU General Public License version 2 or later.
*/
/**
* @package RSFormCleanUp.Cli
*
* @copyright Copyright 2018 Peter Martin, db8.nl. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
use Joomla\CMS\Application\CliApplication;
use Joomla\CMS\Factory;
/**
* A command line cron job to remove old submission data from RSFormPro database tables.
*/
// We are a valid entry point.
const _JEXEC = 1;
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';
// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';
/**
* @package Joomla.CLI
* @since 3.0
*/
class Db8RSFormClean extends JApplicationCli
{
/**
* Entry point for CLI script
*
* @return void
*
* @since 3.0
*/
public function doExecute()
{
// Database connector
$db = Factory::getDbo();
$removeBeforeDate = $db->quote(Factory::getDate('now -1 month')->toSql());
// Find all Submission IDs that need to get removed
$query = $db->getQuery(true);
$query
->select('SubmissionId')
->from($db->quoteName('#__rsform_submissions'))
->where($db->quoteName('DateSubmitted') . ' < (' . $removeBeforeDate . ')');
$db->setQuery($query);
$submissionIds = implode(',', $db->loadColumn());
// Check if there is data to be removed. If not, then quit.
if(!$submissionIds)
{
// if no submissions found, stop processing
$this->out('No data found that needed to be removed.');
exit;
}
// Remove all submitted data from #__rsform_submission_values
$query = $db->getQuery(true)
->delete($db->quoteName('#__rsform_submission_values'))
->where($db->quoteName('SubmissionId') . ' IN (' . $submissionIds . ')');
$db->setQuery($query);
$db->execute();
// Remove form submission info from #__rsform_submissions
$query = $db->getQuery(true)
->delete($db->quoteName('#__rsform_submissions'))
->where($db->quoteName('SubmissionId') . ' IN (' . $submissionIds . ')');
$db->setQuery($query);
$db->execute();
$this->out('The data from ' . $db->getAffectedRows() . ' submitted forms has been removed.');
}
}
// Instantiate the application object, passing the class name to CliApplication::getInstance
// and use chaining to execute the application.
CliApplication::getInstance('Db8RSFormClean')->execute();
@pe7er
Copy link
Author

pe7er commented May 4, 2018

If you want to use parameters to fine tune the removal period and specify forms,
check out this script from René Kreijveld:
https://gist.github.com/renekreijveld/a197d17c73a19a55880eb3068d5ab5c5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment