0
votes

How can I use this plugin (a fork of CakeDC) to search between dates?

https://github.com/dereuromark/search

I've read the doc but I could not do it. Even after I searched for only one date, I didn't succeed.

public $filterArgs = array(
    'my_date' => array('type' => 'like')
);

In the view:

echo $this->Form->input('my_date', array('div' => false, 'label' => 'Date', 'dateFormat'     => 'DMY', 'minYear' => date('Y') - 80));
1
you only got one date there - you will you ever be able to search between two dates this way? :) also you did not mention your cakephp version. please always do so.mark

1 Answers

3
votes

there's two ways to do a date search with CakeDC,

first : looking only for a specific year

in model

'creationdate'  => array('type' => 'like'),

in controller action :

    $this->Prg->commonProcess();

if(array_key_exists('creationdate',$this->passedArgs))
$this->passedArgs['creationdate'] = $this->passedArgs['creationdate']['year'];

and in view :

    echo $this->Form->input('creationdate', array(
    'label'             => __('Creation date'),
    'dateFormat'    => 'Y',
    'minYear'           => date('Y') - 100,
    'maxYear'           => date('Y'),
    'empty'             => true
    )
);

second way : looking for a range of dates

in model :

        'creationDateBetween'       => array(
            'type'      => 'expression',
            'method'    => 'CreationDateRangeCondition',
            'field'     => 'MODEL.creationdate BETWEEN ? AND ?',
        ),

public function CreationDateRangeCondition($data = array()){
    if(strpos($data['creationDateBetween'], ' - ') !== false){
        $tmp = explode(' - ', $data['creationDateBetween']);
        $tmp[0] = $tmp[0]."-01-01";
        $tmp[1] = $tmp[1]."-12-31";
        return $tmp;
    }else{
        return array($data['creationDateBetween']."-01-01", $data['creationDateBetween']."-12-31");
    }
}

in view : note that i'm using a slider for year range

    echo $this->Form->input('creationDateBetween', 
    array(
        'label'     => __('Creation date between X and Y'),
        'div'       => false,
        'style' => 'border: 0; color: #49AFCD; font-weight: bold;'
    )
);

?><div id="creationDateBetweenSlider" style="padding:0;"></div><?php





<script>
$(function() {


    var     creationDateBetweenSlider = $( "#creationDateBetweenSlider" ),
        institutionCreationDateBetween  = $( "#MODELCreationDateBetween" ), 

lock = 0;

creationDateBetweenSlider.slider({
                range: true,
                min: 1900,
                max: 2050,
                values: [ 2000, 2013 ],
                slide: function( event, ui ) {
                    MODELCreationDateBetween.val(  ui.values[ 0 ] + " - " + ui.values[ 1 ] );
                }
            });
        if(lock != 0) MODELCreationDateBetween.val( creationDateBetweenSlider.slider( "values", 0 ) + " - " + creationDateBetweenSlider.slider( "values", 1 ) );

lock = 1;

    });
</script>

Hope it helps !