CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
reportprintpage.php675 linesDownload Raw Back to classes
1<?php2class ReportPrintPage extends ReportPage3{4	public $pageWidth = PDF_PAGE_WIDTH;5	public $pageHeight = PDF_PAGE_HEIGHT;6 7	public $splitAtServer = false;8	public $splitByGroups = 0;9	public $pages = array();10 11	public $arrPages = array();12 13	/**14	 *15	 */16	public $pdfFitToPage = 1;17 18	/**19	 *20	 */21	public $landscape = 0;22 23	/**24	 *25	 */26	public $isDetail = false;27 28	public $isReportEmpty = false;29 30	public $multipleDetails = false;31	32	/**33	 *34	 */35	function __construct(&$params)36	{37		parent::__construct($params);38 39		$this->jsSettings['tableSettings'][ $this->tName ]['reportType'] = $this->crossTable ? 1 : 0;40 41		if(	$this->pdfMode )42		{43			//	pdf mode44			if( $this->pSet->getReportPrintPDFGroupsPerPage() != 0 )45			{46				$this->splitAtServer = true;47				$this->splitByGroups = $this->pSet->getReportPrintPDFGroupsPerPage();48			}49 50		}51		else if( $this->format == "excel" || $this->format == "word")52		{53			//	export mode54			$this->splitAtServer = false;55			$this->splitByGroups = 0;56		}57		else if( !$this->pdfJsonMode() )58		{59			//	print mode60			$this->splitAtServer = true;61			if( !$this->splitByGroups )62				$this->splitByGroups = $this->pSet->getReportPrintGroupsPerPage();63			$this->pageData["printRecords"] = $this->splitByGroups;64 65		} else if ( $this->pdfJsonMode() ) {66			$this->splitAtServer = true;67			if( !$this->splitByGroups )68				$this->splitByGroups = 100000;69		}70 71		if( $this->isDetail )72		{73			$this->splitAtServer = false;74			$this->splitByGroups = 0;75		}76 77		//	Before Process event78		if( $this->eventsObject->exists("BeforeProcessReportPrint") )79			$this->eventsObject->BeforeProcessReportPrint( $this );80 81 82		if( isRTL() )83			$this->jsSettings['tableSettings'][ $this->tName ]['isRTL'] = true;84 85		$this->jsSettings['tableSettings'][ $this->tName ]['reportPrintPartitionType'] = 1;86		$this->jsSettings['tableSettings'][ $this->tName ]['reportPrintGroupsPerPage'] = $this->pSet->getReportPrintGroupsPerPage();87		$this->jsSettings['tableSettings'][ $this->tName ]['reportPrintLayout'] = $this->pSet->getReportLayout();88 89		$this->jsSettings['tableSettings'][ $this->tName ]['printerPagePDF'] = $this->pSet->isPrinterPagePDF();90 91		$this->jsSettings['tableSettings'][ $this->tName ]['printerPageOrientation'] = $this->pSet->getPrinterPageOrientation();92		$this->jsSettings['tableSettings'][ $this->tName ]['printerPageScale'] = $this->pSet->getPrinterPageScale();93		$this->jsSettings['tableSettings'][ $this->tName ]['isPrinterPageFitToPage'] = $this->pSet->isPrinterPageFitToPage();94 95		$this->jsSettings['tableSettings'][ $this->tName ]['printerSplitRecords'] = $this->pSet->getReportPrintGroupsPerPage();96 97		$this->jsSettings['tableSettings'][ $this->tName ]['printerPDFSplitRecords'] = $this->pSet->getReportPrintPDFGroupsPerPage();98		99		if ( $this->crossTable ) 100			$this->pageData["crossParams"] = $this->getCurrentCrossParams();		101	}102 103	/**104	 *105	 */106	public function assignPDFFormatSettings()107	{108		if( !$this->pdfMode )109			return;110 111		$this->landscape = $this->pSet->isLandscapePrinterPagePDFOrientation();112		$this->pdfFitToPage = $this->crossTable ? 1 : $this->pSet->isPrinterPagePDFFitToPage();113 114		$this->pageWidth = PDF_PAGE_WIDTH;115		$this->pageHeight = PDF_PAGE_HEIGHT;116 117		if( !$this->pdfFitToPage )118		{119			$PrinterPagePDFScale = $this->pSet->getPrinterPagePDFScale();120			$this->pageWidth = $this->pageWidth * 100 / $PrinterPagePDFScale;121			$this->pageHeight = $this->pageHeight * 100 / $PrinterPagePDFScale;122		}123 124		$this->jsSettings['tableSettings'][ $this->tName ]['pdfPrinterPageOrientation'] = $this->pSet->isLandscapePrinterPagePDFOrientation();125		$this->jsSettings['tableSettings'][ $this->tName ]['printerPageOrientation'] = $this->landscape;126		$this->jsSettings['tableSettings'][ $this->tName ]['createPdf'] = 1;127		$this->jsSettings['tableSettings'][ $this->tName ]['pdfFitToPage'] = $this->pdfFitToPage;128 129		if( $this->landscape )130		{131			$temp = $this->pageWidth;132			$this->pageWidth = $this->pageHeight;133			$this->pageHeight = $temp;134		}135 136		$this->jsSettings['tableSettings'][ $this->tName ]['pageWidth'] = $this->pageWidth;137		$this->jsSettings['tableSettings'][ $this->tName ]['pageHeight'] = $this->pageHeight;138	}139 140	/**141	 * @return Array142	 */143	public function getExtraReportParams()144	{145		$extraParams = parent::getExtraReportParams();146		147		if( $this->format == "excel" )148			$extraParams['forExport'] = "excel";149		elseif( $this->format == "word" )150			$extraParams['forExport'] = "word";151		else152			$extraParams['forExport'] = false;153		154		if( !$this->crossTable )155			$extraParams['mode'] = MODE_PRINT;156 157		return $extraParams;158	}159 160	/**161	 *162	 */163	public function process()164	{165		global $cCharset;166		167		$this->displayMasterTableInfo();168		169		$this->assignPDFFormatSettings();170 171		$forExport = false;172		if( $this->format == "excel" )173		{174			$forExport = "excel";175			header("Content-Type: application/vnd.ms-excel");176			header("Content-Disposition: attachment;Filename=".$this->shortTableName.".xls");177			echo "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";178			echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";179		}180		else if( $this->format == "word" )181		{182			$forExport = "word";183			header("Content-Type: application/vnd.ms-word");184			header("Content-Disposition: attachment;Filename=".$this->shortTableName.".doc");185			echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";186		}187 188		$this->doCommonAssignments();189		190		// the table info params191		$extraParams = $this->getExtraReportParams();	192		$this->setGoogleMapsParams( $extraParams['fieldsArr'] );193		$this->fillAdvancedMapData();194 195		if( $this->pdfJsonMode() ) {196			$this->assignTotalsDefaults();197		}198		RunnerContext::pushSearchContext( $this->searchClauseObj );199		$this->setReportData( $extraParams );200 201		// add button events if exist202		$this->addButtonHandlers();203		$this->addCommonJs();204 205		$this->showPage();206	}207	208	/**209	 *210	 */211	public function doCommonAssignments()212	{213		$this->assignBody();214		215		$this->xt->assign("stylesheetlink", strlen( $this->format ) > 0);216		217		foreach( $this->pSet->getFieldsList() as $fName ) 218		{219			$this->xt->assign( $fName."_fieldheader", true );220		}221 222		if( $this->format )223		{224			$this->xt->assign("pdflink_block", false);225		}226		else if( $this->crossTable )227		{228			$this->xt->assign("pdflink_block", $this->pSet->isPrinterPagePDF() && !$this->pdfMode );229		}230 231		if ( count($this->gridTabs) > 1 ) 232		{233			$curTabId = $this->getCurrentTabId();234			$this->xt->assign("printtabheader",true);235			$this->xt->assign("printtabheader_text", $this->getTabTitle($curTabId));236		}237		foreach( $this->pSet->getPageFields() as $f )238		{239			$gf = GoodFieldName($f);240			$this->xt->assign( $gf . "_class", $this->fieldClass( $f ));241			$this->xt->assign( $gf . "_align", $this->fieldAlign( $f ));242		}243		if( $this->pdfJsonMode() ) {244			$this->xt->assign( "pdfFonts", my_json_encode( getPdfFonts() ) );245		}246	}247 248	protected function setRecordsId() {249		$pageCount = count( $this->arrReport['list'] );250		for( $i = 0; $i < $pageCount; ++$i ) {251			$page = &$this->arrReport['list'][ $i ];252			$recCount = count( $page );253			for( $j = 0; $j < $recCount; ++$j ) {254				$this->genId();255				$page[$j]["recId"] = $this->recId;256			}257		}258	}259 260	/**261	 *262	 */263	protected function assignBody()264	{		265		if( !$this->pdfJsonMode() ) {266			$this->body["begin"].= GetBaseScriptsForPage( false );267			$this->body["end"] = XTempl::create_method_assignment( "assignBodyEnd", $this);268		}269 270		$this->xt->assignbyref("body", $this->body);271		$this->xt->assign("grid_block", true);272		$this->xt->assign("grid_header", true);273 274		if( $this->format && $this->format != "pdf" )275		{276			$this->body["begin"] = "";277			$this->body["end"] = "";278			$this->xt->assignbyref("body", $this->body);279		}			280	}281	282	/**283	 * A stub284	 */285	protected function getnoRecOnFirstPageWhereCondition()286	{287		return "";288	}289 290	/**291	 * Assign values obtained from crossTable object to292	 * the basic cross table xt variables293	 * @param Boolean showSummary294	 */295	protected function crossTableCommonAssign( $showSummary )296	{297		$this->xt->assign( "report_cross_header", $this->crossTableObj->getPrintCrossHeader() );298		$this->xt->assign( "totals", $this->crossTableObj->getTotalsName() );299		300		$grid_row["data"] = $this->crossTableObj->getCrossTableData();301 302		if( count($grid_row["data"]) > 0 )303		{304			$this->xt->assign("grid_row", $grid_row);305			$group_headerArr = $this->crossTableObj->getCrossTableHeader();306			$this->xt->assignbyref("group_header", $group_headerArr );307			$this->xt->assign("group_x_count", count( $group_headerArr["data"] ) );308			$this->xt->assign("group_x_label", $this->crossTableObj->getXGroupLabel() );309			$this->xt->assign("group_y_label", $this->crossTableObj->getYGroupLabel() );310			$this->xt->assign("report_cross_field",  $this->crossTableObj->getDataFieldLabel() );311			$this->xt->assign("report_cross_type", $this->crossTableObj->getTotalsName() );312 313			$this->xt->assignbyref("col_summary", $this->crossTableObj->getCrossTableSummary());314			$this->xt->assignbyref("total_summary", $this->crossTableObj->getTotalSummary());315			$this->xt->assign("cross_totals", $showSummary);316			317			if( $this->pdfJsonMode() && count( $group_headerArr["data"] ) > 1 ) 318			{319				// add fake cells for pdfMake to make 'colSpan: group_x_count' work320				$cells = array();321				for( $i = 0; $i < count( $group_headerArr["data"] ) - 1; $i++ )322				{323					$cells[] = true;324				}325 326				$this->xt->assign("fake_header_cells", array( "data" => $cells ) );327			}			328		}329		else if ( $this->isDetail ) 330		{331			$this->isReportEmpty = true;332			return;333		}334 335		$pages = array();336		$pages[0]['grid_row'] = $grid_row;337		$pages[0]['begin'] = "<div name=page class=printpage>";338		$pages[0]['end'] = "</div>";339 340		$this->xt->assign("pageno", 1);341		$this->xt->assign("maxpages", 1);342		$this->xt->assign("printheader", true);343		$this->xt->assign_loopsection("pages", $pages);344 345		if( !$this->pdfMode )346			$this->xt->assign("printbuttons", true);347	}348 349	/**350	 * Get data for standart report and assign with xt351	 * @param &Array _options352	 */353	public function setStandartData(&$_options)354	{355		include_once(getabspath('classes/reportlib.php'));356 357		if( !$_SESSION[ $this->sessionPrefix."_pagesize" ] )358			$_SESSION[ $this->sessionPrefix."_pagesize" ] = -1; // a temporary fix359 360		if( !$_SESSION[ $this->sessionPrefix."_pagenumber" ] )361			$_SESSION[ $this->sessionPrefix."_pagenumber" ] = 1;362 363 364		if( isset($_REQUEST["all"]) && $_REQUEST["all"] || $this->isDetail )365		{366			$this->pageData["printAll"] = true;367			$PageSize = 0;368			$pagestart = 0;369			$this->jsSettings['tableSettings'][$this->tName]['reportPrintMode'] = 1;370			$this->controlsMap["pdfSettings"]["allPagesMode"] = 1;			371		}372		else373		{374			$PageSize = $_SESSION[ $this->sessionPrefix."_pagesize" ];375			$pagestart = ($_SESSION[ $this->sessionPrefix."_pagenumber" ] - 1) * $PageSize;376		}377 378 379		$rb = new Report( $this->pSet->getOrderIndexes(), $this->connection380			, $PageSize, $this->splitByGroups, $_options, $this);381 382		$this->arrReport = $rb->getReport( $pagestart );383		$this->arrPages = $rb->getPages();384		$this->standardReportCommonAssign();385		$this->assignColumnHeaderClasses();386	}387 388	/**389	 * Assign the basic cross table xt variables390	 */391	protected function standardReportCommonAssign()392	{393		$this->xt->assign( "printheader", true );394		if( $this->splitAtServer )395		{396			$this->standardReportCommonAssignSplit();397			return;398		}399		400		foreach($this->arrReport['page'] as $key => $value)401		{402			$this->xt->assign($key, $value);403		}404 405		// for print as details406		if ( $this->isDetail && !$this->arrReport['list'] )407		{408			$this->isReportEmpty = true;409			return;410		}411 412 413		$this->xt->assign_loopsection("grid_row", $this->arrReport['list']);414 415		if( $this->arrReport['global'] )416		{417			foreach($this->arrReport['global'] as $key => $value)418			{419				$this->xt->assign($key, $value);420			}421		}422		$this->xt->assign("pageno", 1);423		$this->xt->assign("maxpages", 1);424		if( !$this->pdfMode )425			$this->xt->assign("printbuttons", true);426		$this->xt->assign("global_summary", true);427		//	legacy assignment428		$this->xt->assign("pages", true);429	}430	431	/**432	 *433	 */434	protected function standardReportCommonAssignSplit()435	{436		$page = array( 'grid_row' => array("data" => array()) );437		$pageno = 1;438		439		$this->setRecordsId();440		foreach( $this->arrReport['list'] as $pagerecords)441		{442			$page['grid_row']['data'] = $pagerecords;443			$this->addPage( $page, $pageno );444			++$pageno;445			$page = array( 'grid_row' => array("data" => array()) );446		}447 448		if( $this->arrReport['global'] )449		{450			$lastPage = &$this->pages[ count($this->pages) - 1 ];451			foreach($this->arrReport['global'] as $key => $value)452			{453				$lastPage[$key] = $value;454			}455 456			$lastPage['global_summary'] = true;457		}458 459		$this->xt->assign("maxpages", $pageno);460		461 462		// update %total% value463		$total = count( $this->pages );464		foreach ( $this->pSet->printPagesLabelsData() as $itemId => $mLString )465		{			466			foreach( $this->pages as $idx => $pageBody ) 467			{468				$this->pages[$idx][ "print_pages_label".$itemId ] = str_replace( "%total%", $total, $pageBody["print_pages_label".$itemId] );469			}470		}471				472		473		$this->body[ "data" ] = $this->pages;474		$this->xt->assign( "page_number", true );475 476		$this->xt->assign( "pagecount", $pageno - 1 );477		if( !$this->pdfMode )478			$this->xt->assign("printbuttons", true);479		//	legacy assignment480		$this->xt->assign("pages", true);481	}482	483	/**484	 *485	 */486	protected function addPage(&$page, $pageno)487	{488		//	hide buttons on second and other pages. xt->assign("printbuttons", true) is required to display the container489		$page["printbuttons"] = ($pageno == 1 && !$this->pdfMode);490 491		if( $pageno == 1 )492		{493			$page["pdflink_block"] = $this->pSet->isPrinterPagePDF();494		}		495		496		497		if( !$this->pdfJsonMode() ) {498			if( !$this->pdfMode )499			{500				$page['begin'] = "<div class=\"rp-presplitpage rp-page\">";501			}502			else503			{504				$page['begin'] = "<div class=\"rp-page\">";505			}506			$page['end'] = "</div>";507		}508		$page["pageno"] = $pageno;509		if( is_array( $this->arrPages[$pageno - 1] ) )510		foreach($this->arrPages[$pageno - 1] as $key => $value)511		{512			$page[$key] = $value;513		}514		$page["pageno"] = $pageno;515		516 517		$this->xt->assign( "print_pages", true );518		foreach ( $this->pSet->printPagesLabelsData() as $itemId => $mLString )519		{			520			$label = str_replace( "%current%", $pageno, GetMLString( $mLString ) );521			$page[ "print_pages_label".$itemId ] = $label;522		}	523				524		525		$this->pages[] = $page;526	}527 528	/**529	 *530	 */531	public function prepareWordOrExcelTemplate($contents)532	{533		$pos1 = 0;534		while($pos1 !== false)535		{536			$pos1 = stripos($contents, "<link ", $pos1);537			if( $pos1 !== false )538			{539				$pos2 = strpos($contents, ">", $pos1);540				if( !$pos2 == false)541					$contents = substr($contents, 0, $pos1).substr($contents, $pos2 + 1);542			}543		}544 545		$contents = str_ireplace("<img src=\"/".GetRootPathForResources("images/spacer.gif")."\">", "", $contents);546		$contents = str_ireplace("<img src=\"/".GetRootPathForResources("images/spacer.gif")."\"/>", "", $contents);547		$contents = str_ireplace("<img src=\"@webRootPath/images/spacer.gif\" />", "", $contents); // .net template compatibility548 549		return $contents;550	}551 552	/**553	 *554	 */555	public function processDetailPrint()556	{557		if( $this->crossTable && !$this->checkCrossParams() )	558			$this->setDefaultParams();		559		560		// array with extra report params561		$extraParams = $this->getExtraReportParams();562 563		$this->setReportData( $extraParams );564 565		if ( $this->isReportEmpty )566			return;567 568		$this->xt->assign("grid_header", true);569 570		$this->showDetailPrint();571	}572 573	/**574	 * Display the report page575	 */576	public function showDetailPrint()577	{578		if( $this->pdfJsonMode() ) 579		{580			$this->xt->assign( "body", true );581			$this->xt->assign( "embedded_grid", true );582			$this->xt->assign("embedded_page_title", true );583			584			$this->xt->load_templateJSON( $this->templatefile );585			echo  $this->xt->fetch_loadedJSON("body");586			return;587		}		588		589		$this->xt->assign( "grid_block", true );590 591		$this->xt->load_template( $this->templatefile );592		593 594		echo '<div class="panel panel-info details-grid">595			<div class="panel-heading">596				<h4 class="panel-title">' . $this->getPageTitle( $this->pageName, GoodFieldName($this->tName) ) . '</h4>597			</div>598			<div class="panel-body">';599		echo $this->fetchForms( array( "grid" ) );	600		echo '</div>601		</div>';602	}603 604	/**605	 *606	 */607	public function showPage()608	{609		if( $this->eventsObject->exists("BeforeShowReportPrint") )610			$this->eventsObject->BeforeShowReportPrint($this->xt, $this->templatefile, $this);611 612		if( $this->pdfJsonMode() )613		{614			$this->preparePDFBackground();615			$this->xt->assign( "standalone_page", true );616			$this->xt->displayJSON($this->templatefile);617			return;618		}619 620		if( $this->format == "excel" || $this->format == "word" )621		{622			$this->xt->load_template($this->templatefile);623			$contents = $this->prepareWordOrExcelTemplate($this->xt->template);624			$this->xt->template = $contents;625			$this->xt->display_loaded();626		}627		else628		{629			if( $this->format == "pdf" )630			{631				$this->hideElement("printpdf");632				$this->AddCSSFile("styles/defaultPDF.css");633				$this->assignStyleFiles();634				$this->xt->load_template($this->templatefile);635				$this->xt->display_loaded();636			}637			else638				$this->display($this->templatefile);639		}640	}641 642	function element2Item( $name ) {643		if( $name == "printpdf" ) {644			return array( "print_pdf" );645		}646		return parent::element2Item( $name );647	}648 649	function pdfJsonMode() {650		return $this->mode == PRINT_PDFJSON;651	}652 653	function assignTotalsDefaults() {654		$totals = array('min', 'max', 'avg', 'sum' );655		$summaries = array();656		foreach( $this->pSet->getReportGroupFieldsData() as $idx => $group ) {657			$summaries[] = "group" . $group["strGroupField"];658		}659		$summaries[]= "page";660		$summaries[]= "global";661		$fields = array();662		foreach( $this->pSet->getFieldsList() as $field ) {663			$fields[] = GoodFieldName( $field );664		}665 666		foreach( $fields as $f ) {667			foreach( $summaries as $s ) {668				foreach( $totals as $t ) {669					$this->xt->assign( $s . '_total' . $f . '_' . $t, "''" );670				}671			}672		}673	}674}675?>