aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2016-07-27 16:18:13 +0200
committerCamil Staps2016-07-27 16:18:13 +0200
commit08e6ca70604aff5169dbcbf5b74215628ae4097e (patch)
tree2fabea3a1bfd1ebf2d3ab3aa02a93aa8a51a779c
parentInitial Model class (diff)
Reorganise to have client use Model
-rw-r--r--classes/client.php95
-rw-r--r--include/assignments-overview.php4
-rw-r--r--include/assignments-view.php2
-rw-r--r--include/clients-new.php2
-rw-r--r--include/clients-overview.php14
-rw-r--r--include/clients.php6
-rw-r--r--include/contacts-overview.php4
-rw-r--r--include/discounts-overview.php4
-rw-r--r--include/home.php6
-rw-r--r--include/offers-overview.php4
-rw-r--r--nav.php2
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
@@ -138,25 +70,6 @@ class client {
//------------------------------------------------------------------------------
/**
- * 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
*
* @param string $name The name for this contact
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');
<td class='col-min-width'>
<a href='".constants::url_internal."/offers?id={$assignment->getOffer()->getId()}'>#{$assignment->getOffer()->getId()}</a> to
<a href='".constants::url_internal."/contacts?id={$assignment->getOffer()->getContact()->getId()}'>{$assignment->getOffer()->getContact()->getName()}</a>
- (<a href='".constants::url_internal."/clients?id={$assignment->getOffer()->getContact()->getClient()->getId()}'>{$assignment->getOffer()->getContact()->getClient()->getName()}</a>)
+ (<a href='".constants::url_internal."/clients?id={$assignment->getOffer()->getContact()->getClient()->id}'>{$assignment->getOffer()->getContact()->getClient()->name}</a>)
<td class='col-max-width'>
<b><a href='#' class='editable' id='editable-assignment-{$assignment->getId()}-title' data-type='text' data-pk='{$assignment->getId()}' data-url='".constants::url_external."assignments/edit'>{$assignment->getTitle()}</a></b><br/>
<p><a href='#' class='editable editable-noshow' id='editable-assignment-{$assignment->getId()}-description' data-type='textarea' data-mode='inline' data-pk='{$assignment->getId()}' data-url='".constants::url_external."assignments/edit'>{$assignment->getDescription(false)}</a></p>
@@ -86,7 +86,7 @@ require_once('./login.php');
<select name="offerId" class="form-control">
<?php
foreach (BusinessAdmin::getOffers($_pdo) as $offer) {
- echo "<option value='{$offer->getId()}'>#{$offer->getId()} to {$offer->getContact()->getName()} ({$offer->getContact()->getClient()->getName()})</option>";
+ echo "<option value='{$offer->getId()}'>#{$offer->getId()} to {$offer->getContact()->getName()} ({$offer->getContact()->getClient()->name})</option>";
}
?>
</select>
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);
<td class='col-min-width'>
<a href='".constants::url_internal."/offers?id={$_assignment->getOffer()->getId()}'>#{$_assignment->getOffer()->getId()}</a> to
<a href='".constants::url_internal."/contacts?id={$_assignment->getOffer()->getContact()->getId()}'>{$_assignment->getOffer()->getContact()->getName()}</a>
- (<a href='".constants::url_internal."/clients?id={$_assignment->getOffer()->getContact()->getClient()->getId()}'>{$_assignment->getOffer()->getContact()->getClient()->getName()}</a>)
+ (<a href='".constants::url_internal."/clients?id={$_assignment->getOffer()->getContact()->getClient()->id}'>{$_assignment->getOffer()->getContact()->getClient()->name}</a>)
</td>
<td class='col-min-width'><a href='#' class='editable' id='editable-_assignment-{$_assignment->getId()}-hours' data-type='text' data-pk='{$_assignment->getId()}' data-url='".constants::url_external."_assignments/edit'>{$_assignment->getHours()}</a>h</td>
<td class='col-min-width'>
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 <i>'{$client->getName()}'</i> has been created. <a class='alert-link' href='javascript:location.reload(true);'>Refresh the page</a>.";
+ $response->message = "Client <i>'{$client->name}'</i> has been created. <a class='alert-link' href='javascript:location.reload(true);'>Refresh the page</a>.";
}
} 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 "<tr class='mix'
- data-mixer-order-id='{$client->getId()}'
- data-mixer-order-name='{$client->getName()}'>
- <td class='col-min-width'>{$client->getId()}</td>
+ data-mixer-order-id='{$client->id}'
+ data-mixer-order-name='{$client->name}'>
+ <td class='col-min-width'>{$client->id}</td>
<td class='col-max-width'>
- <a href='#' class='editable' id='editable-client-{$client->getId()}-name' data-type='text' data-pk='{$client->getId()}' data-url='".constants::url_external."clients/edit' data-title='Enter new name'>
- {$client->getName()}
+ <a href='#' class='editable' id='editable-client-{$client->id}-name' data-type='text' data-pk='{$client->id}' data-url='".constants::url_external."clients/edit' data-title='Enter new name'>
+ {$client->name}
</a>
</td>
<td class='col-min-width'>
- <a title='View' href='?id={$client->getId()}' class='btn btn-primary btn-circle fa fa-arrow-right'></a>
- <a title='Delete' href='?delete={$client->getId()}' class='btn btn-danger btn-circle fa fa-times'></a>
+ <a title='View' href='?id={$client->id}' class='btn btn-primary btn-circle fa fa-arrow-right'></a>
+ <a title='Delete' href='?delete={$client->id}' class='btn btn-danger btn-circle fa fa-times'></a>
</td>
</tr>";
}
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 = "<a href='".constants::url_external."clients'>Clients</a> / {$client->getName()}";
+ $header = "<a href='".constants::url_external."clients'>Clients</a> / {$client->name}";
$show_individual = $id;
} catch (PDOException $e) {
$alert = "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The client with id $id</i> could not be found.</div>";
@@ -66,9 +66,9 @@ require('./header.php');
try {
$client = new client($_pdo, $id);
if ($client->delete()) {
- echo "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The client with name <i>{$client->getName()}</i> has been removed.</div>";
+ echo "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The client with name <i>{$client->name}</i> has been removed.</div>";
} else {
- echo "<div class='alert alert-warning alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The client with name <i>{$client->getName()}</i> could not be removed. Perhaps it's already removed?</div>";
+ echo "<div class='alert alert-warning alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The client with name <i>{$client->name}</i> could not be removed. Perhaps it's already removed?</div>";
}
} catch (PDOException $e) {
echo "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The client could not be removed due to a PDO error.</div>";
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');
<td class='col-min-width'>{$contact->getId()}</td>
<td class='col-min-width'>
<a href='#' class='editable' id='editable-contact-{$contact->getId()}-name' data-type='text' data-pk='{$contact->getId()}' data-url='".constants::url_external."contacts/edit'>{$contact->getName()}</a><br/>
- ({$contact->getClient()->getName()})
+ ({$contact->getClient()->name})
</td>
<td class='col-max-width'>
<a href='#' class='editable' id='editable-contact-{$contact->getId()}-email' data-type='text' data-pk='{$contact->getId()}' data-url='".constants::url_external."contacts/edit'>{$contact->getEmail()}</a><br/>
@@ -85,7 +85,7 @@ require_once('./login.php');
<select name="clientId" class="form-control">
<?php
foreach (BusinessAdmin::getClients($_pdo) as $client) {
- echo "<option value='{$client->getId()}'>{$client->getName()}</option>";
+ echo "<option value='{$client->id}'>{$client->name}</option>";
}
?>
</select>
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');
<td class='col-min-width'>
<a href='".constants::url_internal."/offers?id={$discount->getOffer()->getId()}'>#{$discount->getOffer()->getId()}</a> to
<a href='".constants::url_internal."/contacts?id={$discount->getOffer()->getContact()->getId()}'>{$discount->getOffer()->getContact()->getName()}</a>
- (<a href='".constants::url_internal."/clients?id={$discount->getOffer()->getContact()->getClient()->getId()}'>{$discount->getOffer()->getContact()->getClient()->getName()}</a>)
+ (<a href='".constants::url_internal."/clients?id={$discount->getOffer()->getContact()->getClient()->id}'>{$discount->getOffer()->getContact()->getClient()->name}</a>)
<td class='col-max-width'>
<b><a href='#' class='editable' id='editable-discount-{$discount->getId()}-title' data-type='text' data-pk='{$discount->getId()}' data-url='".constants::url_external."discounts/edit'>{$discount->getTitle()}</a></b><br/>
<p><a href='#' class='editable editable-noshow' id='editable-discount-{$discount->getId()}-description' data-type='textarea' data-mode='inline' data-pk='{$discount->getId()}' data-url='".constants::url_external."discounts/edit'>{$discount->getDescription(false)}</a></p>
@@ -83,7 +83,7 @@ require_once('./login.php');
<select name="offerId" class="form-control">
<?php
foreach (BusinessAdmin::getOffers($_pdo) as $offer) {
- echo "<option value='{$offer->getId()}'>#{$offer->getId()} to {$offer->getContact()->getName()} ({$offer->getContact()->getClient()->getName()})</option>";
+ echo "<option value='{$offer->getId()}'>#{$offer->getId()} to {$offer->getContact()->getName()} ({$offer->getContact()->getClient()->name})</option>";
}
?>
</select>
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 "<tr>";
echo "<td>{$offer->getId()}</td>";
- echo "<td>{$offer->getContact()->getClient()->getName()}</td>";
+ echo "<td>{$offer->getContact()->getClient()->name}</td>";
echo "<td>".BusinessAdmin::formatDate($offer->getEndDate(), false)."</td>";
echo "</tr>";
}
@@ -249,7 +249,7 @@ require('./header.php');
foreach ($offers as $offer) {
echo "<tr>";
echo "<td>{$offer->getId()}</td>";
- echo "<td>{$offer->getContact()->getClient()->getName()}</td>";
+ echo "<td>{$offer->getContact()->getClient()->name}</td>";
echo "<td>".BusinessAdmin::formatDate($offer->getInvoiceDate(), false)."</td>";
echo "</tr>";
}
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 "<tr>
<td class='col-min-width'>{$offer->getId()}</td>
- <td class='col-min-width'><span title='{$offer->getContact()->getClient()->getName()}'>{$offer->getContact()->getName()}</span></td>
+ <td class='col-min-width'><span title='{$offer->getContact()->getClient()->name}'>{$offer->getContact()->getName()}</span></td>
<td class='col-max-width'>";
foreach ($offer->getAssignments() as $assignment) {
echo "<b>{$assignment->getTitle()}</b><br/><span class='smaller'>(".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)</span><br/><p>{$assignment->getDescription()}</p>";
@@ -120,7 +120,7 @@ require_once('./login.php');
<select name="contactId" class="form-control">
<?php
foreach (BusinessAdmin::getContacts($_pdo) as $contact) {
- echo "<option value='{$contact->getId()}'>{$contact->getName()} ({$contact->getClient()->getName()})</option>";
+ echo "<option value='{$contact->getId()}'>{$contact->getName()} ({$contact->getClient()->name})</option>";
}
?>
</select>
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)
);