Add support for static boxes in PIP
authorMatthias Schmidt <gravatronics@live.com>
Sat, 30 Apr 2016 17:05:14 +0000 (19:05 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 30 Apr 2016 17:05:14 +0000 (19:05 +0200)
wcfsetup/install/files/lib/system/package/plugin/BoxPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php

index 33672ddc6976d1fc957c216ff3f812322540aaaf..fd01f9faa61abf0e3025255aa0d7e2bb880e18a6 100644 (file)
@@ -4,6 +4,7 @@ use wcf\data\box\Box;
 use wcf\data\box\BoxEditor;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\SystemException;
+use wcf\system\language\LanguageFactory;
 use wcf\system\WCF;
 
 /**
@@ -22,6 +23,12 @@ class BoxPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
         */
        public $className = BoxEditor::class;
        
+       /**
+        * box contents
+        * @var array
+        */
+       protected $content = [];
+       
        /**
         * @inheritDoc
         */
@@ -108,6 +115,7 @@ class BoxPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
         * @throws      SystemException
         */
        protected function prepareImport(array $data) {
+               $content = [];
                $boxType = $data['elements']['boxType'];
                $controller = '';
                $identifier = $data['attributes']['identifier'];
@@ -147,6 +155,8 @@ class BoxPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
                                        }
                                }
                                
+                               $content = $data['elements']['content'];
+                               
                                break;
                        
                        default:
@@ -160,6 +170,7 @@ class BoxPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
                
                return [
                        'identifier' => $identifier,
+                       'content' => $content,
                        'name' => $this->getI18nValues($data['elements']['name'], true),
                        'boxType' => $boxType,
                        'position' => $position,
@@ -215,19 +226,57 @@ class BoxPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
         * @inheritDoc
         */
        protected function import(array $row, array $data) {
+               // extract content
+               $content = $data['content'];
+               unset($data['content']);
+               
                // updating boxes is only supported for 'system' type boxes, all other
                // types would potentially overwrite changes made by the user if updated
                if (!empty($row) && $row['boxType'] !== 'system') {
-                       return new Box(null, $row);
+                       $box = new Box(null, $row);
+               }
+               else {
+                       $box = parent::import($row, $data);
                }
                
-               return parent::import($row, $data);
+               // store content for later import
+               $this->content[$box->boxID] = $content;
+               
+               return $box;
        }
        
        /**
         * @inheritDoc
         */
        protected function postImport() {
+               if (!empty($this->content)) {
+                       $sql = "INSERT IGNORE INTO      wcf".WCF_N."_box_content
+                                                       (boxID, languageID, title, content)
+                               VALUES                  (?, ?, ?, ?)";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       
+                       WCF::getDB()->beginTransaction();
+                       foreach ($this->content as $boxID => $contentData) {
+                               foreach ($contentData as $languageCode => $content) {
+                                       $languageID = null;
+                                       if ($languageCode != '') {
+                                               $language = LanguageFactory::getInstance()->getLanguageByCode($languageCode);
+                                               if ($language === null) continue;
+                                               
+                                               $languageID = $language->languageID;
+                                       }
+                                       
+                                       $statement->execute([
+                                               $boxID,
+                                               $languageID,
+                                               $content['title'],
+                                               isset($content['content']) ? $content['content'] : ''
+                                       ]);
+                               }
+                       }
+                       WCF::getDB()->commitTransaction();
+               }
+               
                if (empty($this->visibilityExceptions)) return;
                
                // get all boxes belonging to the identifiers
@@ -248,7 +297,7 @@ class BoxPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
                // save visibility exceptions
                $sql = "DELETE FROM     wcf".WCF_N."_box_to_page
                        WHERE           boxID = ?";
-               $deleteStatement = WCF::getDB()->prepareStatement($sql);                
+               $deleteStatement = WCF::getDB()->prepareStatement($sql);
                $sql = "INSERT IGNORE   wcf".WCF_N."_box_to_page
                                        (boxID, pageID, visible)
                        VALUES          (?, ?, ?)";
index 158d9a7cf63c72c6d38e9a77331abfd4a2f0d4d5..49149edc6f107cdd8b1dab19d9263c609c6c147a 100644 (file)
@@ -162,7 +162,7 @@ class PagePackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
                        
                        case 'html':
                        case 'text':
-                       case 'tpl':     
+                       case 'tpl':
                                if (empty($data['elements']['content'])) {
                                        throw new SystemException("Missing required 'content' element(s) for page '{$identifier}'");
                                }
@@ -262,6 +262,8 @@ class PagePackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
                
                // store content for later import
                $this->content[$object->pageID] = $content;
+               
+               return $object;
        }
        
        /**