Commit d3e3e917be9ee79ac56a110bcb8a9bfa0da08e75

Authored by Michael Joseph
1 parent 50908198

initial revision of document and folder subscription objects and tests


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@777 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/subscriptions/DocumentSubscription.inc 0 → 100644
  1 +<?php
  2 +/**
  3 + *
  4 + * $Id$
  5 + *
  6 + * Represents a document subscription.
  7 + *
  8 + * Licensed under the GNU GPL. For full terms see the file COPYING.
  9 + *
  10 + * @version $Revision$
  11 + * @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
  12 + *
  13 + * @package lib.subscriptions
  14 + */
  15 +class DocumentSubscription {
  16 +
  17 + /**
  18 + * Primary key
  19 + */
  20 + var $iID;
  21 + /**
  22 + * The ID of the document subscribed to
  23 + */
  24 + var $iDocumentID;
  25 + /**
  26 + * The ID of the user subscribed to the document
  27 + */
  28 + var $iUserID;
  29 +
  30 + /**
  31 + * Creates a new document subscription object
  32 + *
  33 + * @param integer the document ID
  34 + * @param integer the user ID
  35 + */
  36 + function DocumentSubscription($iDocumentID, $iUserID) {
  37 + //id of -1 means that the object has not yet been stored in the database
  38 + $this->iID = -1;
  39 + $this->iDocumentID = $iDocumentID;
  40 + $this->iUserID = $iUserID;
  41 + }
  42 +
  43 + /**
  44 + * Get the primary key of the current document subscription object
  45 + *
  46 + * @return integer primary key of document subscription
  47 + */
  48 + function getID() {
  49 + return $this->iID;
  50 + }
  51 +
  52 + /**
  53 + * Get the primary key of the document.
  54 + *
  55 + * @return integer primary key of document
  56 + */
  57 + function getDocumentID() {
  58 + return $this->iDocumentID;
  59 + }
  60 +
  61 + /**
  62 + * Set the document id
  63 + *
  64 + * @param integer new document primary key
  65 + */
  66 + function setDocumentID($iNewValue) {
  67 + $this->iDocumentID = $iNewValue;
  68 + }
  69 +
  70 + /**
  71 + * Get the primary key of the user
  72 + *
  73 + * @return integer primary key of user
  74 + */
  75 + function getUserID() {
  76 + return $this->iUserID;
  77 + }
  78 +
  79 + /**
  80 + * Set the user id
  81 + *
  82 + * @param integer new user primary key
  83 + */
  84 + function setUserID($iNewValue) {
  85 + $this->iUserID = $iNewValue;
  86 + }
  87 +
  88 + /**
  89 + * Create the current document subscription in the database
  90 + *
  91 + * @return boolean true and set $this->iID with new primary key, false otherwise and set $_SESSION["errorMessage"]
  92 + */
  93 + function create() {
  94 + global $default, $lang_err_database; $lang_err_object_exists;
  95 + //if the object has not already been stored
  96 + if ($this->iID < 0) {
  97 + $sql = $default->db;
  98 + $result = $sql->query("INSERT INTO " . $default->owl_document_subscriptions_table . " (user_id, document_id) " .
  99 + "VALUES ($this->iUserID, $this->iDocumentID)");
  100 + if ($result) {
  101 + $this->iID = $sql->insert_id();
  102 + return true;
  103 + }
  104 + $_SESSION["errorMessage"] = $lang_err_database;
  105 + return false;
  106 + }
  107 + $_SESSION["errorMessage"] = $lang_err_object_exists . "id = " . $this->iID . " table = document_subscriptions";
  108 + return false;
  109 + }
  110 +
  111 + /**
  112 + * Update the current document subscription values in the database
  113 + *
  114 + * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"]
  115 + */
  116 + function update() {
  117 + global $default, $lang_err_database, $lang_err_object_key;
  118 + //can only update the object if it has already been stored
  119 + if ($this->iID >= 0) {
  120 + $sql = $default->db;
  121 + $result = $sql->query("UPDATE " . $default->owl_document_subscriptions_table . " SET " .
  122 + "user_id = $this->iUserID, " .
  123 + "document_id = $this->iDocumentID " .
  124 + "WHERE id = " . $this->iID);
  125 + if ($result) {
  126 + return true;
  127 + }
  128 + $_SESSION["errorMessage"] = $lang_err_database;
  129 + return false;
  130 + }
  131 + $_SESSION["errorMessage"] = $lang_err_object_key;
  132 + }
  133 +
  134 +
  135 + /**
  136 + * Delete the current object from the database
  137 + *
  138 + * @return boolean true and reset id to -1 on successful delete, false otherwise and set $_SESSION["errorMessage"]
  139 + */
  140 + function delete() {
  141 + global $default, $lang_err_database, $lang_err_object_key;
  142 + if ($this->iID >= 0) {
  143 + $sql = $default->db;
  144 + $result = $sql->query("DELETE FROM " . $default->owl_document_subscriptions_table . " WHERE id = " . $this->iID);
  145 + if ($result) {
  146 + $this->iID = -1;
  147 + return true;
  148 + }
  149 + $_SESSION["errorMessage"] = $lang_err_database;
  150 + return false;
  151 + }
  152 + $_SESSION["errorMessage"] = $lang_err_object_key;
  153 + return false;
  154 +
  155 + }
  156 +
  157 + /**
  158 + * Static function.
  159 + * Given a document subscription primary key will create
  160 + * a document subscription object and populate it with the corresponding
  161 + * database values
  162 + *
  163 + * @param integer primary key of document subscription to get
  164 + *
  165 + * @return object document subscription object on successful retrieval, false otherwise and set $_SESSION["errorMessage"]
  166 + */
  167 + function get($iDocumentSubscriptionID) {
  168 + global $default, $lang_err_object_not_exist;
  169 + $sql = $default->db;
  170 + $sql->query("SELECT * FROM " . $default->owl_document_subscriptions_table . " WHERE id = " . $iDocumentSubscriptionID);
  171 + if ($sql->next_record()) {
  172 + $oDocumentSubscription = & new DocumentSubscription($sql->f("document_id"), $sql->f("user_id"));
  173 + $oDocumentSubscription->iID = $iDocumentSubscriptionID;
  174 + return $oDocumentSubscription;
  175 + }
  176 + $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iDocumentSubscriptionID . " table = document_subscriptions";
  177 + return false;
  178 + }
  179 +}
  180 +?>
... ...
lib/subscriptions/FolderSubscription.inc 0 → 100644
  1 +<?php
  2 +/**
  3 + *
  4 + * $Id$
  5 + *
  6 + * Represents a folder subscription.
  7 + *
  8 + * Licensed under the GNU GPL. For full terms see the file COPYING.
  9 + *
  10 + * @version $Revision$
  11 + * @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
  12 + *
  13 + * @package lib.subscriptions
  14 + */
  15 +class FolderSubscription {
  16 +
  17 + /**
  18 + * Primary key
  19 + */
  20 + var $iID;
  21 + /**
  22 + * The ID of the folder subscribed to
  23 + */
  24 + var $iFolderID;
  25 + /**
  26 + * The ID of the user subscribed to the folder
  27 + */
  28 + var $iUserID;
  29 +
  30 + /**
  31 + * Creates a new folder subscription object
  32 + *
  33 + * @param integer the folder ID
  34 + * @param integer the user ID
  35 + */
  36 + function FolderSubscription($iFolderID, $iUserID) {
  37 + //id of -1 means that the object has not yet been stored in the database
  38 + $this->iID = -1;
  39 + $this->iFolderID = $iFolderID;
  40 + $this->iUserID = $iUserID;
  41 + }
  42 +
  43 + /**
  44 + * Get the primary key of the current folder subscription object
  45 + *
  46 + * @return integer primary key of folder subscription
  47 + */
  48 + function getID() {
  49 + return $this->iID;
  50 + }
  51 +
  52 + /**
  53 + * Get the primary key of the folder.
  54 + *
  55 + * @return integer primary key of folder
  56 + */
  57 + function getFolderID() {
  58 + return $this->iFolderID;
  59 + }
  60 +
  61 + /**
  62 + * Set the folder id
  63 + *
  64 + * @param integer new folder primary key
  65 + */
  66 + function setFolderID($iNewValue) {
  67 + $this->iFolderID = $iNewValue;
  68 + }
  69 +
  70 + /**
  71 + * Get the primary key of the user
  72 + *
  73 + * @return integer primary key of user
  74 + */
  75 + function getUserID() {
  76 + return $this->iUserID;
  77 + }
  78 +
  79 + /**
  80 + * Set the user id
  81 + *
  82 + * @param integer new user primary key
  83 + */
  84 + function setUserID($iNewValue) {
  85 + $this->iUserID = $iNewValue;
  86 + }
  87 +
  88 + /**
  89 + * Create the current folder subscription in the database
  90 + *
  91 + * @return boolean true and set $this->iID with new primary key, false otherwise and set $_SESSION["errorMessage"]
  92 + */
  93 + function create() {
  94 + global $default, $lang_err_database; $lang_err_object_exists;
  95 + //if the object has not already been stored
  96 + if ($this->iID < 0) {
  97 + $sql = $default->db;
  98 + $result = $sql->query("INSERT INTO " . $default->owl_folder_subscriptions_table . " (user_id, folder_id) " .
  99 + "VALUES ($this->iUserID, $this->iFolderID)");
  100 + if ($result) {
  101 + $this->iID = $sql->insert_id();
  102 + // TODO: now create subscriptions for all the documents in this folder
  103 + return true;
  104 + }
  105 + $_SESSION["errorMessage"] = $lang_err_database;
  106 + return false;
  107 + }
  108 + $_SESSION["errorMessage"] = $lang_err_object_exists . "id = " . $this->iID . " table = folder_subscriptions";
  109 + return false;
  110 + }
  111 +
  112 + /**
  113 + * Update the current folder subscription values in the database
  114 + *
  115 + * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"]
  116 + */
  117 + function update() {
  118 + global $default, $lang_err_database, $lang_err_object_key;
  119 + //can only update the object if it has already been stored
  120 + if ($this->iID >= 0) {
  121 + $sql = $default->db;
  122 + $result = $sql->query("UPDATE " . $default->owl_folder_subscriptions_table . " SET " .
  123 + "user_id = $this->iUserID, " .
  124 + "folder_id = $this->iFolderID " .
  125 + "WHERE id = " . $this->iID);
  126 + if ($result) {
  127 + return true;
  128 + }
  129 + $_SESSION["errorMessage"] = $lang_err_database;
  130 + return false;
  131 + }
  132 + $_SESSION["errorMessage"] = $lang_err_object_key;
  133 + }
  134 +
  135 +
  136 + /**
  137 + * Delete the current object from the database
  138 + *
  139 + * @return boolean true and reset id to -1 on successful delete, false otherwise and set $_SESSION["errorMessage"]
  140 + */
  141 + function delete() {
  142 + global $default, $lang_err_database, $lang_err_object_key;
  143 + if ($this->iID >= 0) {
  144 + $sql = $default->db;
  145 + $result = $sql->query("DELETE FROM " . $default->owl_folder_subscriptions_table . " WHERE id = " . $this->iID);
  146 + if ($result) {
  147 + $this->iID = -1;
  148 + // TODO: remove all document subscriptions?
  149 + return true;
  150 + }
  151 + $_SESSION["errorMessage"] = $lang_err_database;
  152 + return false;
  153 + }
  154 + $_SESSION["errorMessage"] = $lang_err_object_key;
  155 + return false;
  156 +
  157 + }
  158 +
  159 + /**
  160 + * Static function.
  161 + * Given a folder subscription primary key will create
  162 + * a folder subscription object and populate it with the corresponding
  163 + * database values
  164 + *
  165 + * @param integer primary key of folder subscription to get
  166 + *
  167 + * @return object folder subscription object on successful retrieval, false otherwise and set $_SESSION["errorMessage"]
  168 + */
  169 + function get($iFolderSubscriptionID) {
  170 + global $default, $lang_err_object_not_exist;
  171 + $sql = $default->db;
  172 + $sql->query("SELECT * FROM " . $default->owl_folder_subscriptions_table . " WHERE id = " . $iFolderSubscriptionID);
  173 + if ($sql->next_record()) {
  174 + $oFolderSubscription = & new FolderSubscription($sql->f("folder_id"), $sql->f("user_id"));
  175 + $oFolderSubscription->iID = $iFolderSubscriptionID;
  176 + return $oFolderSubscription;
  177 + }
  178 + $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iFolderSubscriptionID . " table = folder_subscriptions";
  179 + return false;
  180 + }
  181 +}
  182 +?>
... ...
tests/subscriptions/documentSubscription.php 0 → 100644
  1 +<?php
  2 +
  3 +require_once("../../config/dmsDefaults.php");
  4 +
  5 +/**
  6 + * Contains unit test code for class DocumentSubscription in /lib/subscription/DocumentSubscription.inc
  7 + *
  8 + * Tests are:
  9 + * - creation of document subscription object
  10 + * - setting/getting of values
  11 + * - storing of object
  12 + * - updating of object
  13 + * - deletion of object
  14 + * @package tests.subscriptions
  15 + */
  16 +if (checkSession()) {
  17 + require_once("$default->owl_fs_root/lib/subscriptions/DocumentSubscription.inc");
  18 +
  19 + echo "<b>Testing creation of new document subscription object</b><br>";
  20 + $oDocumentSubscription = & new DocumentSubscription(1, 1);
  21 + if (isset($oDocumentSubscription)) {
  22 + echo "Passed document subscription creation test<br><br>";
  23 +
  24 + echo "<b>Testing getting and setting of values</b><br><br>";
  25 +
  26 + echo "Current value of primary key: " . $oDocumentSubscription->getID() . "<br>";
  27 + echo "This value CANNOT be altered manually<br><br>";
  28 +
  29 + echo "Current value of document subscription user id: " . $oDocumentSubscription->getUserID() . "<br>";
  30 + echo "Setting document subscription user id to: 12<br>";
  31 + $oDocumentSubscription->setUserID(12);
  32 + echo "New value of document subscription user id: " . $oDocumentSubscription->getUserID() . "<br><br>";
  33 +
  34 + echo "Current value of document subscription document id: " . $oDocumentSubscription->getDocumentID() . "<br>";
  35 + echo "Setting document subscription document id to 34<br>";
  36 + $oDocumentSubscription->setDocumentID(34);
  37 + echo "New document subscription document id: " . $oDocumentSubscription->getDocumentID() . "<br><br>";
  38 +
  39 + echo "<b>Testing storing of object in database</b><br>";
  40 + if ($oDocumentSubscription->create()) {
  41 + echo "Passed storing of object in database test<br><br>";
  42 +
  43 + echo "<b>Testing object updating</b><br>";
  44 + if ($oDocumentSubscription->update()) {
  45 + echo "Passed object updating test<br><br>";
  46 +
  47 + echo "<b>Testing getting of object from database using primary key</b><br>";
  48 + $oNewDocumentSubscription = & DocumentSubscription::get($oDocumentSubscription->getID());
  49 + if (isset($oNewDocumentSubscription)) {
  50 + echo "<pre> " . arrayToString($oNewDocumentSubscription) . "</pre><br>";
  51 + echo "Passed getting of object from db using primary key<br><br>";
  52 +
  53 + echo "<b>Testing deletion of object from database</b><br>";
  54 + if ($oDocumentSubscription->delete()) {
  55 + echo "Passed deletion of object from database test.<br><br>END OF UNIT TEST";
  56 + } else {
  57 + echo "Failed deletion of object from database test";
  58 + }
  59 + } else {
  60 + echo "Failed getting of object test.<br> Tests not run (a)deletion of object<br>";
  61 + }
  62 + } else {
  63 + echo "Failed object updating test.<br> Tests not run (a)deletion of object (b)getting of object using id<br>";
  64 + }
  65 + } else {
  66 + echo "Failed storing of object in database test.<br> Tests not run (a)updating of object (b)deletion of object (c)getting of object using id<br>";
  67 + }
  68 + } else {
  69 + echo "Failed document subscription creation tests.<br>Tests not run: (a)setting/getting of values (b)storing of object (c)updating of object (d)deletion of object (e)getting of object using id<br>";
  70 + }
  71 +}
  72 +
  73 +?>
... ...
tests/subscriptions/folderSubscription.php 0 → 100644
  1 +<?php
  2 +
  3 +require_once("../../config/dmsDefaults.php");
  4 +
  5 +/**
  6 + * Contains unit test code for class FolderSubscription in /lib/subscription/FolderSubscription.inc
  7 + *
  8 + * Tests are:
  9 + * - creation of document subscription object
  10 + * - setting/getting of values
  11 + * - storing of object
  12 + * - updating of object
  13 + * - deletion of object
  14 + * @package tests.subscriptions
  15 + */
  16 +if (checkSession()) {
  17 + require_once("$default->owl_fs_root/lib/subscriptions/FolderSubscription.inc");
  18 +
  19 + echo "<b>Testing creation of new folder subscription object</b><br>";
  20 + $oFolderSubscription = & new FolderSubscription(1, 1);
  21 + if (isset($oFolderSubscription)) {
  22 + echo "Passed folder subscription creation test<br><br>";
  23 +
  24 + echo "<b>Testing getting and setting of values</b><br><br>";
  25 +
  26 + echo "Current value of primary key: " . $oFolderSubscription->getID() . "<br>";
  27 + echo "This value CANNOT be altered manually<br><br>";
  28 +
  29 + echo "Current value of folder subscription user id: " . $oFolderSubscription->getUserID() . "<br>";
  30 + echo "Setting folder subscription user id to: 23<br>";
  31 + $oFolderSubscription->setUserID(23);
  32 + echo "New value of folder subscription user id: " . $oFolderSubscription->getUserID() . "<br><br>";
  33 +
  34 + echo "Current value of folder subscription folder id: " . $oFolderSubscription->getFolderID() . "<br>";
  35 + echo "Setting folder subscription folder id to 56<br>";
  36 + $oFolderSubscription->setFolderID(56);
  37 + echo "New folder subscription folder id: " . $oFolderSubscription->getFolderID() . "<br><br>";
  38 +
  39 + echo "<b>Testing storing of object in database</b><br>";
  40 + if ($oFolderSubscription->create()) {
  41 + echo "Passed storing of object in database test<br><br>";
  42 +
  43 + echo "<b>Testing object updating</b><br>";
  44 + if ($oFolderSubscription->update()) {
  45 + echo "Passed object updating test<br><br>";
  46 +
  47 + echo "<b>Testing getting of object from database using primary key</b><br>";
  48 + $oNewFolderSubscription = & FolderSubscription::get($oFolderSubscription->getID());
  49 + if (isset($oNewFolderSubscription)) {
  50 + echo "<pre> " . arrayToString($oNewFolderSubscription) . "</pre><br>";
  51 + echo "Passed getting of object from db using primary key<br><br>";
  52 +
  53 + echo "<b>Testing deletion of object from database</b><br>";
  54 + if ($oFolderSubscription->delete()) {
  55 + echo "Passed deletion of object from database test.<br><br>END OF UNIT TEST";
  56 + } else {
  57 + echo "Failed deletion of object from database test(" . $_SESSION["errorMessage"] . ")";
  58 + }
  59 + } else {
  60 + echo "Failed getting of object test(" . $_SESSION["errorMessage"] . ").<br> Tests not run (a)deletion of object<br>";
  61 + }
  62 + } else {
  63 + echo "Failed object updating test(" . $_SESSION["errorMessage"] . ").<br> Tests not run (a)deletion of object (b)getting of object using id<br>";
  64 + }
  65 + } else {
  66 + echo "Failed storing of object in database test (" . $_SESSION["errorMessage"] . ").<br> Tests not run (a)updating of object (b)deletion of object (c)getting of object using id<br>";
  67 + }
  68 + } else {
  69 + echo "Failed folder subscription creation tests(" . $_SESSION["errorMessage"] . ").<br>Tests not run: (a)setting/getting of values (b)storing of object (c)updating of object (d)deletion of object (e)getting of object using id<br>";
  70 + }
  71 +}
  72 +
  73 +?>
... ...