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