use wcf\system\event\EventHandler;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\exception\SystemException;
+use wcf\system\exception\UserInputException;
use wcf\system\exception\ValidateActionException;
use wcf\system\WCF;
use wcf\util\ClassUtil;
*/
protected $allowGuestAccess = array();
+ const TYPE_INTEGER = 1;
+ const TYPE_STRING = 2;
+
/**
* Initialize a new DatabaseObject-related action.
*
}
}
+ /**
+ * Returns a single object and throws and exception if no or more than one object is given.
+ *
+ * @return wcf\data\DatabaseObject
+ */
+ protected function getSingleObject() {
+ if (empty($this->objects)) {
+ $this->readObjects();
+ }
+
+ if (count($this->objects) != 1) {
+ throw new UserInputException('objectIDs');
+ }
+
+ reset($this->objects);
+ return current($this->objects);
+ }
+
+ /**
+ * Reads an integer value and validates it.
+ *
+ * @param string $variableName
+ * @param boolean $allowEmpty
+ * @param string $arrayIndex
+ */
+ protected function readInteger($variableName, $allowEmpty = false, $arrayIndex = '', $allowUnset = false) {
+ $this->readValue($variableName, $allowEmpty, $arrayIndex, self::TYPE_INTEGER);
+ }
+
+ /**
+ * Reads a string value and validates it.
+ *
+ * @param string $variableName
+ * @param boolean $allowEmpty
+ * @param string $arrayIndex
+ */
+ protected function readString($variableName, $allowEmpty = false, $arrayIndex = '') {
+ $this->readValue($variableName, $allowEmpty, $arrayIndex, self::TYPE_STRING);
+ }
+
+ /**
+ * Reads a value and validates it. If you set $allowEmpty to true, no exception will
+ * be thrown if the variable evaluates to 0 (integer) or '' (string). Furthermore the
+ * variable will be always created with a sane value if it does not exist.
+ *
+ * @param string $variableName
+ * @param boolean $allowEmpty
+ * @param string $arrayIndex
+ * @param integer $type
+ */
+ protected function readValue($variableName, $allowEmpty, $arrayIndex, $type) {
+ if ($arrayIndex) {
+ if (!isset($this->parameters[$arrayIndex])) {
+ throw new SystemException("Corrupt parameters, index '".$arrayIndex."' is missing");
+ }
+
+ $target =& $this->parameters[$variableName];
+ }
+ else {
+ $target =& $this->parameters;
+ }
+
+ switch ($type) {
+ case self::TYPE_INTEGER:
+ if (!isset($target[$variableName])) {
+ if ($allowEmpty) {
+ $target[$variableName] = 0;
+ }
+ else {
+ throw new UserInputException($variableName);
+ }
+ }
+ else {
+ $target[$variableName] = intval($target[$variableName]);
+ if (!$allowEmpty && !$target[$variableName]) {
+ throw new UserInputException($variableName);
+ }
+ }
+ break;
+
+ case self::TYPE_STRING:
+ if (!isset($target[$variableName])) {
+ if ($allowEmpty) {
+ $target[$variableName] = '';
+ }
+ else {
+ throw new UserInputException($variableName);
+ }
+ }
+ else {
+ $target[$variableName] = StringUtil::trim($target[$variableName]);
+ if (!$allowEmpty && empty($target[$variableName])) {
+ throw new UserInputException($variableName);
+ }
+ }
+ break;
+ }
+
+ }
+
/**
* Returns object class name.
*
*/
protected $permissionsUpdate = array('admin.style.canEditStyle');
+ /**
+ * style editor object
+ * @var wcf\data\style\StyleEditor
+ */
+ public $styleEditor = null;
+
/**
* @see wcf\data\AbstractDatabaseObjectAction::create()
*/
throw new PermissionDeniedException();
}
- if (empty($this->objects)) {
- $this->readObjects();
- if (empty($this->objects)) {
- throw new UserInputException('objectIDs');
- }
- }
-
- if (count($this->objects) > 1) {
- throw new UserInputException('objectIDs');
- }
+ $this->styleEditor = $this->getSingleObject();
}
/**
* @return array<string>
*/
public function copy() {
- $style = current($this->objects);
-
// get unique style name
$sql = "SELECT styleName
FROM wcf".WCF_N."_style
AND styleID <> ?";
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute(array(
- $style->styleName.'%',
- $style->styleID
+ $this->styleEditor->styleName.'%',
+ $this->styleEditor->styleID
));
$numbers = array();
$regEx = new Regex('\((\d+)\)$');
$matches = $regEx->getMatches();
// check if name matches the pattern 'styleName (x)'
- if ($styleName == $style->styleName . ' ('.$matches[1].')') {
+ if ($styleName == $this->styleEditor->styleName . ' ('.$matches[1].')') {
$numbers[] = $matches[1];
}
}
}
$number = (count($numbers)) ? max($numbers) + 1 : 2;
- $styleName = $style->styleName . ' ('.$number.')';
+ $styleName = $this->styleEditor->styleName . ' ('.$number.')';
// create the new style
$newStyle = StyleEditor::create(array(
'packageID' => PACKAGE_ID,
'styleName' => $styleName,
- 'templateGroupID' => $style->templateGroupID,
+ 'templateGroupID' => $this->styleEditor->templateGroupID,
'disabled' => 1, // newly created styles are disabled by default
- 'styleDescription' => $style->styleDescription,
- 'styleVersion' => $style->styleVersion,
- 'styleDate' => $style->styleDate,
- 'copyright' => $style->copyright,
- 'license' => $style->license,
- 'authorName' => $style->authorName,
- 'authorURL' => $style->authorURL,
- 'iconPath' => $style->iconPath,
- 'imagePath' => $style->imagePath
+ 'styleDescription' => $this->styleEditor->styleDescription,
+ 'styleVersion' => $this->styleEditor->styleVersion,
+ 'styleDate' => $this->styleEditor->styleDate,
+ 'copyright' => $this->styleEditor->copyright,
+ 'license' => $this->styleEditor->license,
+ 'authorName' => $this->styleEditor->authorName,
+ 'authorURL' => $this->styleEditor->authorURL,
+ 'iconPath' => $this->styleEditor->iconPath,
+ 'imagePath' => $this->styleEditor->imagePath
));
// copy style variables
FROM wcf".WCF_N."_style_variable_value value
WHERE value.styleID = ?";
$statement = WCF::getDB()->prepareStatement($sql);
- $statement->execute(array($style->styleID));
+ $statement->execute(array($this->styleEditor->styleID));
// copy preview image
- if ($style->image) {
+ if ($this->styleEditor->image) {
// get extension
- $fileExtension = StringUtil::substring($style->image, StringUtil::lastIndexOf($style->image, '.'));
+ $fileExtension = StringUtil::substring($this->styleEditor->image, StringUtil::lastIndexOf($this->styleEditor->image, '.'));
// copy existing preview image
- if (@copy(WCF_DIR.'images/'.$style->image, WCF_DIR.'images/stylePreview-'.$newStyle->styleID.$fileExtension)) {
+ if (@copy(WCF_DIR.'images/'.$this->styleEditor->image, WCF_DIR.'images/stylePreview-'.$newStyle->styleID.$fileExtension)) {
// bypass StyleEditor::update() to avoid scaling of already fitting image
$sql = "UPDATE wcf".WCF_N."_style
SET image = ?