Commit 4d9493e24dc6f915715ab49af53e09cc4d407047

Authored by rob
1 parent 3a5dd500

Initial revision. Object that represents units table in database


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@414 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 221 additions and 0 deletions
lib/units/Unit.inc 0 → 100644
  1 +<?php
  2 +/**
  3 +* Class Unit
  4 +* Represents a unit as per the database table units
  5 +*
  6 +* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
  7 +* @date 21 January 2003
  8 +* @package lib.units
  9 +*/
  10 +
  11 +class Unit {
  12 +
  13 + /** object's primary key */
  14 + var $iId;
  15 + /** unit's name */
  16 + var $sName;
  17 + /** primary key to which the unit belongs */
  18 + var $iOrganisationID;
  19 + /** primary key of parent unit */
  20 + var $iParentID;
  21 +
  22 + function Unit($sNewName, $iNewOrganisationID, $iNewParentID) {
  23 + //object has not been created in database yet
  24 + $this->iId = -1;
  25 + $this->sName = $sNewName;
  26 + $this->iOrganisationID = $iNewOrganisationID;
  27 + $this->iParentID = $iNewParentID;
  28 + }
  29 +
  30 + /**
  31 + * Get the object's primary key
  32 + *
  33 + * @return int object's primary key
  34 + *
  35 + */
  36 + function getID() {
  37 + return $this->iId;
  38 + }
  39 +
  40 + /**
  41 + * Get the unit's name
  42 + *
  43 + * @return String unit's name
  44 + *
  45 + */
  46 + function getName() {
  47 + return $this->sName;
  48 + }
  49 +
  50 + /**
  51 + * Set the unit's name
  52 + *
  53 + * @param String Unit's name
  54 + *
  55 + */
  56 + function setName($sNewValue) {
  57 + $this->sName = $sNewValue;
  58 + }
  59 +
  60 + /**
  61 + * Get the primary key of the organisation to which this unit belongs
  62 + *
  63 + * @return int primary key of organisation to which this unit belongs
  64 + *
  65 + */
  66 + function getOrganisationID() {
  67 + return $this->iOrganisationID;
  68 + }
  69 +
  70 + /**
  71 + * Set the primary key of the organisation to which this unit belongs
  72 + *
  73 + * @param int Primary key of organisation to which this unit belongs
  74 + *
  75 + */
  76 + function setOrganisationID($iNewValue) {
  77 + $this->iOrganisationID = $iNewValue;
  78 + }
  79 +
  80 + /**
  81 + * Get the primary key of the parent unit
  82 + *
  83 + * @return int primary key of parent unit
  84 + *
  85 + */
  86 + function getParentID() {
  87 + return $this->iParentID;
  88 + }
  89 +
  90 + /**
  91 + * Set the primary key of the parent unit
  92 + *
  93 + * @param int Primary key of parent unit
  94 + *
  95 + */
  96 + function setParentID($iNewValue) {
  97 + $this->iParentID = $iNewValue;
  98 + }
  99 +
  100 + /**
  101 + * Create the current object in the database
  102 + *
  103 + * @return boolean on successful store, false otherwise and set $_SESSION["errorMessage"]
  104 + *
  105 + */
  106 + function create() {
  107 + global $default, $lang_err_database, $lang_err_object_exists;
  108 + //if the object hasn't been created
  109 + if ($this->iId < 0) {
  110 + $sql = new Owl_DB();
  111 + $result = $sql->query("INSERT INTO " . $default->owl_units_table . " (name, organisation_id, parent_id) VALUES ('" . addslashes($this->sName) . "', $this->iOrganisationID, $this->iParentID)");
  112 + if ($result) {
  113 + $this->iId = $sql->insert_id();
  114 + return true;
  115 + }
  116 + $_SESSION["errorMessage"] = $lang_err_database;
  117 + return false;
  118 + }
  119 + $_SESSION["errorMessage"] = $lang_err_object_exists . "id = " . $this->iId . " table = $default->owl_units_table";
  120 + return false;
  121 + }
  122 +
  123 + /**
  124 + * Update the values in the database table with the object's current values
  125 + *
  126 + * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"]
  127 + *
  128 + */
  129 + function update() {
  130 + global $default, $lang_err_database, $lang_err_object_key;
  131 + //only update if the object has been stored
  132 + if ($this->iId > 0) {
  133 + $sql = new Owl_DB();
  134 + $result = $sql->query("UPDATE " . $default->owl_units_table . " SET name = '" . addslashes($this->sName) . "', organisation_id = $this->iOrganisationID, parent_id = $this->iParentID WHERE id = $this->iId");
  135 + if ($result) {
  136 + return true;
  137 + }
  138 + $_SESSION["errorMessage"] = $lang_err_database;
  139 + return false;
  140 + }
  141 + $_SESSION["errorMessage"] = $lang_err_object_key;
  142 + return false;
  143 + }
  144 +
  145 + /**
  146 + * Delete the current object from the database
  147 + *
  148 + * @return boolean true on successful deletion, false otherwise and set $_SESSION["errorMessage"]
  149 + *
  150 + */
  151 + function delete() {
  152 + global $default, $lang_err_database, $lang_err_object_key;
  153 + //only delete the object if it exists in the database
  154 + if ($this->iId >= 0) {
  155 + $sql = new Owl_DB();
  156 + $result = $sql->query("DELETE FROM $default->owl_units_table WHERE id = $this->iId");
  157 + if ($result) {
  158 + return true;
  159 + }
  160 + $_SESSION["errorMessage"] = $lang_err_database;
  161 + return false;
  162 + }
  163 + $_SESSION["errorMessage"] = $lang_err_object_key;
  164 + return false;
  165 + }
  166 +
  167 + /**
  168 + * Static function.
  169 + * Given a web_documents primary key it will create a
  170 + * Unit object and populate it with the
  171 + * corresponding database values
  172 + *
  173 + * @return Unit populated Unit object on successful query, false otherwise and set $_SESSION["errorMessage"]
  174 + */
  175 + function & get($iUnitID) {
  176 + global $default;
  177 + $sql = new Owl_DB();
  178 + $result = $sql->query("SELECT * FROM $default->owl_units_table WHERE id = $iUnitID");
  179 + if ($result) {
  180 + if ($sql->next_record()) {
  181 + $oUnit = & new Unit(stripslashes($sql->f("name")), $sql->f("organization_id"), $sql->f("parent_id"));
  182 + $oUnit->iId = $iUnitID;
  183 + return $oUnit;
  184 + }
  185 + $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iUnitID . " table = $default->owl_units_table";
  186 + return false;
  187 + }
  188 + $_SESSION["errorMessage"] = $lang_err_database;
  189 + return false;
  190 + }
  191 +
  192 +/**
  193 + * Static function
  194 + * Get a list of web documents
  195 + *
  196 + * @param String Where clause (not required)
  197 + *
  198 + * @return Array array of Unit objects, false otherwise and set $_SESSION["errorMessage"]
  199 + */
  200 + function getList($sWhereClause = null) {
  201 + global $default, $lang_err_database;
  202 + $aUnitArray;
  203 + settype($aUnitArray, "array");
  204 + $sql = new Owl_DB();
  205 + $result = $sql->query("SELECT * FROM " . $default->owl_units_table . (isset($sWhereClause) ? " " . $sWhereClause : ""));
  206 + if ($result) {
  207 + $iCount = 0;
  208 + while ($sql->next_record()) {
  209 + $oUnit = & Unit::get($sql->f("id"));
  210 + $aUnitArray[$iCount] = $oUnit;
  211 + $iCount++;
  212 + }
  213 + return $aUnitArray;
  214 + }
  215 + $_SESSION["errorMessage"] = $lang_err_database;
  216 + return false;
  217 + }
  218 +
  219 +}
  220 +
  221 +?>
... ...