baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / controlledVocab / ControlledVocabEntryDAO.inc.php
blob4f6d13125d7fa7e090944b8c0295625371531e20
1 <?php
3 /**
4 * @file ControlledVocabEntryDAO.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class ControlledVocabEntryDAO
10 * @ingroup controlled_vocab
11 * @see ControlledVocabEntry
13 * @brief Operations for retrieving and modifying ControlledVocabEntry objects
16 //$Id: ControlledVocabEntryDAO.inc.php,v 1.4 2009/05/13 00:13:20 asmecher Exp $
18 import('controlledVocab.ControlledVocabEntry');
20 class ControlledVocabEntryDAO extends DAO {
21 /**
22 * Retrieve a controlled vocab entry by controlled vocab entry ID.
23 * @param $controlledVocabEntryId int
24 * @param $controlledVocabEntry int optional
25 * @return ControlledVocabEntry
27 function getById($controlledVocabEntryId, $controlledVocabId = null) {
28 $params = array((int) $controlledVocabEntryId);
29 if (!empty($controlledVocabId)) $params[] = (int) $controlledVocabId;
31 $result =& $this->retrieve(
32 'SELECT * FROM controlled_vocab_entries WHERE controlled_vocab_entry_id = ?' .
33 (!empty($controlledVocabId)?' AND controlled_vocab_id = ?':''),
34 $params
37 $returner = null;
38 if ($result->RecordCount() != 0) {
39 $returner = $this->_fromRow($result->GetRowAssoc(false));
41 $result->Close();
42 return $returner;
45 /**
46 * Construct a new data object corresponding to this DAO.
47 * @return ControlledVocabEntry
49 function newDataObject() {
50 return new ControlledVocabEntry();
53 /**
54 * Internal function to return an ControlledVocabEntry object from a
55 * row.
56 * @param $row array
57 * @return ControlledVocabEntry
59 function _fromRow(&$row) {
60 $controlledVocabEntry = $this->newDataObject();
61 $controlledVocabEntry->setControlledVocabId($row['controlled_vocab_id']);
62 $controlledVocabEntry->setId($row['controlled_vocab_entry_id']);
63 $controlledVocabEntry->setSequence($row['seq']);
65 $this->getDataObjectSettings('controlled_vocab_entry_settings', 'controlled_vocab_entry_id', $row['controlled_vocab_entry_id'], $controlledVocabEntry);
67 return $controlledVocabEntry;
70 /**
71 * Get the list of fields for which data can be localized.
72 * @return array
74 function getLocaleFieldNames() {
75 return array('name');
78 /**
79 * Update the localized fields for this table
80 * @param $controlledVocabEntry object
82 function updateLocaleFields(&$controlledVocabEntry) {
83 $this->updateDataObjectSettings('controlled_vocab_entry_settings', $controlledVocabEntry, array(
84 'controlled_vocab_entry_id' => $controlledVocabEntry->getId()
85 ));
88 /**
89 * Insert a new ControlledVocabEntry.
90 * @param $controlledVocabEntry ControlledVocabEntry
91 * @return int
93 function insertObject(&$controlledVocabEntry) {
94 $this->update(
95 sprintf('INSERT INTO controlled_vocab_entries
96 (controlled_vocab_id, seq)
97 VALUES
98 (?, ?)'),
99 array(
100 (int) $controlledVocabEntry->getControlledVocabId(),
101 (float) $controlledVocabEntry->getSequence()
104 $controlledVocabEntry->setId($this->getInsertId());
105 $this->updateLocaleFields($controlledVocabEntry);
106 return $controlledVocabEntry->getId();
110 * Delete a controlled vocab entry.
111 * @param $controlledVocabEntry ControlledVocabEntry
112 * @return boolean
114 function deleteObject($controlledVocabEntry) {
115 return $this->deleteObjectById($controlledVocabEntry->getId());
119 * Delete a controlled vocab entry by controlled vocab entry ID.
120 * @param $controlledVocabEntryId int
121 * @return boolean
123 function deleteObjectById($controlledVocabEntryId) {
124 $params = array((int) $controlledVocabEntryId);
125 $this->update('DELETE FROM controlled_vocab_entry_settings WHERE controlled_vocab_entry_id = ?', $params);
126 return $this->update('DELETE FROM controlled_vocab_entries WHERE controlled_vocab_entry_id = ?', $params);
130 * Retrieve an iterator of controlled vocabulary entries matching a
131 * particular controlled vocabulary ID.
132 * @param $controlledVocabId int
133 * @return object DAOResultFactory containing matching CVE objects
135 function getByControlledVocabId($controlledVocabId, $rangeInfo = null) {
136 $result =& $this->retrieveRange(
137 'SELECT * FROM controlled_vocab_entries WHERE controlled_vocab_id = ? ORDER BY seq',
138 array((int) $controlledVocabId),
139 $rangeInfo
142 $returner = new DAOResultFactory($result, $this, '_fromRow');
143 return $returner;
147 * Update an existing review form element.
148 * @param $controlledVocabEntry ControlledVocabEntry
150 function updateObject(&$controlledVocabEntry) {
151 $returner = $this->update(
152 'UPDATE controlled_vocab_entries
153 SET controlled_vocab_id = ?,
154 seq = ?
155 WHERE controlled_vocab_entry_id = ?',
156 array(
157 (int) $controlledVocabEntry->getControlledVocabId(),
158 (float) $controlledVocabEntry->getSequence(),
159 (int) $controlledVocabEntry->getId()
162 $this->updateLocaleFields($controlledVocabEntry);
166 * Sequentially renumber entries in their sequence order.
168 function resequence($controlledVocabId) {
169 $result =& $this->retrieve(
170 'SELECT controlled_vocab_entry_id FROM controlled_vocab_entries WHERE controlled_vocab_id = ? ORDER BY seq',
171 array((int) $controlledVocabId)
174 for ($i=1; !$result->EOF; $i++) {
175 list($controlledVocabEntryId) = $result->fields;
176 $this->update(
177 'UPDATE controlled_vocab_entries SET seq = ? WHERE controlled_vocab_entry_id = ?',
178 array(
179 (int) $i,
180 (int) $controlledVocabEntryId
184 $result->MoveNext();
187 $result->close();
188 unset($result);
192 * Get the ID of the last inserted controlled vocab.
193 * @return int
195 function getInsertId() {
196 return parent::getInsertId('controlled_vocab_entries', 'controlled_vocab_entry_id');