Commit fc4d488bbea9dfcc71c1fd4088b02d97c575be40
1 parent
2b9d886b
Added new printer class that will be used for all printing of pages
Committed By: Prince Mbekwa
Showing
1 changed file
with
155 additions
and
0 deletions
lib/util/printer.inc.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * $Id$ | ||
| 4 | + * | ||
| 5 | + * Small non-domain-specific utility functions | ||
| 6 | + * | ||
| 7 | + * KnowledgeTree Community Edition | ||
| 8 | + * Document Management Made Simple | ||
| 9 | + * Copyright (C) 2008, 2009 KnowledgeTree Inc. | ||
| 10 | + * | ||
| 11 | + * | ||
| 12 | + * This program is free software; you can redistribute it and/or modify it under | ||
| 13 | + * the terms of the GNU General Public License version 3 as published by the | ||
| 14 | + * Free Software Foundation. | ||
| 15 | + * | ||
| 16 | + * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 17 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 18 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 19 | + * details. | ||
| 20 | + * | ||
| 21 | + * You should have received a copy of the GNU General Public License | ||
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 23 | + * | ||
| 24 | + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, | ||
| 25 | + * California 94120-7775, or email info@knowledgetree.com. | ||
| 26 | + * | ||
| 27 | + * The interactive user interfaces in modified source and object code versions | ||
| 28 | + * of this program must display Appropriate Legal Notices, as required under | ||
| 29 | + * Section 5 of the GNU General Public License version 3. | ||
| 30 | + * | ||
| 31 | + * In accordance with Section 7(b) of the GNU General Public License version 3, | ||
| 32 | + * these Appropriate Legal Notices must retain the display of the "Powered by | ||
| 33 | + * KnowledgeTree" logo and retain the original copyright notice. If the display of the | ||
| 34 | + * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices | ||
| 35 | + * must display the words "Powered by KnowledgeTree" and retain the original | ||
| 36 | + * copyright notice. | ||
| 37 | + * Contributor( s): ______________________________________ | ||
| 38 | + */ | ||
| 39 | + | ||
| 40 | +/** | ||
| 41 | + * Class that creates printer friendly version of any referer page | ||
| 42 | + * Prints any content, and has ability to attach css styles to the printing context | ||
| 43 | + **/ | ||
| 44 | +class printer { | ||
| 45 | + /** | ||
| 46 | + * This is the context of the document to be printed | ||
| 47 | + * | ||
| 48 | + * @var string $_context | ||
| 49 | + */ | ||
| 50 | + var $_context = 0; | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * Prep string for line endings | ||
| 54 | + * | ||
| 55 | + * @param string $str | ||
| 56 | + * @return string | ||
| 57 | + */ | ||
| 58 | + | ||
| 59 | + private function _prepareString($str) | ||
| 60 | + { | ||
| 61 | + $str = str_replace("\r", '', $str); | ||
| 62 | + $str = str_replace("\n", '\n', $str); | ||
| 63 | + $str = str_replace("\047", "\\'", $str); | ||
| 64 | + $str = str_replace('"', '\"', $str); | ||
| 65 | + return $str; | ||
| 66 | + } | ||
| 67 | + /** | ||
| 68 | + * Referer type function call to get the body | ||
| 69 | + * of a document to be printed | ||
| 70 | + * | ||
| 71 | + * @param file $fname | ||
| 72 | + * @return string | ||
| 73 | + */ | ||
| 74 | + | ||
| 75 | + function readBody($fname) | ||
| 76 | + { | ||
| 77 | + $html = ''; | ||
| 78 | + ob_start(); | ||
| 79 | + if (@readfile($fname))$html = ob_get_contents(); | ||
| 80 | + | ||
| 81 | + ob_end_clean(); | ||
| 82 | + if (preg_match('#<body[^>]*>(.+?)</body>#is', $html, $matches)) { | ||
| 83 | + $html = $matches[1]; | ||
| 84 | + } | ||
| 85 | + return $html; | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + /** | ||
| 89 | + * Main context for printing. Consits of the Css for layout | ||
| 90 | + * and javascript for the print window | ||
| 91 | + * | ||
| 92 | + * @param string $printHtml | ||
| 93 | + * @param string $stylesheet | ||
| 94 | + * @return string context | ||
| 95 | + */ | ||
| 96 | + | ||
| 97 | + function addPrintContext($printHtml, $stylesheet = array()) | ||
| 98 | + { | ||
| 99 | + $this->_context += 1; | ||
| 100 | + $printHtml = $this->_prepareString($printHtml); | ||
| 101 | + echo '<!-- PRINTING CONTEXT ' . $this->_context . ' -->' . "\n"; | ||
| 102 | + echo '<style type="text/css">' . "\n"; | ||
| 103 | + echo '@media print {' . "\n"; | ||
| 104 | + echo ' #KTScreenOut' . $this->_context . ' { display: none; }' . "\n"; | ||
| 105 | + echo ' #print_ico { display: none; }' . "\n"; | ||
| 106 | + echo ' head title { display: none; }' . "\n"; | ||
| 107 | + foreach ($stylesheet as $k => $v) { | ||
| 108 | + echo ' ' . $k . ' { ' . $v . '}' . "\n"; | ||
| 109 | + } | ||
| 110 | + echo '}' . "\n"; | ||
| 111 | + echo '@media screen {' . "\n"; | ||
| 112 | + echo ' #KTPrintOut' . $this->_context . ' { display: none; }' . "\n"; | ||
| 113 | + echo '}' . "\n"; | ||
| 114 | + echo '</style>' . "\n"; | ||
| 115 | + echo '<script type="text/javascript" language="JavaScript">' . "\n"; | ||
| 116 | + echo 'function KT_GoPrint_' . $this->_context . '()' . "\n"; | ||
| 117 | + echo '{' . "\n"; | ||
| 118 | + echo ' document.body.innerHTML = \'<div id="KTScreenOut' . $this->_context . '">\' + document.body.innerHTML + \'<\/div>\';' . "\n"; | ||
| 119 | + echo ' document.body.innerHTML += \'<div id="KTPrintOut' . $this->_context . '">' . $printHtml . '<\/div>\';' . "\n"; | ||
| 120 | + echo ' window.print();' . "\n"; | ||
| 121 | + echo '}' . "\n"; | ||
| 122 | + echo '</script>' . "\n"; | ||
| 123 | + echo '<!-- END OF PRINTING CONTEXT ' . $this->_context . ' -->' . "\n"; | ||
| 124 | + return $this->_context; | ||
| 125 | + } | ||
| 126 | + /** | ||
| 127 | + * Add show print link on page | ||
| 128 | + * | ||
| 129 | + * @param string $context | ||
| 130 | + * @param string $linkText | ||
| 131 | + * @param string $attributes | ||
| 132 | + */ | ||
| 133 | + | ||
| 134 | + function showPrintLink($context, $linkText, $attributes = '') | ||
| 135 | + { | ||
| 136 | + $hostPath = KTUtil::kt_url() . '/resources/graphics/printer.gif'; | ||
| 137 | + | ||
| 138 | + echo '<a href="javascript:KT_GoPrint_' . $context . '()"' . (!empty($attributes) ? ' ' . $attributes : '') . '> | ||
| 139 | + <img src="'.$hostPath.'"align="left" title="'.$linkText.'" border="0" id="print_ico"/></a>'; | ||
| 140 | + } | ||
| 141 | + /** | ||
| 142 | + * Add show print button | ||
| 143 | + * | ||
| 144 | + * @param string $context | ||
| 145 | + * @param string $buttonText | ||
| 146 | + * @param string $attributes | ||
| 147 | + */ | ||
| 148 | + | ||
| 149 | + function showPrintButton($context, $buttonText, $attributes = '') | ||
| 150 | + { | ||
| 151 | + echo '<input type="button" value="' . $buttonText . '"' . (!empty($attributes) ? ' ' . $attributes : '') . ' onclick="KT_GoPrint_' . $context . '()" />'; | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | +} | ||
| 155 | +?> | ||
| 0 | \ No newline at end of file | 156 | \ No newline at end of file |