Commit 50d1361db68a017bfa666c97a9a42733a9e4327d

Authored by Brad Shuttleworth
1 parent a402fe18

Cleanup of sql->query locations. NBM: PLEASE REVIEW.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@5509 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/browse/CategoryBrowser.inc deleted
1   -<?php
2   -
3   -require_once(KT_LIB_DIR . '/security/Permission.inc');
4   -require_once(KT_LIB_DIR . '/users/User.inc');
5   -require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
6   -require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
7   -/**
8   - * $Id$
9   - *
10   - * Contains category view document browsing business logic.
11   - *
12   - * Copyright (c) 2006 Jam Warehouse http://www.jamwarehouse.com
13   - *
14   - * This program is free software; you can redistribute it and/or modify
15   - * it under the terms of the GNU General Public License as published by
16   - * the Free Software Foundation; using version 2 of the License.
17   - *
18   - *
19   - * This program is distributed in the hope that it will be useful,
20   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22   - * GNU General Public License for more details.
23   - *
24   - * You should have received a copy of the GNU General Public License
25   - * along with this program; if not, write to the Free Software
26   - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27   - *
28   - * @version $Revision$
29   - * @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
30   - * @package lib.browse
31   - */
32   -class CategoryBrowser extends Browser {
33   -
34   - /**
35   - * Construct a new FolderBrowser instance with the specified sort criteria
36   - *
37   - * @param string the field to sort the results by
38   - * @param string the direction to sort the results
39   - */
40   - function CategoryBrowser($sSortField = "name", $sSortDirection = "asc", $aNewSortCriteria = array()) {
41   - Browser::Browser($sSortField, $sSortDirection, $aNewSortCriteria);
42   - }
43   -
44   - /**
45   - * Browse the documents by category
46   - *
47   - * @return array categories and documents
48   - */
49   - function browse() {
50   - global $default;
51   -
52   - // XXX: Shouldn't be picking it out of $_REQUEST
53   - $sCategoryName = $_REQUEST['fCategoryName'];
54   -
55   - // browsing by category
56   - $this->setBrowseStart($sCategoryName);
57   - $sCategoryName = $sCategoryName;
58   -
59   - // TODO: add this to default inserts
60   - $categoryField = "Category";
61   - $results = array();
62   - $sql = $default->db;
63   -
64   - // lookup document_fields id for category
65   - $categoryFieldID = lookupID($default->document_fields_table, "name", "$categoryField");
66   - $default->log->debug("CategoryBrowser::browse() categoryFieldID=$categoryFieldID");
67   -
68   - if ($sCategoryName == "") {
69   - if ($categoryFieldID) {
70   - // no category value supplied, so return a list of categories
71   - // set the first value to "categories"
72   - $results["categories"][] = "Categories";
73   - // get a list of category values
74   - // FIXME: exclude deleted document metadata
75   - $query = "SELECT DISTINCT value FROM $default->document_fields_link_table WHERE document_field_id = ? ORDER BY value " . ($this->sSortField == "name" ? $this->sSortDirection : "ASC");/*ok*/
76   - $aParams = array($categoryFieldID);
77   - $default->log->info("CategoryBrowser::browse() category listing query=$query; $this->sSortField");
78   - $sql->query(array($query, $aParams));
79   - // loop through resultset, build array and return
80   - while ($sql->next_record()) {
81   - $results["categories"][] = $sql->f("value");
82   - }
83   - }
84   - // its ok if we return an empty array- the UI's responsibility to check and print an error
85   - return $results;
86   - } else {
87   - $aLookupCriteria = $this->aSortCriteria[$this->sSortField]["lookup"];
88   -
89   - $results["categories"][] = $sCategoryName;
90   -
91   - // we have a category to use, so find all the documents
92   - // with this category value
93   - $categoryQuery = "SELECT df.document_id FROM $default->document_fields_link_table df " . /*ok*/
94   - "INNER JOIN $default->documents_table d ON df.document_id = d.id ";
95   - if ( isset($aLookupCriteria) ) {
96   - $categoryQuery .= "INNER JOIN " . $aLookupCriteria["table"] . " lt ON ";
97   - $categoryQuery .= "d.$this->sSortField" . "=lt." . (isset($aLookupCriteria["joinColumn"]) ? $aLookupCriteria["joinColumn"] : "id");
98   - }
99   - $categoryQuery .= " WHERE df.document_field_id = ? AND value = ? " . (isset($aLookupCriteria["whereClause"]) ? "AND lt." . $aLookupCriteria["whereClause"] : "") . " ";;
100   - $aParams = array($categoryFieldID, $sCategoryName);
101   - if ( isset($aLookupCriteria) ) {
102   - $categoryQuery .= "ORDER BY lt." . $aLookupCriteria["field"] . " $this->sSortDirection";
103   - } else {
104   - $categoryQuery .= "ORDER BY d.$this->sSortField $this->sSortDirection";
105   - }
106   -
107   -
108   - $default->log->debug("categoryQuery=$categoryQuery");
109   -
110   - $sql->query(array($categoryQuery, $aParams));
111   - // loop through resultset and add to array
112   - $results["accessDenied"] = false;
113   - while ($sql->next_record()) {
114   - $oDocument = & Document::get($sql->f("document_id"));
115   - // only if the document is live
116   - if ($oDocument->isLive()) {
117   - if (Permission::userHasDocumentReadPermission($oDocument)) {
118   - $results["documents"][] = $oDocument;
119   - } else {
120   - $results["accessDenied"] = true;
121   - }
122   - }
123   - }
124   -
125   - return $results;
126   - }
127   - }
128   -
129   - function getSectionName() {
130   - return "Manage Categories";
131   - }
132   -}
lib/browse/DocumentTypeBrowser.inc deleted
1   -<?php
2   -require_once(KT_LIB_DIR . '/security/Permission.inc');
3   -require_once(KT_LIB_DIR . '/users/User.inc');
4   -require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
5   -require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
6   -/**
7   - * $Id$
8   - *
9   - * Contains document type view document browsing business logic.
10   - *
11   - * Copyright (c) 2006 Jam Warehouse http://www.jamwarehouse.com
12   - *
13   - * This program is free software; you can redistribute it and/or modify
14   - * it under the terms of the GNU General Public License as published by
15   - * the Free Software Foundation; using version 2 of the License.
16   - *
17   - *
18   - * This program is distributed in the hope that it will be useful,
19   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
20   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21   - * GNU General Public License for more details.
22   - *
23   - * You should have received a copy of the GNU General Public License
24   - * along with this program; if not, write to the Free Software
25   - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26   - *
27   - * @version $Revision$
28   - * @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
29   - * @package lib.browse
30   - */
31   -class DocumentTypeBrowser extends Browser {
32   -
33   - /**
34   - * Construct a new FolderBrowser instance with the specified sort criteria
35   - *
36   - * @param string the field to sort the results by
37   - * @param string the direction to sort the results
38   - */
39   - function DocumentTypeBrowser($sSortField = "name", $sSortDirection = "asc", $aNewSortCriteria = array()) {
40   - Browser::Browser($sSortField, $sSortDirection, $aNewSortCriteria);
41   - }
42   -
43   - /**
44   - * Browse the documents by document type
45   - *
46   - * @return array document types and documents
47   - */
48   - function browse() {
49   - global $default;
50   -
51   - // XXX: Shouldn't be getting this from $_REQUEST
52   - $iDocumentTypeID = $_REQUEST['fDocumentTypeID'];
53   -
54   - // browsing by document type id
55   - $this->setBrowseStart($iDocumentTypeID);
56   - $iDocumentTypeID = $iDocumentTypeID;
57   -
58   - $results = array();
59   - $sql = $default->db;
60   -
61   - if ($iDocumentTypeID == "") {
62   - // no document type id supplied, so return a list of document types
63   - // set the first value to "Document Types"
64   - $results["documentTypes"][] = array("name" => "Document Types");
65   -
66   - // return a list of document types
67   - /*ok*/$query = "SELECT * FROM $default->document_types_table ORDER BY name " . ($this->sSortField == "name" ? $this->sSortDirection : "ASC");
68   -
69   - $sql->query($query);
70   - while ($sql->next_record()) {
71   - $results["documentTypes"][] = array ("id" => $sql->f("id"), "name" => $sql->f("name"));
72   - }
73   - return $results;
74   - } else {
75   - $aLookupCriteria = $this->aSortCriteria[$this->sSortField]["lookup"];
76   -
77   - // lookup document type name from the passed in id
78   - $documentTypeName = lookupField($default->document_types_table, "name", "id", $iDocumentTypeID);
79   - $results["documentTypes"][] = array("id" => $iDocumentTypeID, "name" => $documentTypeName);
80   -
81   - // create query to retrieve documents with this document type
82   - $documentQuery = "SELECT d.id as id FROM $default->documents_table d ";/*wc*/
83   - if ( isset($aLookupCriteria) ) {
84   - //$documentQuery .= "INNER JOIN " . $aLookupCriteria["table"] . " lt ON d.$this->sSortField=lt.id ";
85   - $documentQuery .= "INNER JOIN " . $aLookupCriteria["table"] . " lt ON ";
86   - $documentQuery .= "d.$this->sSortField" . "=lt." . (isset($aLookupCriteria["joinColumn"]) ? $aLookupCriteria["joinColumn"] : "id");
87   -
88   - }
89   - //$documentQuery .= "WHERE document_type_id=$iDocumentTypeID ";
90   - $documentQuery .= " WHERE document_type_id=$iDocumentTypeID " . (isset($aLookupCriteria["whereClause"]) ? "AND lt." . $aLookupCriteria["whereClause"] : "") . " ";
91   - if ( isset($aLookupCriteria) ) {
92   - $documentQuery .= "ORDER BY lt." . $aLookupCriteria["field"] . " $this->sSortDirection";
93   - } else {
94   - $documentQuery .= "ORDER BY $this->sSortField $this->sSortDirection";
95   - }
96   -
97   - $default->log->debug("docTypeQuery=$documentQuery");
98   -
99   - // loop through resultset and populate array with document objects
100   - $sql->query($documentQuery);
101   - $results["accessDenied"] = false;
102   - while ($sql->next_record()) {
103   - $oDocument = & Document::get($sql->f("id"));
104   - // proceed if the document is live
105   - if ($oDocument->isLive()) {
106   - if (Permission::userHasDocumentReadPermission($oDocument)) {
107   - $results["documents"][] = $oDocument;
108   - } else {
109   - $results["accessDenied"] = true;
110   - }
111   - }
112   - }
113   - $default->log->debug("DocumentTypeBrowser::browse() results=" . arrayToString($results));
114   -
115   - return $results;
116   - }
117   - }
118   -
119   - function getSectionName() {
120   - return "Manage Document Types";
121   - }
122   -}
lib/documentmanagement/Document.inc
... ... @@ -367,11 +367,12 @@ class Document {
367 367 * Deletes content from document data tables
368 368 */
369 369 function cleanupDocumentData($iDocumentID) {
370   - global $default;
371   - $sql = $default->db;
372   -
373   - $result = $sql->query("DELETE FROM $default->document_text_table WHERE document_id = $iDocumentID");
374   - return $result;
  370 + // FIXME this appears to be deprecated, or at least should be
  371 + $sTable = KTUtil::getTableName('document_text')
  372 + $sQuery = "DELETE FROM $sTable WHERE document_id = ?";
  373 + $aParams = array($iDocumentID);
  374 + $res = DBUtil::runQuery(array($sQuery, $aParams));
  375 + return $res;
375 376 }
376 377 // }}}
377 378  
... ...
lib/documentmanagement/DocumentField.inc
... ... @@ -142,37 +142,16 @@ class DocumentField extends KTEntity {
142 142 *
143 143 * @return DocumentFieldLink populated DocumentFieldLink object on success, false otherwise and set $_SESSION["errorMessage"]
144 144 */
145   - function getAllDataTypes() {
146   - global $default, $lang_err_database;
147   - $aDataTypes;
148   - settype($aDataTypes, "array");
149   - $sql = $default->db;
150   - $result = $sql->query("SELECT id, name FROM " . $default->data_types_table );/*ok*/
151   - if ($result) {
152   - $iCount = 0;
153   - while ($sql->next_record()) {
154   -
155   - $aDataTypes[$iCount]["id"] = $sql->f("id");
156   - $aDataTypes[$iCount]["name"] = $sql->f("name");
157   - $iCount++;
158   - }
159   - return $aDataTypes;
160   - }
161   - $_SESSION["errorMessage"] = $lang_err_database;
162   - return false;
163   - }
164   -
165 145 function getLookupCount($iDocumentFieldID){
166   - global $default;
167   -
168   - $sql = $default->db;
169   - $result = $sql->query(array("SELECT COUNT(*) AS count FROM " . $default->metadata_table . " WHERE document_field_id = ?", $iDocumentFieldID));/*ok*/
170   - if ($result) {
171   - if ($sql->next_record()) {
172   - return $sql->f("count");
173   - }
174   - }
175   - return false;
  146 + $sTable = 'metadata';
  147 + $sQuery = "SELECT COUNT(*) AS count FROM " . $sTable . " WHERE document_field_id = ?";
  148 + $aParams = array($iDocumentFieldID);
  149 +
  150 + $res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'count');
  151 + if (PEAR::isError($res)) {
  152 + return false; // return $res;
  153 + }
  154 + return $res;
176 155 }
177 156  
178 157 function getType() {
... ...
lib/documentmanagement/DocumentFieldLink.inc
... ... @@ -129,8 +129,7 @@ class DocumentFieldLink extends KTEntity {
129 129 }
130 130  
131 131 function _table () {
132   - global $default;
133   - return $default->document_fields_link_table;
  132 + KUtil::getTableName('document_fields_link');
134 133 }
135 134  
136 135 /**
... ...
lib/documentmanagement/DocumentType.inc
... ... @@ -157,33 +157,6 @@ class DocumentType extends KTEntity {
157 157 function &getFieldsets() {
158 158 return KTFieldset::getForDocumentType($this);
159 159 }
160   -
161   -
162   - /**
163   - * Static function.
164   - * Given a document_fields primary key it will create a
165   - * DocumentTypes object and populate it with the
166   - * corresponding database values
167   - *
168   - * @return DocumentType populated DocumentType object on successful query, false otherwise and set $_SESSION["errorMessage"]
169   - */
170   - function & get($iDocumentTypeID) {
171   - global $default;
172   - $sql = $default->db;
173   - $result = $sql->query(array("SELECT * FROM ". $default->document_types_table ." WHERE id = ?", $iDocumentTypeID));/*ok*/
174   - if ($result) {
175   - if ($sql->next_record()) {
176   - $oDocumentType = & new DocumentType($sql->f("name"));
177   - $oDocumentType->iId = $sql->f("id");
178   - $oDocumentType->bDisabled = $sql->f("disabled");
179   - return $oDocumentType;
180   - }
181   - $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iDocumentID . " table = document_types";
182   - return false;
183   - }
184   - $_SESSION["errorMessage"] = $lang_err_database;
185   - return false;
186   - }
187 160  
188 161 /**
189 162 * Static- Get a list document types;
... ...
lib/documentmanagement/MetaData.inc
... ... @@ -86,39 +86,24 @@ class MetaData extends KTEntity {
86 86 return KTEntityUtil::get('MetaData', $iId);
87 87 }
88 88  
89   -/*
90   - * static function
91   - *
92   - * sets the id of the groupunit using their groupid
93   - *
94   - * @param String
95   - * The unit_ID
96   - *
97   - */
98   -
  89 + // FIXME this function makes no sense.
99 90 function setMetaDataID($iDocFieldId, $sMetaDataName)
100 91 {
101   - global $default;
102   - $sql = $default->db;
103   - $sQuery = "SELECT id FROM $default->metadata_table WHERE document_field_id = ? and name = ?";/*ok*/
104   - $aParams = array($iDocFieldId, $sMetaDataName);
105   - $result = $sql->query(array($sQuery, $aParams));
106   - if ($result) {
107   - if ($sql->next_record()) {
108   - $id = $sql->f("id");
109   -
110   - }else{
111   - $_SESSION["errorMessage"] = $lang_err_database;
112   - return false;
113   - }
114   -
115   - }else{
116   - $_SESSION["errorMessage"] = $lang_err_database;
117   - return false;
118   - }
119   -
120   - $this->iId = $id;
121   -
  92 + $sTable = KTUtil::getTableName('metadata');
  93 + $sQuery = "SELECT id FROM $sTable WHERE document_field_id = ? and name = ?";
  94 + $aParams = array($iDocFieldId, $sMetaDataName);
  95 + $res = DBUtil::getResultArray(array($sQuery, $aParams));
  96 +
  97 + if (PEAR::isError($res)) {
  98 + return false; // return $res;
  99 + }
  100 +
  101 + if (count($res) != 0) {
  102 + $this->iId = $id;
  103 + return $res[0]['id'];
  104 + } else {
  105 + return false; // return PEAR::raiseError(_kt('No such plugin pack'))
  106 + }
122 107 }
123 108  
124 109 function getList($sWhereClause = null) {
... ...