Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_export_form_submission_logs.php
1 <?php
25 error_reporting(E_ALL);
26 if ((php_sapi_name() != 'cli')) {
27  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
28 }
29 
30 // Get the list of arguments
31 $args = $_SERVER['argv'];
32 
33 // Remove the first argument which is this file name
34 array_shift($args);
35 
36 // Get the Matrix System Root Directory
37 $MATRIX_ROOT_DIR = array_shift($args);
38 if (empty($MATRIX_ROOT_DIR)) {
39  print_usage("ERROR: You need to supply the path to the System Root as the first argument");
40 }
41 
42 if (!is_dir($MATRIX_ROOT_DIR) || !is_readable($MATRIX_ROOT_DIR.'/core/include/init.inc')) {
43  print_usage("ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.");
44 }
45 
46 require $MATRIX_ROOT_DIR.'/core/include/init.inc';
47 
48 // Set root user as current user
49 $root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
50 $GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user);
51 
52 // Get the Form asset ID
53 $form_assetid = array_shift($args);
54 if (empty($form_assetid)) {
55  print_usage("ERROR: You need to enter the Form asset ID as the second argument.");
56 }
57 
58 // Get Form asset
59 $form = $GLOBALS['SQ_SYSTEM']->am->getAsset($form_assetid, 'form_email', TRUE);
60 if (empty($form)) {
61  print_usage("ERROR: Invalid Form asset ID.");
62 }
63 
64 // Get from time
65 $from_time = array_shift($args);
66 if (empty($from_time)) {
67  print_usage("ERROR: You need to enter the From Time as the third argument.");
68 }
69 
70 $from_time = strtotime($from_time);
71 if ($from_time === FALSE) {
72  print_usage("ERROR: Invalid From Time.");
73 }
74 
75 // Get to time
76 $to_time = array_shift($args);
77 if (empty($to_time)) {
78  print_usage("ERROR: You need to enter the To Time as the forth argument.");
79 }
80 
81 $to_time = strtotime($to_time);
82 if ($to_time === FALSE) {
83  print_usage("ERROR: Invalid To Time.");
84 }
85 
86 // Get export file name
87 $export_file = array_shift($args);
88 
89 // Create CSV log file
90 $form_edit_fns = $form->getEditFns();
91 $csv = $form_edit_fns->createCSVSubmissionLogs($form, $from_time, $to_time);
92 
93 // If there is export file name specified, set it
94 if (!is_null($export_file)) {
95  $csv->setFilepath($export_file);
96 }
97 
98 // Print to screen or save to file
99 $csv->export(TRUE);
100 
101 // Restore current user
102 $GLOBALS['SQ_SYSTEM']->restoreCurrentUser();
103 
104 
105 /*-------------------------------FUNCTIONS--------------------------------*/
106 
107 
113 function print_usage($err_msg = '')
114 {
115  echo "$err_msg\n\n";
116  echo "Usage:\n";
117  echo "\tphp export_form_submission_logs.php MATRIX_ROOT_DIRECTORY FORM_ASSET_ID FROM_TIME T0_TIME [EXPORT_FILE]\n\n";
118  echo "\t\tMATRIX_ROOT_DIRECTORY The Matrix System Root directory.\n";
119  echo "\t\tFORM_ASSET_ID The asset ID of the Form asset. Note that this is not the Custom Form asset but the Form Contents asset under it.\n";
120  echo "\t\tFROM_TIME The start time of a specific period to get Form Submissions from. It can be a specific time string like 'Y-m-d H:i:s' or a relative one like 'yesterday H:i:s'. For more information on the accepted formats, refer at http://uk3.php.net/manual/en/datetime.formats.php\n";
121  echo "\t\tT0_TIME The end time of a specific period to get Form Submissions from. It has the same format as FROM_TIME.\n";
122  echo "\t\tEXPORT_FILE The file name or file path to write the export file to. If no file name is specified, the CSV export will be printed to the screen.\n";
123 
124  exit;
125 
126 }//end print_usage()
127 
128 
129 ?>