Author: jablko
Date: Thu Oct 28 04:02:28 2010
New Revision: 8629

Log:
Update date validator, fixes issue 1661

Modified:
   trunk/plugins/sfIsadPlugin/lib/QubitValidatorIsadDates.class.php

Modified: trunk/plugins/sfIsadPlugin/lib/QubitValidatorIsadDates.class.php
==============================================================================
--- trunk/plugins/sfIsadPlugin/lib/QubitValidatorIsadDates.class.php    Thu Oct 
28 03:35:09 2010        (r8628)
+++ trunk/plugins/sfIsadPlugin/lib/QubitValidatorIsadDates.class.php    Thu Oct 
28 04:02:28 2010        (r8629)
@@ -21,49 +21,42 @@
 {
   protected function doClean($value)
   {
-    foreach ($value->events as $event)
+    foreach ($value->events as $item)
     {
-      foreach ($value->ancestors as $ancestor)
+      $valid = true;
+
+      // Only validate this event if it has start or end date
+      if (isset($item->startDate) || isset($item->endDate))
       {
-        if (0 < count($ancestorEvents = $ancestor->getDates(array('type_id' => 
$event->type->id))))
+        // Find first ancestor with an event, with a start or end date, of same
+        // type as event we're validating
+        foreach ($value->ancestors->orderBy('rgt') as $ancestor)
         {
-          $validStartDate = $validEndDate = false;
-
-          foreach ($ancestorEvents as $ancestorEvent)
+          foreach ($ancestor->getDates(array('type_id' => $item->type->id)) as 
$event)
           {
-            if (isset($event->startDate))
-            {
-              if ((!isset($ancestor->startDate) || new 
DateTime($event->startDate) >= new DateTime($ancestor->startDate)) && 
(!isset($ancestor->endDate) || new DateTime($event->startDate) < new 
DateTime($ancestor->endDate)))
-              {
-                $validStartDate = true;
-              }
-            }
-            else
-            {
-              $validStartDate = true;
-            }
-
-            if (isset($endDate))
-            {
-              if ((!isset($ancestor->startDate) || new 
DateTime($event->endDate) > new DateTime($ancestor->startDate)) && 
(!isset($ancestor->endDate) || new DateTime($event->endDate) <= new 
DateTime($ancestor->endDate)))
-              {
-                $validEndDate = true;
-              }
-            }
-            else
-            {
-              $validEndDate = true;
-            }
+            // Found at least one such event, if event we're validating isn't
+            // valid according to at least one of this ancestor's events, then
+            // it's invalid
+            $valid = false;
 
-            if ($validStartDate && $validEndDate)
+            // Valid according to this event?  Start date is greater than or
+            // equal and end date is less than or equal, or start or end dates
+            // are missing
+            if ((!isset($item->startDate)
+                  || !isset($event->startDate)
+                  || new DateTime($item->startDate) >= new 
DateTime($event->startDate))
+                && (!isset($item->endDate)
+                  || !isset($event->endDate)
+                  || new DateTime($item->endDate) <= new 
DateTime($event->endDate)))
             {
-              break;
+              // Valid!  Check next event
+              continue 3;
             }
           }
 
-          // If current dates aren't within at least one of the ranges for this
-          // ancestor, then throw validation error
-          if (!$validStartDate || !$validEndDate)
+          // If event isn't in at least one of the ranges for this ancestor,
+          // then throw validation error
+          if (!$valid)
           {
             throw new sfValidatorError($this, 'invalid', array('ancestor' => 
sfContext::getInstance()->routing->generate(null, array($ancestor, 'module' => 
'informationobject'))));
           }

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to