Exemple #1
0
 /**
  * Copies a style.
  * 
  * @return	array<string>
  */
 public function copy()
 {
     // get unique style name
     $sql = "SELECT\tstyleName\n\t\t\tFROM\twcf" . WCF_N . "_style\n\t\t\tWHERE\tstyleName LIKE ?\n\t\t\t\tAND styleID <> ?";
     $statement = WCF::getDB()->prepareStatement($sql);
     $statement->execute(array($this->styleEditor->styleName . '%', $this->styleEditor->styleID));
     $numbers = array();
     $regEx = new Regex('\\((\\d+)\\)$');
     while ($row = $statement->fetchArray()) {
         $styleName = $row['styleName'];
         if ($regEx->match($styleName)) {
             $matches = $regEx->getMatches();
             // check if name matches the pattern 'styleName (x)'
             if ($styleName == $this->styleEditor->styleName . ' (' . $matches[1] . ')') {
                 $numbers[] = $matches[1];
             }
         }
     }
     $number = count($numbers) ? max($numbers) + 1 : 2;
     $styleName = $this->styleEditor->styleName . ' (' . $number . ')';
     // create the new style
     $newStyle = StyleEditor::create(array('styleName' => $styleName, 'templateGroupID' => $this->styleEditor->templateGroupID, 'isDisabled' => 1, '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, 'imagePath' => $this->styleEditor->imagePath));
     // check if style description uses i18n
     if (preg_match('~^wcf.style.styleDescription\\d+$~', $newStyle->styleDescription)) {
         $styleDescription = 'wcf.style.styleDescription' . $newStyle->styleID;
         // copy language items
         $sql = "INSERT INTO\twcf" . WCF_N . "_language_item\n\t\t\t\t\t\t(languageID, languageItem, languageItemValue, languageItemOriginIsSystem, languageCategoryID, packageID)\n\t\t\t\tSELECT\t\tlanguageID, '" . $styleDescription . "', languageItemValue, 0, languageCategoryID, packageID\n\t\t\t\tFROM\t\twcf" . WCF_N . "_language_item\n\t\t\t\tWHERE\t\tlanguageItem = ?";
         $statement = WCF::getDB()->prepareStatement($sql);
         $statement->execute(array($newStyle->styleDescription));
         // update style description
         $styleEditor = new StyleEditor($newStyle);
         $styleEditor->update(array('styleDescription' => $styleDescription));
     }
     // copy style variables
     $sql = "INSERT INTO\twcf" . WCF_N . "_style_variable_value\n\t\t\t\t\t(styleID, variableID, variableValue)\n\t\t\tSELECT\t\t" . $newStyle->styleID . " AS styleID, value.variableID, value.variableValue\n\t\t\tFROM\t\twcf" . WCF_N . "_style_variable_value value\n\t\t\tWHERE\t\tvalue.styleID = ?";
     $statement = WCF::getDB()->prepareStatement($sql);
     $statement->execute(array($this->styleEditor->styleID));
     // copy preview image
     if ($this->styleEditor->image) {
         // get extension
         $fileExtension = mb_substr($this->styleEditor->image, mb_strrpos($this->styleEditor->image, '.'));
         // copy existing preview image
         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\twcf" . WCF_N . "_style\n\t\t\t\t\tSET\timage = ?\n\t\t\t\t\tWHERE\tstyleID = ?";
             $statement = WCF::getDB()->prepareStatement($sql);
             $statement->execute(array('stylePreview-' . $newStyle->styleID . $fileExtension, $newStyle->styleID));
         }
     }
     // copy images
     if ($this->styleEditor->imagePath && is_dir(WCF_DIR . $this->styleEditor->imagePath)) {
         $path = FileUtil::removeTrailingSlash($this->styleEditor->imagePath);
         $newPath = '';
         $i = 2;
         while (true) {
             $newPath = "{$path}-{$i}/";
             if (!file_exists(WCF_DIR . $newPath)) {
                 break;
             }
             $i++;
         }
         if (!FileUtil::makePath(WCF_DIR . $newPath)) {
             $newPath = '';
         }
         if ($newPath) {
             $src = FileUtil::addTrailingSlash(WCF_DIR . $this->styleEditor->imagePath);
             $dst = WCF_DIR . $newPath;
             $dir = opendir($src);
             while (($file = readdir($dir)) !== false) {
                 if ($file != '.' && $file != '..' && !is_dir($file)) {
                     @copy($src . $file, $dst . $file);
                 }
             }
             closedir($dir);
         }
         $sql = "UPDATE\twcf" . WCF_N . "_style\n\t\t\t\tSET\timagePath = ?\n\t\t\t\tWHERE\tstyleID = ?";
         $statement = WCF::getDB()->prepareStatement($sql);
         $statement->execute(array($newPath, $newStyle->styleID));
     }
     StyleCacheBuilder::getInstance()->reset();
     return array('redirectURL' => LinkHandler::getInstance()->getLink('StyleEdit', array('id' => $newStyle->styleID)));
 }
Exemple #2
0
	/**
	 * Copies a style.
	 * 
	 * @return	array<string>
	 */
	public function copy() {
		// get unique style name
		$sql = "SELECT	styleName
			FROM	wcf".WCF_N."_style
			WHERE	styleName LIKE ?
				AND styleID <> ?";
		$statement = WCF::getDB()->prepareStatement($sql);
		$statement->execute(array(
			$this->styleEditor->styleName.'%',
			$this->styleEditor->styleID
		));
		$numbers = array();
		$regEx = new Regex('\((\d+)\)$');
		while ($row = $statement->fetchArray()) {
			$styleName = $row['styleName'];
			
			if ($regEx->match($styleName)) {
				$matches = $regEx->getMatches();
				
				// check if name matches the pattern 'styleName (x)'
				if ($styleName == $this->styleEditor->styleName . ' ('.$matches[1].')') {
					$numbers[] = $matches[1];
				}
			}
		}
		
		$number = (count($numbers)) ? max($numbers) + 1 : 2;
		$styleName = $this->styleEditor->styleName . ' ('.$number.')';
		
		// create the new style
		$newStyle = StyleEditor::create(array(
			'packageID' => PACKAGE_ID,
			'styleName' => $styleName,
			'templateGroupID' => $this->styleEditor->templateGroupID,
			'isDisabled' => 1, // newly created styles are disabled by default
			'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,
			'imagePath' => $this->styleEditor->imagePath
		));
		
		// copy style variables
		$sql = "INSERT INTO	wcf".WCF_N."_style_variable_value
					(styleID, variableID, variableValue)
			SELECT		".$newStyle->styleID." AS styleID, value.variableID, value.variableValue
			FROM		wcf".WCF_N."_style_variable_value value
			WHERE		value.styleID = ?";
		$statement = WCF::getDB()->prepareStatement($sql);
		$statement->execute(array($this->styleEditor->styleID));
		
		// copy preview image
		if ($this->styleEditor->image) {
			// get extension
			$fileExtension = StringUtil::substring($this->styleEditor->image, StringUtil::lastIndexOf($this->styleEditor->image, '.'));
			
			// copy existing preview image
			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 = ?
					WHERE	styleID = ?";
				$statement = WCF::getDB()->prepareStatement($sql);
				$statement->execute(array(
					'stylePreview-'.$newStyle->styleID.$fileExtension,
					$newStyle->styleID
				));
			}
		}
		
		return array(
			'redirectURL' => LinkHandler::getInstance()->getLink('StyleEdit', array('id' => $newStyle->styleID))
		);
	}