Open Monograph Press  3.3.0
ContextDAO.inc.php
1 <?php
2 
16 import('lib.pkp.classes.db.SchemaDAO');
17 
18 abstract class ContextDAO extends SchemaDAO {
24  function getNames($enabledOnly = false) {
25  $contexts = array();
26  $iterator = $this->getAll($enabledOnly);
27  while ($context = $iterator->next()) {
28  $contexts[$context->getId()] = $context->getLocalizedName();
29  }
30  return $contexts;
31  }
32 
37  function getLocaleFieldNames() {
38  return array('name', 'description');
39  }
40 
46  function existsByPath($path) {
47  $result = $this->retrieve(
48  'SELECT COUNT(*) FROM ' . $this->tableName . ' WHERE path = ?',
49  (string) $path
50  );
51  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
52  $result->Close();
53  return $returner;
54  }
55 
61  function getByPath($path) {
62  $result = $this->retrieve(
63  'SELECT * FROM ' . $this->tableName . ' WHERE path = ?',
64  (string) $path
65  );
66  if ($result->RecordCount() == 0) return null;
67 
68  $returner = $this->_fromRow($result->GetRowAssoc(false));
69  $result->Close();
70  return $returner;
71  }
72 
79  function getAll($enabledOnly = false, $rangeInfo = null) {
80  $result = $this->retrieveRange(
81  'SELECT * FROM ' . $this->tableName .
82  ($enabledOnly?' WHERE enabled = 1':'') .
83  ' ORDER BY seq',
84  false,
85  $rangeInfo
86  );
87 
88  return new DAOResultFactory($result, $this, '_fromRow');
89  }
90 
100  function getAvailable($userId = null, $rangeInfo = null) {
101  $params = array();
102  if ($userId) $params = array_merge(
103  $params,
104  array((int) $userId, (int) $userId, (int) ROLE_ID_SITE_ADMIN)
105  );
106 
107  $result = $this->retrieveRange(
108  'SELECT c.* FROM ' . $this->tableName . ' c
109  WHERE ' .
110  ($userId?
111  'c.' . $this->primaryKeyColumn . ' 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 = ?)
112  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 = ?)'
113  :'c.enabled = 1') .
114  ' ORDER BY seq',
115  $params,
116  $rangeInfo
117  );
118 
119  return new DAOResultFactory($result, $this, '_fromRow');
120  }
121 
129  function getBySetting($settingName, $settingValue, $contextId = null) {
130  $params = array($settingName, $settingValue);
131  if ($contextId) $params[] = $contextId;
132 
133  $result = $this->retrieve(
134  'SELECT * FROM ' . $this->tableName . ' AS c
135  LEFT JOIN ' . $this->settingsTableName . ' AS cs
136  ON c.' . $this->primaryKeyColumn . ' = cs.' . $this->primaryKeyColumn .
137  ' WHERE cs.setting_name = ? AND cs.setting_value = ?' .
138  ($contextId?' AND c.' . $this->primaryKeyColumn . ' = ?':''),
139  $params
140  );
141 
142  return new DAOResultFactory($result, $this, '_fromRow');
143  }
144 
148  function resequence() {
149  $result = $this->retrieve(
150  'SELECT ' . $this->primaryKeyColumn . ' FROM ' . $this->tableName . ' ORDER BY seq'
151  );
152 
153  for ($i=1; !$result->EOF; $i+=2) {
154  list($contextId) = $result->fields;
155  $this->update(
156  'UPDATE ' . $this->tableName . ' SET seq = ? WHERE ' . $this->primaryKeyColumn . ' = ?',
157  array(
158  $i,
159  $contextId
160  )
161  );
162 
163  $result->MoveNext();
164  }
165  $result->Close();
166  }
167 }
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
DAO\retrieveRange
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
ContextDAO\existsByPath
existsByPath($path)
Definition: ContextDAO.inc.php:46
ContextDAO\getAvailable
getAvailable($userId=null, $rangeInfo=null)
Definition: ContextDAO.inc.php:100
ContextDAO\resequence
resequence()
Definition: ContextDAO.inc.php:148
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
ContextDAO\getByPath
getByPath($path)
Definition: ContextDAO.inc.php:61
ContextDAO\getAll
getAll($enabledOnly=false, $rangeInfo=null)
Definition: ContextDAO.inc.php:79
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
SchemaDAO\_fromRow
_fromRow($primaryRow)
Definition: SchemaDAO.inc.php:240
ContextDAO\getBySetting
getBySetting($settingName, $settingValue, $contextId=null)
Definition: ContextDAO.inc.php:129
ContextDAO
Operations for retrieving and modifying context objects.
Definition: ContextDAO.inc.php:18
ContextDAO\getNames
getNames($enabledOnly=false)
Definition: ContextDAO.inc.php:24
ContextDAO\getLocaleFieldNames
getLocaleFieldNames()
Definition: ContextDAO.inc.php:37
SchemaDAO
A base class for DAOs which rely on a json-schema file to define the data object.
Definition: SchemaDAO.inc.php:18