Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.13 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. require_once('C:\xampp\htdocs\jakiepiwomamkupic\app\Questions.php');
  6. require_once('C:\xampp\htdocs\jakiepiwomamkupic\app\http\controllers\ValidationController.php');
  7.  
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use App\Http\Controllers\Controller;
  11. use App\Http\Controllers\ValidationController as Validation;
  12. use App\Http\Controllers\PickingAlgorithm as Algorithm;
  13. use App\Traits\Questions;
  14. use App\Styles;
  15.  
  16. class StylePickerController extends Controller
  17. {
  18.  
  19.     use Questions;
  20.  
  21.     public $errors = false;
  22.     public $errorMessage = array();
  23.  
  24.     public function __construct() {
  25.  
  26.  
  27.     }
  28.  
  29.     /*
  30.     * Custom functions
  31.     */
  32.  
  33.     // Github: https://gist.github.com/yeco/412610
  34.     private function array_push_assoc($array, $key, $value){
  35.         $array[$key] = $value;
  36.         return $array;
  37.     }
  38.  
  39.     // Prints an output with <pre> styling
  40.     public function printPre($data, $die = false) {
  41.  
  42.         $output = var_dump($data);
  43.  
  44.         echo "<pre>";
  45.         print_r($output);
  46.         echo "</pre>";
  47.  
  48.         if ($die) {
  49.             die();
  50.         }
  51.     }
  52.  
  53.     /*
  54.     * Show all the questions
  55.     * return: view
  56.     */
  57.     public function showQuestions() {
  58.  
  59.         return view('index', ['questions' => Questions::$questions, 'accurate_questions' => Questions::$accurate_questions]);
  60.  
  61.     }
  62.  
  63.     // Pokaż odpowiedzi na pytania użytkownika
  64.     public function showAnsweredQuestions() {
  65.  
  66.         return view('pickedstyles', []);
  67.  
  68.     }
  69.  
  70.     /*
  71.     * Prepares a TPL for an e-mail
  72.     * return: $mailTPL string
  73.     */
  74.     private function prepareEmailTemplate() : string {
  75.  
  76.         $mailTPL = '';
  77.         $mailTPL .= '';
  78.         $mailTPL .= '';
  79.  
  80.         return $mailTPL;
  81.  
  82.     }
  83.  
  84.     /*
  85.     * Sends an e-mail if user wants to
  86.     * return: bool
  87.     */
  88.     public function sendEmail() : bool {
  89.  
  90.         $validation = new Validation();
  91.  
  92.         $headers = 'From: jakiepiwomamwybrac@piwolucja.pl' . "\r\n" .
  93.         'Reply-To: jakiepiwomamwybrac@piwolucja.pl' . "\r\n";
  94.  
  95.         $subject = $_POST['username'] . ' oto 3 najlepsze style dla Ciebie';
  96.  
  97.         if ($validation->validateEmail()) {
  98.             mail($_POST['email'], $subject, $this->prepareEmailTemplate(), $headers);
  99.             return true;
  100.         } else {
  101.             $this->error('Wiadomosc');
  102.             return false;
  103.         }
  104.  
  105.     }
  106.  
  107.     private function setNewsletter() : integer {
  108.  
  109.         $validation = new Validation();
  110.  
  111.         if ($validation->validateEmail()) {
  112.             $set_newsletter = 1;
  113.         } else {
  114.             $set_newsletter = 0;
  115.         }
  116.  
  117.         // Intagracja z MailChimpem - wywołanie w InsertToDB
  118.  
  119.         return $set_newsletter;
  120.  
  121.     }
  122.  
  123.     private function prepareAnswers() : ?string {
  124.  
  125.         $answers = array();
  126.         $validation = new Validation();
  127.  
  128.         for ($i = 1; $i <= 15; $i++) {
  129.            
  130.             if (is_null($_POST['answer-'.$i.''])) {
  131.                 continue;
  132.             }
  133.  
  134.             $answers = $this->array_push_assoc($answers, 'answer-'.$i, $_POST['answer-'.$i.'']);
  135.         }
  136.  
  137.         if (!$validation->validateAnswers($answers)) {
  138.             $errorMessage[] = '';
  139.             return false;
  140.         } else {
  141.             $JSON_answers = json_encode($answers); //JSON $_POST answers
  142.             return $JSON_answers;
  143.         }
  144.  
  145.     }
  146.  
  147.     // Wstawia do bazy odpowiedzi użytkownika
  148.     // Rozdzielić na osobną funkcjędo bazy, osobną do wywołania innych rzeczy
  149.     public function insertToDB() : string {
  150.  
  151.         $validation = new Validation();
  152.  
  153.         $name = $_POST['username'] ?? '';
  154.         $email = $validation->validateEmail($_POST['email']) ?? '';
  155.         ($_POST['newsletter'] == 'Tak') ? $newsletter = 1 : $newsletter = 0;
  156.  
  157.         if ($this->prepareAnswers() && empty($errorMessage)) {
  158.             $insert_answers = DB::insert("INSERT INTO `user_answers` (name, e_mail, newsletter, answers, created_at)
  159.                                             VALUES
  160.                                         ('{$name}',
  161.                                         '{$email}',
  162.                                         '{$newsletter}',
  163.                                         '{$this->prepareAnswers()}',
  164.                                         CURRENT_TIMESTAMP)");
  165.             if ($insert_answers) {
  166.  
  167.                 if ($_POST['sendMeAnEmail']) {
  168.                     $this->sendEmail();
  169.                 }
  170.  
  171.                 if ($_POST['newsletter'] === 1) {
  172.                     // DOdaj do listy newsletterowej
  173.                     // Mailchimp API
  174.                 }
  175.  
  176.                 // Wywalamy komuś listę piw
  177.                 $algorithm = new Algorithm();
  178.                 $styles = $algorithm->picker();
  179.  
  180.                 // Wrzutka wybranych stylów do bazy ze stylami
  181.                 // Insert
  182.  
  183.                 return '';
  184.             } else {
  185.                 return '';
  186.             }
  187.         } else {
  188.             return '';
  189.         }
  190.  
  191.  
  192.     }
  193.  
  194.     // Pokazuje style z ostatniej wizyty
  195.     public function showRecentlyPickedStyles() {
  196.  
  197.     }
  198.  
  199.     // 5 najczęściej wybieranych stylów
  200.     public function showMostPickedStyles() {
  201.  
  202.         // Musi być tabela odkładająca wybrane użytkownikom style (zliczanie - jak logi)
  203.         $mostly_picked = DB::select('SELECT id FROM styles GROUP BY count(id) AS mostlypicked ORDER BY mostlypicked DESC LIMIT 3;');
  204.  
  205.  
  206.     }
  207.  
  208.     private function error(string $message) : array {
  209.  
  210.         // Funkcja do zliczania i złączania errorów
  211.  
  212.     }
  213.  
  214.     public function showErrors() {
  215.         if ($errors && !empty($errorMessage)) {
  216.             // Przekaż błedy do widoku
  217.         }
  218.     }
  219.  
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement