classes/plugins/PluginSettingsDAO.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00016 //$Id$
00017 
00018 class PluginSettingsDAO extends DAO {
00019    function &_getCache($conferenceId, $schedConfId, $pluginName) {
00020       static $settingCache;
00021       if (!isset($settingCache)) {
00022          $settingCache = array();
00023       }
00024       if (!isset($settingCache[$conferenceId])) {
00025          $settingCache[$conferenceId] = array();
00026       }
00027       if (!isset($settingCache[$conferenceId][$schedConfId])) {
00028          $settingCache[$conferenceId][$schedConfId] = array();
00029       }
00030       if (!isset($settingCache[$conferenceId][$schedConfId][$pluginName])) {
00031          $cacheManager =& CacheManager::getManager();
00032          $settingCache[$conferenceId][$schedConfId][$pluginName] = $cacheManager->getCache(
00033             'pluginSettings-' . ((int) $conferenceId) . '-' . ((int) $schedConfId), $pluginName,
00034             array($this, '_cacheMiss')
00035          );
00036       }
00037       return $settingCache[$conferenceId][$schedConfId][$pluginName];
00038    }
00039 
00048    function getSetting($conferenceId, $schedConfId, $pluginName, $name) {
00049       $cache =& $this->_getCache($conferenceId, $schedConfId, $pluginName);
00050       return $cache->get($name);
00051    }
00052 
00053    function _cacheMiss(&$cache, $id) {
00054       $contextParts = explode('-', $cache->getContext());
00055       $schedConfId = array_pop($contextParts);
00056       $conferenceId = array_pop($contextParts);
00057       $settings =& $this->getPluginSettings($conferenceId, $schedConfId, $cache->getCacheId());
00058       if (!isset($settings[$id])) {
00059          // Make sure that even null values are cached
00060          $cache->setCache($id, null);
00061          return null;
00062       }
00063       return $settings[$id];
00064    }
00065 
00073    function &getPluginSettings($conferenceId, $schedConfId, $pluginName) {
00074       $pluginSettings[$pluginName] = array();
00075 
00076       $result =& $this->retrieve(
00077          'SELECT setting_name, setting_value, setting_type FROM plugin_settings WHERE plugin_name = ? AND conference_id = ? AND sched_conf_id = ?', array($pluginName, $conferenceId, $schedConfId)
00078       );
00079 
00080       if ($result->RecordCount() == 0) {
00081          $returner = null;
00082          $result->Close();
00083          return $returner;
00084 
00085       } else {
00086          while (!$result->EOF) {
00087             $row =& $result->getRowAssoc(false);
00088             $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
00089             $pluginSettings[$pluginName][$row['setting_name']] = $value;
00090             $result->MoveNext();
00091          }
00092          $result->close();
00093          unset($result);
00094 
00095          $cache =& $this->_getCache($conferenceId, $schedConfId, $pluginName);
00096          $cache->setEntireCache($pluginSettings[$pluginName]);
00097 
00098          return $pluginSettings[$pluginName];
00099       }
00100    }
00101 
00111    function updateSetting($conferenceId, $schedConfId, $pluginName, $name, $value, $type = null) {
00112       $cache =& $this->_getCache($conferenceId, $schedConfId, $pluginName);
00113       $cache->setCache($name, $value);
00114 
00115       $result = $this->retrieve(
00116          'SELECT COUNT(*) FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND conference_id = ? AND sched_conf_id = ?',
00117          array($pluginName, $name, $conferenceId, $schedConfId)
00118       );
00119 
00120       $value = $this->convertToDB($value, $type);
00121       if ($result->fields[0] == 0) {
00122          $returner = $this->update(
00123             'INSERT INTO plugin_settings
00124                (plugin_name, conference_id, sched_conf_id, setting_name, setting_value, setting_type)
00125                VALUES
00126                (?, ?, ?, ?, ?, ?)',
00127             array($pluginName, $conferenceId, $schedConfId, $name, $value, $type)
00128          );
00129       } else {
00130          $returner = $this->update(
00131             'UPDATE plugin_settings SET
00132                setting_value = ?,
00133                setting_type = ?
00134                WHERE plugin_name = ? AND setting_name = ? AND conference_id = ? AND sched_conf_id = ?',
00135             array($value, $type, $pluginName, $name, $conferenceId, $schedConfId)
00136          );
00137       }
00138 
00139       $result->Close();
00140       unset($result);
00141 
00142       return $returner;
00143    }
00144 
00153    function deleteSetting($conferenceId, $schedConfId, $pluginName, $name) {
00154       $cache =& $this->_getCache($conferenceId, $schedConfId, $pluginName);
00155       $cache->setCache($name, null);
00156 
00157       return $this->update(
00158          'DELETE FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND conference_id = ? AND sched_conf_id = ?',
00159          array($pluginName, $name, $conferenceId, $schedConfId)
00160       );
00161    }
00162 
00167    function deleteSettingsByPlugin($pluginName, $conferenceId = null, $schedConfId = null) {
00168       if ( $conferenceId && $schedConfId) { 
00169          $cache =& $this->_getCache($conferenceId, $schedConfId, $pluginName);
00170          $cache->flush();
00171 
00172          return $this->update(
00173                'DELETE FROM plugin_settings WHERE plugin_name = ? AND conference_id = ? AND sched_conf_id = ?', 
00174                array($pluginName, $conferenceId, $schedConfId)
00175          );
00176       } else {
00177          $cacheManager =& CacheManager::getManager();
00178          // NB: this actually deletes all plugins' settings cache       
00179          $cacheManager->flush('pluginSettings');
00180          
00181          $params = array($pluginName);
00182          if ($conferenceId) $params[] = $conferenceId;
00183 
00184          return $this->update(
00185             'DELETE FROM plugin_settings WHERE plugin_name = ?' . (($conferenceId)?' AND conference_id = ?':''), 
00186             $params
00187          );
00188       }     
00189    }
00190 
00195    function deleteSettingsByConferenceId($conferenceId) {
00196       return $this->update(
00197             'DELETE FROM plugin_settings WHERE conference_id = ?', $conferenceId
00198       );
00199    }
00200 
00207    function _performReplacement($rawInput, $paramArray = array()) {
00208       $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_plugin_regexp_callback', $rawInput);
00209       foreach ($paramArray as $pKey => $pValue) {
00210          $value = str_replace('{$' . $pKey . '}', $pValue, $value);
00211       }
00212       return $value;
00213    }
00214 
00221    function &_buildObject (&$node, $paramArray = array()) {
00222       $value = array();
00223       foreach ($node->getChildren() as $element) {
00224          $key = $element->getAttribute('key');
00225          $childArray =& $element->getChildByName('array');
00226          if (isset($childArray)) {
00227             $content = $this->_buildObject($childArray, $paramArray);
00228          } else {
00229             $content = $this->_performReplacement($element->getValue(), $paramArray);
00230          }
00231          if (!empty($key)) {
00232             $key = $this->_performReplacement($key, $paramArray);
00233             $value[$key] = $content;
00234          } else $value[] = $content;
00235       }
00236       return $value;
00237    }
00238 
00247    function installSettings($conferenceId, $schedConfId, $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       foreach ($tree->getChildren() as $setting) {
00257          $nameNode =& $setting->getChildByName('name');
00258          $valueNode =& $setting->getChildByName('value');
00259 
00260          if (isset($nameNode) && isset($valueNode)) {
00261             $type = $setting->getAttribute('type');
00262             $name =& $nameNode->getValue();
00263 
00264             if ($type == 'object') {
00265                $arrayNode =& $valueNode->getChildByName('array');
00266                $value = $this->_buildObject($arrayNode, $paramArray);
00267             } else {
00268                $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
00269             }
00270 
00271             // Replace translate calls with translated content
00272             $this->updateSetting($conferenceId, $schedConfId, $pluginName, $name, $value, $type);
00273          }
00274       }
00275 
00276       $xmlParser->destroy();
00277 
00278    }
00279 }
00280 
00284 function _installer_plugin_regexp_callback($matches) {
00285    return __($matches[1]);
00286 }
00287 
00288 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7