baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / controlledVocab / ControlledVocabDAO.inc.php
blobca545ccd32a72b373177d7bb55580e3293e5da54
1 <?php
3 /**
4 * @file ControlledVocabDAO.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 ControlledVocabDAO
10 * @ingroup controlled_vocab
11 * @see ControlledVocab
13 * @brief Operations for retrieving and modifying ControlledVocab objects.
16 //$Id: ControlledVocabDAO.inc.php,v 1.4 2009/05/13 00:13:20 asmecher Exp $
19 import('controlledVocab.ControlledVocab');
21 class ControlledVocabDAO extends DAO {
22 /**
23 * Retrieve a controlled vocab by controlled vocab ID.
24 * @param $controlledVocabId int
25 * @return ControlledVocab
27 function getById($controlledVocabId) {
28 $result =& $this->retrieve(
29 'SELECT * FROM controlled_vocabs WHERE controlled_vocab_id = ?', array((int) $controlledVocabId)
32 $returner = null;
33 if ($result->RecordCount() != 0) {
34 $returner =& $this->_fromRow($result->GetRowAssoc(false));
36 $result->Close();
37 return $returner;
40 /**
41 * Fetch a controlled vocab by symbolic info, building it if needed.
42 * @param $symbolic string
43 * @param $assocType int
44 * @param $assocId int
45 * @return $controlledVocab
47 function build($symbolic, $assocType, $assocId) {
48 // If one exists, fetch and return.
49 $controlledVocab = $this->getBySymbolic($symbolic, $assocType, $assocId);
50 if ($controlledVocab) return $controlledVocab;
52 // Otherwise, build one.
53 unset($controlledVocab);
54 $controlledVocab = $this->newDataObject();
55 $controlledVocab->setSymbolic($symbolic);
56 $controlledVocab->setAssocType($assocType);
57 $controlledVocab->setAssocId($assocId);
58 $this->insertObject($controlledVocab);
59 return $controlledVocab;
62 /**
63 * Construct a new data object corresponding to this DAO.
64 * @return ControlledVocabEntry
66 function newDataObject() {
67 return new ControlledVocab();
70 /**
71 * Internal function to return an ControlledVocab object from a row.
72 * @param $row array
73 * @return ControlledVocab
75 function _fromRow(&$row) {
76 $controlledVocab = $this->newDataObject();
77 $controlledVocab->setId($row['controlled_vocab_id']);
78 $controlledVocab->setAssocType($row['assoc_type']);
79 $controlledVocab->setAssocId($row['assoc_id']);
80 $controlledVocab->setSymbolic($row['symbolic']);
82 return $controlledVocab;
85 /**
86 * Insert a new ControlledVocab.
87 * @param $controlledVocab ControlledVocab
88 * @return int
90 function insertObject(&$controlledVocab) {
91 $this->update(
92 sprintf('INSERT INTO controlled_vocabs
93 (symbolic, assoc_type, assoc_id)
94 VALUES
95 (?, ?, ?)'),
96 array(
97 $controlledVocab->getSymbolic(),
98 (int) $controlledVocab->getAssocType(),
99 (int) $controlledVocab->getAssocId()
102 $controlledVocab->setId($this->getInsertId());
103 return $controlledVocab->getId();
107 * Update an existing controlled vocab.
108 * @param $controlledVocab ControlledVocab
109 * @return boolean
111 function updateObject(&$controlledVocab) {
112 $returner = $this->update(
113 sprintf('UPDATE controlled_vocabs
114 SET symbolic = ?,
115 assoc_type = ?,
116 assoc_id = ?
117 WHERE controlled_vocab_id = ?'),
118 array(
119 $controlledVocab->getSymbolic(),
120 (int) $controlledVocab->getAssocType(),
121 (int) $controlledVocab->getAssocId(),
122 (int) $controlledVocab->getId()
125 return $returner;
129 * Delete a controlled vocab.
130 * @param $controlledVocab ControlledVocab
131 * @return boolean
133 function deleteObject($controlledVocab) {
134 return $this->deleteObjectById($controlledVocab->getId());
138 * Delete a controlled vocab by controlled vocab ID.
139 * @param $controlledVocabId int
140 * @return boolean
142 function deleteObjectById($controlledVocabId) {
143 $params = array((int) $controlledVocabId);
144 $controlledVocabEntryDao =& DAORegistry::getDAO('ControlledVocabEntryDAO');
145 $controlledVocabEntries =& $controlledVocabEntryDao->getControlledVocabEntries($controlledVocabId);
146 while ($controlledVocabEntry =& $controlledVocabEntries->next()) {
147 $controlledVocabEntryDao->deleteObject($controlledVocabEntry);
148 unset($controlledVocabEntry);
150 return $this->update('DELETE FROM controlled_vocabs WHERE controlled_vocab_id = ?', $params);
154 * Retrieve an array of controlled vocabs matching the specified
155 * symbolic name and assoc info.
156 * @param $symbolic string
157 * @param $assocType int
158 * @param $assocId int
160 function getBySymbolic($symbolic, $assocType, $assocId) {
161 $result =& $this->retrieve(
162 'SELECT * FROM controlled_vocabs WHERE symbolic = ? AND assoc_type = ? AND assoc_id = ?',
163 array($symbolic, (int) $assocType, (int) $assocId)
166 $returner = null;
167 if ($result->RecordCount() != 0) {
168 $returner = $this->_fromRow($result->GetRowAssoc(false));
170 $result->Close();
171 return $returner;
175 * Get a list of controlled vocabulary options.
176 * @param $symbolic string
177 * @param $assocType int
178 * @param $assocId int
179 * @param $settingName string optional
180 * @return array $controlledVocabEntryId => $settingValue
182 function enumerateBySymbolic($symbolic, $assocType, $assocId, $settingName = 'name') {
183 $controlledVocab = $this->getBySymbolic($symbolic, $assocType, $assocId);
184 if (!$controlledVocab) {
185 $returner = array();
186 return $returner;
188 return $controlledVocab->enumerate($settingName);
192 * Get a list of controlled vocabulary options.
193 * @param $controlledVocabId int
194 * @param $settingName string optional
195 * @return array $controlledVocabEntryId => name
197 function enumerate($controlledVocabId, $settingName = 'name') {
198 $result =& $this->retrieve(
199 'SELECT e.controlled_vocab_entry_id,
200 COALESCE(l.setting_value, p.setting_value, n.setting_value) AS setting_value,
201 COALESCE(l.setting_type, p.setting_type, n.setting_type) AS setting_type
202 FROM controlled_vocab_entries e
203 LEFT JOIN controlled_vocab_entry_settings l ON (l.controlled_vocab_entry_id = e.controlled_vocab_entry_id AND l.setting_name = ? AND l.locale = ?)
204 LEFT JOIN controlled_vocab_entry_settings p ON (p.controlled_vocab_entry_id = e.controlled_vocab_entry_id AND p.setting_name = ? AND p.locale = ?)
205 LEFT JOIN controlled_vocab_entry_settings n ON (n.controlled_vocab_entry_id = e.controlled_vocab_entry_id AND n.setting_name = ? AND n.locale = ?)
206 WHERE e.controlled_vocab_id = ?
207 ORDER BY e.seq',
208 array(
209 $settingName, Locale::getLocale(), // Current locale
210 $settingName, Locale::getPrimaryLocale(), // Primary locale
211 $settingName, '', // No locale
212 (int) $controlledVocabId
216 $returner = array();
217 while (!$result->EOF) {
218 $row = $result->GetRowAssoc(false);
219 $returner[$row['controlled_vocab_entry_id']] = $this->convertFromDB(
220 $row['setting_value'],
221 $row['setting_type']
223 $result->MoveNext();
225 $result->Close();
227 return $returner;
231 * Get the ID of the last inserted controlled vocab.
232 * @return int
234 function getInsertId() {
235 return parent::getInsertId('controlled_vocabs', 'controlled_vocab_id');