Skip to content

Instantly share code, notes, and snippets.

@gmann1982
Created February 24, 2014 20:21
Show Gist options
  • Select an option

  • Save gmann1982/9196224 to your computer and use it in GitHub Desktop.

Select an option

Save gmann1982/9196224 to your computer and use it in GitHub Desktop.
<?php
/**
*
*/
class Dealer
{
public function buildSqlQuery($sql, $distinct_field=false, $where_field=array(), $order_field=array(), $limit=false) {
if (!empty($where_field)) {
$sql .= " WHERE ";
foreach ($where_field as $k => $w) {
if (!is_array($w)) {
if (preg_match("/[<=>]+|\!=|LIKE.*/", $w)) {
$sql .= $k." ".$w;
} else {
$sql .= $k." = '".$w."'";
}
} else {
$sql .= "(";
foreach ($w as $sw) {
if (preg_match("/[<=>]+|\!=|LIKE.*/", $sw[1])) {
$sql .= $sw[0]." ".$sw[1]." OR ";
} else {
$sql .= $sw[0]." = ".$sw[1]." OR ";
}
}
$sql = substr($sql, 0, -4);
$sql .= ")";
}
$sql .= " AND ";
}
$sql = substr($sql, 0, -5);
}
$sql .= (!empty($distinct_field) ? " GROUP BY ".$distinct_field : "");
if (!empty($order_field)) {
$sql .= " ORDER BY ";
foreach ($order_field as $key => $value) {
$sql .= $key." ".$value;
$sql .= ", ";
}
if(!empty($limit)){
$sql .= "LIMIT ".$limit;
}
$sql = substr($sql, 0, -2);
}
return $sql;
}
public function fetchDealer($where_field=array(), $order_field=array(), $distinct_field=false) {
try {
global $database;
$sql = "SELECT * FROM tbl_dealers";
$sql = $this->buildSqlQuery($sql, $distinct_field, $where_field, $order_field);
$q = $database->query($sql);
$result = $q->fetch(PDO::FETCH_OBJ);
if(empty($result)) {
return array();
} else {
return $result;
}
} catch(Exception $e) {
$this->error = "Could not fetch user data using function fetchDealer\n";
$this->error .= "The following exception was given: ".$e;
echo $this->error;
}
}
public function fetchDealerCompany($where_field=array(), $order_field=array(), $distinct_field=false) {
try {
global $database;
$sql = "SELECT * FROM tbl_company";
$sql = $this->buildSqlQuery($sql, $distinct_field, $where_field, $order_field);
$q = $database->query($sql);
$result = $q->fetch(PDO::FETCH_OBJ);
if (empty($result)) {
return array();
} else {
return $result;
}
} catch(Exception $e) {
$this->error = "Could not fetch user data using function fetchDealer\n";
$this->error .= "The following exception was given: ".$e;
echo $this->error;
}
}
public function fetchDealers($where_field=array(), $order_field=array(), $distinct_field=false)
{
try {
global $database;
$sql = "SELECT * FROM tbl_dealers";
$sql = $this->buildSqlQuery($sql, $distinct_field, $where_field, $order_field);
$q = $database->query($sql);
while ($result = $q->fetch(PDO::FETCH_ASSOC)) {
$array = array();
foreach ($result as $key => $val) {
$array[$key] = $val;
}
$data[] = $array;
$this->num_rows++;
}
if (empty($data)) {
return array();
} else {
return $data;
}
} catch(Exception $e) {
$this->error = "Could not fetch user data using function fetchDealers\n";
$this->error .= "The following exception was given: ".$e;
echo $this->error;
}
}
public function getDealersByFirstLetter($where_field=array(), $order_field=array(), $distinct_field=false)
{
try {
global $database;
$sql = "SELECT * FROM tbl_dealers";
$sql = $this->buildSqlQuery($sql, $distinct_field, $where_field, $order_field);
// echo $sql;
$q = $database->query($sql);
while ($result = $q->fetch(PDO::FETCH_ASSOC)) {
$array = array();
foreach ($result as $key => $val) {
$array[$key] = $val;
}
$data[] = $array;
$this->num_rows++;
}
if (empty($data)) {
return array();
} else {
return $data;
}
} catch(Exception $e) {
$this->error = "Could not fetch user data using function fetchDealers\n";
$this->error .= "The following exception was given: ".$e;
echo $this->error;
}
}
public function updateActiveStatus() {
$email = array( 'enabled' => stripslashes(filter_var($_POST['enabled'])),
'id' => stripslashes(filter_var($_POST['dealerId'])));
try {
global $database;
$sql = "UPDATE tbl_dealers SET enabled = ?
WHERE id = ?";
$q = $database->prepare($sql);
$q->execute(array_values($email));
return true;
} catch(Exception $e) {
$this->error = "Could not add client data using function updateActiveStatus\n";
$this->error .= "The following exception was given: ".$e;
echo $this->error;
}
}
public function updatePassword($dealerId) {
try {
global $database;
$sql = "SELECT password FROM tbl_dealers WHERE id = $dealerId";
$q = $database->query($sql);
$result = $q->fetch(PDO::FETCH_OBJ);
if( $result->password == sha1($_POST['currentPassword']) )
{
$data = array( 'password' => sha1($_POST['newPassword']),
'passwordLastUpdated' => time(),
'id' => $dealerId);
$sql = "UPDATE tbl_dealers SET password = ?, passwordLastUpdated = ?
WHERE id = ?";
$q = $database->prepare($sql);
$q->execute(array_values($data));
return true;
}
else
{
return false;
}
} catch(Exception $e) {
$this->error = "Could not add client data using function updateActiveStatus\n";
$this->error .= "The following exception was given: ".$e;
echo $this->error;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment