Ok, so I finally was able to convert my framework to the PSR-0 standards, but now I am getting errors in the sample application that I have not been getting before. I am not sure why, so I need a fresh pair of eyes. Any help is greatly appreciated. The two errors are:
Notice: Undefined property: tinyPHP\Classes\Controllers\User::$model in /Applications/MAMP/htdocs/tPHP/tinyPHP/Classes/Controllers/user.php on line 41
Fatal error: Call to a member function userList() on a non-object in /Applications/MAMP/htdocs/tPHP/tinyPHP/Classes/Controllers/user.php on line 41
So here are the three files that deal with user.
User Controller:
namespace tinyPHP\Classes\Controllers;
class User extends \tinyPHP\Classes\Core\Controller {
//public $model = null;
public function __construct() {
parent::__construct();
\tinyPHP\Classes\Core\Session::init();
$logged = \tinyPHP\Classes\Core\Session::get('loggedIn');
$role = \tinyPHP\Classes\Core\Session::get('role');
if ($logged == false || $role != 'owner') {
\tinyPHP\Classes\Core\Session::destroy();
header('location: ../login');
exit;
}
}
public function index() {
$this->view->userList = $this->model->userList();
$this->view->render('user/index');
}
public function create() {
$data = array();
$data['login'] = $_POST['login'];
$data['password'] = $_POST['password'];
$data['role'] = $_POST['role'];
$this->model->create($data);
header('location: ' . BASE_URL . 'user');
}
public function edit($id) {
$this->view->user = $this->model->userSingleList($id);
$this->view->render('user/edit');
}
public function editSave($id) {
$data = array();
$data['id'] = $id;
$data['login'] = $_POST['login'];
$data['password'] = $_POST['password'];
$data['role'] = $_POST['role'];
$this->model->editSave($data);
header('location: ' . BASE_URL . 'user');
}
public function delete($id) {
$this->model->delete($id);
header('location: ' . BASE_URL . 'user');
}
}
User Model:
namespace tinyPHP\Classes\Models;
class UserModel {
private $db;
public function __construct() {
$this->db = new \tinyPHP\Classes\Core\MySQLiDriver();
$this->db->conn();
}
public function userList() {
$q = $this->db->select(TP . 'user', 'id,login,role', null, null);
while($r = $q->fetch_assoc()) {
$array[] = $r;
}
return $array;
}
public function userSingleList($id) {
$q = $this->db->select(TP . "user", "id,login,role", "id = '$id'", null);
while($r = $q->fetch_assoc()) {
$array[] = $r;
}
return $array;
}
public function create($data) {
$tp_hasher = new PasswordHash(8, FALSE);
$this->db->insert('user', array(
$this->db->escape( $data['login'] ),
$tp_hasher->HashPassword( $this->db->escape( $data['password'] ) ),
$this->db->escape( $data['role'] )
),
'login,
password,
role');
}
public function editSave($data) {
$tp_hasher = new PasswordHash(8, FALSE);
$pass = $this->db->escape($data['password']);
$login = $this->db->escape( $data['login'] );
$password = $tp_hasher->HashPassword( $pass );
$role = $this->db->escape( $data['role'] );
$id = $data['id'];
$this->db->query( "UPDATE " . TP . "user SET login='$login',password='$password',role='$role' WHERE id = '$id' ");
}
public function delete($id) {
$q = $this->db->query("SELECT role FROM " . TP . "user WHERE id = '$id'");
$r = $q->fetch_array();
if ($r['role'] == 'owner')
return false;
$this->db->delete('user', "id = '$id'");
}
public function __destruct() {
$this->db->disconnect();
}
}
User View:
<h1><?php echo _t( 'User' ); ?></h1>
<form method="post" action="<?php echo BASE_URL;?>user/create">
<label><?php echo _t( 'Login' ); ?></label><input type="text" name="login" /><br />
<label><?php echo _t( 'Password' ); ?></label><input type="text" name="password" /><br />
<label><?php echo _t( 'Role' ); ?></label>
<select name="role">
<option value="default"><?php echo _t( 'Default' ); ?></option>
<option value="admin"><?php echo _t( 'Admin' ); ?></option>
</select><br />
<label> </label><input type="submit" />
</form>
<hr />
<table>
<?php
foreach($this->userList as $key => $value) {
echo '<tr>';
echo '<td>' . $value['id'] . '</td>';
echo '<td>' . $value['login'] . '</td>';
echo '<td>' . $value['role'] . '</td>';
echo '<td>
<a href="'.BASE_URL.'user/edit/'.$value['id'].'">' . _t( 'Edit' ) . '</a>
<a href="'.BASE_URL.'user/delete/'.$value['id'].'">' . _t( 'Delete' ) . '</a></td>';
echo '</tr>';
}
?>
</table>