| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Settings\Storage\Backend; |
|
|
| use Piwik\Common; |
| use Piwik\Db; |
| use Exception; |
|
|
| |
| |
| |
| |
| |
| class MeasurableSettingsTable extends BaseSettingsTable |
| { |
| |
| |
| |
| private $idSite; |
|
|
| |
| |
| |
| private $pluginName; |
|
|
| public function __construct($idSite, $pluginName) |
| { |
| parent::__construct(); |
|
|
| if (empty($pluginName)) { |
| throw new Exception('No plugin name given for MeasurableSettingsTable backend'); |
| } |
|
|
| if (empty($idSite)) { |
| throw new Exception('No idSite given for MeasurableSettingsTable backend'); |
| } |
|
|
| $this->idSite = (int) $idSite; |
| $this->pluginName = $pluginName; |
| } |
|
|
| |
| |
| |
| |
| public function save($values) |
| { |
| $this->initDbIfNeeded(); |
|
|
| $valuesKeep = array(); |
| foreach ($values as $name => $value) { |
| if (!isset($value)) { |
| continue; |
| } |
| if (is_array($value) || is_object($value)) { |
| $jsonEncoded = 1; |
| $value = json_encode($value); |
| } else { |
| $jsonEncoded = 0; |
| if (is_bool($value)) { |
| |
| |
| $value = (int) $value; |
| } |
| } |
| $valuesKeep[] = array($this->idSite, $this->pluginName, $name, $value, $jsonEncoded); |
| } |
|
|
| $columns = array('idsite', 'plugin_name', 'setting_name', 'setting_value', 'json_encoded'); |
|
|
| $table = $this->getTableName(); |
| $lockKey = $this->getStorageId(); |
| $this->lock->execute($lockKey, function () use ($valuesKeep, $table, $columns) { |
| $this->delete(); |
| |
| if (!empty($valuesKeep)) { |
| Db\BatchInsert::tableInsertBatchSql($table, $columns, $valuesKeep, true); |
| } |
| }); |
| } |
|
|
| |
| |
| |
| public function getStorageId() |
| { |
| return 'MeasurableSettings_' . $this->idSite . '_' . $this->pluginName; |
| } |
|
|
| private function jsonEncodedMissingError(Exception $e) |
| { |
| return strpos($e->getMessage(), 'json_encoded') !== false; |
| } |
|
|
| public function load() |
| { |
| $this->initDbIfNeeded(); |
|
|
| $table = $this->getTableName(); |
|
|
| $sql = "SELECT `setting_name`, `setting_value`, `json_encoded` FROM `$table` WHERE idsite = ? and plugin_name = ?"; |
| $bind = array($this->idSite, $this->pluginName); |
|
|
| try { |
| $settings = $this->db->fetchAll($sql, $bind); |
| } catch (\Exception $e) { |
| |
| |
| if ($this->jsonEncodedMissingError($e)) { |
| $sql = "SELECT `setting_name`, `setting_value` FROM `$table` WHERE idsite = ? and plugin_name = ?"; |
| $settings = $this->db->fetchAll($sql, $bind); |
| } else { |
| throw $e; |
| } |
| } |
|
|
| $flat = array(); |
| foreach ($settings as $setting) { |
| $name = $setting['setting_name']; |
|
|
| if (!empty($setting['json_encoded'])) { |
| $flat[$name] = json_decode($setting['setting_value'], true); |
| } elseif (array_key_exists($name, $flat)) { |
| if (!is_array($flat[$name])) { |
| $flat[$name] = array($flat[$name]); |
| } |
| $flat[$name][] = $setting['setting_value']; |
| } else { |
| $flat[$name] = $setting['setting_value']; |
| } |
| } |
|
|
| return $flat; |
| } |
|
|
| protected function getTableName() |
| { |
| return Common::prefixTable('site_setting'); |
| } |
|
|
| public function delete() |
| { |
| $this->initDbIfNeeded(); |
|
|
| $table = $this->getTableName(); |
| $sql = "DELETE FROM `$table` WHERE `idsite` = ? and plugin_name = ?"; |
| $bind = array($this->idSite, $this->pluginName); |
|
|
| $this->db->query($sql, $bind); |
| } |
|
|
| |
| |
| |
| |
| |
| public static function removeAllSettingsForSite($idSite) |
| { |
| try { |
| $query = sprintf('DELETE FROM `%s` WHERE idsite = ?', Common::prefixTable('site_setting')); |
| Db::query($query, array($idSite)); |
| } catch (Exception $e) { |
| if ($e->getCode() != 42) { |
| |
| throw $e; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| public static function removeAllSettingsForPlugin($pluginName) |
| { |
| try { |
| $query = sprintf('DELETE FROM `%s` WHERE plugin_name = ?', Common::prefixTable('site_setting')); |
| Db::query($query, array($pluginName)); |
| } catch (Exception $e) { |
| if ($e->getCode() != 42) { |
| |
| throw $e; |
| } |
| } |
| } |
| } |
|
|