4 Commits a0ea45f3f0 ... f3ab59584c

Auteur SHA1 Message Date
  Ecconia f3ab59584c Adding database connector il y a 7 ans
  Ecconia 437c9b3ffc CodeStyle Add public to constructor il y a 7 ans
  Ecconia 592d9140b9 Adding options to app inputtypes il y a 7 ans
  Ecconia 10b4700791 Getter for app structure il y a 7 ans

+ 4 - 0
.gitignore

@@ -1,3 +1,7 @@
 #Composer and bibs
 /vendor/
 composer.lock
+
+#Database credentials:
+/conf/db/root.php
+/conf/db/user.php

+ 8 - 0
conf/db/sample-credentials.php

@@ -0,0 +1,8 @@
+<?php
+
+return [
+	'username' => 'root',
+	'password' => 'password',
+	'host' => 'localhost',
+	'database' => 'your-db'
+];

+ 21 - 5
src/Application/AppPattern.php

@@ -6,7 +6,7 @@ class AppPattern {
 	public $instruction;
 	public $fields;
 
-	function __construct() {
+	public function __construct() {
 		$this->instruction = 'First impressions are important. Please tell us about yourself. A well-written application is likely to speed up your approval on the server, while a poorly written one might not get approved at all. 2-3 well-written sentences per question is recommended.';
 		
 		$this->fields = json_decode('[
@@ -19,8 +19,12 @@ class AppPattern {
 				{
 					"text": "Age?",
 					"hint": "How old are you? Please leave blank if you don\'t want to tell us!",
+					"optional": true,
 					"type": "age",
-					"optional": true
+					"options": {
+						"min": 1,
+						"max": 150
+					}
 				},
 				{
 					"text": "About how often do you play Minecraft?",
@@ -31,19 +35,31 @@ class AppPattern {
 					"text": "Why are you interested in joining this server?",
 					"hint": "Why would you like to join this server? What can you offer to others on the server? What can this server offer you that other servers can not? Please describe in detail.",
 					"optional": false,
-					"type": "text-box-limited"
+					"type": "text-box-limited",
+					"options": {
+						"min": 270,
+						"steps": 10
+					}
 				},
 				{
 					"text": "Current Redstone knowledge",
 					"hint": "What are your current limits in redstone? Tell us anything you know concerning redstone.",
 					"optional": false,
-					"type": "text-box-limited"
+					"type": "text-box-limited",
+					"options": {
+						"min": 270,
+						"steps": 10
+					}
 				},
 				{
 					"text": "Past Redstone Experience",
 					"hint": "Describe your best Redstone creation(s) to date. Tell us how they work and what kind of Redstone logic was used.",
 					"optional": false,
-					"type": "text-box-limited"
+					"type": "text-box-limited",
+					"options": {
+						"min": 270,
+						"steps": 10
+					}
 				},
 				{
 					"text": "What kind of creations would you like to build on this server?",

+ 1 - 1
src/Application/Application.php

@@ -12,7 +12,7 @@ class Application {
 	public $state;
 	public $time;
 
-	function __construct(User $user, AppPattern $pattern, int $time, Array $answers, $state) {
+	public function __construct(User $user, AppPattern $pattern, int $time, Array $answers, $state) {
 		$this->user = $user;
 		$this->pattern = $pattern;
 		$this->answers = $answers;

+ 104 - 0
src/DB/Database.php

@@ -0,0 +1,104 @@
+<?php
+	namespace RS\DB;
+	
+	use PDO;
+	
+	/**
+	 * Database
+	 * Access databases using PDO and MySQL. Supports prepared statements.
+	 * 
+	 * @package de.it-talent.QuickTalk.lib.base
+	 * @author GIDIX
+	 */
+	class Database {
+		private $db;
+		public $lastQuery;
+
+		public function __construct($host, $username, $password, $database) {
+			$this->db = new PDO('mysql:host=' . $host . ';dbname=' . $database . ';charset=utf8', $username, $password);
+			$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+			$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+		}
+
+		/**
+		 * 	Send a query.
+		 *  This method supports prepared statements. Just write ? or use placeholders, like ':id' in your
+		 *  $sql and provide $args with its values.
+		 * 
+		 *  @param string $sql Query to send.
+		 *  @param array $args Params to bind, if preparing.
+		 *  
+		 *  @return PDOStatement PDO Statement
+		 */
+		public function query($sql, array $args = null) {
+			try {
+				if (!is_null($args)) {
+					$this->lastQuery = $this->db->prepare($sql);
+					$this->lastQuery->execute($args);
+				} else {
+					$this->lastQuery = $this->db->query($sql);
+				}
+
+				return $this->lastQuery;
+			} catch (PDOException $e) {
+				$this->error($e->getMessage(), $sql, $e->getCode());
+			}
+		}
+
+		/**
+		 * 	Prepare a statement and return it without executing.
+		 * 
+		 *  @param string $query Query to send.
+		 */
+		public function prepare($sql) {
+			try {
+				return $this->db->prepare($sql);
+			} catch (PDOException $e) {
+				$this->error($e->getMessage(), $sql, $e->getCode());
+			}
+		}
+
+		public function fetchObject(\PDOStatement $stmt = null) {
+			if (!is_null($stmt)) {
+				return $stmt->fetch(PDO::FETCH_OBJ);
+			} else {
+				return $this->lastQuery->fetch(PDO::FETCH_OBJ);
+			}
+		}
+
+		public function numRows(\PDOStatement $stmt = null) {
+			if (!is_null($stmt)) {
+				return $stmt->rowCount();
+			} else {
+				return $this->lastQuery->rowCount();
+			}
+		}
+
+		public function insertID() {
+			return $this->db->lastInsertId();
+		}
+
+		public function beginTransaction() {
+			return $this->db->beginTransaction();
+		}
+
+		public function commit() {
+			return $this->db->commit();
+		}
+
+		public function rollback() {
+			return $this->db->rollBack();
+		}
+
+		protected function error($msg, $sql, $code) {
+			//Functions::log(Functions::LOG_ERROR, $msg . '<br /><br /><code class="important">' . $sql . '</code>');
+			//TODO: Throw exception!
+			die('
+				<h1>Database Error ('.$code.')</h1>
+
+				'.(!is_null($sql) ? '<code>' . $sql . '</code>' : '').'
+				<p>'.$msg.'</p>
+			');
+		}
+	}
+?>

+ 19 - 0
src/DB/DatabasePool.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace RS\DB;
+
+use RS\DB\Database;
+
+class DatabasePool {
+	private static $db;
+
+	public static function getDB() {
+		if(!self::$db) {
+			//TODO: Root PATH class
+			$cred = require __DIR__ . '/../../conf/db/user.php';
+			self::$db = new Database($cred['host'], $cred['username'], $cred['password'], $cred['database']);
+		}
+
+		return self::$db;
+	}
+}

+ 10 - 0
src/Handler/MyHandler.php

@@ -15,6 +15,7 @@ use Psr\Http\Message\{
 };
 
 use RS\DummyData;
+use RS\Application\AppPattern;
 
 class MyHandler extends Handler {
 	public function soos(ServerRequestInterface $request, ResponseInterface $response, \stdClass $args): ResponseInterface {
@@ -125,6 +126,14 @@ class MyHandler extends Handler {
 		return $response->withJson($a[$id]);
 	}
 
+	public function appPattern(ServerRequestInterface $request, ResponseInterface $response, \stdClass $args): ResponseInterface {
+		$appPattern = new AppPattern();
+
+		$obj['instruction'] = $appPattern->instruction;
+		$obj['fields'] = $appPattern->fields;
+		return $response->withJson($obj);
+	}
+
 	public static function getRoutes(): array {
 		return [
 			new Route('GET', '/soos', 'soos'),
@@ -133,6 +142,7 @@ class MyHandler extends Handler {
 			new Route('GET', '/applications', 'applications'),
 			new Route('GET', '/dummies', 'dummies'),
 			new Route('GET', '/application/{id:[0-9]+}', 'application'),
+			new Route('GET', '/app-pattern', 'appPattern'),
 		];
 	}
 }

+ 1 - 1
src/User.php

@@ -5,7 +5,7 @@ namespace RS;
 class User {
 	public $username;
 
-	function __construct(string $username) {
+	public function __construct(string $username) {
 		$this->username = $username;
 	}
 }