| Conditions | 21 |
| Paths | 800 |
| Total Lines | 125 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 101 | function export_pdf_with_html($headers_table, $data_table, $headers_pdf, $footers_pdf, $title_pdf) |
||
| 102 | { |
||
| 103 | $headers_in_pdf = '<img src="'.api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/images/header-logo.png">'; |
||
| 104 | |||
| 105 | if (is_array($headers_pdf)) { |
||
| 106 | // preparing headers pdf |
||
| 107 | $header = '<br/><br/> |
||
| 108 | <table width="100%" cellspacing="1" cellpadding="5" border="0" class="strong"> |
||
| 109 | <tr> |
||
| 110 | <td width="100%" style="text-align: center;" class="title" colspan="4"> |
||
| 111 | <h1>'.$title_pdf.'</h1></td></tr>'; |
||
| 112 | foreach ($headers_pdf as $header_pdf) { |
||
| 113 | if (!empty($header_pdf[0]) && !empty($header_pdf[1])) { |
||
| 114 | $header .= '<tr><td><strong>'.$header_pdf[0].'</strong> </td><td>'.$header_pdf[1].'</td></tr>'; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | $header .= '</table><br />'; |
||
| 118 | } |
||
| 119 | |||
| 120 | // preparing footer pdf |
||
| 121 | $footer = '<table width="100%" cellspacing="2" cellpadding="10" border="0">'; |
||
| 122 | if (is_array($footers_pdf)) { |
||
| 123 | $footer .= '<tr>'; |
||
| 124 | foreach ($footers_pdf as $foot_pdf) { |
||
| 125 | $footer .= '<td width="33%" style="text-align: center;">'.$foot_pdf.'</td>'; |
||
| 126 | } |
||
| 127 | $footer .= '</tr>'; |
||
| 128 | } |
||
| 129 | $footer .= '</table>'; |
||
| 130 | $footer .= '<div align="right" style="font-weight: bold;">{PAGENO}/{nb}</div>'; |
||
| 131 | |||
| 132 | // preparing content pdf |
||
| 133 | $css_file = api_get_path(SYS_CSS_PATH).'themes/'.api_get_setting('stylesheets').'/print.css'; |
||
| 134 | if (file_exists($css_file)) { |
||
| 135 | $css = @file_get_contents($css_file); |
||
| 136 | } else { |
||
| 137 | $css = ''; |
||
| 138 | } |
||
| 139 | $items_per_page = 30; |
||
| 140 | $count_pages = ceil(count($data_table) / $items_per_page); |
||
| 141 | for ($x = 0; $x < $count_pages; $x++) { |
||
| 142 | $content_table .= '<table width="100%" border="1" style="border-collapse:collapse">'; |
||
|
|
|||
| 143 | // header table |
||
| 144 | $content_table .= '<tr>'; |
||
| 145 | $i = 0; |
||
| 146 | if (is_array($headers_table)) { |
||
| 147 | foreach ($headers_table as $head_table) { |
||
| 148 | if (!empty($head_table[0])) { |
||
| 149 | $width = (!empty($head_table[1]) ? $head_table[1].'%' : ''); |
||
| 150 | $content_table .= '<th width="'.$width.'">'.$head_table[0].'</th>'; |
||
| 151 | $i++; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | $content_table .= '</tr>'; |
||
| 156 | // body table |
||
| 157 | |||
| 158 | if (is_array($data_table) && count($data_table) > 0) { |
||
| 159 | $offset = $x * $items_per_page; |
||
| 160 | $data_table = array_slice($data_table, $offset, count($data_table)); |
||
| 161 | $i = 1; |
||
| 162 | $item = $offset + 1; |
||
| 163 | foreach ($data_table as $data) { |
||
| 164 | $content_table .= '<tr>'; |
||
| 165 | $content_table .= '<td>'.($item < 10 ? '0'.$item : $item).'</td>'; |
||
| 166 | foreach ($data as $key => $content) { |
||
| 167 | if (isset($content)) { |
||
| 168 | $key == 1 ? $align = 'align="left"' : $align = 'align="center"'; |
||
| 169 | $content_table .= '<td '.$align.' style="padding:4px;" >'.$content.'</td>'; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | $content_table .= '</tr>'; |
||
| 173 | $i++; |
||
| 174 | $item++; |
||
| 175 | if ($i > $items_per_page) { |
||
| 176 | break; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } else { |
||
| 180 | $content_table .= '<tr colspan="'.$i.'"><td>'.get_lang('Empty').'</td></tr>'; |
||
| 181 | } |
||
| 182 | $content_table .= '</table>'; |
||
| 183 | if ($x < ($count_pages - 1)) { |
||
| 184 | $content_table .= '<pagebreak />'; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | $pdf = new PDF(); |
||
| 188 | $pdf->set_custom_footer($footer); |
||
| 189 | $pdf->set_custom_header($headers_in_pdf); |
||
| 190 | $pdf->content_to_pdf($header.$content_table, $css, $title_pdf); |
||
| 191 | exit; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Exports the data as a table on a PDF page. |
||
| 196 | * |
||
| 197 | * @param resource The PDF object (ezpdf class) used to generate the file |
||
| 198 | * @param array The data array |
||
| 199 | * @param array Table headers |
||
| 200 | * @param string Format (portrait or landscape) |
||
| 201 | */ |
||
| 202 | function export_pdf($pdf, $newarray, $header_names, $format) |
||
| 203 | { |
||
| 204 | $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm'); |
||
| 205 | $pdf->ezSetCmMargins(0, 0, 0, 0); |
||
| 206 | $pdf->ezSetY(($format == 'portrait') ? '820' : '570'); |
||
| 207 | $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm'); |
||
| 208 | if ($format == 'portrait') { |
||
| 209 | $pdf->line(40, 790, 540, 790); |
||
| 210 | $pdf->line(40, 40, 540, 40); |
||
| 211 | } else { |
||
| 212 | $pdf->line(40, 540, 790, 540); |
||
| 213 | $pdf->line(40, 40, 790, 40); |
||
| 214 | } |
||
| 215 | $pdf->ezSetY(($format == 'portrait') ? '750' : '520'); |
||
| 216 | $pdf->ezTable($newarray, $header_names, '', [ |
||
| 217 | 'showHeadings' => 1, |
||
| 218 | 'shaded' => 1, |
||
| 219 | 'showLines' => 1, |
||
| 220 | 'rowGap' => 3, |
||
| 221 | 'width' => (($format == 'portrait') ? '500' : '750'), |
||
| 222 | ]); |
||
| 223 | $pdf->ezStream(); |
||
| 224 | } |
||
| 225 |