3 require_once 'CRM/Core/Page.php';
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');
10 class CRM_Lp_Page_Greeter
extends CRM_Core_Page
{
13 function getPdfs($dir) {
15 $files = scandir($dir);
17 foreach($files as $key => $file ) {
19 if(preg_match("/.*\.pdf/i", $file) === 0) {
28 return $this->getPdfs(DIR_SPOOL
);
32 return $this->getPdfs(DIR_STORE
);
41 function checkStatus($file) {
43 $status['status'] = $_SESSION['mode'] == 'history' ?
'Finished' : 'Not started';
45 if(file_exists(DIR_TEMP
.'/'.$file.'.dat')) {
46 $status['status'] = 'Running';
47 $dh = fopen(DIR_TEMP
.'/'.$file.'.dat', 'r');
49 $status['printer'] = strstr($data, '-', true);
50 $status['jid'] = trim(strstr($data, '-'), '-');
57 function getPrinters() {
58 $lph = popen(BIN_LPSTAT
.' -s', 'r');
62 while($data = fgets($lph)) {
64 $words = str_word_count($data, 1, '0123456789-_');
65 $output[$count-1] = $words[2];
74 function getPrinterSelect() {
75 $lph = popen(BIN_LPSTAT
.' -s', 'r');
78 $printers = $this->getPrinters();
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()) {
86 $output .= '<option value="'.$printer.'" '.$picked.'>'.$printer.'</option>';
90 $output .= '</select>';
91 $output .= '<noscript><input type="submit" value="Use"/></noscript>';
98 function getTable($data) {
100 $output .= '<table>';
101 $output .= '<tr><th>File</th><th>Status</th><th>Actions</th></tr>';
103 foreach($data as $item) {
104 $status = $this->checkStatus($item);
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>';
113 $output .= '</table>';
118 function getCurrentPrinter() {
119 return $_SESSION['printer'];
122 function printFile($file) {
123 $dir = $_SESSION['mode'] == 'history' ? DIR_STORE
: DIR_SPOOL
;
125 $lph = popen('lp -d '.$_SESSION['printer'].' '.$dir.'/'.$file, 'r');
128 $words = str_word_count($data, 1, '0123456789-_');
132 /* Create a status file */
133 $tfh = fopen(DIR_TEMP
.'/'.$file.'.dat', 'w');
134 fwrite($tfh, $jid."\n");
138 /* FIXME: Use civicrm session functions */
139 function initSettings() {
142 if(array_key_exists('printer', $_POST)) {
143 $_SESSION['printer'] = $_POST['printer'];
147 if(array_key_exists('file', $_GET)) {
148 $this->printFile($_GET['file']);
149 header('Location: /civicrm/lp-admin');
151 if(array_key_exists('mode', $_GET)) {
152 $_SESSION['mode'] = $_GET['mode'];
155 if(array_key_exists('gfile', $_GET)) {
156 header('Content-Type: application/pdf');
157 header('Content-Disposition: attachment; filename="'.$_GET['gfile'].'"');
159 if($_SESSION['mode'] == history
) {
160 echo file_get_contents(DIR_STORE
.'/'.$_GET['gfile']);
162 echo file_get_contents(DIR_SPOOL
.'/'.$_GET['gfile']);
164 // echo DIR_SPOOL.'/'.$_GET['gfile'];
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']);
176 if(file_exists(DIR_SPOOL
.'/'.$_GET['dfile'])) {
177 unlink(DIR_SPOOL
.'/'.$_GET['dfile']);
181 header('Location: /civicrm/lp-admin');
187 if(!array_key_exists('printer', $_SESSION)) {
188 $printers = $this->getPrinters();
189 $_SESSION['printer'] = $printers[0];
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'));
197 $this->initSettings();
199 if($_SESSION['mode'] == 'history') {
200 $spools = $this->getStore();
202 $spools = $this->getSpool();
205 $this->assign('printers', $this->getPrinterSelect());
207 $this->assign('datatable', $this->getTable($spools));