added files yo
[civicrm-lp.git] / CRM / Lp / Page / Greeter.php
1 <?php
2
3 require_once 'CRM/Core/Page.php';
4
5 define('DIR_SPOOL', '/var/www/html/sites/default/files/civicrm/lp/spool');
6 define('DIR_STORE', '/var/www/html/sites/default/files/civicrm/lp/store');
7 define('DIR_TEMP', '/var/www/html/sites/default/files/civicrm/lp/tmp');
8 define('BIN_LPSTAT', '/usr/bin/lpstat');
9
10 class CRM_Lp_Page_Greeter extends CRM_Core_Page {
11
12
13 function getPdfs($dir) {
14
15 $files = scandir($dir);
16
17 foreach($files as $key => $file ) {
18
19 if(preg_match("/.*\.pdf/i", $file) === 0) {
20 unset($files[$key]);
21 }
22 }
23 return $files;
24 }
25
26
27 function getSpool() {
28 return $this->getPdfs(DIR_SPOOL);
29 }
30
31 function getStore() {
32 return $this->getPdfs(DIR_STORE);
33 }
34
35
36 function getAll() {
37 getSpool();
38 getDone();
39 }
40
41 function checkStatus($file) {
42
43 $status['status'] = $_SESSION['mode'] == 'history' ? 'Finished' : 'Not started';
44
45 if(file_exists(DIR_TEMP.'/'.$file.'.dat')) {
46 $status['status'] = 'Running';
47 $dh = fopen(DIR_TEMP.'/'.$file.'.dat', 'r');
48 $data = fgets($dh);
49 $status['printer'] = strstr($data, '-', true);
50 $status['jid'] = trim(strstr($data, '-'), '-');
51 fclose($dh);
52 }
53
54 return $status;
55 }
56
57 function getPrinters() {
58 $lph = popen(BIN_LPSTAT.' -s', 'r');
59
60 $count = 0;
61
62 while($data = fgets($lph)) {
63 if($count > 0) {
64 $words = str_word_count($data, 1, '0123456789-_');
65 $output[$count-1] = $words[2];
66 }
67 $count++;
68 }
69
70 pclose($lph);
71 return $output;
72 }
73
74 function getPrinterSelect() {
75 $lph = popen(BIN_LPSTAT.' -s', 'r');
76
77 $count = 0;
78 $printers = $this->getPrinters();
79
80 $output = '<form action="/civicrm/lp-admin" method="POST">';
81 $output .= 'Current Printer: <select name="printer" onchange=\'this.form.submit()\'>';
82 foreach($printers as $printer) {
83 if ($printer == $this->getCurrentPrinter()) {
84 $picked = 'selected';
85 }
86 $output .= '<option value="'.$printer.'" '.$picked.'>'.$printer.'</option>';
87 $picked = '';
88 }
89
90 $output .= '</select>';
91 $output .= '<noscript><input type="submit" value="Use"/></noscript>';
92 $output .= '</form>';
93
94 pclose($lph);
95 return $output;
96 }
97
98 function getTable($data) {
99 $output = '';
100 $output .= '<table>';
101 $output .= '<tr><th>File</th><th>Status</th><th>Actions</th></tr>';
102
103 foreach($data as $item) {
104 $status = $this->checkStatus($item);
105 $output .= '<tr>';
106 $output .= '<td><a href="/civicrm/lp-admin?gfile='.$item.'">'.$item.'</a></td><td>'.$status['status'];
107 if(array_key_exists('printer', $status)) $output .= ' ('.$status['printer'].' #'.$status['jid'].')';
108 $output .= '</td><td><a href="/civicrm/lp-admin?file='.$item.'">Print</a>';
109 $output .= ' |<a href="/civicrm/lp-admin?dfile='.$item.'"> Delete</a></td>';
110 $output .= '</tr>';
111 }
112
113 $output .= '</table>';
114
115 return $output;
116 }
117
118 function getCurrentPrinter() {
119 return $_SESSION['printer'];
120 }
121
122 function printFile($file) {
123 $dir = $_SESSION['mode'] == 'history' ? DIR_STORE : DIR_SPOOL;
124
125 $lph = popen('lp -d '.$_SESSION['printer'].' '.$dir.'/'.$file, 'r');
126 $data = fgets($lph);
127
128 $words = str_word_count($data, 1, '0123456789-_');
129 $jid = $words[3];
130 pclose($lph);
131
132 /* Create a status file */
133 $tfh = fopen(DIR_TEMP.'/'.$file.'.dat', 'w');
134 fwrite($tfh, $jid."\n");
135 fclose($tfh);
136 }
137
138 /* FIXME: Use civicrm session functions */
139 function initSettings() {
140
141 if(isset($_POST)) {
142 if(array_key_exists('printer', $_POST)) {
143 $_SESSION['printer'] = $_POST['printer'];
144 }
145 }
146 if(isset($_GET)) {
147 if(array_key_exists('file', $_GET)) {
148 $this->printFile($_GET['file']);
149 header('Location: /civicrm/lp-admin');
150 }
151 if(array_key_exists('mode', $_GET)) {
152 $_SESSION['mode'] = $_GET['mode'];
153 }
154
155 if(array_key_exists('gfile', $_GET)) {
156 header('Content-Type: application/pdf');
157 header('Content-Disposition: attachment; filename="'.$_GET['gfile'].'"');
158
159 if($_SESSION['mode'] == history) {
160 echo file_get_contents(DIR_STORE.'/'.$_GET['gfile']);
161 } else {
162 echo file_get_contents(DIR_SPOOL.'/'.$_GET['gfile']);
163 }
164 // echo DIR_SPOOL.'/'.$_GET['gfile'];
165 exit;
166 }
167
168
169 /* FIXME: Add confirmation page */
170 if(array_key_exists('dfile', $_GET)) {
171 if($_SESSION['mode'] == history) {
172 if(file_exists(DIR_STORE.'/'.$_GET['dfile'])) {
173 unlink(DIR_STORE.'/'.$_GET['dfile']);
174 }
175 } else {
176 if(file_exists(DIR_SPOOL.'/'.$_GET['dfile'])) {
177 unlink(DIR_SPOOL.'/'.$_GET['dfile']);
178 }
179 }
180
181 header('Location: /civicrm/lp-admin');
182
183 }
184
185
186 }
187 if(!array_key_exists('printer', $_SESSION)) {
188 $printers = $this->getPrinters();
189 $_SESSION['printer'] = $printers[0];
190 }
191 }
192
193 function run() {
194 // Example: Set the page-title dynamically; alternatively, declare a static title in xml/Menu/*.xml
195 CRM_Utils_System::setTitle(ts('LP-Admin Console'));
196
197 $this->initSettings();
198
199 if($_SESSION['mode'] == 'history') {
200 $spools = $this->getStore();
201 } else {
202 $spools = $this->getSpool();
203 }
204
205 $this->assign('printers', $this->getPrinterSelect());
206
207 $this->assign('datatable', $this->getTable($spools));
208
209 parent::run();
210 }
211 }