Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
DefaultSettingDAO.inc.php
1 <?php
14 define('DEFAULT_SETTING_GENRES', 1);
15 define('DEFAULT_SETTING_PUBLICATION_FORMATS', 2);
16 
17 class DefaultSettingDAO extends DAO {
21  function DefaultSettingDAO() {
22  parent::DAO();
23  }
24 
30  function installDefaultBase($contextId) {
31  return null;
32  }
33 
39  return null;
40  }
41 
47  // Must be implemented by sub-classes.
48  assert(false);
49  }
50 
55  function getDefaultKey() {
56  return 'entry_key';
57  }
58 
66  function getSettingAttributes($node = null) {
67  return array();
68  }
69 
74  function getSettingsTableName() {
75  return null;
76  }
77 
82  function getTableName() {
83  return null;
84  }
85 
90  function getDefaultType() {
91  return null;
92  }
93 
102  function installDefaultBaseData($locale, $contextId, $skipLoad = true, $localeInstall = false) {
103  $xmlDao = new XMLDAO();
104  $data = $xmlDao->parse($this->getDefaultBaseFilename());
105  if (!$data) return false;
106  $defaultIds = $this->getDefaultSettingIds($contextId);
107  AppLocale::requireComponents(LOCALE_COMPONENT_APP_DEFAULT, LOCALE_COMPONENT_PKP_DEFAULT, $locale);
108 
109  foreach ($data->getChildren() as $formatNode) {
110 
111  $settings =& $this->getSettingAttributes($formatNode, $locale);
112 
113  if (empty($defaultIds[$formatNode->getAttribute('key')])) { // ignore keys not associated with this context
114  continue;
115  } else { // prepare a list of attributes not defined in the current settings xml file
116  unset($defaultIds[$formatNode->getAttribute('key')]);
117  }
118 
119  foreach ($settings as $settingName => $settingValue) {
120 
121  $this->update(
122  'INSERT INTO context_defaults
123  (context_id, assoc_type, entry_key, locale, setting_name, setting_value, setting_type)
124  VALUES
125  (?, ?, ?, ?, ?, ?, ?)',
126  array(
127  $contextId,
128  $this->getDefaultType(),
129  $formatNode->getAttribute('key'),
130  $locale,
131  $settingName,
132  $settingValue,
133  'string'
134  )
135  );
136  }
137  }
138 
139  $attributeNames =& $this->getSettingAttributes();
140 
141  // install defaults for keys not defined in the xml
142  foreach ($defaultIds as $key => $id) {
143  foreach ($attributeNames as $setting) {
144  $this->update(
145  'INSERT INTO context_defaults
146  (context_id, assoc_type, entry_key, locale, setting_name, setting_value, setting_type)
147  VALUES
148  (?, ?, ?, ?, ?, ?, ?)',
149  array(
150  $contextId,
151  $this->getDefaultType(),
152  $key,
153  $locale,
154  $setting,
155  '##',
156  'string'
157  )
158  );
159  }
160  }
161 
162  if ($skipLoad) {
163  return true;
164  }
165 
166  if ($localeInstall) {
167  $this->restoreByContextId($contextId, $locale);
168  } else {
169  $this->restoreByContextId($contextId);
170  }
171 
172  return true;
173  }
174 
179  function &getDefaultSettingIds($contextId) {
180  $result = $this->retrieve(
181  'SELECT '. $this->getPrimaryKeyColumnName() .', '. $this->getDefaultKey() .' FROM '. $this->getTableName() .'
182  WHERE context_id = ? AND '. $this->getDefaultKey() .' IS NOT NULL', $contextId
183  );
184 
185  $returner = null;
186  while (!$result->EOF) {
187  $returner[$result->fields[$this->getDefaultKey()]] =& $result->fields[$this->getPrimaryKeyColumnName()];
188  $result->MoveNext();
189  }
190  $result->Close();
191  return $returner;
192  }
193 
199  function restoreByContextId($contextId, $locale = null) {
200 
201  $defaultIds = $this->getDefaultSettingIds($contextId);
202 
203  if ($locale) {
204  foreach ($defaultIds as $key => $id) {
205  $this->update('DELETE FROM '. $this->getSettingsTableName() .' WHERE '. $this->getPrimaryKeyColumnName() .' = ? AND locale = ?', array($id, $locale));
206  }
207  } else {
208  foreach ($defaultIds as $key => $id) {
209  $this->update('DELETE FROM '. $this->getSettingsTableName() .' WHERE '. $this->getPrimaryKeyColumnName() .' = ?', $id);
210  }
211  }
212 
213  if (!$locale) {
214  $this->update('UPDATE '. $this->getTableName() .' SET enabled = ? WHERE context_id = ? AND '. $this->getDefaultKey() .' IS NOT NULL', array(1, $contextId));
215  $this->update('UPDATE '. $this->getTableName() .' SET enabled = ? WHERE context_id = ? AND '. $this->getDefaultKey() .' IS NULL', array(0, $contextId));
216  }
217 
218  $sql = 'SELECT * FROM context_defaults WHERE context_id = ? AND assoc_type = ?';
219  $sqlParams = array($contextId, $this->getDefaultType());
220  if ($locale) {
221  $sql .= ' AND locale = ?';
222  $sqlParams[] = $locale;
223  }
224 
225  $result = $this->retrieve($sql, $sqlParams);
226 
227  $returner = null;
228  while (!$result->EOF) {
229  $row = $result->GetRowAssoc(false);
230  $this->update(
231  'INSERT INTO '. $this->getSettingsTableName() .'
232  ('. $this->getPrimaryKeyColumnName() .', locale, setting_name, setting_value, setting_type)
233  VALUES
234  (?, ?, ?, ?, ?)',
235  array($defaultIds[$row['entry_key']], $row['locale'], $row['setting_name'], $row['setting_value'], $row['setting_type'])
236  );
237  $result->MoveNext();
238  }
239  $result->Close();
240  }
241 
247  function installDefaults($contextId, $locales) {
248  $this->installDefaultBase($contextId);
249  foreach ($locales as $locale) {
250  $this->installDefaultBaseData($locale, $contextId);
251  }
252  $this->restoreByContextId($contextId);
253  }
254 
259  function installLocale($locale) {
260  $contextDao = Application::getContextDAO();
261  $contexts = $contextDao->getNames();
262 
263  foreach ($contexts as $id => $name) {
264  $this->installDefaultBaseData($locale, $id, false, true);
265  }
266 
267  }
268 
273  function uninstallLocale($locale) {
274  $this->update('DELETE FROM '. $this->getSettingsTableName() .' WHERE locale = ?', array($locale));
275  $this->update('DELETE FROM context_defaults WHERE locale = ?', array($locale));
276  }
277 }
278 
279 ?>
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
static requireComponents()
installDefaults($contextId, $locales)
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:18
Operations for retrieving and modifying context default settings.
& getDefaultSettingIds($contextId)
getSettingAttributes($node=null)
installDefaultBaseData($locale, $contextId, $skipLoad=true, $localeInstall=false)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:207
restoreByContextId($contextId, $locale=null)