23554332680ecfdd6ba7a981ad75c493a93a30e2
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / option / OptionEditor.class.php
1 <?php
2 namespace wcf\data\option;
3 use wcf\data\DatabaseObjectEditor;
4 use wcf\data\IEditableCachedObject;
5 use wcf\system\cache\CacheHandler;
6 use wcf\system\io\File;
7 use wcf\system\WCF;
8 use wcf\util\FileUtil;
9
10 /**
11 * Provides functions to edit options.
12 *
13 * @author Alexander Ebert
14 * @copyright 2001-2012 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package com.woltlab.wcf
17 * @subpackage data.option
18 * @category Community Framework
19 */
20 class OptionEditor extends DatabaseObjectEditor implements IEditableCachedObject {
21 /**
22 * options cache file name
23 * @var string
24 */
25 const FILENAME = 'options.inc.php';
26
27 /**
28 * @see wcf\data\DatabaseObjectDecorator::$baseClass
29 */
30 protected static $baseClass = 'wcf\data\option\Option';
31
32 /**
33 * Imports the given options.
34 *
35 * @param array $options name to value
36 */
37 public static function import(array $options) {
38 // get option ids
39 $sql = "SELECT optionName, optionID
40 FROM wcf".WCF_N."_option";
41 $statement = WCF::getDB()->prepareStatement($sql);
42 $statement->execute();
43 $optionIDs = array();
44 while ($row = $statement->fetchArray()) {
45 $optionIDs[$row['optionName']] = $row['optionID'];
46 }
47
48 $newOptions = array();
49 foreach ($options as $name => $value) {
50 if (isset($optionIDs[$name])) {
51 $newOptions[$optionIDs[$name]] = $value;
52 }
53 }
54
55 self::updateAll($newOptions);
56 }
57
58 /**
59 * Updates the values of the given options.
60 *
61 * @param array $options id to value
62 */
63 public static function updateAll(array $options) {
64 $sql = "UPDATE wcf".WCF_N."_option
65 SET optionValue = ?
66 WHERE optionID = ?";
67 $statement = WCF::getDB()->prepareStatement($sql);
68
69 foreach ($options as $id => $value) {
70 $statement->execute(array(
71 $value,
72 $id
73 ));
74 }
75
76 // force a cache reset if options were changed
77 self::resetCache();
78 }
79
80 /**
81 * @see wcf\data\IEditableCachedObject::resetCache()
82 */
83 public static function resetCache() {
84 // reset cache
85 CacheHandler::getInstance()->clear(WCF_DIR.'cache', 'cache.option.php');
86
87 // reset options.inc.php files
88 self::rebuild();
89 }
90
91 /**
92 * Rebuilds the option file.
93 */
94 public static function rebuild() {
95 $buffer = '';
96
97 // file header
98 $buffer .= "<?php\n/**\n* generated at ".gmdate('r')."\n*/\n";
99
100 // get all options
101 $options = Option::getOptions();
102 foreach ($options as $optionName => $option) {
103 $buffer .= "if (!defined('".$optionName."')) define('".$optionName."', ".(($option->optionType == 'boolean' || $option->optionType == 'integer') ? intval($option->optionValue) : "'".addcslashes($option->optionValue, "'\\")."'").");\n";
104 }
105 unset($options);
106
107 // file footer
108 $buffer .= "\n";
109
110 // open file
111 $file = new File(WCF_DIR.'options.inc.php');
112
113 // write buffer
114 $file->write($buffer);
115
116 // close file
117 $file->close();
118 @$file->chmod(0777);
119 }
120 }