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