Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(cleanup) Members card conf : adaptive size #484

Merged
merged 1 commit into from May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions galette/docs/CHANGES
Expand Up @@ -30,6 +30,7 @@ Changes
- Add payment type on transactions
- Remove non user related files from cofiguration folder
- Add monthly contribution membership
- PDF Member card adaptative size

1.0.3 -> 1.0.4

Expand Down
23 changes: 18 additions & 5 deletions galette/lib/Galette/Controllers/AuthController.php
Expand Up @@ -122,11 +122,24 @@ public function doLogin(Request $request, Response $response): Response
|| $this->login->isAdmin()
|| $this->login->isStaff()
) {
if (defined('NON_UTF_DBCONNECT')) {
$this->flash->addMessage(
'warning',
'It appears you are using NON_UTF_DBCONNECT constant, that has been removed in current release.'
);
$deprecated_constants = [
'NON_UTF_DBCONNECT',
'GALETTE_CARD_WIDTH',
'GALETTE_CARD_HEIGHT',
'GALETTE_CARD_COLS',
'GALETTE_CARD_ROWS'
];

foreach ($deprecated_constants as $deprecated_constant) {
if (defined($deprecated_constant)) {
$this->flash->addMessage(
'warning',
sprintf(
'It appears you are using %1$s constant, that has been removed in current release.',
$deprecated_constant
)
);
}
}

try {
Expand Down
12 changes: 0 additions & 12 deletions galette/lib/Galette/Controllers/GaletteController.php
Expand Up @@ -258,7 +258,6 @@ public function storePreferences(Request $request, Response $response): Response
{
$post = $request->getParsedBody();
$error_detected = [];
$warning_detected = [];

// Validation
if (isset($post['valid']) && $post['valid'] == '1') {
Expand All @@ -271,7 +270,6 @@ public function storePreferences(Request $request, Response $response): Response
_T("Preferences has been saved.")
);
}
$warning_detected = array_merge($warning_detected, $this->preferences->checkCardsSizes());

// picture upload
if (!Galette::isDemo() && isset($_FILES['logo'])) {
Expand Down Expand Up @@ -350,16 +348,6 @@ public function storePreferences(Request $request, Response $response): Response
);
}
}

if (count($warning_detected) > 0) {
//report warnings
foreach ($warning_detected as $warning) {
$this->flash->addMessage(
'warning_detected',
$warning
);
}
}
}
if (isset($post['tab']) && $post['tab'] != 'general') {
$tab = '?tab=' . $post['tab'];
Expand Down
61 changes: 25 additions & 36 deletions galette/lib/Galette/Core/Preferences.php
Expand Up @@ -241,6 +241,10 @@ class Preferences
'pref_card_bcol' => '#53248C',
'pref_card_hcol' => '#248C53',
'pref_bool_display_title' => false,
'pref_card_hsize' => 84,
'pref_card_vsize' => 52,
'pref_card_rows' => 6,
'pref_card_cols' => 2,
'pref_card_address' => 1,
'pref_card_year' => '',
'pref_card_marges_v' => 15,
Expand Down Expand Up @@ -307,7 +311,9 @@ class Preferences
'pref_card_marges_v',
'pref_card_marges_h',
'pref_card_hspace',
'pref_card_vspace'
'pref_card_vspace',
'pref_card_hsize',
'pref_card_vsize'
);

/**
Expand Down Expand Up @@ -704,6 +710,16 @@ public function validateValue(string $fieldname, mixed $value): mixed
$this->errors[] = _T("- The numbers and measures have to be integers!");
}
break;
case 'pref_card_vsize':
if (!is_numeric($value) || $value < 40 || $value > 55) {
$this->errors[] = _T("- The card height have to be an integer between 40 and 55!");
}
break;
case 'pref_card_hsize':
if (!is_numeric($value) || $value < 70 || $value > 95) {
$this->errors[] = _T("- The card width have to be an integer between 75 and 95!");
}
break;
case 'pref_card_tcol':
case 'pref_card_scol':
case 'pref_card_bcol':
Expand Down Expand Up @@ -808,6 +824,14 @@ public function store(): bool
}
$value = $v;
}
if ($k === 'pref_card_cols') {
$v = PdfMembersCards::getCols();
$value = $v;
}
if ($k === 'pref_card_rows') {
$v = PdfMembersCards::getRows();
$value = $v;
}

$stmt->execute(
array(
Expand Down Expand Up @@ -1155,41 +1179,6 @@ public function getRegistrationDate(): ?string
return null;
}

/**
* Check member cards sizes
* Always a A4/portrait
*
* @return array<string>
*/
public function checkCardsSizes(): array
{
$warning_detected = [];
//check page width
$max = 210;
//margins
$size = $this->pref_card_marges_h * 2;
//cards
$size += PdfMembersCards::getWidth() * PdfMembersCards::getCols();
//spacing
$size += $this->pref_card_hspace * (PdfMembersCards::getCols() - 1);
if ($size > $max) {
$warning_detected[] = _T('Current cards configuration may exceed page width!');
}

$max = 297;
//margins
$size = $this->pref_card_marges_v * 2;
//cards
$size += PdfMembersCards::getHeight() * PdfMembersCards::getRows();
//spacing
$size += $this->pref_card_vspace * (PdfMembersCards::getRows() - 1);
if ($size > $max) {
$warning_detected[] = _T('Current cards configuration may exceed page height!');
}

return $warning_detected;
}

/**
* Get errors
*
Expand Down
92 changes: 60 additions & 32 deletions galette/lib/Galette/IO/PdfMembersCards.php
Expand Up @@ -36,10 +36,8 @@

class PdfMembersCards extends Pdf
{
public const WIDTH = 75;
public const HEIGHT = 40;
public const COLS = 2;
public const ROWS = 6;
public const PAGE_WIDTH = 210;
public const PAGE_HEIGHT = 297;

/** @var array<string,float|int> */
private array $tcol;
Expand Down Expand Up @@ -134,8 +132,8 @@ public function init(): void
$this->vspacing = $this->preferences->pref_card_vspace;

//maximum size for visible text. May vary with fonts.
$this->max_text_size = 80;
$this->year_font_size = 8;
$this->max_text_size = self::getWidth() - (int)round($this->wi / 3.5) - 2;
$this->year_font_size = (int)round(self::getWidth() / 7);

// Get fixed data from preferences
$this->an_cot = $this->preferences->pref_card_year;
Expand All @@ -144,19 +142,19 @@ public function init(): void
$print_logo = new PrintLogo();
$this->logofile = $print_logo->getPath();

// Set logo size to max width 30mm (113px) or max height 17mm (64px)
// Set logo size to max 20% width or max 25% height
$ratio = $print_logo->getWidth() / $print_logo->getHeight();
if ($ratio < 1.71) {
if ($print_logo->getHeight() > 64) {
$this->hlogo = 17;
if ($print_logo->getHeight() > 0.25 * $this->wi * 3.78) {
$this->hlogo = round(0.25 * $this->wi);
} else {
// Convert original pixels size to millimeters
$this->hlogo = $print_logo->getHeight() / 3.78;
}
$this->wlogo = round($this->hlogo * $ratio);
} else {
if ($print_logo->getWidth() > 113) {
$this->wlogo = 30;
if ($print_logo->getWidth() > 0.2 * $this->he * 3.78) {
$this->wlogo = round(0.2 * $this->he);
} else {
// Convert original pixels size to millimeters
$this->wlogo = $print_logo->getWidth() / 3.78;
Expand Down Expand Up @@ -237,8 +235,8 @@ public function drawCards(array $members): void
$photofile = $photo->getPath();

// Photo 100x130 and logo
$this->Image($photofile, $x0, $y0, 25);
$this->Image($this->logofile, $xl, $y0, round($this->wlogo));
$this->Image($photofile, $x0 + 1, $y0 + 1, round($this->wi / 3.5));
$this->Image($this->logofile, $xl - 1, $y0 + 1, round($this->wlogo));

// Color=#8C8C8C: Shadow of the year
$this->SetTextColor(140);
Expand All @@ -250,29 +248,28 @@ public function drawCards(array $members): void
$an_cot = $member->due_date;
}

$xan_cot = $x0 + $this->wi - $this->GetStringWidth(
$xan_cot = $x0 + $this->wi / 2 - $this->GetStringWidth(
$an_cot,
self::FONT,
'B',
$this->year_font_size
) - 0.2;
$this->SetXY($xan_cot, $y0 + $this->hlogo - 0.3);
) / 2 ;
$this->SetXY($xan_cot, $y0 + 1);
$this->writeHTML('<strong>' . $an_cot . '</strong>', false, false);

// Colored Text (Big label, id, year)
$this->SetTextColor($fcol['R'], $fcol['G'], $fcol['B']);

$this->SetFontSize(8);

if (!empty($this->preferences->pref_show_id) || !empty($member->number)) {
$member_id = (!empty($member->number)) ? $member->number : $member->id;
$xid = $x0 + $this->wi - $this->GetStringWidth($member_id, self::FONT, 'B', 8) - 0.2;
$this->SetXY($xid, $y0 + 28);
$this->writeHTML('<strong>' . $member_id . '</strong>', false, false);
$xid = $x0 + $this->wi / 2 - $this->GetStringWidth(_T("Member") . ' n° : ' . $member_id, self::FONT, 'B', 8) / 2;
$this->SetXY($xid, $y0 + 8);
$this->writeHTML('<strong>' . _T("Member") . ' n° : ' . $member_id . ' </strong>', false, false);
}
$this->SetFontSize($this->year_font_size);
$xan_cot = $xan_cot - 0.3;
$this->SetXY($xan_cot, $y0 + $this->hlogo - 0.3);
$xan_cot = $xan_cot - 0.1;
$this->SetXY($xan_cot, $y0 + 1 - 0.1);
$this->writeHTML('<strong>' . $an_cot . '</strong>', false, false);

// Abbrev: Adapt font size to text length
Expand All @@ -282,7 +279,8 @@ public function drawCards(array $members): void
12,
'B'
);
$this->SetXY($x0 + 27, $y0 + 12);
$xid = $x0 + $this->wi / 2 - $this->GetStringWidth($this->abrev, self::FONT, 'B', 12) / 2;
$this->SetXY($xid, $y0 + 12);
$this->writeHTML('<strong>' . $this->abrev . '</strong>', true, false);

// Name: Adapt font size to text length
Expand All @@ -293,7 +291,7 @@ public function drawCards(array $members): void
8,
'B'
);
$this->SetXY($x0 + 27, $this->getY() + 4);
$this->SetXY($x0 + round($this->wi / 3.5) + 2, $y0 + $this->hlogo + 3);
//$this->setX($x0 + 27);
$this->writeHTML('<strong>' . $nom_adh_ext . '</strong>', true, false);

Expand All @@ -304,7 +302,7 @@ public function drawCards(array $members): void
6,
'B'
);
$this->setX($x0 + 27);
$this->setX($x0 + round($this->wi / 3.5) + 2);
$this->writeHTML('<strong>' . $email . '</strong>', false, false);

// Lower colored strip with long text
Expand All @@ -314,11 +312,11 @@ public function drawCards(array $members): void
$this->tcol['G'],
$this->tcol['B']
);
$this->SetFont(self::FONT, 'B', 6);
$this->SetXY($x0, $y0 + 33);
$this->SetFont(self::FONT, 'B', 8);
$this->SetXY($x0, $y0 + round($this->wi / 3.5) * 1.3 + 2);
$this->Cell(
$this->wi,
7,
($this->he - (round($this->wi / 3.5) * 1.3 + 2)),
$this->preferences->pref_card_strip,
0,
0,
Expand All @@ -339,7 +337,9 @@ public function drawCards(array $members): void
*/
public static function getWidth(): int
{
return defined('GALETTE_CARD_WIDTH') ? GALETTE_CARD_WIDTH : self::WIDTH;
global $preferences;

return $preferences->pref_card_hsize ;
}

/**
Expand All @@ -349,7 +349,9 @@ public static function getWidth(): int
*/
public static function getHeight(): int
{
return defined('GALETTE_CARD_HEIGHT') ? GALETTE_CARD_HEIGHT : self::HEIGHT;
global $preferences;

return $preferences->pref_card_vsize ;
}

/**
Expand All @@ -359,7 +361,20 @@ public static function getHeight(): int
*/
public static function getCols(): int
{
return defined('GALETTE_CARD_COLS') ? GALETTE_CARD_COLS : self::COLS;
global $preferences;

$margins = $preferences->pref_card_marges_h * 2;

$nbcols = (int)round(
((self::PAGE_WIDTH - $margins) / $preferences->pref_card_hsize),
0,
PHP_ROUND_HALF_DOWN
);
if ((($nbcols - 1) * $preferences->pref_card_hspace + $margins + $preferences->pref_card_hsize * $nbcols) > self::PAGE_WIDTH) {
--$nbcols;
}

return $nbcols;
}

/**
Expand All @@ -369,6 +384,19 @@ public static function getCols(): int
*/
public static function getRows(): int
{
return defined('GALETTE_CARD_ROWS') ? GALETTE_CARD_ROWS : self::ROWS;
global $preferences;

$margins = $preferences->pref_card_marges_v * 2;

$nbrows = (int)round(
((self::PAGE_HEIGHT - $margins) / $preferences->pref_card_vsize),
0,
PHP_ROUND_HALF_DOWN
);
if ((($nbrows - 1) * $preferences->pref_card_vspace + $margins + $preferences->pref_card_vsize * $nbrows) > self::PAGE_HEIGHT) {
--$nbrows;
}

return $nbrows;
}
}