1c34e033207276adbc092fd34011a35be1bc060a
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\user\group\option\UserGroupOption;
4 use wcf\data\user\group\option\UserGroupOptionEditor;
5 use wcf\data\user\group\UserGroup;
6 use wcf\system\WCF;
7 use wcf\util\StringUtil;
8
9 /**
10 * This PIP installs, updates or deletes user group permissions.
11 *
12 * @author Benjamin Kunz
13 * @copyright 2001-2011 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package com.woltlab.wcf
16 * @subpackage system.package.plugin
17 * @category Community Framework
18 */
19 class UserGroupOptionsPackageInstallationPlugin extends AbstractOptionPackageInstallationPlugin {
20 /**
21 * @see wcf\system\package\plugin\AbstractPackageInstallationPlugin::$tableName
22 */
23 public $tableName = 'user_group_option';
24
25 public static $reservedTags = array('name', 'optiontype', 'defaultvalue', 'admindefaultvalue', 'validationpattern', 'showorder', 'categoryname', 'selectoptions', 'enableoptions', 'permissions', 'options', 'attrs', 'cdata');
26
27 /**
28 * Deletes group-option-categories and/or group-options which where installed by the package.
29 */
30 public function uninstall() {
31 // Delete value-entries using categories or options
32 // which will be deleted.
33 $sql = "DELETE FROM wcf".WCF_N."_user_group_option_value
34 WHERE optionID IN (
35 SELECT optionID
36 FROM wcf".WCF_N."_user_group_option
37 WHERE packageID = ?
38 )";
39 $statement = WCF::getDB()->prepareStatement($sql);
40 $statement->execute(array($this->installation->getPackageID()));
41
42 parent::uninstall();
43 }
44
45 /**
46 * @see wcf\system\package\plugin\AbstractOptionPackageInstallationPlugin::saveOption()
47 */
48 protected function saveOption($option, $categoryName, $existingOptionID = 0) {
49 // default values
50 $optionName = $optionType = $defaultValue = $adminDefaultValue = $validationPattern = $enableOptions = $permissions = $options = '';
51 $showOrder = null;
52
53 // get values
54 if (isset($option['name'])) $optionName = $option['name'];
55 if (isset($option['optiontype'])) $optionType = $option['optiontype'];
56 if (isset($option['defaultvalue'])) $defaultValue = $option['defaultvalue'];
57 if (isset($option['admindefaultvalue'])) $adminDefaultValue = $option['admindefaultvalue'];
58 if (isset($option['validationpattern'])) $validationPattern = $option['validationpattern'];
59 if (!empty($option['showorder'])) $showOrder = intval($option['showorder']);
60 $showOrder = $this->getShowOrder($showOrder, $categoryName, 'categoryName');
61 if (isset($option['enableoptions'])) $enableOptions = $option['enableoptions'];
62 if (isset($option['permissions'])) $permissions = $option['permissions'];
63 if (isset($option['options'])) $options = $option['options'];
64
65 // check if optionType exists
66 $className = 'wcf\system\option\user\group\\'.StringUtil::firstCharToUpperCase($optionType).'UserGroupOptionType';
67 if (!class_exists($className)) {
68 throw new SystemException("unable to find class '".$className."'");
69 }
70
71 // collect additional tags and their values
72 $additionalData = array();
73 foreach ($option as $tag => $value) {
74 if (!in_array($tag, self::$reservedTags)) $additionalData[$tag] = $value;
75 }
76
77 // check if the otion exist already and was installed by this package
78 $sql = "SELECT optionID
79 FROM wcf".WCF_N."_user_group_option
80 WHERE optionName = ?
81 AND packageID = ?";
82 $statement = WCF::getDB()->prepareStatement($sql);
83 $statement->execute(array(
84 $optionName,
85 $this->installation->getPackageID()
86 ));
87 $row = $statement->fetchArray();
88
89 $data = array(
90 'categoryName' => $categoryName,
91 'optionType' => $optionType,
92 'defaultValue' => $defaultValue,
93 'adminDefaultValue' => $adminDefaultValue,
94 'validationPattern' => $validationPattern,
95 'showOrder' => $showOrder,
96 'enableOptions' => $enableOptions,
97 'permissions' => $permissions,
98 'options' => $options,
99 'additionalData' => serialize($additionalData)
100 );
101
102 if (!empty($row['optionID'])) {
103 // update existing option
104 $optionID = $row['optionID'];
105
106 $groupOption = new UserGroupOption(null, $row);
107 $groupOptionEditor = new UserGroupOptionEditor($groupOption);
108 $groupOptionEditor->update($data);
109 }
110 else {
111 // add new option
112 $data['packageID'] = $this->installation->getPackageID();
113 $data['optionName'] = $optionName;
114
115 $groupOptionEditor = UserGroupOptionEditor::create($data);
116 $optionID = $groupOptionEditor->optionID;
117
118 // get default group ("everyone")
119 $sql = "SELECT groupID
120 FROM wcf".WCF_N."_user_group
121 WHERE groupType = ?";
122 $statement = WCF::getDB()->prepareStatement($sql);
123 $statement->execute(array(UserGroup::EVERYONE));
124 $row = $statement->fetchArray();
125
126 // save default value
127 $sql = "INSERT INTO wcf".WCF_N."_user_group_option_value
128 (groupID, optionID, optionValue)
129 VALUES (?, ?, ?)";
130 $statement = WCF::getDB()->prepareStatement($sql);
131 $statement->execute(array($row['groupID'], $optionID, $defaultValue));
132
133 if ($adminDefaultValue && $defaultValue != $adminDefaultValue) {
134 $sql = "SELECT groupID
135 FROM wcf".WCF_N."_user_group_option_value
136 WHERE optionID = (
137 SELECT optionID
138 FROM wcf".WCF_N."_user_group_option
139 WHERE optionName = ?
140 )
141 AND optionValue = '1'";
142 $statement2 = WCF::getDB()->prepareStatement($sql);
143 $statement2->execute(array('admin.general.canUseAcp'));
144
145 $acpGroups = array();
146 while ($row = $statement2->fetchArray()) {
147 $acpGroups[] = $row['groupID'];
148 }
149
150 $statement2->execute(array('admin.user.canEditGroup'));
151 while ($row = $statement2->fetchArray()) {
152 if (!in_array($row['groupID'], $acpGroups)) {
153 continue;
154 }
155
156 $statement->execute(array(
157 $row['groupID'],
158 $optionID,
159 $adminDefaultValue
160 ));
161 }
162 }
163 }
164 }
165 }