<?php
require_once "AppModel.php";
require_once "Helper.php";
require_once "Session.php";
class User extends AppModel {
protected $table = 'user';
protected $alias = 'User';
protected $rules = array(
"email" => array(
"form" => array(
"type" => "text"
),
"notEmpty" => array(
"rule" => "notEmpty",
"message" => MSG_ERR_NOTEMPTY
),
"isEmail" => array(
"rule" => "email",
"message" => MSG_ERR_EMAIL
)
),
"password" => array(
"form" => array(
"type" => "password"
),
"notEmpty" => array(
"rule" => "notEmpty",
"message" => MSG_ERR_NOTEMPTY
)
),
"fullname" => array(
"form" => array(
"type" => "text"
),
"notEmpty" => array(
"rule" => "notEmpty",
"message" => MSG_ERR_NOTEMPTY
)
),
"address" => array(
"form" => array(
"type" => "textarea"
),
"notEmpty" => array(
"rule" => "notEmpty",
"message" => MSG_ERR_NOTEMPTY
)
)
);
public function __construct() {
parent::__construct();
}
public function login($data) {
// array (
// 'Model' =>
// array (
// 'email' => 'admin@gmail.com',
// 'password' => '123456',
// ),
// )
$exists = $this->find(array(
'conditions' => array(
'email' => $data[$this->alias]['email'],
'password' => Helper::hash($data[$this->alias]['password'])
)
), 'first');
if (!empty($exists)) {
$this->session->write(USER_INFO, $exists);
$this->session->write(LOGGED_IN, true);
return true;
}
return false;
}
public function find($conditions, $first = 'all') {
$results = $this->db->select($this->table, $conditions);
if (!empty($results) && $first == 'first') {
return $results[0];
}
return $results;
}
function select($myTable, $options = array(), $isCount = false) {
// Get options by parameters
$myFields = isset($options['fields']) ? $options['fields'] : '*';
$conditions = isset($options['conditions']) ? $options['conditions'] : null;
$orders = isset($options['orders']) ? $options['orders'] : null;
$groups = isset($options['groups']) ? $options['groups'] : null;
$mJoins = isset($options['joins']) ? $options['joins'] : null;
$mlimit = isset($options['limit']) ? $options['limit'] : false;
$moffset = isset($options['offset']) ? $options['offset'] : false;
try {
$returnArr = array();
$table = $myTable;
$alias = $joins = $order = $group = $limit = "";
$fields = "";
if (is_array($myFields)) {
$fields = implode(', ', $myFields);
} else {
$fields = $myFields;
}
if (isset($mJoins) && is_array($mJoins)) {
foreach ($mJoins as $jTable => $join) {
if (empty($jTable) || empty($join['join_key']) || empty($join['main_key']) || !isset($join['join_fields']) || !isset($join['join_fields'][1]))
continue;
$fields .= ", " . $jTable . "." . $join['join_fields'][0] . ", " . $jTable . "." . $join['join_fields'][1];
}
}
$conditions = $this->buildConditions($conditions);
$order = $this->buildOrders($orders);
$group = $this->buildGroups($groups);
$joins = $this->buildJoins($myTable, $mJoins);
$tmpTable = explode('_', $table);
$alias = array();
foreach ($tmpTable as $tmp) {
$alias[] = ucfirst($tmp);
}
$alias = implode($alias);
$query = compact('table', 'alias', 'joins', 'fields', 'conditions', 'joins', 'group', 'order', 'limit');
$sql = $this->renderStatement('select', $query);
$sql = $this->setLimit($sql, $mlimit, $moffset);
if ($isCount)
$returnArr = $this->fetchRow($sql);
else
$returnArr = $this->fetchAll($sql);
}
catch (Exception $ex) {
var_dump($ex);
}
return $returnArr;
}
}
<?php
if (!class_exists("Mysql")) require_once "Mysql.php";
if(!class_exists('Form')) require_once 'Form.php';
class AppModel {
public $form = null;
protected $rules = null;
protected $db = null;
protected $table = '';
protected $alias = null;
public function __construct() {
$this->db = Mysql::getInstance();
$this->form = new Form();
$this->form->setModel($this->alias);
$this->form->setRules($this->rules);
}
};
?>
<?php
class Form {
private $model = 'Model';
protected $rules = null;
public $inputs = array();
private $errors = null;
public function input($name) {
$type = 'text';
foreach ($this->inputs as $field => $input) {
if ($field == $name) {
$type = $input['type'];
$style = isset($input['style']) ? ' style="' . $input['style'] . '"' : '';
switch ($type) {
case 'text':
$inputField = '<input type="text"' . $style . ' name="data[' . $this->model . '][' . $name . ']"' . ' value="' . (isset($this->data[$this->model][$name]) ? $this->data[$this->model][$name] : '') . '" />';
break;
case 'password':
$inputField = '<input type="password"' . $style . ' name="data[' . $this->model . '][' . $name . ']"' . ' value="' . (isset($this->data[$this->model][$name]) ? $this->data[$this->model][$name] : '') . '" />';
break;
}
}
}
echo $inputField;
}
public function setRules($rules) {
if (!empty($rules)) {
$this->rules = $rules;
foreach ($rules as $field => $rule) {
if (isset($rule['form'])) {
$this->inputs[$field] = $rule['form'];
}
}
}
}
public function error($field) {
if (!empty($this->errors[$field])) {
echo '<p class="err">' . $this->errors[$field] . '</p>';
}
}
public function setModel($model) {
$this->model = $model;
}
}
?>