Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Created December 20, 2018 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdmunir/9b8994abbabd3ccc6578701ea07f8644 to your computer and use it in GitHub Desktop.
Save mdmunir/9b8994abbabd3ccc6578701ea07f8644 to your computer and use it in GitHub Desktop.
<?php
namespace console\controllers;
/**
* Description of AlmanakController
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class AlmanakController extends \yii\console\Controller
{
const CENTER_X = 100;
const CENTER_Y = 120;
protected function getXY($R, $i)
{
$sd = (2 * $i + 1) * pi() / 35;
return [self::CENTER_X + $R * sin($sd), self::CENTER_Y - $R * cos($sd)];
}
public function actionCreatePdf()
{
$R1 = 90;
$R2 = 79;
$R3 = 60;
$pdf = new \common\classes\PdfRotate();
$pdf->AddPage();
for ($it = 0; $it < 2; $it++) {
if ($it === 1) {
$pdf->AddPage();
}
list($x0, $y0) = $this->getXY($R1, -1);
$pdf->SetTextColor(0, 128, 0);
$pdf->SetFont('Arial', 'B', 18);
$pdf->Rotate(90, $x0, $y0);
$pdf->Text($x0 - 6, $y0 + 10, 'BULAN');
$pdf->Rotate(0);
// panah
$baseX = self::CENTER_X;
$baseY = self::CENTER_Y - 110;
$arrows = [[-5, 2], [-5, 25], [-6, 25], [0, 29], [6, 25], [5, 25], [5, 2]];
for ($i = 0; $i < count($arrows) - 1; $i++) {
$a1 = $arrows[$i];
$a2 = $arrows[$i + 1];
$pdf->Line($baseX + $a1[0], $baseY + $a1[1], $baseX + $a2[0], $baseY + $a2[1]);
}
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('Arial', '', 26);
for ($i = 0; $i < 35; $i++) {
$pdf->Line(self::CENTER_X, self::CENTER_Y, $x0, $y0);
// text
if ($i >= 1 && $i <= 31) {
$pdf->Rotate(-$i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + ($i <= 9 ? 6 : 3), $y0 + 8, $i);
$pdf->Rotate(0);
}
// line
list($x1, $y1) = $this->getXY($R1, $i);
if ($i > 0) {
$pdf->Line($x0, $y0, $x1, $y1);
}
$x0 = $x1;
$y0 = $y1;
}
// tahuns
$x = 0;
$tahuns = [];
for ($i = 0; $i <= 99; $i++) {
$tahuns[$x % 35][] = sprintf('%02d', $i);
$x += ($i % 4 == 3) ? 16 : 15;
}
// hari
if ($it === 1) {
$pdf->AddPage();
}
$days1 = ['MIN', 'SEN', 'SEL', 'RAB', 'KAM', 'JUM', 'SAB'];
$days2 = ['LEG', 'PAH', 'PON', 'WAG', 'KLI'];
list($x0, $y0) = $this->getXY($R2, -1);
for ($i = 0; $i < 35; $i++) {
$pdf->Line(self::CENTER_X, self::CENTER_Y, $x0, $y0);
// text
$pdf->SetFont('Arial', '', 12);
if ($i % 7 === 0) {
$pdf->SetTextColor(255, 0, 0);
}
$pdf->Rotate(-$i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + 3, $y0 + 4, $days1[$i % 7]);
$pdf->Text($x0 + 3, $y0 + 8, $days2[$i % 5]);
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->Text($x0 + 2, $y0 + 13, "{$tahuns[$i][0]}, {$tahuns[$i][1]}");
if (isset($tahuns[$i][2])) {
$pdf->Text($x0 + 5, $y0 + 17, $tahuns[$i][2]);
}
$pdf->Rotate(0);
// line
list($x1, $y1) = $this->getXY($R2, $i);
$pdf->Line($x0, $y0, $x1, $y1);
$x0 = $x1;
$y0 = $y1;
}
// bulan
if ($it === 1) {
$pdf->AddPage();
}
$months = [];
$kabisat = [];
$sbulan = ['JAN', 'FEB', 'MAR', 'APR', 'MEI', 'JUN', 'JUL', 'AGU', 'SEP', 'OKT', 'NOV', 'DES'];
$nbulan = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
$abads = [
0 => 19,
15 => 20,
];
$x = 0;
for ($i = 0; $i < 12; $i++) {
$months[$x % 35][] = $sbulan[$i];
if ($i <= 1) { // februari
$kabisat[($x + 34) % 35] = $sbulan[$i] . ' KABISAT';
}
$x += $nbulan[$i];
}
list($x0, $y0) = $this->getXY($R3, -1);
for ($i = 0; $i < 35; $i++) {
$pdf->Line(self::CENTER_X, self::CENTER_Y, $x0, $y0);
// text
$pdf->SetFont('Arial', 'B', 11);
if (isset($months[$i])) {
$pdf->Rotate(-90 - $i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + 6, $y0 - 4, implode(', ', $months[$i]));
$pdf->Rotate(0);
}
if (isset($kabisat[$i])) {
$pdf->SetTextColor(0, 0, 128);
$pdf->SetFont('Arial', 'BI', 11);
$pdf->Rotate(-90 - $i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + 6, $y0 - 4, $kabisat[$i]);
$pdf->Rotate(0);
$pdf->SetTextColor(0, 0, 0);
}
if (isset($abads[$i])) {
$pdf->SetTextColor(128, 0, 128);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Rotate(-$i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + 4, $y0 + 4, $abads[$i]);
$pdf->Rotate(0);
$pdf->SetTextColor(0, 0, 0);
}
// line
list($x1, $y1) = $this->getXY($R3, $i);
$pdf->Line($x0, $y0, $x1, $y1);
$x0 = $x1;
$y0 = $y1;
}
}
$file = \Yii::getAlias('@runtime/almanak.pdf');
$pdf->Output('f', $file);
}
public function actionCreatePdfHijri()
{
$R1 = 90;
$R2 = 79;
$R3 = 60;
$pdf = new \common\classes\PdfRotate();
$pdf->AddPage();
for ($it = 0; $it < 2; $it++) {
if ($it === 1) {
$pdf->AddPage();
}
list($x0, $y0) = $this->getXY($R1, -1);
$pdf->SetTextColor(0, 128, 0);
$pdf->SetFont('Arial', 'B', 18);
$pdf->Rotate(90, $x0, $y0);
$pdf->Text($x0 - 6, $y0 + 10, 'BULAN');
$pdf->Rotate(0);
// panah
$baseX = self::CENTER_X;
$baseY = self::CENTER_Y - 110;
$arrows = [[-5, 2], [-5, 25], [-6, 25], [0, 29], [6, 25], [5, 25], [5, 2]];
for ($i = 0; $i < count($arrows) - 1; $i++) {
$a1 = $arrows[$i];
$a2 = $arrows[$i + 1];
$pdf->Line($baseX + $a1[0], $baseY + $a1[1], $baseX + $a2[0], $baseY + $a2[1]);
}
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('Arial', '', 26);
for ($i = 0; $i < 35; $i++) {
$pdf->Line(self::CENTER_X, self::CENTER_Y, $x0, $y0);
// text
if ($i >= 1 && $i <= 30) {
$pdf->Rotate(-$i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + ($i <= 9 ? 6 : 3), $y0 + 8, $i);
$pdf->Rotate(0);
}
// line
list($x1, $y1) = $this->getXY($R1, $i);
if ($i > 0) {
$pdf->Line($x0, $y0, $x1, $y1);
}
$x0 = $x1;
$y0 = $y1;
}
// tahuns
$x = 0;
$tahuns = [];
for ($i = 0; $i <= 99; $i++) {
$tahuns[$x % 35][] = sprintf('%02d', $i);
$x += ((11 * ($i % 30) + 3) % 30 > 18) ? 10 : 9;
}
// hari
if ($it === 1) {
$pdf->AddPage();
}
$days1 = ['MIN', 'SEN', 'SEL', 'RAB', 'KAM', 'JUM', 'SAB'];
$days2 = ['LEG', 'PAH', 'PON', 'WAG', 'KLI'];
list($x0, $y0) = $this->getXY($R2, -1);
for ($i = 0; $i < 35; $i++) {
$pdf->Line(self::CENTER_X, self::CENTER_Y, $x0, $y0);
// text
$pdf->SetFont('Arial', '', 12);
if ($i % 7 === 0) {
$pdf->SetTextColor(255, 0, 0);
}
$pdf->Rotate(-$i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + 3, $y0 + 4, $days1[$i % 7]);
$pdf->Text($x0 + 3, $y0 + 8, $days2[$i % 5]);
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->Text($x0 + 2, $y0 + 13, "{$tahuns[$i][0]}, {$tahuns[$i][1]}");
if (isset($tahuns[$i][2])) {
$pdf->Text($x0 + 5, $y0 + 17, $tahuns[$i][2]);
}
$pdf->Rotate(0);
// line
list($x1, $y1) = $this->getXY($R2, $i);
$pdf->Line($x0, $y0, $x1, $y1);
$x0 = $x1;
$y0 = $y1;
}
// bulan
if ($it === 1) {
$pdf->AddPage();
}
$months = [];
$sbulan = ['MUHARRAM', 'SHAFAR', 'R AWAL', 'R AKHIR', 'J AWAL', 'J AKHIR', 'RAJAB', 'SA\'BAN', 'RAMADHAN', 'SHAWWAL', 'DZULQA\'DAH', 'DZULHIJJAH'];
//$nbulan = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
$abads = [
0 => 13,
15 => 14,
];
$x = 0;
for ($i = 0; $i < 12; $i++) {
$months[$x % 35][] = $sbulan[$i];
$x += ($i % 2 ? 30 : 29);
}
list($x0, $y0) = $this->getXY($R3, -1);
for ($i = 0; $i < 35; $i++) {
$pdf->Line(self::CENTER_X, self::CENTER_Y, $x0, $y0);
// text
$pdf->SetFont('Arial', 'B', 11);
if (isset($months[$i])) {
$pdf->Rotate(-90 - $i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + 6, $y0 - 4, implode(', ', $months[$i]));
$pdf->Rotate(0);
}
if (isset($abads[$i])) {
$pdf->SetTextColor(128, 0, 128);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Rotate(-$i * 360 / 35, $x0, $y0);
$pdf->Text($x0 + 4, $y0 + 4, $abads[$i]);
$pdf->Rotate(0);
$pdf->SetTextColor(0, 0, 0);
}
// line
list($x1, $y1) = $this->getXY($R3, $i);
$pdf->Line($x0, $y0, $x1, $y1);
$x0 = $x1;
$y0 = $y1;
}
}
$file = \Yii::getAlias('@runtime/almanak-h.pdf');
$pdf->Output('f', $file);
}
public function actionCheck()
{
$selisih = (strtotime('2017-11-09') - strtotime('2017-01-01')) / (24 * 3600);
$days1 = ['MIN', 'SEN', 'SEL', 'RAB', 'KAM', 'JUM', 'SAB'];
$days2 = ['LEG', 'PAH', 'PON', 'WAG', 'KLI'];
$x = 1;
for ($i = 0; $i < 200; $i++) {
if ($x % 35 === 1) {
echo ' ===> ';
}
echo sprintf("%d-01-01 : %s %s\n", $i + 1900, $days1[$x % 7], $days2[$x % 5]);
if ($i == 117) {
echo sprintf("20017-11-09 : %s %s\n", $days1[($x + $selisih) % 7], $days2[($x + $selisih) % 5]);
}
$x += ($i % 4 === 0 && $i !== 0) ? 16 : 15;
}
}
}
<?php
namespace common\classes;
/**
* Description of PdfRotate
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class PdfRotate extends \FPDF
{
var $angle = 0;
function Rotate($angle, $x = -1, $y = -1)
{
if ($x == -1) {
$x = $this->x;
}
if ($y == -1) {
$y = $this->y;
}
if ($this->angle != 0) {
$this->_out('Q');
}
$this->angle = $angle;
if ($angle != 0) {
$angle *= M_PI / 180;
$c = cos($angle);
$s = sin($angle);
$cx = $x * $this->k;
$cy = ($this->h - $y) * $this->k;
$this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
}
}
function _endpage()
{
if ($this->angle != 0) {
$this->angle = 0;
$this->_out('Q');
}
parent::_endpage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment