From 08e6ca70604aff5169dbcbf5b74215628ae4097e Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 27 Jul 2016 16:18:13 +0200 Subject: Reorganise to have client use Model --- classes/client.php | 95 ++-------------------------------------- include/assignments-overview.php | 4 +- include/assignments-view.php | 2 +- include/clients-new.php | 2 +- include/clients-overview.php | 14 +++--- include/clients.php | 6 +-- include/contacts-overview.php | 4 +- include/discounts-overview.php | 4 +- include/home.php | 6 +-- include/offers-overview.php | 4 +- nav.php | 2 +- 11 files changed, 28 insertions(+), 115 deletions(-) diff --git a/classes/client.php b/classes/client.php index f45f562..46e2da2 100644 --- a/classes/client.php +++ b/classes/client.php @@ -24,78 +24,10 @@ /** * An interface to the client table in the database */ -class client { - /** - * @var pdo $pdo The PDO class for database communication - * @var int $id The id of the client - * @var string $name The name of the client - */ - protected $pdo, $id, $name; - - /** - * Create a new instance - * - * @param PDO $pdo The PDO class, to access the database - * @param int $id The id of the client to fetch - * - * @throws PDOException If something went wrong with the database - * @throws Exception If the client could not be found - */ - public function __construct($pdo, $id) { - $this->pdo = $pdo; - - $stmt = $this->pdo->prepare("SELECT * FROM `".constants::db_prefix."client` WHERE `id`=?"); - $stmt->execute(array($id)); - if ($stmt->rowCount() == 0) { - throw new Exception("The client with id '$id' could not be found."); - } - $client = $stmt->fetch(PDO::FETCH_ASSOC); - - $this->id = $client['id']; - $this->name = $client['name']; - } - - //------------------------------------------------------------------------------ - // Getters and setters - //------------------------------------------------------------------------------ - - /** - * Get the ID of the client - * - * @return int The ID - */ - public function getId() { - return $this->id; - } - - /** - * Get the name of the client - * - * @return string The name - */ - public function getName() { - return $this->name; - } - - /** - * Set the name of the client - * - * @param string $name The new name for the client - * - * @throws PDOException If something went wrong with the database - * - * @return bool True on succes, false on failure - */ - public function setName($name) { - $stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."client` SET `name`=? WHERE `id`=?"); - $stmt->execute(array($name, $this->id)); - if ($stmt->rowCount() == 1) { - $this->name = $name; - return true; - } else { - return false; - } - } +class client extends Model { + public + $table = 'client', + $fillable_columns = ['name']; /** * Get all contact ids for this client @@ -137,25 +69,6 @@ class client { // Other functions //------------------------------------------------------------------------------ - /** - * Remove this client from the database - * - * If this doesn't succeed (i.e. false is returned), that means the client was removed manually or by another instance of this class - * - * @throws PDOException If something went wrong with the database - * - * @return bool True on success, false on failure - */ - public function delete() { - $stmt = $this->pdo->prepare("DELETE FROM `".constants::db_prefix."client` WHERE `id`=?"); - $stmt->execute(array($this->id)); - if ($stmt->rowCount() != 1) { - return false; - } else { - return true; - } - } - /** * Make a new contact for this client * diff --git a/include/assignments-overview.php b/include/assignments-overview.php index 2de6858..ee46928 100644 --- a/include/assignments-overview.php +++ b/include/assignments-overview.php @@ -48,7 +48,7 @@ require_once('./login.php'); #{$assignment->getOffer()->getId()} to {$assignment->getOffer()->getContact()->getName()} - ({$assignment->getOffer()->getContact()->getClient()->getName()}) + ({$assignment->getOffer()->getContact()->getClient()->name}) {$assignment->getTitle()}

{$assignment->getDescription(false)}

@@ -86,7 +86,7 @@ require_once('./login.php'); diff --git a/include/assignments-view.php b/include/assignments-view.php index 7a2923e..f2717b0 100644 --- a/include/assignments-view.php +++ b/include/assignments-view.php @@ -45,7 +45,7 @@ $_assignment = new assignment($_pdo, $_id); #{$_assignment->getOffer()->getId()} to {$_assignment->getOffer()->getContact()->getName()} - ({$_assignment->getOffer()->getContact()->getClient()->getName()}) + ({$_assignment->getOffer()->getContact()->getClient()->name}) {$_assignment->getHours()}h diff --git a/include/clients-new.php b/include/clients-new.php index b073b8e..e87557e 100644 --- a/include/clients-new.php +++ b/include/clients-new.php @@ -30,7 +30,7 @@ try { $response->message = "The client could not be created due to an error."; } else { $response->success = true; - $response->message = "Client '{$client->getName()}' has been created. Refresh the page."; + $response->message = "Client '{$client->name}' has been created. Refresh the page."; } } catch (PDOException $e) { $response->success = false; diff --git a/include/clients-overview.php b/include/clients-overview.php index fc2c3a0..58fad49 100644 --- a/include/clients-overview.php +++ b/include/clients-overview.php @@ -37,17 +37,17 @@ require_once('./login.php'); $clients = BusinessAdmin::getClients($_pdo); foreach ($clients as $client) { echo " - {$client->getId()} + data-mixer-order-id='{$client->id}' + data-mixer-order-name='{$client->name}'> + {$client->id} - - {$client->getName()} + + {$client->name} - - + + "; } diff --git a/include/clients.php b/include/clients.php index 88608ab..b07a7c8 100644 --- a/include/clients.php +++ b/include/clients.php @@ -46,7 +46,7 @@ require('./header.php'); $id = (int) $_GET['id']; try { $client = new client($_pdo, $id); - $header = "Clients / {$client->getName()}"; + $header = "Clients / {$client->name}"; $show_individual = $id; } catch (PDOException $e) { $alert = "
The client with id $id could not be found.
"; @@ -66,9 +66,9 @@ require('./header.php'); try { $client = new client($_pdo, $id); if ($client->delete()) { - echo "
The client with name {$client->getName()} has been removed.
"; + echo "
The client with name {$client->name} has been removed.
"; } else { - echo "
The client with name {$client->getName()} could not be removed. Perhaps it's already removed?
"; + echo "
The client with name {$client->name} could not be removed. Perhaps it's already removed?
"; } } catch (PDOException $e) { echo "
The client could not be removed due to a PDO error.
"; diff --git a/include/contacts-overview.php b/include/contacts-overview.php index c1f282b..9cd71f3 100644 --- a/include/contacts-overview.php +++ b/include/contacts-overview.php @@ -44,7 +44,7 @@ require_once('./login.php'); {$contact->getId()} {$contact->getName()}
- ({$contact->getClient()->getName()}) + ({$contact->getClient()->name}) {$contact->getEmail()}
@@ -85,7 +85,7 @@ require_once('./login.php'); diff --git a/include/discounts-overview.php b/include/discounts-overview.php index f9a3630..d4d8245 100644 --- a/include/discounts-overview.php +++ b/include/discounts-overview.php @@ -46,7 +46,7 @@ require_once('./login.php'); #{$discount->getOffer()->getId()} to {$discount->getOffer()->getContact()->getName()} - ({$discount->getOffer()->getContact()->getClient()->getName()}) + ({$discount->getOffer()->getContact()->getClient()->name}) {$discount->getTitle()}

{$discount->getDescription(false)}

@@ -83,7 +83,7 @@ require_once('./login.php'); diff --git a/include/home.php b/include/home.php index 51d899b..a9dca1e 100644 --- a/include/home.php +++ b/include/home.php @@ -165,7 +165,7 @@ require('./header.php'); 'start' => $start, 'end' => $end, 'id' => $offer->getId(), - 'contactClientName' => $offer->getContact()->getClient()->getName(), + 'contactClientName' => $offer->getContact()->getClient()->name, 'percentage' => $percentage, 'price_excl' => constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL), 'price_incl' => constants::invoice_valuta . $offer->calculate(offer::TOTAL) @@ -208,7 +208,7 @@ require('./header.php'); foreach ($offers as $offer) { echo ""; echo "{$offer->getId()}"; - echo "{$offer->getContact()->getClient()->getName()}"; + echo "{$offer->getContact()->getClient()->name}"; echo "".BusinessAdmin::formatDate($offer->getEndDate(), false).""; echo ""; } @@ -249,7 +249,7 @@ require('./header.php'); foreach ($offers as $offer) { echo ""; echo "{$offer->getId()}"; - echo "{$offer->getContact()->getClient()->getName()}"; + echo "{$offer->getContact()->getClient()->name}"; echo "".BusinessAdmin::formatDate($offer->getInvoiceDate(), false).""; echo ""; } diff --git a/include/offers-overview.php b/include/offers-overview.php index 7fd2de2..a246190 100644 --- a/include/offers-overview.php +++ b/include/offers-overview.php @@ -43,7 +43,7 @@ require_once('./login.php'); echo " {$offer->getId()} - {$offer->getContact()->getName()} + {$offer->getContact()->getName()} "; foreach ($offer->getAssignments() as $assignment) { echo "{$assignment->getTitle()}
(".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)

{$assignment->getDescription()}

"; @@ -120,7 +120,7 @@ require_once('./login.php'); diff --git a/nav.php b/nav.php index 3b67dbb..caeb6e5 100644 --- a/nav.php +++ b/nav.php @@ -57,7 +57,7 @@ 'start' => $start, 'end' => $end, 'id' => $offer->getId(), - 'contactClientName' => $offer->getContact()->getClient()->getName(), + 'contactClientName' => $offer->getContact()->getClient()->name, 'percentage' => $percentage, 'price' => constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL) ); -- cgit v1.2.3