aboutsummaryrefslogtreecommitdiff
path: root/docs/files/BusinessAdmin.class.php.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/files/BusinessAdmin.class.php.txt')
-rw-r--r--docs/files/BusinessAdmin.class.php.txt282
1 files changed, 282 insertions, 0 deletions
diff --git a/docs/files/BusinessAdmin.class.php.txt b/docs/files/BusinessAdmin.class.php.txt
new file mode 100644
index 0000000..7efedd2
--- /dev/null
+++ b/docs/files/BusinessAdmin.class.php.txt
@@ -0,0 +1,282 @@
+<?php
+/**
+ * This file provides the BusinessAdmin class
+ *
+ * @author Camil Staps
+ *
+ * BusinessAdmin: administrative software for small companies
+ * Copyright (C) 2015 Camil Staps (ViviSoft)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Provides basic functions like adding elements to the database
+ */
+class BusinessAdmin {
+ //------------------------------------------------------------------------------
+ // Getters and setters
+ //------------------------------------------------------------------------------
+
+ /**
+ * Get all client ids
+ *
+ * @see BusinessAdmin::getClients() This funtion returns instances of the client class instead of just the ids
+ *
+ * @param PDO $pdo The PDO class for database connection
+ *
+ * @throws PDOException Is something went wrong with the database
+ *
+ * @return int[] The ids
+ */
+ public static function getClientIds($pdo) {
+ $ids = array();
+ $clients = $pdo->query("SELECT `id` FROM `".constants::db_prefix."client`")->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($clients as $client) {
+ $ids[] = $client['id'];
+ }
+ return $ids;
+ }
+
+ /**
+ * Get all clients
+ *
+ * @see BusinessAdmin::getClientIds() This function returns just the ids of the clients, and not instances of the client class
+ *
+ * @param PDO $pdo The PDO class for database connection
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return client[] An array indexed by id of instances of the client class
+ */
+ public static function getClients($pdo) {
+ $ids = self::getClientIds($pdo);
+ $clients = array();
+ foreach ($ids as $id) {
+ $clients[$id] = new client($pdo, $id);
+ }
+ return $clients;
+ }
+
+ /**
+ * Get all contact ids
+ *
+ * @see BusinessAdmin::getContacts() This funtion returns instances of the contact class instead of just the ids
+ *
+ * @param PDO $pdo The PDO class for database connection
+ *
+ * @throws PDOException Is something went wrong with the database
+ *
+ * @return int[] The ids
+ */
+ public static function getContactIds($pdo) {
+ $ids = array();
+ $contacts = $pdo->query("SELECT `id` FROM `".constants::db_prefix."contact`")->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($contacts as $contact) {
+ $ids[] = $contact['id'];
+ }
+ return $ids;
+ }
+
+ /**
+ * Get all contacts
+ *
+ * @see BusinessAdmin::getContactIds() This function returns just the ids of the contacts, and not instances of the contact class
+ *
+ * @param PDO $pdo The PDO class for database connection
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return contact[] An array indexed by id of instances of the contact class
+ */
+ public static function getContacts($pdo) {
+ $ids = self::getContactIds($pdo);
+ $contacts = array();
+ foreach ($ids as $id) {
+ $contacts[$id] = new contact($pdo, $id);
+ }
+ return $contacts;
+ }
+
+ /**
+ * Get all offer ids
+ *
+ * @see BusinessAdmin::getOffers() This funtion returns instances of the offer class instead of just the ids
+ *
+ * @param PDO $pdo The PDO class for database connection
+ * @param string[] $where An array of WHERE clauses that will be AND-ed
+ *
+ * @throws PDOException Is something went wrong with the database
+ *
+ * @return int[] The ids
+ */
+ public static function getOfferIds($pdo, $where = array()) {
+ $ids = array();
+ $offers = $pdo->query("SELECT `id` FROM `".constants::db_prefix."offer`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""))->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($offers as $offer) {
+ $ids[] = $offer['id'];
+ }
+ return $ids;
+ }
+
+ /**
+ * Get all offers
+ *
+ * @see BusinessAdmin::getOfferIds() This function returns just the ids of the offers, and not instances of the offer class
+ *
+ * @param PDO $pdo The PDO class for database connection
+ * @param string[] $where An array of WHERE clauses that will be AND-ed
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return offer[] An array indexed by id of instances of the offer class
+ */
+ public static function getOffers($pdo, $where = array()) {
+ $ids = self::getOfferIds($pdo, $where);
+ $offers = array();
+ foreach ($ids as $id) {
+ $offers[$id] = new offer($pdo, $id);
+ }
+ return $offers;
+ }
+
+ /**
+ * Get all assignment ids
+ *
+ * @see BusinessAdmin::getAssignments() This funtion returns instances of the assignment class instead of just the ids
+ *
+ * @param PDO $pdo The PDO class for database connection
+ *
+ * @throws PDOException Is something went wrong with the database
+ *
+ * @return int[] The ids
+ */
+ public static function getAssignmentIds($pdo) {
+ $ids = array();
+ $assignments = $pdo->query("SELECT `id` FROM `".constants::db_prefix."assignment`")->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($assignments as $assignment) {
+ $ids[] = $assignment['id'];
+ }
+ return $ids;
+ }
+
+ /**
+ * Get all assignments
+ *
+ * @see BusinessAdmin::getAssignmentIds() This function returns just the ids of the assignments, and not instances of the assignment class
+ *
+ * @param PDO $pdo The PDO class for database connection
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return assignment[] An array indexed by id of instances of the assignment class
+ */
+ public static function getAssignments($pdo) {
+ $ids = self::getAssignmentIds($pdo);
+ $assignments = array();
+ foreach ($ids as $id) {
+ $assignments[$id] = new assignment($pdo, $id);
+ }
+ return $assignments;
+ }
+
+ //------------------------------------------------------------------------------
+ // Other functions
+ //------------------------------------------------------------------------------
+
+ /**
+ * Create a new client
+ *
+ * @param PDO $pdo The database connection
+ * @param string $name The name for the new client
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return client|bool A new instance of the client object, or false on failure
+ */
+ public static function createClient($pdo, $name) {
+ $stmt = $pdo->prepare("INSERT INTO `".constants::db_prefix."client` (`name`) VALUES (?)");
+ $stmt->execute(array($name));
+ if ($stmt->rowCount() == 1) {
+ return new client($pdo, $pdo->lastInsertId());
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Create a new file
+ *
+ * @param PDO $pdo The database connection
+ * @param string $filename The desired filename
+ *
+ * @throws PDOException If something went wrong with the database
+ * @throws Exception If the file could not be created (due to permissions, file existed already, etc.), or the database record couldn't be added
+ *
+ * @return file A new instance of the file object
+ */
+ public static function createFile($pdo, $filename) {
+ // Check for file existence
+ if (file_exists(constants::files_folder . $filename)) {
+ throw new Exception("$filename already exists.");
+ }
+
+ // Try to create the file
+ if (file_put_contents(constants::files_folder . $filename, '') === false) {
+ throw new Exception("$filename could not be created. Check the permissions.");
+ }
+
+ $stmt = $pdo->prepare("INSERT INTO `".constants::db_prefix."file` (`filename`) VALUES (?)");
+ $stmt->execute(array($filename));
+ if ($stmt->rowCount() == 1) {
+ return new file($pdo, $pdo->lastInsertId());
+ } else {
+ unlink(constants::files_folder . $filename);
+ throw new Exception("$filename could not be added to the database");
+ }
+ }
+
+ /**
+ * Format a date nicely
+ *
+ * @todo implement $relatively = true
+ *
+ * @param int $timestamp The UNIX timestamp to format
+ * @param bool $with_time If false, only the date is returned; if false, also the time
+ * @param bool $full_date If true, a year will be outputted even if the date is in the current year
+ * @param bool $relatively Whether or not to show the date relatively (e.g. '1 day ago') (NOT IMPLEMENTED YET)
+ *
+ * @return string The formatted date
+ */
+ public static function formatDate($timestamp, $with_time = true, $full_date = false, $relatively = false) {
+ $output = '';
+ if (date('Y', $timestamp) == 1970) {
+ return 'never';
+ }
+ if (date('d/m/Y') == date('d/m/Y', $timestamp)) {
+ return 'today';
+ }
+ if (date('Y') != date('Y', $timestamp) || $full_date) {
+ $output .= date('Y-', $timestamp);
+ }
+ if (date('d/m/Y') != date('d/m/Y', $timestamp) || $full_date) {
+ $output .= date('m-d', $timestamp);
+ }
+ if ($with_time) {
+ $output .= date(' H:i', $timestamp);
+ }
+
+ return $output;
+ }
+}