/**
  * In the event that the type is BEFORE or AFTER, and the firstDate value is not populated, it will be treated
  * as null, and the search on this attribute will be ignored.  At some point in the future the search form
  * could have validation added, so that the empty firstDate combined with a type of BEFORE or AFTER would not
  * get this far, but for now this is the easiest approach to ensuring a valid BEFORE or AFTER value.
  * @param mixed $value
  * @return mixed
  */
 public static function resolveValueDataIntoUsableValue($value)
 {
     if (isset($value['type']) && $value['type'] != null) {
         $validValueTypes = static::getValidValueTypes();
         if (!in_array($value['type'], $validValueTypes)) {
             throw new NotSupportedException();
         }
         if ($value['type'] == self::TYPE_TODAY || $value['type'] == self::TYPE_BEFORE_TODAY) {
             return DateTimeCalculatorUtil::calculateNew(DateTimeCalculatorUtil::TODAY, new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
         } elseif ($value['type'] == self::TYPE_TOMORROW) {
             return DateTimeCalculatorUtil::calculateNew(DateTimeCalculatorUtil::TOMORROW, new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
         } elseif ($value['type'] == self::TYPE_YESTERDAY) {
             return DateTimeCalculatorUtil::calculateNew(DateTimeCalculatorUtil::YESTERDAY, new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
         } elseif ($value['type'] == self::TYPE_BEFORE || $value['type'] == self::TYPE_AFTER || $value['type'] == self::TYPE_ON) {
             if ($value["firstDate"] == null) {
                 return null;
             }
             return $value['firstDate'];
         } else {
             throw new NotSupportedException();
         }
     }
     return null;
 }
 /**
  * @expectedException NotSupportedException
  */
 public function testCalculateNewNotSupportedCalculation()
 {
     DateTimeCalculatorUtil::calculateNew(1231, new DateTime());
 }
 public function testSearchFormDynamicAttributesTodayAndBeforeTodayDateSearch()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $searchAttributes = array('date__Date' => array('type' => MixedDateTypesSearchFormAttributeMappingRules::TYPE_TODAY), 'date2__Date' => array('type' => MixedDateTypesSearchFormAttributeMappingRules::TYPE_BEFORE_TODAY));
     $searchForm = new MixedRelationsModelSearchFormTestModel(new MixedRelationsModel());
     $metadataAdapter = new SearchDataProviderMetadataAdapter($searchForm, $super->id, $searchAttributes);
     $metadata = $metadataAdapter->getAdaptedMetadata();
     $today = DateTimeCalculatorUtil::calculateNew(DateTimeCalculatorUtil::TODAY, new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
     $compareClauses = array(1 => array('attributeName' => 'date', 'operatorType' => 'equals', 'value' => $today), 2 => array('attributeName' => 'date2', 'operatorType' => 'lessThan', 'value' => $today));
     $compareStructure = '(1) and (2)';
     $this->assertEquals($compareClauses, $metadata['clauses']);
     $this->assertEquals($compareStructure, $metadata['structure']);
 }
 /**
  * Override to calculate the
  */
 protected function calculate()
 {
     if ($this->value != null) {
         return DateTimeCalculatorUtil::calculateNew((int) $this->value, new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
     }
 }