Does this support date range from the result node? I have start and end date with multiple values, need to find nodes, that DO NOT have any entries for a date range. For example:
Node: Date fielde: value 1: start date:1.1.2015
end date: 10.1.2015
value 2: start date: 1.2.2015
end date: 20.2.2015

Contextual filter: NOT 2.2.2015--5.2.2015
so in this case, the first node would NOT pass as it has an entry for these dates.

Thanks

Comments

rdeboer’s picture

Not sure. Why don't you give it a go and report your findings here?

roman_pro’s picture

I spent couple of hours yesterday trying all posisble combinations but with no luck.

rdeboer’s picture

Thanks for trying and reporting back!

That doesn't come unexpected. The existing code is pretty generic. Not having looked at it for a long time, I was hoping it might work, but at the same time I never put any code in to specifically deal with your use-case.

I welcome patches and commissioned work.

roman_pro’s picture

The code is indeed to general to make it easily work. Instead I used hook_query_alter in my own module.

rdeboer’s picture

Would you like to share your hook implementation?

roman_pro’s picture

If it's useful for anyone, here you go:

function YOUR_MODULE_views_query_alter(&$view, &$query) {
  if ($view->name == 'search_properties') {

  if( ( ($view->current_display == 'landing_page_search') || ($view->current_display == 'landing_page_search_map') ) && (isset($view->args[5])) ){
        $dates = explode(' to ',$view->args[5]);
        if(count($dates) != 2){ /*dpm($dates,'dates not correct');*/ }
        $fromDate = strtotime($dates[0]);
        $toDate = strtotime($dates[1]);
        $nids = getAvailablepropsByDate($fromDate,$toDate);
        if (count($nids) > 0) {
                $condition = db_and()->condition('node.nid', $nids);
                          $query->add_where(0, $condition);
                }

    }
  }
}

function getAvailablepropsByDate($from,$to){
        $efq = new EntityFieldQuery();
         $qresult = $efq->entityCondition('entity_type', 'node')
                ->entityCondition('bundle', 'sls_property')
                ->propertyCondition('status', 1)
                ->execute();
        $all_nodes = $qresult['node'];

        $nids = FALSE;
        $query = "SELECT entity_id,field_availability_value,field_availability_value2 FROM field_data_field_availability WHERE bundle = 'sls_property'";
        $result = db_query($query)->fetchAll();
        foreach($result as $avail){
                $fromCurrent = strtotime($avail->field_availability_value);
                $toCurrent = strtotime($avail->field_availability_value2);
                if( ($toCurrent < $from) || ($fromCurrent > $to) ) {
                        //all ok
                }else{
                        if(isset($all_nodes[$avail->entity_id])){
                        //remove this node from the list
                        unset($all_nodes[$avail->entity_id]);
                        }
                }
        }



        return array_keys($all_nodes);
}
rdeboer’s picture

Thanks @roman_pro for sharing!
Rik

rdeboer’s picture

Status: Active » Closed (outdated)