32 function getInfo($msg)
35 echo
"Usage: php system_integrity_form_submissions.php SYSTEM_ROOT [list|delete]\n\n";
36 echo
"\tSYSTEM_ROOT: The root path of Matrix system.\n";
37 echo
"\tlist|delete: List or delete the Form Submission assets associated with the wrong Form (ref. Bug #4119).\n";
38 echo
"\t Default value is 'list'.\n";
46 error_reporting(E_ALL);
47 if ((php_sapi_name() !=
'cli')) {
48 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
51 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
52 if (empty($SYSTEM_ROOT)) {
53 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
57 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
58 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
62 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
64 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
65 $am = $GLOBALS[
'SQ_SYSTEM']->am;
66 $root_user = $am->getSystemAsset(
'root_user');
67 $GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user);
70 $custom_forms = $am->getChildren(1,
'page_custom_form');
72 $delete = (isset($_SERVER[
'argv'][2]) && ($_SERVER[
'argv'][2] ==
'delete')) ? TRUE : FALSE;
75 echo
"\nThe following invalid Form Submissions will be deleted:\n";
77 echo
"\nThe following invalid Form Submissions will be deleted if you run this script with delete option:\n";
81 $total_form_submission_count = 0;
83 $invalid_form_submission_count = 0;
85 foreach ($custom_forms as $custom_form_id => $custom_form_details) {
87 if (!$am->assetInTrash($custom_form_id, TRUE)) {
88 $custom_form = $am->getAsset($custom_form_id);
89 if (is_null($custom_form)) {
90 echo
"Warning: the custom form #$custom_form_id does not exist\n";
95 $form = $custom_form->getForm();
97 echo
"Warning: the custom form #$custom_form_id does not have a Form asset under it\n";
102 $possible_quesion_id_prefixes = Array($form->id);
105 $section_links = $form->getSectionLinks();
106 foreach ($section_links as $link) {
107 $possible_quesion_id_prefixes[] = $link[
'minorid'];
111 $submissions_folder = $form->getSubmissionsFolder();
112 if (is_null($submissions_folder)) {
113 echo
"Warning: the custom form #$custom_form_id does not have a Submissions folder under it\n";
118 $form_submission_links = $am->getLinks($submissions_folder->id, SQ_LINK_TYPE_3,
'form_submission', TRUE);
119 foreach ($form_submission_links as $form_submission_link) {
120 $total_form_submission_count++;
121 echo
"Processing the form submission $total_form_submission_count...\n";
124 $form_submission_id = $form_submission_link[
'minorid'];
125 $form_submission = $am->getAsset($form_submission_id);
128 if (is_null($form_submission) || !($form_submission instanceof
Form_Submission)) {
129 echo
"Warning: the form submission asset #$form_submission_id does not exist\n";
133 $answers = $form_submission->getAnswers();
135 $in_right_place = FALSE;
139 if (!empty($answers)) {
140 foreach ($answers as $question_id => $answer) {
141 $question_id_parts = explode(
':', $question_id);
142 if (is_null($temp_qid)) {
143 $temp_qid = $question_id;
145 if (in_array($question_id_parts[0], $possible_quesion_id_prefixes)) {
146 $in_right_place = TRUE;
153 if (!$in_right_place) {
154 $invalid_form_submission_count++;
155 $submission_answer_str = is_null($temp_qid) ?
'There is no answer in the submission.' :
"The submission answers the question #$temp_qid which is not under the Form Contents #{$form->id}";
156 echo
"The form submission #$form_submission_id is in the wrong place under #{$submissions_folder->id}. $submission_answer_str\n";
160 echo
"The {$invalid_form_submission_count}th invalid form submission asset is being deleted...\n";
161 if ($am->deleteAssetLink($form_submission_link[
'linkid'], FALSE)) {
162 echo
"DELETED: The form submission #$form_submission_id is deleted from the Submissions folder #{$submissions_folder->id}\n";
164 echo
"ERROR: The form submission #$form_submission_id can not be deleted from the Submissions folder #{$submissions_folder->id}\n";
170 $am->forgetAsset($form_submission, TRUE);
175 $am->forgetAsset($submissions_folder, TRUE);
176 $am->forgetAsset($form, TRUE);
177 $am->forgetAsset($custom_form, TRUE);
185 echo
"\nThere are $invalid_form_submission_count invalid form submission assets in the total of $total_form_submission_count.\n";
188 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();