Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SDClowen committed Aug 14, 2024
1 parent 650b6c4 commit b71e817
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
59 changes: 45 additions & 14 deletions app/controllers/Reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Core\Attributes\route;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Border;

class Reports extends Controller
{
Expand Down Expand Up @@ -63,16 +66,15 @@ public function csv(int $surveyId)
$csvFileName = preg_replace('/[^A-Za-z0-9_-]/', '', str_replace(' ', '-', $survey->title));
header('Content-Disposition: attachment; filename=' . $csvFileName . '.xlsx');
header('Cache-Control: max-age=0');
$file = fopen('php://output', 'w');

##################################################

$questionData = json_decode($survey->data);
$questions = array_map(fn($v) => strip_tags(trim($v->title)), $questionData);
$title = ['Sicil No', 'Ad Soyad'];

$data = [
array_merge(['Sicil No', 'Ad Soyad'], $questions)
];
foreach ($questionData as $value)
if($value->type != "description")
$title[$value->slug] = html_entity_decode(strip_tags(trim($value->title)));

$data["title"] = $title;

#################
$participators = Survey::participators($surveyId);
Expand All @@ -85,14 +87,31 @@ public function csv(int $surveyId)
$participator->id,
$participator->fullname
];


$answerData = json_decode($participator->data);

foreach ($answerData as $questionKey => $answerKey) {
$question = current(array_filter($questionData, fn($val) => $val->slug == $questionKey));

$inline[] = strip_tags(trim($question->type == "textarea" ? $answerKey : $question->answers[$answerKey]));
$answerData = json_decode($participator->data, JSON_OBJECT_AS_ARRAY);

foreach ($answerData as $answerKey => $answerValue) {

$question = null;

foreach ($questionData as $k => $v) {
if(str_starts_with($answerKey,$v->slug))
$question = $v;
}

if(empty($v) || !$question)
continue;

if($question->type == "checkbox")
{
if(!array_key_exists($question->slug, $inline))
$inline[$question->slug] = null;

$inline[$question->slug] .= "".html_entity_decode(strip_tags(trim($question->answers[$answerValue])));
$inline[$question->slug] = trim($inline[$question->slug], "");
}
else
$inline[$question->slug] = html_entity_decode(strip_tags(trim($question->type == "textarea" ? $answerValue : $question->answers[$answerValue])));
}

$data[] = $inline;
Expand All @@ -102,6 +121,18 @@ public function csv(int $surveyId)
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray($data);

for ($colIndex = 0; $colIndex <= count($title); $colIndex++) {
$columnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex);
$cellCoordinate = $columnLetter . '1'; // 1. satır

$sheetStyle = $sheet->getStyle($cellCoordinate);

$sheetStyle->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FFC00000');
$sheetStyle->getFont()->setColor(new Color(Color::COLOR_WHITE))->setSize(12);
$sheetStyle->getBorders()->getBottom()->setBorderStyle(Border::BORDER_MEDIUM)->getColor()->setARGB('FF888888');
}


$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
}
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/surveys.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function create()
#[route(method: route::xhr_post, uri: "apply", session: "user")]
public function apply(int $id = 0)
{
$post = Request::post();
$post = (object)Request::post(filter: false);

$validate = validate($post, [
"title" => ["name" => lang("title"), "required" => true, "min" => 8, "max" => 255],
"about" => ["name" => lang("about"), "min" => 0, "max" => 65535],
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ $(function () {

function createDescription(questionDummyText, slug, subType = 0) {
const body = `
<div contenteditable="true" data-slug="${slug}" class="mb-3 h-14 bg-gray-100 focus:outline-blue-600 dark:bg-gray-700 rounded-md px-4 py-2">${questionDummyText}</div>
<div contenteditable="true" data-slug="${slug}" class="mb-3 min-h-14 bg-gray-100 focus:outline-blue-600 dark:bg-gray-700 rounded-md px-4 py-2">${questionDummyText}</div>
<div class="rounded-t-lg bg-yellow-100 dark:bg-gray-900 p-2">
<div class="col-span-4">
Expand Down

0 comments on commit b71e817

Please sign in to comment.