[TASK] Implement the application logic
authorOliver Eglseder <oliver.eglseder@in2code.de>
Thu, 3 Sep 2015 11:47:17 +0000 (13:47 +0200)
committerOliver Eglseder <oliver.eglseder@in2code.de>
Thu, 3 Sep 2015 11:47:17 +0000 (13:47 +0200)
Classes/Controller/ConditionController.php
Classes/Domain/Comparator/Comparator.php [new file with mode: 0644]
Classes/Domain/Model/Condition.php
Classes/Domain/Model/ConditionContainer.php
Classes/Domain/Model/Rule.php

index a7bbf44..bc783a9 100644 (file)
@@ -1,7 +1,11 @@
 <?php
 namespace In2code\PowermailCond\Controller;
 
+use In2code\Powermail\Domain\Model\Field;
+use In2code\Powermail\Domain\Model\Form;
+use In2code\Powermail\Domain\Model\Page;
 use In2code\PowermailCond\Domain\Model\ConditionContainer;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
 
 /***************************************************************
@@ -52,16 +56,25 @@ class ConditionController extends ActionController {
        /**
         * Build Condition for AJAX call
         *
-        * @return void
+        * @return string
         */
        public function buildConditionAction() {
-               $form = $this->formRepository->findByUid(154);
-               /** @var ConditionContainer $container */
-               $container = $this->objectManager->get(
-                       'In2code\\PowermailCond\\Domain\\Model\\ConditionContainer',
-                       $this->conditionRepository->findByForm($form)
-               );
-               \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($container, 'in2code: ' . __CLASS__ . ':' . __LINE__);
-//             $container->applyConditions($form);
+               $arguments = GeneralUtility::_GP('tx_powermail_pi1');
+               /** @var Form $formObject */
+               $formObject = $this->formRepository->findByIdentifier($arguments['mail']['form']);
+               /** @var Page $page */
+               foreach ($formObject->getPages() as $page) {
+                       /** @var Field $field */
+                       foreach ($page->getFields() as $field) {
+                               foreach ($arguments['field'] as $fieldName => $fieldValue) {
+                                       if ($field->getMarker() === $fieldName) {
+                                               $field->setText($fieldValue);
+                                       }
+                               }
+                       }
+               }
+               $conditionsContainer = new ConditionContainer($this->conditionRepository->findByForm($formObject));
+               $arguments = $conditionsContainer->applyConditions($formObject, $arguments);
+               return json_encode($arguments);
        }
-}
\ No newline at end of file
+}
diff --git a/Classes/Domain/Comparator/Comparator.php b/Classes/Domain/Comparator/Comparator.php
new file mode 100644 (file)
index 0000000..141f76d
--- /dev/null
@@ -0,0 +1,120 @@
+<?php
+namespace In2code\PowermailCond\Domain\Comparator;
+
+use In2code\PowermailCond\Domain\Model\Rule;
+
+/**
+ * Class Comparator
+ */
+class Comparator {
+
+       /**
+        * @var callable[]
+        */
+       protected $callbacks = array();
+
+       /**
+        *
+        */
+       public function __construct() {
+               $this->callbacks = array(
+                       Rule::OPERATOR_IS_SET => array($this, 'operationIsSet'),
+                       Rule::OPERATOR_NOT_IS_SET => array($this, 'operationNotIsSet'),
+                       Rule::OPERATOR_CONTAINS_VALUE => array($this, 'operationContainsValue'),
+                       Rule::OPERATOR_NOT_CONTAINS_VALUE => array($this, 'operationNotContainsValue'),
+                       Rule::OPERATOR_IS => array($this, 'operationIs'),
+                       Rule::OPERATOR_NOT_IS => array($this, 'operationNotIs'),
+                       Rule::OPERATOR_GREATER_THAN => array($this, 'operationGreaterThan'),
+                       Rule::OPERATOR_LESS_THAN => array($this, 'operationLessThan'),
+                       Rule::OPERATOR_CONTAINS_VALUE_FROM_FIELD => array($this, 'operationContainsValueFromField'),
+                       Rule::OPERATOR_NOT_CONTAINS_VALUE_FROM_FIELD => array($this, 'operationNotContainsValueFromField'),
+               );
+       }
+
+       /**
+        * @param int $operator
+        * @return callable
+        */
+       public function getCallbackForOperator($operator) {
+               return $this->callbacks[$operator];
+       }
+
+       /**
+        * @param string $left
+        * @return bool
+        */
+       public function operationIsSet($left) {
+               return !$this->operationNotIsSet($left);
+       }
+
+       /**
+        * @param string $left
+        * @return bool
+        */
+       public function operationNotIsSet($left) {
+               return empty($left);
+       }
+
+       /**
+        * @param string $left
+        * @param string $right
+        * @return bool
+        */
+       public function operationContainsValue($left, $right) {
+               return (strpos($left, $right) !== FALSE);
+       }
+
+       /**
+        * @param string $left
+        * @param string $right
+        * @return bool
+        */
+       public function operationNotContainsValue($left, $right) {
+               return !$this->operationContainsValue($left, $right);
+       }
+
+       /**
+        * @param string $left
+        * @param string $right
+        * @return bool
+        */
+       public function operationIs($left, $right) {
+               return ($left === $right);
+       }
+
+       /**
+        * @param string $left
+        * @param string $right
+        * @return bool
+        */
+       public function operationNotIs($left, $right) {
+               return ($left !== $right);
+       }
+
+       /**
+        * @param string|int $left
+        * @param string|int $right
+        * @return bool
+        */
+       public function operationGreaterThan($left, $right) {
+               return (int) $left > (int) $right;
+       }
+
+       /**
+        * @param string|int $left
+        * @param string|int $right
+        * @return bool
+        */
+       public function operationLessThan($left, $right) {
+               return (int) $left < (int) $right;
+       }
+
+
+       public function operationContainsValueFromField($left, $right) {
+               throw new \Exception('NOT YET IMPLEMENTED', 1441276049);
+       }
+
+       public function operationNotContainsValueFromField($left, $right) {
+               throw new \Exception('NOT YET IMPLEMENTED', 1441276053);
+       }
+}
index 4879497..6cec52e 100644 (file)
@@ -224,15 +224,51 @@ class Condition extends AbstractEntity {
                return $this;
        }
 
-//     public function applies($form) {
-//             foreach ($this->rules as $rule) {
-//                     if ($rule->applies($form)) {
-//                             return true;
-//                     }
-//             }
-//             return false;
-//     }
-//     public function applyOnForm($form) {
-//             $this->doStuff($form);
-//     }
-}
\ No newline at end of file
+       /**
+        * @param Form $form
+        * @return bool
+        */
+       public function applies(Form $form) {
+               if ($this->conjunction === self::CONJUNCTION_OR) {
+                       /** @var Rule $rule */
+                       foreach ($this->rules as $rule) {
+                               if ($rule->applies($form)) {
+                                       return TRUE;
+                               }
+                       }
+               } elseif ($this->conjunction === self::CONJUNCTION_AND) {
+                       /** @var Rule $rule */
+                       foreach ($this->rules as $rule) {
+                               if (!$rule->applies($form)) {
+                                       return FALSE;
+                               }
+                       }
+                       return TRUE;
+               }
+               return FALSE;
+       }
+
+       /**
+        * @param Form $form
+        * @param array $arguments
+        * @return array
+        */
+       public function apply(Form $form, array $arguments) {
+               $affectedFieldMarker = '';
+               /** @var Page $page */
+               foreach ($form->getPages() as $page) {
+                       /** @var Field $field */
+                       foreach ($page->getFields() as $field) {
+                               if ($field->getUid() === (int) $this->targetField) {
+                                       $affectedFieldMarker = $field->getMarker();
+                                       break;
+                               }
+                       }
+               }
+               if ($affectedFieldMarker !== '') {
+                       $arguments['todo'][$affectedFieldMarker] = $this->actionNumberMap[$this->actions];
+               }
+               return $arguments;
+       }
+
+}
index b484f2b..3b6764c 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace In2code\PowermailCond\Domain\Model;
 
+use In2code\Powermail\Domain\Model\Form;
 use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
 
 /***************************************************************
@@ -37,37 +38,47 @@ use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
 class ConditionContainer {
 
        /**
+        * @var int
+        */
+       protected $loopCount = 0;
+
+       /**
         * @var Condition[]
         */
        protected $conditions = array();
 
-
        /**
         * @var bool
         */
        protected $somethingChanged = TRUE;
 
        /**
-        * @param array $conditions
+        * @param QueryResult $conditions
         */
        public function __construct(QueryResult $conditions) {
                $this->conditions = $conditions;
        }
 
        /**
-        * @param $form
+        * @param Form $form
+        * @param array $arguments
+        * @return Form
         */
-       public function applyConditions($form) {
-               while ($this->somethingChanged && $this->loop < 100) {
+       public function applyConditions(Form $form, array $arguments) {
+               while ($this->somethingChanged && $this->loopCount < 100) {
                        $this->somethingChanged = FALSE;
-                       $this->loop++;
+                       $this->loopCount++;
                        foreach ($this->conditions as $condition) {
-                               while ($condition->applies($form)) {
-                                       $this->somethingChanged = TRUE;
-                                       $condition->applyOnForm($form);
+                               if ($condition->applies($form)) {
+                                       $newArguments = $condition->apply($form, $arguments);
+                                       if ($newArguments !== $arguments) {
+                                               $this->somethingChanged = TRUE;
+                                       }
+                                       $arguments = $newArguments;
                                }
                        }
                }
+               return $arguments;
        }
 
-}
\ No newline at end of file
+}
index a9c925d..a5c4fd5 100644 (file)
@@ -2,6 +2,9 @@
 namespace In2code\PowermailCond\Domain\Model;
 
 use In2code\Powermail\Domain\Model\Field;
+use In2code\Powermail\Domain\Model\Form;
+use In2code\Powermail\Domain\Model\Page;
+use In2code\PowermailCond\Domain\Comparator\Comparator;
 use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
 
 /***************************************************************
@@ -167,4 +170,25 @@ class Rule extends AbstractEntity {
                $this->equalField = $equalField;
                return $this;
        }
-}
\ No newline at end of file
+
+       /**
+        * @param Form $form
+        * @return bool
+        */
+       public function applies(Form $form) {
+               $comparator = new Comparator();
+               /** @var Page $page */
+               foreach ($form->getPages() as $page) {
+                       /** @var Field $field */
+                       foreach ($page->getFields() as $field) {
+                               if ($field === $this->startField) {
+                                       $comparison = $comparator->getCallbackForOperator($this->ops);
+                                       if ($comparison($field->getText(), $this->condString)) {
+                                               return TRUE;
+                                       }
+                               }
+                       }
+               }
+               return FALSE;
+       }
+}