33 error_reporting(E_ALL);
34 if ((php_sapi_name() !=
'cli')) trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
36 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
37 if (empty($SYSTEM_ROOT)) {
38 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
42 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
43 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
47 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
51 if (count($argv) != 5) {
52 echo
'Usage: remove_form_submission.php <system_root> <custom_form_id> <from_date> <to_date>'.
"\n";
53 echo
'Date format: YYYY-MM-DD'.
"\n";
55 }
else if (preg_match(
'/^[0-9]{4}-[0-9]{2}-[0-9]{2}/',$argv[3]) != TRUE) {
57 echo
"ERROR: 'From date' must be in the format 'YYYY-MM-DD'\n";
59 }
else if (preg_match(
'/^[0-9]{4}-[0-9]{2}-[0-9]{2}/',$argv[4]) != TRUE) {
60 echo
"ERROR: 'To date' must be in the format 'YYYY-MM-DD'\n";
63 require_once SQ_FUDGE_PATH.
'/general/datetime.inc';
64 $from_value = iso8601_date_component($argv[3]).
' 00:00:00';
65 $to_value = iso8601_date_component($argv[4]).
' 23:59:59';
69 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
70 if (is_null($asset)) {
71 echo
"ERROR: #$assetid is not a valid asset ID";
74 if (!is_a($asset,
'page_custom_form')) {
75 echo
"ERROR: Asset #$assetid is not a custom form asset";
78 $form = $asset->getForm();
79 $sub_folder = $form->getSubmissionsFolder();
83 require_once SQ_FUDGE_PATH.
'/db_extras/db_extras.inc';
84 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
85 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
86 $db = $GLOBALS[
'SQ_SYSTEM']->db;
92 ('.SQ_TABLE_RUNNING_PREFIX.
'ast a
93 JOIN '.SQ_TABLE_RUNNING_PREFIX.
'ast_typ_inhd i ON a.type_code = i.type_code)
94 JOIN '.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk l
95 ON l.minorid = a.assetid';
96 $where =
'l.majorid IN (:assetid, :subfolder_assetid)
97 AND i.inhd_type_code = :inhd_type_code
100 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where,
'a');
101 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where,
'l');
102 $sql = $sql.$where.
' ORDER BY a.created DESC';
112 if (empty($assetids)) {
113 echo
"No form submission found for '$asset->name' (#$assetid) within the specified date range\n";
116 echo
'Found '.count($assetids).
" form submission(s) for '$asset->name' (#$assetid)\n(Date range: $from_value to $to_value)\n";
118 $unquoted_assetids = $assetids;
121 foreach ($assetids as $key => $assetid) {
126 $assetid_in = Array();
127 foreach (array_chunk($assetids, 999) as $chunk) {
128 $assetid_in[] =
' assetid IN ('.implode(
', ', $chunk).
')';
130 $in_assetid =
'('.implode(
' OR ', $assetid_in).
')';
132 $assetid_in = Array();
133 foreach (array_chunk($assetids, 999) as $chunk) {
134 $assetid_in[] =
' minorid IN ('.implode(
', ', $chunk).
')';
136 $in_minorid =
'('.implode(
' OR ', $assetid_in).
')';
138 $assetid_in = Array();
139 foreach (array_chunk($assetids, 999) as $chunk) {
140 $assetid_in[] =
' majorid IN ('.implode(
', ', $chunk).
')';
142 $in_majorid =
'('.implode(
' OR ', $assetid_in).
')';
145 echo
"Removing assets ...\n";
146 $sql =
'DELETE FROM sq_ast WHERE '.$in_assetid;
149 echo
"\tUpdating link table...\n";
150 $sql =
'DELETE FROM sq_ast_lnk WHERE '.$in_minorid;
153 echo
"\tUpdating link tree table ...\n";
154 $sql =
'DELETE FROM sq_ast_lnk_tree WHERE linkid NOT IN (SELECT linkid FROM sq_ast_lnk)';
156 $sql =
"UPDATE sq_ast_lnk_tree SET num_kids=num_kids-$delete_count WHERE linkid = (SELECT linkid FROM sq_ast_lnk WHERE minorid = '".$sub_folder->id.
"')";
159 echo
"\tUpdating attribute value table ...\n";
160 $sql =
'DELETE FROM sq_ast_attr_val WHERE '.$in_assetid;
163 echo
"\tUpdating metadata table ...\n";
164 $sql =
'DELETE FROM sq_ast_mdata WHERE '.$in_assetid;
167 echo
"\tUpdating metadata value table ...\n";
168 $sql =
'DELETE FROM sq_ast_mdata_val WHERE '.$in_assetid;
171 echo
"\tUpdating workflow table...\n";
172 $sql =
'DELETE FROM sq_ast_wflow WHERE '.$in_assetid;
175 echo
"\tUpdating permissions table...\n";
176 $sql =
'DELETE FROM sq_ast_perm WHERE '.$in_assetid;
179 echo
"\tUpdating roles table...\n";
180 $sql =
'DELETE FROM sq_ast_role WHERE '.$in_assetid;
183 echo
"\tUpdating shadow link table...\n";
184 $sql =
'DELETE FROM sq_shdw_ast_lnk WHERE '.$in_majorid;
187 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
188 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
190 echo
"\tDeleting asset data directories...\n";
191 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
192 foreach ($unquoted_assetids as $sub_assetid){
194 $data_path_suffix = asset_data_path_suffix(
'form_submission', $sub_assetid);
195 $data_path = SQ_DATA_PATH.
'/private/'.$data_path_suffix;
196 $data_path_public = SQ_DATA_PATH.
'/public/'.$data_path_suffix;
198 if (is_dir($data_path)) {
199 if (!delete_directory($data_path)) {
200 trigger_error(
"Could not delete private data directory for Form Submission (Id: #$assetid)", E_USER_WARNING);
204 if (is_dir($data_path_public)) {
205 if (!delete_directory($data_path_public)) {
206 trigger_error(
"Could not delete public data directory for Form Submission (Id: #$assetid)", E_USER_WARNING);