Skip to content

Commit

Permalink
Merge pull request #9014 from atm-john/NEW_PDF_DOCUMENT_COLUMN_new_files
Browse files Browse the repository at this point in the history
Experimental new pdf documents  using new column system
  • Loading branch information
eldy committed Oct 19, 2018
2 parents 4571210 + 3fb2d4c commit fecb6e4
Show file tree
Hide file tree
Showing 7 changed files with 5,824 additions and 0 deletions.
225 changes: 225 additions & 0 deletions htdocs/core/class/commondocgenerator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,4 +823,229 @@ function printRect($pdf, $x, $y, $l, $h, $hidetop=0, $hidebottom=0)
if (empty($hidebottom)) $pdf->line($x+$l, $y+$h, $x, $y+$h);
$pdf->line($x, $y+$h, $x, $y);
}


/**
* uasort callback function to Sort colums fields
*
* @param array $a PDF lines array fields configs
* @param array $b PDF lines array fields configs
* @return int Return compare result
*/
function columnSort($a, $b) {

if(empty($a['rank'])){ $a['rank'] = 0; }
if(empty($b['rank'])){ $b['rank'] = 0; }
if ($a['rank'] == $b['rank']) {
return 0;
}
return ($a['rank'] > $b['rank']) ? -1 : 1;

}

/**
* Prepare Array Column Field
*
* @param object $object common object
* @param outputlangs $outputlangs langs
* @param int $hidedetails Do not show line details
* @param int $hidedesc Do not show desc
* @param int $hideref Do not show ref
* @return null
*/
function prepareArrayColumnField($object,$outputlangs,$hidedetails=0,$hidedesc=0,$hideref=0){

global $conf;

$this->defineColumnField($object,$outputlangs,$hidedetails,$hidedesc,$hideref);


// Sorting
uasort ( $this->cols, array( $this, 'columnSort' ) );

// Positionning
$curX = $this->page_largeur-$this->marge_droite; // start from right

// Array witdh
$arrayWidth = $this->page_largeur-$this->marge_droite-$this->marge_gauche;

// Count flexible column
$totalDefinedColWidth = 0;
$countFlexCol = 0;
foreach ($this->cols as $colKey =>& $colDef)
{
if(!$this->getColumnStatus($colKey)) continue; // continue if desable

if(!empty($colDef['scale'])){
// In case of column widht is defined by percentage
$colDef['width'] = abs($arrayWidth * $colDef['scale'] / 100 );
}

if(empty($colDef['width'])){
$countFlexCol++;
}
else{
$totalDefinedColWidth += $colDef['width'];
}
}

foreach ($this->cols as $colKey =>& $colDef)
{
// setting empty conf with default
if(!empty($colDef['title'])){
$colDef['title'] = array_replace($this->defaultTitlesFieldsStyle, $colDef['title']);
}
else{
$colDef['title'] = $this->defaultTitlesFieldsStyle;
}

// setting empty conf with default
if(!empty($colDef['content'])){
$colDef['content'] = array_replace($this->defaultContentsFieldsStyle, $colDef['content']);
}
else{
$colDef['content'] = $this->defaultContentsFieldsStyle;
}

if($this->getColumnStatus($colKey))
{
// In case of flexible column
if(empty($colDef['width'])){
$colDef['width'] = abs(($arrayWidth - $totalDefinedColWidth)) / $countFlexCol;
}

// Set positions
$lastX = $curX;
$curX = $lastX - $colDef['width'];
$colDef['xStartPos'] = $curX;
$colDef['xEndPos'] = $lastX;
}
}
}

/**
* get column content width from column key
*
* @param string $colKey the column key
* @return float width in mm
*/
function getColumnContentWidth($colKey)
{
$colDef = $this->cols[$colKey];
return $colDef['width'] - $colDef['content']['padding'][3] - $colDef['content']['padding'][1];
}


/**
* get column content X (abscissa) left position from column key
*
* @param string $colKey the column key
* @return float X position in mm
*/
function getColumnContentXStart($colKey)
{
$colDef = $this->cols[$colKey];
return $colDef['xStartPos'] + $colDef['content']['padding'][3];
}

/**
* get column position rank from column key
*
* @param string $colKey the column key
* @return int rank on success and -1 on error
*/
function getColumnRank($colKey)
{
if(!isset($this->cols[$colKey]['rank'])) return -1;
return $this->cols[$colKey]['rank'];
}

/**
* get column position rank from column key
*
* @param string $newColKey the new column key
* @param array $defArray a single column definition array
* @param string $targetCol target column used to place the new column beside
* @param bool $insertAfterTarget insert before or after target column ?
* @return int new rank on success and -1 on error
*/
function insertNewColumnDef($newColKey, $defArray, $targetCol = false, $insertAfterTarget = false)
{
// prepare wanted rank
$rank = -1;

// try to get rank from target column
if(!empty($targetCol)){
$rank = $this->getColumnRank($targetCol);
if($rank>=0 && $insertAfterTarget){ $rank++; }
}

// get rank from new column definition
if($rank<0 && !empty($defArray['rank'])){
$rank = $defArray['rank'];
}

// error: no rank
if($rank<0){ return -1; }

foreach ($this->cols as $colKey =>& $colDef)
{
if( $rank <= $colDef['rank'])
{
$colDef['rank'] = $colDef['rank'] + 1;
}
}

$defArray['rank'] = $rank;
$this->cols[$newColKey] = $defArray; // array_replace is used to preserve keys

return $rank;
}


/**
* print standard column content
*
* @param PDF $pdf pdf object
* @param float $curY curent Y position
* @param string $colKey the column key
* @param string $columnText column text
* @return int new rank on success and -1 on error
*/
function printStdColumnContent($pdf, &$curY, $colKey, $columnText = '')
{
global $hookmanager;

$parameters=array(
'object' => $object,
'curY' =>& $curY,
'columnText' => $columnText,
'colKey' => $colKey
);
$reshook=$hookmanager->executeHooks('printStdColumnContent',$parameters,$this); // Note that $action and $object may have been modified by hook
if ($reshook < 0) setEventMessages($hookmanager->error,$hookmanager->errors,'errors');
if (!$reshook)
{
if(empty($columnText)) return;
$pdf->SetXY($this->getColumnContentXStart($colKey),$curY); // Set curent position
$colDef = $this->cols[$colKey];
$pdf->MultiCell( $this->getColumnContentWidth($colKey),2, $columnText,'',$colDef['content']['align']);
}

}


/**
* get column status from column key
*
* @param string $colKey the column key
* @return float width in mm
*/
function getColumnStatus($colKey)
{
if( !empty($this->cols[$colKey]['status'])){
return true;
}
else return false;
}
}
Loading

0 comments on commit fecb6e4

Please sign in to comment.