00001 <?php
00015 define('DEFAULT_SETTING_GENRES', 1);
00016 define('DEFAULT_SETTING_PUBLICATION_FORMATS', 2);
00017
00018 class DefaultSettingDAO extends DAO {
00022 function DefaultSettingDAO() {
00023 parent::DAO();
00024 }
00025
00031 function installDefaultBase($pressId) {
00032 return null;
00033 }
00034
00039 function getDefaultBaseFilename() {
00040 return null;
00041 }
00042
00047 function getPrimaryKeyColumnName() {
00048
00049 assert(false);
00050 }
00051
00056 function getDefaultKey() {
00057 return 'entry_key';
00058 }
00059
00067 function getSettingAttributes($node = null) {
00068 return array();
00069 }
00070
00075 function getSettingsTableName() {
00076 return null;
00077 }
00078
00083 function getTableName() {
00084 return null;
00085 }
00086
00091 function getDefaultType() {
00092 return null;
00093 }
00094
00103 function installDefaultBaseData($locale, $pressId, $skipLoad = true, $localeInstall = false) {
00104 $xmlDao = new XMLDAO();
00105 $data = $xmlDao->parse($this->getDefaultBaseFilename());
00106 if (!$data) return false;
00107 $defaultIds = $this->getDefaultSettingIds($pressId);
00108 AppLocale::requireComponents(LOCALE_COMPONENT_OMP_DEFAULT_SETTINGS, $locale);
00109
00110 foreach ($data->getChildren() as $formatNode) {
00111
00112 $settings =& $this->getSettingAttributes($formatNode, $locale);
00113
00114 if (empty($defaultIds[$formatNode->getAttribute('key')])) {
00115 continue;
00116 } else {
00117 unset($defaultIds[$formatNode->getAttribute('key')]);
00118 }
00119
00120 foreach ($settings as $settingName => $settingValue) {
00121
00122 $this->update(
00123 'INSERT INTO press_defaults
00124 (press_id, assoc_type, entry_key, locale, setting_name, setting_value, setting_type)
00125 VALUES
00126 (?, ?, ?, ?, ?, ?, ?)',
00127 array(
00128 $pressId,
00129 $this->getDefaultType(),
00130 $formatNode->getAttribute('key'),
00131 $locale,
00132 $settingName,
00133 $settingValue,
00134 'string'
00135 )
00136 );
00137 }
00138 }
00139
00140 $attributeNames =& $this->getSettingAttributes();
00141
00142
00143 foreach ($defaultIds as $key => $id) {
00144 foreach ($attributeNames as $setting) {
00145 $this->update(
00146 'INSERT INTO press_defaults
00147 (press_id, assoc_type, entry_key, locale, setting_name, setting_value, setting_type)
00148 VALUES
00149 (?, ?, ?, ?, ?, ?, ?)',
00150 array(
00151 $pressId,
00152 $this->getDefaultType(),
00153 $key,
00154 $locale,
00155 $setting,
00156 '##',
00157 'string'
00158 )
00159 );
00160 }
00161 }
00162
00163 if ($skipLoad) {
00164 return true;
00165 }
00166
00167 if ($localeInstall) {
00168 $this->restoreByPressId($pressId, $locale);
00169 } else {
00170 $this->restoreByPressId($pressId);
00171 }
00172
00173 return true;
00174 }
00175
00180 function &getDefaultSettingIds($pressId) {
00181 $result =& $this->retrieve(
00182 'SELECT '. $this->getPrimaryKeyColumnName() .', '. $this->getDefaultKey() .' FROM '. $this->getTableName() .'
00183 WHERE press_id = ? AND '. $this->getDefaultKey() .' IS NOT NULL', $pressId
00184 );
00185
00186 $returner = null;
00187 while (!$result->EOF) {
00188 $returner[$result->fields[$this->getDefaultKey()]] =& $result->fields[$this->getPrimaryKeyColumnName()];
00189 $result->MoveNext();
00190 }
00191 $result->Close();
00192 unset($result);
00193
00194 return $returner;
00195 }
00196
00202 function restoreByPressId($pressId, $locale = null) {
00203
00204 $defaultIds = $this->getDefaultSettingIds($pressId);
00205
00206 if ($locale) {
00207 foreach ($defaultIds as $key => $id) {
00208 $this->update('DELETE FROM '. $this->getSettingsTableName() .' WHERE '. $this->getPrimaryKeyColumnName() .' = ? AND locale = ?', array($id, $locale));
00209 }
00210 } else {
00211 foreach ($defaultIds as $key => $id) {
00212 $this->update('DELETE FROM '. $this->getSettingsTableName() .' WHERE '. $this->getPrimaryKeyColumnName() .' = ?', $id);
00213 }
00214 }
00215
00216 if (!$locale) {
00217 $this->update('UPDATE '. $this->getTableName() .' SET enabled = ? WHERE press_id = ? AND '. $this->getDefaultKey() .' IS NOT NULL', array(1, $pressId));
00218 $this->update('UPDATE '. $this->getTableName() .' SET enabled = ? WHERE press_id = ? AND '. $this->getDefaultKey() .' IS NULL', array(0, $pressId));
00219 }
00220
00221 $sql = 'SELECT * FROM press_defaults WHERE press_id = ? AND assoc_type = ?';
00222 $sqlParams = array($pressId, $this->getDefaultType());
00223 if ($locale) {
00224 $sql .= ' AND locale = ?';
00225 $sqlParams[] = $locale;
00226 }
00227
00228 $result =& $this->retrieve($sql, $sqlParams);
00229
00230 $returner = null;
00231 while (!$result->EOF) {
00232 $row =& $result->GetRowAssoc(false);
00233 $this->update(
00234 'INSERT INTO '. $this->getSettingsTableName() .'
00235 ('. $this->getPrimaryKeyColumnName() .', locale, setting_name, setting_value, setting_type)
00236 VALUES
00237 (?, ?, ?, ?, ?)',
00238 array($defaultIds[$row['entry_key']], $row['locale'], $row['setting_name'], $row['setting_value'], $row['setting_type'])
00239 );
00240 unset($row);
00241 $result->MoveNext();
00242 }
00243 $result->Close();
00244 unset($result);
00245 }
00246
00252 function installDefaults($pressId, $locales) {
00253 $this->installDefaultBase($pressId);
00254 foreach ($locales as $locale) {
00255 $this->installDefaultBaseData($locale, $pressId);
00256 }
00257 $this->restoreByPressId($pressId);
00258 }
00259
00264 function installLocale($locale) {
00265 $pressDao =& DAORegistry::getDAO('PressDAO');
00266 $presses =& $pressDao->getNames();
00267
00268 foreach ($presses as $id => $name) {
00269 $this->installDefaultBaseData($locale, $id, false, true);
00270 }
00271
00272 }
00273
00278 function uninstallLocale($locale) {
00279 $this->update('DELETE FROM '. $this->getSettingsTableName() .' WHERE locale = ?', array($locale));
00280 $this->update('DELETE FROM press_defaults WHERE locale = ?', array($locale));
00281 }
00282 }
00283
00284 ?>