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  // Attempt to build a new controlled vocabulary.
63  $controlledVocab = $this->newDataObject();
64  $controlledVocab->setSymbolic($symbolic);
65  $controlledVocab->setAssocType($assocType);
66  $controlledVocab->setAssocId($assocId);
67  $id = $this->insertObject($controlledVocab, false);
68  if ($id !== null) return $controlledVocab;
69 
70  // Presume that an error was a duplicate insert.
71  // In this case, try to fetch an existing controlled
72  // vocabulary.
73  return $this->getBySymbolic($symbolic, $assocType, $assocId);
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, $dieOnError = true) {
105  $success = $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  true, // callHooks
116  $dieOnError
117  );
118  if ($success) {
119  $controlledVocab->setId($this->getInsertId());
120  return $controlledVocab->getId();
121  }
122  else return null; // An error occurred on insert
123  }
124 
130  function updateObject(&$controlledVocab) {
131  $returner = $this->update(
132  sprintf('UPDATE controlled_vocabs
133  SET symbolic = ?,
134  assoc_type = ?,
135  assoc_id = ?
136  WHERE controlled_vocab_id = ?'),
137  array(
138  $controlledVocab->getSymbolic(),
139  (int) $controlledVocab->getAssocType(),
140  (int) $controlledVocab->getAssocId(),
141  (int) $controlledVocab->getId()
142  )
143  );
144  return $returner;
145  }
146 
152  function deleteObject($controlledVocab) {
153  return $this->deleteObjectById($controlledVocab->getId());
154  }
155 
161  function deleteObjectById($controlledVocabId) {
162  $params = array((int) $controlledVocabId);
163  $controlledVocabEntryDao = DAORegistry::getDAO('ControlledVocabEntryDAO');
164  $controlledVocabEntries =& $this->enumerate($controlledVocabId);
165  foreach ($controlledVocabEntries as $controlledVocabEntryId => $controlledVocabEntryName) {
166  $controlledVocabEntryDao->deleteObjectById($controlledVocabEntryId);
167  }
168  return $this->update('DELETE FROM controlled_vocabs WHERE controlled_vocab_id = ?', $params);
169  }
170 
178  function getBySymbolic($symbolic, $assocType, $assocId) {
179  $result = $this->retrieve(
180  'SELECT * FROM controlled_vocabs WHERE symbolic = ? AND assoc_type = ? AND assoc_id = ?',
181  array($symbolic, (int) $assocType, (int) $assocId)
182  );
183 
184  $returner = null;
185  if ($result->RecordCount() != 0) {
186  $returner = $this->_fromRow($result->GetRowAssoc(false));
187  }
188  $result->Close();
189  return $returner;
190  }
191 
200  function enumerateBySymbolic($symbolic, $assocType, $assocId, $settingName = 'name') {
201  $controlledVocab = $this->getBySymbolic($symbolic, $assocType, $assocId);
202  if (!$controlledVocab) {
203  $returner = array();
204  return $returner;
205  }
206  return $controlledVocab->enumerate($settingName);
207  }
208 
215  function enumerate($controlledVocabId, $settingName = 'name') {
216  $result = $this->retrieve(
217  'SELECT e.controlled_vocab_entry_id,
218  COALESCE(l.setting_value, p.setting_value, n.setting_value) AS setting_value,
219  COALESCE(l.setting_type, p.setting_type, n.setting_type) AS setting_type
220  FROM controlled_vocab_entries e
221  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 = ?)
222  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 = ?)
223  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 = ?)
224  WHERE e.controlled_vocab_id = ?
225  ORDER BY e.seq',
226  array(
227  $settingName, AppLocale::getLocale(), // Current locale
228  $settingName, AppLocale::getPrimaryLocale(), // Primary locale
229  $settingName, '', // No locale
230  (int) $controlledVocabId
231  )
232  );
233 
234  $returner = array();
235  while (!$result->EOF) {
236  $row = $result->GetRowAssoc(false);
237  $returner[$row['controlled_vocab_entry_id']] = $this->convertFromDB(
238  $row['setting_value'],
239  $row['setting_type']
240  );
241  $result->MoveNext();
242  }
243  $result->Close();
244  return $returner;
245  }
246 
251  function getInsertId() {
252  return parent::_getInsertId('controlled_vocabs', 'controlled_vocab_id');
253  }
254 }
255 
256 ?>
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
deleteObject($controlledVocab)
deleteObjectById($controlledVocabId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
getBySymbolic($symbolic, $assocType, $assocId)