Open Journal Systems  3.0.0
 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  $controlledVocab = $this->newDataObject();
68  $controlledVocab->setSymbolic($symbolic);
69  $controlledVocab->setAssocType($assocType);
70  $controlledVocab->setAssocId($assocId);
71  $this->insertObject($controlledVocab);
72  return $controlledVocab;
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) {
104  $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  );
115  $controlledVocab->setId($this->getInsertId());
116  return $controlledVocab->getId();
117  }
118 
124  function updateObject(&$controlledVocab) {
125  $returner = $this->update(
126  sprintf('UPDATE controlled_vocabs
127  SET symbolic = ?,
128  assoc_type = ?,
129  assoc_id = ?
130  WHERE controlled_vocab_id = ?'),
131  array(
132  $controlledVocab->getSymbolic(),
133  (int) $controlledVocab->getAssocType(),
134  (int) $controlledVocab->getAssocId(),
135  (int) $controlledVocab->getId()
136  )
137  );
138  return $returner;
139  }
140 
146  function deleteObject($controlledVocab) {
147  return $this->deleteObjectById($controlledVocab->getId());
148  }
149 
155  function deleteObjectById($controlledVocabId) {
156  $params = array((int) $controlledVocabId);
157  $controlledVocabEntryDao = DAORegistry::getDAO('ControlledVocabEntryDAO');
158  $controlledVocabEntries =& $this->enumerate($controlledVocabId);
159  foreach ($controlledVocabEntries as $controlledVocabEntryId => $controlledVocabEntryName) {
160  $controlledVocabEntryDao->deleteObjectById($controlledVocabEntryId);
161  }
162  return $this->update('DELETE FROM controlled_vocabs WHERE controlled_vocab_id = ?', $params);
163  }
164 
172  function getBySymbolic($symbolic, $assocType, $assocId) {
173  $result = $this->retrieve(
174  'SELECT * FROM controlled_vocabs WHERE symbolic = ? AND assoc_type = ? AND assoc_id = ?',
175  array($symbolic, (int) $assocType, (int) $assocId)
176  );
177 
178  $returner = null;
179  if ($result->RecordCount() != 0) {
180  $returner = $this->_fromRow($result->GetRowAssoc(false));
181  }
182  $result->Close();
183  return $returner;
184  }
185 
194  function enumerateBySymbolic($symbolic, $assocType, $assocId, $settingName = 'name') {
195  $controlledVocab = $this->getBySymbolic($symbolic, $assocType, $assocId);
196  if (!$controlledVocab) {
197  $returner = array();
198  return $returner;
199  }
200  return $controlledVocab->enumerate($settingName);
201  }
202 
209  function enumerate($controlledVocabId, $settingName = 'name') {
210  $result = $this->retrieve(
211  'SELECT e.controlled_vocab_entry_id,
212  COALESCE(l.setting_value, p.setting_value, n.setting_value) AS setting_value,
213  COALESCE(l.setting_type, p.setting_type, n.setting_type) AS setting_type
214  FROM controlled_vocab_entries e
215  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 = ?)
216  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 = ?)
217  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 = ?)
218  WHERE e.controlled_vocab_id = ?
219  ORDER BY e.seq',
220  array(
221  $settingName, AppLocale::getLocale(), // Current locale
222  $settingName, AppLocale::getPrimaryLocale(), // Primary locale
223  $settingName, '', // No locale
224  (int) $controlledVocabId
225  )
226  );
227 
228  $returner = array();
229  while (!$result->EOF) {
230  $row = $result->GetRowAssoc(false);
231  $returner[$row['controlled_vocab_entry_id']] = $this->convertFromDB(
232  $row['setting_value'],
233  $row['setting_type']
234  );
235  $result->MoveNext();
236  }
237  $result->Close();
238  return $returner;
239  }
240 
245  function getInsertId() {
246  return parent::_getInsertId('controlled_vocabs', 'controlled_vocab_id');
247  }
248 }
249 
250 ?>
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)
insertObject(&$controlledVocab)
Operations for retrieving and modifying ControlledVocab objects.
build($symbolic, $assocType=0, $assocId=0)
static getLocale()
enumerateBySymbolic($symbolic, $assocType, $assocId, $settingName= 'name')
convertFromDB($value, $type)
Definition: DAO.inc.php:335
deleteObject($controlledVocab)
deleteObjectById($controlledVocabId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
getBySymbolic($symbolic, $assocType, $assocId)