From 55dcd3a199149beaff4dbe0b0cc68d5e8eebe562 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Wed, 4 Oct 2017 14:35:27 +0200
Subject: [PATCH] feat: quiqqer/package-bricks#38. quiqqer/package-bricks#51 -
 Bautsein UniqueIds

---
 events.xml                |  5 +--
 patches/uniqueIds.php     | 71 +++++++++++++++++++++++++++++++++++++++
 src/QUI/Bricks/Events.php | 10 ++++++
 3 files changed, 84 insertions(+), 2 deletions(-)
 create mode 100644 patches/uniqueIds.php

diff --git a/events.xml b/events.xml
index 8e28176..97d3fbe 100644
--- a/events.xml
+++ b/events.xml
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <events>
-    <event on="onSiteSave" fire="\QUI\Bricks\Events::onSiteSave" />
-    <event on="onSmartyInit" fire="\QUI\Bricks\Events::onSmartyInit" />
+    <event on="onSiteSave" fire="\QUI\Bricks\Events::onSiteSave"/>
+    <event on="onSmartyInit" fire="\QUI\Bricks\Events::onSmartyInit"/>
+    <event on="onPackageSetup" fire="\QUI\Bricks\Events::onPackageSetup"/>
 </events>
\ No newline at end of file
diff --git a/patches/uniqueIds.php b/patches/uniqueIds.php
new file mode 100644
index 0000000..4a75145
--- /dev/null
+++ b/patches/uniqueIds.php
@@ -0,0 +1,71 @@
+<?php
+
+define('QUIQQER_SYSTEM', true);
+require dirname(dirname(dirname(dirname(__FILE__)))).'/header.php';
+
+// workaround for older patch
+$Bricks = QUI\Bricks\Manager::init();
+$result = QUI::getDataBase()->fetch(array(
+    'count' => 'count',
+    'from'  => $Bricks->getUIDTable(),
+));
+
+// if unique ids already exist, the pages no longer have to be passed through
+if (isset($result[0]) && isset($result[0]['count']) && $result[0]['count']) {
+    echo 'Already executed'.PHP_EOL;
+
+    return;
+}
+
+$projects = QUI::getProjectManager()->getProjectList();
+
+// helper to look if areas are empty
+$areasEmpty = function ($areas) {
+    if (!is_array($areas)) {
+        return true;
+    }
+
+    foreach ($areas as $area => $data) {
+        if (!empty($data)) {
+            return false;
+        }
+    }
+
+    return true;
+};
+
+echo PHP_EOL;
+
+/* @var $Project QUI\Projects\Project */
+foreach ($projects as $Project) {
+    $ids = $Project->getSitesIds();
+
+    foreach ($ids as $id) {
+        $id = $id['id'];
+
+        try {
+            $Site  = $Project->get($id);
+            $areas = $Site->getAttribute('quiqqer.bricks.areas');
+
+            if ($areas === false) {
+                continue;
+            }
+
+            $areas = json_decode($areas, true);
+
+            if ($areasEmpty($areas)) {
+                continue;
+            }
+
+            echo $Project->getName().'-'.$Project->getLang().'-'.$id.PHP_EOL;
+
+            $Edit = $Site->getEdit();
+            $Edit->save(QUI::getUsers()->getSystemUser());
+        } catch (QUI\Exception $Exception) {
+            continue;
+        }
+    }
+
+    unset($Edit);
+    unset($Project);
+}
diff --git a/src/QUI/Bricks/Events.php b/src/QUI/Bricks/Events.php
index 40ca687..8c70aae 100644
--- a/src/QUI/Bricks/Events.php
+++ b/src/QUI/Bricks/Events.php
@@ -202,4 +202,14 @@ public static function brickarea($params, $smarty)
 
         return '';
     }
+
+    /**
+     * @param QUI\Package\Package $Package
+     */
+    public static function onPackageSetup(QUI\Package\Package $Package)
+    {
+        if ($Package->getName() !== 'quiqqer/bricks') {
+            return;
+        }
+    }
 }
-- 
GitLab