• Main Page
  • Modules
  • Classes
  • Files
  • File List

classes/plugins/PluginSettingsDAO.inc.php

00001 <?php
00002 
00016 class PluginSettingsDAO extends DAO {
00020    function PluginSettingsDAO() {
00021       parent::DAO();
00022    }
00023 
00030    function &_getCache($pressId, $pluginName) {
00031       static $settingCache;
00032 
00033       if (!isset($settingCache)) {
00034          $settingCache = array();
00035       }
00036       if (!isset($settingCache[$pressId])) {
00037          $settingCache[$pressId] = array();
00038       }
00039       if (!isset($settingCache[$pressId][$pluginName])) {
00040          $cacheManager =& CacheManager::getManager();
00041          $settingCache[$pressId][$pluginName] = $cacheManager->getCache(
00042             'pluginSettings-' . $pressId, $pluginName,
00043             array($this, '_cacheMiss')
00044          );
00045       }
00046       return $settingCache[$pressId][$pluginName];
00047    }
00048 
00055    function getSetting($pressId, $pluginName, $name) {
00056       // Normalize the plug-in name to lower case.
00057       $pluginName = strtolower_codesafe($pluginName);
00058 
00059       // Retrieve the setting.
00060       $cache =& $this->_getCache($pressId, $pluginName);
00061       return $cache->get($name);
00062    }
00063 
00070    function _cacheMiss(&$cache, $id) {
00071       $contextParts = explode('-', $cache->getContext());
00072       $pressId = array_pop($contextParts);
00073       $settings =& $this->getPluginSettings($pressId, $cache->getCacheId());
00074       if (!isset($settings[$id])) {
00075          // Make sure that even null values are cached
00076          $cache->setCache($id, null);
00077          return null;
00078       }
00079       return $settings[$id];
00080    }
00081 
00088    function &getPluginSettings($pressId, $pluginName) {
00089       // Normalize plug-in name to lower case.
00090       $pluginName = strtolower_codesafe($pluginName);
00091 
00092       $result =& $this->retrieve(
00093          'SELECT setting_name, setting_value, setting_type FROM plugin_settings WHERE plugin_name = ? AND press_id = ?', array($pluginName, $pressId)
00094       );
00095 
00096       $pluginSettings = array();
00097       while (!$result->EOF) {
00098          $row =& $result->getRowAssoc(false);
00099          $pluginSettings[$row['setting_name']] = $this->convertFromDB($row['setting_value'], $row['setting_type']);
00100          $result->MoveNext();
00101       }
00102       $result->Close();
00103       unset($result);
00104 
00105       $cache =& $this->_getCache($pressId, $pluginName);
00106       $cache->setEntireCache($pluginSettings);
00107 
00108       return $pluginSettings;
00109    }
00110 
00119    function updateSetting($pressId, $pluginName, $name, $value, $type = null) {
00120       // Normalize the plug-in name to lower case.
00121       $pluginName = strtolower_codesafe($pluginName);
00122 
00123       $cache =& $this->_getCache($pressId, $pluginName);
00124       $cache->setCache($name, $value);
00125 
00126       $result = $this->retrieve(
00127          'SELECT COUNT(*) FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
00128          array($pluginName, $name, $pressId)
00129       );
00130 
00131       $value = $this->convertToDB($value, $type);
00132       if ($result->fields[0] == 0) {
00133          $returner = $this->update(
00134             'INSERT INTO plugin_settings
00135                (plugin_name, press_id, setting_name, setting_value, setting_type)
00136                VALUES
00137                (?, ?, ?, ?, ?)',
00138             array($pluginName, $pressId, $name, $value, $type)
00139          );
00140       } else {
00141          $returner = $this->update(
00142             'UPDATE plugin_settings SET
00143                setting_value = ?,
00144                setting_type = ?
00145                WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
00146             array($value, $type, $pluginName, $name, $pressId)
00147          );
00148       }
00149 
00150       $result->Close();
00151       unset($result);
00152 
00153       return $returner;
00154    }
00155 
00162    function deleteSetting($pressId, $pluginName, $name) {
00163       // Normalize the plug-in name to lower case.
00164       $pluginName = strtolower_codesafe($pluginName);
00165 
00166       $cache =& $this->_getCache($pressId, $pluginName);
00167       $cache->setCache($name, null);
00168 
00169       return $this->update(
00170          'DELETE FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
00171          array($pluginName, $name, $pressId)
00172       );
00173    }
00174 
00180    function deleteSettingsByPlugin($pressId, $pluginName) {
00181       // Normalize the plug-in name to lower case.
00182       $pluginName = strtolower_codesafe($pluginName);
00183 
00184       $cache =& $this->_getCache($pressId, $pluginName);
00185       $cache->flush();
00186 
00187       return $this->update(
00188          'DELETE FROM plugin_settings WHERE press_id = ? AND plugin_name = ?',
00189          array($pressId, $pluginName)
00190       );
00191    }
00192 
00197    function deleteSettingsByPressId($pressId) {
00198       return $this->update(
00199          'DELETE FROM plugin_settings WHERE press_id = ?', $pressId
00200       );
00201    }
00202 
00209    function _performReplacement($rawInput, $paramArray = array()) {
00210       $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_plugin_regexp_callback', $rawInput);
00211       foreach ($paramArray as $pKey => $pValue) {
00212          $value = str_replace('{$' . $pKey . '}', $pValue, $value);
00213       }
00214       return $value;
00215    }
00216 
00223    function &_buildObject (&$node, $paramArray = array()) {
00224       $value = array();
00225       foreach ($node->getChildren() as $element) {
00226          $key = $element->getAttribute('key');
00227          $childArray =& $element->getChildByName('array');
00228          if (isset($childArray)) {
00229             $content = $this->_buildObject($childArray, $paramArray);
00230          } else {
00231             $content = $this->_performReplacement($element->getValue(), $paramArray);
00232          }
00233          if (!empty($key)) {
00234             $key = $this->_performReplacement($key, $paramArray);
00235             $value[$key] = $content;
00236          } else $value[] = $content;
00237       }
00238       return $value;
00239    }
00240 
00247    function installSettings($pressId, $pluginName, $filename, $paramArray = array()) {
00248       $xmlParser = new XMLParser();
00249       $tree = $xmlParser->parse($filename);
00250 
00251       if (!$tree) {
00252          $xmlParser->destroy();
00253          return false;
00254       }
00255 
00256       // Check for existing settings and leave them if they are already in place.
00257       $currentSettings =& $this->getPluginSettings($pressId, $pluginName);
00258 
00259       foreach ($tree->getChildren() as $setting) {
00260          $nameNode =& $setting->getChildByName('name');
00261          $valueNode =& $setting->getChildByName('value');
00262 
00263          if (isset($nameNode) && isset($valueNode)) {
00264             $type = $setting->getAttribute('type');
00265             $name =& $nameNode->getValue();
00266 
00267             // If the setting already exists, respect it.
00268             if (isset($currentSettings[$name])) continue;
00269 
00270             if ($type == 'object') {
00271                $arrayNode =& $valueNode->getChildByName('array');
00272                $value = $this->_buildObject($arrayNode, $paramArray);
00273             } else {
00274                $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
00275             }
00276 
00277             // Replace translate calls with translated content
00278             $this->updateSetting($pressId, $pluginName, $name, $value, $type);
00279          }
00280       }
00281 
00282       $xmlParser->destroy();
00283    }
00284 }
00285 
00292 function _installer_plugin_regexp_callback($matches) {
00293    return __($matches[1]);
00294 }
00295 
00296 ?>

Generated on Mon Sep 17 2012 13:58:55 for Open Monograph Press by  doxygen 1.7.1