/** * This method is a general method for getting DB data. * We use it to execute SQL Query and to set error message if there was an error. * * @param $queryId - The query id to execute * @param null $params - List of params to bind to the prepare statment. * * More details can be found in the DBLayer class since this method is a wrapper for $dbLayer->executeQuery * * @link DBLayer */ public function getData($queryId, $params = null) { $dbLayer = DBLayer::getInstance(); $data = $dbLayer->executeQuery($queryId, $params); if (isset($_REQUEST['DBLayer.executeQuery.error'])) { $_REQUEST['backoffice.error'] = $_REQUEST['DBLayer.executeQuery.error'][2]; } else { $_REQUEST['backoffice.data'] = $data; } }
/** * Load the user playlist info. * The function process teh results form the query and set the playlist as an array. */ private function loadPlaylist() { $dbLayer = DBLayer::getInstance(); $this->playlist = null; // load the user playlist $data = $dbLayer->executeQuery('users.playlist', array(':user_id' => $this->userId)); if ($data) { // Clear previous data - if any $items = array(); // Get all the playlist records foreach ($data as $item) { array_push($items, $item); } $this->playlist = $items; } }
/** * Load songs of the given playlist. * The playlist id is extracted from the request and the data is store as REQUEST['songs'] */ public function loadSongs() { $dbLayer = DBLayer::getInstance(); $data = $dbLayer->executeQuery('playlist.songs', array(':pId' => Utils::getParam("pId"))); if ($data) { $_REQUEST['songs'] = $data; } }
<?php use Moood\DBLayer; use Moood\Bootstrap; use Moood\helpers\Utils; use Moood\User\UserActions; $ROOT_PATH = $_SERVER['DOCUMENT_ROOT']; include_once $ROOT_PATH . '/src/bootstrap.php'; // Init the DBlayer to verify that we have a valid DB $dbLayer = DBLayer::getInstance(); // The instance of the class that responsible of processing this page actions $actions = new UserActions(); // First of all logout the current user since we are in the login page $actions->logout(); // Now process the given action $actions->processRequest(); // Check if we have errors or not $error = Utils::getParam('error', null); $errorClass = isset($error) ? '' : 'hidden'; ?> <!DOCTYPE html > <html> <head> <meta charset='UTF-8'> <title>Music for your mood</title> <link href="/style/style.css" rel="stylesheet" type="text/css"/> </head> <body> <div class="pageContent login">
/** * This method check to see that the given credentials are valid. * Once user is logged in we will load his data */ public function login() { // Get the form values // The password is encrypted using sha1. // We could have used some stronger method like adding a prefix and then encode it and verify it // but since this a demo project this is not a issue here in my opnion $params = array(':username' => Utils::getParam('username'), ':password' => sha1(Utils::getParam('password'))); // Load the user details $data = DBLayer::getInstance()->executeQuery('users.select_user', $params); // Check to see if we have a valid user or not if ($data) { $userData = $data[0]; // Get the userId $_SESSION['userId'] = $userData['id']; // Set the user details $_SESSION['user'] = new User($userData['id']); // Make sure all session content is flushed before redirected session_write_close(); // We found the user login valid - redirect to the application page. header("Location: /views/playlist/playlist.php"); exit; } else { $_REQUEST['error'] = 'Wrong user name/password. Please try again'; } }