Open Monograph Press  3.3.0
ControlledVocabDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.controlledVocab.ControlledVocab');
18 
19 class ControlledVocabDAO extends DAO {
20 
25  function getEntryDAO() {
26  return DAORegistry::getDAO('ControlledVocabEntryDAO');
27  }
28 
34  function getById($controlledVocabId) {
35  $result = $this->retrieve(
36  'SELECT * FROM controlled_vocabs WHERE controlled_vocab_id = ?', array((int) $controlledVocabId)
37  );
38 
39  $returner = null;
40  if ($result->RecordCount() != 0) {
41  $returner = $this->_fromRow($result->GetRowAssoc(false));
42  }
43  $result->Close();
44  return $returner;
45  }
46 
54  function _build($symbolic, $assocType = 0, $assocId = 0) {
55  // Attempt to fetch an existing controlled vocabulary.
56  $controlledVocab = $this->getBySymbolic($symbolic, $assocType, $assocId);
57  if ($controlledVocab) return $controlledVocab;
58 
59  // Attempt to build a new controlled vocabulary.
60  $controlledVocab = $this->newDataObject();
61  $controlledVocab->setSymbolic($symbolic);
62  $controlledVocab->setAssocType($assocType);
63  $controlledVocab->setAssocId($assocId);
64  $id = $this->insertObject($controlledVocab, false);
65  if ($id !== null) return $controlledVocab;
66 
67  // Presume that an error was a duplicate insert.
68  // In this case, try to fetch an existing controlled
69  // vocabulary.
70  return $this->getBySymbolic($symbolic, $assocType, $assocId);
71  }
72 
77  function newDataObject() {
78  return new ControlledVocab();
79  }
80 
86  function _fromRow($row) {
87  $controlledVocab = $this->newDataObject();
88  $controlledVocab->setId($row['controlled_vocab_id']);
89  $controlledVocab->setAssocType($row['assoc_type']);
90  $controlledVocab->setAssocId($row['assoc_id']);
91  $controlledVocab->setSymbolic($row['symbolic']);
92 
93  return $controlledVocab;
94  }
95 
101  function insertObject($controlledVocab, $dieOnError = true) {
102  $success = $this->update(
103  sprintf('INSERT INTO controlled_vocabs
104  (symbolic, assoc_type, assoc_id)
105  VALUES
106  (?, ?, ?)'),
107  array(
108  $controlledVocab->getSymbolic(),
109  (int) $controlledVocab->getAssocType(),
110  (int) $controlledVocab->getAssocId()
111  ),
112  true, // callHooks
113  $dieOnError
114  );
115  if ($success) {
116  $controlledVocab->setId($this->getInsertId());
117  return $controlledVocab->getId();
118  }
119  else return null; // An error occurred on insert
120  }
121 
127  function updateObject(&$controlledVocab) {
128  $returner = $this->update(
129  sprintf('UPDATE controlled_vocabs
130  SET symbolic = ?,
131  assoc_type = ?,
132  assoc_id = ?
133  WHERE controlled_vocab_id = ?'),
134  array(
135  $controlledVocab->getSymbolic(),
136  (int) $controlledVocab->getAssocType(),
137  (int) $controlledVocab->getAssocId(),
138  (int) $controlledVocab->getId()
139  )
140  );
141  return $returner;
142  }
143 
149  function deleteObject($controlledVocab) {
150  return $this->deleteObjectById($controlledVocab->getId());
151  }
152 
158  function deleteObjectById($controlledVocabId) {
159  $params = array((int) $controlledVocabId);
160  $controlledVocabEntryDao = DAORegistry::getDAO('ControlledVocabEntryDAO'); /* @var $controlledVocabEntryDao ControlledVocabEntryDAO */
161  $controlledVocabEntries = $this->enumerate($controlledVocabId);
162  foreach ($controlledVocabEntries as $controlledVocabEntryId => $controlledVocabEntryName) {
163  $controlledVocabEntryDao->deleteObjectById($controlledVocabEntryId);
164  }
165  return $this->update('DELETE FROM controlled_vocabs WHERE controlled_vocab_id = ?', $params);
166  }
167 
175  function getBySymbolic($symbolic, $assocType = 0, $assocId = 0) {
176  $result = $this->retrieve(
177  'SELECT * FROM controlled_vocabs WHERE symbolic = ? AND assoc_type = ? AND assoc_id = ?',
178  array($symbolic, (int) $assocType, (int) $assocId)
179  );
180 
181  $returner = null;
182  if ($result->RecordCount() != 0) {
183  $returner = $this->_fromRow($result->GetRowAssoc(false));
184  }
185  $result->Close();
186  return $returner;
187  }
188 
197  function enumerateBySymbolic($symbolic, $assocType, $assocId, $settingName = 'name') {
198  $controlledVocab = $this->getBySymbolic($symbolic, $assocType, $assocId);
199  if (!$controlledVocab) {
200  $returner = array();
201  return $returner;
202  }
203  return $controlledVocab->enumerate($settingName);
204  }
205 
212  function enumerate($controlledVocabId, $settingName = 'name') {
213  $result = $this->retrieve(
214  'SELECT e.controlled_vocab_entry_id,
215  COALESCE(l.setting_value, p.setting_value, n.setting_value) AS setting_value,
216  COALESCE(l.setting_type, p.setting_type, n.setting_type) AS setting_type
217  FROM controlled_vocab_entries e
218  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 = ?)
219  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 = ?)
220  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 = ?)
221  WHERE e.controlled_vocab_id = ?
222  ORDER BY e.seq',
223  array(
224  $settingName, AppLocale::getLocale(), // Current locale
225  $settingName, AppLocale::getPrimaryLocale(), // Primary locale
226  $settingName, '', // No locale
227  (int) $controlledVocabId
228  )
229  );
230 
231  $returner = array();
232  while (!$result->EOF) {
233  $row = $result->GetRowAssoc(false);
234  $returner[$row['controlled_vocab_entry_id']] = $this->convertFromDB(
235  $row['setting_value'],
236  $row['setting_type']
237  );
238  $result->MoveNext();
239  }
240  $result->Close();
241  return $returner;
242  }
243 
248  function getInsertId() {
249  return parent::_getInsertId('controlled_vocabs', 'controlled_vocab_id');
250  }
251 
257  function installXML($filename) {
258  $controlledVocabs = array();
259  $controlledVocabEntryDao = $this->getEntryDAO();
260  $controlledVocabEntrySettingsDao = $controlledVocabEntryDao->getSettingsDAO();
261  $parser = new XMLParser();
262  $tree = $parser->parse($filename);
263  foreach ($tree->getChildren() as $controlledVocabNode) {
264  assert($controlledVocabNode->getName() == 'controlled_vocab');
265 
266  // Try to fetch an existing controlled vocabulary
267  $controlledVocab = $this->getBySymbolic(
268  $symbolic = $controlledVocabNode->getAttribute('symbolic'),
269  $assocType = (int) $controlledVocabNode->getAttribute('assoc-type'),
270  $assocId = (int) $controlledVocabNode->getAttribute('assoc-id')
271  );
272  if ($controlledVocab) {
273  $controlledVocabs[] = $controlledVocab;
274  continue;
275  }
276 
277  // It doesn't exist; create a new one.
278  $controlledVocabs[] = $controlledVocab = $this->_build($symbolic, $assocType, $assocId);
279  foreach ($controlledVocabNode->getChildren() as $entryNode) {
280  $seq = $entryNode->getAttribute('seq');
281  if ($seq !== null) $seq = (float) $seq;
282 
283  $controlledVocabEntry = $controlledVocabEntryDao->newDataObject();
284  $controlledVocabEntry->setControlledVocabId($controlledVocab->getId());
285  $controlledVocabEntry->setSequence($seq);
286  $controlledVocabEntryDao->insertObject($controlledVocabEntry);
287 
288  foreach ($entryNode->getChildren() as $settingNode) {
289  $controlledVocabEntrySettingsDao->updateSetting(
290  $controlledVocabEntry->getId(),
291  $settingNode->getAttribute('name'),
292  $settingNode->getValue(),
293  $settingNode->getAttribute('type'),
294  false // Not localized
295  );
296  }
297  }
298  }
299  return $controlledVocabs;
300  }
301 }
302 
303 
ControlledVocabDAO\getEntryDAO
getEntryDAO()
Definition: ControlledVocabDAO.inc.php:25
ControlledVocabDAO\getInsertId
getInsertId()
Definition: ControlledVocabDAO.inc.php:248
ControlledVocabDAO\newDataObject
newDataObject()
Definition: ControlledVocabDAO.inc.php:77
ControlledVocabDAO\_fromRow
_fromRow($row)
Definition: ControlledVocabDAO.inc.php:86
ControlledVocabDAO\enumerate
enumerate($controlledVocabId, $settingName='name')
Definition: ControlledVocabDAO.inc.php:212
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
ControlledVocab
Basic class describing an controlled vocab.
Definition: ControlledVocab.inc.php:20
ControlledVocabDAO\getById
getById($controlledVocabId)
Definition: ControlledVocabDAO.inc.php:34
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
DAO\convertFromDB
convertFromDB($value, $type)
Definition: DAO.inc.php:341
AppLocale\getPrimaryLocale
static getPrimaryLocale()
Definition: env1/MockAppLocale.inc.php:95
ControlledVocabDAO\getBySymbolic
getBySymbolic($symbolic, $assocType=0, $assocId=0)
Definition: ControlledVocabDAO.inc.php:175
ControlledVocabDAO\deleteObject
deleteObject($controlledVocab)
Definition: ControlledVocabDAO.inc.php:149
ControlledVocabDAO\installXML
installXML($filename)
Definition: ControlledVocabDAO.inc.php:257
ControlledVocabDAO\enumerateBySymbolic
enumerateBySymbolic($symbolic, $assocType, $assocId, $settingName='name')
Definition: ControlledVocabDAO.inc.php:197
ControlledVocabDAO
Operations for retrieving and modifying ControlledVocab objects.
Definition: ControlledVocabDAO.inc.php:19
ControlledVocabDAO\_build
_build($symbolic, $assocType=0, $assocId=0)
Definition: ControlledVocabDAO.inc.php:54
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
XMLParser
Generic class for parsing an XML document into a data structure.
Definition: XMLParser.inc.php:28
ControlledVocabDAO\updateObject
updateObject(&$controlledVocab)
Definition: ControlledVocabDAO.inc.php:127
ControlledVocabDAO\insertObject
insertObject($controlledVocab, $dieOnError=true)
Definition: ControlledVocabDAO.inc.php:101
ControlledVocabDAO\deleteObjectById
deleteObjectById($controlledVocabId)
Definition: ControlledVocabDAO.inc.php:158
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31