[TASK] Show note if form contains too much fields
authorAlex Kellner <alexander.kellner@in2code.de>
Thu, 12 May 2016 14:03:16 +0000 (16:03 +0200)
committerAlex Kellner <alexander.kellner@in2code.de>
Thu, 12 May 2016 14:04:09 +0000 (16:04 +0200)
Add a userfunc that shows a note in the conditioncontainer record in backend

Classes/Tca/Note.php [new file with mode: 0644]
Configuration/TCA/tx_powermailcond_domain_model_conditioncontainer.php
Resources/Private/Language/locallang_db.xml
ext_tables.sql

diff --git a/Classes/Tca/Note.php b/Classes/Tca/Note.php
new file mode 100644 (file)
index 0000000..9574584
--- /dev/null
@@ -0,0 +1,117 @@
+<?php
+namespace In2code\PowermailCond\Tca;
+
+use In2code\Powermail\Domain\Model\Field;
+use In2code\Powermail\Domain\Model\Form;
+use In2code\Powermail\Domain\Model\Page;
+
+/**
+ * Show a note if a form is chosen that has too much fields
+ * 
+ * Class Note
+ * @package In2code\PowermailCond\Tca
+ */
+class Note
+{
+
+    /**
+     * @var \TYPO3\CMS\Core\Database\DatabaseConnection
+     */
+    protected $databaseConnection = null;
+
+    /**
+     * @var \TYPO3\CMS\Lang\LanguageService
+     */
+    protected $languageService = null;
+
+    /**
+     * Path to locallang file (with : as postfix)
+     *
+     * @var string
+     */
+    protected $locallangPath = 'LLL:EXT:powermail_cond/Resources/Private/Language/locallang_db.xml:';
+
+    /**
+     * @var int
+     */
+    protected $fieldLimit = 30;
+
+    /**
+     * @param array $params
+     * @return string
+     */
+    public function showNote(array $params)
+    {
+        $this->initialize();
+        $content = '';
+        if ($this->formHasTooMuchFields($params)) {
+            $content = '<div class="alert alert-warning"><strong>' . $this->getTitle() . '</strong> ' .
+                $this->getDescription() . '</div>';
+        }
+        return $content;
+    }
+
+    /**
+     * @param array $params
+     * @return bool
+     */
+    protected function formHasTooMuchFields(array $params)
+    {
+        $formUid = (int)$params['row']['form'][0];
+        return $formUid > 0 && $this->getNumberOfFieldsToForm($formUid) > $this->fieldLimit;
+    }
+
+    /**
+     * @param int $formUid
+     * @return int
+     */
+    protected function getNumberOfFieldsToForm($formUid)
+    {
+        $select = 'count(f.uid) sum';
+        $from = Field::TABLE_NAME . ' f ' .
+            'left join ' . Page::TABLE_NAME . ' p on f.pages = p.uid ' .
+            'left join ' . Form::TABLE_NAME . ' form on p.forms = form.uid';
+        $where = 'form.uid = ' . (int) $formUid . ' and f.hidden = 0 and f.deleted = 0 ' .
+            'and p.hidden = 0 and p.deleted = 0';
+        $res = $this->databaseConnection->exec_SELECTquery($select, $from, $where);
+        if ($res) {
+            $row = $this->databaseConnection->sql_fetch_assoc($res);
+            return (int)$row['sum'];
+        }
+        return 0;
+    }
+
+    /**
+     * @return string
+     */
+    protected function getTitle()
+    {
+        return $this->languageService->sL(
+            $this->locallangPath . 'tx_powermailcond_conditioncontainer.note.title',
+            true
+        );
+    }
+
+    /**
+     * @return string
+     */
+    protected function getDescription()
+    {
+        return $this->languageService->sL(
+            $this->locallangPath . 'tx_powermailcond_conditioncontainer.note.description',
+            true
+        );
+    }
+
+    /**
+     * Initialize some variables
+     *
+     * @return void
+     * @SuppressWarnings(PHPMD.Superglobals)
+     */
+    protected function initialize()
+    {
+        $this->languageService = $GLOBALS['LANG'];
+        $this->databaseConnection = $GLOBALS['TYPO3_DB'];
+    }
+}
index b0dd12b..106003f 100644 (file)
@@ -25,7 +25,7 @@ return [
             'endtime,title,form,conditions',
     ],
     'types' => [
-        '1' => ['showitem' => 'title, form, conditions'],
+        '1' => ['showitem' => 'title, form, conditions, note'],
     ],
     'palettes' => [
         '1' => [],
@@ -113,6 +113,13 @@ return [
                 ],
             ],
         ],
+        'note' => [
+            'exclude' => 1,
+            'config' => [
+                'type' => 'user',
+                'userFunc' => 'In2code\PowermailCond\Tca\Note->showNote'
+            ],
+        ],
         'title' => [
             'exclude' => 1,
             'label' => 'LLL:EXT:powermail_cond/Resources/Private/Language/locallang_db.xml:' .
index be0e538..ba54167 100644 (file)
@@ -11,6 +11,8 @@
                        <label index="tx_powermailcond_conditioncontainer.form">Form</label>
                        <label index="tx_powermailcond_conditioncontainer.form.pleaseChoose">Please choose...</label>
                        <label index="tx_powermailcond_conditioncontainer.conditions">Conditions</label>
+                       <label index="tx_powermailcond_conditioncontainer.note.title">Warning</label>
+                       <label index="tx_powermailcond_conditioncontainer.note.description">The linked form contains more then 30 fields. That could cause performance issues in the frontend.</label>
                        <label index="tx_powermailcond_conditions">Condition</label>
                        <label index="tx_powermailcond_conditions.title">Title</label>
                        <label index="tx_powermailcond_conditions.targetField">Which field is affected (target field)?</label>
@@ -54,6 +56,8 @@
                        <label index="tx_powermailcond_conditioncontainer.form">Formular</label>
                        <label index="tx_powermailcond_conditioncontainer.form.pleaseChoose">Bitte w√§hlen...</label>
                        <label index="tx_powermailcond_conditioncontainer.conditions">Bedingungen</label>
+                       <label index="tx_powermailcond_conditioncontainer.note.title">Warnung</label>
+                       <label index="tx_powermailcond_conditioncontainer.note.description">Das verlinkte Formular beinhalted mehr als 30 Felder. Hierbei kann es zu Performance-Problemen im Frontend kommen.</label>
                        <label index="tx_powermailcond_conditions">Bedingung</label>
                        <label index="tx_powermailcond_conditions.title">Bezeichnung</label>
                        <label index="tx_powermailcond_conditions.targetField">Welches Feld ist betroffen (Zielfeld)?</label>
@@ -92,4 +96,4 @@
                        <label index="tx_powermailcond_rules.condition">Bedingung</label>
                </languageKey>
        </data>
-</T3locallang>
\ No newline at end of file
+</T3locallang>
index 6ef2d9d..2bec430 100644 (file)
@@ -17,6 +17,7 @@ CREATE TABLE tx_powermailcond_domain_model_conditioncontainer (
        endtime int(11) DEFAULT '0' NOT NULL,\r
 \r
        conditions int(11) DEFAULT '0' NOT NULL,\r
+       note tinyint(4) DEFAULT '0' NOT NULL,\r
 \r
        title tinytext NOT NULL,\r
        form int(11) DEFAULT '0' NOT NULL,\r
@@ -79,4 +80,4 @@ CREATE TABLE tx_powermailcond_domain_model_rule (
        \r
        PRIMARY KEY (uid),\r
        KEY parent (pid)\r
-);
\ No newline at end of file
+);\r