Open Journal Systems  2.4.4
 All Classes 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  $entryDao =& DAORegistry::getDAO('ControlledVocabEntryDAO');
33  return $entryDao;
34  }
35 
41  function getById($controlledVocabId) {
42  $result =& $this->retrieve(
43  'SELECT * FROM controlled_vocabs WHERE controlled_vocab_id = ?', array((int) $controlledVocabId)
44  );
45 
46  $returner = null;
47  if ($result->RecordCount() != 0) {
48  $returner =& $this->_fromRow($result->GetRowAssoc(false));
49  }
50  $result->Close();
51  return $returner;
52  }
53 
61  function build($symbolic, $assocType = 0, $assocId = 0) {
62  // If one exists, fetch and return.
63  $controlledVocab = $this->getBySymbolic($symbolic, $assocType, $assocId);
64  if ($controlledVocab) return $controlledVocab;
65 
66  // Otherwise, build one.
67  unset($controlledVocab);
68  $controlledVocab = $this->newDataObject();
69  $controlledVocab->setSymbolic($symbolic);
70  $controlledVocab->setAssocType($assocType);
71  $controlledVocab->setAssocId($assocId);
72  $this->insertObject($controlledVocab);
73  return $controlledVocab;
74  }
75 
80  function newDataObject() {
81  return new ControlledVocab();
82  }
83 
89  function _fromRow(&$row) {
90  $controlledVocab = $this->newDataObject();
91  $controlledVocab->setId($row['controlled_vocab_id']);
92  $controlledVocab->setAssocType($row['assoc_type']);
93  $controlledVocab->setAssocId($row['assoc_id']);
94  $controlledVocab->setSymbolic($row['symbolic']);
95 
96  return $controlledVocab;
97  }
98 
104  function insertObject(&$controlledVocab) {
105  $this->update(
106  sprintf('INSERT INTO controlled_vocabs
107  (symbolic, assoc_type, assoc_id)
108  VALUES
109  (?, ?, ?)'),
110  array(
111  $controlledVocab->getSymbolic(),
112  (int) $controlledVocab->getAssocType(),
113  (int) $controlledVocab->getAssocId()
114  )
115  );
116  $controlledVocab->setId($this->getInsertId());
117  return $controlledVocab->getId();
118  }
119 
125  function updateObject(&$controlledVocab) {
126  $returner = $this->update(
127  sprintf('UPDATE controlled_vocabs
128  SET symbolic = ?,
129  assoc_type = ?,
130  assoc_id = ?
131  WHERE controlled_vocab_id = ?'),
132  array(
133  $controlledVocab->getSymbolic(),
134  (int) $controlledVocab->getAssocType(),
135  (int) $controlledVocab->getAssocId(),
136  (int) $controlledVocab->getId()
137  )
138  );
139  return $returner;
140  }
141 
147  function deleteObject($controlledVocab) {
148  return $this->deleteObjectById($controlledVocab->getId());
149  }
150 
156  function deleteObjectById($controlledVocabId) {
157  $params = array((int) $controlledVocabId);
158  $controlledVocabEntryDao =& DAORegistry::getDAO('ControlledVocabEntryDAO');
159  $controlledVocabEntries =& $this->enumerate($controlledVocabId);
160  foreach ($controlledVocabEntries as $controlledVocabEntryId => $controlledVocabEntryName) {
161  $controlledVocabEntryDao->deleteObjectById($controlledVocabEntryId);
162  }
163  return $this->update('DELETE FROM controlled_vocabs WHERE controlled_vocab_id = ?', $params);
164  }
165 
173  function getBySymbolic($symbolic, $assocType, $assocId) {
174  $result =& $this->retrieve(
175  'SELECT * FROM controlled_vocabs WHERE symbolic = ? AND assoc_type = ? AND assoc_id = ?',
176  array($symbolic, (int) $assocType, (int) $assocId)
177  );
178 
179  $returner = null;
180  if ($result->RecordCount() != 0) {
181  $returner = $this->_fromRow($result->GetRowAssoc(false));
182  }
183  $result->Close();
184  return $returner;
185  }
186 
195  function enumerateBySymbolic($symbolic, $assocType, $assocId, $settingName = 'name') {
196  $controlledVocab = $this->getBySymbolic($symbolic, $assocType, $assocId);
197  if (!$controlledVocab) {
198  $returner = array();
199  return $returner;
200  }
201  return $controlledVocab->enumerate($settingName);
202  }
203 
210  function enumerate($controlledVocabId, $settingName = 'name') {
211  $result =& $this->retrieve(
212  'SELECT e.controlled_vocab_entry_id,
213  COALESCE(l.setting_value, p.setting_value, n.setting_value) AS setting_value,
214  COALESCE(l.setting_type, p.setting_type, n.setting_type) AS setting_type
215  FROM controlled_vocab_entries e
216  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 = ?)
217  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 = ?)
218  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 = ?)
219  WHERE e.controlled_vocab_id = ?
220  ORDER BY e.seq',
221  array(
222  $settingName, AppLocale::getLocale(), // Current locale
223  $settingName, AppLocale::getPrimaryLocale(), // Primary locale
224  $settingName, '', // No locale
225  (int) $controlledVocabId
226  )
227  );
228 
229  $returner = array();
230  while (!$result->EOF) {
231  $row = $result->GetRowAssoc(false);
232  $returner[$row['controlled_vocab_entry_id']] = $this->convertFromDB(
233  $row['setting_value'],
234  $row['setting_type']
235  );
236  $result->MoveNext();
237  }
238  $result->Close();
239 
240  return $returner;
241  }
242 
247  function getInsertId() {
248  return parent::getInsertId('controlled_vocabs', 'controlled_vocab_id');
249  }
250 }
251 
252 ?>
enumerate($controlledVocabId, $settingName= 'name')
getById($controlledVocabId)
Basic class describing an controlled vocab.
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
updateObject(&$controlledVocab)
insertObject(&$controlledVocab)
Operations for retrieving and modifying ControlledVocab objects.
build($symbolic, $assocType=0, $assocId=0)
enumerateBySymbolic($symbolic, $assocType, $assocId, $settingName= 'name')
convertFromDB($value, $type)
Definition: DAO.inc.php:338
& getDAO($name, $dbconn=null)
deleteObject($controlledVocab)
deleteObjectById($controlledVocabId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
getBySymbolic($symbolic, $assocType, $assocId)