Open Journal Systems  3.0.0
 All Data Structures Namespaces Functions Variables Groups Pages
ContextDAO.inc.php
1 <?php
2 
17 abstract class ContextDAO extends DAO {
21  function ContextDAO() {
22  parent::DAO();
23  }
24 
30  function getById($contextId) {
31  $result = $this->retrieve(
32  'SELECT * FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
33  (int) $contextId
34  );
35 
36  $returner = null;
37  if ($result->RecordCount() != 0) {
38  $returner = $this->_fromRow($result->GetRowAssoc(false));
39  }
40  $result->Close();
41  return $returner;
42  }
43 
49  function getNames($enabledOnly = false) {
50  $contexts = array();
51  $iterator = $this->getAll($enabledOnly);
52  while ($context = $iterator->next()) {
53  $contexts[$context->getId()] = $context->getLocalizedName();
54  }
55  return $contexts;
56  }
57 
62  function getLocaleFieldNames() {
63  return array('name', 'description');
64  }
65 
71  function _fromRow($row) {
72  $context = $this->newDataObject();
73  $context->setId($row[$this->_getPrimaryKeyColumn()]);
74  $context->setPath($row['path']);
75  $context->setSequence($row['seq']);
76  $this->getDataObjectSettings($this->_getSettingsTableName(), $this->_getPrimaryKeyColumn(), $row[$this->_getPrimaryKeyColumn()], $context);
77  return $context;
78  }
79 
85  function insertObject($context) {
86  $this->update(
87  'INSERT INTO ' . $this->_getTableName() . '
88  (path, seq, enabled, primary_locale)
89  VALUES
90  (?, ?, ?, ?)',
91  array(
92  $context->getPath(),
93  (int) $context->getSequence(),
94  (int) $context->getEnabled(),
95  $context->getPrimaryLocale()
96  )
97  );
98 
99  $context->setId($this->getInsertId());
100  return $context->getId();
101  }
102 
107  function updateObject($context) {
108  return $this->update(
109  'UPDATE ' . $this->_getTableName() . '
110  SET
111  path = ?,
112  seq = ?,
113  enabled = ?,
114  primary_locale = ?
115  WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
116  array(
117  $context->getPath(),
118  (int) $context->getSequence(),
119  (int) $context->getEnabled(),
120  $context->getPrimaryLocale(),
121  (int) $context->getId()
122  )
123  );
124  }
125 
131  function existsByPath($path) {
132  $result = $this->retrieve(
133  'SELECT COUNT(*) FROM ' . $this->_getTableName() . ' WHERE path = ?',
134  (string) $path
135  );
136  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
137  $result->Close();
138  return $returner;
139  }
140 
146  function getByPath($path) {
147  $result = $this->retrieve(
148  'SELECT * FROM ' . $this->_getTableName() . ' WHERE path = ?',
149  (string) $path
150  );
151  if ($result->RecordCount() == 0) return null;
152 
153  $returner = $this->_fromRow($result->GetRowAssoc(false));
154  $result->Close();
155  return $returner;
156  }
157 
164  function getAll($enabledOnly = false, $rangeInfo = null) {
165  $result = $this->retrieveRange(
166  'SELECT * FROM ' . $this->_getTableName() .
167  ($enabledOnly?' WHERE enabled = 1':'') .
168  ' ORDER BY seq',
169  false,
170  $rangeInfo
171  );
172 
173  return new DAOResultFactory($result, $this, '_fromRow');
174  }
175 
182  function getAvailable($userId = null, $rangeInfo = null) {
183  $params = array();
184  if ($userId) $params = array_merge(
185  $params,
186  array((int) $userId, (int) $userId, (int) ROLE_ID_SITE_ADMIN)
187  );
188 
189  $result = $this->retrieveRange(
190  'SELECT c.* FROM ' . $this->_getTableName() . ' c
191  WHERE c.enabled = 1 ' .
192  ($userId?
193  'OR c.' . $this->_getPrimaryKeyColumn() . ' IN (SELECT DISTINCT ug.context_id FROM user_groups ug JOIN user_user_groups uug ON (ug.user_group_id = uug.user_group_id) WHERE uug.user_id = ?)
194  OR ? IN (SELECT user_id FROM user_groups ug JOIN user_user_groups uug ON (ug.user_group_id = uug.user_group_id) WHERE ug.role_id = ?) '
195  :'') .
196  'ORDER BY seq',
197  $params,
198  $rangeInfo
199  );
200 
201  return new DAOResultFactory($result, $this, '_fromRow');
202  }
203 
211  function getBySetting($settingName, $settingValue, $contextId = null) {
212  $params = array($settingName, $settingValue);
213  if ($contextId) $params[] = $contextId;
214 
215  $result = $this->retrieve(
216  'SELECT * FROM ' . $this->_getTableName() . ' AS c
217  LEFT JOIN ' . $this->_getSettingsTableName() . ' AS cs
218  ON c.' . $this->_getPrimaryKeyColumn() . ' = cs.' . $this->_getPrimaryKeyColumn() .
219  ' WHERE cs.setting_name = ? AND cs.setting_value = ?' .
220  ($contextId?' AND c.' . $this->_getPrimaryKeyColumn() . ' = ?':''),
221  $params
222  );
223 
224  return new DAOResultFactory($result, $this, '_fromRow');
225  }
226 
227 
232  function getInsertId() {
233  return $this->_getInsertId($this->_getTableName(), $this->_getPrimaryKeyColumn());
234  }
235 
240  function deleteObject($context) {
241  $this->deleteById($context->getId());
242  }
243 
248  function deleteById($contextId) {
249  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
250  $userGroupDao->deleteAssignmentsByContextId($contextId);
251  $userGroupDao->deleteByContextId($contextId);
252 
253  $genreDao = DAORegistry::getDAO('GenreDAO');
254  $genreDao->deleteByContextId($contextId);
255 
256  $this->update(
257  'DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
258  (int) $contextId
259  );
260  }
261 
265  function resequence() {
266  $result = $this->retrieve(
267  'SELECT ' . $this->_getPrimaryKeyColumn() . ' FROM ' . $this->_getTableName() . ' ORDER BY seq'
268  );
269 
270  for ($i=1; !$result->EOF; $i+=2) {
271  list($contextId) = $result->fields;
272  $this->update(
273  'UPDATE ' . $this->_getTableName() . ' SET seq = ? WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
274  array(
275  $i,
276  $contextId
277  )
278  );
279 
280  $result->MoveNext();
281  }
282  $result->Close();
283  }
284 
285  //
286  // Protected methods
287  //
292  abstract protected function _getTableName();
293 
298  abstract protected function _getSettingsTableName();
299 
304  abstract protected function _getPrimaryKeyColumn();
305 }
306 
307 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
deleteObject($context)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getById($contextId)
updateObject($context)
Operations for retrieving and modifying context objects.
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
getBySetting($settingName, $settingValue, $contextId=null)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
_getPrimaryKeyColumn()
getNames($enabledOnly=false)
insertObject($context)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getAll($enabledOnly=false, $rangeInfo=null)
existsByPath($path)
deleteById($contextId)
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject)
Definition: DAO.inc.php:568
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
getByPath($path)
getAvailable($userId=null, $rangeInfo=null)
_getSettingsTableName()