Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- require_once('C:\xampp\htdocs\jakiepiwomamkupic\app\Questions.php');
- require_once('C:\xampp\htdocs\jakiepiwomamkupic\app\http\controllers\ValidationController.php');
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use App\Http\Controllers\Controller;
- use App\Http\Controllers\ValidationController as Validation;
- use App\Http\Controllers\PickingAlgorithm as Algorithm;
- use App\Traits\Questions;
- use App\Styles;
- class StylePickerController extends Controller
- {
- use Questions;
- public $errors = false;
- public $errorMessage = array();
- public function __construct() {
- }
- /*
- * Custom functions
- */
- // Github: https://gist.github.com/yeco/412610
- private function array_push_assoc($array, $key, $value){
- $array[$key] = $value;
- return $array;
- }
- // Prints an output with <pre> styling
- public function printPre($data, $die = false) {
- $output = var_dump($data);
- echo "<pre>";
- print_r($output);
- echo "</pre>";
- if ($die) {
- die();
- }
- }
- /*
- * Show all the questions
- * return: view
- */
- public function showQuestions() {
- return view('index', ['questions' => Questions::$questions, 'accurate_questions' => Questions::$accurate_questions]);
- }
- // Pokaż odpowiedzi na pytania użytkownika
- public function showAnsweredQuestions() {
- return view('pickedstyles', []);
- }
- /*
- * Prepares a TPL for an e-mail
- * return: $mailTPL string
- */
- private function prepareEmailTemplate() : string {
- $mailTPL = '';
- $mailTPL .= '';
- $mailTPL .= '';
- return $mailTPL;
- }
- /*
- * Sends an e-mail if user wants to
- * return: bool
- */
- public function sendEmail() : bool {
- $validation = new Validation();
- $headers = 'From: jakiepiwomamwybrac@piwolucja.pl' . "\r\n" .
- 'Reply-To: jakiepiwomamwybrac@piwolucja.pl' . "\r\n";
- $subject = $_POST['username'] . ' oto 3 najlepsze style dla Ciebie';
- if ($validation->validateEmail()) {
- mail($_POST['email'], $subject, $this->prepareEmailTemplate(), $headers);
- return true;
- } else {
- $this->error('Wiadomosc');
- return false;
- }
- }
- private function setNewsletter() : integer {
- $validation = new Validation();
- if ($validation->validateEmail()) {
- $set_newsletter = 1;
- } else {
- $set_newsletter = 0;
- }
- // Intagracja z MailChimpem - wywołanie w InsertToDB
- return $set_newsletter;
- }
- private function prepareAnswers() : ?string {
- $answers = array();
- $validation = new Validation();
- for ($i = 1; $i <= 15; $i++) {
- if (is_null($_POST['answer-'.$i.''])) {
- continue;
- }
- $answers = $this->array_push_assoc($answers, 'answer-'.$i, $_POST['answer-'.$i.'']);
- }
- if (!$validation->validateAnswers($answers)) {
- $errorMessage[] = '';
- return false;
- } else {
- $JSON_answers = json_encode($answers); //JSON $_POST answers
- return $JSON_answers;
- }
- }
- // Wstawia do bazy odpowiedzi użytkownika
- // Rozdzielić na osobną funkcjędo bazy, osobną do wywołania innych rzeczy
- public function insertToDB() : string {
- $validation = new Validation();
- $name = $_POST['username'] ?? '';
- $email = $validation->validateEmail($_POST['email']) ?? '';
- ($_POST['newsletter'] == 'Tak') ? $newsletter = 1 : $newsletter = 0;
- if ($this->prepareAnswers() && empty($errorMessage)) {
- $insert_answers = DB::insert("INSERT INTO `user_answers` (name, e_mail, newsletter, answers, created_at)
- VALUES
- ('{$name}',
- '{$email}',
- '{$newsletter}',
- '{$this->prepareAnswers()}',
- CURRENT_TIMESTAMP)");
- if ($insert_answers) {
- if ($_POST['sendMeAnEmail']) {
- $this->sendEmail();
- }
- if ($_POST['newsletter'] === 1) {
- // DOdaj do listy newsletterowej
- // Mailchimp API
- }
- // Wywalamy komuś listę piw
- $algorithm = new Algorithm();
- $styles = $algorithm->picker();
- // Wrzutka wybranych stylów do bazy ze stylami
- // Insert
- return '';
- } else {
- return '';
- }
- } else {
- return '';
- }
- }
- // Pokazuje style z ostatniej wizyty
- public function showRecentlyPickedStyles() {
- }
- // 5 najczęściej wybieranych stylów
- public function showMostPickedStyles() {
- // Musi być tabela odkładająca wybrane użytkownikom style (zliczanie - jak logi)
- $mostly_picked = DB::select('SELECT id FROM styles GROUP BY count(id) AS mostlypicked ORDER BY mostlypicked DESC LIMIT 3;');
- }
- private function error(string $message) : array {
- // Funkcja do zliczania i złączania errorów
- }
- public function showErrors() {
- if ($errors && !empty($errorMessage)) {
- // Przekaż błedy do widoku
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement