Commit b4a21990 authored by Mal's avatar Mal

Where possible, modules are now instantiated directly rather than going

through the module interface. This allows custom public functions to be
called directly as well, so most of the Factory method code was removed.
parent c1ccdbdf
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
// Copyright (C) 2013 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -97,7 +97,7 @@ class Banking extends Base {
'<input id="banking-name-input" name="name" value="'.
$name.'" size="15" maxlength="100"><br>'.
'<label for="banking-number-input">Account Number: </label>'.
'<input id="banking-last-input" name="number" value="'.
'<input id="banking-number-input" name="number" value="'.
$number.'" size="15" maxlength="20"><br>'.
'<label for="banking-bsb-input">BSB: </label>'.
'<input id="banking-bsb-input" name="bsb" value="'.
......@@ -150,7 +150,7 @@ class Banking extends Base {
}
public function Factory($fn) {
if ($fn == "AllSettings") return $this->AllSettings();
}
public function Group() {
......@@ -222,9 +222,9 @@ class Banking extends Base {
$this->AppendScript($path, "dobrado.banking.js", false);
}
// Private functions below here ////////////////////////////////////////////
// Public functions that aren't part of interface here /////////////////////
private function AllSettings() {
public function AllSettings() {
$settings = array();
$mysqli = connect_db();
$query = 'SELECT user, reference, name, number, bsb, credit, surcharge '.
......@@ -248,6 +248,51 @@ class Banking extends Base {
return $settings;
}
public function SaveNextWeek($us_next_week) {
// strtotime depends on the timezone.
$this->config->set_timezone();
// Use the configured day of the week for the co-op to find the next time
// the co-op will be held. Due to the way relative dates work, need to
// check if 'this (day)' is in the past, otherwise use 'next (day)'.
$co_op_day = $this->Substitute("co-op-day");
$timestamp = strtotime("this ".$co_op_day);
// Want to make sure today is not set as the value if today is co-op day.
if ($timestamp < strtotime("24 hours")) {
$timestamp = strtotime("next ".$co_op_day);
}
$mysqli = connect_db();
$user_query = "";
for ($i = 0; $i < count($us_next_week); $i++) {
if ($user_query != "") {
$user_query .= ' OR ';
}
$user_query .= 'user="'.$mysqli->escape_string($us_next_week[$i]).'"';
}
$query = 'UPDATE banking SET next_week='.$timestamp.' WHERE '.$user_query;
if (!$mysqli->query($query)) {
$this->Log('Banking->SaveNextWeek: '.$mysqli->error);
}
$mysqli->close();
}
public function Settings($user) {
$banking = array();
$mysqli = connect_db();
$query = 'SELECT reference, next_week FROM banking WHERE user="'.$user.'"';
if ($result = $mysqli->query($query)) {
$banking = $result->fetch_assoc();
$result->close();
}
else {
$this->Log('Banking->Settings: '.$mysqli->error);
}
$mysqli->close();
return $banking;
}
// Private functions below here ////////////////////////////////////////////
}
?>
\ No newline at end of file
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
// Copyright (C) 2013 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -119,10 +119,7 @@ class Detail extends Base {
public function Factory($fn) {
if (is_array($fn)) {
$name = $fn[0];
if ($name == "User" && count($fn) == 2) {
$user = $fn[1];
return $this->User($user);
}
// Account module calls UpdateUser through module interface.
if ($name == "UpdateUser" && count($fn) == 5) {
$user = $fn[1];
$first = $fn[2];
......@@ -130,10 +127,7 @@ class Detail extends Base {
$phone = $fn[4];
return $this->UpdateUser($user, $first, $last, $phone);
}
return;
}
if ($fn == "User") return $this->User();
if ($fn == "AllUsers") return $this->AllUsers();
}
public function Group() {
......@@ -202,35 +196,9 @@ class Detail extends Base {
$this->AppendScript($path, "dobrado.detail.js");
}
// Private functions below here ////////////////////////////////////////////
private function User($user = "") {
$object = array();
if ($user === "") {
$user = $this->user->name;
}
$mysqli = connect_db();
$query = 'SELECT email, first, last, thumbnail, phone FROM '.
'users LEFT JOIN user_detail ON users.user=user_detail.user WHERE '.
'users.user="'.$user.'"';
if ($result = $mysqli->query($query)) {
if ($detail = $result->fetch_assoc()) {
$object = array("first" => $detail["first"],
"last" => $detail["last"],
"thumbnail" => $detail["thumbnail"],
"phone" => $detail["phone"],
"email" => $detail["email"]);
}
$result->close();
}
else {
$this->Log('Detail->User: '.$mysqli->error);
}
$mysqli->close();
return $object;
}
// Public functions that aren't part of interface here /////////////////////
private function AllUsers() {
public function AllUsers() {
$object = array();
$mysqli = connect_db();
$query = 'SELECT users.user, email, first, last, phone FROM '.
......@@ -251,7 +219,7 @@ class Detail extends Base {
return $object;
}
private function UpdateUser($user, $first, $last, $phone) {
public function UpdateUser($user, $first, $last, $phone) {
$mysqli = connect_db();
$query = 'INSERT INTO user_detail VALUES ("'.$user.'","'.
$first.'","'.$last.'","","'.$phone.'") '.
......@@ -263,6 +231,34 @@ class Detail extends Base {
$mysqli->close();
}
public function User($user = "") {
$object = array();
if ($user === "") {
$user = $this->user->name;
}
$mysqli = connect_db();
$query = 'SELECT email, first, last, thumbnail, phone FROM '.
'users LEFT JOIN user_detail ON users.user=user_detail.user WHERE '.
'users.user="'.$user.'"';
if ($result = $mysqli->query($query)) {
if ($detail = $result->fetch_assoc()) {
$object = array("first" => $detail["first"],
"last" => $detail["last"],
"thumbnail" => $detail["thumbnail"],
"phone" => $detail["phone"],
"email" => $detail["email"]);
}
$result->close();
}
else {
$this->Log('Detail->User: '.$mysqli->error);
}
$mysqli->close();
return $object;
}
// Private functions below here ////////////////////////////////////////////
}
?>
\ No newline at end of file
This diff is collapsed.
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
// Copyright (C) 2013 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -43,11 +43,11 @@ class Manager extends Base {
else {
$this->Log('Manager->Callback: '.$mysqli->error);
}
$detail = new Module($this->user, $this->owner, "detail", $this->config);
$object["details"] = $detail->Factory("AllUsers");
$detail = new Detail($this->user, $this->owner, $this->config);
$object["details"] = $detail->AllUsers();
$stock = new Module($this->user, $this->owner, "stock", $this->config);
$object["products"] = $stock->Factory("AllProducts");
$stock = new Stock($this->user, $this->owner, $this->config);
$object["products"] = $stock->AllProducts();
}
else if ($action == "search") {
$username = $mysqli->escape_string($_POST["username"]);
......@@ -57,11 +57,9 @@ class Manager extends Base {
$price = $mysqli->escape_string($_POST["price"]);
$start = (int)$mysqli->escape_string($_POST["start"]) / 1000;
$end = (int)$mysqli->escape_string($_POST["end"]) / 1000;
$purchase = new Module($this->user, $this->owner, "purchase",
$this->config);
$object = $purchase->Factory(array("Search", $username, $timestamp,
$product, $quantity, $price, $start,
$end));
$purchase = new Purchase($this->user, $this->owner, $this->config);
$object = $purchase->Search($username, $timestamp, $product, $quantity,
$price, $start, $end);
}
else if ($action == "submit") {
$username = $mysqli->escape_string($_POST["username"]);
......@@ -69,20 +67,17 @@ class Manager extends Base {
$product = $mysqli->escape_string($_POST["product"]);
$quantity = $mysqli->escape_string($_POST["quantity"]);
$price = $mysqli->escape_string($_POST["price"]);
$purchase = new Module($this->user, $this->owner, "purchase",
$this->config);
$purchase->Factory(array("AddPurchase", $username, $timestamp, $product,
$quantity, $price, $this->user->name));
$purchase = new Purchase($this->user, $this->owner, $this->config);
$purchase->AddPurchase($username, $timestamp, $product, $quantity,
$price, $this->user->name);
$object["done"] = true;
}
else if ($action == "remove") {
$username = $mysqli->escape_string($_POST["username"]);
$product = $mysqli->escape_string($_POST["product"]);
$timestamp = (int)$mysqli->escape_string($_POST["timestamp"]) / 1000;
$purchase = new Module($this->user, $this->owner, "purchase",
$this->config);
$purchase->Factory(array("RemovePurchase", $username, $timestamp,
$product));
$purchase = new Purchase($this->user, $this->owner, $this->config);
$purchase->RemovePurchase($username, $timestamp, $product);
$object["done"] = true;
}
else if ($action == "editDetails") {
......@@ -90,8 +85,8 @@ class Manager extends Base {
$first = $mysqli->escape_string($_POST["first"]);
$last = $mysqli->escape_string($_POST["last"]);
$phone = $mysqli->escape_string($_POST["phone"]);
$detail = new Module($this->user, $this->owner, "detail", $this->config);
$detail->Factory(array("UpdateUser", $username, $first, $last, $phone));
$detail = new Detail($this->user, $this->owner, $this->config);
$detail->UpdateUser($username, $first, $last, $phone);
$object["done"] = true;
}
$mysqli->close();
......
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
// Copyright (C) 2013 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -35,11 +35,10 @@ class Payment extends Base {
$object = $this->AllData();
}
if ($action == "details") {
$banking = new Module($this->user, $this->owner, "banking",
$this->config);
$object["bank"] = $banking->Factory("AllSettings");
$detail = new Module($this->user, $this->owner, "detail", $this->config);
$object["contact"] = $detail->Factory("AllUsers");
$banking = new Banking($this->user, $this->owner, $this->config);
$object["bank"] = $banking->AllSettings();
$detail = new Detail($this->user, $this->owner, $this->config);
$object["contact"] = $detail->AllUsers();
}
if ($action == "edit") {
$new_name = $mysqli->escape_string($_POST["newUsername"]);
......@@ -229,24 +228,7 @@ class Payment extends Base {
}
public function Factory($fn) {
if (is_array($fn)) {
$name = $fn[0];
if ($name == "Data" && count($fn) == 3) {
$start = $fn[1];
$end = $fn[2];
return $this->Data($start, $end);
}
if ($name == "Surcharge" && count($fn) == 2) {
$purchases = $fn[1];
return $this->Surcharge($purchases);
}
return;
}
if ($fn == "LevelSettings") return $this->LevelSettings();
if ($fn == "Total") return $this->Total();
if ($fn == "AllTotals") return $this->AllTotals();
if ($fn == "AllData") return $this->AllData();
if ($fn == "NewUser") return $this->NewUser();
}
public function Group() {
......@@ -378,58 +360,75 @@ class Payment extends Base {
$this->AppendScript($path, "dobrado.payment.js", false);
}
// Private functions below here ////////////////////////////////////////////
// Public functions that aren't part of interface here /////////////////////
private function Data($start, $end) {
public function AllData() {
$object = array();
$mysqli = connect_db();
$query = 'SELECT timestamp, amount, comment FROM payments WHERE '.
'timestamp >='.$start.' AND timestamp <='.$end.' AND user="'.
$this->user->name.'" ORDER BY timestamp';
$query = 'SELECT user, timestamp, reference, amount, comment FROM '.
'payments ORDER BY timestamp DESC';
if ($result = $mysqli->query($query)) {
while ($payments = $result->fetch_assoc()) {
$amount = (float)$payments["amount"];
$amount = number_format($amount, 2, ".", "");
$timestamp = (int)$payments["timestamp"] * 1000;
$object[] = array("date" => $timestamp,
"name" => $payments["user"],
"reference" => $payments["reference"],
"comment" => $payments["comment"],
"amount" => $amount);
}
$result->close();
}
else {
$this->Log('Payment->Data: '.$mysqli->error);
$this->Log('Payment->AllData: '.$mysqli->error);
}
$mysqli->close();
return $object;
}
private function AllData() {
public function AllTotals() {
$totals = array();
$mysqli = connect_db();
$query = 'SELECT user, amount FROM payment_totals';
if ($result = $mysqli->query($query)) {
while ($payment_totals = $result->fetch_assoc()) {
$totals[$payment_totals["user"]] = (float)$payment_totals["amount"];
}
$result->close();
}
else {
$this->Log('Payment->AllTotals: '.$mysqli->error);
}
$mysqli->close();
return $totals;
}
public function Data($start, $end) {
$object = array();
$mysqli = connect_db();
$query = 'SELECT user, timestamp, reference, amount, comment FROM '.
'payments ORDER BY timestamp DESC';
$query = 'SELECT timestamp, amount, comment FROM payments WHERE '.
'timestamp >='.$start.' AND timestamp <='.$end.' AND user="'.
$this->user->name.'" ORDER BY timestamp';
if ($result = $mysqli->query($query)) {
while ($payments = $result->fetch_assoc()) {
$amount = (float)$payments["amount"];
$amount = number_format($amount, 2, ".", "");
$timestamp = (int)$payments["timestamp"] * 1000;
$object[] = array("date" => $timestamp,
"name" => $payments["user"],
"reference" => $payments["reference"],
"comment" => $payments["comment"],
"amount" => $amount);
}
$result->close();
}
else {
$this->Log('Payment->AllData: '.$mysqli->error);
$this->Log('Payment->Data: '.$mysqli->error);
}
$mysqli->close();
return $object;
}
private function LevelSettings() {
public function LevelSettings() {
$info = "";
$warning = "";
$mysqli = connect_db();
......@@ -448,42 +447,7 @@ class Payment extends Base {
return array((float)$info, (float)$warning);
}
private function Total() {
$total = 0;
$mysqli = connect_db();
$query = 'SELECT amount FROM payment_totals WHERE user="'.
$this->user->name.'"';
if ($result = $mysqli->query($query)) {
if ($payment_totals = $result->fetch_assoc()) {
$total = (float)$payment_totals["amount"];
}
$result->close();
}
else {
$this->Log('Payment->Total: '.$mysqli->error);
}
$mysqli->close();
return $total;
}
private function AllTotals() {
$totals = array();
$mysqli = connect_db();
$query = 'SELECT user, amount FROM payment_totals';
if ($result = $mysqli->query($query)) {
while ($payment_totals = $result->fetch_assoc()) {
$totals[$payment_totals["user"]] = (float)$payment_totals["amount"];
}
$result->close();
}
else {
$this->Log('Payment->AllTotals: '.$mysqli->error);
}
$mysqli->close();
return $totals;
}
private function NewUser() {
public function NewUser() {
$mysqli = connect_db();
$query = 'INSERT INTO payment_totals VALUES ("'.$this->user->name.'", 0)';
if (!$mysqli->query($query)) {
......@@ -509,7 +473,7 @@ class Payment extends Base {
$mysqli->close();
}
private function Surcharge($purchases) {
public function Surcharge($purchases) {
$mysqli = connect_db();
$surcharge = 0;
$query = 'SELECT surcharge FROM payment_settings';
......@@ -532,6 +496,26 @@ class Payment extends Base {
return 4;
}
public function Total() {
$total = 0;
$mysqli = connect_db();
$query = 'SELECT amount FROM payment_totals WHERE user="'.
$this->user->name.'"';
if ($result = $mysqli->query($query)) {
if ($payment_totals = $result->fetch_assoc()) {
$total = (float)$payment_totals["amount"];
}
$result->close();
}
else {
$this->Log('Payment->Total: '.$mysqli->error);
}
$mysqli->close();
return $total;
}
// Private functions below here ////////////////////////////////////////////
}
?>
\ No newline at end of file
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
// Copyright (C) 2013 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -411,10 +411,10 @@ class Post extends Base {
$query = 'SELECT box_id, label FROM modules WHERE user="'.$this->owner.
'" AND box_id!="'.$id.'" AND page="'.$permalink.'" AND deleted="0"';
if ($result = $mysqli->query($query)) {
while ($modules = $result->fetch_assoc()) {
$module = new Module($this->user, $this->owner, $modules['label'],
while ($module_list = $result->fetch_assoc()) {
$module = new Module($this->user, $this->owner, $module_list['label'],
$this->config);
$module->Remove($modules['box_id']);
$module->Remove($module_list['box_id']);
}
$result->close();
}
......
This diff is collapsed.
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
// Copyright (C) 2013 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -35,7 +35,8 @@ class Roster extends Base {
// Split fullname into first and last names if matching.
$first = $username;
$last = "";
if (preg_match('/^(\w+)\s*(\w+)?$/', $fullname, $matches)) {
// Match a first name, and then be less strict about the last name.
if (preg_match('/^(\w+)\s*(\w.*)?$/', $fullname, $matches)) {
$first = $matches[1];
// There will only be 3 matches if a last name was given too.
if (count($matches) == 3) {
......@@ -141,16 +142,7 @@ class Roster extends Base {
}
public function Factory($fn) {
if (is_array($fn)) {
$name = $fn[0];
if ($name == "Description" && count($fn) == 2) {
$user = $fn[1];
return $this->Description($user);
}
return;
}
if ($fn == "FirstName") return $this->FirstName();
if ($fn == "Description") return $this->Description();
}
public function Group() {
......@@ -203,21 +195,15 @@ class Roster extends Base {
public function Update() {
// This is called when the version of the module is updated,
// to provide a way to update or modify tables etc..
$mysqli = connect_db();
$query = 'ALTER TABLE roster ADD COLUMN role VARCHAR(50) AFTER phone';
if (!$mysqli->query($query)) {
$this->Log('Roster->Update: '.$mysqli->error);
}
$mysqli->close();
}
public function UpdateScript($path) {
$this->AppendScript($path, "dobrado.roster.js", false);
}
// Private functions below here ////////////////////////////////////////////
// Public functions that aren't part of interface here /////////////////////
private function FirstName() {
public function FirstName() {
// strtotime depends on the timezone.
$this->config->set_timezone();
......@@ -239,6 +225,48 @@ class Roster extends Base {
return $first;
}
public function Description($user = "") {
// strtotime depends on the timezone.
$this->config->set_timezone();
if ($user === "") {
$user = $this->user->name;
}
$timestamp = 0;
$role = "";
$mysqli = connect_db();
$query = 'SELECT MAX(timestamp) as timestamp, role FROM roster WHERE '.
'timestamp > '.strtotime("-4 weeks").' AND user="'.$user.'"';
if ($result = $mysqli->query($query)) {
if ($roster = $result->fetch_assoc()) {
$role = $roster["role"];
$timestamp = (int)$roster["timestamp"];
}
$result->close();
}
else {
$this->Log('Roster->Description: '.$mysqli->error);
}
$mysqli->close();
if ($timestamp == 0) {
return "You haven't volunteered for on the night recently.<br>".
"Please consider putting your name on the roster soon.";
}
if ($timestamp < strtotime("-1 week")) {
return "Thanks for volunteering for ".$role." recently! :-)";
}
if ($timestamp < time()) {
return "Thanks for volunteering for ".$role." last week! :-)";
}
if ($timestamp < strtotime("+1 week")) {
return "Thanks for volunteering for ".$role." this week! :-)";
}
return "Thanks for volunteering for ".$role." soon! :-)";
}
// Private functions below here ////////////////////////////////////////////
private function Show() {
$object = array();
$object["content"] = array();
......@@ -261,8 +289,8 @@ class Roster extends Base {
$this->config->set_timezone();
// Get user details for autocomplete.
$detail = new Module($this->user, $this->owner, "detail", $this->config);
$object["details"] = $detail->Factory("AllUsers");
$detail = new Detail($this->user, $this->owner, $this->config);
$object["details"] = $detail->AllUsers();
$query = 'SELECT user, first, last, phone, role, timestamp FROM roster '.
'WHERE timestamp > '.strtotime("-3 weeks").' ORDER BY timestamp DESC';
......@@ -287,46 +315,6 @@ class Roster extends Base {
return $object;
}
private function Description($user = "") {
// strtotime depends on the timezone.
$this->config->set_timezone();
if ($user === "") {
$user = $this->user->name;
}
$timestamp = 0;
$role = "";
$mysqli = connect_db();
$query = 'SELECT MAX(timestamp) as timestamp, role FROM roster WHERE '.
'timestamp > '.strtotime("-4 weeks").' AND user="'.$user.'"';
if ($result = $mysqli->query($query)) {
if ($roster = $result->fetch_assoc()) {
$role = $roster["role"];
$timestamp = (int)$roster["timestamp"];
}
$result->close();
}
else {
$this->Log('Roster->Description: '.$mysqli->error);
}
$mysqli->close();
if ($timestamp == 0) {
return "You haven't volunteered for on the night recently.<br>".
"Please consider putting your name on the roster soon.";
}
if ($timestamp < strtotime("-1 week")) {
return "Thanks for volunteering for ".$role." recently! :-)";
}
if ($timestamp < time()) {
return "Thanks for volunteering for ".$role." last week! :-)";
}
if ($timestamp < strtotime("+1 week")) {
return "Thanks for volunteering for ".$role." this week! :-)";
}
return "Thanks for volunteering for ".$role." soon! :-)";
}
}
?>
\ No newline at end of file
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
// Copyright (C) 2013 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -78,10 +78,8 @@ class Stock extends Base {
}
else {
// Can only remove stock items if no purchases have been made.
$purchase = new Module($this->user, $this->owner, "purchase",
$this->config);
$search = $purchase->Factory(array("Search", "", 0, $name,
"", "", 0, 0));
$purchase = new Purchase($this->user, $this->owner, $this->config);
$search = $purchase->Search("", 0, $name, "", "", 0, 0);
if (count($search) === 0) {
$query = 'DELETE FROM stock WHERE name="'.$name.'"';
if (!$mysqli->query($query)) {
......@@ -164,23 +162,7 @@ class Stock extends Base {
}
public function Factory($fn) {
if (is_array($fn)) {
$name = $fn[0];
if ($name == "Supplier" && count($fn) == 2) {
$user = $fn[1];
return $this->Supplier($user);
}
if ($name == "AvailableProducts" && count($fn) == 2) {
$array = $fn[1];
return $this->AvailableProducts($array);
}
return;
}
if ($fn == "AvailableProducts") return $this->AvailableProducts();
if ($fn == "AllProducts") return $this->AllProducts();
if ($fn == "LastUpdate") return $this->LastUpdate();
if ($fn == "AllSuppliers") return $this->AllSuppliers();
if ($fn == "Supplier") return $this->Supplier();
}
public function Group() {
......@@ -270,9 +252,50 @@ class Stock extends Base {