CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
exportpage.php605 linesDownload Raw Back to classes
1<?php2 3class ExportPage extends RunnerPage4{5	public $exportType = "";6 7	public $allPagesMode = false;8	public $currentPageMode = false;9	10	protected $textFormattingType;11 12	public $useRawValues = false;13 14	public $csvDelimiter = ",";15 16	public $selectedFields = array();17 18	/**19	 * @constructor20	 * @param &Array params21	 */22	function __construct( &$params )23	{24		parent::__construct( $params );25 26		if( $this->getLayoutVersion() === PD_BS_LAYOUT ) 27		{28			$this->headerForms = array( "top" );29			$this->footerForms = array( "footer" );30			$this->bodyForms = array( "grid" );31		} 32		else 33		{		34			$this->formBricks["header"] = "exportheader";35			$this->formBricks["footer"] = "exportbuttons";36			$this->assignFormFooterAndHeaderBricks( true );37		}38		39		if( $this->pSet->chekcExportDelimiterSelection() )40			$this->jsSettings["tableSettings"][ $this->tName ]["csvDelimiter"] = $this->pSet->getExportDelimiter();41 42		$this->textFormattingType = $this->pSet->getExportTxtFormattingType();43 44		$this->useRawValues = $this->useRawValues || $this->textFormattingType == EXPORT_RAW;45 46		if( $this->exportType && $this->useRawValues && $this->textFormattingType == EXPORT_FORMATTED )47			$this->useRawValues = false;48 49		if( !$this->selectedFields )50			$this->selectedFields = $this->pSet->getExportFields();51 52		if( !$this->searchClauseObj )53			$this->searchClauseObj = $this->getSearchObject();54			55		if( $this->selection )56			$this->jsSettings["tableSettings"][ $this->tName ]["selection"] = $this->getSelection();			57	}58 59	60	/**61	 *62	 */63	public function process()64	{65		if( $this->eventsObject->exists("BeforeProcessExport") )66			$this->eventsObject->BeforeProcessExport( $this );67 68		if( $this->exportType ) {69			RunnerContext::pushSearchContext( $this->searchClauseObj );70			$this->exportByType();71			exit();72			return;73		}74 75		$this->fillSettings();76 77		$this->doCommonAssignments();78		$this->addButtonHandlers();79		$this->addCommonJs();80 81		$this->displayExportPage();82	}83 84	/**85	 *86	 */87	function addCommonJs()88	{89		parent::addCommonJs();90 91		if( $this->pSet->checkExportFieldsSelection() )92		{93			$this->AddCSSFile("include/chosen/bootstrap-chosen.css");94			$this->AddJSFile("include/chosen/chosen.jquery.js");95		}96	}97 98	/**99	 * Assign basic page's xt variables100	 */101	protected function doCommonAssignments()102	{103 104		if( $this->mode == EXPORT_SIMPLE )105		{106			$this->body["begin"] = GetBaseScriptsForPage( false );107			$this->body["end"] = XTempl::create_method_assignment( "assignBodyEnd", $this );108			$this->xt->assignbyref("body", $this->body);109		}110		else111			$this->xt->assign("cancel_button", true);112 113		$this->xt->assign("groupExcel", true);114		$this->xt->assign("exportlink_attrs", 'id="saveButton'.$this->id.'"');115 116		if( $this->pSet->checkExportFieldsSelection() )117		{118			$this->xt->assign("choosefields", true);119			$this->xt->assign("exportFieldsCtrl", $this->getChooseFieldsCtrlMarkup() );120		}121 122		if( !$this->selection )123		{124			$this->xt->assign("rangeheader_block", true);125			$this->xt->assign("range_block", true);126		}127 128		if( $this->textFormattingType == EXPORT_BOTH )129			$this->xt->assign("exportformat", true);130	}131 132	/**133	 * @return String134	 */135	protected function getChooseFieldsCtrlMarkup()136	{137		$options = array();138		foreach( $this->pSet->getExportFields() as $field )139		{140			$options[] = '<option value="'.runner_htmlspecialchars( $field ).'" selected="selected">'.runner_htmlspecialchars( $this->pSet->label( $field ) ).'</option>';141		}142 143		return '<select name="exportFields" multiple style="width: 100%;" data-placeholder="'."Please select".'" id="exportFields'. $this->id .'">'. implode( "", $options ) .'</select>';144	}145	146	/**147	 *148	 */149	protected function exportByType()150	{151		$myPage = 1;152		$pageSize = 0;153		if( $this->currentPageMode ) {154			$myPage = (integer)@$_SESSION[ $this->tName."_pagenumber" ];155			if( !$myPage )156				$myPage = 1;157 158			$pageSize = (integer)@$_SESSION[ $this->tName."_pagesize" ];159			if( !$pageSize )160				$pageSize = $this->pSet->getInitialPageSize();161 162			if( $this->pSet->getRecordsLimit() )163				$pageSize = $this->pSet->getRecordsLimit() - ( $myPage - 1 ) * $pageSize;164			165			if( $pageSize < 0 )166				$pageSize = 0;167			168		} else if( $this->allPagesMode && $this->pSet->getRecordsLimit() ) {169			$pageSize = $this->pSet->getRecordsLimit();170		}171		172 173		$dc = $this->getSubsetDataCommand();174		if( $this->currentPageMode || $this->allPagesMode && $this->pSet->getRecordsLimit() ) {175			$dc->startRecord = $pageSize * ( $myPage - 1 );176			$dc->reccount = $pageSize;				177		}178 179		if( $this->eventsObject->exists("BeforeQueryExport") ) {180			$prep = $this->dataSource->prepareSQL( $dc );181			$sql = $prep["sql"];182			$where = $prep["where"];183			$order = $prep["order"];184			185			$this->eventsObject->BeforeQueryExport( $sql, $where, $order, $this );186			if( $sql != $prep["sql"] ) {187				$this->dataSource->overrideSQL( $dc, $sql );188			} else if( $where != $prep["where"] ) {189				$this->dataSource->overrideWhere( $dc, $where );190			}191			if( $order != $prep["order"] ) {192				$this->dataSource->overrideOrder( $dc, $order );193			}194		}195		runner_set_page_timeout(300);196 197		$rs = $this->dataSource->getList( $dc );198		if( !$rs ) {199			showError( $this->dataSource->lastError() );200		}201		202		$this->exportTo( $this->exportType, $rs , $pageSize );203	}204 205	public function getSubsetDataCommand( $ignoreFilterField = "" ) {206		$dc = parent::getSubsetDataCommand( $ignoreFilterField );207		208		$this->reoderCommandForReoderedRows( $this->getListPSet(), $dc );209		return $dc;210	}211	212	/**213	 * @param String type214	 * @param Mixed rs215	 * @param Number pageSize216	 */217	public function exportTo( $type, $rs, $pageSize )218	{219		global $locale_info;220 221		if( substr($type, 0, 5) == "excel" )222			$type = "excel";223		224		if( $type == "excel" ) 225		{226			$locale_info["LOCALE_SGROUPING"] = "0";227			$locale_info["LOCALE_SMONGROUPING"] = "0";228			ExportToExcel( $rs, $pageSize, $this );229		} 230		else if( $type == "word" ) 231		{232				$this->ExportToWord( $rs, $pageSize );233		}234		else if( $type == "xml" ) 235		{236			$this->ExportToXML( $rs, $pageSize );237		}238		else if( $type == "csv" ) 239		{240			$locale_info["LOCALE_SGROUPING"] = "0";241			$locale_info["LOCALE_SDECIMAL"] = ".";242			$locale_info["LOCALE_SMONGROUPING"] = "0";243			$locale_info["LOCALE_SMONDECIMALSEP"] = ".";244 245			$this->ExportToCSV( $rs, $pageSize );246		}247	}248 249	/**250	 * @param Mixed rs251	 * @param Number pageSize252	 */253	public function ExportToWord( $rs, $pageSize )254	{255		global $cCharset;256		header("Content-Type: application/vnd.ms-word");257		header("Content-Disposition: attachment;Filename=".GetTableURL( $this->tName ).".doc");258 259		echo "<html>";260		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";261		echo "<body>";262		echo "<table border=1>";263 264		$this->WriteTableData( $rs, $pageSize );265 266		echo "</table>";267		echo "</body>";268		echo "</html>";269	}270 271	/**272	 * @param Mixed rs273	 * @param Number pageSize274	 */275	public function ExportToXML( $rs, $pageSize )276	{277		global $cCharset;278 279		header("Content-Type: text/xml");280		header("Content-Disposition: attachment;Filename=".GetTableURL( $this->tName ).".xml");281		282		echo "<?xml version=\"1.0\" encoding=\"".$cCharset."\" standalone=\"yes\"?>\r\n";283		echo "<table>\r\n";284		285		$this->viewControls->setForExportVar("xml");286 287		$row = $this->cipherer->DecryptFetchedArray( $rs->fetchAssoc() );288		$numberOfRows = 0;289		290		while( ( !$pageSize || $numberOfRows < $pageSize ) && $row )291		{292			RunnerContext::pushRecordContext( $row, $this );293			$values = $this->getValuesFromRow( $row );294			295			$eventRes = true;296			if( $this->eventsObject->exists('BeforeOut') )297				$eventRes = $this->eventsObject->BeforeOut( $row, $values, $this );298 299			if( $eventRes ) {300				$numberOfRows++;301				302				echo "<row>\r\n";303				foreach( $values as $fName => $val ) {304					$field = runner_htmlspecialchars( XMLNameEncode( $fName ) );305					echo "<".$field.">" . xmlencode( $values[ $fName ] ) . "</".$field.">\r\n";306				}307				echo "</row>\r\n";308			}309 310			RunnerContext::pop();311			$row = $this->cipherer->DecryptFetchedArray( $rs->fetchAssoc() );312		}313 314		echo "</table>\r\n";315	}316 317	/**318	 * @param Mixed rs319	 * @param Number pageSize320	 */321	public function ExportToCSV( $rs, $pageSize )322	{323		if( $this->pSet->chekcExportDelimiterSelection() && strlen( $this->csvDelimiter ) )324			$delimiter = $this->csvDelimiter;325		else326			$delimiter = $this->pSet->getExportDelimiter();327 328		header("Content-Type: application/csv");329		header("Content-Disposition: attachment;Filename=".GetTableURL($this->tName).".csv");330		printBOM();331 332		$this->viewControls->setForExportVar( "csv" );333 334		$row = $this->cipherer->DecryptFetchedArray( $rs->fetchAssoc() );335		336		// write header337		$headerParts = array();338		foreach( $this->selectedFields as $field )339		{340			$headerParts[] = '"'.str_replace( '"', '""', $field ).'"';341		// 	$headerParts[] = '"'.str_replace( '"', '""', GetFieldLabel( GoodFieldName( $this->tName ), GoodFieldName( $field ) ) ).'"';			342		}343		echo implode( $delimiter, $headerParts );344		echo "\r\n";345 346		// write data rows347		$numberOfRows = 0;348		while( ( !$pageSize || $numberOfRows < $pageSize ) && $row )349		{350			RunnerContext::pushRecordContext( $row, $this );351			$values = $this->getValuesFromRow( $row );352 353			$eventRes = true;354			if( $this->eventsObject->exists('BeforeOut') )355				$eventRes = $this->eventsObject->BeforeOut( $row, $values, $this );356 357			if( $eventRes ) {358				$dataRowParts = array();359				foreach( $this->selectedFields as $field ) {360					$dataRowParts[] = '"'.str_replace( '"', '""', $values[ $field ] ).'"';361				}362				echo implode( $delimiter, $dataRowParts );363			}364			365			RunnerContext::pop();366			367			$numberOfRows++;368			$row = $this->cipherer->DecryptFetchedArray( $rs->fetchAssoc() );369 370			if( ( !$pageSize || $numberOfRows < $pageSize) && $row && $eventRes )371				echo "\r\n";372		}373	}374 375	/**376	 * @param String fName377	 * @param Array row378	 */379	public function getFormattedFieldValue( $fName, $row )380	{381		if( $this->useRawValues )382			return $row[ $fName ];383 384		return $this->getExportValue( $fName, $row, "", $this->exportType == "word" );385	}386 387	/**388	 * @param Mixed rs389	 * @param Number pageSize390	 */391	protected function WriteTableData( $rs, $pageSize )392	{393		$totalFieldsData = $this->pSet->getTotalsFields();394		$row = $this->cipherer->DecryptFetchedArray( $rs->fetchAssoc() );395 396		// write header397		echo "<tr>";398		foreach( $this->selectedFields as $field ) {399			if( $this->exportType == "excel" )400				echo '<td style="width: 100" x:str>'.PrepareForExcel( $this->pSet->label( $field ) ).'</td>';401			else402				echo "<td>".$this->pSet->label( $field )."</td>";403		}404		echo "</tr>";405 406		$totals = array();407		$totalsFields = array();408		foreach( $totalFieldsData as $data )409		{410			if( !in_array( $data["fName"], $this->selectedFields ) )411				continue;412 413			$totals[ $data["fName"] ] = array("value" => 0, "numRows" => 0);414			$totalsFields[] = array('fName' => $data["fName"], 'totalsType' => $data["totalsType"], 415				'viewFormat' => $this->pSet->getViewFormat( $data["fName"] ));416		}417 418		// write data rows419		$numberOfRows = 0;420		$this->viewControls->setForExportVar( "export" );421		while( ( !$pageSize || $numberOfRows < $pageSize ) && $row )422		{423			RunnerContext::pushRecordContext( $row, $this );			424			countTotals( $totals, $totalsFields, $row );425			$values = $this->getValuesFromRow( $row );426 427			$eventRes = true;428			if( $this->eventsObject->exists('BeforeOut') )429				$eventRes = $this->eventsObject->BeforeOut( $row, $values, $this );430 431			if( $eventRes )432			{433				$numberOfRows++;434				echo "<tr>";435 436				foreach( $this->selectedFields as $field )437				{438					$fType = $this->pSet->getFieldType( $field );439					if( IsCharType( $fType ) && $this->exportType == "excel" )440						echo '<td x:str>';441					else442						echo '<td>';443 444					$editFormat = $this->pSet->getEditFormat( $field );445					if( $editFormat == EDIT_FORMAT_LOOKUP_WIZARD )446					{447						if( $this->pSet->NeedEncode( $field ) && $this->exportType == "excel" )448							echo PrepareForExcel( $values[ $field ] );449						else450							echo $values[ $field ];451					}452					elseif( IsBinaryType( $fType ) )453					{454						echo $values[ $field ];455					}456					else457					{458						if( NeedQuotes( $field ) && $this->exportType == "excel" && $editFormat != FORMAT_CUSTOM && !$this->pSet->isUseRTE( $field ) )459							echo PrepareForExcel( $values[ $field ] );460						else461							echo $values[ $field ];462					}463 464					echo '</td>';465				}466				echo "</tr>";467			}468 469			RunnerContext::pop();470			471			$row = $this->cipherer->DecryptFetchedArray( $rs->fetchAssoc() );472		}473 474		if( count( $totalFieldsData ) )475		{476			echo "<tr>";477			foreach( $totalFieldsData as $data )478			{479				if( !in_array( $data["fName"], $this->selectedFields ) )480					continue;481 482				echo "<td>";483				if( strlen( $data["totalsType"] ) )484				{485					if( $data["totalsType"] == "COUNT" )486						echo "Count".": ";487					elseif( $data["totalsType"] == "TOTAL" )488						echo "Total".": ";489					elseif( $data["totalsType"] == "AVERAGE" )490						echo "Average".": ";491 492					echo runner_htmlspecialchars( GetTotals($data["fName"],493						$totals[ $data["fName"] ]["value"],494						$data["totalsType"],495						$totals[ $data["fName"] ]["numRows"],496						$this->pSet->getViewFormat( $data["fName"] ),497						PAGE_EXPORT,498						$this->pSet, $this->useRawValues,499						$this ) );500				}501 502				echo "</td>";503			}504 505			echo "</tr>";506		}507	}508 509	public function getValuesFromRow( &$row ) {510		$values = array();511		foreach( $this->selectedFields as $field )512		{513			$fType = $this->pSet->getFieldType( $field );514			if( IsBinaryType( $fType ) )515				$values[ $field ] = "LONG BINARY DATA - CANNOT BE DISPLAYED";516			else517				$values[ $field ] = $this->getFormattedFieldValue( $field, $row );518		}519		return $values;520	}521	522	/**523	 * @deprecated524	 * @param Mixed rs525	 * @param Number nPageSize526	 */527	public function ExportToExcel_old($rs, $nPageSize)528	{529		global $cCharset;530		header("Content-Type: application/vnd.ms-excel");531		header("Content-Disposition: attachment;Filename=".GetTableURL( $this->tName ).".xls");532 533		echo "<html>";534		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\">";535 536		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";537		echo "<body>";538		echo "<table border=1>";539 540		$this->WriteTableData( $rs, $nPageSize );541 542		echo "</table>";543		echo "</body>";544		echo "</html>";545	}546 547	/**548	 *549	 */550	protected function displayExportPage()551	{552		$templatefile = $this->templatefile;553 554		if( $this->eventsObject->exists("BeforeShowExport") )555			$this->eventsObject->BeforeShowExport( $this->xt, $templatefile, $this );556 557		if( $this->mode == EXPORT_POPUP )558		{559			$this->xt->assign("footer", false);560			$this->xt->assign("header", false);561			$this->xt->assign("body", $this->body);562 563			$this->displayAJAX( $templatefile, $this->id + 1 );564			exit();565		}566 567		$this->display( $templatefile );568	}569 570	/**571	 * @return Number572	 */573	public static function readModeFromRequest()574	{575		if( postvalue("onFly") )576			return EXPORT_POPUP;577 578		return EXPORT_SIMPLE;579	}580 581	582	public function getDataSourceFilterCriteria( $ignoreFilterField = "" ) {583		$filter = parent::getDataSourceFilterCriteria();584		$selectedRecords = $this->getSelectedRecords();585 586		if( $selectedRecords !== null ) {587			$keyFields = $this->pSet->getTableKeys();588			589			$recConditions = array();590			foreach( $selectedRecords as $keys ) {591				$recConditions[] = DataCondition::FieldsEqual( $keyFields, $keys );592			}593 594			$filter = DataCondition::_And( array( 595				$filter,596				DataCondition::_Or( $recConditions )597			));598		}599		return $filter;600	}601	602	public function getSecurityCondition() {603		return Security::SelectCondition( "P", $this->pSet );604	}605}