Commit c2480749576bf852591478587b8f6c91b5befb57
1 parent
8137ba62
Initial revision. Object that represents roles table in database
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@378 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
158 additions
and
0 deletions
lib/roles/Role.inc
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | +* package lib.roles | ||
| 4 | +* | ||
| 5 | +* Class Role | ||
| 6 | +* Represents a role as per the roles database table | ||
| 7 | +* | ||
| 8 | +* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa | ||
| 9 | +* @date 20 January 2003 | ||
| 10 | +*/ | ||
| 11 | + | ||
| 12 | +class Role { | ||
| 13 | + | ||
| 14 | + /** role object primary key */ | ||
| 15 | + var $iId; | ||
| 16 | + /** role name */ | ||
| 17 | + var $sName; | ||
| 18 | + /** role has document read permission */ | ||
| 19 | + var $bCanRead; | ||
| 20 | + /** role has document write */ | ||
| 21 | + var $bCanWrite; | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * Default constructor | ||
| 25 | + * | ||
| 26 | + * @param String Role name | ||
| 27 | + * @param boolean Role has document read permission | ||
| 28 | + * @param boolean Role has document write permission | ||
| 29 | + * | ||
| 30 | + */ | ||
| 31 | + function Role($sNewName, $bNewCanRead, $bNewCanWrite) { | ||
| 32 | + //object not yet created in database | ||
| 33 | + $this->iId = -1; | ||
| 34 | + $this->sName = $sNewName; | ||
| 35 | + $this->bCanRead = $bNewCanRead; | ||
| 36 | + $this->bCanWrite = $bNewCanWrite; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * Create the current object in the database | ||
| 41 | + * | ||
| 42 | + * @return boolean on successful store, false otherwise and set $_SESSION["errorMessage"] | ||
| 43 | + * | ||
| 44 | + */ | ||
| 45 | + function create() { | ||
| 46 | + global $default, $lang_err_database, $lang_err_object_exists; | ||
| 47 | + //if the object hasn't been created | ||
| 48 | + if ($this->iId < 0) { | ||
| 49 | + $sql = new Owl_DB(); | ||
| 50 | + $result = $sql->query("INSERT INTO " . $default->owl_roles_table . " (name, can_read, can_write) VALUES ('" . addslashes($this->sName) . "', " . ($this->bCanRead ? 1 : 0) . ", " . ($this->bCanWrite ? 1 : 0) . ")"); | ||
| 51 | + if ($result) { | ||
| 52 | + $this->iId = $sql->insert_id(); | ||
| 53 | + return true; | ||
| 54 | + } | ||
| 55 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 56 | + return false; | ||
| 57 | + } | ||
| 58 | + $_SESSION["errorMessage"] = $lang_err_object_exists . "id = " . $this->iId . " table = document_fields"; | ||
| 59 | + return false; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * Update the values in the database table with the object's current values | ||
| 64 | + * | ||
| 65 | + * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"] | ||
| 66 | + * | ||
| 67 | + */ | ||
| 68 | + function update() { | ||
| 69 | + global $default, $lang_err_database, $lang_err_object_key; | ||
| 70 | + //only update if the object has been stored | ||
| 71 | + if ($this->iId > 0) { | ||
| 72 | + $sql = new Owl_DB(); | ||
| 73 | + $result = $sql->query("UPDATE " . $default->owl_roles_table . " SET name = '" . addslashes($this->sName) . "', can_read = " . ($this->bCanRead ? 1 : 0) . ", can_write = " . ($this->bCanWrite ? 1 : 0) . " WHERE id = $this->iId"); | ||
| 74 | + if ($result) { | ||
| 75 | + return true; | ||
| 76 | + } | ||
| 77 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 78 | + return false; | ||
| 79 | + } | ||
| 80 | + $_SESSION["errorMessage"] = $lang_err_object_key; | ||
| 81 | + return false; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * Delete the current object from the database | ||
| 86 | + * | ||
| 87 | + * @return boolean true on successful deletion, false otherwise and set $_SESSION["errorMessage"] | ||
| 88 | + * | ||
| 89 | + */ | ||
| 90 | + function delete() { | ||
| 91 | + global $default, $lang_err_database, $lang_err_object_key; | ||
| 92 | + //only delete the object if it exists in the database | ||
| 93 | + if ($this->iId >= 0) { | ||
| 94 | + $sql = new Owl_DB(); | ||
| 95 | + $result = $sql->query("DELETE FROM $default->owl_roles_table WHERE id = $this->iId"); | ||
| 96 | + if ($result) { | ||
| 97 | + return true; | ||
| 98 | + } | ||
| 99 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 100 | + return false; | ||
| 101 | + } | ||
| 102 | + $_SESSION["errorMessage"] = $lang_err_object_key; | ||
| 103 | + return false; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * Static function. | ||
| 108 | + * Given a roles primary key it will create a | ||
| 109 | + * Roles object and populate it with the | ||
| 110 | + * corresponding database values | ||
| 111 | + * | ||
| 112 | + * @return WebDocument populated WebDocument object on successful query, false otherwise and set $_SESSION["errorMessage"] | ||
| 113 | + */ | ||
| 114 | + function & get($iRoleID) { | ||
| 115 | + global $default; | ||
| 116 | + $sql = new Owl_DB(); | ||
| 117 | + $result = $sql->query("SELECT * FROM $default->owl_roles_table WHERE id = $iRoleID"); | ||
| 118 | + if ($result) { | ||
| 119 | + if ($sql->next_record()) { | ||
| 120 | + $oRole = & new Role($sql->f("document_id"), $sql->f("web_site_id"), $sql->f("unit_id"), $sql->f("status_id"), $sql->f("datetime")); | ||
| 121 | + $oRole->iId = $iRoleID; | ||
| 122 | + return $oRole; | ||
| 123 | + } | ||
| 124 | + $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iRoleID . " table = $default->owl_roles_table"; | ||
| 125 | + return false; | ||
| 126 | + } | ||
| 127 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 128 | + return false; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | +/** | ||
| 132 | + * Static function | ||
| 133 | + * Get a list of web documents | ||
| 134 | + * | ||
| 135 | + * @param String Where clause (not required) | ||
| 136 | + * | ||
| 137 | + * @return Array array of WebDocument objects, false otherwise and set $_SESSION["errorMessage"] | ||
| 138 | + */ | ||
| 139 | + function getList($sWhereClause = null) { | ||
| 140 | + global $default, $lang_err_database; | ||
| 141 | + $aRoleArray; | ||
| 142 | + settype($aRoleArray, "array"); | ||
| 143 | + $sql = new Owl_DB(); | ||
| 144 | + $result = $sql->query("SELECT * FROM " . $default->owl_roles_table . (isset($sWhereClause) ? " " . $sWhereClause : "")); | ||
| 145 | + if ($result) { | ||
| 146 | + $iCount = 0; | ||
| 147 | + while ($sql->next_record()) { | ||
| 148 | + $oRole = & Role::get($sql->f("id")); | ||
| 149 | + $aRoleArray[$iCount] = $oRole; | ||
| 150 | + $iCount++; | ||
| 151 | + } | ||
| 152 | + return $aRoleArray; | ||
| 153 | + } | ||
| 154 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 155 | + return false; | ||
| 156 | + } | ||
| 157 | +} | ||
| 158 | +?> |