Jump to content

Elchin

Members
  • Posts

    5
  • Joined

  • Last visited

Posts posted by Elchin

  1. 12 hours ago, Autofahrn said:

    ok, tested a little and the selectors seem to work for me, primarily tested with this one:

    
    $results = $page->children("!date_end|date_end>=$start,date<$start,sort=-date,limit=12");

    Did you try outputting your date and date_end fields unformatted to see if they contain proper values?

    This not work for me.

    The example below returns pages that have property `date_end` and `date_end`>=$start

    $results = $page->children("date_end>=$start,date<$start,sort=-date,limit=12");

    The example below returns pages that not have property `date_end` at all

    $results = $page->children("!date_end,date<$start,sort=-date,limit=12");

    But I can't get this pages with this two conditions together.

    I don't know why but the OR condition `!date_end|date_end>=$start` didn't worked in this case.

    11 hours ago, flydev said:

    Hi @Elchin

    I got them in the past but the query was to slow so I finally used a custom query to achieve that, you might be interested as I don't know the number of pages your trying to fetch - I give you a small module which could be extended, shipped with a hook function which return all pages between two dates given a fieldname and a template id :

     

      Reveal hidden contents
    
    
    <?php namespace ProcessWire;
    
    
    class MyPageHooks extends WireData implements Module {
    
      public static function getModuleInfo() {
    
          return array(
              'title'    => 'MyPageHooks',
              'version'  => '1.0.0',
              'summary'  => 'Adds new methods to $page',
              'singular' => true,
              'autoload' => true,
              'author'   => 'flydev',
              'href'     => 'https://processwire.com/talk/topic/21021-selector-where-now-between-start-date-and-end-date-or-now-bigger-than-start-date-and-end-date-is-empty/'
          );
      }
    
      public function init() {
        $this->addHook('Page::getPagesFromDateRange', $this, 'getPagesFromDateRange');
      }
    
      /**
      * Hook getPagesFromDateRange(string fieldName, string $dateStart, string $dateEnd, int $templateId)
      * date format: YYYY/MM/DD
      * 
      * Usage:
      * 
      * 
      *  Get pages between `dateStart` and `dateEnd` with template id 53 from fieldname 'date1'
      *  $page->getPagesFromDateRange('date1', '2019/01/01', '2019/03/11', 53);
      */
      public function getPagesFromDateRange($event) {
        $page = $event->object;
        $fieldname = $event->arguments(0);
        $dateStart = $this->sanitizer->datetime($event->arguments(1));
        $dateEnd = $this->sanitizer->datetime($event->arguments(2));
        $templateId = $this->sanitizer->datetime($event->arguments(3));
    
        $q = new DatabaseQuerySelect();
        $q->select("pages.id");
        $q->from("pages");
        $q->where("
          (pages.parent_id IN (SELECT pages_id FROM pages_parents WHERE parents_id={$page->id})) 
          AND (pages.templates_id={$templateId}) 
          AND(
          pages.id IN (
            SELECT pages.id 
            FROM `pages` 
            JOIN field_{$fieldname} AS dateStart ON dateStart.pages_id=pages.id AND (((DATE(dateStart.data)>='{$dateStart}'))) 
            JOIN field_{$fieldname} AS dateEnd ON dateEnd.pages_id=pages.id AND (((DATE(dateEnd.data)<='{$dateEnd}'))) 
          )   
        )");
    
        $stmt = $q->execute();
        $event->return = $stmt->fetchAll(\PDO::FETCH_ASSOC);
      }
    }

     

     

     

     

     

    How can I use paging in this case?

  2. Hi.

    I want select pages where now between date and end_date or now bigger than date and end_date is empty.

    I have five tried variants:

    
    $start = strtotime(date('Y-m-d') . " 00:00:00");
    $results = $page->children("foo=(date<$start,date_end=''),bar=(date<$start,date_end>=$start),sort=-date,limit=12");
    
    
    
    $start = strtotime(date('Y-m-d') . " 00:00:00");
    $results = $page->children("date<$start,(date_end='',date_end>=$start),sort=-date,limit=12");
    
    
    
    $start = strtotime(date('Y-m-d') . " 00:00:00");
    $results = $page->children("date_end=''|date_end>=$start,date<$start,sort=-date,limit=12");
    
    
    
    $start = strtotime(date('Y-m-d') . " 00:00:00");
    $results = $page->children("!date_end|date_end>=$start,date<$start,sort=-date,limit=12");
    
    
    
    $start = strtotime(date('Y-m-d') . " 00:00:00");
    $results = $page->children("date_end>=$start|!date_end,date<$start,sort=-date,limit=12");
    
    

    All this variants not worked for me and returned zero results.

×
×
  • Create New...